Title: [214601] trunk
Revision
214601
Author
[email protected]
Date
2017-03-30 00:16:27 -0700 (Thu, 30 Mar 2017)

Log Message

WebAssembly: fix misc JS API implementation inconsistencies
https://bugs.webkit.org/show_bug.cgi?id=170187

Reviewed by Keith Miller.

JSTests:

Verify that WebAssembly function are on the object's __proto__.

* wasm/js-api/Module-compile.js:
* wasm/js-api/validate.js:
* wasm/js-api/web-assembly-instantiate.js:

Source/_javascript_Core:

Auto-generate lookup tables.
Methods should be on prototype.
Exception returns should be idiomatic.

* wasm/JSWebAssembly.cpp: validate / compile / instantiate should
be on the prototype
(JSC::JSWebAssembly::create):
(JSC::JSWebAssembly::finishCreation):
(JSC::reject): Deleted.
(JSC::webAssemblyCompileFunc): Deleted.
(JSC::resolve): Deleted.
(JSC::instantiate): Deleted.
(JSC::compileAndInstantiate): Deleted.
(JSC::webAssemblyInstantiateFunc): Deleted.
(JSC::webAssemblyValidateFunc): Deleted.
* wasm/JSWebAssembly.h:
* wasm/js/WebAssemblyMemoryPrototype.cpp: move from JSWebAssembly.cpp
(JSC::webAssemblyMemoryProtoFuncBuffer):
(JSC::WebAssemblyMemoryPrototype::create):
(JSC::WebAssemblyMemoryPrototype::finishCreation):
* wasm/js/WebAssemblyMemoryPrototype.h:
* wasm/js/WebAssemblyPrototype.cpp:
(JSC::reject):
(JSC::webAssemblyCompileFunc):
(JSC::resolve):
(JSC::instantiate):
(JSC::compileAndInstantiate):
(JSC::webAssemblyInstantiateFunc):
(JSC::webAssemblyValidateFunc):
(JSC::webAssemblyFunctionValidate): Deleted.
(JSC::webAssemblyFunctionCompile): Deleted.
* wasm/js/WebAssemblyTablePrototype.cpp:
(JSC::webAssemblyTableProtoFuncGrow):
(JSC::webAssemblyTableProtoFuncGet):
(JSC::webAssemblyTableProtoFuncSet):
(JSC::WebAssemblyTablePrototype::create):
(JSC::WebAssemblyTablePrototype::finishCreation):
* wasm/js/WebAssemblyTablePrototype.h:

Modified Paths

Diff

Modified: trunk/JSTests/ChangeLog (214600 => 214601)


--- trunk/JSTests/ChangeLog	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/JSTests/ChangeLog	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,3 +1,16 @@
+2017-03-30  JF Bastien  <[email protected]>
+
+        WebAssembly: fix misc JS API implementation inconsistencies
+        https://bugs.webkit.org/show_bug.cgi?id=170187
+
+        Reviewed by Keith Miller.
+
+        Verify that WebAssembly function are on the object's __proto__.
+
+        * wasm/js-api/Module-compile.js:
+        * wasm/js-api/validate.js:
+        * wasm/js-api/web-assembly-instantiate.js:
+
 2017-03-29  JF Bastien  <[email protected]>
 
         WebAssembly: add shell-only Memory mode helper

Modified: trunk/JSTests/wasm/js-api/Module-compile.js (214600 => 214601)


--- trunk/JSTests/wasm/js-api/Module-compile.js	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/JSTests/wasm/js-api/Module-compile.js	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,6 +1,10 @@
 import * as assert from '../assert.js';
 import Builder from '../Builder.js';
 
