#interfacing #emscripten #javascript #rsjs

nightly rsjs

Makes interfacing Rust and Javascript easier

3 releases

Uses old Rust 2015

0.1.2 Aug 11, 2017
0.1.1 Aug 9, 2017
0.1.0 Aug 4, 2017

#1162 in WebAssembly

22 downloads per month

Apache-2.0

26KB
334 lines

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.

No runtime deps