Commit 53137df8 authored by mtrofin's avatar mtrofin Committed by Commit bot

[wasm] JS APIs & memory

- updated WebAssembly.Instance ctor uses in our tests to match spec
- disallowing mem section *and* mem import, as per spec

BUG=v8:5824

Review-Url: https://codereview.chromium.org/2627763002
Cr-Commit-Position: refs/heads/master@{#42278}
parent d3d57eba
...@@ -324,11 +324,7 @@ class ModuleDecoder : public Decoder { ...@@ -324,11 +324,7 @@ class ModuleDecoder : public Decoder {
&module->min_mem_pages, &has_max, &module->min_mem_pages, &has_max,
kSpecMaxWasmMemoryPages, kSpecMaxWasmMemoryPages,
&module->max_mem_pages); &module->max_mem_pages);
if (module->has_memory) { SetHasMemory(module);
error("At most one memory object is supported");
} else {
module->has_memory = true;
}
break; break;
} }
case kExternalGlobal: { case kExternalGlobal: {
...@@ -403,7 +399,7 @@ class ModuleDecoder : public Decoder { ...@@ -403,7 +399,7 @@ class ModuleDecoder : public Decoder {
"memory", "pages", kV8MaxWasmMemoryPages, &module->min_mem_pages, "memory", "pages", kV8MaxWasmMemoryPages, &module->min_mem_pages,
&has_max, kSpecMaxWasmMemoryPages, &module->max_mem_pages); &has_max, kSpecMaxWasmMemoryPages, &module->max_mem_pages);
} }
module->has_memory = true; SetHasMemory(module);
section_iter.advance(); section_iter.advance();
} }
...@@ -684,6 +680,14 @@ class ModuleDecoder : public Decoder { ...@@ -684,6 +680,14 @@ class ModuleDecoder : public Decoder {
uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); } uint32_t off(const byte* ptr) { return static_cast<uint32_t>(ptr - start_); }
void SetHasMemory(WasmModule* module) {
if (module->has_memory) {
error("At most one memory object is supported");
} else {
module->has_memory = true;
}
}
// Decodes a single global entry inside a module starting at {pc_}. // Decodes a single global entry inside a module starting at {pc_}.
void DecodeGlobalInModule(WasmModule* module, uint32_t index, void DecodeGlobalInModule(WasmModule* module, uint32_t index,
WasmGlobal* global) { WasmGlobal* global) {
......
...@@ -9,7 +9,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); ...@@ -9,7 +9,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
(function SerializeAndDeserializeModule() { (function SerializeAndDeserializeModule() {
var builder = new WasmModuleBuilder(); var builder = new WasmModuleBuilder();
builder.addMemory(1,1, true); builder.addImportedMemory("", "memory", 1,1);
var kSig_v_i = makeSig([kWasmI32], []); var kSig_v_i = makeSig([kWasmI32], []);
var signature = builder.addType(kSig_v_i); var signature = builder.addType(kSig_v_i);
builder.addImport("", "some_value", kSig_i_v); builder.addImport("", "some_value", kSig_i_v);
...@@ -48,8 +48,11 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); ...@@ -48,8 +48,11 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
view_1[0] = 42; view_1[0] = 42;
var outval_1; var outval_1;
var i1 = new WebAssembly.Instance(module, {"": {some_value: () => 1, var i1 = new WebAssembly.Instance(module, {"":
writer: (x)=>outval_1 = x }}, mem_1); {some_value: () => 1,
writer: (x) => outval_1 = x ,
memory: mem_1}
});
assertEquals(43, i1.exports.main(0)); assertEquals(43, i1.exports.main(0));
......
...@@ -58,7 +58,6 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); ...@@ -58,7 +58,6 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
var i2; var i2;
{ {
let builder = new WasmModuleBuilder(); let builder = new WasmModuleBuilder();
builder.addMemory(1, 1, false);
builder.addImportedMemory("fil", "imported_mem"); builder.addImportedMemory("fil", "imported_mem");
builder.addFunction("bar", kSig_i_i) builder.addFunction("bar", kSig_i_i)
.addBody([ .addBody([
......
...@@ -29,7 +29,7 @@ function CheckInstance(instance) { ...@@ -29,7 +29,7 @@ function CheckInstance(instance) {
assertFalse(Object.isExtensible(instance.exports)); assertFalse(Object.isExtensible(instance.exports));
assertTrue(Object.isFrozen(instance.exports)); assertTrue(Object.isFrozen(instance.exports));
// Check the memory is an ArrayBuffer. // Check the memory is WebAssembly.Memory.
var mem = instance.exports.memory; var mem = instance.exports.memory;
assertFalse(mem === undefined); assertFalse(mem === undefined);
assertFalse(mem === null); assertFalse(mem === null);
...@@ -126,7 +126,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -126,7 +126,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
(function InstancesAreIsolatedFromEachother() { (function InstancesAreIsolatedFromEachother() {
print("InstancesAreIsolatedFromEachother..."); print("InstancesAreIsolatedFromEachother...");
var builder = new WasmModuleBuilder(); var builder = new WasmModuleBuilder();
builder.addMemory(1,1, true); builder.addImportedMemory("", "memory", 1,1);
var kSig_v_i = makeSig([kWasmI32], []); var kSig_v_i = makeSig([kWasmI32], []);
var signature = builder.addType(kSig_v_i); var signature = builder.addType(kSig_v_i);
builder.addImport("m", "some_value", kSig_i_v); builder.addImport("m", "some_value", kSig_i_v);
...@@ -165,11 +165,12 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -165,11 +165,12 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
var outval_1; var outval_1;
var outval_2; var outval_2;
var i1 = new WebAssembly.Instance(module, {m: {some_value: () => 1, var i1 = new WebAssembly.Instance(module, {m: {some_value: () => 1,
writer: (x)=>outval_1 = x }}, writer: (x)=>outval_1 = x },
mem_1); "": {memory: mem_1}});
var i2 = new WebAssembly.Instance(module, {m: {some_value: () => 2, var i2 = new WebAssembly.Instance(module, {m: {some_value: () => 2,
writer: (x)=>outval_2 = x }}, writer: (x)=>outval_2 = x },
mem_2); "": {memory: mem_2}});
assertEquals(43, i1.exports.main(0)); assertEquals(43, i1.exports.main(0));
assertEquals(1002, i2.exports.main(0)); assertEquals(1002, i2.exports.main(0));
...@@ -206,7 +207,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -206,7 +207,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
(function InstanceMemoryIsIsolated() { (function InstanceMemoryIsIsolated() {
print("InstanceMemoryIsIsolated..."); print("InstanceMemoryIsIsolated...");
var builder = new WasmModuleBuilder(); var builder = new WasmModuleBuilder();
builder.addMemory(1,1, true); builder.addImportedMemory("", "memory", 1,1);
builder.addFunction("f", kSig_i_v) builder.addFunction("f", kSig_i_v)
.addBody([ .addBody([
...@@ -222,8 +223,8 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -222,8 +223,8 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
view_2[0] = 1000; view_2[0] = 1000;
var module = new WebAssembly.Module(builder.toBuffer()); var module = new WebAssembly.Module(builder.toBuffer());
var i1 = new WebAssembly.Instance(module, null, mem_1); var i1 = new WebAssembly.Instance(module, {"":{memory:mem_1}});
var i2 = new WebAssembly.Instance(module, null, mem_2); var i2 = new WebAssembly.Instance(module, {"":{memory:mem_2}});
assertEquals(1, i1.exports.f()); assertEquals(1, i1.exports.f());
assertEquals(1000, i2.exports.f()); assertEquals(1000, i2.exports.f());
...@@ -232,6 +233,11 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -232,6 +233,11 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
(function MustBeMemory() { (function MustBeMemory() {
print("MustBeMemory..."); print("MustBeMemory...");
var memory = new ArrayBuffer(65536); var memory = new ArrayBuffer(65536);
var module = new WebAssembly.Module(buffer); let builder = new WasmModuleBuilder();
assertThrows(() => new WebAssembly.Instance(module, null, memory), TypeError); builder.addImportedMemory("", "memory");
let module = new WebAssembly.Module(builder.toBuffer());
assertThrows(() => new WebAssembly.Instance(module, {"":{memory:memory}}), WebAssembly.LinkError);
})(); })();
...@@ -48,6 +48,13 @@ let moduleBinaryImporting2Memories = (() => { ...@@ -48,6 +48,13 @@ let moduleBinaryImporting2Memories = (() => {
return new Int8Array(builder.toBuffer()); return new Int8Array(builder.toBuffer());
})(); })();
let moduleBinaryWithMemSectionAndMemImport = (() => {
var builder = new WasmModuleBuilder();
builder.addMemory(1, 1, false);
builder.addImportedMemory("", "memory1");
return new Int8Array(builder.toBuffer());
})();
// 'WebAssembly' data property on global object // 'WebAssembly' data property on global object
let wasmDesc = Object.getOwnPropertyDescriptor(this, 'WebAssembly'); let wasmDesc = Object.getOwnPropertyDescriptor(this, 'WebAssembly');
assertEq(typeof wasmDesc.value, "object"); assertEq(typeof wasmDesc.value, "object");
...@@ -511,6 +518,7 @@ assertErrorMessage(() => WebAssembly.validate("hi"), TypeError); ...@@ -511,6 +518,7 @@ assertErrorMessage(() => WebAssembly.validate("hi"), TypeError);
assertEq(WebAssembly.validate(emptyModuleBinary), true); assertEq(WebAssembly.validate(emptyModuleBinary), true);
// TODO: other ways for validate to return false. // TODO: other ways for validate to return false.
assertEq(WebAssembly.validate(moduleBinaryImporting2Memories), false); assertEq(WebAssembly.validate(moduleBinaryImporting2Memories), false);
assertEq(WebAssembly.validate(moduleBinaryWithMemSectionAndMemImport), false);
// 'WebAssembly.compile' data property // 'WebAssembly.compile' data property
let compileDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'compile'); let compileDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'compile');
......
...@@ -12,7 +12,8 @@ var kMemSize = 65536; ...@@ -12,7 +12,8 @@ var kMemSize = 65536;
function genModule(memory) { function genModule(memory) {
var builder = new WasmModuleBuilder(); var builder = new WasmModuleBuilder();
builder.addMemory(1, 1, true); builder.addImportedMemory("", "memory", 1, 1);
builder.exportMemoryAs("memory");
builder.addFunction("main", kSig_i_i) builder.addFunction("main", kSig_i_i)
.addBody([ .addBody([
// main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0; // main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0;
...@@ -36,7 +37,7 @@ function genModule(memory) { ...@@ -36,7 +37,7 @@ function genModule(memory) {
/**/ kExprI32Const, 0 // -- /**/ kExprI32Const, 0 // --
]) ])
.exportFunc(); .exportFunc();
var module = builder.instantiate(null, memory); var module = builder.instantiate({"": {memory:memory}});
assertTrue(module.exports.memory instanceof WebAssembly.Memory); assertTrue(module.exports.memory instanceof WebAssembly.Memory);
if (memory != null) assertEquals(memory.buffer, module.exports.memory.buffer); if (memory != null) assertEquals(memory.buffer, module.exports.memory.buffer);
return module; return module;
...@@ -44,7 +45,7 @@ function genModule(memory) { ...@@ -44,7 +45,7 @@ function genModule(memory) {
function testPokeMemory() { function testPokeMemory() {
print("testPokeMemory"); print("testPokeMemory");
var module = genModule(null); var module = genModule(new WebAssembly.Memory({initial: 1}));
var buffer = module.exports.memory.buffer; var buffer = module.exports.memory.buffer;
var main = module.exports.main; var main = module.exports.main;
assertEquals(kMemSize, buffer.byteLength); assertEquals(kMemSize, buffer.byteLength);
...@@ -75,7 +76,7 @@ function genAndGetMain(buffer) { ...@@ -75,7 +76,7 @@ function genAndGetMain(buffer) {
} }
function testSurvivalAcrossGc() { function testSurvivalAcrossGc() {
var checker = genAndGetMain(null); var checker = genAndGetMain(new WebAssembly.Memory({initial: 1}));
for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) {
print("gc run ", i); print("gc run ", i);
assertEquals(0, checker(kMemSize - 4)); assertEquals(0, checker(kMemSize - 4));
......
...@@ -547,9 +547,9 @@ class WasmModuleBuilder { ...@@ -547,9 +547,9 @@ class WasmModuleBuilder {
return buffer; return buffer;
} }
instantiate(...args) { instantiate(ffi) {
let module = new WebAssembly.Module(this.toBuffer()); let module = new WebAssembly.Module(this.toBuffer());
let instance = new WebAssembly.Instance(module, ...args); let instance = new WebAssembly.Instance(module, ffi);
return instance; return instance;
} }
} }
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