Skip to content

Commit b01bf92

Browse files
srujzsCommit Queue
authored andcommitted
[dart2wasm] Typed array wrappers should unwrap instead of reinstantiate
Fixes #61543 toJS and jsify conversions instantiate a new typed array using the same underlying buffer instead of returning the original typed array. To fix this, the typed array wrappers now capture the original array ref (if any) and return it in toJSArrayExternRef if it exists and the caller didn't ask for a subrange. Note that _ref in the wrappers refer to a DataView, and not the array ref. Some methods are renamed to reduce that confusion. Change-Id: Ia9befd114a77489aedbb3644c9604aef4b3af7c1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/450966 Reviewed-by: Ömer Ağacan <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]>
1 parent d60c837 commit b01bf92

File tree

8 files changed

+340
-226
lines changed

8 files changed

+340
-226
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ instead.
145145
well. Specifically, the function should be a statically known type, cannot
146146
contain invalid types in its signature, cannot have any type parameters, and
147147
cannot have any named parameters.
148+
- On dart2wasm, typed lists that are wrappers around typed arrays now return the
149+
original typed array when unwrapped instead of instantiating a new typed array
150+
with the same buffer. This applies to both the `.toJS` conversions and
151+
`jsify`. See [#61543][] for more details.
148152

149153
[#59830]: https://github.com/dart-lang/sdk/issues/59830
150154
[#55138]: https://github.com/dart-lang/sdk/issues/55138
155+
[#61543]: https://github.com/dart-lang/sdk/issues/61543
151156

152157
## 3.9.0
153158

pkg/dart2wasm/lib/js/util.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,24 @@ class CoreTypesUtil {
143143
coreTypes.listClass:
144144
coreTypes.index.getTopLevelProcedure('dart:_js_helper', 'toDartList'),
145145
coreTypes.index.getClass('dart:typed_data', 'Int8List'): coreTypes.index
146-
.getProcedure('dart:_js_types', 'JSInt8ArrayImpl', 'fromRef'),
146+
.getProcedure('dart:_js_types', 'JSInt8ArrayImpl', 'fromArrayRef'),
147147
coreTypes.index.getClass('dart:typed_data', 'Uint8List'): coreTypes.index
148-
.getProcedure('dart:_js_types', 'JSUint8ArrayImpl', 'fromRef'),
149-
coreTypes.index.getClass('dart:typed_data', 'Uint8ClampedList'): coreTypes
150-
.index
151-
.getProcedure('dart:_js_types', 'JSUint8ClampedArrayImpl', 'fromRef'),
148+
.getProcedure('dart:_js_types', 'JSUint8ArrayImpl', 'fromArrayRef'),
149+
coreTypes.index.getClass('dart:typed_data', 'Uint8ClampedList'):
150+
coreTypes.index.getProcedure(
151+
'dart:_js_types', 'JSUint8ClampedArrayImpl', 'fromArrayRef'),
152152
coreTypes.index.getClass('dart:typed_data', 'Int16List'): coreTypes.index
153-
.getProcedure('dart:_js_types', 'JSInt16ArrayImpl', 'fromRef'),
153+
.getProcedure('dart:_js_types', 'JSInt16ArrayImpl', 'fromArrayRef'),
154154
coreTypes.index.getClass('dart:typed_data', 'Uint16List'): coreTypes.index
155-
.getProcedure('dart:_js_types', 'JSUint16ArrayImpl', 'fromRef'),
155+
.getProcedure('dart:_js_types', 'JSUint16ArrayImpl', 'fromArrayRef'),
156156
coreTypes.index.getClass('dart:typed_data', 'Int32List'): coreTypes.index
157-
.getProcedure('dart:_js_types', 'JSInt32ArrayImpl', 'fromRef'),
157+
.getProcedure('dart:_js_types', 'JSInt32ArrayImpl', 'fromArrayRef'),
158158
coreTypes.index.getClass('dart:typed_data', 'Uint32List'): coreTypes.index
159-
.getProcedure('dart:_js_types', 'JSUint32ArrayImpl', 'fromRef'),
159+
.getProcedure('dart:_js_types', 'JSUint32ArrayImpl', 'fromArrayRef'),
160160
coreTypes.index.getClass('dart:typed_data', 'Float32List'): coreTypes.index
161-
.getProcedure('dart:_js_types', 'JSFloat32ArrayImpl', 'fromRef'),
161+
.getProcedure('dart:_js_types', 'JSFloat32ArrayImpl', 'fromArrayRef'),
162162
coreTypes.index.getClass('dart:typed_data', 'Float64List'): coreTypes.index
163-
.getProcedure('dart:_js_types', 'JSFloat64ArrayImpl', 'fromRef'),
163+
.getProcedure('dart:_js_types', 'JSFloat64ArrayImpl', 'fromArrayRef'),
164164
coreTypes.index.getClass('dart:typed_data', 'ByteBuffer'): coreTypes.index
165165
.getProcedure('dart:_js_types', 'JSArrayBufferImpl', 'fromRef'),
166166
coreTypes.index.getClass('dart:typed_data', 'ByteData'): coreTypes.index

sdk/lib/_internal/wasm/lib/js_helper.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,24 +512,28 @@ Object? dartifyRaw(WasmExternRef? ref, [int? refType]) {
512512
ExternRefType.number => toDartNumber(ref),
513513
ExternRefType.string => JSStringImpl.fromRefUnchecked(ref),
514514
ExternRefType.array => toDartList(ref),
515-
ExternRefType.int8Array => js_types.JSInt8ArrayImpl.fromRefUnchecked(ref),
516-
ExternRefType.uint8Array => js_types.JSUint8ArrayImpl.fromRefUnchecked(ref),
517-
ExternRefType.uint8ClampedArray =>
518-
js_types.JSUint8ClampedArrayImpl.fromRefUnchecked(ref),
519-
ExternRefType.int16Array => js_types.JSInt16ArrayImpl.fromRefUnchecked(ref),
520-
ExternRefType.uint16Array => js_types.JSUint16ArrayImpl.fromRefUnchecked(
515+
ExternRefType.int8Array => js_types.JSInt8ArrayImpl.fromArrayRefUnchecked(
521516
ref,
522517
),
523-
ExternRefType.int32Array => js_types.JSInt32ArrayImpl.fromRefUnchecked(ref),
524-
ExternRefType.uint32Array => js_types.JSUint32ArrayImpl.fromRefUnchecked(
518+
ExternRefType.uint8Array => js_types.JSUint8ArrayImpl.fromArrayRefUnchecked(
525519
ref,
526520
),
527-
ExternRefType.float32Array => js_types.JSFloat32ArrayImpl.fromRefUnchecked(
521+
ExternRefType.uint8ClampedArray =>
522+
js_types.JSUint8ClampedArrayImpl.fromArrayRefUnchecked(ref),
523+
ExternRefType.int16Array => js_types.JSInt16ArrayImpl.fromArrayRefUnchecked(
528524
ref,
529525
),
530-
ExternRefType.float64Array => js_types.JSFloat64ArrayImpl.fromRefUnchecked(
526+
ExternRefType.uint16Array =>
527+
js_types.JSUint16ArrayImpl.fromArrayRefUnchecked(ref),
528+
ExternRefType.int32Array => js_types.JSInt32ArrayImpl.fromArrayRefUnchecked(
531529
ref,
532530
),
531+
ExternRefType.uint32Array =>
532+
js_types.JSUint32ArrayImpl.fromArrayRefUnchecked(ref),
533+
ExternRefType.float32Array =>
534+
js_types.JSFloat32ArrayImpl.fromArrayRefUnchecked(ref),
535+
ExternRefType.float64Array =>
536+
js_types.JSFloat64ArrayImpl.fromArrayRefUnchecked(ref),
533537
ExternRefType.arrayBuffer || ExternRefType.sharedArrayBuffer =>
534538
js_types.JSArrayBufferImpl.fromRefUnchecked(ref),
535539
ExternRefType.dataView => js_types.JSDataViewImpl.fromRefUnchecked(ref),

sdk/lib/_internal/wasm/lib/js_interop_patch.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ extension ByteDataToJSDataView on ByteData {
290290
@patch
291291
extension JSInt8ArrayToInt8List on JSInt8Array {
292292
@patch
293-
Int8List get toDart => js_types.JSInt8ArrayImpl.fromRef(toExternRef);
293+
Int8List get toDart => js_types.JSInt8ArrayImpl.fromArrayRef(toExternRef);
294294
}
295295

296296
@patch
@@ -313,7 +313,7 @@ extension Int8ListToJSInt8Array on Int8List {
313313
@patch
314314
extension JSUint8ArrayToUint8List on JSUint8Array {
315315
@patch
316-
Uint8List get toDart => js_types.JSUint8ArrayImpl.fromRef(toExternRef);
316+
Uint8List get toDart => js_types.JSUint8ArrayImpl.fromArrayRef(toExternRef);
317317
}
318318

319319
@patch
@@ -337,7 +337,7 @@ extension Uint8ListToJSUint8Array on Uint8List {
337337
extension JSUint8ClampedArrayToUint8ClampedList on JSUint8ClampedArray {
338338
@patch
339339
Uint8ClampedList get toDart =>
340-
js_types.JSUint8ClampedArrayImpl.fromRef(toExternRef);
340+
js_types.JSUint8ClampedArrayImpl.fromArrayRef(toExternRef);
341341
}
342342

343343
@patch
@@ -360,7 +360,7 @@ extension Uint8ClampedListToJSUint8ClampedArray on Uint8ClampedList {
360360
@patch
361361
extension JSInt16ArrayToInt16List on JSInt16Array {
362362
@patch
363-
Int16List get toDart => js_types.JSInt16ArrayImpl.fromRef(toExternRef);
363+
Int16List get toDart => js_types.JSInt16ArrayImpl.fromArrayRef(toExternRef);
364364
}
365365

366366
@patch
@@ -383,7 +383,7 @@ extension Int16ListToJSInt16Array on Int16List {
383383
@patch
384384
extension JSUint16ArrayToInt16List on JSUint16Array {
385385
@patch
386-
Uint16List get toDart => js_types.JSUint16ArrayImpl.fromRef(toExternRef);
386+
Uint16List get toDart => js_types.JSUint16ArrayImpl.fromArrayRef(toExternRef);
387387
}
388388

389389
@patch
@@ -406,7 +406,7 @@ extension Uint16ListToJSInt16Array on Uint16List {
406406
@patch
407407
extension JSInt32ArrayToInt32List on JSInt32Array {
408408
@patch
409-
Int32List get toDart => js_types.JSInt32ArrayImpl.fromRef(toExternRef);
409+
Int32List get toDart => js_types.JSInt32ArrayImpl.fromArrayRef(toExternRef);
410410
}
411411

412412
@patch
@@ -429,7 +429,7 @@ extension Int32ListToJSInt32Array on Int32List {
429429
@patch
430430
extension JSUint32ArrayToUint32List on JSUint32Array {
431431
@patch
432-
Uint32List get toDart => js_types.JSUint32ArrayImpl.fromRef(toExternRef);
432+
Uint32List get toDart => js_types.JSUint32ArrayImpl.fromArrayRef(toExternRef);
433433
}
434434

435435
@patch
@@ -452,7 +452,8 @@ extension Uint32ListToJSUint32Array on Uint32List {
452452
@patch
453453
extension JSFloat32ArrayToFloat32List on JSFloat32Array {
454454
@patch
455-
Float32List get toDart => js_types.JSFloat32ArrayImpl.fromRef(toExternRef);
455+
Float32List get toDart =>
456+
js_types.JSFloat32ArrayImpl.fromArrayRef(toExternRef);
456457
}
457458

458459
@patch
@@ -475,7 +476,8 @@ extension Float32ListToJSFloat32Array on Float32List {
475476
@patch
476477
extension JSFloat64ArrayToFloat64List on JSFloat64Array {
477478
@patch
478-
Float64List get toDart => js_types.JSFloat64ArrayImpl.fromRef(toExternRef);
479+
Float64List get toDart =>
480+
js_types.JSFloat64ArrayImpl.fromArrayRef(toExternRef);
479481
}
480482

481483
@patch

0 commit comments

Comments
 (0)