Commit 326608fe authored by Emanuel Ziegler's avatar Emanuel Ziegler Committed by Commit Bot

[wasm][reference-types] Additional nullref changes

Add tests for select, reject undefined globals for nullref and reformat JS

R=ahaas@chromium.org

Bug: chromium:10063
Change-Id: I97f7c86fe7435ca64ce1f4b86d8d9eadd29cc76b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2002531Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Commit-Queue: Emanuel Ziegler <ecmziegler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65795}
parent d4dc8a9c
...@@ -1166,10 +1166,7 @@ bool InstanceBuilder::ProcessImportedGlobal(Handle<WasmInstanceObject> instance, ...@@ -1166,10 +1166,7 @@ bool InstanceBuilder::ProcessImportedGlobal(Handle<WasmInstanceObject> instance,
return false; return false;
} }
} else if (global.type == ValueType::kWasmNullRef) { } else if (global.type == ValueType::kWasmNullRef) {
if (value->IsNullOrUndefined(isolate_)) { if (!value->IsNull(isolate_)) {
WriteGlobalAnyRef(global, isolate_->factory()->null_value());
return true;
} else {
ReportLinkError("imported nullref global must be null", import_index, ReportLinkError("imported nullref global must be null", import_index,
module_name, import_name); module_name, import_name);
return false; return false;
......
...@@ -19,6 +19,10 @@ let kSig_v_in = makeSig([kWasmI32, kWasmNullRef], []); ...@@ -19,6 +19,10 @@ let kSig_v_in = makeSig([kWasmI32, kWasmNullRef], []);
let kSig_n_i = makeSig([kWasmI32], [kWasmNullRef]); let kSig_n_i = makeSig([kWasmI32], [kWasmNullRef]);
let kSig_r_i = makeSig([kWasmI32], [kWasmAnyRef]); let kSig_r_i = makeSig([kWasmI32], [kWasmAnyRef]);
let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]); let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]);
let kSig_n_nni = makeSig([kWasmNullRef, kWasmNullRef, kWasmI32], [kWasmNullRef]);
let kSig_r_nni = makeSig([kWasmNullRef, kWasmNullRef, kWasmI32], [kWasmAnyRef]);
let kSig_a_nni = makeSig([kWasmNullRef, kWasmNullRef, kWasmI32], [kWasmAnyFunc]);
let kSig_e_nni = makeSig([kWasmNullRef, kWasmNullRef, kWasmI32], [kWasmExnRef]);
(function testNullRefIdentityFunction() { (function testNullRefIdentityFunction() {
print(arguments.callee.name); print(arguments.callee.name);
...@@ -209,24 +213,24 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]); ...@@ -209,24 +213,24 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]);
(function testGetNullRefImportedGlobal() { (function testGetNullRefImportedGlobal() {
print(arguments.callee.name); print(arguments.callee.name);
const builder = new WasmModuleBuilder(); const builder = new WasmModuleBuilder();
const initialized_index = builder.addImportedGlobal("foo", "initialized", kWasmNullRef); const global_index = builder.addImportedGlobal("foo", "bar",
const uninitialized_index = builder.addImportedGlobal("foo", "uninitialized", kWasmNullRef); kWasmNullRef);
const sig_n_v = builder.addType(kSig_n_v); const sig_n_v = builder.addType(kSig_n_v);
const sig_v_n = builder.addType(kSig_v_n); const sig_v_n = builder.addType(kSig_v_n);
const sig_v_v = builder.addType(kSig_v_v); const sig_v_v = builder.addType(kSig_v_v);
builder.addFunction('get_initialized', sig_n_v) builder.addFunction('get', sig_n_v)
.addBody([kExprGlobalGet, initialized_index]) .addBody([kExprGlobalGet, global_index])
.exportFunc();
builder.addFunction('get_uninitialized', sig_n_v)
.addBody([kExprGlobalGet, uninitialized_index])
.exportFunc(); .exportFunc();
assertThrows(() => builder.instantiate({foo: {initialized: {}}}), WebAssembly.LinkError); assertThrows(() => builder.instantiate(), TypeError);
assertThrows(() => builder.instantiate({foo: {initialized: a => a}}), WebAssembly.LinkError); assertThrows(() => builder.instantiate({foo: {}}), WebAssembly.LinkError);
assertThrows(() => builder.instantiate({foo: {bar: {}}}),
WebAssembly.LinkError);
assertThrows(() => builder.instantiate({foo: {bar: a => a}}),
WebAssembly.LinkError);
const instance = builder.instantiate({foo: {initialized: null}}); const instance = builder.instantiate({foo: {bar: null}});
assertEquals(null, instance.exports.get_initialized()); assertEquals(null, instance.exports.get());
assertEquals(null, instance.exports.get_uninitialized());
})(); })();
(function testNullRefTable() { (function testNullRefTable() {
...@@ -293,7 +297,8 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]); ...@@ -293,7 +297,8 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]);
(function testImportNullRefTable() { (function testImportNullRefTable() {
print(arguments.callee.name); print(arguments.callee.name);
const builder = new WasmModuleBuilder(); const builder = new WasmModuleBuilder();
const table_index = builder.addImportedTable("imp", "table", 2, 10, kWasmNullRef); const table_index = builder.addImportedTable("imp", "table", 2, 10,
kWasmNullRef);
builder.addFunction('get_null', kSig_n_i) builder.addFunction('get_null', kSig_n_i)
.addBody([kExprLocalGet, 0, kExprTableGet, table_index]) .addBody([kExprLocalGet, 0, kExprTableGet, table_index])
.exportFunc(); .exportFunc();
...@@ -307,15 +312,18 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]); ...@@ -307,15 +312,18 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]);
.addBody([kExprLocalGet, 0, kExprTableGet, table_index]) .addBody([kExprLocalGet, 0, kExprTableGet, table_index])
.exportFunc(); .exportFunc();
let table_func = new WebAssembly.Table({element: "anyfunc", initial: 2, maximum: 10}); let table_func = new WebAssembly.Table({element: "anyfunc", initial: 2,
maximum: 10});
assertThrows(() => builder.instantiate({imp: {table: table_func}}), assertThrows(() => builder.instantiate({imp: {table: table_func}}),
WebAssembly.LinkError, /imported table does not match the expected type/); WebAssembly.LinkError, /imported table does not match the expected type/);
let table_any = new WebAssembly.Table({element: "anyref", initial: 2, maximum: 10}); let table_any = new WebAssembly.Table({element: "anyref", initial: 2,
maximum: 10});
assertThrows(() => builder.instantiate({imp: {table: table_any}}), assertThrows(() => builder.instantiate({imp: {table: table_any}}),
WebAssembly.LinkError, /imported table does not match the expected type/); WebAssembly.LinkError, /imported table does not match the expected type/);
let table_null = new WebAssembly.Table({element: "nullref", initial: 2, maximum: 10}); let table_null = new WebAssembly.Table({element: "nullref", initial: 2,
maximum: 10});
table_null.set(1, null); table_null.set(1, null);
const instance = builder.instantiate({imp: {table: table_null}}); const instance = builder.instantiate({imp: {table: table_null}});
...@@ -336,12 +344,57 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]); ...@@ -336,12 +344,57 @@ let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]);
(function testImportNullRefTableIntoAnyRefTable() { (function testImportNullRefTableIntoAnyRefTable() {
print(arguments.callee.name); print(arguments.callee.name);
const builder = new WasmModuleBuilder(); const builder = new WasmModuleBuilder();
const table_index = builder.addImportedTable("imp", "table", 2, 10, kWasmAnyRef); const table_index = builder.addImportedTable("imp", "table", 2, 10,
kWasmAnyRef);
builder.addFunction('get', kSig_r_v) builder.addFunction('get', kSig_r_v)
.addBody([kExprI32Const, 0, kExprTableGet, table_index]) .addBody([kExprI32Const, 0, kExprTableGet, table_index])
.exportFunc(); .exportFunc();
let table_null = new WebAssembly.Table({element: "nullref", initial: 2, maximum: 10}); let table_null = new WebAssembly.Table({element: "nullref", initial: 2,
maximum: 10});
assertThrows(() => builder.instantiate({imp: {table: table_null}}), assertThrows(() => builder.instantiate({imp: {table: table_null}}),
WebAssembly.LinkError, /imported table does not match the expected type/); WebAssembly.LinkError, /imported table does not match the expected type/);
})(); })();
(function testSelectNullRef() {
print(arguments.callee.name);
const builder = new WasmModuleBuilder();
builder.addFunction('select_null', kSig_n_nni)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprLocalGet, 2,
kExprSelectWithType, 1, kWasmNullRef])
.exportFunc();
builder.addFunction('select_any', kSig_r_nni)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprLocalGet, 2,
kExprSelectWithType, 1, kWasmAnyRef])
.exportFunc();
builder.addFunction('select_func', kSig_a_nni)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprLocalGet, 2,
kExprSelectWithType, 1, kWasmAnyFunc])
.exportFunc();
builder.addFunction('select_exn', kSig_e_nni)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprLocalGet, 2,
kExprSelectWithType, 1, kWasmExnRef])
.exportFunc();
builder.addFunction('select_null_as_any', kSig_r_nni)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprLocalGet, 2,
kExprSelectWithType, 1, kWasmNullRef])
.exportFunc();
builder.addFunction('select_null_as_func', kSig_a_nni)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprLocalGet, 2,
kExprSelectWithType, 1, kWasmNullRef])
.exportFunc();
builder.addFunction('select_null_as_exn', kSig_e_nni)
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprLocalGet, 2,
kExprSelectWithType, 1, kWasmNullRef])
.exportFunc();
const instance = builder.instantiate();
assertEquals(null, instance.exports.select_null(null, null, 0));
assertEquals(null, instance.exports.select_any(null, null, 0));
assertEquals(null, instance.exports.select_func(null, null, 0));
assertEquals(null, instance.exports.select_exn(null, null, 0));
assertEquals(null, instance.exports.select_null_as_any(null, null, 0));
assertEquals(null, instance.exports.select_null_as_func(null, null, 0));
assertEquals(null, instance.exports.select_null_as_exn(null, null, 0));
})();
...@@ -209,6 +209,7 @@ let kExprReturnCall = 0x12; ...@@ -209,6 +209,7 @@ let kExprReturnCall = 0x12;
let kExprReturnCallIndirect = 0x13; let kExprReturnCallIndirect = 0x13;
let kExprDrop = 0x1a; let kExprDrop = 0x1a;
let kExprSelect = 0x1b; let kExprSelect = 0x1b;
let kExprSelectWithType = 0x1c;
let kExprLocalGet = 0x20; let kExprLocalGet = 0x20;
let kExprLocalSet = 0x21; let kExprLocalSet = 0x21;
let kExprLocalTee = 0x22; let kExprLocalTee = 0x22;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment