Crate rsjs [−] [src]
rsjs is a small library which attempts to provide a convenient way to interface the Rust and JavaScript worlds.
It currently requires using the rust nightly channel and either the asmjs-unknown-emscripten
or wasm-unknown-emscripten
targets. See the README for any help setting up a development
environment.
Javascript helpers
RSJS
provides a number of helper JavaScript functions to store and convert JavaScript objects to be
used from Rust. All of these can be accessed through the JavaScript global RSJS
after init
has
been called.
RSJS
global
A global JavaScript object named RSJS
is created by init
. It contains all the helper JavaScript
functions as well as an object table to keep the JavaScript objects that are held by Rust code. See
JSObject
for more details.
RSJS.loadObject(index)
Loads a JavaScript object from the object table and returns it.
RSJS.storeObject(js_object)
Stores an object into the object table and returns the index. The result is commonly wrapped into
a JSObject
by js_obj!
.
Private helper functions
RSJS.releaseObject(index)
Removes an object from the object table. For use in the Drop
implementation of JSObject
only.
Releasing an object that is still refered to by a JSObject
will cause problems.
RSJS.copyStringToHeap(js_string)
Copy a JavaScript string into the Rust heap and returns the address. The string is stored as a 32-bit unsigned integer containing the number of 16-bit code units (not bytes or characters!) in the buffer, followed by that number of 16-bit code units from the UTF-16 string.
Used by js_string!
and the implementation of std::convert::From<JSObject> for String
.
RSJS.copyStringFromHeap(pointer)
Convert a String stored on the Rust heap (as described in copyStringToHeap
) into a JavaScript
string. The memory is freed after conversion, so the pointer should be considered invalid by the caller.
Modules
emscripten |
This module declares C functions provided by either emscripten or the C standard library. These are all unsafe, and most are described in the emscripten documentation. You should probably avoid using these directly. |
Macros
__js_macro |
Helper macro used by |
js |
Macro that evaluates a JavaScript code snippet with no return value and takes any number of arguments. |
js_double |
Macro that evaluates a JavaScript code snippet which returns a floating point number. |
js_int |
Macro that evaluates a JavaScript code snippet which returns an integer. |
js_obj |
Macro that evaluates a JavaScript code snippet which returns a JavaScript object. |
js_string |
Macro that evaluates a JavaScript code snippet which returns a string. |
Structs
JSObject |
A reference to a JavaScript object. |
Functions
init |
Initializes the JavaScript RSJS global object and helper functions. Should be called before using any other functions or macros from this crate. |
js_eval |
Run a snippet of JavaScript code. |