-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat: Implement JsValue.as_ptr
and (bonus) JsValue.downcast_unchecked
#3088
base: main
Are you sure you want to change the base?
feat: Implement JsValue.as_ptr
and (bonus) JsValue.downcast_unchecked
#3088
Conversation
ping @alexcrichton :-) ? |
Could you describe your use-case for this feature? I don't think I mind too much having the intrinsic returning the |
@daxpedda My usecase is when I've an Having at least the intrinsic returning the |
That makes sense indeed. I think solving the issue around downcasting from I will get a third opinion on this and get back to you again. |
Also, this PR should be updated since the merge of #3408. |
Ah! Thank you.
Yeah, if you rebase it, it should hopefully fail the test you added. |
This patch implements `JsValue.as_ptr`. The idea is to be able to retrieve the `ptr` value of a `JsValue` from the JS land, which represents the raw pointer value of the value from within the Wasm memory. To achieve this, this patch introduces a new intrinsic: `__wbindgen_jsval_ptr`, which maps to `Intrinsic::JsvalPtr`, which returns a (new) `opt_u32`. It generates the following JS code: `{obj}.ptr`. If `ptr` is absent, it will return `undefined`, so `None` once on the Rust side, otherwise `Some(u32)`. The following Rust code is the “old” way of achieving this behavior: ```rust pub struct JsvalPtr; let js_val: JsValue = JsvalPtr.into(); let ptr = Reflect::get(&js_val, &JsValue::from_str("ptr").unwrap().as_f64().unwrap() as u32; ``` The following Rust code is the "new” way of achieving this behavior: ```rust let ptr = js_val.as_ptr().unwrap(); ``` The `Reflect` API is not longer necessary.
Since the introduction of the new `JsValue.as_ptr` method, it is now possible to implement a new handy method: `JsValue.downcast_unchecked` (which is unsafe by nature because no check is applied). `downcast_unchecked` will downcast the `JsValue` to `T::Anchor` where it implements `RefFromWasmAbi<Abi = u32>`. It returns a `Ref<T>` which can be deref to `&T`.
da67411
to
fff49af
Compare
Rebased :-). |
This PR implements
JsValue.as_ptr
. The idea is to be able toretrieve the
ptr
value of aJsValue
from the JS land, whichrepresents the raw pointer value of the value from within the Wasm
memory.
To achieve this, this patch introduces a new intrinsic:
__wbindgen_jsval_ptr
, which maps toIntrinsic::JsvalPtr
, whichreturns a (new)
opt_u32
. It generates the following JS code:{obj}.ptr
. Ifptr
is absent, it will returnundefined
, soNone
once on the Rust side, otherwise
Some(u32)
.The following Rust code is the “old” way of achieving this behavior:
The following Rust code is the "new” way of achieving this behavior:
The
Reflect
API is no longer necessary.Since the introduction of the new
JsValue.as_ptr
method, it is nowpossible to implement a new handy method:
JsValue.downcast_unchecked
(which is unsafe by nature because no check is applied).
downcast_unchecked
will downcast theJsValue
toT::Anchor
whereit implements
RefFromWasmAbi<Abi = u32>
. It returns aRef<T>
whichcan be deref to
&T
.I reckon the test summarizes well the API:
Thoughts?