+assert.isFunction(WebAssembly.compile);
+assert.isFunction(WebAssembly.__proto__.compile);
+assert.eq(WebAssembly.compile.length, 1);
+
 async function testPromiseAPI() {
     {
         // Can't declare more than one memory.

Modified: trunk/JSTests/wasm/js-api/validate.js (214600 => 214601)


--- trunk/JSTests/wasm/js-api/validate.js	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/JSTests/wasm/js-api/validate.js	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,6 +1,10 @@
 import * as assert from '../assert.js';
 import Builder from '../Builder.js';
 
+assert.isFunction(WebAssembly.validate);
+assert.isFunction(WebAssembly.__proto__.validate);
+assert.eq(WebAssembly.validate.length, 1);
+
 {
     const builder = (new Builder())
         .Type().End()

Modified: trunk/JSTests/wasm/js-api/web-assembly-instantiate.js (214600 => 214601)


--- trunk/JSTests/wasm/js-api/web-assembly-instantiate.js	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/JSTests/wasm/js-api/web-assembly-instantiate.js	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,7 +1,8 @@
 import * as assert from '../assert.js';
 import Builder from '../Builder.js';
 
-assert.truthy(WebAssembly.instantiate instanceof Function);
+assert.isFunction(WebAssembly.instantiate);
+assert.isFunction(WebAssembly.__proto__.instantiate);
 assert.eq(WebAssembly.instantiate.length, 1);
 
 {

Modified: trunk/Source/_javascript_Core/ChangeLog (214600 => 214601)


--- trunk/Source/_javascript_Core/ChangeLog	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/ChangeLog	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,3 +1,49 @@
+2017-03-30  JF Bastien  <[email protected]>
+
+        WebAssembly: fix misc JS API implementation inconsistencies
+        https://bugs.webkit.org/show_bug.cgi?id=170187
+
+        Reviewed by Keith Miller.
+
+        Auto-generate lookup tables.
+        Methods should be on prototype.
+        Exception returns should be idiomatic.
+
+        * wasm/JSWebAssembly.cpp: validate / compile / instantiate should
+        be on the prototype
+        (JSC::JSWebAssembly::create):
+        (JSC::JSWebAssembly::finishCreation):
+        (JSC::reject): Deleted.
+        (JSC::webAssemblyCompileFunc): Deleted.
+        (JSC::resolve): Deleted.
+        (JSC::instantiate): Deleted.
+        (JSC::compileAndInstantiate): Deleted.
+        (JSC::webAssemblyInstantiateFunc): Deleted.
+        (JSC::webAssemblyValidateFunc): Deleted.
+        * wasm/JSWebAssembly.h:
+        * wasm/js/WebAssemblyMemoryPrototype.cpp: move from JSWebAssembly.cpp
+        (JSC::webAssemblyMemoryProtoFuncBuffer):
+        (JSC::WebAssemblyMemoryPrototype::create):
+        (JSC::WebAssemblyMemoryPrototype::finishCreation):
+        * wasm/js/WebAssemblyMemoryPrototype.h:
+        * wasm/js/WebAssemblyPrototype.cpp:
+        (JSC::reject):
+        (JSC::webAssemblyCompileFunc):
+        (JSC::resolve):
+        (JSC::instantiate):
+        (JSC::compileAndInstantiate):
+        (JSC::webAssemblyInstantiateFunc):
+        (JSC::webAssemblyValidateFunc):
+        (JSC::webAssemblyFunctionValidate): Deleted.
+        (JSC::webAssemblyFunctionCompile): Deleted.
+        * wasm/js/WebAssemblyTablePrototype.cpp:
+        (JSC::webAssemblyTableProtoFuncGrow):
+        (JSC::webAssemblyTableProtoFuncGet):
+        (JSC::webAssemblyTableProtoFuncSet):
+        (JSC::WebAssemblyTablePrototype::create):
+        (JSC::WebAssemblyTablePrototype::finishCreation):
+        * wasm/js/WebAssemblyTablePrototype.h:
+
 2017-03-29  Keith Miller  <[email protected]>
 
         Unreviewed, fix the build, again. Hopefully for the last time, again!

Modified: trunk/Source/_javascript_Core/wasm/JSWebAssembly.cpp (214600 => 214601)


--- trunk/Source/_javascript_Core/wasm/JSWebAssembly.cpp	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/wasm/JSWebAssembly.cpp	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -28,223 +28,18 @@
 
 #if ENABLE(WEBASSEMBLY)
 
-#include "Exception.h"
-#include "FunctionPrototype.h"
 #include "JSCInlines.h"
-#include "JSPromiseDeferred.h"
-#include "JSWebAssemblyHelpers.h"
-#include "JSWebAssemblyInstance.h"
-#include "JSWebAssemblyModule.h"
-#include "ObjectConstructor.h"
-#include "PromiseDeferredTimer.h"
-#include "StrongInlines.h"
-#include "WasmPlan.h"
-#include "WasmWorklist.h"
-#include "WebAssemblyModuleConstructor.h"
 
-using JSC::Wasm::Plan;
-
 namespace JSC {
 
 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSWebAssembly);
 
-EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState*);
-
-static EncodedJSValue reject(ExecState* exec, CatchScope& catchScope, JSPromiseDeferred* promise)
-{
-    Exception* exception = catchScope.exception();
-    ASSERT(exception);
-    catchScope.clearException();
-    promise->reject(exec, exception->value());
-    return JSValue::encode(promise->promise());
-}
-
-EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState* exec)
-{
-    VM& vm = exec->vm();
-    auto scope = DECLARE_CATCH_SCOPE(vm);
-    auto* globalObject = exec->lexicalGlobalObject();
-
-    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, globalObject);
-    RETURN_IF_EXCEPTION(scope, { });
-
-    RefPtr<ArrayBuffer> source = createSourceBufferFromValue(vm, exec, exec->argument(0));
-    RETURN_IF_EXCEPTION(scope, { });
-
-    Vector<Strong<JSCell>> dependencies;
-    dependencies.append(Strong<JSCell>(vm, globalObject));
-    vm.promiseDeferredTimer->addPendingPromise(promise, WTFMove(dependencies));
-
-    Ref<Plan> plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, globalObject] (Plan& p) mutable {
-        RefPtr<Plan> plan = makeRef(p);
-        plan->vm().promiseDeferredTimer->scheduleWorkSoon(promise, [source, promise, globalObject, plan = WTFMove(plan)] () mutable {
-            VM& vm = plan->vm();
-            auto scope = DECLARE_CATCH_SCOPE(vm);
-            ExecState* exec = globalObject->globalExec();
-            JSValue module = JSWebAssemblyModule::createStub(vm, exec, globalObject->WebAssemblyModuleStructure(), WTFMove(source), WTFMove(plan));
-            if (scope.exception()) {
-                reject(exec, scope, promise);
-                return;
-            }
-
-            promise->resolve(exec, module);
-        });
-    }));
-
-    Wasm::ensureWorklist().enqueue(WTFMove(plan));
-    return JSValue::encode(promise->promise());
-}
-
-enum class Resolve { WithInstance, WithModuleAndInstance };
-static void resolve(VM& vm, ExecState* exec, JSPromiseDeferred* promise, JSWebAssemblyInstance* instance, JSWebAssemblyModule* module, Resolve entries)
-{
-    auto scope = DECLARE_CATCH_SCOPE(vm);
-    instance->finalizeCreation(vm, exec);
-    if (scope.exception()) {
-        reject(exec, scope, promise);
-        return;
-    }
-
-    if (entries == Resolve::WithInstance)
-        promise->resolve(exec, instance);
-    else {
-        JSObject* result = constructEmptyObject(exec);
-        result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("module")), module);
-        result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("instance")), instance);
-        promise->resolve(exec, result);
-    }
-}
-
-static void instantiate(VM& vm, ExecState* exec, JSPromiseDeferred* promise, JSWebAssemblyModule* module, JSObject* importObject, Resolve entries)
-{
-    auto scope = DECLARE_CATCH_SCOPE(vm);
-    // In order to avoid potentially recompiling a module. We first gather all the import/memory information prior to compiling code.
-    JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, exec, module, importObject, exec->lexicalGlobalObject()->WebAssemblyInstanceStructure());
-    if (scope.exception()) {
-        reject(exec, scope, promise);
-        return;
-    }
-
-    // There are three possible cases:
-    // 1) The instance already has an initialized CodeBlock, so we have no more work to do.
-    // 2) The instance has no CodeBlock, so we need to make one and compile the code for it.
-    // 3) The instance already has an uninitialized CodeBlock, so someone else is compiling code and we just need to wait for them.
-
-    if (instance->initialized()) {
-        resolve(vm, exec, promise, instance, module, entries);
-        return;
-    }
-
-    Vector<Strong<JSCell>> dependencies;
-    // The instance keeps the module alive.
-    dependencies.append(Strong<JSCell>(vm, instance));
-    vm.promiseDeferredTimer->addPendingPromise(promise, WTFMove(dependencies));
-
-    if (instance->codeBlock()) {
-        vm.promiseDeferredTimer->scheduleBlockedTask(instance->codeBlock()->plan().pendingPromise(), [&vm, promise, instance, module, entries] () {
-            auto* globalObject = instance->globalObject();
-            ExecState* exec = globalObject->globalExec();
-            resolve(vm, exec, promise, instance, module, entries);
-        });
-        return;
-    }
-    ASSERT(!instance->codeBlock());
-
-    // FIXME: This re-parses the module header, which shouldn't be necessary.
-    // https://bugs.webkit.org/show_bug.cgi?id=170205
-    Ref<Plan> plan = adoptRef(*new Plan(vm, module->source(), Plan::FullCompile, [promise, instance, module, entries] (Plan& p) {
-        RefPtr<Plan> plan = makeRef(p);
-        plan->vm().promiseDeferredTimer->scheduleWorkSoon(promise, [promise, instance, module, entries, plan = WTFMove(plan)] () {
-            VM& vm = plan->vm();
-            ExecState* exec = instance->globalObject()->globalExec();
-            resolve(vm, exec, promise, instance, module, entries);
-        });
-    }));
-
-    instance->addUnitializedCodeBlock(vm, plan.copyRef());
-    plan->setModeAndPromise(instance->memoryMode(), promise);
-    Wasm::ensureWorklist().enqueue(WTFMove(plan));
-}
-
-static void compileAndInstantiate(VM& vm, ExecState* exec, JSPromiseDeferred* promise, JSValue buffer, JSObject* importObject)
-{
-    auto scope = DECLARE_THROW_SCOPE(vm);
-    RefPtr<ArrayBuffer> source = createSourceBufferFromValue(vm, exec, buffer);
-    RETURN_IF_EXCEPTION(scope, void());
-
-    auto* globalObject = exec->lexicalGlobalObject();
-
-    Vector<Strong<JSCell>> dependencies;
-    dependencies.append(Strong<JSCell>(vm, importObject));
-    vm.promiseDeferredTimer->addPendingPromise(promise, WTFMove(dependencies));
-
-    Ref<Plan> plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, importObject, globalObject] (Plan& p) mutable {
-        RefPtr<Plan> plan = makeRef(p);
-        plan->vm().promiseDeferredTimer->scheduleWorkSoon(promise, [source, promise, importObject, globalObject, plan = WTFMove(plan)] () mutable {
-            VM& vm = plan->vm();
-            auto scope = DECLARE_CATCH_SCOPE(vm);
-            ExecState* exec = globalObject->globalExec();
-            JSWebAssemblyModule* module = JSWebAssemblyModule::createStub(vm, exec, globalObject->WebAssemblyModuleStructure(), WTFMove(source), plan.copyRef());
-            if (scope.exception()) {
-                reject(exec, scope, promise);
-                return;
-            }
-
-            instantiate(vm, exec, promise, module, importObject, Resolve::WithModuleAndInstance);
-        });
-    }));
-
-    Wasm::ensureWorklist().enqueue(WTFMove(plan));
-}
-
-EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState* exec)
-{
-    VM& vm = exec->vm();
-    auto catchScope = DECLARE_CATCH_SCOPE(vm);
-
-    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, exec->lexicalGlobalObject());
-    RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
-
-    JSValue importArgument = exec->argument(1);
-    JSObject* importObject = importArgument.getObject();
-    if (!importArgument.isUndefined() && !importObject) {
-        promise->reject(exec, createTypeError(exec,
-            ASCIILiteral("second argument to WebAssembly.instantiate must be undefined or an Object"), defaultSourceAppender, runtimeTypeForValue(importArgument)));
-        return JSValue::encode(promise->promise());
-    }
-
-    JSValue firstArgument = exec->argument(0);
-    if (auto* module = jsDynamicCast<JSWebAssemblyModule*>(vm, firstArgument))
-        instantiate(vm, exec, promise, module, importObject, Resolve::WithInstance);
-    else
-        compileAndInstantiate(vm, exec, promise, firstArgument, importObject);
-
-    return JSValue::encode(promise->promise());
-}
-
-EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState* exec)
-{
-    VM& vm = exec->vm();
-    auto scope = DECLARE_THROW_SCOPE(vm);
-
-    size_t byteOffset;
-    size_t byteSize;
-    uint8_t* base = getWasmBufferFromValue(exec, exec->argument(0), byteOffset, byteSize);
-    RETURN_IF_EXCEPTION(scope, encodedJSValue());
-    Wasm::Plan plan(vm, base + byteOffset, byteSize, Plan::Validation, Plan::dontFinalize);
-    // FIXME: We might want to throw an OOM exception here if we detect that something will OOM.
-    // https://bugs.webkit.org/show_bug.cgi?id=166015
-    return JSValue::encode(jsBoolean(plan.parseAndValidateModule()));
-}
-
 const ClassInfo JSWebAssembly::s_info = { "WebAssembly", &Base::s_info, 0, CREATE_METHOD_TABLE(JSWebAssembly) };
 
-JSWebAssembly* JSWebAssembly::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
+JSWebAssembly* JSWebAssembly::create(VM& vm, JSGlobalObject*, Structure* structure)
 {
     auto* object = new (NotNull, allocateCell<JSWebAssembly>(vm.heap)) JSWebAssembly(vm, structure);
-    object->finishCreation(vm, globalObject);
+    object->finishCreation(vm);
     return object;
 }
 
@@ -253,13 +48,10 @@
     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
 }
 
-void JSWebAssembly::finishCreation(VM& vm, JSGlobalObject* globalObject)
+void JSWebAssembly::finishCreation(VM& vm)
 {
     Base::finishCreation(vm);
     ASSERT(inherits(vm, info()));
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("validate", webAssemblyValidateFunc, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("compile", webAssemblyCompileFunc, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("instantiate", webAssemblyInstantiateFunc, DontEnum, 1);
 }
 
 JSWebAssembly::JSWebAssembly(VM& vm, Structure* structure)

Modified: trunk/Source/_javascript_Core/wasm/JSWebAssembly.h (214600 => 214601)


--- trunk/Source/_javascript_Core/wasm/JSWebAssembly.h	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/wasm/JSWebAssembly.h	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -67,7 +67,7 @@
     DECLARE_INFO;
 
 protected:
-    void finishCreation(VM&, JSGlobalObject*);
+    void finishCreation(VM&);
 
 private:
     JSWebAssembly(VM&, Structure*);

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyMemoryPrototype.cpp (214600 => 214601)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyMemoryPrototype.cpp	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyMemoryPrototype.cpp	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -36,6 +36,7 @@
 
 namespace JSC {
 static EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncGrow(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState*);
 }
 
 #include "WebAssemblyMemoryPrototype.lut.h"
@@ -47,12 +48,11 @@
 
 /* Source for WebAssemblyMemoryPrototype.lut.h
 @begin prototypeTableWebAssemblyMemory
-  grow     webAssemblyMemoryProtoFuncGrow   DontEnum|Function 1
+ grow   webAssemblyMemoryProtoFuncGrow   DontEnum|Function 1
+ buffer webAssemblyMemoryProtoFuncBuffer DontEnum|Accessor 0
 @end
 */
 
-EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState*);
-
 ALWAYS_INLINE JSWebAssemblyMemory* getMemory(ExecState* exec, JSValue value)
 {
     VM& vm = exec->vm();
@@ -67,16 +67,6 @@
     return memory;
 }
 
-EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState* exec)
-{
-    VM& vm = exec->vm();
-    auto throwScope = DECLARE_THROW_SCOPE(vm);
-
-    JSWebAssemblyMemory* memory = getMemory(exec, exec->thisValue()); 
-    RETURN_IF_EXCEPTION(throwScope, { });
-    return JSValue::encode(memory->buffer(exec->vm(), exec->lexicalGlobalObject()));
-}
-
 EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncGrow(ExecState* exec)
 {
     VM& vm = exec->vm();
@@ -95,10 +85,20 @@
     return JSValue::encode(jsNumber(result.pageCount()));
 }
 
