browserify / rustify

Rust WebAssembly transform for Browserify
Apache License 2.0
494 stars 25 forks source link

Map JS Types to Rust #5

Open yoshuawuyts opened 6 years ago

yoshuawuyts commented 6 years ago

Currently we only support passing i32 integers — great for math, but having more options would be great! We should probably look into rust-experiments and add some of the things they're doing.

Also s/o to @killercup for giving pointers to get in the right direction! 🙏

See Also

yoshuawuyts commented 6 years ago

Here is the complete mappings file:

killercup commented 6 years ago

Some more thoughts: https://www.reddit.com/r/rust/comments/7ft2of/whats_everyone_working_on_this_week_482017/dqefqg1/

steveklabnik commented 6 years ago

Looks like @killercup beat me to it, hehe

yoshuawuyts commented 6 years ago

Ohh, so if I understand things correctly (which is errr, not necessarily the case), then most of the trouble will be to map Strings (utf16) to utf8-array buffers. This is mostly what I've understood from the post above.

Complex JS types

Things like Objects, and the other higher-order JS things might need to be mapped down to strings before they can be passed (or, like json). There's been talk of using protobufs, but it seems most people are rather "meh" about it.

Mapping strings

Mapping utf16 to utf8 can either be done in Rust or in JS. I'm not sure about the Rust version, but the JS version seems to be:

Browser

var encode = (new TextEncoder('utf-8')).encode
console.log(encode('hello world')
// => [Uint8Array]

var decode = (new TextDecoder('utf-8')).decode
console.log(decode(SomeUint8Array)
// =>'hello world'

Node

var TextEncoder = require('util').TextEncoder
var TextDecoder = require('util').TextDecoder

var encode = (new TextEncoder('utf-8')).encode
console.log(encode('hello world')
// => [Uint8Array]

var decode = (new TextDecoder('utf-8')).decode
console.log(decode(SomeUint8Array)
// =>'hello world'

Rust

This should also be possible from within Rust, as the code is rather fast (and small) thanks to optimizations. There's a possibility of creating custom attributes such as #[expose_wasm] to help streamline mappings in Rust, but it seems to rely on experimental Nightly features and nobody seems to have tried it yet.

Shared memory

Now in an ideal scenario, we could use SharedArrayBuffers to allocate memory in JS, and then pass it to Rust. I'm not sure if this is feasible, but it'd be neat if it were the case.

yoshuawuyts commented 6 years ago

More links!