Skip to content

Commit 64290e1

Browse files
joshualittCommit Queue
authored and
Commit Queue
committed
[js] Add JSStringImpl box for JSString.
Change-Id: I63a2ecdf3fd2331f91632ae5b2cc51813cd44c66 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/307961 Commit-Queue: Joshua Litt <[email protected]> Reviewed-by: Srujan Gaddam <[email protected]> Reviewed-by: Ömer Ağacan <[email protected]>
1 parent 76eb709 commit 64290e1

16 files changed

+1727
-231
lines changed

pkg/dart2wasm/lib/class_info.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,10 @@ class ClassInfoCollector {
242242
cls: cls
243243
};
244244

245+
late final Set<Class> _neverMasquerades = {
246+
translator.jsStringImplClass,
247+
};
248+
245249
/// Wasm field type for fields with type [_Type]. Fields of this type are
246250
/// added to classes for type parameters.
247251
///
@@ -350,6 +354,9 @@ class ClassInfoCollector {
350354
translator.classForHeapType.putIfAbsent(info.struct, () => info!);
351355

352356
ClassInfo? computeMasquerade() {
357+
if (_neverMasquerades.contains(cls)) {
358+
return null;
359+
}
353360
if (info!.superInfo?.masquerade != null) {
354361
return info.superInfo!.masquerade;
355362
}

pkg/dart2wasm/lib/code_generator.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,8 +2585,10 @@ class CodeGenerator extends ExpressionVisitor1<w.ValueType, w.ValueType>
25852585
return visitStringLiteral(expr, expectedType);
25862586
}
25872587

2588-
makeListFromExpressions(node.expressions,
2589-
InterfaceType(translator.stringBaseClass, Nullability.nonNullable));
2588+
makeListFromExpressions(
2589+
node.expressions,
2590+
InterfaceType(
2591+
translator.coreTypes.stringClass, Nullability.nonNullable));
25902592
return call(translator.stringInterpolate.reference);
25912593
}
25922594

pkg/dart2wasm/lib/kernel_nodes.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ mixin KernelNodes {
1212

1313
late final LibraryIndex index = LibraryIndex(component, [
1414
"dart:_internal",
15+
"dart:_js_types",
1516
"dart:async",
1617
"dart:collection",
1718
"dart:core",
@@ -23,6 +24,10 @@ mixin KernelNodes {
2324
// dart:_internal classes
2425
late final Class symbolClass = index.getClass("dart:_internal", "Symbol");
2526

27+
// dart:_js_types classes
28+
late final Class jsStringImplClass =
29+
index.getClass("dart:_js_types", "JSStringImpl");
30+
2631
// dart:collection classes
2732
late final Class hashFieldBaseClass =
2833
index.getClass("dart:collection", "_HashFieldBase");

pkg/dart2wasm/lib/target.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class WasmTarget extends Target {
7171
'dart:_internal',
7272
'dart:_http',
7373
'dart:_js_helper',
74+
'dart:_js_types',
7475
'dart:typed_data',
7576
'dart:nativewrappers',
7677
'dart:io',
@@ -85,13 +86,22 @@ class WasmTarget extends Target {
8586
@override
8687
List<String> get extraIndexedLibraries => const <String>[
8788
'dart:_js_helper',
89+
'dart:_js_types',
8890
'dart:collection',
8991
'dart:typed_data',
9092
'dart:js_interop',
9193
'dart:js_util',
9294
'dart:_wasm',
9395
];
9496

97+
@override
98+
bool mayDefineRestrictedType(Uri uri) =>
99+
uri.isScheme('dart') &&
100+
(uri.path == 'core' ||
101+
uri.path == 'typed_data' ||
102+
uri.path == '_js_types');
103+
104+
@override
95105
bool allowPlatformPrivateLibraryAccess(Uri importer, Uri imported) =>
96106
super.allowPlatformPrivateLibraryAccess(importer, imported) ||
97107
importer.path.contains('tests/web/wasm') ||

sdk/lib/_internal/wasm/lib/core_patch.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import "dart:_internal" as _internal show Symbol;
3232

3333
import 'dart:_js_helper' show JS, JSSyntaxRegExp, quoteStringForRegExp;
3434

35+
import 'dart:_js_types' show JSStringImpl;
36+
3537
import "dart:collection"
3638
show
3739
HashMap,
@@ -50,6 +52,8 @@ import 'dart:math' show Random;
5052

5153
import "dart:typed_data" show Uint8List, Uint16List;
5254

55+
import 'dart:_string_helper';
56+
5357
import 'dart:_wasm';
5458

5559
part "bool.dart";

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ library dart._js_helper;
77

88
import 'dart:_internal';
99
import 'dart:_js_annotations' as js;
10+
import 'dart:_js_types' as js_types;
1011
import 'dart:_wasm';
11-
import 'dart:collection';
1212
import 'dart:js_interop';
1313
import 'dart:typed_data';
1414

@@ -316,7 +316,7 @@ WasmExternRef? callMethodVarArgsRaw(
316316
JS<WasmExternRef?>("(o, m, a) => o[m].apply(o, a)", o, method, args);
317317

318318
String stringify(WasmExternRef? object) =>
319-
JS<String>("o => stringToDartString(String(o))", object);
319+
js_types.JSStringImpl(JS<WasmExternRef?>("o => String(o)", object));
320320

321321
void promiseThen(WasmExternRef? promise, WasmExternRef? successFunc,
322322
WasmExternRef? failureFunc) =>
@@ -346,6 +346,8 @@ WasmExternRef? jsifyRaw(Object? object) {
346346
assert(functionToJSWrapper.containsKey(object),
347347
'Must call `allowInterop` on functions before they flow to JS');
348348
return functionToJSWrapper[object]!.toExternRef;
349+
} else if (object is js_types.JSStringImpl) {
350+
return object.toExternRef;
349351
} else if (object is JSValue) {
350352
return object.toExternRef;
351353
} else if (object is String) {
@@ -391,7 +393,7 @@ Object? dartifyRaw(WasmExternRef? ref) {
391393
} else if (isJSNumber(ref)) {
392394
return toDartNumber(ref);
393395
} else if (isJSString(ref)) {
394-
return jsStringToDartString(ref);
396+
return js_types.JSStringImpl.box(ref);
395397
} else if (isJSInt8Array(ref)) {
396398
return toDartInt8List(ref);
397399
} else if (isJSUint8Array(ref)) {

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:_internal' show patch;
66
import 'dart:_js_helper' hide JS;
77
import 'dart:_js_helper' as js_helper;
8+
import 'dart:_js_types' as js_types;
89
import 'dart:_wasm';
910
import 'dart:async' show Completer;
1011
import 'dart:js_interop';
@@ -330,11 +331,20 @@ extension BoolToJSBoolean on bool {
330331
@patch
331332
extension JSStringToString on JSString {
332333
@patch
333-
String get toDart => jsStringToDartString(toExternRef);
334+
String get toDart => js_types.JSStringImpl(toExternRef);
334335
}
335336

336337
@patch
337338
extension StringToJSString on String {
338339
@patch
339-
JSString get toJS => _box<JSString>(jsStringFromDartString(this));
340+
JSString get toJS {
341+
final t = this;
342+
WasmExternRef? ref;
343+
if (t is js_types.JSStringImpl) {
344+
ref = t.toExternRef;
345+
} else {
346+
ref = jsStringFromDartString(this);
347+
}
348+
return _box<JSString>(ref);
349+
}
340350
}

0 commit comments

Comments
 (0)