-WebAssemblyMemoryPrototype* WebAssemblyMemoryPrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
+EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState* exec)
 {
+    VM& vm = exec->vm();
+    auto throwScope = DECLARE_THROW_SCOPE(vm);
+
+    JSWebAssemblyMemory* memory = getMemory(exec, exec->thisValue()); 
+    RETURN_IF_EXCEPTION(throwScope, { });
+    return JSValue::encode(memory->buffer(exec->vm(), exec->lexicalGlobalObject()));
+}
+
+WebAssemblyMemoryPrototype* WebAssemblyMemoryPrototype::create(VM& vm, JSGlobalObject*, Structure* structure)
+{
     auto* object = new (NotNull, allocateCell<WebAssemblyMemoryPrototype>(vm.heap)) WebAssemblyMemoryPrototype(vm, structure);
-    object->finishCreation(vm, globalObject);
+    object->finishCreation(vm);
     return object;
 }
 
@@ -107,10 +107,10 @@
     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
 }
 
-void WebAssemblyMemoryPrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
+void WebAssemblyMemoryPrototype::finishCreation(VM& vm)
 {
     Base::finishCreation(vm);
-    JSC_NATIVE_GETTER("buffer", webAssemblyMemoryProtoFuncBuffer, DontEnum | Accessor);
+    ASSERT(inherits(vm, info()));
 }
 
 WebAssemblyMemoryPrototype::WebAssemblyMemoryPrototype(VM& vm, Structure* structure)

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyMemoryPrototype.h (214600 => 214601)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyMemoryPrototype.h	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyMemoryPrototype.h	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -43,7 +43,7 @@
     DECLARE_INFO;
 
 protected:
-    void finishCreation(VM&, JSGlobalObject*);
+    void finishCreation(VM&);
 
 private:
     WebAssemblyMemoryPrototype(VM&, Structure*);

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyPrototype.cpp (214600 => 214601)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyPrototype.cpp	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyPrototype.cpp	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -28,40 +28,230 @@
 
 #if ENABLE(WEBASSEMBLY)
 
+#include "Exception.h"
 #include "FunctionPrototype.h"
 #include "JSCInlines.h"
+#include "JSPromiseDeferred.h"
+#include "JSWebAssemblyHelpers.h"
+#include "JSWebAssemblyInstance.h"
+#include "JSWebAssemblyModule.h"
+#include "ObjectConstructor.h"
+#include "PromiseDeferredTimer.h"
+#include "StrongInlines.h"
+#include "WasmPlan.h"
+#include "WasmWorklist.h"
+#include "WebAssemblyInstanceConstructor.h"
+#include "WebAssemblyModuleConstructor.h"
 
+using JSC::Wasm::Plan;
+
 namespace JSC {
+static EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState*);
+}
 
-static EncodedJSValue JSC_HOST_CALL webAssemblyFunctionValidate(ExecState* exec)
+#include "WebAssemblyPrototype.lut.h"
+
+namespace JSC {
+
+const ClassInfo WebAssemblyPrototype::s_info = { "WebAssembly.prototype", &Base::s_info, &prototypeTableWebAssembly, CREATE_METHOD_TABLE(WebAssemblyPrototype) };
+
+/* Source for WebAssemblyPrototype.lut.h
+ @begin prototypeTableWebAssembly
+ compile     webAssemblyCompileFunc     DontEnum|Function 1
+ instantiate webAssemblyInstantiateFunc DontEnum|Function 1
+ validate    webAssemblyValidateFunc    DontEnum|Function 1
+ @end
+ */
+
+static EncodedJSValue reject(ExecState* exec, CatchScope& catchScope, JSPromiseDeferred* promise)
 {
-    VM& vm = exec->vm();
-    auto scope = DECLARE_THROW_SCOPE(vm);
-    return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral("WebAssembly doesn't yet implement the validate function property"))));
+    Exception* exception = catchScope.exception();
+    ASSERT(exception);
+    catchScope.clearException();
+    promise->reject(exec, exception->value());
+    return JSValue::encode(promise->promise());
 }
 
