[dart2wasm] Implement constant symbols.

Change-Id: I758576a7a35c6ffe127fd02eb394e97719750476
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250776
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
This commit is contained in:
Joshua Litt 2022-07-26 17:40:07 +00:00 committed by Commit Bot
parent 139d9216ad
commit 9e83c53c61
5 changed files with 89 additions and 2 deletions

View file

@ -826,4 +826,20 @@ class ConstantCreator extends ConstantVisitor<ConstantInfo?> {
});
}
}
@override
ConstantInfo? visitSymbolConstant(SymbolConstant constant) {
ClassInfo info = translator.classInfo[translator.symbolClass]!;
translator.functions.allocateClass(info.classId);
w.RefType stringType =
translator.classInfo[translator.coreTypes.stringClass]!.nonNullableType;
StringConstant nameConstant = StringConstant(constant.name);
ensureConstant(nameConstant);
return createConstant(constant, info.nonNullableType, (function, b) {
b.i32_const(info.classId);
b.i32_const(initialIdentityHash);
constants.instantiateConstant(function, b, nameConstant, stringType);
translator.struct_new(b, info);
});
}
}

View file

@ -112,6 +112,7 @@ class Translator {
late final Class byteDataViewClass;
late final Class typeErrorClass;
late final Class typeUniverseClass;
late final Class symbolClass;
late final Procedure wasmFunctionCall;
late final Procedure wasmTableCallIndirect;
late final Procedure stackTraceCurrent;
@ -188,6 +189,7 @@ class Translator {
Class Function(String) lookupCore = makeLookup("dart.core");
Class Function(String) lookupCollection = makeLookup("dart.collection");
Class Function(String) lookupFfi = makeLookup("dart.ffi");
Class Function(String) lookupInternal = makeLookup("dart._internal");
Class Function(String) lookupTypedData = makeLookup("dart.typed_data");
Class Function(String) lookupWasm = makeLookup("dart.wasm");
@ -235,6 +237,7 @@ class Translator {
typedListClass = lookupTypedData("_TypedList");
typedListViewClass = lookupTypedData("_TypedListView");
byteDataViewClass = lookupTypedData("_ByteDataView");
symbolClass = lookupInternal("Symbol");
wasmFunctionCall =
wasmFunctionClass.procedures.firstWhere((p) => p.name.text == "call");
wasmTableCallIndirect = wasmTableClass.procedures

View file

@ -0,0 +1,68 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// 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 "internal_patch.dart";
// TODO(49531): This file is nearly identical to the VM's
// symbol_patch.dart, with the exception of the added pragma on `const Symbol`.
// Unfortunately, adding this pragma to the VM's symbol_patch causes a
// deadlock. When this bug is fixed we can share a patch file with the VM.
@patch
class Symbol {
// TODO(http://dartbug.com/46716): Recognize Symbol in the VM.
@patch
@pragma("wasm:entry-point")
const Symbol(String name) : this._name = name;
@patch
String toString() => 'Symbol("${computeUnmangledName(this)}")';
@patch
static String computeUnmangledName(Symbol symbol) {
String string = Symbol.getName(symbol);
// get:foo -> foo
// set:foo -> foo=
// get:_foo@xxx -> _foo
// set:_foo@xxx -> _foo=
// Class._constructor@xxx -> Class._constructor
// _Class@xxx._constructor@xxx -> _Class._constructor
// lib._S@xxx with lib._M1@xxx, lib._M2@xxx -> lib._S with lib._M1, lib._M2
StringBuffer result = new StringBuffer();
bool add_setter_suffix = false;
var pos = 0;
if (string.length >= 4 && string[3] == ':') {
// Drop 'get:' or 'set:' prefix.
pos = 4;
if (string[0] == 's') {
add_setter_suffix = true;
}
}
// Skip everything between AT and PERIOD, SPACE, COMMA or END
bool skip = false;
for (; pos < string.length; pos++) {
var char = string[pos];
if (char == '@') {
skip = true;
} else if (char == '.' || char == ' ' || char == ',') {
skip = false;
}
if (!skip) {
result.write(char);
}
}
if (add_setter_suffix) {
result.write('=');
}
return result.toString();
}
// Must be kept in sync with Symbol::CanonicalizeHash in object.cc.
@patch
int get hashCode {
const arbitraryPrime = 664597;
return 0x1fffffff & (arbitraryPrime * _name.hashCode);
}
}

View file

@ -168,7 +168,7 @@
"_internal/wasm/lib/class_id.dart",
"_internal/wasm/lib/patch.dart",
"_internal/wasm/lib/print_patch.dart",
"_internal/vm/lib/symbol_patch.dart"
"_internal/wasm/lib/symbol_patch.dart"
]
},
"_js_helper": {

View file

@ -165,7 +165,7 @@ wasm:
- _internal/wasm/lib/class_id.dart
- _internal/wasm/lib/patch.dart
- _internal/wasm/lib/print_patch.dart
- _internal/vm/lib/symbol_patch.dart
- _internal/wasm/lib/symbol_patch.dart
_js_helper:
uri: _internal/wasm/lib/js_helper.dart
async: