-
Notifications
You must be signed in to change notification settings - Fork 45
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
Support for RefFromWasmAbi
and RefMutFromWasmAbi
#3
Comments
It is possible to implement What do you think? |
I could see the concern about hiding the cost, and I think you're probably right that this isn't worth it. I was still learning a fair bit about I also was reading something about how passing by value can cause issues with garbage collection if things are build with I could still see a use for the |
As a premise, I do not have a good understanding of The Therefore, this description is possible. #[derive(Tsify)]
pub struct MyWasmObj;
#[wasm_bindgen]
pub fn modify_some_object(my_wasm_obj: &<MyWasmObj as Tsify>::JsType) {
js_sys::Reflect::set(my_wasm_obj, &"property_key".into(), &"value".into()).unwrap();
} In this case, of course, no json conversion occurs and not even It may not be impossible to combine this behavior with |
You make a good point, I'm not sure it'd be worth the attempt to create that abstraction, it might hide a lot of complexity.
means that the memory of the object lives within WASM and you can access it via methods. This is mostly fine for things like classes or things with functionality, but I'm actually using tsify because I specifically want plain JS objects (no methods or anything). So it would have been nice if there was an easy way to get plain (but typed) JS objects, that you can also easily mutate from Rust. |
Come to think of it, implementing It seems to me that struct TsifyRefMut<T: Serialize + DeserializeOwned> {
js: ManuallyDrop<JsValue>,
owner: T,
}
impl<T: Serialize + DeserializeOwned> TsifyRefMut<T> {
unsafe fn new(js: <JsValue as RefFromWasmAbi>::Abi) -> Self {
let js = JsValue::ref_from_abi(js);
let owner: T = js.into_serde().unwrap();
Self { js, owner }
}
}
impl<T: Serialize + DeserializeOwned> Drop for TsifyRefMut<T> {
fn drop(&mut self) {
let target = js_sys::Object::try_from(&self.js).unwrap();
let output = JsValue::from_serde(&self.owner).unwrap();
let source = js_sys::Object::try_from(&output).unwrap();
js_sys::Object::assign(target, source);
}
}
impl RefMutFromWasmAbi for Foo {
type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = TsifyRefMut<Self>;
unsafe fn ref_mut_from_abi(js: Self::Abi) -> Self::Anchor {
TsifyRefMut::new(js)
}
}
impl<T: Serialize + DeserializeOwned> Deref for TsifyRefMut<T> {
...
}
impl<T: Serialize + DeserializeOwned> DerefMut for TsifyRefMut<T> {
...
} |
I'm not sure if I have the appropriate knowledge here to comment on if it's safe or not :( Reading through the code does make sense to me though. |
This definitely should be possible to implement soundly because of that lifetime limitation. While yes there wouldn't be any benefit to calling the & version on wasm, there basically never is. However if you're calling the same function from rust in any situation, being able to declare them with a reference would be very useful. |
update is_js_ident to cover most cases
I've just tried out this crate and the typescript interfaces it's creating are awesome. Unfortunately I don't seem to be able to use it in methods like
as
tsify(into_wasm_abi, from_wasm_abi)
doesn't implement support forRefFromWasmAbi
andRefMutFromWasmAbi
Is there any plan to support deriving impl's for these?
The text was updated successfully, but these errors were encountered: