<!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>[207929] 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/207929">207929</a></dd>
<dt>Author</dt> <dd>jfbastien@apple.com</dd>
<dt>Date</dt> <dd>2016-10-26 19:18:51 -0700 (Wed, 26 Oct 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>WebAssembly API: implement Instance

As described in: https://github.com/WebAssembly/design/blob/master/JS.md#webassemblyinstance-objects

 - Take ownership of Wasm::Plan's compilation result when successfully creating a JSWebAssemblyModule object.
 - Construct a basic Instance with a Module.
 - Handle second argument (importObject) of WebAssembly.Instance.
 - Add reference text from the spec to WebAssembly.Module's code.
 - Expose and test an empty 'exports' ModuleNamespaceObject on WebAssembly.Instance.

The implementation isn't complete yet: it relies on further work for which I've filed bugs.

WebAssembly API: implement Instance
https://bugs.webkit.org/show_bug.cgi?id=163998

Reviewed by Keith Miller.

JSTests:

* wasm/js-api/test_Instance.js: Added.
(EmptyModule): use the Builder, create the simplest Module possible, and create an Instance from it
* wasm/js-api/test_basic_api.js:
(const.c.in.constructorProperties.switch): basic tests of the API

Source/JavaScriptCore:

* wasm/WasmPlan.h:
(JSC::Wasm::Plan::getFunctions): allow transfering state out
(JSC::Wasm::Plan::getMemory): allow transfering state out
* wasm/js/JSWebAssemblyInstance.cpp:
(JSC::JSWebAssemblyInstance::create):
(JSC::JSWebAssemblyInstance::finishCreation): set the ModuleNamespaceObject, and expose it as &quot;exports&quot;
(JSC::JSWebAssemblyInstance::visitChildren): visit the ModuleNamespaceObject child
* wasm/js/JSWebAssemblyInstance.h:
* wasm/js/JSWebAssemblyModule.cpp:
(JSC::JSWebAssemblyModule::create):
(JSC::JSWebAssemblyModule::JSWebAssemblyModule): move in the compiled functions and the memory
* wasm/js/JSWebAssemblyModule.h:
* wasm/js/WebAssemblyInstanceConstructor.cpp:
(JSC::constructJSWebAssemblyInstance): take the Module, extract the Plan's results, and create the (empty for now) ModuleNamespaceObject
* wasm/js/WebAssemblyModuleConstructor.cpp:
(JSC::constructJSWebAssemblyModule): add a few comments from the spec, and pass out the Plan's results</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="#trunkSourceJavaScriptCorewasmWasmPlanh">trunk/Source/JavaScriptCore/wasm/WasmPlan.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsJSWebAssemblyInstancecpp">trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsJSWebAssemblyInstanceh">trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsJSWebAssemblyModulecpp">trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyModule.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsJSWebAssemblyModuleh">trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyModule.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyInstanceConstructorcpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsWebAssemblyModuleConstructorcpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleConstructor.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkJSTestswasmjsapitest_Instancejs">trunk/JSTests/wasm/js-api/test_Instance.js</a></li>
</ul>

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

</body>
</html>