[wasm-gc] Support i31ref in WebAssembly.Global js interop

Bug: v8:7748
Change-Id: Id37b67170173149b59ad6bbf46218c529cc8d6c1
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4162928
Reviewed-by: Manos Koukoutos <manoskouk@chromium.org>
Commit-Queue: Matthias Liedtke <mliedtke@chromium.org>
Cr-Commit-Position: refs/heads/main@{#85290}
This commit is contained in:
Matthias Liedtke 2023-01-13 16:13:13 +01:00 committed by V8 LUCI CQ
parent 1219328b1e
commit 928c3e7829
2 changed files with 45 additions and 1 deletions

View File

@ -1411,9 +1411,11 @@ bool GetValueType(Isolate* isolate, MaybeLocal<Value> maybe,
} else if (enabled_features.has_gc() &&
string->StringEquals(v8_str(isolate, "arrayref"))) {
*type = i::wasm::kWasmArrayRef;
} else if (enabled_features.has_gc() &&
string->StringEquals(v8_str(isolate, "i31ref"))) {
*type = i::wasm::kWasmI31Ref;
} else {
// Unrecognized type.
// TODO(7748): Add "i31ref".
*type = i::wasm::kWasmVoid;
}
return true;

View File

@ -239,6 +239,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
builder.addImportedGlobal("imports", "eq2", kWasmEqRef, false);
builder.addImportedGlobal("imports", "eq3", kWasmEqRef, false);
builder.addImportedGlobal("imports", "array", kWasmArrayRef, false);
builder.addImportedGlobal("imports", "i31ref", kWasmI31Ref, false);
builder.instantiate({imports : {
any1: exporting_instance.exports.create_struct(),
any2: exporting_instance.exports.create_array(),
@ -249,6 +250,7 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
eq2: exporting_instance.exports.create_array(),
eq3: exporting_instance.exports.create_struct(),
array: exporting_instance.exports.create_array(),
i31ref: -123,
}});
})();
@ -500,3 +502,43 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
assertThrows(() => arrayref_global.value = "string", TypeError);
assertThrows(() => arrayref_global.value = wasm.create_struct(1), TypeError);
})();
(function TestI31RefGlobalFromJS() {
print(arguments.callee.name);
let i31ref_global = new WebAssembly.Global(
{ value: "i31ref", mutable: true }, 123);
assertEquals(123, i31ref_global.value);
let builder = new WasmModuleBuilder();
builder.addImportedGlobal("imports", "i31ref_global", kWasmI31Ref, true);
let struct_type = builder.addStruct([makeField(kWasmI32, false)]);
builder.addFunction("get_i31", makeSig([], [kWasmI32]))
.addBody([
kExprGlobalGet, 0,
kGCPrefix, kExprI31GetS
])
.exportFunc();
builder.addFunction("create_struct",
makeSig([kWasmI32], [wasmRefType(struct_type)]))
.addBody([
kExprLocalGet, 0,
kGCPrefix, kExprStructNew, struct_type,
])
.exportFunc();
let instance = builder.instantiate({imports : {i31ref_global}});
let wasm = instance.exports;
assertEquals(123, i31ref_global.value);
i31ref_global.value = 42;
assertEquals(42, i31ref_global.value);
assertEquals(42, wasm.get_i31());
i31ref_global.value = null;
assertEquals(null, i31ref_global.value);
assertThrows(() => i31ref_global.value = undefined, TypeError);
assertThrows(() => i31ref_global.value = "string", TypeError);
assertThrows(() => i31ref_global.value = wasm.create_struct(1), TypeError);
assertThrows(() => i31ref_global.value = Math.pow(2, 33), TypeError);
})();