Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map JS Types to Rust #5

Open
yoshuawuyts opened this issue Nov 27, 2017 · 5 comments
Open

Map JS Types to Rust #5

yoshuawuyts opened this issue Nov 27, 2017 · 5 comments

Comments

@yoshuawuyts
Copy link
Member

yoshuawuyts commented Nov 27, 2017

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
Copy link
Member Author

@killercup
Copy link

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

@steveklabnik
Copy link

Looks like @killercup beat me to it, hehe

@yoshuawuyts
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants