Commit 96d12ac6 authored by eholk's avatar eholk Committed by Commit bot

[wasm] make WebAssembly.Instance require a WebAssembly.Memory

BUG=

Review-Url: https://codereview.chromium.org/2460773003
Cr-Commit-Position: refs/heads/master@{#40654}
parent 92d9a56a
...@@ -229,10 +229,14 @@ void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -229,10 +229,14 @@ void WebAssemblyInstance(const v8::FunctionCallbackInfo<v8::Value>& args) {
} }
i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null(); i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
if (args.Length() > 2 && args[2]->IsArrayBuffer()) { if (args.Length() > 2 && args[2]->IsObject()) {
Local<Object> obj = Local<Object>::Cast(args[2]); Local<Object> obj = Local<Object>::Cast(args[2]);
i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj); i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
memory = i::Handle<i::JSArrayBuffer>(i::JSArrayBuffer::cast(*mem_obj)); if (i::WasmJs::IsWasmMemoryObject(i_isolate, mem_obj)) {
memory = i::WasmJs::GetWasmMemoryArrayBuffer(i_isolate, mem_obj);
} else {
thrower.TypeError("Argument 2 must be a WebAssembly.Memory");
}
} }
i::MaybeHandle<i::JSObject> instance = i::MaybeHandle<i::JSObject> instance =
i::wasm::WasmModule::Instantiate(i_isolate, &thrower, i_obj, ffi, memory); i::wasm::WasmModule::Instantiate(i_isolate, &thrower, i_obj, ffi, memory);
......
...@@ -42,8 +42,8 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); ...@@ -42,8 +42,8 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
gc(); gc();
module = %DeserializeWasmModule(buff, wire_bytes); module = %DeserializeWasmModule(buff, wire_bytes);
var mem_1 = new ArrayBuffer(4); var mem_1 = new WebAssembly.Memory({initial: 1});
var view_1 = new Int32Array(mem_1); var view_1 = new Int32Array(mem_1.buffer);
view_1[0] = 42; view_1[0] = 42;
......
...@@ -141,10 +141,10 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -141,10 +141,10 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
var module = new WebAssembly.Module(builder.toBuffer()); var module = new WebAssembly.Module(builder.toBuffer());
var mem_1 = new ArrayBuffer(4); var mem_1 = new WebAssembly.Memory({initial: 1});
var mem_2 = new ArrayBuffer(4); var mem_2 = new WebAssembly.Memory({initial: 1});
var view_1 = new Int32Array(mem_1); var view_1 = new Int32Array(mem_1.buffer);
var view_2 = new Int32Array(mem_2); var view_2 = new Int32Array(mem_2.buffer);
view_1[0] = 42; view_1[0] = 42;
view_2[0] = 1000; view_2[0] = 1000;
...@@ -199,10 +199,10 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -199,10 +199,10 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
kExprI32LoadMem, 0, 0 kExprI32LoadMem, 0, 0
]).exportFunc(); ]).exportFunc();
var mem_1 = new ArrayBuffer(65536); var mem_1 = new WebAssembly.Memory({initial: 1});
var mem_2 = new ArrayBuffer(65536); var mem_2 = new WebAssembly.Memory({initial: 1});
var view_1 = new Int32Array(mem_1); var view_1 = new Int32Array(mem_1.buffer);
var view_2 = new Int32Array(mem_2); var view_2 = new Int32Array(mem_2.buffer);
view_1[0] = 1; view_1[0] = 1;
view_2[0] = 1000; view_2[0] = 1000;
...@@ -213,3 +213,9 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88))); ...@@ -213,3 +213,9 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
assertEquals(1, i1.exports.f()); assertEquals(1, i1.exports.f());
assertEquals(1000, i2.exports.f()); assertEquals(1000, i2.exports.f());
})(); })();
(function MustBeMemory() {
var memory = new ArrayBuffer(65536);
var module = new WebAssembly.Module(buffer);
assertThrows(() => new WebAssembly.Instance(module, null, memory), TypeError);
})();
...@@ -38,11 +38,12 @@ function genModule(memory) { ...@@ -38,11 +38,12 @@ function genModule(memory) {
.exportFunc(); .exportFunc();
var module = builder.instantiate(null, memory); var module = builder.instantiate(null, memory);
assertTrue(module.exports.memory instanceof WebAssembly.Memory); assertTrue(module.exports.memory instanceof WebAssembly.Memory);
if (memory != null) assertEquals(memory, module.exports.memory.buffer); if (memory != null) assertEquals(memory.buffer, module.exports.memory.buffer);
return module; return module;
} }
function testPokeMemory() { function testPokeMemory() {
print("testPokeMemory");
var module = genModule(null); var module = genModule(null);
var buffer = module.exports.memory.buffer; var buffer = module.exports.memory.buffer;
var main = module.exports.main; var main = module.exports.main;
...@@ -89,12 +90,13 @@ testSurvivalAcrossGc(); ...@@ -89,12 +90,13 @@ testSurvivalAcrossGc();
function testPokeOuterMemory() { function testPokeOuterMemory() {
var buffer = new ArrayBuffer(kMemSize); print("testPokeOuterMemory");
var buffer = new WebAssembly.Memory({initial: kMemSize / kPageSize});
var module = genModule(buffer); var module = genModule(buffer);
var main = module.exports.main; var main = module.exports.main;
assertEquals(kMemSize, buffer.byteLength); assertEquals(kMemSize, buffer.buffer.byteLength);
var array = new Int8Array(buffer); var array = new Int8Array(buffer.buffer);
assertEquals(kMemSize, array.length); assertEquals(kMemSize, array.length);
for (var i = 0; i < kMemSize; i++) { for (var i = 0; i < kMemSize; i++) {
...@@ -116,7 +118,7 @@ function testPokeOuterMemory() { ...@@ -116,7 +118,7 @@ function testPokeOuterMemory() {
testPokeOuterMemory(); testPokeOuterMemory();
function testOuterMemorySurvivalAcrossGc() { function testOuterMemorySurvivalAcrossGc() {
var buffer = new ArrayBuffer(kMemSize); var buffer = new WebAssembly.Memory({initial: kMemSize / kPageSize});
var checker = genAndGetMain(buffer); var checker = genAndGetMain(buffer);
for (var i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) {
print("gc run ", i); print("gc run ", i);
......
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