Skip to content

Commit f25a2b4

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Implement jsifyRaw() of Dart lists in Dart
Currently `jsifyRaw()` on a Dart list calls out to JS which allocates an array and then calls back into Dart for each array element, which uses `jsifyRaw()` on the array elements. The JS runtimes will not inline the call back into Dart. So there's actually very little reason to do this in JS, so we can implement it in Dart. That in return will also make this more ammenable to tree shaking as the `$listRead` and `$listLenght` aren't force-exported anymore. Change-Id: I8b3b7f553ce9fa72880f6ca08edb95b900e98570 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/414781 Reviewed-by: Srujan Gaddam <[email protected]> Commit-Queue: Martin Kustermann <[email protected]>
1 parent ef64387 commit f25a2b4

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

pkg/dart2wasm/lib/js/runtime_blob.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@ class CompiledApp {
7272
throw "Unable to print message: " + js;
7373
}
7474
75-
// Converts a Dart List to a JS array. Any Dart objects will be converted, but
76-
// this will be cheap for JSValues.
77-
function arrayFromDartList(constructor, list) {
78-
const exports = dartInstance.exports;
79-
const read = exports.$listRead;
80-
const length = exports.$listLength(list);
81-
const array = new constructor(length);
82-
for (let i = 0; i < length; i++) {
83-
array[i] = read(list, i);
84-
}
85-
return array;
86-
}
87-
8875
// A special symbol attached to functions that wrap Dart functions.
8976
const jsWrappedDartFunctionSymbol = Symbol("JSWrappedDartFunction");
9077

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,14 @@ external WasmExternRef jsFloat64ArrayFromDartFloat64List(Float64List l);
256256

257257
external WasmExternRef jsDataViewFromDartByteData(ByteData data, int length);
258258

259-
WasmExternRef? jsArrayFromDartList(List<Object?> l) =>
260-
JS<WasmExternRef?>('l => arrayFromDartList(Array, l)', l);
259+
WasmExternRef? _jsifyRawList(List<Object?> list) {
260+
final length = list.length;
261+
final result = JSArray<JSAny?>.withLength(length);
262+
for (int i = 0; i < length; i++) {
263+
result[i] = JSValue.box(jsifyRaw(list[i])) as JSAny?;
264+
}
265+
return (result as JSValue).toExternRef;
266+
}
261267

262268
external JSStringImpl jsStringFromDartString(String s);
263269
external String jsStringToDartString(JSStringImpl s);
@@ -369,7 +375,7 @@ WasmExternRef? jsifyRaw(Object? o) {
369375
if (o is js_types.JSDataViewImpl) return o.toExternRef;
370376
if (o is ByteData) return jsDataViewFromDartByteData(o, o.lengthInBytes);
371377
} else if (o is List<Object?>) {
372-
return jsArrayFromDartList(o);
378+
return _jsifyRawList(o);
373379
} else if (o is ByteBuffer) {
374380
if (o is js_types.JSArrayBufferImpl) return o.toExternRef;
375381
return jsArrayBufferFromDartByteBuffer(o);
@@ -672,16 +678,3 @@ external T JS<T>(
672678
arg18,
673679
arg19,
674680
]);
675-
676-
/// Methods used by the wasm runtime.
677-
@pragma("wasm:export", "\$listLength")
678-
WasmI32 _listLength(WasmExternRef? ref) =>
679-
unsafeCastOpaque<List>(
680-
unsafeCast<WasmExternRef>(ref).internalize(),
681-
).length.toWasmI32();
682-
683-
@pragma("wasm:export", "\$listRead")
684-
WasmExternRef? _listRead(WasmExternRef? ref, WasmI32 index) => jsifyRaw(
685-
unsafeCastOpaque<List>(unsafeCast<WasmExternRef>(ref).internalize())[index
686-
.toIntSigned()],
687-
);

0 commit comments

Comments
 (0)