-
Notifications
You must be signed in to change notification settings - Fork 51
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
DiplomatOption support for JS #637
Conversation
Note to self: unions are always compiled to |
Not easy, actually. We don't currently codegen JS-to-Rust code for writing to a segment of memory formatted that way. ugh. |
I could cheat by just disallowing |
Playing with endianness: #[repr(C)]
#[derive(Debug)]
pub struct MyStruct {
x: u8,
y: u16,
z: u32,
}
#[no_mangle]
pub extern "C" fn opt(x: DiplomatOption<MyStruct>) {
let val = unsafe { x.value.ok };
log(&format!("{val:#x?}"));
} wasm.opt(0x12345678, 0x9ABCDEF0); Produces MyStruct {
x: 0x78,
y: 0x1234,
z: 0x9abcdef0,
} The toFFI for MyStruct would produce |
The union ABI stuff changes how this needs to be done, stashing old code at the old-js-option branch, and will start this branch anew. |
88b0c62
to
3ab2f4c
Compare
3ab2f4c
to
6e8d883
Compare
And we're actually ready! Would love a review from @ambiguousname if you can get to it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to do the job just fine, and this looks good to me. The context handling in rust is very clever, and I'm glad things are working there without too much hassle.
There is some unavoidable mess though. My biggest fears are with the really long helper functions (like writeOptionToArrayBuffer
and optionToArgsForCalling
). I'm glad they're documented, and most of the mess seems to stem from the current WASM ABI. So once we update to the new ABI, hopefully we can untangle some of that. My dream would be a more readable JS backend, which I believe means redesigning functions like _intoFFI
or _writeToArrayBuffer
(per my comments on #668).
As a short term fix, maybe we could group optionToArgsForCalling
, readOption
, and writeOptionToArrayBuffer
into one helper class? But that's a minor nitpick.
So, to summarize: this looks good, but I believe the JS backend feels so convoluted from handling edge cases (amplified with the current ABI and the new type). Tests will improve our ability to detect these, and hopefully changing to the new ABI in the future will help.
Note that the new ABI basically would be entirely dependent on |
Ultimately I think the class wouldn't actually wrap anything, I'm overall fine with having a bunch of free functions floating around. Classes help when they have state, or a coherent lifecycle, but these things all get called at different times. |
Next steps for #246
Fixes #659