Skip to content

WebIDL impl: Replace use of NonNull<JSObject>  #30889

Closed

Description

@gterzian

Part of #30862

Where appropriate, we should remove the direct use of unsafe NonNull<JSObject>, and replace it with use of the appropriate safe wrapper found in /mozjs/src/typedarray.rs.

Example: AudioBuffer's getChannelData

WedIDL says to return a Float32Array, but implementation returns a NonNull<JSObject>:

fn GetChannelData(&self, cx: JSContext, channel: u32) -> Fallible<NonNull<JSObject>> {

The return value of the WebIDL method is backed by a Heap<*mut JSObject> on the dom struct.

Solution: use js::typedarray::Float32Array instead of js::jsapi::JSObject.

Bonus: wrap the use of js::typedarray inside something at dom/bindings/. (maybe see bindings/iterable.rs for an example)


How to do it?

  1. Update codegen(look also at third_party/webidl)

codegen starts at(probably needs other updates):

def getRetvalDeclarationForType(returnType, descriptorProvider):

  1. Make sure that the right _Methods are generated.
  2. Update the Rust code, where the struct would the typed array directly, and implement the WebIDL methods returning the typed array.
  3. Bonus: put the typed array behind something in dom/bindings so that we don't use js:: directly elsewhere. For example, this kind of code should be hidden from view. In some way this is probably not a bonus, but a prerequisite for 3.

More investigation is probably necessary.

Activity

  1. added
    A-content/script
    L-javascript
    on Dec 19, 2023
  2. gterzian commented on Dec 19, 2023

    @gterzian
    MemberAuthor

    @jdm Does this make sense? Do you know why this wasn't done to begin with?

  3. added
    I-safety
    on Dec 19, 2023
  4. gterzian commented on Dec 19, 2023

    @gterzian
    MemberAuthor

    Had a quick look everywhere, it seems there are really four cases:

    1. Should return some variant of js::typedarray (the majority). Dealt with in this issue
    2. Should return a ReadableStream, see WebIDL impl: remove unsafe JSObject when returning a ReadableStream #30891
    3. Should return an enum, see WebIDL impl: remove unsafe JSObject from return value of Document::NamedGetter #30890
    4. Should return a WebGL extension, which is actually an object? in WebIDL. See WebIDL impl: remove unsafe JSObject from WebGLExtensionWrapper #30892
  5. gterzian commented on Dec 19, 2023

    @gterzian
    MemberAuthor

    See also https://github.com/servo/mozjs/blob/de842d84a07eabac4e1bfe72a954b74ee0244b82/mozjs/src/conversions.rs#L722

    It may be that what is required is simply implementing ToJSValConvertible for the various types in js::rust::typedarray

    In some ways it would in that case still be nicer to wrap the use of js:: inside dom/bindings...

  6. jdm commented on Dec 19, 2023

    @jdm
    Member

    This seems reasonable to me! I'm not sure why we didn't integrate the concrete types originally.

  7. changed the title [-]WebIDL impl: Replace use of NonNull<JSObject> with TypedArray[/-] [+]WebIDL impl: Replace use of NonNull<JSObject> [/+] on Feb 19, 2024
  8. Gae24 commented on Feb 17, 2025

    @Gae24
    Contributor

    Other usages that require discussion about the appropriate wrapper types are:

  9. Taym95 commented on Sep 19, 2026

    @Taym95
    Member

    no remaining NonNull<JSObject>, this can be closed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/scriptI-safetyL-javascript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions