<!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>[213506] 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/213506">213506</a></dd>
<dt>Author</dt> <dd>sbarati@apple.com</dd>
<dt>Date</dt> <dd>2017-03-06 23:38:41 -0800 (Mon, 06 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>WebAssembly: Implement the WebAssembly.instantiate API
https://bugs.webkit.org/show_bug.cgi?id=165982
&lt;rdar://problem/29760110&gt;

Reviewed by Keith Miller.

JSTests:

* wasm/js-api/web-assembly-instantiate.js: Added.
(assert.eq.async.test):
(assert.eq):
(assert.truthy.async.test):
(assert.truthy):

Source/JavaScriptCore:

This patch is a straight forward implementation of the WebAssembly.instantiate
API: https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyinstantiate
        
I implemented the API in a synchronous manner. We should make it
asynchronous: https://bugs.webkit.org/show_bug.cgi?id=169187

* wasm/JSWebAssembly.cpp:
(JSC::webAssemblyCompileFunc):
(JSC::webAssemblyInstantiateFunc):
(JSC::JSWebAssembly::finishCreation):
* wasm/js/WebAssemblyInstanceConstructor.cpp:
(JSC::constructJSWebAssemblyInstance):
(JSC::WebAssemblyInstanceConstructor::createInstance):
* wasm/js/WebAssemblyInstanceConstructor.h:
* wasm/js/WebAssemblyModuleConstructor.cpp:
(JSC::constructJSWebAssemblyModule):
(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="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmJSWebAssemblycpp">trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyInstanceConstructorcpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyInstanceConstructorh">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.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="#trunkJSTestswasmjsapiwebassemblyinstantiatejs">trunk/JSTests/wasm/js-api/web-assembly-instantiate.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (213505 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog        2017-03-07 07:30:27 UTC (rev 213505)
+++ trunk/JSTests/ChangeLog        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -1,5 +1,19 @@
</span><span class="cx"> 2017-03-06  Saam Barati  &lt;sbarati@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        WebAssembly: Implement the WebAssembly.instantiate API
+        https://bugs.webkit.org/show_bug.cgi?id=165982
+        &lt;rdar://problem/29760110&gt;
+
+        Reviewed by Keith Miller.
+
+        * wasm/js-api/web-assembly-instantiate.js: Added.
+        (assert.eq.async.test):
+        (assert.eq):
+        (assert.truthy.async.test):
+        (assert.truthy):
+
+2017-03-06  Saam Barati  &lt;sbarati@apple.com&gt;
+
</ins><span class="cx">         Unreviewed. Fix test expected error message.
</span><span class="cx"> 
</span><span class="cx">         * wasm/js-api/element.js:
</span></span></pre></div>
<a id="trunkJSTestswasmjsapiwebassemblyinstantiatejs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/js-api/web-assembly-instantiate.js (0 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/js-api/web-assembly-instantiate.js                                (rev 0)
+++ trunk/JSTests/wasm/js-api/web-assembly-instantiate.js        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -0,0 +1,219 @@
</span><ins>+import * as assert from '../assert.js';
+import Builder from '../Builder.js';
+
+assert.truthy(WebAssembly.instantiate instanceof Function);
+assert.eq(WebAssembly.instantiate.length, 1);
+
+{
+    const builder = (new Builder())
+          .Type().End()
+          .Function().End()
+          .Export()
+              .Function(&quot;foo&quot;)
+          .End()
+          .Code()
+              .Function(&quot;foo&quot;, { params: [], ret: &quot;i32&quot; })
+                  .I32Const(1)
+              .End()
+          .End();
+
+    const bin = builder.WebAssembly().get();
+
+    let done = false;
+    async function test() {
+        let {module, instance} = await WebAssembly.instantiate(bin);
+        assert.truthy(module instanceof WebAssembly.Module);
+        assert.truthy(instance instanceof WebAssembly.Instance);
+        assert.eq(instance.exports.foo(20), 1);
+        done = true;
+    }
+
+    test();
+    drainMicrotasks();
+    assert.truthy(done);
+}
+
+{
+    const builder = (new Builder())
+          .Type().End()
+          .Function().End()
+          .Export()
+              .Function(&quot;foo&quot;)
+          .End()
+          .Code()
+              .Function(&quot;foo&quot;, { params: [], ret: &quot;i32&quot; })
+                  .I32Const(1)
+              .End()
+          .End();
+
+    const bin = builder.WebAssembly().get();
+
+    let done = false;
+    async function test() {
+        try {
+            let {module, instance} = await WebAssembly.instantiate(bin, null);
+        } catch(e) {
+            assert.eq(e.message, &quot;second argument to WebAssembly.instantiate must be undefined or an Object (evaluating 'WebAssembly.instantiate(bin, null)')&quot;);
+        }
+        done = true;
+    }
+
+    test();
+    drainMicrotasks();
+    assert.truthy(done);
+}
+
+{
+    const builder = (new Builder())
+          .Type().End()
+          .Function().End()
+          .Export()
+              .Function(&quot;foo&quot;)
+          .End()
+          .Code()
+              .Function(&quot;foo&quot;, { params: [], ret: &quot;i32&quot; })
+                  .F32Const(1)
+              .End()
+          .End();
+
+    const bin = builder.WebAssembly().get();
+
+    let done = false;
+    async function test() {
+        try {
+            let {module, instance} = await WebAssembly.instantiate(bin);
+        } catch(e) {
+            assert.truthy(e instanceof WebAssembly.CompileError);
+            assert.eq(e.message, &quot;WebAssembly.Module doesn't validate: control flow returns with unexpected type, in function at index 0 (evaluating 'WebAssembly.instantiate(bin)')&quot;);
+        }
+        done = true;
+    }
+
+    test();
+    drainMicrotasks();
+    assert.truthy(done);
+}
+
+{
+    const builder = (new Builder())
+          .Type().End()
+          .Import().Memory(&quot;imp&quot;, &quot;memory&quot;, {initial:100}).End()
+          .Function().End()
+          .Export()
+              .Function(&quot;foo&quot;)
+          .End()
+          .Code()
+              .Function(&quot;foo&quot;, { params: [], ret: &quot;i32&quot; })
+                  .I32Const(1)
+              .End()
+          .End();
+
+    const bin = builder.WebAssembly().get();
+
+    let done = false;
+    async function test() {
+        try {
+            let {module, instance} = await WebAssembly.instantiate(bin, {imp: {memory: 20}});
+        } catch(e) {
+            assert.eq(e.message, &quot;Memory import is not an instance of WebAssembly.Memory (evaluating 'WebAssembly.instantiate(bin, {imp: {memory: 20}})')&quot;);
+        }
+        done = true;
+    }
+
+    test();
+    drainMicrotasks();
+    assert.truthy(done);
+}
+
+{
+    const builder = (new Builder())
+          .Type().End()
+          .Import().Memory(&quot;imp&quot;, &quot;memory&quot;, {initial:100}).End()
+          .Function().End()
+          .Export()
+              .Function(&quot;foo&quot;)
+          .End()
+          .Code()
+              .Function(&quot;foo&quot;, { params: [], ret: &quot;i32&quot; })
+                  .I32Const(1)
+              .End()
+          .End();
+
+    const bin = builder.WebAssembly().get();
+
+    let done = false;
+    async function test() {
+        try {
+            const module = new WebAssembly.Module(bin);
+            let instance = await WebAssembly.instantiate(bin, {imp: {memory: 20}});
+        } catch(e) {
+            assert.eq(e.message, &quot;Memory import is not an instance of WebAssembly.Memory (evaluating 'WebAssembly.instantiate(bin, {imp: {memory: 20}})')&quot;);
+        }
+        done = true;
+    }
+
+    test();
+    drainMicrotasks();
+    assert.truthy(done);
+}
+
+{
+    const builder = (new Builder())
+          .Type().End()
+          .Function().End()
+          .Export()
+              .Function(&quot;foo&quot;)
+          .End()
+          .Code()
+              .Function(&quot;foo&quot;, { params: [], ret: &quot;i32&quot; })
+                  .I32Const(1)
+              .End()
+          .End();
+
+    const bin = builder.WebAssembly().get();
+
+    let done = false;
+    async function test() {
+        let module = new WebAssembly.Module(bin);
+        let instance = await WebAssembly.instantiate(module);
+        assert.truthy(instance instanceof WebAssembly.Instance);
+        assert.eq(instance.exports.foo(20), 1);
+        done = true;
+    }
+
+    test();
+    drainMicrotasks();
+    assert.truthy(done);
+}
+
+{
+    const builder = (new Builder())
+          .Type().End()
+          .Import().Memory(&quot;imp&quot;, &quot;memory&quot;, {initial:100}).End()
+          .Function().End()
+          .Export()
+              .Function(&quot;foo&quot;)
+          .End()
+          .Code()
+              .Function(&quot;foo&quot;, { params: [], ret: &quot;i32&quot; })
+                  .I32Const(1)
+              .End()
+          .End();
+
+    const bin = builder.WebAssembly().get();
+
+    let done = false;
+    async function test() {
+        try {
+            await WebAssembly.instantiate(25);
+        } catch(e) {
+            // FIXME: Better error message here.
+            assert.eq(e.message, &quot;first argument must be an ArrayBufferView or an ArrayBuffer (evaluating 'WebAssembly.instantiate(25)')&quot;);
+        }
+        done = true;
+    }
+
+    test();
+    drainMicrotasks();
+    assert.truthy(done);
+}
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (213505 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-03-07 07:30:27 UTC (rev 213505)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -1,3 +1,30 @@
</span><ins>+2017-03-06  Saam Barati  &lt;sbarati@apple.com&gt;
+
+        WebAssembly: Implement the WebAssembly.instantiate API
+        https://bugs.webkit.org/show_bug.cgi?id=165982
+        &lt;rdar://problem/29760110&gt;
+
+        Reviewed by Keith Miller.
+
+        This patch is a straight forward implementation of the WebAssembly.instantiate
+        API: https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyinstantiate
+        
+        I implemented the API in a synchronous manner. We should make it
+        asynchronous: https://bugs.webkit.org/show_bug.cgi?id=169187
+
+        * wasm/JSWebAssembly.cpp:
+        (JSC::webAssemblyCompileFunc):
+        (JSC::webAssemblyInstantiateFunc):
+        (JSC::JSWebAssembly::finishCreation):
+        * wasm/js/WebAssemblyInstanceConstructor.cpp:
+        (JSC::constructJSWebAssemblyInstance):
+        (JSC::WebAssemblyInstanceConstructor::createInstance):
+        * wasm/js/WebAssemblyInstanceConstructor.h:
+        * wasm/js/WebAssemblyModuleConstructor.cpp:
+        (JSC::constructJSWebAssemblyModule):
+        (JSC::WebAssemblyModuleConstructor::createModule):
+        * wasm/js/WebAssemblyModuleConstructor.h:
+
</ins><span class="cx"> 2017-03-06  Michael Saboff  &lt;msaboff@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Take advantage of fast permissions switching of JIT memory for devices that support it
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmJSWebAssemblycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp (213505 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp        2017-03-07 07:30:27 UTC (rev 213505)
+++ trunk/Source/JavaScriptCore/wasm/JSWebAssembly.cpp        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -33,6 +33,7 @@
</span><span class="cx"> #include &quot;JSCInlines.h&quot;
</span><span class="cx"> #include &quot;JSPromiseDeferred.h&quot;
</span><span class="cx"> #include &quot;JSWebAssemblyHelpers.h&quot;
</span><ins>+#include &quot;ObjectConstructor.h&quot;
</ins><span class="cx"> #include &quot;WasmPlan.h&quot;
</span><span class="cx"> #include &quot;WebAssemblyModuleConstructor.h&quot;
</span><span class="cx"> 
</span><span class="lines">@@ -42,6 +43,7 @@
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState*);
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState*);
</span><ins>+EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState*);
</ins><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL webAssemblyCompileFunc(ExecState* exec)
</span><span class="cx"> {
</span><span class="lines">@@ -53,7 +55,7 @@
</span><span class="cx"> 
</span><span class="cx">     // FIXME: Make this truly asynchronous:
</span><span class="cx">     // https://bugs.webkit.org/show_bug.cgi?id=166016
</span><del>-    JSValue module = WebAssemblyModuleConstructor::createModule(exec, exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyModuleStructure());
</del><ins>+    JSValue module = WebAssemblyModuleConstructor::createModule(exec, exec-&gt;argument(0), exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyModuleStructure());
</ins><span class="cx">     if (Exception* exception = catchScope.exception()) {
</span><span class="cx">         catchScope.clearException();
</span><span class="cx">         promise-&gt;reject(exec, exception-&gt;value());
</span><span class="lines">@@ -64,6 +66,60 @@
</span><span class="cx">     return JSValue::encode(promise-&gt;promise());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState* exec)
+{
+    VM&amp; vm = exec-&gt;vm();
+    auto catchScope = DECLARE_CATCH_SCOPE(vm);
+
+    // FIXME: Make this API truly asynchronous: https://bugs.webkit.org/show_bug.cgi?id=169187
+
+    JSPromiseDeferred* promise = JSPromiseDeferred::create(exec, exec-&gt;lexicalGlobalObject());
+    RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
+
+    auto reject = [&amp;] () {
+        Exception* exception = catchScope.exception();
+        ASSERT(exception);
+        catchScope.clearException();
+        promise-&gt;reject(exec, exception-&gt;value());
+        return JSValue::encode(promise-&gt;promise());
+    };
+
+    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);
+    JSValue module;
+    bool firstArgumentIsModule = false;
+    if (firstArgument.inherits(vm, JSWebAssemblyModule::info())) {
+        firstArgumentIsModule = true;
+        module = firstArgument;
+    } else {
+        module = WebAssemblyModuleConstructor::createModule(exec, firstArgument, exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyModuleStructure());
+        if (catchScope.exception())
+            return reject();
+    }
+
+    JSWebAssemblyInstance* instance = WebAssemblyInstanceConstructor::createInstance(exec, jsCast&lt;JSWebAssemblyModule*&gt;(module), importObject, exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyInstanceStructure());
+    if (catchScope.exception())
+        return reject();
+
+    if (firstArgumentIsModule)
+        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);
+    }
+
+    return JSValue::encode(promise-&gt;promise());
+}
+
</ins><span class="cx"> EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx">     VM&amp; vm = exec-&gt;vm();
</span><span class="lines">@@ -99,6 +155,7 @@
</span><span class="cx">     ASSERT(inherits(vm, info()));
</span><span class="cx">     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;validate&quot;, webAssemblyValidateFunc, DontEnum, 1);
</span><span class="cx">     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;compile&quot;, webAssemblyCompileFunc, DontEnum, 1);
</span><ins>+    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(&quot;instantiate&quot;, webAssemblyInstantiateFunc, 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="trunkSourceJavaScriptCorewasmjsWebAssemblyInstanceConstructorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp (213505 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp        2017-03-07 07:30:27 UTC (rev 213505)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -57,13 +57,11 @@
</span><span class="cx"> {
</span><span class="cx">     auto&amp; vm = exec-&gt;vm();
</span><span class="cx">     auto throwScope = DECLARE_THROW_SCOPE(vm);
</span><del>-    auto* globalObject = exec-&gt;lexicalGlobalObject();
</del><span class="cx"> 
</span><span class="cx">     // If moduleObject is not a WebAssembly.Module instance, a TypeError is thrown.
</span><span class="cx">     JSWebAssemblyModule* jsModule = jsDynamicCast&lt;JSWebAssemblyModule*&gt;(vm, exec-&gt;argument(0));
</span><span class="cx">     if (!jsModule)
</span><span class="cx">         return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral(&quot;first argument to WebAssembly.Instance must be a WebAssembly.Module&quot;), defaultSourceAppender, runtimeTypeForValue(exec-&gt;argument(0)))));
</span><del>-    const Wasm::ModuleInformation&amp; moduleInformation = jsModule-&gt;moduleInformation();
</del><span class="cx"> 
</span><span class="cx">     // If the importObject parameter is not undefined and Type(importObject) is not Object, a TypeError is thrown.
</span><span class="cx">     JSValue importArgument = exec-&gt;argument(1);
</span><span class="lines">@@ -70,20 +68,38 @@
</span><span class="cx">     JSObject* importObject = importArgument.getObject();
</span><span class="cx">     if (!importArgument.isUndefined() &amp;&amp; !importObject)
</span><span class="cx">         return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral(&quot;second argument to WebAssembly.Instance must be undefined or an Object&quot;), defaultSourceAppender, runtimeTypeForValue(importArgument))));
</span><ins>+    
+    Structure* instanceStructure = InternalFunction::createSubclassStructure(exec, exec-&gt;newTarget(), exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyInstanceStructure());
+    RETURN_IF_EXCEPTION(throwScope, { });
</ins><span class="cx"> 
</span><ins>+    throwScope.release();
+    return JSValue::encode(WebAssemblyInstanceConstructor::createInstance(exec, jsModule, importObject, instanceStructure));
+}
+
+JSWebAssemblyInstance* WebAssemblyInstanceConstructor::createInstance(ExecState* exec, JSWebAssemblyModule* jsModule, JSObject* importObject, Structure* instanceStructure)
+{
+    auto&amp; vm = exec-&gt;vm();
+    auto throwScope = DECLARE_THROW_SCOPE(vm);
+    auto* globalObject = exec-&gt;lexicalGlobalObject();
+
+    const Wasm::ModuleInformation&amp; moduleInformation = jsModule-&gt;moduleInformation();
+
+    auto exception = [&amp;] (JSObject* error) {
+        throwException(exec, throwScope, error);
+        return nullptr;
+    };
+
</ins><span class="cx">     // If the list of module.imports is not empty and Type(importObject) is not Object, a TypeError is thrown.
</span><span class="cx">     if (moduleInformation.imports.size() &amp;&amp; !importObject)
</span><del>-        return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral(&quot;second argument to WebAssembly.Instance must be Object because the WebAssembly.Module has imports&quot;), defaultSourceAppender, runtimeTypeForValue(importArgument))));
</del><ins>+        return exception(createTypeError(exec, ASCIILiteral(&quot;can't make WebAssembly.Instance because there is no imports Object and the WebAssembly.Module requires imports&quot;)));
</ins><span class="cx"> 
</span><span class="cx">     Identifier moduleKey = Identifier::fromUid(PrivateName(PrivateName::Description, &quot;WebAssemblyInstance&quot;));
</span><span class="cx">     WebAssemblyModuleRecord* moduleRecord = WebAssemblyModuleRecord::create(exec, vm, globalObject-&gt;webAssemblyModuleRecordStructure(), moduleKey, moduleInformation);
</span><del>-    RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+    RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx"> 
</span><del>-    Structure* instanceStructure = InternalFunction::createSubclassStructure(exec, exec-&gt;newTarget(), globalObject-&gt;WebAssemblyInstanceStructure());
-    RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><span class="cx"> 
</span><span class="cx">     JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, instanceStructure, jsModule, moduleRecord-&gt;getModuleNamespace(exec));
</span><del>-    RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+    RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     // Let funcs, memories and tables be initially-empty lists of callable JavaScript objects, WebAssembly.Memory objects and WebAssembly.Table objects, respectively.
</span><span class="lines">@@ -97,15 +113,15 @@
</span><span class="cx">     for (auto&amp; import : moduleInformation.imports) {
</span><span class="cx">         // 1. Let o be the resultant value of performing Get(importObject, i.module_name).
</span><span class="cx">         JSValue importModuleValue = importObject-&gt;get(exec, import.module);
</span><del>-        RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+        RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx">         // 2. If Type(o) is not Object, throw a TypeError.
</span><span class="cx">         if (!importModuleValue.isObject())
</span><del>-            return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral(&quot;import must be an object&quot;), defaultSourceAppender, runtimeTypeForValue(importModuleValue))));
</del><ins>+            return exception(createTypeError(exec, ASCIILiteral(&quot;import must be an object&quot;), defaultSourceAppender, runtimeTypeForValue(importModuleValue)));
</ins><span class="cx"> 
</span><span class="cx">         // 3. Let v be the value of performing Get(o, i.item_name)
</span><span class="cx">         JSObject* object = jsCast&lt;JSObject*&gt;(importModuleValue);
</span><span class="cx">         JSValue value = object-&gt;get(exec, import.field);
</span><del>-        RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+        RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx"> 
</span><span class="cx">         switch (import.kind) {
</span><span class="cx">         case Wasm::ExternalKind::Function: {
</span><span class="lines">@@ -112,7 +128,8 @@
</span><span class="cx">             // 4. If i is a function import:
</span><span class="cx">             // i. If IsCallable(v) is false, throw a WebAssembly.LinkError.
</span><span class="cx">             if (!value.isFunction())
</span><del>-                return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;import function must be callable&quot;))));
</del><ins>+                return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;import function must be callable&quot;)));
+
</ins><span class="cx">             JSCell* cell = value.asCell();
</span><span class="cx">             // ii. If v is an Exported Function Exotic Object:
</span><span class="cx">             if (WebAssemblyFunction* importedExport = jsDynamicCast&lt;WebAssemblyFunction*&gt;(vm, cell)) {
</span><span class="lines">@@ -120,7 +137,7 @@
</span><span class="cx">                 Wasm::SignatureIndex importedSignatureIndex = importedExport-&gt;signatureIndex();
</span><span class="cx">                 Wasm::SignatureIndex expectedSignatureIndex = moduleInformation.importFunctionSignatureIndices[import.kindIndex];
</span><span class="cx">                 if (importedSignatureIndex != expectedSignatureIndex)
</span><del>-                    return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;imported function's signature doesn't match the provided WebAssembly function's signature&quot;))));
</del><ins>+                    return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;imported function's signature doesn't match the provided WebAssembly function's signature&quot;)));
</ins><span class="cx">                 // b. Let closure be v.[[Closure]].
</span><span class="cx">             }
</span><span class="cx">             // iii. Otherwise:
</span><span class="lines">@@ -139,23 +156,19 @@
</span><span class="cx">             JSWebAssemblyTable* table = jsDynamicCast&lt;JSWebAssemblyTable*&gt;(vm, value);
</span><span class="cx">             // i. If v is not a WebAssembly.Table object, throw a WebAssembly.LinkError.
</span><span class="cx">             if (!table)
</span><del>-                return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Table import is not an instance of WebAssembly.Table&quot;))));
</del><ins>+                return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Table import is not an instance of WebAssembly.Table&quot;)));
</ins><span class="cx"> 
</span><span class="cx">             uint32_t expectedInitial = moduleInformation.tableInformation.initial();
</span><span class="cx">             uint32_t actualInitial = table-&gt;size();
</span><span class="cx">             if (actualInitial &lt; expectedInitial)
</span><del>-                return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Table import provided an 'initial' that is too small&quot;))));
</del><ins>+                return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Table import provided an 'initial' that is too small&quot;)));
</ins><span class="cx"> 
</span><span class="cx">             if (std::optional&lt;uint32_t&gt; expectedMaximum = moduleInformation.tableInformation.maximum()) {
</span><span class="cx">                 std::optional&lt;uint32_t&gt; actualMaximum = table-&gt;maximum();
</span><del>-                if (!actualMaximum) {
-                    return JSValue::encode(
-                        throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Table import does not have a 'maximum' but the module requires that it does&quot;))));
-                }
-                if (*actualMaximum &gt; *expectedMaximum) {
-                    return JSValue::encode(
-                        throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Imported Table's 'maximum' is larger than the module's expected 'maximum'&quot;))));
-                }
</del><ins>+                if (!actualMaximum)
+                    return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Table import does not have a 'maximum' but the module requires that it does&quot;)));
+                if (*actualMaximum &gt; *expectedMaximum)
+                    return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Imported Table's 'maximum' is larger than the module's expected 'maximum'&quot;)));
</ins><span class="cx">             }
</span><span class="cx"> 
</span><span class="cx">             // ii. Append v to tables.
</span><span class="lines">@@ -171,30 +184,26 @@
</span><span class="cx">             JSWebAssemblyMemory* memory = jsDynamicCast&lt;JSWebAssemblyMemory*&gt;(vm, value);
</span><span class="cx">             // i. If v is not a WebAssembly.Memory object, throw a WebAssembly.LinkError.
</span><span class="cx">             if (!memory)
</span><del>-                return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory import is not an instance of WebAssembly.Memory&quot;))));
</del><ins>+                return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory import is not an instance of WebAssembly.Memory&quot;)));
</ins><span class="cx"> 
</span><span class="cx">             Wasm::PageCount expectedInitial = moduleInformation.memory.initial();
</span><span class="cx">             Wasm::PageCount actualInitial = memory-&gt;memory().initial();
</span><span class="cx">             if (actualInitial &lt; expectedInitial)
</span><del>-                return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory import provided an 'initial' that is too small&quot;))));
</del><ins>+                return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory import provided an 'initial' that is too small&quot;)));
</ins><span class="cx"> 
</span><span class="cx">             if (Wasm::PageCount expectedMaximum = moduleInformation.memory.maximum()) {
</span><span class="cx">                 Wasm::PageCount actualMaximum = memory-&gt;memory().maximum();
</span><del>-                if (!actualMaximum) {
-                    return JSValue::encode(
-                        throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory import did not have a 'maximum' but the module requires that it does&quot;))));
-                }
</del><ins>+                if (!actualMaximum)
+                    return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory import did not have a 'maximum' but the module requires that it does&quot;)));
</ins><span class="cx"> 
</span><del>-                if (actualMaximum &gt; expectedMaximum) {
-                    return JSValue::encode(
-                        throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory imports 'maximum' is larger than the module's expected 'maximum'&quot;))));
-                }
</del><ins>+                if (actualMaximum &gt; expectedMaximum)
+                    return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;Memory imports 'maximum' is larger than the module's expected 'maximum'&quot;)));
</ins><span class="cx">             }
</span><span class="cx"> 
</span><span class="cx">             // ii. Append v to memories.
</span><span class="cx">             // iii. Append v.[[Memory]] to imports.
</span><span class="cx">             instance-&gt;setMemory(vm, exec, memory);
</span><del>-            RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+            RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx">             break;
</span><span class="cx">         }
</span><span class="cx">         case Wasm::ExternalKind::Global: {
</span><span class="lines">@@ -203,9 +212,9 @@
</span><span class="cx">             ASSERT(moduleInformation.globals[import.kindIndex].mutability == Wasm::Global::Immutable);
</span><span class="cx">             // ii. If the global_type of i is i64 or Type(v) is not Number, throw a WebAssembly.LinkError.
</span><span class="cx">             if (moduleInformation.globals[import.kindIndex].type == Wasm::I64)
</span><del>-                return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;imported global cannot be an i64&quot;))));
</del><ins>+                return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;imported global cannot be an i64&quot;)));
</ins><span class="cx">             if (!value.isNumber())
</span><del>-                return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;imported global must be a number&quot;))));
</del><ins>+                return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral(&quot;imported global must be a number&quot;)));
</ins><span class="cx">             // iii. Append ToWebAssemblyValue(v) to imports.
</span><span class="cx">             switch (moduleInformation.globals[import.kindIndex].type) {
</span><span class="cx">             case Wasm::I32:
</span><span class="lines">@@ -241,11 +250,11 @@
</span><span class="cx">             else {
</span><span class="cx">                 memory = Wasm::Memory::create(vm, moduleInformation.memory.initial(), moduleInformation.memory.maximum());
</span><span class="cx">                 if (!memory)
</span><del>-                    return JSValue::encode(throwException(exec, throwScope, createOutOfMemoryError(exec)));
</del><ins>+                    return exception(createOutOfMemoryError(exec));
</ins><span class="cx">             }
</span><span class="cx">             instance-&gt;setMemory(vm, exec,
</span><span class="cx">                 JSWebAssemblyMemory::create(vm, exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyMemoryStructure(), memory.releaseNonNull()));
</span><del>-            RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+            RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -288,15 +297,15 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     moduleRecord-&gt;link(exec, instance);
</span><del>-    RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+    RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx"> 
</span><span class="cx">     if (verbose)
</span><span class="cx">         moduleRecord-&gt;dump();
</span><span class="cx">     JSValue startResult = moduleRecord-&gt;evaluate(exec);
</span><span class="cx">     UNUSED_PARAM(startResult);
</span><del>-    RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><ins>+    RETURN_IF_EXCEPTION(throwScope, nullptr);
</ins><span class="cx"> 
</span><del>-    return JSValue::encode(instance);
</del><ins>+    return instance;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyInstance(ExecState* state)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyInstanceConstructorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.h (213505 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.h        2017-03-07 07:30:27 UTC (rev 213505)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.h        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -32,6 +32,7 @@
</span><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><ins>+class JSWebAssemblyModule;
</ins><span class="cx"> class WebAssemblyInstancePrototype;
</span><span class="cx"> 
</span><span class="cx"> class WebAssemblyInstanceConstructor : public InternalFunction {
</span><span class="lines">@@ -44,6 +45,8 @@
</span><span class="cx"> 
</span><span class="cx">     DECLARE_INFO;
</span><span class="cx"> 
</span><ins>+    static JSWebAssemblyInstance* createInstance(ExecState*, JSWebAssemblyModule*, JSObject* importObject, Structure*);
+
</ins><span class="cx"> protected:
</span><span class="cx">     void finishCreation(VM&amp;, WebAssemblyInstancePrototype*);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyModuleConstructorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp (213505 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp        2017-03-07 07:30:27 UTC (rev 213505)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -60,7 +60,7 @@
</span><span class="cx">     auto* structure = InternalFunction::createSubclassStructure(exec, exec-&gt;newTarget(), exec-&gt;lexicalGlobalObject()-&gt;WebAssemblyModuleStructure());
</span><span class="cx">     RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</span><span class="cx">     throwScope.release();
</span><del>-    return JSValue::encode(WebAssemblyModuleConstructor::createModule(exec, structure));
</del><ins>+    return JSValue::encode(WebAssemblyModuleConstructor::createModule(exec, exec-&gt;argument(0), structure));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyModule(ExecState* state)
</span><span class="lines">@@ -70,7 +70,7 @@
</span><span class="cx">     return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(state, scope, &quot;WebAssembly.Module&quot;));
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-JSValue WebAssemblyModuleConstructor::createModule(ExecState* state, Structure* structure)
</del><ins>+JSValue WebAssemblyModuleConstructor::createModule(ExecState* state, JSValue buffer, Structure* structure)
</ins><span class="cx"> {
</span><span class="cx">     VM&amp; vm = state-&gt;vm();
</span><span class="cx">     auto scope = DECLARE_THROW_SCOPE(vm);
</span><span class="lines">@@ -77,7 +77,7 @@
</span><span class="cx"> 
</span><span class="cx">     size_t byteOffset;
</span><span class="cx">     size_t byteSize;
</span><del>-    uint8_t* base = getWasmBufferFromValue(state, state-&gt;argument(0), byteOffset, byteSize);
</del><ins>+    uint8_t* base = getWasmBufferFromValue(state, buffer, byteOffset, byteSize);
</ins><span class="cx">     RETURN_IF_EXCEPTION(scope, { });
</span><span class="cx"> 
</span><span class="cx">     scope.release();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyModuleConstructorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.h (213505 => 213506)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.h        2017-03-07 07:30:27 UTC (rev 213505)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.h        2017-03-07 07:38:41 UTC (rev 213506)
</span><span class="lines">@@ -44,7 +44,7 @@
</span><span class="cx"> 
</span><span class="cx">     DECLARE_INFO;
</span><span class="cx"> 
</span><del>-    static JSValue createModule(ExecState*, Structure*);
</del><ins>+    static JSValue createModule(ExecState*, JSValue buffer, Structure*);
</ins><span class="cx"> 
</span><span class="cx"> protected:
</span><span class="cx">     void finishCreation(VM&amp;, WebAssemblyModulePrototype*);
</span></span></pre>
</div>
</div>

</body>
</html>