[dart2wasm] Move JSStringImpl to dart:_string

This moves `JSStringImpl` class from `dart:_js_types` to `dart:_string`.

This allows implementing a common base class with unchecked operations
to all string classes as internal methods (so users won't be able to
call them via `dyanmic`), and extension methods to call these unchecked
methods in libraries like `dart:convert`.

Uses of these methods are introduced in
https://dart-review.googlesource.com/c/sdk/+/372443.

Change-Id: Ie4cfe778654c42d62bc4a90391fe349fa783a42c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372442
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
This commit is contained in:
Ömer Sinan Ağacan 2024-06-20 13:08:03 +00:00 committed by Commit Queue
parent a616e736b3
commit a954aa0200
16 changed files with 46 additions and 24 deletions

View file

@ -38,6 +38,7 @@ JSMethods _performJSInteropTransformations(
'_js_helper',
'_js_string_convert',
'_js_types',
'_string',
'convert',
'js_interop',
'js_interop_unsafe',

View file

@ -17,7 +17,7 @@ mixin KernelNodes {
// dart:_js_types classes
late final Class jsStringClass =
index.getClass("dart:_js_types", "JSStringImpl");
index.getClass("dart:_string", "JSStringImpl");
// dart:collection classes
late final Class hashFieldBaseClass =
@ -195,9 +195,9 @@ mixin KernelNodes {
// dart:_js_types procedures
late final Procedure jsStringEquals =
index.getProcedure("dart:_js_types", "JSStringImpl", "==");
index.getProcedure("dart:_string", "JSStringImpl", "==");
late final Procedure jsStringInterpolate =
index.getProcedure("dart:_js_types", "JSStringImpl", "_interpolate");
index.getProcedure("dart:_string", "JSStringImpl", "_interpolate");
// dart:collection procedures and fields
late final Procedure mapFactory =

View file

@ -146,6 +146,7 @@ class WasmTarget extends Target {
'dart:_internal',
'dart:_js_helper',
'dart:_js_types',
'dart:_string',
'dart:_wasm',
'dart:async',
'dart:developer',
@ -157,20 +158,19 @@ class WasmTarget extends Target {
'dart:js_util',
'dart:nativewrappers',
'dart:typed_data',
if (mode != Mode.jsCompatibility) 'dart:_string',
];
@override
List<String> get extraIndexedLibraries => [
'dart:_js_helper',
'dart:_js_types',
'dart:_string',
'dart:_wasm',
'dart:collection',
'dart:js_interop',
'dart:js_interop_unsafe',
'dart:js_util',
'dart:typed_data',
if (mode != Mode.jsCompatibility) 'dart:_string',
];
@override
@ -280,6 +280,8 @@ class WasmTarget extends Target {
component, Uri.parse("dart:js_interop")),
...jsInteropHelper.calculateTransitiveImportsOfJsInteropIfUsed(
component, Uri.parse("dart:convert")),
...jsInteropHelper.calculateTransitiveImportsOfJsInteropIfUsed(
component, Uri.parse("dart:_string")),
};
if (transitiveImportingJSInterop.isEmpty) {
logger?.call("Skipped JS interop transformations");
@ -489,7 +491,7 @@ class WasmTarget extends Target {
// In JSCM all strings are JS strings.
if (mode == Mode.jsCompatibility) {
return _jsString ??=
coreTypes.index.getClass("dart:_js_types", "JSStringImpl");
coreTypes.index.getClass("dart:_string", "JSStringImpl");
}
const int maxLatin1 = 0xff;
for (int i = 0; i < value.length; ++i) {

View file

@ -31,7 +31,7 @@ import "dart:_internal" as _internal;
import 'dart:_js_helper' show JS, JSSyntaxRegExp, quoteStringForRegExp;
import 'dart:_js_types' show JSStringImpl;
import 'dart:_string' show JSStringImpl;
import "dart:collection"
show

View file

@ -8,6 +8,7 @@ library dart._js_helper;
import 'dart:_internal';
import 'dart:_js_annotations' as js;
import 'dart:_js_types' as js_types;
import 'dart:_string';
import 'dart:_wasm';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
@ -325,10 +326,10 @@ WasmExternRef? callMethodVarArgsRaw(
JS<WasmExternRef?>("(o, m, a) => o[m].apply(o, a)", o, method, args);
String typeof(WasmExternRef? object) =>
js_types.JSStringImpl(JS<WasmExternRef?>("o => typeof o", object));
JSStringImpl(JS<WasmExternRef?>("o => typeof o", object));
String stringify(WasmExternRef? object) =>
js_types.JSStringImpl(JS<WasmExternRef?>("o => String(o)", object));
JSStringImpl(JS<WasmExternRef?>("o => String(o)", object));
void promiseThen(WasmExternRef? promise, WasmExternRef? successFunc,
WasmExternRef? failureFunc) =>
@ -358,7 +359,7 @@ WasmExternRef? jsifyRaw(Object? object) {
assert(functionToJSWrapper.containsKey(object),
'Must call `allowInterop` on functions before they flow to JS');
return functionToJSWrapper[object]!.toExternRef;
} else if (object is js_types.JSStringImpl) {
} else if (object is JSStringImpl) {
return object.toExternRef;
} else if (object is JSValue) {
return object.toExternRef;
@ -427,7 +428,7 @@ Object? dartifyRaw(WasmExternRef? ref) {
} else if (isJSNumber(ref)) {
return toDartNumber(ref);
} else if (isJSString(ref)) {
return js_types.JSStringImpl.box(ref);
return JSStringImpl.box(ref);
} else if (isJSInt8Array(ref)) {
return js_types.JSInt8ArrayImpl.fromJSArray(ref);
} else if (isJSUint8Array(ref)) {

View file

@ -3,9 +3,10 @@
// BSD-style license that can be found in the LICENSE file.
import 'dart:_internal' show patch;
import 'dart:_js_helper' hide JS;
import 'dart:_js_helper' as js_helper;
import 'dart:_js_helper' hide JS;
import 'dart:_js_types' as js_types;
import 'dart:_string';
import 'dart:_wasm';
import 'dart:async' show Completer;
import 'dart:js_interop';
@ -450,7 +451,7 @@ extension BoolToJSBoolean on bool {
@patch
extension JSStringToString on JSString {
@patch
String get toDart => js_types.JSStringImpl(toExternRef);
String get toDart => JSStringImpl(toExternRef);
}
@patch
@ -458,9 +459,8 @@ extension StringToJSString on String {
@patch
JSString get toJS {
final t = this;
return JSString._(JSValue(t is js_types.JSStringImpl
? t.toExternRef
: jsStringFromDartString(t)));
return JSString._(
JSValue(t is JSStringImpl ? t.toExternRef : jsStringFromDartString(t)));
}
}

View file

@ -2,7 +2,14 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of dart._js_types;
import 'dart:_error_utils';
import 'dart:_internal';
import 'dart:_js_helper' as js;
import 'dart:_js_types';
import 'dart:_object_helper';
import 'dart:_string_helper';
import 'dart:_wasm';
import 'dart:js_interop';
final class JSStringImpl implements String {
final WasmExternRef? _ref;
@ -37,7 +44,8 @@ final class JSStringImpl implements String {
final s = o.toString();
final jsString =
s is JSStringImpl ? js.JSValue.boxT<JSAny?>(s.toExternRef) : s.toJS;
array._setUnchecked(i, jsString);
// array._setUnchecked(i, jsString);
array[i] = jsString;
}
return JSStringImpl(
js.JS<WasmExternRef?>("a => a.join('')", array.toExternRef));

View file

@ -7,6 +7,7 @@ library dart._js_string_convert;
import 'dart:_js_helper' as js;
import 'dart:_js_types';
import 'dart:_string';
import 'dart:_wasm';
import 'dart:js_interop';

View file

@ -22,6 +22,7 @@ import 'dart:_simd'
NaiveUnmodifiableInt32x4List,
NaiveUnmodifiableFloat32x4List,
NaiveUnmodifiableFloat64x2List;
import 'dart:_string';
import 'dart:_string_helper';
import 'dart:_wasm';
import 'dart:collection';
@ -30,7 +31,6 @@ import 'dart:math';
import 'dart:typed_data';
part 'js_array.dart';
part 'js_string.dart';
part 'js_typed_array.dart';
typedef JSAnyRepType = js.JSValue;

View file

@ -2,8 +2,6 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library dart._string;
import "dart:_internal"
show
CodeUnits,

View file

@ -5,6 +5,7 @@
import 'dart:_internal';
import 'dart:_js_helper';
import 'dart:_js_types';
import 'dart:_string';
import 'dart:_wasm';
@patch

View file

@ -6,6 +6,7 @@ import 'dart:_internal' show ClassID, patch, POWERS_OF_TEN, unsafeCast;
import 'dart:_js_helper' as js;
import 'dart:_js_string_convert';
import 'dart:_js_types';
import 'dart:_string';
import 'dart:_wasm';
import 'dart:js_interop';
import 'dart:typed_data' show Uint8List, Uint16List;

View file

@ -3,7 +3,7 @@
// BSD-style license that can be found in the LICENSE file.
import "dart:_internal" show patch, unsafeCast;
import "dart:_js_types" show JSStringImpl;
import "dart:_string" show JSStringImpl;
import 'dart:_js_helper' as js;
@patch

View file

@ -5,6 +5,7 @@
import 'dart:_internal' show EfficientLengthIterable, patch;
import 'dart:_js_helper' as js;
import 'dart:_js_types';
import 'dart:_string';
import 'dart:_wasm';
import 'dart:js_interop';
import 'dart:typed_data';

View file

@ -159,7 +159,8 @@
]
},
"_string": {
"uri": "_internal/wasm/lib/string.dart"
"uri": "_internal/wasm/lib/js_string.dart",
"patches": "_internal/wasm/lib/string.dart"
},
"_typed_data": {
"uri": "_internal/wasm/lib/typed_data.dart"
@ -208,6 +209,9 @@
"_internal/wasm_js_compatibility/lib/typed_data.dart",
"_internal/wasm_js_compatibility/lib/typed_data_patch.dart"
]
},
"_string": {
"uri": "_internal/wasm/lib/js_string.dart"
}
}
},

View file

@ -144,7 +144,9 @@ wasm:
- _internal/wasm/lib/simd_patch.dart
- _internal/wasm/lib/typed_data_patch.dart
_string:
uri: _internal/wasm/lib/string.dart
uri: _internal/wasm/lib/js_string.dart
patches:
_internal/wasm/lib/string.dart
_typed_data:
uri: _internal/wasm/lib/typed_data.dart
@ -182,6 +184,8 @@ wasm_js_compatibility:
- _internal/wasm/lib/simd_patch.dart
- _internal/wasm_js_compatibility/lib/typed_data.dart
- _internal/wasm_js_compatibility/lib/typed_data_patch.dart
_string:
uri: _internal/wasm/lib/js_string.dart
wasm_common:
libraries: