[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}
This commit is contained in:
titzer 2016-10-26 09:58:40 -07:00 committed by Commit bot
parent 3f207617d7
commit 24d38be132
4 changed files with 53 additions and 193 deletions

View File

@ -92,108 +92,6 @@ RawBuffer GetRawBufferSource(
return {start, end};
}
void VerifyModule(const v8::FunctionCallbackInfo<v8::Value>& args) {
HandleScope scope(args.GetIsolate());
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(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<v8::Value>& args) {
HandleScope scope(args.GetIsolate());
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(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<i::JSObject> InstantiateModule(
const v8::FunctionCallbackInfo<v8::Value>& args, const byte* start,
const byte* end, ErrorThrower* thrower) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(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<i::JSObject> module_object =
i::wasm::CreateModuleObjectFromBytes(
isolate, start, end, thrower, i::wasm::kWasmOrigin,
i::Handle<i::Script>::null(), nullptr, nullptr);
i::MaybeHandle<i::JSObject> object;
if (!module_object.is_null()) {
// Success. Instantiate the module and return the object.
i::Handle<i::JSObject> ffi = i::Handle<i::JSObject>::null();
if (args.Length() > 1 && args[1]->IsObject()) {
Local<Object> obj = Local<Object>::Cast(args[1]);
ffi = i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj));
}
i::Handle<i::JSArrayBuffer> memory = i::Handle<i::JSArrayBuffer>::null();
if (args.Length() > 2 && args[2]->IsArrayBuffer()) {
Local<Object> obj = Local<Object>::Cast(args[2]);
i::Handle<i::Object> mem_obj = v8::Utils::OpenHandle(*obj);
memory = i::Handle<i::JSArrayBuffer>(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<v8::Value>& args) {
HandleScope scope(args.GetIsolate());
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(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<i::JSObject> CreateModuleObject(
v8::Isolate* isolate, const v8::Local<v8::Value> source,
ErrorThrower* thrower) {
@ -877,44 +775,13 @@ void WasmJs::Install(Isolate* isolate, Handle<JSGlobalObject> global) {
return;
}
Factory* factory = isolate->factory();
// Setup wasm function map.
Handle<Context> 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<String> name = v8_str(isolate, "Wasm");
Handle<JSFunction> cons = factory->NewFunction(name);
JSFunction::SetInstancePrototype(
cons, Handle<Object>(context->initial_object_prototype(), isolate));
cons->shared()->set_instance_class_name(*name);
Handle<JSObject> wasm_object = factory->NewJSObject(cons, TENURED);
PropertyAttributes attributes = static_cast<PropertyAttributes>(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<String> name = v8_str(isolate, "experimentalVersion");
PropertyAttributes attributes =
static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY);
Handle<Smi> value =
Handle<Smi>(Smi::FromInt(wasm::kWasmVersion), isolate);
JSObject::AddProperty(wasm_object, name, value, attributes);
}
}
InstallWasmConstructors(isolate, global, context);
}
void WasmJs::InstallWasmMapsIfNeeded(Isolate* isolate,

View File

@ -8,5 +8,4 @@ function f() {
"use asm";
}
assertFalse(Wasm == undefined);
assertTrue(%IsNotAsmWasmCode(f));

View File

@ -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();
})();

View File

@ -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);