Repository navigation
Description
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>:
servo/components/script/dom/audiobuffer.rs
Line 265 in 8bbcf0a
| 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?
- Update codegen(look also at third_party/webidl)
codegen starts at(probably needs other updates):
| def getRetvalDeclarationForType(returnType, descriptorProvider): |
- Make sure that the right
_Methodsare generated. - Update the Rust code, where the struct would the typed array directly, and implement the WebIDL methods returning the typed array.
- Bonus: put the typed array behind something in
dom/bindingsso that we don't usejs::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
gterzian commentedon Dec 19, 2023
@jdm Does this make sense? Do you know why this wasn't done to begin with?
gterzian commentedon Dec 19, 2023
Had a quick look everywhere, it seems there are really four cases:
js::typedarray(the majority). Dealt with in this issueReadableStream, see WebIDL impl: remove unsafe JSObject when returning a ReadableStream #30891object?in WebIDL. See WebIDL impl: remove unsafe JSObject from WebGLExtensionWrapper #30892gterzian commentedon Dec 19, 2023
See also https://github.com/servo/mozjs/blob/de842d84a07eabac4e1bfe72a954b74ee0244b82/mozjs/src/conversions.rs#L722
It may be that what is required is simply implementing
ToJSValConvertiblefor the various types injs::rust::typedarrayIn some ways it would in that case still be nicer to wrap the use of
js::insidedom/bindings...jdm commentedon Dec 19, 2023
This seems reasonable to me! I'm not sure why we didn't integrate the concrete types originally.
[-]WebIDL impl: Replace use of NonNull<JSObject> with TypedArray[/-][+]WebIDL impl: Replace use of NonNull<JSObject> [/+]Gae24 commentedon Feb 17, 2025
Other usages that require discussion about the appropriate wrapper types are:
servo/components/script/dom/cryptokey.rs
Line 132 in 6dce329
servo/components/script/dom/cryptokey.rs
Line 138 in 6dce329
servo/components/script/dom/htmlinputelement.rs
Line 1362 in 6dce329
servo/components/script/dom/promiserejectionevent.rs
Line 125 in 6dce329
Remove unsafe WebGL uniform typed-array creation (servo#48104)
Migrate remaining WebGL typed-array constructors to BufferSource (ser…
Migrate direct ArrayBuffer creation to BufferSource (servo#48129)
Make ImageData pixel access safe (servo#48134)
Taym95 commentedon Sep 19, 2026
no remaining
NonNull<JSObject>, this can be closed now.Remove unsafe WebGL uniform typed-array creation (servo#48104)
Migrate remaining WebGL typed-array constructors to BufferSource (ser…
Migrate direct ArrayBuffer creation to BufferSource (servo#48129)
Make ImageData pixel access safe (servo#48134)
Remove unsafe WebGL uniform typed-array creation (servo#48104)
Migrate remaining WebGL typed-array constructors to BufferSource (ser…
Migrate direct ArrayBuffer creation to BufferSource (servo#48129)
Make ImageData pixel access safe (servo#48134)