Commit 01c464a5 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Set JS API names and function lengths appropriately.

R=clemensh@chromium.org
BUG=chromium:575167

Review-Url: https://codereview.chromium.org/2590243003
Cr-Commit-Position: refs/heads/master@{#41885}
parent 0f39ef5f
......@@ -573,11 +573,14 @@ static i::Handle<i::FunctionTemplateInfo> NewTemplate(i::Isolate* i_isolate,
namespace internal {
Handle<JSFunction> InstallFunc(Isolate* isolate, Handle<JSObject> object,
const char* str, FunctionCallback func) {
const char* str, FunctionCallback func,
int length = 0) {
Handle<String> name = v8_str(isolate, str);
Handle<FunctionTemplateInfo> temp = NewTemplate(isolate, func);
Handle<JSFunction> function =
ApiNatives::InstantiateFunction(temp).ToHandleChecked();
JSFunction::SetName(function, name, isolate->factory()->empty_string());
function->shared()->set_length(length);
PropertyAttributes attributes = static_cast<PropertyAttributes>(DONT_ENUM);
JSObject::AddProperty(object, name, function, attributes);
return function;
......@@ -636,14 +639,14 @@ void WasmJs::InstallWasmConstructors(Isolate* isolate,
JSObject::AddProperty(global, name, webassembly, attributes);
// Setup compile
InstallFunc(isolate, webassembly, "compile", WebAssemblyCompile);
InstallFunc(isolate, webassembly, "compile", WebAssemblyCompile, 1);
// Setup compile
InstallFunc(isolate, webassembly, "validate", WebAssemblyValidate);
InstallFunc(isolate, webassembly, "validate", WebAssemblyValidate, 1);
// Setup Module
Handle<JSFunction> module_constructor =
InstallFunc(isolate, webassembly, "Module", WebAssemblyModule);
InstallFunc(isolate, webassembly, "Module", WebAssemblyModule, 1);
context->set_wasm_module_constructor(*module_constructor);
Handle<JSObject> module_proto =
factory->NewJSObject(module_constructor, TENURED);
......@@ -656,12 +659,12 @@ void WasmJs::InstallWasmConstructors(Isolate* isolate,
// Setup Instance
Handle<JSFunction> instance_constructor =
InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance);
InstallFunc(isolate, webassembly, "Instance", WebAssemblyInstance, 1);
context->set_wasm_instance_constructor(*instance_constructor);
// Setup Table
Handle<JSFunction> table_constructor =
InstallFunc(isolate, webassembly, "Table", WebAssemblyTable);
InstallFunc(isolate, webassembly, "Table", WebAssemblyTable, 1);
context->set_wasm_table_constructor(*table_constructor);
Handle<JSObject> table_proto =
factory->NewJSObject(table_constructor, TENURED);
......@@ -672,13 +675,13 @@ void WasmJs::InstallWasmConstructors(Isolate* isolate,
JSObject::AddProperty(table_proto, isolate->factory()->constructor_string(),
table_constructor, DONT_ENUM);
InstallGetter(isolate, table_proto, "length", WebAssemblyTableGetLength);
InstallFunc(isolate, table_proto, "grow", WebAssemblyTableGrow);
InstallFunc(isolate, table_proto, "get", WebAssemblyTableGet);
InstallFunc(isolate, table_proto, "set", WebAssemblyTableSet);
InstallFunc(isolate, table_proto, "grow", WebAssemblyTableGrow, 1);
InstallFunc(isolate, table_proto, "get", WebAssemblyTableGet, 1);
InstallFunc(isolate, table_proto, "set", WebAssemblyTableSet, 2);
// Setup Memory
Handle<JSFunction> memory_constructor =
InstallFunc(isolate, webassembly, "Memory", WebAssemblyMemory);
InstallFunc(isolate, webassembly, "Memory", WebAssemblyMemory, 1);
context->set_wasm_memory_constructor(*memory_constructor);
Handle<JSObject> memory_proto =
factory->NewJSObject(memory_constructor, TENURED);
......@@ -688,7 +691,7 @@ void WasmJs::InstallWasmConstructors(Isolate* isolate,
JSFunction::SetInitialMap(memory_constructor, map, memory_proto);
JSObject::AddProperty(memory_proto, isolate->factory()->constructor_string(),
memory_constructor, DONT_ENUM);
InstallFunc(isolate, memory_proto, "grow", WebAssemblyMemoryGrow);
InstallFunc(isolate, memory_proto, "grow", WebAssemblyMemoryGrow, 1);
InstallGetter(isolate, memory_proto, "buffer", WebAssemblyMemoryGetBuffer);
// Setup errors
......
......@@ -54,42 +54,57 @@ assertEq(wasmDesc.configurable, true);
assertEq(WebAssembly, wasmDesc.value);
//TODO assertEq(String(WebAssembly), "[object WebAssembly]");
// 'WebAssembly.(Compile|Runtime)Error' data property
// 'WebAssembly.CompileError'
let compileErrorDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'CompileError');
let runtimeErrorDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'RuntimeError');
assertEq(typeof compileErrorDesc.value, "function");
assertEq(typeof runtimeErrorDesc.value, "function");
if (PROP_FLAGS) assertEq(compileErrorDesc.writable, true);
if (PROP_FLAGS) assertEq(runtimeErrorDesc.writable, true);
if (PROP_FLAGS) assertEq(compileErrorDesc.enumerable, false);
if (PROP_FLAGS) assertEq(runtimeErrorDesc.enumerable, false);
if (PROP_FLAGS) assertEq(compileErrorDesc.configurable, true);
if (PROP_FLAGS) assertEq(runtimeErrorDesc.configurable, true);
// 'WebAssembly.(Compile|Runtime)Error' constructor function
assertEq(compileErrorDesc.writable, true);
assertEq(compileErrorDesc.enumerable, false);
assertEq(compileErrorDesc.configurable, true);
let CompileError = WebAssembly.CompileError;
let RuntimeError = WebAssembly.RuntimeError;
assertEq(CompileError, compileErrorDesc.value);
assertEq(RuntimeError, runtimeErrorDesc.value);
assertEq(CompileError.length, 1);
assertEq(RuntimeError.length, 1);
assertEq(CompileError.name, "CompileError");
assertEq(RuntimeError.name, "RuntimeError");
// 'WebAssembly.(Compile|Runtime)Error' instance objects
let compileError = new CompileError;
let runtimeError = new RuntimeError;
assertEq(compileError instanceof CompileError, true);
assertEq(runtimeError instanceof RuntimeError, true);
assertEq(compileError instanceof Error, true);
assertEq(runtimeError instanceof Error, true);
assertEq(compileError instanceof TypeError, false);
assertEq(runtimeError instanceof TypeError, false);
assertEq(compileError.message, "");
assertEq(runtimeError.message, "");
assertEq(new CompileError("hi").message, "hi");
// 'WebAssembly.RuntimeError'
let runtimeErrorDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'RuntimeError');
assertEq(typeof runtimeErrorDesc.value, "function");
assertEq(runtimeErrorDesc.writable, true);
assertEq(runtimeErrorDesc.enumerable, false);
assertEq(runtimeErrorDesc.configurable, true);
let RuntimeError = WebAssembly.RuntimeError;
assertEq(RuntimeError, runtimeErrorDesc.value);
assertEq(RuntimeError.length, 1);
assertEq(RuntimeError.name, "RuntimeError");
let runtimeError = new RuntimeError;
assertEq(runtimeError instanceof RuntimeError, true);
assertEq(runtimeError instanceof Error, true);
assertEq(runtimeError instanceof TypeError, false);
assertEq(runtimeError.message, "");
assertEq(new RuntimeError("hi").message, "hi");
// 'WebAssembly.LinkError'
let linkErrorDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'LinkError');
assertEq(typeof linkErrorDesc.value, "function");
assertEq(linkErrorDesc.writable, true);
assertEq(linkErrorDesc.enumerable, false);
assertEq(linkErrorDesc.configurable, true);
let LinkError = WebAssembly.LinkError;
assertEq(LinkError, linkErrorDesc.value);
assertEq(LinkError.length, 1);
assertEq(LinkError.name, "LinkError");
let linkError = new LinkError;
assertEq(linkError instanceof LinkError, true);
assertEq(linkError instanceof Error, true);
assertEq(linkError instanceof TypeError, false);
assertEq(linkError.message, "");
assertEq(new LinkError("hi").message, "hi");
// 'WebAssembly.Module' data property
let moduleDesc = Object.getOwnPropertyDescriptor(WebAssembly, 'Module');
assertEq(typeof moduleDesc.value, "function");
......@@ -100,8 +115,8 @@ assertEq(moduleDesc.configurable, true);
// 'WebAssembly.Module' constructor function
let Module = WebAssembly.Module;
assertEq(Module, moduleDesc.value);
//TODO assertEq(Module.length, 1);
//TODO assertEq(Module.name, "Module");
assertEq(Module.length, 1);
assertEq(Module.name, "Module");
assertErrorMessage(() => Module(), TypeError, /constructor without new is forbidden/);
assertErrorMessage(() => new Module(), TypeError, /requires more than 0 arguments/);
assertErrorMessage(() => new Module(undefined), TypeError, "first argument must be an ArrayBuffer or typed array object");
......@@ -207,8 +222,8 @@ assertEq(instanceDesc.configurable, true);
// 'WebAssembly.Instance' constructor function
let Instance = WebAssembly.Instance;
assertEq(Instance, instanceDesc.value);
//TODO assertEq(Instance.length, 1);
//TODO assertEq(Instance.name, "Instance");
assertEq(Instance.length, 1);
assertEq(Instance.name, "Instance");
assertErrorMessage(() => Instance(), TypeError, /constructor without new is forbidden/);
assertErrorMessage(() => new Instance(1), TypeError, "first argument must be a WebAssembly.Module");
assertErrorMessage(() => new Instance({}), TypeError, "first argument must be a WebAssembly.Module");
......@@ -260,8 +275,8 @@ assertEq(memoryDesc.configurable, true);
// 'WebAssembly.Memory' constructor function
let Memory = WebAssembly.Memory;
assertEq(Memory, memoryDesc.value);
//TODO assertEq(Memory.length, 1);
//TODO assertEq(Memory.name, "Memory");
assertEq(Memory.length, 1);
assertEq(Memory.name, "Memory");
assertErrorMessage(() => Memory(), TypeError, /constructor without new is forbidden/);
assertErrorMessage(() => new Memory(1), TypeError, "first argument must be a memory descriptor");
assertErrorMessage(() => new Memory({initial:{valueOf() { throw new Error("here")}}}), Error, "here");
......@@ -348,8 +363,8 @@ assertEq(tableDesc.configurable, true);
// 'WebAssembly.Table' constructor function
let Table = WebAssembly.Table;
assertEq(Table, tableDesc.value);
//TODO assertEq(Table.length, 1);
//TODO assertEq(Table.name, "Table");
assertEq(Table.length, 1);
assertEq(Table.name, "Table");
assertErrorMessage(() => Table(), TypeError, /constructor without new is forbidden/);
assertErrorMessage(() => new Table(1), TypeError, "first argument must be a table descriptor");
assertErrorMessage(() => new Table({initial:1, element:1}), TypeError, /must be "anyfunc"/);
......@@ -407,7 +422,7 @@ assertEq(getDesc.configurable, true);
// 'WebAssembly.Table.prototype.get' method
let get = getDesc.value;
//TODO:length assertEq(get.length, 1);
assertEq(get.length, 1);
assertErrorMessage(() => get.call(), TypeError, /called on incompatible undefined/);
assertErrorMessage(() => get.call({}), TypeError, /called on incompatible Object/);
assertEq(get.call(tbl1, 0), null);
......@@ -427,7 +442,7 @@ assertEq(setDesc.configurable, true);
// 'WebAssembly.Table.prototype.set' method
let set = setDesc.value;
//TODO assertEq(set.length, 2);
assertEq(set.length, 2);
assertErrorMessage(() => set.call(), TypeError, /called on incompatible undefined/);
assertErrorMessage(() => set.call({}), TypeError, /called on incompatible Object/);
assertErrorMessage(() => set.call(tbl1, 0), TypeError, /requires more than 1 argument/);
......@@ -451,7 +466,7 @@ assertEq(tblGrowDesc.configurable, true);
// 'WebAssembly.Table.prototype.grow' method
if (false) { // TODO: Table.grow
let tblGrow = tblGrowDesc.value;
//TODO assertEq(tblGrow.length, 1);
assertEq(tblGrow.length, 1);
assertErrorMessage(() => tblGrow.call(), TypeError, /called on incompatible undefined/);
assertErrorMessage(() => tblGrow.call({}), TypeError, /called on incompatible Object/);
assertErrorMessage(() => tblGrow.call(tbl1, -1), RangeError, /bad Table grow delta/);
......@@ -475,8 +490,8 @@ assertEq(compileDesc.configurable, true);
// 'WebAssembly.compile' function
let compile = WebAssembly.compile;
assertEq(compile, compileDesc.value);
//TODO assertEq(compile.length, 1);
//TODO assertEq(compile.name, "compile");
assertEq(compile.length, 1);
assertEq(compile.name, "compile");
function assertCompileError(args, err, msg) {
var error = null;
try {
......
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