Open vmx opened 5 years ago
I ended up digging into Serde a bit more https://serde.rs/ and it seems to be where a lot of the early work we’d need to do probably will be related to Serde in order to get dag-cbor
and dag-json
codecs implemented.
With that in mind, it might make sense for us to use the Cloudflare’s serde-wasm-bindgen
Some more digging, I think all that we need to do in order to get dag-cbor
working in Rust is to add a custom format aware serializer/deserializer to CID similar to this datetime example https://github.com/alexcrichton/toml-rs/blob/master/src/datetime.rs
dag-json
might be a little trickier since we have to deal with our custom binary representation.
This is the result of some research in regards to passing JavaScript data into WebAssembly.
Currently the best integration between a language that targets WASM and a language outside WASM (a host language) is probably between Rust and JavaScript.
wasm-bindgen takes care of passing values from JavaScript into Rust and back. I'll concentrate on using JavaScript values with Rust as that's the most important case for IPLD at the moment. You would use serde for the conversion. Currently there are two implementations for that.
Built-in conversion
If you pass JSON from JS into Rust, it gets converted into a string first (via
JSON.stringify()
) and then parsed into Rust structs via serde. For more details see the wasm-bindgen source code.External crate
There is also a create from Cloudfare called serde-wasm-bindgen. It takes a different approach. It doesn't really serialize the JavaScript value, but calls JavaScript native methods directly on the objects that were created in the JavaScript space.
Web IDL Bindings
The Web IDL Bindings Proposal defines basic types and expressions. With those you can represent UTF-8 strings, Dictionaries (ordered maps), Typed Array Views (for binary data), fixed size values (with the
as
expression) like integers, floats, booleans and nulls.Sequences need to have a single type, but it could be of type
Any
.Dictionaries could be used to encode JSON, though JSON is out of scope for the Web IDL Bindings.
The only missing one (obviously) is the IPLD Link kind. That one could be represented as binary data.
It's interesting to note that Web IDL is not directly related to JavaScript.
Potentially there could also be IDLs for other languages which could then be supported by WASM, see the Web IDL FAQ for more information.
Structs in WASM
Typed Objects are needed for the GC v1 JS API, which also defines struct access within WASM. This struct access could potentially be used for IPLD Data Model Maps. Though it sounds like really early days (Typed Objects are not even a thing yet), so I would just ignore that one.