-static EncodedJSValue JSC_HOST_CALL webAssemblyFunctionCompile(ExecState* exec)
+static EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState* exec)
 {
     VM& vm = exec->vm();
-    auto scope = DECLARE_THROW_SCOPE(vm);
-    return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral("WebAssembly doesn't yet implement the compile function property"))));
+    auto scope = DECLARE_CATCH_SCOPE(vm);
+    auto* globalObject = exec->lexicalGlobalObject();
+
+    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, globalObject);
+    RETURN_IF_EXCEPTION(scope, { });
+
+    RefPtr<ArrayBuffer> source = createSourceBufferFromValue(vm, exec, exec->argument(0));
+    RETURN_IF_EXCEPTION(scope, { });
+
+    Vector<Strong<JSCell>> dependencies;
+    dependencies.append(Strong<JSCell>(vm, globalObject));
+    vm.promiseDeferredTimer->addPendingPromise(promise, WTFMove(dependencies));
+
+    Ref<Plan> plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, globalObject] (Plan& p) mutable {
+        RefPtr<Plan> plan = makeRef(p);
+        plan->vm().promiseDeferredTimer->scheduleWorkSoon(promise, [source, promise, globalObject, plan = WTFMove(plan)] () mutable {
+            VM& vm = plan->vm();
+            auto scope = DECLARE_CATCH_SCOPE(vm);
+            ExecState* exec = globalObject->globalExec();
+            JSValue module = JSWebAssemblyModule::createStub(vm, exec, globalObject->WebAssemblyModuleStructure(), WTFMove(source), WTFMove(plan));
+            if (scope.exception()) {
+                reject(exec, scope, promise);
+                return;
+            }
+
+            promise->resolve(exec, module);
+        });
+    }));
+
+    Wasm::ensureWorklist().enqueue(WTFMove(plan));
+    return JSValue::encode(promise->promise());
 }
 
+enum class Resolve { WithInstance, WithModuleAndInstance };
+static void resolve(VM& vm, ExecState* exec, JSPromiseDeferred* promise, JSWebAssemblyInstance* instance, JSWebAssemblyModule* module, Resolve entries)
+{
+    auto scope = DECLARE_CATCH_SCOPE(vm);
+    instance->finalizeCreation(vm, exec);
+    if (scope.exception()) {
+        reject(exec, scope, promise);
+        return;
+    }
+
+    if (entries == Resolve::WithInstance)
+        promise->resolve(exec, instance);
+    else {
+        JSObject* result = constructEmptyObject(exec);
+        result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("module")), module);
+        result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("instance")), instance);
+        promise->resolve(exec, result);
+    }
 }
 
-#include "WebAssemblyPrototype.lut.h"
+static void instantiate(VM& vm, ExecState* exec, JSPromiseDeferred* promise, JSWebAssemblyModule* module, JSObject* importObject, Resolve entries)
+{
+    auto scope = DECLARE_CATCH_SCOPE(vm);
+    // In order to avoid potentially recompiling a module. We first gather all the import/memory information prior to compiling code.
+    JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, exec, module, importObject, exec->lexicalGlobalObject()->WebAssemblyInstanceStructure());
+    if (scope.exception()) {
+        reject(exec, scope, promise);
+        return;
+    }
 
