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.


Loads a JavaScript object from the object table and returns it.


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


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.


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.


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.



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.



Helper macro used by js!, js_int!, js_double!, js_string! or js_obj!.


Macro that evaluates a JavaScript code snippet with no return value and takes any number of arguments.


Macro that evaluates a JavaScript code snippet which returns a floating point number.


Macro that evaluates a JavaScript code snippet which returns an integer.


Macro that evaluates a JavaScript code snippet which returns a JavaScript object.


Macro that evaluates a JavaScript code snippet which returns a string.



A reference to a JavaScript object.



Initializes the JavaScript RSJS global object and helper functions. Should be called before using any other functions or macros from this crate.


Run a snippet of JavaScript code.