[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}
This commit is contained in:
mtrofin 2017-01-12 08:48:09 -08:00 committed by Commit bot
parent d3d57ebaf6
commit 53137df89a
7 changed files with 48 additions and 27 deletions

View File

@ -324,11 +324,7 @@ class ModuleDecoder : public Decoder {
&module->min_mem_pages, &has_max,
kSpecMaxWasmMemoryPages,
&module->max_mem_pages);
if (module->has_memory) {
error("At most one memory object is supported");
} else {
module->has_memory = true;
}
SetHasMemory(module);
break;
}
case kExternalGlobal: {
@ -403,7 +399,7 @@ class ModuleDecoder : public Decoder {
"memory", "pages", kV8MaxWasmMemoryPages, &module->min_mem_pages,
&has_max, kSpecMaxWasmMemoryPages, &module->max_mem_pages);
}
module->has_memory = true;
SetHasMemory(module);
section_iter.advance();
}
@ -684,6 +680,14 @@ class ModuleDecoder : public Decoder {
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_}.
void DecodeGlobalInModule(WasmModule* module, uint32_t index,
WasmGlobal* global) {

View File

@ -9,7 +9,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
(function SerializeAndDeserializeModule() {
var builder = new WasmModuleBuilder();
builder.addMemory(1,1, true);
builder.addImportedMemory("", "memory", 1,1);
var kSig_v_i = makeSig([kWasmI32], []);
var signature = builder.addType(kSig_v_i);
builder.addImport("", "some_value", kSig_i_v);
@ -48,8 +48,11 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
view_1[0] = 42;
var outval_1;
var i1 = new WebAssembly.Instance(module, {"": {some_value: () => 1,
writer: (x)=>outval_1 = x }}, mem_1);
var i1 = new WebAssembly.Instance(module, {"":
{some_value: () => 1,
writer: (x) => outval_1 = x ,
memory: mem_1}
});
assertEquals(43, i1.exports.main(0));

View File

@ -58,7 +58,6 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
var i2;
{
let builder = new WasmModuleBuilder();
builder.addMemory(1, 1, false);
builder.addImportedMemory("fil", "imported_mem");
builder.addFunction("bar", kSig_i_i)
.addBody([

View File

@ -29,7 +29,7 @@ function CheckInstance(instance) {
assertFalse(Object.isExtensible(instance.exports));
assertTrue(Object.isFrozen(instance.exports));
// Check the memory is an ArrayBuffer.
// Check the memory is WebAssembly.Memory.
var mem = instance.exports.memory;
assertFalse(mem === undefined);
assertFalse(mem === null);
@ -126,7 +126,7 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
(function InstancesAreIsolatedFromEachother() {
print("InstancesAreIsolatedFromEachother...");
var builder = new WasmModuleBuilder();
builder.addMemory(1,1, true);
builder.addImportedMemory("", "memory", 1,1);
var kSig_v_i = makeSig([kWasmI32], []);
var signature = builder.addType(kSig_v_i);
builder.addImport("m", "some_value", kSig_i_v);
@ -165,11 +165,12 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
var outval_1;
var outval_2;
var i1 = new WebAssembly.Instance(module, {m: {some_value: () => 1,
writer: (x)=>outval_1 = x }},
mem_1);
writer: (x)=>outval_1 = x },
"": {memory: mem_1}});
var i2 = new WebAssembly.Instance(module, {m: {some_value: () => 2,
writer: (x)=>outval_2 = x }},
mem_2);
writer: (x)=>outval_2 = x },
"": {memory: mem_2}});
assertEquals(43, i1.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)));
(function InstanceMemoryIsIsolated() {
print("InstanceMemoryIsIsolated...");
var builder = new WasmModuleBuilder();
builder.addMemory(1,1, true);
builder.addImportedMemory("", "memory", 1,1);
builder.addFunction("f", kSig_i_v)
.addBody([
@ -222,8 +223,8 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
view_2[0] = 1000;
var module = new WebAssembly.Module(builder.toBuffer());
var i1 = new WebAssembly.Instance(module, null, mem_1);
var i2 = new WebAssembly.Instance(module, null, mem_2);
var i1 = new WebAssembly.Instance(module, {"":{memory:mem_1}});
var i2 = new WebAssembly.Instance(module, {"":{memory:mem_2}});
assertEquals(1, i1.exports.f());
assertEquals(1000, i2.exports.f());
@ -232,6 +233,11 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
(function MustBeMemory() {
print("MustBeMemory...");
var memory = new ArrayBuffer(65536);
var module = new WebAssembly.Module(buffer);
assertThrows(() => new WebAssembly.Instance(module, null, memory), TypeError);
let builder = new WasmModuleBuilder();
builder.addImportedMemory("", "memory");
let module = new WebAssembly.Module(builder.toBuffer());
assertThrows(() => new WebAssembly.Instance(module, {"":{memory:memory}}), WebAssembly.LinkError);
})();

View File

@ -48,6 +48,13 @@ let moduleBinaryImporting2Memories = (() => {
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
let wasmDesc = Object.getOwnPropertyDescriptor(this, 'WebAssembly');
assertEq(typeof wasmDesc.value, "object");
@ -511,6 +518,7 @@ assertErrorMessage(() => WebAssembly.validate("hi"), TypeError);
assertEq(WebAssembly.validate(emptyModuleBinary), true);
// TODO: other ways for validate to return false.
assertEq(WebAssembly.validate(moduleBinaryImporting2Memories), false);
assertEq(WebAssembly.validate(moduleBinaryWithMemSectionAndMemImport), false);
// 'WebAssembly.compile' data property
let compileDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'compile');

View File

@ -12,7 +12,8 @@ var kMemSize = 65536;
function genModule(memory) {
var builder = new WasmModuleBuilder();
builder.addMemory(1, 1, true);
builder.addImportedMemory("", "memory", 1, 1);
builder.exportMemoryAs("memory");
builder.addFunction("main", kSig_i_i)
.addBody([
// main body: while(i) { if(mem[i]) return -1; i -= 4; } return 0;
@ -36,7 +37,7 @@ function genModule(memory) {
/**/ kExprI32Const, 0 // --
])
.exportFunc();
var module = builder.instantiate(null, memory);
var module = builder.instantiate({"": {memory:memory}});
assertTrue(module.exports.memory instanceof WebAssembly.Memory);
if (memory != null) assertEquals(memory.buffer, module.exports.memory.buffer);
return module;
@ -44,7 +45,7 @@ function genModule(memory) {
function testPokeMemory() {
print("testPokeMemory");
var module = genModule(null);
var module = genModule(new WebAssembly.Memory({initial: 1}));
var buffer = module.exports.memory.buffer;
var main = module.exports.main;
assertEquals(kMemSize, buffer.byteLength);
@ -75,7 +76,7 @@ function genAndGetMain(buffer) {
}
function testSurvivalAcrossGc() {
var checker = genAndGetMain(null);
var checker = genAndGetMain(new WebAssembly.Memory({initial: 1}));
for (var i = 0; i < 3; i++) {
print("gc run ", i);
assertEquals(0, checker(kMemSize - 4));

View File

@ -547,9 +547,9 @@ class WasmModuleBuilder {
return buffer;
}
instantiate(...args) {
instantiate(ffi) {
let module = new WebAssembly.Module(this.toBuffer());
let instance = new WebAssembly.Instance(module, ...args);
let instance = new WebAssembly.Instance(module, ffi);
return instance;
}
}