Diff
Modified: trunk/JSTests/ChangeLog (207928 => 207929)
--- trunk/JSTests/ChangeLog 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/JSTests/ChangeLog 2016-10-27 02:18:51 UTC (rev 207929)
@@ -1,3 +1,27 @@
+2016-10-26 JF Bastien <[email protected]>
+
+ WebAssembly API: implement Instance
+
+ As described in: https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyinstance-objects
+
+ - Take ownership of Wasm::Plan's compilation result when successfully creating a JSWebAssemblyModule object.
+ - Construct a basic Instance with a Module.
+ - Handle second argument (importObject) of WebAssembly.Instance.
+ - Add reference text from the spec to WebAssembly.Module's code.
+ - Expose and test an empty 'exports' ModuleNamespaceObject on WebAssembly.Instance.
+
+ The implementation isn't complete yet: it relies on further work for which I've filed bugs.
+
+ WebAssembly API: implement Instance
+ https://bugs.webkit.org/show_bug.cgi?id=163998
+
+ Reviewed by Keith Miller.
+
+ * wasm/js-api/test_Instance.js: Added.
+ (EmptyModule): use the Builder, create the simplest Module possible, and create an Instance from it
+ * wasm/js-api/test_basic_api.js:
+ (const.c.in.constructorProperties.switch): basic tests of the API
+
2016-10-26 Mark Lam <[email protected]>
JSGenericTypedArrayView::set() should check for exceptions.
Added: trunk/JSTests/wasm/js-api/test_Instance.js (0 => 207929)
--- trunk/JSTests/wasm/js-api/test_Instance.js (rev 0)
+++ trunk/JSTests/wasm/js-api/test_Instance.js 2016-10-27 02:18:51 UTC (rev 207929)
@@ -0,0 +1,10 @@
+import * as assert from '../assert.js';
+import Builder from '../Builder.js';
+
+(function EmptyModule() {
+ const builder = new Builder();
+ const bin = builder.WebAssembly().get();
+ const module = new WebAssembly.Module(bin);
+ const instance = new WebAssembly.Instance(module);
+ assert.instanceof(instance, WebAssembly.Instance);
+})();
Modified: trunk/JSTests/wasm/js-api/test_basic_api.js (207928 => 207929)
--- trunk/JSTests/wasm/js-api/test_basic_api.js 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/JSTests/wasm/js-api/test_basic_api.js 2016-10-27 02:18:51 UTC (rev 207929)
@@ -2,7 +2,9 @@
import * as utilities from '../utilities.js';
const version = 0xC;
-const emptyModule = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, version, 0x00, 0x00, 0x00);
+const emptyModuleArray = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, version, 0x00, 0x00, 0x00);
+const invalidConstructorInputs = [undefined, null, "", 1, {}, []];
+const invalidInstanceImports = [null, "", 1];
const checkOwnPropertyDescriptor = (obj, prop, expect) => {
const descriptor = Object.getOwnPropertyDescriptor(obj, prop);
@@ -49,17 +51,27 @@
assert.throws(() => WebAssembly[c](), TypeError, `calling WebAssembly.${c} constructor without new is invalid`);
switch (c) {
case "Module":
- for (const invalid of [undefined, "", 1, {}, []])
+ for (const invalid of invalidConstructorInputs)
assert.throws(() => new WebAssembly[c](invalid), TypeError, `first argument to WebAssembly.Module must be an ArrayBufferView or an ArrayBuffer (evaluating 'new WebAssembly[c](invalid)')`);
for (const buffer of [new ArrayBuffer(), new DataView(new ArrayBuffer()), new Int8Array(), new Uint8Array(), new Uint8ClampedArray(), new Int16Array(), new Uint16Array(), new Int32Array(), new Uint32Array(), new Float32Array(), new Float64Array()])
// FIXME the following should be WebAssembly.CompileError. https://bugs.webkit.org/show_bug.cgi?id=163768
assert.throws(() => new WebAssembly[c](buffer), Error, `Module is 0 bytes, expected at least 8 bytes (evaluating 'new WebAssembly[c](buffer)')`);
- assert.instanceof(new WebAssembly[c](emptyModule), WebAssembly.Module);
+ assert.instanceof(new WebAssembly[c](emptyModuleArray), WebAssembly.Module);
// FIXME test neutered TypedArray and TypedArrayView. https://bugs.webkit.org/show_bug.cgi?id=163899
break;
case "Instance":
- // FIXME Implement and test these APIs further. For now they just throw. https://bugs.webkit.org/show_bug.cgi?id=159775
- assert.throws(() => new WebAssembly[c](), Error, `WebAssembly doesn't yet implement the ${c} constructor property`);
+ for (const invalid of invalidConstructorInputs)
+ assert.throws(() => new WebAssembly[c](invalid), TypeError, `first argument to WebAssembly.Instance must be a WebAssembly.Module (evaluating 'new WebAssembly[c](invalid)')`);
+ const instance = new WebAssembly[c](new WebAssembly.Module(emptyModuleArray));
+ assert.instanceof(instance, WebAssembly.Instance);
+ for (const invalid of invalidInstanceImports)
+ assert.throws(() => new WebAssembly[c](new WebAssembly.Module(emptyModuleArray), invalid), TypeError, `second argument to WebAssembly.Instance must be undefined or an Object (evaluating 'new WebAssembly[c](new WebAssembly.Module(emptyModuleArray), invalid)')`);
+ assert.notUndef(instance.exports);
+ checkOwnPropertyDescriptor(instance, "exports", { typeofvalue: "object", writable: true, configurable: true, enumerable: true });
+ assert.isUndef(instance.exports.__proto__);
+ assert.eq(Reflect.isExtensible(instance.exports), false);
+ assert.eq(Symbol.iterator in instance.exports, true);
+ assert.eq(Symbol.toStringTag in instance.exports, true);
break;
case "Memory":
// FIXME Implement and test these APIs further. For now they just throw. https://bugs.webkit.org/show_bug.cgi?id=159775
Modified: trunk/Source/_javascript_Core/ChangeLog (207928 => 207929)
--- trunk/Source/_javascript_Core/ChangeLog 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/ChangeLog 2016-10-27 02:18:51 UTC (rev 207929)
@@ -1,3 +1,39 @@
+2016-10-26 JF Bastien <[email protected]>
+
+ WebAssembly API: implement Instance
+
+ As described in: https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyinstance-objects
+
+ - Take ownership of Wasm::Plan's compilation result when successfully creating a JSWebAssemblyModule object.
+ - Construct a basic Instance with a Module.
+ - Handle second argument (importObject) of WebAssembly.Instance.
+ - Add reference text from the spec to WebAssembly.Module's code.
+ - Expose and test an empty 'exports' ModuleNamespaceObject on WebAssembly.Instance.
+
+ The implementation isn't complete yet: it relies on further work for which I've filed bugs.
+
+ WebAssembly API: implement Instance
+ https://bugs.webkit.org/show_bug.cgi?id=163998
+
+ Reviewed by Keith Miller.
+
+ * wasm/WasmPlan.h:
+ (JSC::Wasm::Plan::getFunctions): allow transfering state out
+ (JSC::Wasm::Plan::getMemory): allow transfering state out
+ * wasm/js/JSWebAssemblyInstance.cpp:
+ (JSC::JSWebAssemblyInstance::create):
+ (JSC::JSWebAssemblyInstance::finishCreation): set the ModuleNamespaceObject, and expose it as "exports"
+ (JSC::JSWebAssemblyInstance::visitChildren): visit the ModuleNamespaceObject child
+ * wasm/js/JSWebAssemblyInstance.h:
+ * wasm/js/JSWebAssemblyModule.cpp:
+ (JSC::JSWebAssemblyModule::create):
+ (JSC::JSWebAssemblyModule::JSWebAssemblyModule): move in the compiled functions and the memory
+ * wasm/js/JSWebAssemblyModule.h:
+ * wasm/js/WebAssemblyInstanceConstructor.cpp:
+ (JSC::constructJSWebAssemblyInstance): take the Module, extract the Plan's results, and create the (empty for now) ModuleNamespaceObject
+ * wasm/js/WebAssemblyModuleConstructor.cpp:
+ (JSC::constructJSWebAssemblyModule): add a few comments from the spec, and pass out the Plan's results
+
2016-10-26 Brian Burg <[email protected]>
Web Inspector: remove unused bool return value from FrontendChannel::sendMessageToFrontend
Modified: trunk/Source/_javascript_Core/wasm/WasmPlan.h (207928 => 207929)
--- trunk/Source/_javascript_Core/wasm/WasmPlan.h 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/wasm/WasmPlan.h 2016-10-27 02:18:51 UTC (rev 207929)
@@ -38,6 +38,8 @@
class Plan {
public:
+ typedef Vector<std::unique_ptr<FunctionCompilation>> CompiledFunctions;
+
JS_EXPORT_PRIVATE Plan(VM&, Vector<uint8_t>);
JS_EXPORT_PRIVATE Plan(VM&, const uint8_t*, size_t);
JS_EXPORT_PRIVATE ~Plan();
@@ -63,9 +65,20 @@
RELEASE_ASSERT(!failed());
return m_memory.get();
}
+
+ CompiledFunctions* getFunctions()
+ {
+ RELEASE_ASSERT(!failed());
+ return &m_result;
+ }
+ std::unique_ptr<Memory>* getMemory()
+ {
+ RELEASE_ASSERT(!failed());
+ return &m_memory;
+ }
private:
- Vector<std::unique_ptr<FunctionCompilation>> m_result;
+ CompiledFunctions m_result;
std::unique_ptr<Memory> m_memory;
bool m_failed { true };
String m_errorMessage;
Modified: trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyInstance.cpp (207928 => 207929)
--- trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyInstance.cpp 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyInstance.cpp 2016-10-27 02:18:51 UTC (rev 207929)
@@ -29,13 +29,14 @@
#if ENABLE(WEBASSEMBLY)
#include "JSCInlines.h"
+#include "JSModuleNamespaceObject.h"
namespace JSC {
-JSWebAssemblyInstance* JSWebAssemblyInstance::create(VM& vm, Structure* structure)
+JSWebAssemblyInstance* JSWebAssemblyInstance::create(VM& vm, Structure* structure, JSModuleNamespaceObject* moduleNamespaceObject)
{
auto* instance = new (NotNull, allocateCell<JSWebAssemblyInstance>(vm.heap)) JSWebAssemblyInstance(vm, structure);
- instance->finishCreation(vm);
+ instance->finishCreation(vm, moduleNamespaceObject);
return instance;
}
@@ -49,9 +50,11 @@
{
}
-void JSWebAssemblyInstance::finishCreation(VM& vm)
+void JSWebAssemblyInstance::finishCreation(VM& vm, JSModuleNamespaceObject* moduleNamespaceObject)
{
Base::finishCreation(vm);
+ m_moduleNamespaceObject.set(vm, this, moduleNamespaceObject);
+ putDirectWithoutTransition(vm, Identifier::fromString(&vm, "exports"), m_moduleNamespaceObject.get(), None);
ASSERT(inherits(info()));
}
@@ -66,6 +69,7 @@
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
+ visitor.append(&thisObject->m_moduleNamespaceObject);
}
const ClassInfo JSWebAssemblyInstance::s_info = { "WebAssembly.Instance", &Base::s_info, 0, CREATE_METHOD_TABLE(JSWebAssemblyInstance) };
Modified: trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyInstance.h (207928 => 207929)
--- trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyInstance.h 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyInstance.h 2016-10-27 02:18:51 UTC (rev 207929)
@@ -31,12 +31,14 @@
#include "JSObject.h"
namespace JSC {
+
+class JSModuleNamespaceObject;
class JSWebAssemblyInstance : public JSDestructibleObject {
public:
typedef JSDestructibleObject Base;
- static JSWebAssemblyInstance* create(VM&, Structure*);
+ static JSWebAssemblyInstance* create(VM&, Structure*, JSModuleNamespaceObject*);
static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
DECLARE_INFO;
@@ -43,9 +45,12 @@
protected:
JSWebAssemblyInstance(VM&, Structure*);
- void finishCreation(VM&);
+ void finishCreation(VM&, JSModuleNamespaceObject*);
static void destroy(JSCell*);
static void visitChildren(JSCell*, SlotVisitor&);
+
+private:
+ WriteBarrier<JSModuleNamespaceObject> m_moduleNamespaceObject;
};
} // namespace JSC
Modified: trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyModule.cpp (207928 => 207929)
--- trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyModule.cpp 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyModule.cpp 2016-10-27 02:18:51 UTC (rev 207929)
@@ -29,12 +29,15 @@
#if ENABLE(WEBASSEMBLY)
#include "JSCInlines.h"
+#include "WasmFormat.h"
+#include "WasmMemory.h"
+#include <wtf/StdLibExtras.h>
namespace JSC {
-JSWebAssemblyModule* JSWebAssemblyModule::create(VM& vm, Structure* structure)
+JSWebAssemblyModule* JSWebAssemblyModule::create(VM& vm, Structure* structure, Vector<std::unique_ptr<Wasm::FunctionCompilation>>* compiledFunctions, std::unique_ptr<Wasm::Memory>* memory)
{
- auto* instance = new (NotNull, allocateCell<JSWebAssemblyModule>(vm.heap)) JSWebAssemblyModule(vm, structure);
+ auto* instance = new (NotNull, allocateCell<JSWebAssemblyModule>(vm.heap)) JSWebAssemblyModule(vm, structure, compiledFunctions, memory);
instance->finishCreation(vm);
return instance;
}
@@ -44,8 +47,10 @@
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
-JSWebAssemblyModule::JSWebAssemblyModule(VM& vm, Structure* structure)
+JSWebAssemblyModule::JSWebAssemblyModule(VM& vm, Structure* structure, Vector<std::unique_ptr<Wasm::FunctionCompilation>>* compiledFunctions, std::unique_ptr<Wasm::Memory>* memory)
: Base(vm, structure)
+ , m_compiledFunctions(WTFMove(*compiledFunctions))
+ , m_memory(WTFMove(*memory))
{
}
Modified: trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyModule.h (207928 => 207929)
--- trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyModule.h 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/wasm/js/JSWebAssemblyModule.h 2016-10-27 02:18:51 UTC (rev 207929)
@@ -32,20 +32,29 @@
namespace JSC {
+namespace Wasm {
+struct FunctionCompilation;
+class Memory;
+}
+
class JSWebAssemblyModule : public JSDestructibleObject {
public:
typedef JSDestructibleObject Base;
- static JSWebAssemblyModule* create(VM&, Structure*);
+ static JSWebAssemblyModule* create(VM&, Structure*, Vector<std::unique_ptr<Wasm::FunctionCompilation>>*, std::unique_ptr<Wasm::Memory>*);
static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
DECLARE_INFO;
protected:
- JSWebAssemblyModule(VM&, Structure*);
+ JSWebAssemblyModule(VM&, Structure*, Vector<std::unique_ptr<Wasm::FunctionCompilation>>*, std::unique_ptr<Wasm::Memory>*);
void finishCreation(VM&);
static void destroy(JSCell*);
static void visitChildren(JSCell*, SlotVisitor&);
+
+private:
+ Vector<std::unique_ptr<Wasm::FunctionCompilation>> m_compiledFunctions;
+ std::unique_ptr<Wasm::Memory> m_memory;
};
} // namespace JSC
Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyInstanceConstructor.cpp (207928 => 207929)
--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyInstanceConstructor.cpp 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyInstanceConstructor.cpp 2016-10-27 02:18:51 UTC (rev 207929)
@@ -30,6 +30,10 @@
#include "FunctionPrototype.h"
#include "JSCInlines.h"
+#include "JSModuleNamespaceObject.h"
+#include "JSModuleRecord.h"
+#include "JSWebAssemblyInstance.h"
+#include "JSWebAssemblyModule.h"
#include "WebAssemblyInstancePrototype.h"
#include "WebAssemblyInstanceConstructor.lut.h"
@@ -45,9 +49,42 @@
static EncodedJSValue JSC_HOST_CALL constructJSWebAssemblyInstance(ExecState* state)
{
- VM& vm = state->vm();
+ auto& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
- return JSValue::encode(throwException(state, scope, createError(state, ASCIILiteral("WebAssembly doesn't yet implement the Instance constructor property"))));
+ auto* globalObject = state->lexicalGlobalObject();
+
+ // If moduleObject is not a WebAssembly.Module instance, a TypeError is thrown.
+ JSWebAssemblyModule* module = jsDynamicCast<JSWebAssemblyModule*>(state->argument(0));
+ if (!module)
+ return JSValue::encode(throwException(state, scope, createTypeError(state, ASCIILiteral("first argument to WebAssembly.Instance must be a WebAssembly.Module"), defaultSourceAppender, runtimeTypeForValue(state->argument(0)))));
+
+ // If the importObject parameter is not undefined and Type(importObject) is not Object, a TypeError is thrown.
+ JSValue importArgument = state->argument(1);
+ JSObject* importObject = importArgument.getObject();
+ if (!importArgument.isUndefined() && !importObject)
+ return JSValue::encode(throwException(state, scope, createTypeError(state, ASCIILiteral("second argument to WebAssembly.Instance must be undefined or an Object"), defaultSourceAppender, runtimeTypeForValue(importArgument))));
+
+ // FIXME use the importObject. https://bugs.webkit.org/show_bug.cgi?id=164039
+ // If the list of module.imports is not empty and Type(importObject) is not Object, a TypeError is thrown.
+
+ // FIXME String things from https://bugs.webkit.org/show_bug.cgi?id=164023
+ // Let exports be a list of (string, JS value) pairs that is mapped from each external value e in instance.exports as follows:
+ IdentifierSet instanceExports;
+ for (const auto& name : instanceExports) {
+ // FIXME validate according to Module.Instance spec.
+ (void)name;
+ }
+ Identifier moduleKey;
+ SourceCode sourceCode;
+ VariableEnvironment declaredVariables;
+ VariableEnvironment lexicalVariables;
+ auto* moduleRecord = JSModuleRecord::create(state, vm, globalObject->moduleRecordStructure(), moduleKey, sourceCode, declaredVariables, lexicalVariables);
+ auto* moduleNamespaceObject = JSModuleNamespaceObject::create(state, globalObject, globalObject->moduleNamespaceObjectStructure(), moduleRecord, instanceExports);
+
+ auto* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), globalObject->WebAssemblyInstanceStructure());
+ RETURN_IF_EXCEPTION(scope, encodedJSValue());
+
+ return JSValue::encode(JSWebAssemblyInstance::create(vm, structure, moduleNamespaceObject));
}
static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyInstance(ExecState* state)
Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleConstructor.cpp (207928 => 207929)
--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleConstructor.cpp 2016-10-27 00:17:52 UTC (rev 207928)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyModuleConstructor.cpp 2016-10-27 02:18:51 UTC (rev 207929)
@@ -54,6 +54,8 @@
auto& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue val = state->argument(0);
+
+ // If the given bytes argument is not a BufferSource, a TypeError exception is thrown.
JSArrayBuffer* arrayBuffer = val.getObject() ? jsDynamicCast<JSArrayBuffer*>(val.getObject()) : nullptr;
JSArrayBufferView* arrayBufferView = val.getObject() ? jsDynamicCast<JSArrayBufferView*>(val.getObject()) : nullptr;
if (!(arrayBuffer || arrayBufferView))
@@ -67,14 +69,17 @@
const auto* base = arrayBufferView ? static_cast<uint8_t*>(arrayBufferView->vector()) : static_cast<uint8_t*>(arrayBuffer->impl()->data());
Wasm::Plan plan(vm, base + byteOffset, byteSize);
+ // On failure, a new WebAssembly.CompileError is thrown.
if (plan.failed())
return JSValue::encode(throwException(state, scope, createWebAssemblyCompileError(state, plan.errorMessage())));
- // FIXME take content from Plan.
+ // The spec string values inside Ast.module are decoded as UTF8 as described in Web.md. FIXME https://bugs.webkit.org/show_bug.cgi?id=164023
+ // On success, a new WebAssembly.Module object is returned with [[Module]] set to the validated Ast.module.
auto* structure = InternalFunction::createSubclassStructure(state, state->newTarget(), asInternalFunction(state->callee())->globalObject()->WebAssemblyModuleStructure());
RETURN_IF_EXCEPTION(scope, encodedJSValue());
- return JSValue::encode(JSWebAssemblyModule::create(vm, structure));
+
+ return JSValue::encode(JSWebAssemblyModule::create(vm, structure, plan.getFunctions(), plan.getMemory()));
}
static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyModule(ExecState* state)