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

feat: Implement JsValue.as_ptr and (bonus) JsValue.downcast_unchecked #3088

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Commits on May 11, 2023

  1. Configuration menu
    Copy the full SHA
    d2576e0 View commit details
    Browse the repository at this point in the history
  2. feat: Implement JsValue.as_ptr.

    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.
    Hywan committed May 11, 2023
    Configuration menu
    Copy the full SHA
    1c18cab View commit details
    Browse the repository at this point in the history
  3. feat: Implement JsValue.downcast_unchecked.

    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`.
    Hywan committed May 11, 2023
    Configuration menu
    Copy the full SHA
    a2a2794 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    19dedec View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    03551b5 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    fff49af View commit details
    Browse the repository at this point in the history