-namespace JSC {
+    // There are three possible cases:
+    // 1) The instance already has an initialized CodeBlock, so we have no more work to do.
+    // 2) The instance has no CodeBlock, so we need to make one and compile the code for it.
+    // 3) The instance already has an uninitialized CodeBlock, so someone else is compiling code and we just need to wait for them.
 
-const ClassInfo WebAssemblyPrototype::s_info = { "WebAssembly.prototype", &Base::s_info, &prototypeTableWebAssembly, CREATE_METHOD_TABLE(WebAssemblyPrototype) };
+    if (instance->initialized()) {
+        resolve(vm, exec, promise, instance, module, entries);
+        return;
+    }
 
-/* Source for WebAssemblyPrototype.lut.h
- @begin prototypeTableWebAssembly
- validate webAssemblyFunctionValidate  DontEnum|Function 1
- compile  webAssemblyFunctionCompile   DontEnum|Function 1
- @end
- */
+    Vector<Strong<JSCell>> dependencies;
+    // The instance keeps the module alive.
+    dependencies.append(Strong<JSCell>(vm, instance));
+    vm.promiseDeferredTimer->addPendingPromise(promise, WTFMove(dependencies));
 
+    if (instance->codeBlock()) {
+        vm.promiseDeferredTimer->scheduleBlockedTask(instance->codeBlock()->plan().pendingPromise(), [&vm, promise, instance, module, entries] () {
+            auto* globalObject = instance->globalObject();
+            ExecState* exec = globalObject->globalExec();
+            resolve(vm, exec, promise, instance, module, entries);
+        });
+        return;
+    }
+    ASSERT(!instance->codeBlock());
+
+    // FIXME: This re-parses the module header, which shouldn't be necessary.
+    // https://bugs.webkit.org/show_bug.cgi?id=170205
+    Ref<Plan> plan = adoptRef(*new Plan(vm, module->source(), Plan::FullCompile, [promise, instance, module, entries] (Plan& p) {
+        RefPtr<Plan> plan = makeRef(p);
+        plan->vm().promiseDeferredTimer->scheduleWorkSoon(promise, [promise, instance, module, entries, plan = WTFMove(plan)] () {
+            VM& vm = plan->vm();
+            ExecState* exec = instance->globalObject()->globalExec();
+            resolve(vm, exec, promise, instance, module, entries);
+        });
+    }));
+
+    instance->addUnitializedCodeBlock(vm, plan.copyRef());
+    plan->setModeAndPromise(instance->memoryMode(), promise);
+    Wasm::ensureWorklist().enqueue(WTFMove(plan));
+}
+
+static void compileAndInstantiate(VM& vm, ExecState* exec, JSPromiseDeferred* promise, JSValue buffer, JSObject* importObject)
+{
+    auto scope = DECLARE_THROW_SCOPE(vm);
+    RefPtr<ArrayBuffer> source = createSourceBufferFromValue(vm, exec, buffer);
+    RETURN_IF_EXCEPTION(scope, void());
+
+    auto* globalObject = exec->lexicalGlobalObject();
+
+    Vector<Strong<JSCell>> dependencies;
+    dependencies.append(Strong<JSCell>(vm, importObject));
+    vm.promiseDeferredTimer->addPendingPromise(promise, WTFMove(dependencies));
+
+    Ref<Plan> plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, importObject, globalObject] (Plan& p) mutable {
+        RefPtr<Plan> plan = makeRef(p);
+        plan->vm().promiseDeferredTimer->scheduleWorkSoon(promise, [source, promise, importObject, globalObject, plan = WTFMove(plan)] () mutable {
+            VM& vm = plan->vm();
+            auto scope = DECLARE_CATCH_SCOPE(vm);
+            ExecState* exec = globalObject->globalExec();
+            JSWebAssemblyModule* module = JSWebAssemblyModule::createStub(vm, exec, globalObject->WebAssemblyModuleStructure(), WTFMove(source), plan.copyRef());
+            if (scope.exception()) {
+                reject(exec, scope, promise);
+                return;
+            }
+
+            instantiate(vm, exec, promise, module, importObject, Resolve::WithModuleAndInstance);
+        });
+    }));
+
+    Wasm::ensureWorklist().enqueue(WTFMove(plan));
+}
+
+static EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState* exec)
+{
+    VM& vm = exec->vm();
+    auto catchScope = DECLARE_CATCH_SCOPE(vm);
+
+    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, exec->lexicalGlobalObject());
+    RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
+
+    JSValue importArgument = exec->argument(1);
+    JSObject* importObject = importArgument.getObject();
+    if (!importArgument.isUndefined() && !importObject) {
+        promise->reject(exec, createTypeError(exec,
+            ASCIILiteral("second argument to WebAssembly.instantiate must be undefined or an Object"), defaultSourceAppender, runtimeTypeForValue(importArgument)));
+        return JSValue::encode(promise->promise());
+    }
+
+    JSValue firstArgument = exec->argument(0);
+    if (auto* module = jsDynamicCast<JSWebAssemblyModule*>(vm, firstArgument))
+        instantiate(vm, exec, promise, module, importObject, Resolve::WithInstance);
+    else
+        compileAndInstantiate(vm, exec, promise, firstArgument, importObject);
+
+    return JSValue::encode(promise->promise());
+}
+
+static EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState* exec)
+{
+    VM& vm = exec->vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+
+    size_t byteOffset;
+    size_t byteSize;
+    uint8_t* base = getWasmBufferFromValue(exec, exec->argument(0), byteOffset, byteSize);
+    RETURN_IF_EXCEPTION(scope, encodedJSValue());
+    Wasm::Plan plan(vm, base + byteOffset, byteSize, Plan::Validation, Plan::dontFinalize);
+    // FIXME: We might want to throw an OOM exception here if we detect that something will OOM.
+    // https://bugs.webkit.org/show_bug.cgi?id=166015
+    return JSValue::encode(jsBoolean(plan.parseAndValidateModule()));
+}
+
 WebAssemblyPrototype* WebAssemblyPrototype::create(VM& vm, JSGlobalObject*, Structure* structure)
 {
     auto* object = new (NotNull, allocateCell<WebAssemblyPrototype>(vm.heap)) WebAssemblyPrototype(vm, structure);

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyTablePrototype.cpp (214600 => 214601)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyTablePrototype.cpp	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyTablePrototype.cpp	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -33,6 +33,13 @@
 #include "JSWebAssemblyHelpers.h"
 #include "JSWebAssemblyTable.h"
 
+namespace JSC {
+static EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncLength(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncGrow(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncGet(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncSet(ExecState*);
+}
+
 #include "WebAssemblyTablePrototype.lut.h"
 
 namespace JSC {
@@ -41,6 +48,10 @@
 
 /* Source for WebAssemblyTablePrototype.lut.h
  @begin prototypeTableWebAssemblyTable
+ length webAssemblyTableProtoFuncLength DontEnum|Accessor 0
+ grow   webAssemblyTableProtoFuncGrow   DontEnum|Function 1
+ get    webAssemblyTableProtoFuncGet    DontEnum|Function 1
+ set    webAssemblyTableProtoFuncSet    DontEnum|Function 2
  @end
  */
 
@@ -56,11 +67,6 @@
     return result;
 }
 
-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncLength(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncGrow(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncGet(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncSet(ExecState*);
-
 EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncLength(ExecState* exec)
 {
     VM& vm = exec->vm();
@@ -81,11 +87,8 @@
 
     uint32_t index = toNonWrappingUint32(exec, exec->argument(0));
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
-    if (!table->grow(index)) {
-        throwException(exec, throwScope,
-            createTypeError(exec, ASCIILiteral("WebAssembly.Table.prototype.grow could not grow the table")));
-        return { };
-    }
+    if (!table->grow(index))
+        return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("WebAssembly.Table.prototype.grow could not grow the table"))));
 
     return JSValue::encode(jsUndefined());
 }
@@ -100,11 +103,8 @@
 
     uint32_t index = toNonWrappingUint32(exec, exec->argument(0));
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
-    if (index >= table->size()) {
-        throwException(exec, throwScope,
-            createRangeError(exec, ASCIILiteral("WebAssembly.Table.prototype.get expects an integer less than the size of the table")));
-        return { };
-    }
+    if (index >= table->size())
+        return JSValue::encode(throwException(exec, throwScope, createRangeError(exec, ASCIILiteral("WebAssembly.Table.prototype.get expects an integer less than the size of the table"))));
 
     if (JSObject* result = table->getFunction(index))
         return JSValue::encode(result);
@@ -122,20 +122,14 @@
     JSValue value = exec->argument(1);
     WebAssemblyFunction* wasmFunction;
     WebAssemblyWrapperFunction* wasmWrapperFunction;
-    if (!value.isNull() && !isWebAssemblyHostFunction(vm, value, wasmFunction, wasmWrapperFunction)) {
-        throwException(exec, throwScope,
-            createTypeError(exec, ASCIILiteral("WebAssembly.Table.prototype.set expects the second argument to be null or an instance of WebAssembly.Function")));
-        return { };
-    }
+    if (!value.isNull() && !isWebAssemblyHostFunction(vm, value, wasmFunction, wasmWrapperFunction))
+        return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("WebAssembly.Table.prototype.set expects the second argument to be null or an instance of WebAssembly.Function"))));
 
     uint32_t index = toNonWrappingUint32(exec, exec->argument(0));
     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
 
-    if (index >= table->size()) {
-        throwException(exec, throwScope,
-            createRangeError(exec, ASCIILiteral("WebAssembly.Table.prototype.set expects an integer less than the size of the table")));
-        return { };
-    }
+    if (index >= table->size())
+        return JSValue::encode(throwException(exec, throwScope, createRangeError(exec, ASCIILiteral("WebAssembly.Table.prototype.set expects an integer less than the size of the table"))));
 
     if (value.isNull())
         table->clearFunction(index);
@@ -151,10 +145,10 @@
     return JSValue::encode(jsUndefined());
 }
 
