From 24d38be132d66783406e8f18595218d47ef1edda Mon Sep 17 00:00:00 2001 From: titzer Date: Wed, 26 Oct 2016 09:58:40 -0700 Subject: [PATCH] [wasm] Remove the "Wasm" object. BUG=chromium:575167, v8:5507 R=rossberg@chromium.org,bradnelson@chromium.org CC=ahaas@chromium.org Review-Url: https://codereview.chromium.org/2447013004 Cr-Commit-Position: refs/heads/master@{#40601} --- src/wasm/wasm-js.cc | 137 +----------------- test/mjsunit/regress/regress-575364.js | 1 - test/mjsunit/wasm/test-wasm-module-builder.js | 92 ++++++------ test/mjsunit/wasm/wasm-object-api.js | 16 +- 4 files changed, 53 insertions(+), 193 deletions(-) diff --git a/src/wasm/wasm-js.cc b/src/wasm/wasm-js.cc index 6fde52b90b..72482388ed 100644 --- a/src/wasm/wasm-js.cc +++ b/src/wasm/wasm-js.cc @@ -92,108 +92,6 @@ RawBuffer GetRawBufferSource( return {start, end}; } -void VerifyModule(const v8::FunctionCallbackInfo& args) { - HandleScope scope(args.GetIsolate()); - i::Isolate* isolate = reinterpret_cast(args.GetIsolate()); - ErrorThrower thrower(isolate, "Wasm.verifyModule()"); - - if (args.Length() < 1) { - thrower.TypeError("Argument 0 must be a buffer source"); - return; - } - RawBuffer buffer = GetRawBufferSource(args[0], &thrower); - if (thrower.error()) return; - - internal::wasm::ModuleResult result = internal::wasm::DecodeWasmModule( - isolate, buffer.start, buffer.end, true, internal::wasm::kWasmOrigin); - - if (result.failed()) { - thrower.CompileFailed("", result); - } - - if (result.val) delete result.val; -} - -void VerifyFunction(const v8::FunctionCallbackInfo& args) { - HandleScope scope(args.GetIsolate()); - i::Isolate* isolate = reinterpret_cast(args.GetIsolate()); - ErrorThrower thrower(isolate, "Wasm.verifyFunction()"); - - if (args.Length() < 1) { - thrower.TypeError("Argument 0 must be a buffer source"); - return; - } - RawBuffer buffer = GetRawBufferSource(args[0], &thrower); - if (thrower.error()) return; - - internal::wasm::FunctionResult result; - { - // Verification of a single function shouldn't allocate. - i::DisallowHeapAllocation no_allocation; - i::Zone zone(isolate->allocator(), ZONE_NAME); - result = internal::wasm::DecodeWasmFunction(isolate, &zone, nullptr, - buffer.start, buffer.end); - } - - if (result.failed()) { - thrower.CompileFailed("", result); - } - - if (result.val) delete result.val; -} - -i::MaybeHandle InstantiateModule( - const v8::FunctionCallbackInfo& args, const byte* start, - const byte* end, ErrorThrower* thrower) { - i::Isolate* isolate = reinterpret_cast(args.GetIsolate()); - - // Decode but avoid a redundant pass over function bodies for verification. - // Verification will happen during compilation. - i::Zone zone(isolate->allocator(), ZONE_NAME); - i::MaybeHandle module_object = - i::wasm::CreateModuleObjectFromBytes( - isolate, start, end, thrower, i::wasm::kWasmOrigin, - i::Handle::null(), nullptr, nullptr); - i::MaybeHandle object; - if (!module_object.is_null()) { - // Success. Instantiate the module and return the object. - i::Handle ffi = i::Handle::null(); - if (args.Length() > 1 && args[1]->IsObject()) { - Local obj = Local::Cast(args[1]); - ffi = i::Handle::cast(v8::Utils::OpenHandle(*obj)); - } - - i::Handle memory = i::Handle::null(); - if (args.Length() > 2 && args[2]->IsArrayBuffer()) { - Local obj = Local::Cast(args[2]); - i::Handle mem_obj = v8::Utils::OpenHandle(*obj); - memory = i::Handle(i::JSArrayBuffer::cast(*mem_obj)); - } - - object = i::wasm::WasmModule::Instantiate( - isolate, thrower, module_object.ToHandleChecked(), ffi, memory); - if (!object.is_null()) { - args.GetReturnValue().Set(v8::Utils::ToLocal(object.ToHandleChecked())); - } - } - return object; -} - -void InstantiateModule(const v8::FunctionCallbackInfo& args) { - HandleScope scope(args.GetIsolate()); - i::Isolate* isolate = reinterpret_cast(args.GetIsolate()); - ErrorThrower thrower(isolate, "Wasm.instantiateModule()"); - - if (args.Length() < 1) { - thrower.TypeError("Argument 0 must be a buffer source"); - return; - } - RawBuffer buffer = GetRawBufferSource(args[0], &thrower); - if (buffer.start == nullptr) return; - - InstantiateModule(args, buffer.start, buffer.end, &thrower); -} - static i::MaybeHandle CreateModuleObject( v8::Isolate* isolate, const v8::Local source, ErrorThrower* thrower) { @@ -877,44 +775,13 @@ void WasmJs::Install(Isolate* isolate, Handle global) { return; } - Factory* factory = isolate->factory(); - // Setup wasm function map. Handle context(global->native_context(), isolate); InstallWasmMapsIfNeeded(isolate, context); - if (!FLAG_expose_wasm) { - return; + if (FLAG_expose_wasm) { + InstallWasmConstructors(isolate, global, context); } - - // Bind the experimental "Wasm" object. - // TODO(rossberg, titzer): remove once it's no longer needed. - { - Handle name = v8_str(isolate, "Wasm"); - Handle cons = factory->NewFunction(name); - JSFunction::SetInstancePrototype( - cons, Handle(context->initial_object_prototype(), isolate)); - cons->shared()->set_instance_class_name(*name); - Handle wasm_object = factory->NewJSObject(cons, TENURED); - PropertyAttributes attributes = static_cast(DONT_ENUM); - JSObject::AddProperty(global, name, wasm_object, attributes); - - // Install functions on the WASM object. - InstallFunc(isolate, wasm_object, "verifyModule", VerifyModule); - InstallFunc(isolate, wasm_object, "verifyFunction", VerifyFunction); - InstallFunc(isolate, wasm_object, "instantiateModule", InstantiateModule); - - { - // Add the Wasm.experimentalVersion property. - Handle name = v8_str(isolate, "experimentalVersion"); - PropertyAttributes attributes = - static_cast(DONT_DELETE | READ_ONLY); - Handle value = - Handle(Smi::FromInt(wasm::kWasmVersion), isolate); - JSObject::AddProperty(wasm_object, name, value, attributes); - } - } - InstallWasmConstructors(isolate, global, context); } void WasmJs::InstallWasmMapsIfNeeded(Isolate* isolate, diff --git a/test/mjsunit/regress/regress-575364.js b/test/mjsunit/regress/regress-575364.js index 8671aec06b..34957a23c1 100644 --- a/test/mjsunit/regress/regress-575364.js +++ b/test/mjsunit/regress/regress-575364.js @@ -8,5 +8,4 @@ function f() { "use asm"; } -assertFalse(Wasm == undefined); assertTrue(%IsNotAsmWasmCode(f)); diff --git a/test/mjsunit/wasm/test-wasm-module-builder.js b/test/mjsunit/wasm/test-wasm-module-builder.js index 8be2933b87..30bbe4ac93 100644 --- a/test/mjsunit/wasm/test-wasm-module-builder.js +++ b/test/mjsunit/wasm/test-wasm-module-builder.js @@ -9,40 +9,44 @@ load('test/mjsunit/wasm/wasm-module-builder.js'); var debug = true; +function instantiate(buffer, ffi) { + return new WebAssembly.Instance(WebAssembly.Module(buffer), ffi); +} + (function BasicTest() { - var module = new WasmModuleBuilder(); - module.addMemory(1, 2, false); - module.addFunction("foo", kSig_i_v) + let builder = new WasmModuleBuilder(); + builder.addMemory(1, 2, false); + builder.addFunction("foo", kSig_i_v) .addBody([kExprI8Const, 11]) .exportAs("blarg"); - var buffer = module.toBuffer(debug); - var instance = Wasm.instantiateModule(buffer); + var buffer = builder.toBuffer(debug); + var instance = instantiate(buffer); assertEquals(11, instance.exports.blarg()); })(); (function ImportTest() { - var module = new WasmModuleBuilder(); - var index = module.addImport("print", makeSig_v_x(kAstI32)); - module.addFunction("foo", kSig_v_v) + let builder = new WasmModuleBuilder(); + var index = builder.addImport("print", makeSig_v_x(kAstI32)); + builder.addFunction("foo", kSig_v_v) .addBody([kExprI8Const, 13, kExprCallFunction, index]) .exportAs("main"); - var buffer = module.toBuffer(debug); - var instance = Wasm.instantiateModule(buffer, {print: print}); + var buffer = builder.toBuffer(debug); + var instance = instantiate(buffer, {print: print}); print("should print 13! "); instance.exports.main(); })(); (function LocalsTest() { - var module = new WasmModuleBuilder(); - module.addFunction(undefined, kSig_i_i) + let builder = new WasmModuleBuilder(); + builder.addFunction(undefined, kSig_i_i) .addLocals({i32_count: 1}) .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) .exportAs("main"); - var buffer = module.toBuffer(debug); - var instance = Wasm.instantiateModule(buffer); + var buffer = builder.toBuffer(debug); + var instance = instantiate(buffer); assertEquals(19, instance.exports.main(19)); assertEquals(27777, instance.exports.main(27777)); })(); @@ -57,72 +61,72 @@ var debug = true; ]; for (p of types) { - var module = new WasmModuleBuilder(); - module.addFunction(undefined, makeSig_r_x(p.type, p.type)) + let builder = new WasmModuleBuilder(); + builder.addFunction(undefined, makeSig_r_x(p.type, p.type)) .addLocals(p.locals) .addBody([kExprGetLocal, 0, kExprSetLocal, 1, kExprGetLocal, 1]) .exportAs("main"); - var buffer = module.toBuffer(debug); - var instance = Wasm.instantiateModule(buffer); + var buffer = builder.toBuffer(debug); + var instance = instantiate(buffer); assertEquals(19, instance.exports.main(19)); assertEquals(27777, instance.exports.main(27777)); } })(); (function CallTest() { - var module = new WasmModuleBuilder(); - module.addFunction("add", kSig_i_ii) + let builder = new WasmModuleBuilder(); + builder.addFunction("add", kSig_i_ii) .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); - module.addFunction("main", kSig_i_ii) + builder.addFunction("main", kSig_i_ii) .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprCallFunction, 0]) .exportAs("main"); - var instance = module.instantiate(); + var instance = builder.instantiate(); assertEquals(44, instance.exports.main(11, 33)); assertEquals(7777, instance.exports.main(2222, 5555)); })(); (function IndirectCallTest() { - var module = new WasmModuleBuilder(); - module.addFunction("add", kSig_i_ii) + let builder = new WasmModuleBuilder(); + builder.addFunction("add", kSig_i_ii) .addBody([kExprGetLocal, 0, kExprGetLocal, 1, kExprI32Add]); - module.addFunction("main", kSig_i_iii) + builder.addFunction("main", kSig_i_iii) .addBody([kExprGetLocal, 1, kExprGetLocal, 2, kExprGetLocal, 0, kExprCallIndirect, 0, kTableZero]) .exportAs("main"); - module.appendToTable([0]); + builder.appendToTable([0]); - var instance = module.instantiate(); + var instance = builder.instantiate(); assertEquals(44, instance.exports.main(0, 11, 33)); assertEquals(7777, instance.exports.main(0, 2222, 5555)); assertThrows(function() { instance.exports.main(1, 1, 1); }); })(); (function DataSegmentTest() { - var module = new WasmModuleBuilder(); - module.addMemory(1, 1, false); - module.addFunction("load", kSig_i_i) + let builder = new WasmModuleBuilder(); + builder.addMemory(1, 1, false); + builder.addFunction("load", kSig_i_i) .addBody([kExprGetLocal, 0, kExprI32LoadMem, 0, 0]) .exportAs("load"); - module.addDataSegment(0, [9, 9, 9, 9]); + builder.addDataSegment(0, [9, 9, 9, 9]); - var buffer = module.toBuffer(debug); - var instance = Wasm.instantiateModule(buffer); + var buffer = builder.toBuffer(debug); + var instance = instantiate(buffer); assertEquals(151587081, instance.exports.load(0)); })(); (function BasicTestWithUint8Array() { - var module = new WasmModuleBuilder(); - module.addMemory(1, 2, false); - module.addFunction("foo", kSig_i_v) + let builder = new WasmModuleBuilder(); + builder.addMemory(1, 2, false); + builder.addFunction("foo", kSig_i_v) .addBody([kExprI8Const, 17]) .exportAs("blarg"); - var buffer = module.toBuffer(debug); + var buffer = builder.toBuffer(debug); var array = new Uint8Array(buffer); - var instance = Wasm.instantiateModule(array); + var instance = instantiate(array); assertEquals(17, instance.exports.blarg()); var kPad = 5; @@ -135,19 +139,19 @@ var debug = true; for (var i = 0; i < array2.byteLength; i++) { array2[i] = array[i]; } - var instance = Wasm.instantiateModule(array2); + var instance = instantiate(array2); assertEquals(17, instance.exports.blarg()); })(); (function ImportTestTwoLevel() { - var module = new WasmModuleBuilder(); - var index = module.addImportWithModule("mod", "print", makeSig_v_x(kAstI32)); - module.addFunction("foo", kSig_v_v) + let builder = new WasmModuleBuilder(); + var index = builder.addImportWithModule("mod", "print", makeSig_v_x(kAstI32)); + builder.addFunction("foo", kSig_v_v) .addBody([kExprI8Const, 19, kExprCallFunction, index]) .exportAs("main"); - var buffer = module.toBuffer(debug); - var instance = Wasm.instantiateModule(buffer, {mod: {print: print}}); + var buffer = builder.toBuffer(debug); + var instance = instantiate(buffer, {mod: {print: print}}); print("should print 19! "); instance.exports.main(); })(); diff --git a/test/mjsunit/wasm/wasm-object-api.js b/test/mjsunit/wasm/wasm-object-api.js index 2fa57b033b..3888e3638b 100644 --- a/test/mjsunit/wasm/wasm-object-api.js +++ b/test/mjsunit/wasm/wasm-object-api.js @@ -4,20 +4,10 @@ // Flags: --expose-wasm -var oldApi = true; - -if (oldApi) { - assertFalse(undefined === Wasm); - assertFalse(undefined == Wasm); - assertEquals("function", typeof Wasm.verifyModule); - assertEquals("function", typeof Wasm.verifyFunction); - assertEquals("function", typeof Wasm.instantiateModule); - assertFalse(undefined == Wasm.experimentalVersion); -} else { - assertTrue(undefined === Wasm); - assertTrue(undefined == Wasm); -} +// Old API should be gone. +assertEquals("undefined", typeof Wasm); +// New API should rule. assertEquals('object', typeof WebAssembly); assertEquals('function', typeof WebAssembly.Module); assertEquals('function', typeof WebAssembly.Instance);