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