[dart2wasm] Implement == for JSValue.

Change-Id: I45af5bdabfb0dfa12b6b39094843b9394f4fe510
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/257923
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Joshua Litt <joshualitt@google.com>
This commit is contained in:
Joshua Litt 2022-09-07 16:37:17 +00:00 committed by Commit Bot
parent 6f0fe880c4
commit 2b85e02c5d
3 changed files with 53 additions and 0 deletions

View file

@ -354,6 +354,9 @@ var dart2wasm = {
}
return stringToDartString(jsString);
},
areEqualInJS: function(l, r) {
return l === r;
},
};
function instantiate(filename, imports) {

View file

@ -24,6 +24,10 @@ class JSValue {
static JSValue? box(WasmAnyRef? ref) =>
isDartNull(ref) ? null : JSValue(ref!);
@override
bool operator ==(Object that) =>
that is JSValue && areEqualInJS(_ref, that._ref);
WasmAnyRef toAnyRef() => _ref;
String toString() => jsStringToDartString(_ref);
List<Object?> toObjectList() => toDartList(_ref);
@ -196,6 +200,9 @@ external bool isJSSimpleObject(WasmAnyRef? o);
@pragma("wasm:import", "dart2wasm.isJSRegExp")
external bool isJSRegExp(WasmAnyRef object);
@pragma("wasm:import", "dart2wasm.areEqualInJS")
external bool areEqualInJS(WasmAnyRef? l, WasmAnyRef? r);
// The JS runtime will run helpful conversion routines between refs and bool /
// double. In the longer term hopefully we can find a way to avoid the round
// trip.

View file

@ -19,6 +19,48 @@ void createObjectTest() {
Expect.equals('bar', getProperty(o, 'foo'));
}
void equalTest() {
// Different objects aren't equal.
{
Object o1 = newObject();
Object o2 = newObject();
Expect.notEquals(o1, o2);
}
{
eval(r'''
function JSClass() {}
globalThis.boolData = true;
globalThis.boolData2 = true;
globalThis.numData = 4;
globalThis.numData2 = 4;
globalThis.arrData = [1, 2, 3];
globalThis.strData = 'foo';
globalThis.strData2 = 'foo';
globalThis.funcData = function JSClass() {}
globalThis.JSClass = new globalThis.funcData();
''');
Object gt = globalThis;
void test(String propertyName, bool testCanonicalization) {
Expect.equals(
getProperty(gt, propertyName), getProperty(gt, propertyName));
if (testCanonicalization) {
Expect.equals(
getProperty(gt, propertyName), getProperty(gt, propertyName + "2"));
}
}
test("boolData", true);
test("numData", true);
// TODO(joshualitt): Start returning arrays by reference.
//test("arrData", false);
test("strData", true);
test("funcData", false);
test("JSClass", false);
}
}
void _expectIterableEquals(Iterable<Object?> l, Iterable<Object?> r) {
final lIt = l.iterator;
final rIt = r.iterator;
@ -212,6 +254,7 @@ void deepConversionsTest() {
void main() {
createObjectTest();
equalTest();
evalAndConstructTest();
dartObjectRoundTripTest();
deepConversionsTest();