rustify icon indicating copy to clipboard operation
rustify copied to clipboard

Map JS Types to Rust

Open yoshuawuyts opened this issue 8 years ago • 5 comments

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

  • https://www.reddit.com/r/rust/comments/7fqvif/rust_webassembly_transform_for_browserify/
  • https://github.com/killercup/wasm-experiments

yoshuawuyts avatar Nov 27 '17 12:11 yoshuawuyts

Here is the complete mappings file:

  • https://github.com/killercup/wasm-experiments/blob/master/src/type-converter.js

yoshuawuyts avatar Nov 27 '17 13:11 yoshuawuyts

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

killercup avatar Nov 29 '17 14:11 killercup

Looks like @killercup beat me to it, hehe

steveklabnik avatar Nov 29 '17 14:11 steveklabnik

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 avatar Nov 29 '17 15:11 yoshuawuyts

More links!

  • https://www.steveklabnik.com/wasm/demos/semver.html
  • https://github.com/cretz/asmble/blob/8b51e14c330a562bb09c8e938a5c687cbdc962ab/examples/rust-string/src/lib.rs#L32
  • https://www.reddit.com/r/rust/comments/7ge5tt/semver_on_the_web_with_rust_and_webassembly/

yoshuawuyts avatar Nov 30 '17 02:11 yoshuawuyts