<!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>[209979] 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/209979">209979</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2016-12-18 23:22:42 -0800 (Sun, 18 Dec 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>WebAssembly: Implement the WebAssembly.compile and WebAssembly.validate
https://bugs.webkit.org/show_bug.cgi?id=165936

Reviewed by Mark Lam.

JSTests:

* wasm/js-api/Module-compile.js: Added.
(async.testPromiseAPI):
* wasm/js-api/test_basic_api.js:
(const.c.in.constructorProperties.switch):
* wasm/js-api/validate.js: Added.
(assert.truthy.WebAssembly.validate.builder.WebAssembly):

Source/JavaScriptCore:

The APIs are documented here:
- https://github.com/WebAssembly/design/blob/master/JS.md#webassemblycompile
- https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyvalidate

* wasm/JSWebAssembly.cpp:
(JSC::webAssemblyCompileFunc):
(JSC::webAssemblyValidateFunc):
(JSC::JSWebAssembly::finishCreation):
* wasm/WasmPlan.cpp:
(JSC::Wasm::Plan::parseAndValidateModule):
(JSC::Wasm::Plan::run):
* wasm/WasmPlan.h:
* wasm/js/JSWebAssemblyHelpers.h:
(JSC::getWasmBufferFromValue):
* wasm/js/WebAssemblyModuleConstructor.cpp:
(JSC::constructJSWebAssemblyModule):
(JSC::callJSWebAssemblyModule):
(JSC::WebAssemblyModuleConstructor::createModule):
* wasm/js/WebAssemblyModuleConstructor.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<li><a href="#trunkJSTestswasmjsapitest_basic_apijs">trunk/JSTests/wasm/js-api/test_basic_api.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="#trunkSourceJavaScriptCorewasmWasmPlancpp">trunk/Source/JavaScriptCore/wasm/WasmPlan.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmPlanh">trunk/Source/JavaScriptCore/wasm/WasmPlan.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsJSWebAssemblyHelpersh">trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyHelpers.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyModuleConstructorcpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyModuleConstructorh">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<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>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/JSTests/ChangeLog        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2016-12-18  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: Implement the WebAssembly.compile and WebAssembly.validate
+        https://bugs.webkit.org/show_bug.cgi?id=165936
+
+        Reviewed by Mark Lam.
+
+        * wasm/js-api/Module-compile.js: Added.
+        (async.testPromiseAPI):
+        * wasm/js-api/test_basic_api.js:
+        (const.c.in.constructorProperties.switch):
+        * wasm/js-api/validate.js: Added.
+        (assert.truthy.WebAssembly.validate.builder.WebAssembly):
+
</ins><span class="cx"> 2016-12-16  Mark Lam  &lt;mark.lam@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         De-duplicate finally blocks.
</span></span></pre></div>
<a id="trunkJSTestswasmjsapiModulecompilejs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/js-api/Module-compile.js (0 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/js-api/Module-compile.js                                (rev 0)
+++ trunk/JSTests/wasm/js-api/Module-compile.js        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -0,0 +1,58 @@
</span><ins>+import * as assert from '../assert.js';
+import Builder from '../Builder.js';
+
+let done = false;
+async function testPromiseAPI() {
+    {
+        // Can't declare more than one memory.
+        const builder = (new Builder())
+            .Type().End()
+            .Import().Memory(&quot;imp&quot;, &quot;memory&quot;, {initial: 20}).End()
+            .Function().End()
+            .Memory().InitialMaxPages(1, 1).End()
+            .Export().End()
+            .Code()
+            .End();
+
+        let threw = false;
+        try {
+            await WebAssembly.compile(builder.WebAssembly().get());
+        } catch(e) {
+            threw = true;
+            assert.truthy(e instanceof WebAssembly.CompileError);
+            assert.truthy(e.message === &quot;WebAssembly.Module doesn't parse at byte 34 / 43: Memory section cannot exist if an Import has a memory (evaluating 'WebAssembly.compile(builder.WebAssembly().get())')&quot;);
+        }
+        assert.truthy(threw);
+    }
+
+    {
+        let threw = false;
+        try {
+            await WebAssembly.compile(20);
+        } catch(e) {
+            threw = true;
+            assert.truthy(e instanceof TypeError);
+            assert.eq(e.message, &quot;first argument must be an ArrayBufferView or an ArrayBuffer (evaluating 'WebAssembly.compile(20)')&quot;);
+        }
+        assert.truthy(threw);
+    }
+
+    {
+        const builder = (new Builder())
+            .Type().End()
+            .Import().Memory(&quot;imp&quot;, &quot;memory&quot;, {initial: 20}).End()
+            .Function().End()
+            .Export().End()
+            .Code()
+            .End();
+
+        let module = await WebAssembly.compile(builder.WebAssembly().get());
+        assert.truthy(module instanceof WebAssembly.Module);
+    }
+
+    done = true;
+}
+
+testPromiseAPI();
+drainMicrotasks();
+assert.truthy(done);
</ins></span></pre></div>
<a id="trunkJSTestswasmjsapitest_basic_apijs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/js-api/test_basic_api.js (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/js-api/test_basic_api.js        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/JSTests/wasm/js-api/test_basic_api.js        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -52,7 +52,7 @@
</span><span class="cx">     switch (c) {
</span><span class="cx">     case &quot;Module&quot;:
</span><span class="cx">         for (const invalid of invalidConstructorInputs)
</span><del>-            assert.throws(() =&gt; new WebAssembly[c](invalid), TypeError, `first argument to WebAssembly.Module must be an ArrayBufferView or an ArrayBuffer (evaluating 'new WebAssembly[c](invalid)')`);
</del><ins>+            assert.throws(() =&gt; new WebAssembly[c](invalid), TypeError, `first argument must be an ArrayBufferView or an ArrayBuffer (evaluating 'new WebAssembly[c](invalid)')`);
</ins><span class="cx">         for (const buffer of [new ArrayBuffer(), new DataView(new ArrayBuffer()), new Int8Array(), new Uint8Array(), new Uint8ClampedArray(), new Int16Array(), new Uint16Array(), new Int32Array(), new Uint32Array(), new Float32Array(), new Float64Array()])
</span><span class="cx">             // FIXME the following should be WebAssembly.CompileError. https://bugs.webkit.org/show_bug.cgi?id=163768
</span><span class="cx">             assert.throws(() =&gt; new WebAssembly[c](buffer), Error, `WebAssembly.Module doesn't parse at byte 0 / 0: expected a module of at least 8 bytes (evaluating 'new WebAssembly[c](buffer)')`);
</span><span class="lines">@@ -97,8 +97,3 @@
</span><span class="cx">     default: throw new Error(`Implementation error: unexpected constructor property &quot;${c}&quot;`);
</span><span class="cx">     }
</span><span class="cx"> }
</span><del>-
-// FIXME Implement and test these APIs further. For now they just throw. https://bugs.webkit.org/show_bug.cgi?id=159775
-for (const f in functionProperties) {
-    assert.throws(() =&gt; WebAssembly[f](), Error, `WebAssembly doesn't yet implement the ${f} function property`);
-}
</del></span></pre></div>
<a id="trunkJSTestswasmjsapivalidatejs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/js-api/validate.js (0 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/js-api/validate.js                                (rev 0)
+++ trunk/JSTests/wasm/js-api/validate.js        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -0,0 +1,27 @@
</span><ins>+import * as assert from '../assert.js';
+import Builder from '../Builder.js';
+
+{
+    const builder = (new Builder())
+        .Type().End()
+        .Import().Memory(&quot;imp&quot;, &quot;memory&quot;, {initial: 20}).End()
+        .Function().End()
+        .Memory().InitialMaxPages(1, 1).End()
+        .Export().End()
+        .Code()
+        .End();
+
+    assert.truthy(!WebAssembly.validate(builder.WebAssembly().get()));
+}
+
+{
+    const builder = (new Builder())
+        .Type().End()
+        .Import().Memory(&quot;imp&quot;, &quot;memory&quot;, {initial: 20}).End()
+        .Function().End()
+        .Export().End()
+        .Code()
+        .End();
+
+    assert.truthy(WebAssembly.validate(builder.WebAssembly().get()));
+}
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -1,3 +1,30 @@
</span><ins>+2016-12-18  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: Implement the WebAssembly.compile and WebAssembly.validate
+        https://bugs.webkit.org/show_bug.cgi?id=165936
+
+        Reviewed by Mark Lam.
+
+        The APIs are documented here:
+        - https://github.com/WebAssembly/design/blob/master/JS.md#webassemblycompile
+        - https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyvalidate
+
+        * wasm/JSWebAssembly.cpp:
+        (JSC::webAssemblyCompileFunc):
+        (JSC::webAssemblyValidateFunc):
+        (JSC::JSWebAssembly::finishCreation):
+        * wasm/WasmPlan.cpp:
+        (JSC::Wasm::Plan::parseAndValidateModule):
+        (JSC::Wasm::Plan::run):
+        * wasm/WasmPlan.h:
+        * wasm/js/JSWebAssemblyHelpers.h:
+        (JSC::getWasmBufferFromValue):
+        * wasm/js/WebAssemblyModuleConstructor.cpp:
+        (JSC::constructJSWebAssemblyModule):
+        (JSC::callJSWebAssemblyModule):
+        (JSC::WebAssemblyModuleConstructor::createModule):
+        * wasm/js/WebAssemblyModuleConstructor.h:
+
</ins><span class="cx"> 2016-12-18  Mark Lam  &lt;mark.lam@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Rename finallyActionRegister to completionTypeRegister and only store int JSValues in it.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmJSWebAssemblycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -28,13 +28,57 @@
</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;WasmPlan.h&quot;
+#include &quot;WebAssemblyModuleConstructor.h&quot;
</ins><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><span class="cx"> STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSWebAssembly);
</span><span class="cx"> 
</span><ins>+EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState*);
+EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState*);
+
+EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(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, { });
+
+    // FIXME: Make this truly asynchronous:
+    // https://bugs.webkit.org/show_bug.cgi?id=166016
+    JSValue module = WebAssemblyModuleConstructor::createModule(exec, exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyModuleStructure());
+    if (Exception* exception = catchScope.exception()) {
+        catchScope.clearException();
+        promise-&gt;reject(exec, exception);
+        return JSValue::encode(promise-&gt;promise());
+    }
+
+    promise-&gt;resolve(exec, module);
+    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, { });
+    Wasm::Plan plan(&amp;vm, base + byteOffset, byteSize);
+    // 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"> const ClassInfo JSWebAssembly::s_info = { &quot;WebAssembly&quot;, &amp;Base::s_info, 0, CREATE_METHOD_TABLE(JSWebAssembly) };
</span><span class="cx"> 
</span><span class="cx"> JSWebAssembly* JSWebAssembly::create(VM&amp; vm, JSGlobalObject* globalObject, Structure* structure)
</span><span class="lines">@@ -49,10 +93,12 @@
</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*)
</del><ins>+void JSWebAssembly::finishCreation(VM&amp; vm, JSGlobalObject* globalObject)
</ins><span class="cx"> {
</span><span class="cx">     Base::finishCreation(vm);
</span><span class="cx">     ASSERT(inherits(info()));
</span><ins>+    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;validate&quot;, webAssemblyValidateFunc, DontEnum, 1);
+    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;compile&quot;, webAssemblyCompileFunc, DontEnum, 1);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> JSWebAssembly::JSWebAssembly(VM&amp; vm, Structure* structure)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmPlancpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmPlan.cpp (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmPlan.cpp        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/Source/JavaScriptCore/wasm/WasmPlan.cpp        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -58,7 +58,7 @@
</span><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void Plan::run()
</del><ins>+bool Plan::parseAndValidateModule()
</ins><span class="cx"> {
</span><span class="cx">     {
</span><span class="cx">         ModuleParser moduleParser(m_vm, m_source, m_sourceLength);
</span><span class="lines">@@ -65,7 +65,7 @@
</span><span class="cx">         auto parseResult = moduleParser.parse();
</span><span class="cx">         if (!parseResult) {
</span><span class="cx">             m_errorMessage = parseResult.error();
</span><del>-            return; // FIXME return an Expected.
</del><ins>+            return false;
</ins><span class="cx">         }
</span><span class="cx">         m_moduleInformation = WTFMove(parseResult-&gt;module);
</span><span class="cx">         m_functionLocationInBinary = WTFMove(parseResult-&gt;functionLocationInBinary);
</span><span class="lines">@@ -73,6 +73,34 @@
</span><span class="cx">         m_functionIndexSpace.buffer = parseResult-&gt;functionIndexSpace.releaseBuffer();
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    for (unsigned functionIndex = 0; functionIndex &lt; m_functionLocationInBinary.size(); ++functionIndex) {
+        if (verbose)
+            dataLogLn(&quot;Processing function starting at: &quot;, m_functionLocationInBinary[functionIndex].start, &quot; and ending at: &quot;, m_functionLocationInBinary[functionIndex].end);
+        const uint8_t* functionStart = m_source + m_functionLocationInBinary[functionIndex].start;
+        size_t functionLength = m_functionLocationInBinary[functionIndex].end - m_functionLocationInBinary[functionIndex].start;
+        ASSERT(Checked&lt;uintptr_t&gt;(bitwise_cast&lt;uintptr_t&gt;(functionStart)) + functionLength &lt;= Checked&lt;uintptr_t&gt;(bitwise_cast&lt;uintptr_t&gt;(m_source)) + m_sourceLength);
+        Signature* signature = m_moduleInformation-&gt;internalFunctionSignatures[functionIndex];
+
+        auto validationResult = validateFunction(functionStart, functionLength, signature, m_functionIndexSpace, *m_moduleInformation);
+        if (!validationResult) {
+            if (verbose) {
+                for (unsigned i = 0; i &lt; functionLength; ++i)
+                    dataLog(RawPointer(reinterpret_cast&lt;void*&gt;(functionStart[i])), &quot;, &quot;);
+                dataLogLn();
+            }
+            m_errorMessage = validationResult.error(); // FIXME make this an Expected.
+            return false;
+        }
+    }
+
+    return true;
+}
+
+void Plan::run()
+{
+    if (!parseAndValidateModule())
+        return;
+
</ins><span class="cx">     auto tryReserveCapacity = [this] (auto&amp; vector, size_t size, const char* what) {
</span><span class="cx">         if (UNLIKELY(!vector.tryReserveCapacity(size))) {
</span><span class="cx">             StringBuilder builder;
</span><span class="lines">@@ -84,6 +112,7 @@
</span><span class="cx">         }
</span><span class="cx">         return true;
</span><span class="cx">     };
</span><ins>+
</ins><span class="cx">     Vector&lt;Vector&lt;UnlinkedWasmToWasmCall&gt;&gt; unlinkedWasmToWasmCalls;
</span><span class="cx">     if (!tryReserveCapacity(m_wasmToJSStubs, m_moduleInformation-&gt;importFunctions.size(), &quot; WebAssembly to JavaScript stubs&quot;)
</span><span class="cx">         || !tryReserveCapacity(unlinkedWasmToWasmCalls, m_functionLocationInBinary.size(), &quot; unlinked WebAssembly to WebAssembly calls&quot;)
</span><span class="lines">@@ -112,16 +141,7 @@
</span><span class="cx">         unsigned functionIndexSpace = m_wasmToJSStubs.size() + functionIndex;
</span><span class="cx">         ASSERT(m_functionIndexSpace.buffer.get()[functionIndexSpace].signature == signature);
</span><span class="cx"> 
</span><del>-        auto validateResult = validateFunction(functionStart, functionLength, signature, m_functionIndexSpace, *m_moduleInformation);
-        if (!validateResult) {
-            if (verbose) {
-                for (unsigned i = 0; i &lt; functionLength; ++i)
-                    dataLog(RawPointer(reinterpret_cast&lt;void*&gt;(functionStart[i])), &quot;, &quot;);
-                dataLogLn();
-            }
-            m_errorMessage = validateResult.error(); // FIXME make this an Expected.
-            return;
-        }
</del><ins>+        ASSERT(validateFunction(functionStart, functionLength, signature, m_functionIndexSpace, *m_moduleInformation));
</ins><span class="cx"> 
</span><span class="cx">         unlinkedWasmToWasmCalls.uncheckedAppend(Vector&lt;UnlinkedWasmToWasmCall&gt;());
</span><span class="cx">         auto parseAndCompileResult = parseAndCompile(*m_vm, functionStart, functionLength, signature, unlinkedWasmToWasmCalls.at(functionIndex), m_functionIndexSpace, *m_moduleInformation);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmPlanh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmPlan.h (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmPlan.h        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/Source/JavaScriptCore/wasm/WasmPlan.h        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -48,6 +48,8 @@
</span><span class="cx">     JS_EXPORT_PRIVATE Plan(VM*, const uint8_t*, size_t);
</span><span class="cx">     JS_EXPORT_PRIVATE ~Plan();
</span><span class="cx"> 
</span><ins>+    bool parseAndValidateModule();
+
</ins><span class="cx">     JS_EXPORT_PRIVATE void run();
</span><span class="cx"> 
</span><span class="cx">     JS_EXPORT_PRIVATE void initializeCallees(JSGlobalObject*, std::function&lt;void(unsigned, JSWebAssemblyCallee*, JSWebAssemblyCallee*)&gt;);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsJSWebAssemblyHelpersh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyHelpers.h (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyHelpers.h        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyHelpers.h        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(WEBASSEMBLY)
</span><span class="cx"> 
</span><ins>+#include &quot;JSArrayBuffer.h&quot;
</ins><span class="cx"> #include &quot;JSCJSValue.h&quot;
</span><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="lines">@@ -46,6 +47,30 @@
</span><span class="cx">     return static_cast&lt;uint32_t&gt;(doubleValue);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+ALWAYS_INLINE uint8_t* getWasmBufferFromValue(ExecState* exec, JSValue value, size_t&amp; byteOffset, size_t&amp; byteSize)
+{
+    VM&amp; vm = exec-&gt;vm();
+    auto throwScope = DECLARE_THROW_SCOPE(vm);
+    // If the given bytes argument is not a BufferSource, a TypeError exception is thrown.
+    JSArrayBuffer* arrayBuffer = value.getObject() ? jsDynamicCast&lt;JSArrayBuffer*&gt;(value.getObject()) : nullptr;
+    JSArrayBufferView* arrayBufferView = value.getObject() ? jsDynamicCast&lt;JSArrayBufferView*&gt;(value.getObject()) : nullptr;
+    if (!(arrayBuffer || arrayBufferView)) {
+        throwException(exec, throwScope, createTypeError(exec,
+            ASCIILiteral(&quot;first argument must be an ArrayBufferView or an ArrayBuffer&quot;), defaultSourceAppender, runtimeTypeForValue(value)));
+        return nullptr;
+    }
+
+    if (arrayBufferView ? arrayBufferView-&gt;isNeutered() : arrayBuffer-&gt;impl()-&gt;isNeutered()) {
+        throwException(exec, throwScope, createTypeError(exec,
+            ASCIILiteral(&quot;underlying TypedArray has been detatched from the ArrayBuffer&quot;), defaultSourceAppender, runtimeTypeForValue(value)));
+        return nullptr;
+    }
+
+    byteOffset = arrayBufferView ? arrayBufferView-&gt;byteOffset() : 0;
+    byteSize = arrayBufferView ? arrayBufferView-&gt;length() : arrayBuffer-&gt;impl()-&gt;byteLength();
+    return arrayBufferView ? static_cast&lt;uint8_t*&gt;(arrayBufferView-&gt;vector()) : static_cast&lt;uint8_t*&gt;(arrayBuffer-&gt;impl()-&gt;data());
+}
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(WEBASSEMBLY)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyModuleConstructorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -35,6 +35,7 @@
</span><span class="cx"> #include &quot;JSTypedArrays.h&quot;
</span><span class="cx"> #include &quot;JSWebAssemblyCallee.h&quot;
</span><span class="cx"> #include &quot;JSWebAssemblyCompileError.h&quot;
</span><ins>+#include &quot;JSWebAssemblyHelpers.h&quot;
</ins><span class="cx"> #include &quot;JSWebAssemblyModule.h&quot;
</span><span class="cx"> #include &quot;SymbolTable.h&quot;
</span><span class="cx"> #include &quot;WasmPlan.h&quot;
</span><span class="lines">@@ -52,34 +53,40 @@
</span><span class="cx">  @end
</span><span class="cx">  */
</span><span class="cx"> 
</span><del>-static EncodedJSValue JSC_HOST_CALL constructJSWebAssemblyModule(ExecState* state)
</del><ins>+static EncodedJSValue JSC_HOST_CALL constructJSWebAssemblyModule(ExecState* exec)
</ins><span class="cx"> {
</span><ins>+    VM&amp; vm = exec-&gt;vm();
+    auto throwScope = DECLARE_THROW_SCOPE(vm);
+    auto* structure = InternalFunction::createSubclassStructure(exec, exec-&gt;newTarget(), exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyModuleStructure());
+    RETURN_IF_EXCEPTION(throwScope, { });
+    throwScope.release();
+    return JSValue::encode(WebAssemblyModuleConstructor::createModule(exec, structure));
+}
+
+static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyModule(ExecState* state)
+{
</ins><span class="cx">     VM&amp; vm = state-&gt;vm();
</span><span class="cx">     auto scope = DECLARE_THROW_SCOPE(vm);
</span><del>-    JSValue val = state-&gt;argument(0);
</del><ins>+    return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(state, scope, &quot;WebAssembly.Module&quot;));
+}
</ins><span class="cx"> 
</span><del>-    // If the given bytes argument is not a BufferSource, a TypeError exception is thrown.
-    JSArrayBuffer* arrayBuffer = val.getObject() ? jsDynamicCast&lt;JSArrayBuffer*&gt;(val.getObject()) : nullptr;
-    JSArrayBufferView* arrayBufferView = val.getObject() ? jsDynamicCast&lt;JSArrayBufferView*&gt;(val.getObject()) : nullptr;
-    if (!(arrayBuffer || arrayBufferView))
-        return JSValue::encode(throwException(state, scope, createTypeError(state, ASCIILiteral(&quot;first argument to WebAssembly.Module must be an ArrayBufferView or an ArrayBuffer&quot;), defaultSourceAppender, runtimeTypeForValue(val))));
</del><ins>+JSValue WebAssemblyModuleConstructor::createModule(ExecState* state, Structure* structure)
+{
+    VM&amp; vm = state-&gt;vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
</ins><span class="cx"> 
</span><del>-    if (arrayBufferView ? arrayBufferView-&gt;isNeutered() : arrayBuffer-&gt;impl()-&gt;isNeutered())
-        return JSValue::encode(throwException(state, scope, createTypeError(state, ASCIILiteral(&quot;underlying TypedArray has been detatched from the ArrayBuffer&quot;), defaultSourceAppender, runtimeTypeForValue(val))));
</del><ins>+    size_t byteOffset;
+    size_t byteSize;
+    uint8_t* base = getWasmBufferFromValue(state, state-&gt;argument(0), byteOffset, byteSize);
+    RETURN_IF_EXCEPTION(scope, { });
</ins><span class="cx"> 
</span><del>-    size_t byteOffset = arrayBufferView ? arrayBufferView-&gt;byteOffset() : 0;
-    size_t byteSize = arrayBufferView ? arrayBufferView-&gt;length() : arrayBuffer-&gt;impl()-&gt;byteLength();
-    const auto* base = arrayBufferView ? static_cast&lt;uint8_t*&gt;(arrayBufferView-&gt;vector()) : static_cast&lt;uint8_t*&gt;(arrayBuffer-&gt;impl()-&gt;data());
-
</del><span class="cx">     Wasm::Plan plan(&amp;vm, base + byteOffset, byteSize);
</span><span class="cx">     // On failure, a new WebAssembly.CompileError is thrown.
</span><span class="cx">     plan.run();
</span><span class="cx">     if (plan.failed())
</span><del>-        return JSValue::encode(throwException(state, scope, createWebAssemblyCompileError(state, plan.errorMessage())));
</del><ins>+        return throwException(state, scope, createWebAssemblyCompileError(state, plan.errorMessage()));
</ins><span class="cx"> 
</span><span class="cx">     // On success, a new WebAssembly.Module object is returned with [[Module]] set to the validated Ast.module.
</span><del>-    auto* structure = InternalFunction::createSubclassStructure(state, state-&gt;newTarget(), asInternalFunction(state-&gt;jsCallee())-&gt;globalObject()-&gt;WebAssemblyModuleStructure());
-    RETURN_IF_EXCEPTION(scope, { });
</del><span class="cx"> 
</span><span class="cx">     // The export symbol table is the same for all Instances of a Module.
</span><span class="cx">     SymbolTable* exportSymbolTable = SymbolTable::create(vm);
</span><span class="lines">@@ -96,14 +103,8 @@
</span><span class="cx">             result-&gt;setJSEntrypointCallee(vm, calleeIndex, jsEntrypointCallee);
</span><span class="cx">             result-&gt;setWasmEntrypointCallee(vm, calleeIndex, wasmEntrypointCallee);
</span><span class="cx">         });
</span><del>-    return JSValue::encode(result);
-}
</del><span class="cx"> 
</span><del>-static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyModule(ExecState* state)
-{
-    VM&amp; vm = state-&gt;vm();
-    auto scope = DECLARE_THROW_SCOPE(vm);
-    return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(state, scope, &quot;WebAssembly.Module&quot;));
</del><ins>+    return result;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> WebAssemblyModuleConstructor* WebAssemblyModuleConstructor::create(VM&amp; vm, Structure* structure, WebAssemblyModulePrototype* thisPrototype)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyModuleConstructorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.h (209978 => 209979)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.h        2016-12-19 06:29:17 UTC (rev 209978)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.h        2016-12-19 07:22:42 UTC (rev 209979)
</span><span class="lines">@@ -44,6 +44,8 @@
</span><span class="cx"> 
</span><span class="cx">     DECLARE_INFO;
</span><span class="cx"> 
</span><ins>+    static JSValue createModule(ExecState*, Structure*);
+
</ins><span class="cx"> protected:
</span><span class="cx">     void finishCreation(VM&amp;, WebAssemblyModulePrototype*);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>