-WebAssemblyTablePrototype* WebAssemblyTablePrototype::create(VM& vm, JSGlobalObject* globalObject, Structure* structure)
+WebAssemblyTablePrototype* WebAssemblyTablePrototype::create(VM& vm, JSGlobalObject*, Structure* structure)
 {
     auto* object = new (NotNull, allocateCell<WebAssemblyTablePrototype>(vm.heap)) WebAssemblyTablePrototype(vm, structure);
-    object->finishCreation(vm, globalObject);
+    object->finishCreation(vm);
     return object;
 }
 
@@ -163,14 +157,10 @@
     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
 }
 
-void WebAssemblyTablePrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
+void WebAssemblyTablePrototype::finishCreation(VM& vm)
 {
     Base::finishCreation(vm);
-
-    JSC_NATIVE_GETTER("length", webAssemblyTableProtoFuncLength, DontEnum | Accessor);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("grow", webAssemblyTableProtoFuncGrow, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("get", webAssemblyTableProtoFuncGet, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("set", webAssemblyTableProtoFuncSet, DontEnum, 2);
+    ASSERT(inherits(vm, info()));
 }
 
 WebAssemblyTablePrototype::WebAssemblyTablePrototype(VM& vm, Structure* structure)

Modified: trunk/Source/_javascript_Core/wasm/js/WebAssemblyTablePrototype.h (214600 => 214601)


--- trunk/Source/_javascript_Core/wasm/js/WebAssemblyTablePrototype.h	2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/_javascript_Core/wasm/js/WebAssemblyTablePrototype.h	2017-03-30 07:16:27 UTC (rev 214601)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -43,7 +43,7 @@
     DECLARE_INFO;
 
 protected:
-    void finishCreation(VM&, JSGlobalObject*);
+    void finishCreation(VM&);
 
 private:
     WebAssemblyTablePrototype(VM&, Structure*);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to