<!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
<rdar://problem/29760110>
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 <sbarati@apple.com>
</span><span class="cx">
</span><ins>+ WebAssembly: Implement the WebAssembly.instantiate API
+ https://bugs.webkit.org/show_bug.cgi?id=165982
+ <rdar://problem/29760110>
+
+ 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 <sbarati@apple.com>
+
</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("foo")
+ .End()
+ .Code()
+ .Function("foo", { params: [], ret: "i32" })
+ .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("foo")
+ .End()
+ .Code()
+ .Function("foo", { params: [], ret: "i32" })
+ .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, "second argument to WebAssembly.instantiate must be undefined or an Object (evaluating 'WebAssembly.instantiate(bin, null)')");
+ }
+ done = true;
+ }
+
+ test();
+ drainMicrotasks();
+ assert.truthy(done);
+}
+
+{
+ const builder = (new Builder())
+ .Type().End()
+ .Function().End()
+ .Export()
+ .Function("foo")
+ .End()
+ .Code()
+ .Function("foo", { params: [], ret: "i32" })
+ .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, "WebAssembly.Module doesn't validate: control flow returns with unexpected type, in function at index 0 (evaluating 'WebAssembly.instantiate(bin)')");
+ }
+ done = true;
+ }
+
+ test();
+ drainMicrotasks();
+ assert.truthy(done);
+}
+
+{
+ const builder = (new Builder())
+ .Type().End()
+ .Import().Memory("imp", "memory", {initial:100}).End()
+ .Function().End()
+ .Export()
+ .Function("foo")
+ .End()
+ .Code()
+ .Function("foo", { params: [], ret: "i32" })
+ .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, "Memory import is not an instance of WebAssembly.Memory (evaluating 'WebAssembly.instantiate(bin, {imp: {memory: 20}})')");
+ }
+ done = true;
+ }
+
+ test();
+ drainMicrotasks();
+ assert.truthy(done);
+}
+
+{
+ const builder = (new Builder())
+ .Type().End()
+ .Import().Memory("imp", "memory", {initial:100}).End()
+ .Function().End()
+ .Export()
+ .Function("foo")
+ .End()
+ .Code()
+ .Function("foo", { params: [], ret: "i32" })
+ .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, "Memory import is not an instance of WebAssembly.Memory (evaluating 'WebAssembly.instantiate(bin, {imp: {memory: 20}})')");
+ }
+ done = true;
+ }
+
+ test();
+ drainMicrotasks();
+ assert.truthy(done);
+}
+
+{
+ const builder = (new Builder())
+ .Type().End()
+ .Function().End()
+ .Export()
+ .Function("foo")
+ .End()
+ .Code()
+ .Function("foo", { params: [], ret: "i32" })
+ .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("imp", "memory", {initial:100}).End()
+ .Function().End()
+ .Export()
+ .Function("foo")
+ .End()
+ .Code()
+ .Function("foo", { params: [], ret: "i32" })
+ .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, "first argument must be an ArrayBufferView or an ArrayBuffer (evaluating 'WebAssembly.instantiate(25)')");
+ }
+ 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 <sbarati@apple.com>
+
+ WebAssembly: Implement the WebAssembly.instantiate API
+ https://bugs.webkit.org/show_bug.cgi?id=165982
+ <rdar://problem/29760110>
+
+ 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 <msaboff@apple.com>
</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 "JSCInlines.h"
</span><span class="cx"> #include "JSPromiseDeferred.h"
</span><span class="cx"> #include "JSWebAssemblyHelpers.h"
</span><ins>+#include "ObjectConstructor.h"
</ins><span class="cx"> #include "WasmPlan.h"
</span><span class="cx"> #include "WebAssemblyModuleConstructor.h"
</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->lexicalGlobalObject()->WebAssemblyModuleStructure());
</del><ins>+ JSValue module = WebAssemblyModuleConstructor::createModule(exec, exec->argument(0), exec->lexicalGlobalObject()->WebAssemblyModuleStructure());
</ins><span class="cx"> if (Exception* exception = catchScope.exception()) {
</span><span class="cx"> catchScope.clearException();
</span><span class="cx"> promise->reject(exec, exception->value());
</span><span class="lines">@@ -64,6 +66,60 @@
</span><span class="cx"> return JSValue::encode(promise->promise());
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+EncodedJSValue JSC_HOST_CALL webAssemblyInstantiateFunc(ExecState* exec)
+{
+ VM& vm = exec->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->lexicalGlobalObject());
+ RETURN_IF_EXCEPTION(catchScope, encodedJSValue());
+
+ auto reject = [&] () {
+ Exception* exception = catchScope.exception();
+ ASSERT(exception);
+ catchScope.clearException();
+ promise->reject(exec, exception->value());
+ return JSValue::encode(promise->promise());
+ };
+
+ JSValue importArgument = exec->argument(1);
+ JSObject* importObject = importArgument.getObject();
+ if (!importArgument.isUndefined() && !importObject) {
+ promise->reject(exec, createTypeError(exec,
+ ASCIILiteral("second argument to WebAssembly.instantiate must be undefined or an Object"), defaultSourceAppender, runtimeTypeForValue(importArgument)));
+ return JSValue::encode(promise->promise());
+ }
+
+ JSValue firstArgument = exec->argument(0);
+ JSValue module;
+ bool firstArgumentIsModule = false;
+ if (firstArgument.inherits(vm, JSWebAssemblyModule::info())) {
+ firstArgumentIsModule = true;
+ module = firstArgument;
+ } else {
+ module = WebAssemblyModuleConstructor::createModule(exec, firstArgument, exec->lexicalGlobalObject()->WebAssemblyModuleStructure());
+ if (catchScope.exception())
+ return reject();
+ }
+
+ JSWebAssemblyInstance* instance = WebAssemblyInstanceConstructor::createInstance(exec, jsCast<JSWebAssemblyModule*>(module), importObject, exec->lexicalGlobalObject()->WebAssemblyInstanceStructure());
+ if (catchScope.exception())
+ return reject();
+
+ if (firstArgumentIsModule)
+ promise->resolve(exec, instance);
+ else {
+ JSObject* result = constructEmptyObject(exec);
+ result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("module")), module);
+ result->putDirect(vm, Identifier::fromString(&vm, ASCIILiteral("instance")), instance);
+ promise->resolve(exec, result);
+ }
+
+ return JSValue::encode(promise->promise());
+}
+
</ins><span class="cx"> EncodedJSValue JSC_HOST_CALL webAssemblyValidateFunc(ExecState* exec)
</span><span class="cx"> {
</span><span class="cx"> VM& vm = exec->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("validate", webAssemblyValidateFunc, DontEnum, 1);
</span><span class="cx"> JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("compile", webAssemblyCompileFunc, DontEnum, 1);
</span><ins>+ JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("instantiate", webAssemblyInstantiateFunc, DontEnum, 1);
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> JSWebAssembly::JSWebAssembly(VM& 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& vm = exec->vm();
</span><span class="cx"> auto throwScope = DECLARE_THROW_SCOPE(vm);
</span><del>- auto* globalObject = exec->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<JSWebAssemblyModule*>(vm, exec->argument(0));
</span><span class="cx"> if (!jsModule)
</span><span class="cx"> return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("first argument to WebAssembly.Instance must be a WebAssembly.Module"), defaultSourceAppender, runtimeTypeForValue(exec->argument(0)))));
</span><del>- const Wasm::ModuleInformation& moduleInformation = jsModule->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->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() && !importObject)
</span><span class="cx"> return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("second argument to WebAssembly.Instance must be undefined or an Object"), defaultSourceAppender, runtimeTypeForValue(importArgument))));
</span><ins>+
+ Structure* instanceStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), exec->lexicalGlobalObject()->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& vm = exec->vm();
+ auto throwScope = DECLARE_THROW_SCOPE(vm);
+ auto* globalObject = exec->lexicalGlobalObject();
+
+ const Wasm::ModuleInformation& moduleInformation = jsModule->moduleInformation();
+
+ auto exception = [&] (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() && !importObject)
</span><del>- return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("second argument to WebAssembly.Instance must be Object because the WebAssembly.Module has imports"), defaultSourceAppender, runtimeTypeForValue(importArgument))));
</del><ins>+ return exception(createTypeError(exec, ASCIILiteral("can't make WebAssembly.Instance because there is no imports Object and the WebAssembly.Module requires imports")));
</ins><span class="cx">
</span><span class="cx"> Identifier moduleKey = Identifier::fromUid(PrivateName(PrivateName::Description, "WebAssemblyInstance"));
</span><span class="cx"> WebAssemblyModuleRecord* moduleRecord = WebAssemblyModuleRecord::create(exec, vm, globalObject->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->newTarget(), globalObject->WebAssemblyInstanceStructure());
- RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
</del><span class="cx">
</span><span class="cx"> JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, instanceStructure, jsModule, moduleRecord->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& 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->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("import must be an object"), defaultSourceAppender, runtimeTypeForValue(importModuleValue))));
</del><ins>+ return exception(createTypeError(exec, ASCIILiteral("import must be an object"), 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<JSObject*>(importModuleValue);
</span><span class="cx"> JSValue value = object->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("import function must be callable"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("import function must be callable")));
+
</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<WebAssemblyFunction*>(vm, cell)) {
</span><span class="lines">@@ -120,7 +137,7 @@
</span><span class="cx"> Wasm::SignatureIndex importedSignatureIndex = importedExport->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("imported function's signature doesn't match the provided WebAssembly function's signature"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("imported function's signature doesn't match the provided WebAssembly function's signature")));
</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<JSWebAssemblyTable*>(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("Table import is not an instance of WebAssembly.Table"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Table import is not an instance of WebAssembly.Table")));
</ins><span class="cx">
</span><span class="cx"> uint32_t expectedInitial = moduleInformation.tableInformation.initial();
</span><span class="cx"> uint32_t actualInitial = table->size();
</span><span class="cx"> if (actualInitial < expectedInitial)
</span><del>- return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Table import provided an 'initial' that is too small"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Table import provided an 'initial' that is too small")));
</ins><span class="cx">
</span><span class="cx"> if (std::optional<uint32_t> expectedMaximum = moduleInformation.tableInformation.maximum()) {
</span><span class="cx"> std::optional<uint32_t> actualMaximum = table->maximum();
</span><del>- if (!actualMaximum) {
- return JSValue::encode(
- throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Table import does not have a 'maximum' but the module requires that it does"))));
- }
- if (*actualMaximum > *expectedMaximum) {
- return JSValue::encode(
- throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Imported Table's 'maximum' is larger than the module's expected 'maximum'"))));
- }
</del><ins>+ if (!actualMaximum)
+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Table import does not have a 'maximum' but the module requires that it does")));
+ if (*actualMaximum > *expectedMaximum)
+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Imported Table's 'maximum' is larger than the module's expected 'maximum'")));
</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<JSWebAssemblyMemory*>(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("Memory import is not an instance of WebAssembly.Memory"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Memory import is not an instance of WebAssembly.Memory")));
</ins><span class="cx">
</span><span class="cx"> Wasm::PageCount expectedInitial = moduleInformation.memory.initial();
</span><span class="cx"> Wasm::PageCount actualInitial = memory->memory().initial();
</span><span class="cx"> if (actualInitial < expectedInitial)
</span><del>- return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Memory import provided an 'initial' that is too small"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Memory import provided an 'initial' that is too small")));
</ins><span class="cx">
</span><span class="cx"> if (Wasm::PageCount expectedMaximum = moduleInformation.memory.maximum()) {
</span><span class="cx"> Wasm::PageCount actualMaximum = memory->memory().maximum();
</span><del>- if (!actualMaximum) {
- return JSValue::encode(
- throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Memory import did not have a 'maximum' but the module requires that it does"))));
- }
</del><ins>+ if (!actualMaximum)
+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Memory import did not have a 'maximum' but the module requires that it does")));
</ins><span class="cx">
</span><del>- if (actualMaximum > expectedMaximum) {
- return JSValue::encode(
- throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Memory imports 'maximum' is larger than the module's expected 'maximum'"))));
- }
</del><ins>+ if (actualMaximum > expectedMaximum)
+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("Memory imports 'maximum' is larger than the module's expected 'maximum'")));
</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->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("imported global cannot be an i64"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("imported global cannot be an i64")));
</ins><span class="cx"> if (!value.isNumber())
</span><del>- return JSValue::encode(throwException(exec, throwScope, createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("imported global must be a number"))));
</del><ins>+ return exception(createJSWebAssemblyLinkError(exec, vm, ASCIILiteral("imported global must be a number")));
</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->setMemory(vm, exec,
</span><span class="cx"> JSWebAssemblyMemory::create(vm, exec->lexicalGlobalObject()->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->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->dump();
</span><span class="cx"> JSValue startResult = moduleRecord->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&, 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->newTarget(), exec->lexicalGlobalObject()->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->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, "WebAssembly.Module"));
</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& vm = state->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->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&, WebAssemblyModulePrototype*);
</span></span></pre>
</div>
</div>
</body>
</html>