<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[214601] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/214601">214601</a></dd>
<dt>Author</dt> <dd>jfbastien@apple.com</dd>
<dt>Date</dt> <dd>2017-03-30 00:16:27 -0700 (Thu, 30 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>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/JavaScriptCore:

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:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<li><a href="#trunkJSTestswasmjsapiModulecompilejs">trunk/JSTests/wasm/js-api/Module-compile.js</a></li>
<li><a href="#trunkJSTestswasmjsapivalidatejs">trunk/JSTests/wasm/js-api/validate.js</a></li>
<li><a href="#trunkJSTestswasmjsapiwebassemblyinstantiatejs">trunk/JSTests/wasm/js-api/web-assembly-instantiate.js</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmJSWebAssemblycpp">trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmJSWebAssemblyh">trunk/Source/JavaScriptCore/wasm/JSWebAssembly.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyMemoryPrototypecpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyMemoryPrototypeh">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyPrototypecpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyPrototype.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyTablePrototypecpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyTablePrototypeh">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/JSTests/ChangeLog        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2017-03-30  JF Bastien  &lt;jfbastien@apple.com&gt;
+
+        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:
+
</ins><span class="cx"> 2017-03-29  JF Bastien  &lt;jfbastien@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         WebAssembly: add shell-only Memory mode helper
</span></span></pre></div>
<a id="trunkJSTestswasmjsapiModulecompilejs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/js-api/Module-compile.js (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,6 +1,10 @@
</span><span class="cx"> import * as assert from '../assert.js';
</span><span class="cx"> import Builder from '../Builder.js';
</span><span class="cx"> 
</span><ins>+assert.isFunction(WebAssembly.compile);
+assert.isFunction(WebAssembly.__proto__.compile);
+assert.eq(WebAssembly.compile.length, 1);
+
</ins><span class="cx"> async function testPromiseAPI() {
</span><span class="cx">     {
</span><span class="cx">         // Can't declare more than one memory.
</span></span></pre></div>
<a id="trunkJSTestswasmjsapivalidatejs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/js-api/validate.js (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,6 +1,10 @@
</span><span class="cx"> import * as assert from '../assert.js';
</span><span class="cx"> import Builder from '../Builder.js';
</span><span class="cx"> 
</span><ins>+assert.isFunction(WebAssembly.validate);
+assert.isFunction(WebAssembly.__proto__.validate);
+assert.eq(WebAssembly.validate.length, 1);
+
</ins><span class="cx"> {
</span><span class="cx">     const builder = (new Builder())
</span><span class="cx">         .Type().End()
</span></span></pre></div>
<a id="trunkJSTestswasmjsapiwebassemblyinstantiatejs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/js-api/web-assembly-instantiate.js (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -1,7 +1,8 @@
</span><span class="cx"> import * as assert from '../assert.js';
</span><span class="cx"> import Builder from '../Builder.js';
</span><span class="cx"> 
</span><del>-assert.truthy(WebAssembly.instantiate instanceof Function);
</del><ins>+assert.isFunction(WebAssembly.instantiate);
+assert.isFunction(WebAssembly.__proto__.instantiate);
</ins><span class="cx"> assert.eq(WebAssembly.instantiate.length, 1);
</span><span class="cx"> 
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,3 +1,49 @@
</span><ins>+2017-03-30  JF Bastien  &lt;jfbastien@apple.com&gt;
+
+        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:
+
</ins><span class="cx"> 2017-03-29  Keith Miller  &lt;keith_miller@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, fix the build, again. Hopefully for the last time, again!
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmJSWebAssemblycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -28,223 +28,18 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBASSEMBLY)
</span><span class="cx"> 
</span><del>-#include &quot;Exception.h&quot;
-#include &quot;FunctionPrototype.h&quot;
</del><span class="cx"> #include &quot;JSCInlines.h&quot;
</span><del>-#include &quot;JSPromiseDeferred.h&quot;
-#include &quot;JSWebAssemblyHelpers.h&quot;
-#include &quot;JSWebAssemblyInstance.h&quot;
-#include &quot;JSWebAssemblyModule.h&quot;
-#include &quot;ObjectConstructor.h&quot;
-#include &quot;PromiseDeferredTimer.h&quot;
-#include &quot;StrongInlines.h&quot;
-#include &quot;WasmPlan.h&quot;
-#include &quot;WasmWorklist.h&quot;
-#include &quot;WebAssemblyModuleConstructor.h&quot;
</del><span class="cx"> 
</span><del>-using JSC::Wasm::Plan;
-
</del><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><span class="cx"> STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSWebAssembly);
</span><span class="cx"> 
</span><del>-EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState*);
-
-static EncodedJSValue reject(ExecState* exec, CatchScope&amp; catchScope, JSPromiseDeferred* promise)
-{
-    Exception* exception = catchScope.exception();
-    ASSERT(exception);
-    catchScope.clearException();
-    promise-&gt;reject(exec, exception-&gt;value());
-    return JSValue::encode(promise-&gt;promise());
-}
-
-EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState* exec)
-{
-    VM&amp; vm = exec-&gt;vm();
-    auto scope = DECLARE_CATCH_SCOPE(vm);
-    auto* globalObject = exec-&gt;lexicalGlobalObject();
-
-    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, globalObject);
-    RETURN_IF_EXCEPTION(scope, { });
-
-    RefPtr&lt;ArrayBuffer&gt; source = createSourceBufferFromValue(vm, exec, exec-&gt;argument(0));
-    RETURN_IF_EXCEPTION(scope, { });
-
-    Vector&lt;Strong&lt;JSCell&gt;&gt; dependencies;
-    dependencies.append(Strong&lt;JSCell&gt;(vm, globalObject));
-    vm.promiseDeferredTimer-&gt;addPendingPromise(promise, WTFMove(dependencies));
-
-    Ref&lt;Plan&gt; plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, globalObject] (Plan&amp; p) mutable {
-        RefPtr&lt;Plan&gt; plan = makeRef(p);
-        plan-&gt;vm().promiseDeferredTimer-&gt;scheduleWorkSoon(promise, [source, promise, globalObject, plan = WTFMove(plan)] () mutable {
-            VM&amp; vm = plan-&gt;vm();
-            auto scope = DECLARE_CATCH_SCOPE(vm);
-            ExecState* exec = globalObject-&gt;globalExec();
-            JSValue module = JSWebAssemblyModule::createStub(vm, exec, globalObject-&gt;WebAssemblyModuleStructure(), WTFMove(source), WTFMove(plan));
-            if (scope.exception()) {
-                reject(exec, scope, promise);
-                return;
-            }
-
-            promise-&gt;resolve(exec, module);
-        });
-    }));
-
-    Wasm::ensureWorklist().enqueue(WTFMove(plan));
-    return JSValue::encode(promise-&gt;promise());
-}
-
-enum class Resolve { WithInstance, WithModuleAndInstance };
-static void resolve(VM&amp; vm, ExecState* exec, JSPromiseDeferred* promise, JSWebAssemblyInstance* instance, JSWebAssemblyModule* module, Resolve entries)
-{
-    auto scope = DECLARE_CATCH_SCOPE(vm);
-    instance-&gt;finalizeCreation(vm, exec);
-    if (scope.exception()) {
-        reject(exec, scope, promise);
-        return;
-    }
-
-    if (entries == Resolve::WithInstance)
-        promise-&gt;resolve(exec, instance);
-    else {
-        JSObject* result = constructEmptyObject(exec);
-        result-&gt;putDirect(vm, Identifier::fromString(&amp;vm, ASCIILiteral(&quot;module&quot;)), module);
-        result-&gt;putDirect(vm, Identifier::fromString(&amp;vm, ASCIILiteral(&quot;instance&quot;)), instance);
-        promise-&gt;resolve(exec, result);
-    }
-}
-
-static void instantiate(VM&amp; 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-&gt;lexicalGlobalObject()-&gt;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-&gt;initialized()) {
-        resolve(vm, exec, promise, instance, module, entries);
-        return;
-    }
-
-    Vector&lt;Strong&lt;JSCell&gt;&gt; dependencies;
-    // The instance keeps the module alive.
-    dependencies.append(Strong&lt;JSCell&gt;(vm, instance));
-    vm.promiseDeferredTimer-&gt;addPendingPromise(promise, WTFMove(dependencies));
-
-    if (instance-&gt;codeBlock()) {
-        vm.promiseDeferredTimer-&gt;scheduleBlockedTask(instance-&gt;codeBlock()-&gt;plan().pendingPromise(), [&amp;vm, promise, instance, module, entries] () {
-            auto* globalObject = instance-&gt;globalObject();
-            ExecState* exec = globalObject-&gt;globalExec();
-            resolve(vm, exec, promise, instance, module, entries);
-        });
-        return;
-    }
-    ASSERT(!instance-&gt;codeBlock());
-
-    // FIXME: This re-parses the module header, which shouldn't be necessary.
-    // https://bugs.webkit.org/show_bug.cgi?id=170205
-    Ref&lt;Plan&gt; plan = adoptRef(*new Plan(vm, module-&gt;source(), Plan::FullCompile, [promise, instance, module, entries] (Plan&amp; p) {
-        RefPtr&lt;Plan&gt; plan = makeRef(p);
-        plan-&gt;vm().promiseDeferredTimer-&gt;scheduleWorkSoon(promise, [promise, instance, module, entries, plan = WTFMove(plan)] () {
-            VM&amp; vm = plan-&gt;vm();
-            ExecState* exec = instance-&gt;globalObject()-&gt;globalExec();
-            resolve(vm, exec, promise, instance, module, entries);
-        });
-    }));
-
-    instance-&gt;addUnitializedCodeBlock(vm, plan.copyRef());
-    plan-&gt;setModeAndPromise(instance-&gt;memoryMode(), promise);
-    Wasm::ensureWorklist().enqueue(WTFMove(plan));
-}
-
-static void compileAndInstantiate(VM&amp; vm, ExecState* exec, JSPromiseDeferred* promise, JSValue buffer, JSObject* importObject)
-{
-    auto scope = DECLARE_THROW_SCOPE(vm);
-    RefPtr&lt;ArrayBuffer&gt; source = createSourceBufferFromValue(vm, exec, buffer);
-    RETURN_IF_EXCEPTION(scope, void());
-
-    auto* globalObject = exec-&gt;lexicalGlobalObject();
-
-    Vector&lt;Strong&lt;JSCell&gt;&gt; dependencies;
-    dependencies.append(Strong&lt;JSCell&gt;(vm, importObject));
-    vm.promiseDeferredTimer-&gt;addPendingPromise(promise, WTFMove(dependencies));
-
-    Ref&lt;Plan&gt; plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, importObject, globalObject] (Plan&amp; p) mutable {
-        RefPtr&lt;Plan&gt; plan = makeRef(p);
-        plan-&gt;vm().promiseDeferredTimer-&gt;scheduleWorkSoon(promise, [source, promise, importObject, globalObject, plan = WTFMove(plan)] () mutable {
-            VM&amp; vm = plan-&gt;vm();
-            auto scope = DECLARE_CATCH_SCOPE(vm);
-            ExecState* exec = globalObject-&gt;globalExec();
-            JSWebAssemblyModule* module = JSWebAssemblyModule::createStub(vm, exec, globalObject-&gt;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&amp; vm = exec-&gt;vm();
-    auto catchScope = DECLARE_CATCH_SCOPE(vm);
-
-    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, exec-&gt;lexicalGlobalObject());
-    RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
-
-    JSValue importArgument = exec-&gt;argument(1);
-    JSObject* importObject = importArgument.getObject();
-    if (!importArgument.isUndefined() &amp;&amp; !importObject) {
-        promise-&gt;reject(exec, createTypeError(exec,
-            ASCIILiteral(&quot;second argument to WebAssembly.instantiate must be undefined or an Object&quot;), defaultSourceAppender, runtimeTypeForValue(importArgument)));
-        return JSValue::encode(promise-&gt;promise());
-    }
-
-    JSValue firstArgument = exec-&gt;argument(0);
-    if (auto* module = jsDynamicCast&lt;JSWebAssemblyModule*&gt;(vm, firstArgument))
-        instantiate(vm, exec, promise, module, importObject, Resolve::WithInstance);
-    else
-        compileAndInstantiate(vm, exec, promise, firstArgument, importObject);
-
-    return JSValue::encode(promise-&gt;promise());
-}
-
-EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState* exec)
-{
-    VM&amp; vm = exec-&gt;vm();
-    auto scope = DECLARE_THROW_SCOPE(vm);
-
-    size_t byteOffset;
-    size_t byteSize;
-    uint8_t* base = getWasmBufferFromValue(exec, exec-&gt;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()));
-}
-
</del><span class="cx"> const ClassInfo JSWebAssembly::s_info = { &quot;WebAssembly&quot;, &amp;Base::s_info, 0, CREATE_METHOD_TABLE(JSWebAssembly) };
</span><span class="cx"> 
</span><del>-JSWebAssembly* JSWebAssembly::create(VM&amp; vm, JSGlobalObject* globalObject, Structure* structure)
</del><ins>+JSWebAssembly* JSWebAssembly::create(VM&amp; vm, JSGlobalObject*, Structure* structure)
</ins><span class="cx"> {
</span><span class="cx">     auto* object = new (NotNull, allocateCell&lt;JSWebAssembly&gt;(vm.heap)) JSWebAssembly(vm, structure);
</span><del>-    object-&gt;finishCreation(vm, globalObject);
</del><ins>+    object-&gt;finishCreation(vm);
</ins><span class="cx">     return object;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -253,13 +48,10 @@
</span><span class="cx">     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void JSWebAssembly::finishCreation(VM&amp; vm, JSGlobalObject* globalObject)
</del><ins>+void JSWebAssembly::finishCreation(VM&amp; vm)
</ins><span class="cx"> {
</span><span class="cx">     Base::finishCreation(vm);
</span><span class="cx">     ASSERT(inherits(vm, info()));
</span><del>-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;validate&quot;, webAssemblyValidateFunc, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;compile&quot;, webAssemblyCompileFunc, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;instantiate&quot;, webAssemblyInstantiateFunc, DontEnum, 1);
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> JSWebAssembly::JSWebAssembly(VM&amp; vm, Structure* structure)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmJSWebAssemblyh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/JSWebAssembly.h (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/JSWebAssembly.h        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/wasm/JSWebAssembly.h        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -67,7 +67,7 @@
</span><span class="cx">     DECLARE_INFO;
</span><span class="cx"> 
</span><span class="cx"> protected:
</span><del>-    void finishCreation(VM&amp;, JSGlobalObject*);
</del><ins>+    void finishCreation(VM&amp;);
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     JSWebAssembly(VM&amp;, Structure*);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyMemoryPrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.cpp (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.cpp        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.cpp        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -36,6 +36,7 @@
</span><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncGrow(ExecState*);
</span><ins>+static EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState*);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #include &quot;WebAssemblyMemoryPrototype.lut.h&quot;
</span><span class="lines">@@ -47,12 +48,11 @@
</span><span class="cx"> 
</span><span class="cx"> /* Source for WebAssemblyMemoryPrototype.lut.h
</span><span class="cx"> @begin prototypeTableWebAssemblyMemory
</span><del>-  grow     webAssemblyMemoryProtoFuncGrow   DontEnum|Function 1
</del><ins>+ grow   webAssemblyMemoryProtoFuncGrow   DontEnum|Function 1
+ buffer webAssemblyMemoryProtoFuncBuffer DontEnum|Accessor 0
</ins><span class="cx"> @end
</span><span class="cx"> */
</span><span class="cx"> 
</span><del>-EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState*);
-
</del><span class="cx"> ALWAYS_INLINE JSWebAssemblyMemory* getMemory(ExecState* exec, JSValue value)
</span><span class="cx"> {
</span><span class="cx">     VM&amp; vm = exec-&gt;vm();
</span><span class="lines">@@ -67,16 +67,6 @@
</span><span class="cx">     return memory;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState* exec)
-{
-    VM&amp; vm = exec-&gt;vm();
-    auto throwScope = DECLARE_THROW_SCOPE(vm);
-
-    JSWebAssemblyMemory* memory = getMemory(exec, exec-&gt;thisValue()); 
-    RETURN_IF_EXCEPTION(throwScope, { });
-    return JSValue::encode(memory-&gt;buffer(exec-&gt;vm(), exec-&gt;lexicalGlobalObject()));
-}
-
</del><span class="cx"> EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncGrow(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx">     VM&amp; vm = exec-&gt;vm();
</span><span class="lines">@@ -95,10 +85,20 @@
</span><span class="cx">     return JSValue::encode(jsNumber(result.pageCount()));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-WebAssemblyMemoryPrototype* WebAssemblyMemoryPrototype::create(VM&amp; vm, JSGlobalObject* globalObject, Structure* structure)
</del><ins>+EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState* exec)
</ins><span class="cx"> {
</span><ins>+    VM&amp; vm = exec-&gt;vm();
+    auto throwScope = DECLARE_THROW_SCOPE(vm);
+
+    JSWebAssemblyMemory* memory = getMemory(exec, exec-&gt;thisValue()); 
+    RETURN_IF_EXCEPTION(throwScope, { });
+    return JSValue::encode(memory-&gt;buffer(exec-&gt;vm(), exec-&gt;lexicalGlobalObject()));
+}
+
+WebAssemblyMemoryPrototype* WebAssemblyMemoryPrototype::create(VM&amp; vm, JSGlobalObject*, Structure* structure)
+{
</ins><span class="cx">     auto* object = new (NotNull, allocateCell&lt;WebAssemblyMemoryPrototype&gt;(vm.heap)) WebAssemblyMemoryPrototype(vm, structure);
</span><del>-    object-&gt;finishCreation(vm, globalObject);
</del><ins>+    object-&gt;finishCreation(vm);
</ins><span class="cx">     return object;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -107,10 +107,10 @@
</span><span class="cx">     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebAssemblyMemoryPrototype::finishCreation(VM&amp; vm, JSGlobalObject* globalObject)
</del><ins>+void WebAssemblyMemoryPrototype::finishCreation(VM&amp; vm)
</ins><span class="cx"> {
</span><span class="cx">     Base::finishCreation(vm);
</span><del>-    JSC_NATIVE_GETTER(&quot;buffer&quot;, webAssemblyMemoryProtoFuncBuffer, DontEnum | Accessor);
</del><ins>+    ASSERT(inherits(vm, info()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> WebAssemblyMemoryPrototype::WebAssemblyMemoryPrototype(VM&amp; vm, Structure* structure)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyMemoryPrototypeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.h (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.h        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyMemoryPrototype.h        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -43,7 +43,7 @@
</span><span class="cx">     DECLARE_INFO;
</span><span class="cx"> 
</span><span class="cx"> protected:
</span><del>-    void finishCreation(VM&amp;, JSGlobalObject*);
</del><ins>+    void finishCreation(VM&amp;);
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     WebAssemblyMemoryPrototype(VM&amp;, Structure*);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyPrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyPrototype.cpp (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyPrototype.cpp        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyPrototype.cpp        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -28,40 +28,230 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBASSEMBLY)
</span><span class="cx"> 
</span><ins>+#include &quot;Exception.h&quot;
</ins><span class="cx"> #include &quot;FunctionPrototype.h&quot;
</span><span class="cx"> #include &quot;JSCInlines.h&quot;
</span><ins>+#include &quot;JSPromiseDeferred.h&quot;
+#include &quot;JSWebAssemblyHelpers.h&quot;
+#include &quot;JSWebAssemblyInstance.h&quot;
+#include &quot;JSWebAssemblyModule.h&quot;
+#include &quot;ObjectConstructor.h&quot;
+#include &quot;PromiseDeferredTimer.h&quot;
+#include &quot;StrongInlines.h&quot;
+#include &quot;WasmPlan.h&quot;
+#include &quot;WasmWorklist.h&quot;
+#include &quot;WebAssemblyInstanceConstructor.h&quot;
+#include &quot;WebAssemblyModuleConstructor.h&quot;
</ins><span class="cx"> 
</span><ins>+using JSC::Wasm::Plan;
+
</ins><span class="cx"> namespace JSC {
</span><ins>+static EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState*);
+static EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState*);
+}
</ins><span class="cx"> 
</span><del>-static EncodedJSValue JSC_HOST_CALL webAssemblyFunctionValidate(ExecState* exec)
</del><ins>+#include &quot;WebAssemblyPrototype.lut.h&quot;
+
+namespace JSC {
+
+const ClassInfo WebAssemblyPrototype::s_info = { &quot;WebAssembly.prototype&quot;, &amp;Base::s_info, &amp;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&amp; catchScope, JSPromiseDeferred* promise)
</ins><span class="cx"> {
</span><del>-    VM&amp; vm = exec-&gt;vm();
-    auto scope = DECLARE_THROW_SCOPE(vm);
-    return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral(&quot;WebAssembly doesn't yet implement the validate function property&quot;))));
</del><ins>+    Exception* exception = catchScope.exception();
+    ASSERT(exception);
+    catchScope.clearException();
+    promise-&gt;reject(exec, exception-&gt;value());
+    return JSValue::encode(promise-&gt;promise());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-static EncodedJSValue JSC_HOST_CALL webAssemblyFunctionCompile(ExecState* exec)
</del><ins>+static EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState* exec)
</ins><span class="cx"> {
</span><span class="cx">     VM&amp; vm = exec-&gt;vm();
</span><del>-    auto scope = DECLARE_THROW_SCOPE(vm);
-    return JSValue::encode(throwException(exec, scope, createError(exec, ASCIILiteral(&quot;WebAssembly doesn't yet implement the compile function property&quot;))));
</del><ins>+    auto scope = DECLARE_CATCH_SCOPE(vm);
+    auto* globalObject = exec-&gt;lexicalGlobalObject();
+
+    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, globalObject);
+    RETURN_IF_EXCEPTION(scope, { });
+
+    RefPtr&lt;ArrayBuffer&gt; source = createSourceBufferFromValue(vm, exec, exec-&gt;argument(0));
+    RETURN_IF_EXCEPTION(scope, { });
+
+    Vector&lt;Strong&lt;JSCell&gt;&gt; dependencies;
+    dependencies.append(Strong&lt;JSCell&gt;(vm, globalObject));
+    vm.promiseDeferredTimer-&gt;addPendingPromise(promise, WTFMove(dependencies));
+
+    Ref&lt;Plan&gt; plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, globalObject] (Plan&amp; p) mutable {
+        RefPtr&lt;Plan&gt; plan = makeRef(p);
+        plan-&gt;vm().promiseDeferredTimer-&gt;scheduleWorkSoon(promise, [source, promise, globalObject, plan = WTFMove(plan)] () mutable {
+            VM&amp; vm = plan-&gt;vm();
+            auto scope = DECLARE_CATCH_SCOPE(vm);
+            ExecState* exec = globalObject-&gt;globalExec();
+            JSValue module = JSWebAssemblyModule::createStub(vm, exec, globalObject-&gt;WebAssemblyModuleStructure(), WTFMove(source), WTFMove(plan));
+            if (scope.exception()) {
+                reject(exec, scope, promise);
+                return;
+            }
+
+            promise-&gt;resolve(exec, module);
+        });
+    }));
+
+    Wasm::ensureWorklist().enqueue(WTFMove(plan));
+    return JSValue::encode(promise-&gt;promise());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+enum class Resolve { WithInstance, WithModuleAndInstance };
+static void resolve(VM&amp; vm, ExecState* exec, JSPromiseDeferred* promise, JSWebAssemblyInstance* instance, JSWebAssemblyModule* module, Resolve entries)
+{
+    auto scope = DECLARE_CATCH_SCOPE(vm);
+    instance-&gt;finalizeCreation(vm, exec);
+    if (scope.exception()) {
+        reject(exec, scope, promise);
+        return;
+    }
+
+    if (entries == Resolve::WithInstance)
+        promise-&gt;resolve(exec, instance);
+    else {
+        JSObject* result = constructEmptyObject(exec);
+        result-&gt;putDirect(vm, Identifier::fromString(&amp;vm, ASCIILiteral(&quot;module&quot;)), module);
+        result-&gt;putDirect(vm, Identifier::fromString(&amp;vm, ASCIILiteral(&quot;instance&quot;)), instance);
+        promise-&gt;resolve(exec, result);
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-#include &quot;WebAssemblyPrototype.lut.h&quot;
</del><ins>+static void instantiate(VM&amp; 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-&gt;lexicalGlobalObject()-&gt;WebAssemblyInstanceStructure());
+    if (scope.exception()) {
+        reject(exec, scope, promise);
+        return;
+    }
</ins><span class="cx"> 
</span><del>-namespace JSC {
</del><ins>+    // 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.
</ins><span class="cx"> 
</span><del>-const ClassInfo WebAssemblyPrototype::s_info = { &quot;WebAssembly.prototype&quot;, &amp;Base::s_info, &amp;prototypeTableWebAssembly, CREATE_METHOD_TABLE(WebAssemblyPrototype) };
</del><ins>+    if (instance-&gt;initialized()) {
+        resolve(vm, exec, promise, instance, module, entries);
+        return;
+    }
</ins><span class="cx"> 
</span><del>-/* Source for WebAssemblyPrototype.lut.h
- @begin prototypeTableWebAssembly
- validate webAssemblyFunctionValidate  DontEnum|Function 1
- compile  webAssemblyFunctionCompile   DontEnum|Function 1
- @end
- */
</del><ins>+    Vector&lt;Strong&lt;JSCell&gt;&gt; dependencies;
+    // The instance keeps the module alive.
+    dependencies.append(Strong&lt;JSCell&gt;(vm, instance));
+    vm.promiseDeferredTimer-&gt;addPendingPromise(promise, WTFMove(dependencies));
</ins><span class="cx"> 
</span><ins>+    if (instance-&gt;codeBlock()) {
+        vm.promiseDeferredTimer-&gt;scheduleBlockedTask(instance-&gt;codeBlock()-&gt;plan().pendingPromise(), [&amp;vm, promise, instance, module, entries] () {
+            auto* globalObject = instance-&gt;globalObject();
+            ExecState* exec = globalObject-&gt;globalExec();
+            resolve(vm, exec, promise, instance, module, entries);
+        });
+        return;
+    }
+    ASSERT(!instance-&gt;codeBlock());
+
+    // FIXME: This re-parses the module header, which shouldn't be necessary.
+    // https://bugs.webkit.org/show_bug.cgi?id=170205
+    Ref&lt;Plan&gt; plan = adoptRef(*new Plan(vm, module-&gt;source(), Plan::FullCompile, [promise, instance, module, entries] (Plan&amp; p) {
+        RefPtr&lt;Plan&gt; plan = makeRef(p);
+        plan-&gt;vm().promiseDeferredTimer-&gt;scheduleWorkSoon(promise, [promise, instance, module, entries, plan = WTFMove(plan)] () {
+            VM&amp; vm = plan-&gt;vm();
+            ExecState* exec = instance-&gt;globalObject()-&gt;globalExec();
+            resolve(vm, exec, promise, instance, module, entries);
+        });
+    }));
+
+    instance-&gt;addUnitializedCodeBlock(vm, plan.copyRef());
+    plan-&gt;setModeAndPromise(instance-&gt;memoryMode(), promise);
+    Wasm::ensureWorklist().enqueue(WTFMove(plan));
+}
+
+static void compileAndInstantiate(VM&amp; vm, ExecState* exec, JSPromiseDeferred* promise, JSValue buffer, JSObject* importObject)
+{
+    auto scope = DECLARE_THROW_SCOPE(vm);
+    RefPtr&lt;ArrayBuffer&gt; source = createSourceBufferFromValue(vm, exec, buffer);
+    RETURN_IF_EXCEPTION(scope, void());
+
+    auto* globalObject = exec-&gt;lexicalGlobalObject();
+
+    Vector&lt;Strong&lt;JSCell&gt;&gt; dependencies;
+    dependencies.append(Strong&lt;JSCell&gt;(vm, importObject));
+    vm.promiseDeferredTimer-&gt;addPendingPromise(promise, WTFMove(dependencies));
+
+    Ref&lt;Plan&gt; plan = adoptRef(*new Plan(vm, *source, Plan::Validation, [source, promise, importObject, globalObject] (Plan&amp; p) mutable {
+        RefPtr&lt;Plan&gt; plan = makeRef(p);
+        plan-&gt;vm().promiseDeferredTimer-&gt;scheduleWorkSoon(promise, [source, promise, importObject, globalObject, plan = WTFMove(plan)] () mutable {
+            VM&amp; vm = plan-&gt;vm();
+            auto scope = DECLARE_CATCH_SCOPE(vm);
+            ExecState* exec = globalObject-&gt;globalExec();
+            JSWebAssemblyModule* module = JSWebAssemblyModule::createStub(vm, exec, globalObject-&gt;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&amp; vm = exec-&gt;vm();
+    auto catchScope = DECLARE_CATCH_SCOPE(vm);
+
+    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, exec-&gt;lexicalGlobalObject());
+    RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
+
+    JSValue importArgument = exec-&gt;argument(1);
+    JSObject* importObject = importArgument.getObject();
+    if (!importArgument.isUndefined() &amp;&amp; !importObject) {
+        promise-&gt;reject(exec, createTypeError(exec,
+            ASCIILiteral(&quot;second argument to WebAssembly.instantiate must be undefined or an Object&quot;), defaultSourceAppender, runtimeTypeForValue(importArgument)));
+        return JSValue::encode(promise-&gt;promise());
+    }
+
+    JSValue firstArgument = exec-&gt;argument(0);
+    if (auto* module = jsDynamicCast&lt;JSWebAssemblyModule*&gt;(vm, firstArgument))
+        instantiate(vm, exec, promise, module, importObject, Resolve::WithInstance);
+    else
+        compileAndInstantiate(vm, exec, promise, firstArgument, importObject);
+
+    return JSValue::encode(promise-&gt;promise());
+}
+
+static EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState* exec)
+{
+    VM&amp; vm = exec-&gt;vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+
+    size_t byteOffset;
+    size_t byteSize;
+    uint8_t* base = getWasmBufferFromValue(exec, exec-&gt;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()));
+}
+
</ins><span class="cx"> WebAssemblyPrototype* WebAssemblyPrototype::create(VM&amp; vm, JSGlobalObject*, Structure* structure)
</span><span class="cx"> {
</span><span class="cx">     auto* object = new (NotNull, allocateCell&lt;WebAssemblyPrototype&gt;(vm.heap)) WebAssemblyPrototype(vm, structure);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyTablePrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.cpp (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.cpp        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.cpp        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -33,6 +33,13 @@
</span><span class="cx"> #include &quot;JSWebAssemblyHelpers.h&quot;
</span><span class="cx"> #include &quot;JSWebAssemblyTable.h&quot;
</span><span class="cx"> 
</span><ins>+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*);
+}
+
</ins><span class="cx"> #include &quot;WebAssemblyTablePrototype.lut.h&quot;
</span><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="lines">@@ -41,6 +48,10 @@
</span><span class="cx"> 
</span><span class="cx"> /* Source for WebAssemblyTablePrototype.lut.h
</span><span class="cx">  @begin prototypeTableWebAssemblyTable
</span><ins>+ length webAssemblyTableProtoFuncLength DontEnum|Accessor 0
+ grow   webAssemblyTableProtoFuncGrow   DontEnum|Function 1
+ get    webAssemblyTableProtoFuncGet    DontEnum|Function 1
+ set    webAssemblyTableProtoFuncSet    DontEnum|Function 2
</ins><span class="cx">  @end
</span><span class="cx">  */
</span><span class="cx"> 
</span><span class="lines">@@ -56,11 +67,6 @@
</span><span class="cx">     return result;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncLength(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncGrow(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncGet(ExecState*);
-EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncSet(ExecState*);
-
</del><span class="cx"> EncodedJSValue JSC_HOST_CALL webAssemblyTableProtoFuncLength(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx">     VM&amp; vm = exec-&gt;vm();
</span><span class="lines">@@ -81,11 +87,8 @@
</span><span class="cx"> 
</span><span class="cx">     uint32_t index = toNonWrappingUint32(exec, exec-&gt;argument(0));
</span><span class="cx">     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</span><del>-    if (!table-&gt;grow(index)) {
-        throwException(exec, throwScope,
-            createTypeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.grow could not grow the table&quot;)));
-        return { };
-    }
</del><ins>+    if (!table-&gt;grow(index))
+        return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.grow could not grow the table&quot;))));
</ins><span class="cx"> 
</span><span class="cx">     return JSValue::encode(jsUndefined());
</span><span class="cx"> }
</span><span class="lines">@@ -100,11 +103,8 @@
</span><span class="cx"> 
</span><span class="cx">     uint32_t index = toNonWrappingUint32(exec, exec-&gt;argument(0));
</span><span class="cx">     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</span><del>-    if (index &gt;= table-&gt;size()) {
-        throwException(exec, throwScope,
-            createRangeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.get expects an integer less than the size of the table&quot;)));
-        return { };
-    }
</del><ins>+    if (index &gt;= table-&gt;size())
+        return JSValue::encode(throwException(exec, throwScope, createRangeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.get expects an integer less than the size of the table&quot;))));
</ins><span class="cx"> 
</span><span class="cx">     if (JSObject* result = table-&gt;getFunction(index))
</span><span class="cx">         return JSValue::encode(result);
</span><span class="lines">@@ -122,20 +122,14 @@
</span><span class="cx">     JSValue value = exec-&gt;argument(1);
</span><span class="cx">     WebAssemblyFunction* wasmFunction;
</span><span class="cx">     WebAssemblyWrapperFunction* wasmWrapperFunction;
</span><del>-    if (!value.isNull() &amp;&amp; !isWebAssemblyHostFunction(vm, value, wasmFunction, wasmWrapperFunction)) {
-        throwException(exec, throwScope,
-            createTypeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.set expects the second argument to be null or an instance of WebAssembly.Function&quot;)));
-        return { };
-    }
</del><ins>+    if (!value.isNull() &amp;&amp; !isWebAssemblyHostFunction(vm, value, wasmFunction, wasmWrapperFunction))
+        return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.set expects the second argument to be null or an instance of WebAssembly.Function&quot;))));
</ins><span class="cx"> 
</span><span class="cx">     uint32_t index = toNonWrappingUint32(exec, exec-&gt;argument(0));
</span><span class="cx">     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</span><span class="cx"> 
</span><del>-    if (index &gt;= table-&gt;size()) {
-        throwException(exec, throwScope,
-            createRangeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.set expects an integer less than the size of the table&quot;)));
-        return { };
-    }
</del><ins>+    if (index &gt;= table-&gt;size())
+        return JSValue::encode(throwException(exec, throwScope, createRangeError(exec, ASCIILiteral(&quot;WebAssembly.Table.prototype.set expects an integer less than the size of the table&quot;))));
</ins><span class="cx"> 
</span><span class="cx">     if (value.isNull())
</span><span class="cx">         table-&gt;clearFunction(index);
</span><span class="lines">@@ -151,10 +145,10 @@
</span><span class="cx">     return JSValue::encode(jsUndefined());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-WebAssemblyTablePrototype* WebAssemblyTablePrototype::create(VM&amp; vm, JSGlobalObject* globalObject, Structure* structure)
</del><ins>+WebAssemblyTablePrototype* WebAssemblyTablePrototype::create(VM&amp; vm, JSGlobalObject*, Structure* structure)
</ins><span class="cx"> {
</span><span class="cx">     auto* object = new (NotNull, allocateCell&lt;WebAssemblyTablePrototype&gt;(vm.heap)) WebAssemblyTablePrototype(vm, structure);
</span><del>-    object-&gt;finishCreation(vm, globalObject);
</del><ins>+    object-&gt;finishCreation(vm);
</ins><span class="cx">     return object;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -163,14 +157,10 @@
</span><span class="cx">     return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebAssemblyTablePrototype::finishCreation(VM&amp; vm, JSGlobalObject* globalObject)
</del><ins>+void WebAssemblyTablePrototype::finishCreation(VM&amp; vm)
</ins><span class="cx"> {
</span><span class="cx">     Base::finishCreation(vm);
</span><del>-
-    JSC_NATIVE_GETTER(&quot;length&quot;, webAssemblyTableProtoFuncLength, DontEnum | Accessor);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;grow&quot;, webAssemblyTableProtoFuncGrow, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;get&quot;, webAssemblyTableProtoFuncGet, DontEnum, 1);
-    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;set&quot;, webAssemblyTableProtoFuncSet, DontEnum, 2);
</del><ins>+    ASSERT(inherits(vm, info()));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> WebAssemblyTablePrototype::WebAssemblyTablePrototype(VM&amp; vm, Structure* structure)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyTablePrototypeh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.h (214600 => 214601)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.h        2017-03-30 06:35:31 UTC (rev 214600)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyTablePrototype.h        2017-03-30 07:16:27 UTC (rev 214601)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2016 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2016-2017 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -43,7 +43,7 @@
</span><span class="cx">     DECLARE_INFO;
</span><span class="cx"> 
</span><span class="cx"> protected:
</span><del>-    void finishCreation(VM&amp;, JSGlobalObject*);
</del><ins>+    void finishCreation(VM&amp;);
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     WebAssemblyTablePrototype(VM&amp;, Structure*);
</span></span></pre>
</div>
</div>

</body>
</html>