<!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>[286703] 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/286703">286703</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2021-12-08 12:27:51 -0800 (Wed, 08 Dec 2021)</dd>
</dl>

<h3>Log Message</h3>
<pre>Support WebAssembly.Memory imports in Wasm/ESM modules.
https://bugs.webkit.org/show_bug.cgi?id=184745

Patch by Asumu Takikawa <asumu@igalia.com> on 2021-12-08
Reviewed by Yusuke Suzuki.

JSTests:

* wasm/modules/wasm-imports-js-exports.js:
(then):
* wasm/modules/wasm-imports-js-exports/memory-fail-1.js: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-1.wasm: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-1.wat: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-2.js: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-2.wasm: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-2.wat: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-3.js: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-3.wasm: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-3.wat: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-4.js: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-4.wasm: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-4.wat: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-5.js: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-5.wasm: Added.
* wasm/modules/wasm-imports-js-exports/memory-fail-5.wat: Added.
* wasm/modules/wasm-imports-js-exports/memory.js: Added.
(export.setMemory):
* wasm/modules/wasm-imports-js-exports/memory.wasm: Added.
* wasm/modules/wasm-imports-js-exports/memory.wat: Added.
* wasm/modules/wasm-imports-js-exports/shared-memory.js: Added.
* wasm/modules/wasm-imports-js-exports/shared-memory.wasm: Added.
* wasm/modules/wasm-imports-js-exports/shared-memory.wat: Added.
* wasm/modules/wasm-imports-js-re-exports-wasm-exports.js:
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm:
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat:
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js:
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm:
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat:

Source/JavaScriptCore:

Changes how Wasm code is initialized for modules that are loaded
by the module loader and have memory imports. The initial code for
the LLInt tier is copied to all memory modes, so that the memory
import can be initialized after compilation. When LLInt is turned
off, the compilation of B3/Air code is delayed until the memory is
initialized.

* wasm/WasmCodeBlock.cpp:
(JSC::Wasm::CodeBlock::createFromExisting):
(JSC::Wasm::CodeBlock::CodeBlock):
* wasm/WasmCodeBlock.h:
* wasm/WasmInstance.h:
(JSC::Wasm::Instance::finalizeCreation):
(JSC::Wasm::Instance::module const):
(JSC::Wasm::Instance::codeBlock const):
(JSC::Wasm::Instance::memory const):
(JSC::Wasm::Instance::isImportFunction const):
(JSC::Wasm::Instance::module): Deleted.
(JSC::Wasm::Instance::codeBlock): Deleted.
(JSC::Wasm::Instance::memory): Deleted.
* wasm/WasmModule.cpp:
(JSC::Wasm::Module::copyInitialCodeBlockToAllMemoryModes):
* wasm/WasmModule.h:
* wasm/js/JSWebAssembly.cpp:
(JSC::instantiate):
* wasm/js/JSWebAssemblyInstance.cpp:
(JSC::JSWebAssemblyInstance::finalizeCreation):
(JSC::JSWebAssemblyInstance::tryCreate):
* wasm/js/JSWebAssemblyInstance.h:
* wasm/js/WebAssemblyModuleRecord.cpp:
(JSC::WebAssemblyModuleRecord::initializeImportsAndExports):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJSTestsChangeLog">trunk/JSTests/ChangeLog</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsjs">trunk/JSTests/wasm/modules/wasm-imports-js-exports.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsimportswasm">trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsimportswat">trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsreexportjs">trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportssumwasm">trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportssumwat">trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsjs">trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports.js</a></li>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmCodeBlockcpp">trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmCodeBlockh">trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmInstanceh">trunk/Source/JavaScriptCore/wasm/WasmInstance.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmModulecpp">trunk/Source/JavaScriptCore/wasm/WasmModule.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmWasmModuleh">trunk/Source/JavaScriptCore/wasm/WasmModule.h</a></li>
<li><a href="#trunkSourceJavaScriptCorewasmjsJSWebAssemblycpp">trunk/Source/JavaScriptCore/wasm/js/JSWebAssembly.cpp</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="#trunkSourceJavaScriptCorewasmjsWebAssemblyModuleRecordcpp">trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail1js">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail1wasm">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail1wat">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail2js">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail2wasm">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail2wat">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail3js">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail3wasm">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail3wat">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail4js">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail4wasm">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail4wat">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail5js">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail5wasm">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail5wat">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemoryjs">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemorywasm">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportsmemorywat">trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wat</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportssharedmemoryjs">trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.js</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportssharedmemorywasm">trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wasm</a></li>
<li><a href="#trunkJSTestswasmmoduleswasmimportsjsexportssharedmemorywat">trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wat</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJSTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/ChangeLog (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/ChangeLog  2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/ChangeLog     2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2021-12-08  Asumu Takikawa  <asumu@igalia.com>
+
+        Support WebAssembly.Memory imports in Wasm/ESM modules.
+        https://bugs.webkit.org/show_bug.cgi?id=184745
+
+        Reviewed by Yusuke Suzuki.
+
+        * wasm/modules/wasm-imports-js-exports.js:
+        (then):
+        * wasm/modules/wasm-imports-js-exports/memory-fail-1.js: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-1.wasm: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-1.wat: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-2.js: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-2.wasm: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-2.wat: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-3.js: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-3.wasm: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-3.wat: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-4.js: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-4.wasm: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-4.wat: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-5.js: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-5.wasm: Added.
+        * wasm/modules/wasm-imports-js-exports/memory-fail-5.wat: Added.
+        * wasm/modules/wasm-imports-js-exports/memory.js: Added.
+        (export.setMemory):
+        * wasm/modules/wasm-imports-js-exports/memory.wasm: Added.
+        * wasm/modules/wasm-imports-js-exports/memory.wat: Added.
+        * wasm/modules/wasm-imports-js-exports/shared-memory.js: Added.
+        * wasm/modules/wasm-imports-js-exports/shared-memory.wasm: Added.
+        * wasm/modules/wasm-imports-js-exports/shared-memory.wat: Added.
+        * wasm/modules/wasm-imports-js-re-exports-wasm-exports.js:
+        * wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm:
+        * wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat:
+        * wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js:
+        * wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm:
+        * wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat:
+
</ins><span class="cx"> 2021-12-07  Saam Barati  <sbarati@apple.com>
</span><span class="cx"> 
</span><span class="cx">         TypedArray prototype set should go down the fast path when using non clamped integer types of the same byte size
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail1js"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.js (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.js                              (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.js 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+export let memory = 3;
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail1wasm"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wasm (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wasm                            (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wasm       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+asm./memory-fail-1.jsmemory
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail1wat"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wat (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wat                             (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-1.wat        2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,2 @@
</span><ins>+(module
+    (import "./memory-fail-1.js" "memory" (memory $m 1)))
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail2js"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.js (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.js                              (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.js 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,3 @@
</span><ins>+export let memory = new WebAssembly.Memory({
+    initial: 1,
+});
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail2wasm"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wasm (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wasm                            (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wasm       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+asm./memory-fail-2.jsmemory
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail2wat"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wat (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wat                             (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-2.wat        2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,2 @@
</span><ins>+(module
+    (import "./memory-fail-2.js" "memory" (memory $m 4)))
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail3js"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.js (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.js                              (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.js 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,3 @@
</span><ins>+export let memory = new WebAssembly.Memory({
+    initial: 1,
+});
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail3wasm"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wasm (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wasm                            (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wasm       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+asm./memory-fail-3.jsmemory
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail3wat"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wat (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wat                             (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-3.wat        2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,2 @@
</span><ins>+(module
+    (import "./memory-fail-3.js" "memory" (memory $m 1 10)))
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail4js"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.js (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.js                              (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.js 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,4 @@
</span><ins>+export let memory = new WebAssembly.Memory({
+    initial: 1,
+    maximum: 11
+});
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail4wasm"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wasm (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wasm                            (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wasm       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+asm./memory-fail-4.jsmemory
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail4wat"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wat (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wat                             (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-4.wat        2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,2 @@
</span><ins>+(module
+    (import "./memory-fail-4.js" "memory" (memory $m 1 10)))
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail5js"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.js (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.js                              (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.js 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,5 @@
</span><ins>+export let memory = new WebAssembly.Memory({
+    initial: 1,
+    maximum: 10,
+    shared: false,
+});
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail5wasm"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wasm (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wasm                            (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wasm       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1 @@
</span><ins>+asm./memory-fail-5.jsmemory
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryfail5wat"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wat (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wat                             (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory-fail-5.wat        2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,2 @@
</span><ins>+(module
+    (import "./memory-fail-5.js" "memory" (memory $m 1 10 shared)))
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemoryjs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.js (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.js                             (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.js        2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,10 @@
</span><ins>+export let memory = new WebAssembly.Memory({
+    initial: 1,
+});
+
+export function setMemory(mem) {
+    memory = mem;
+}
+
+const b = new Int8Array(memory.buffer);
+b[0] = 42;
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemorywasm"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wasm (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wasm                           (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wasm      2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,3 @@
</span><ins>+asm` ./memory.jsmemory
+getMem
+        -
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsmemorywat"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wat (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wat                            (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/memory.wat       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,4 @@
</span><ins>+(module
+    (import "./memory.js" "memory" (memory $m 1))
+    (func (export "getMem") (param i32) (result i32)
+        (i32.load8_u (local.get 0))))
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportssharedmemoryjs"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.js (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.js                              (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.js 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,8 @@
</span><ins>+export const memory = new WebAssembly.Memory({
+    initial: 1,
+    maximum: 10,
+    shared: true
+});
+
+const b = new Int8Array(memory.buffer);
+b[0] = 42;
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportssharedmemorywasm"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wasm (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wasm                            (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wasm       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,4 @@
</span><ins>+asm`./shared-memory.jsmemory
+
+getMem
+        -
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportssharedmemorywat"></a>
<div class="addfile"><h4>Added: trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wat (0 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wat                             (rev 0)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports/shared-memory.wat        2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -0,0 +1,4 @@
</span><ins>+(module
+    (import "./shared-memory.js" "memory" (memory $m 1 10 shared))
+    (func (export "getMem") (param i32) (result i32)
+        (i32.load8_u (local.get 0))))
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsexportsjs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/modules/wasm-imports-js-exports.js (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-exports.js    2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-exports.js       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -18,3 +18,37 @@
</span><span class="cx"> 
</span><span class="cx"> assert.isFunction(getElem);
</span><span class="cx"> assert.eq(getElem(), "foo");
</span><ins>+
+import { getMem } from "./wasm-imports-js-exports/memory.wasm";
+
+assert.isFunction(getMem);
+assert.eq(getMem(0), 42);
+assert.eq(getMem(65535), 0);
+assert.throws(() => getMem(65536), WebAssembly.RuntimeError, "Out of bounds memory access");
+
+import { getMem as sharedGetMem } from "./wasm-imports-js-exports/shared-memory.wasm";
+
+assert.isFunction(sharedGetMem);
+assert.eq(sharedGetMem(0), 42);
+assert.eq(sharedGetMem(65535), 0);
+assert.throws(() => sharedGetMem(65536), WebAssembly.RuntimeError, "Out of bounds memory access");
+
+import("./wasm-imports-js-exports/memory-fail-1.wasm").then($vm.abort, function (error) {
+    assert.eq(String(error), `LinkError: Memory import ./memory-fail-1.js:memory is not an instance of WebAssembly.Memory`);
+}).then(function () { }, $vm.abort);
+
+import("./wasm-imports-js-exports/memory-fail-2.wasm").then($vm.abort, function (error) {
+    assert.eq(String(error), `LinkError: Memory import ./memory-fail-2.js:memory provided an 'initial' that is smaller than the module's declared 'initial' import memory size`);
+}).then(function () { }, $vm.abort);
+
+import("./wasm-imports-js-exports/memory-fail-3.wasm").then($vm.abort, function (error) {
+    assert.eq(String(error), `LinkError: Memory import ./memory-fail-3.js:memory did not have a 'maximum' but the module requires that it does`);
+}).then(function () { }, $vm.abort);
+
+import("./wasm-imports-js-exports/memory-fail-4.wasm").then($vm.abort, function (error) {
+    assert.eq(String(error), `LinkError: Memory import ./memory-fail-4.js:memory provided a 'maximum' that is larger than the module's declared 'maximum' import memory size`);
+}).then(function () { }, $vm.abort);
+
+import("./wasm-imports-js-exports/memory-fail-5.wasm").then($vm.abort, function (error) {
+    assert.eq(String(error), `LinkError: Memory import ./memory-fail-5.js:memory provided a 'shared' that is different from the module's declared 'shared' import memory attribute`);
+}).then(function () { }, $vm.abort);
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsimportswasm"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm  2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm     2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -1,2 +1,2 @@
</span><del>-asm```H./re-export.jssum./re-export.jsanswer./re-export.jstableptableaddOne       getAnswer    A 
</del><ins>+asm```b./re-export.jssum./re-export.jsanswer./re-export.jstablep./re-export.jsmemory'tablememoryaddOne    getAnswer    A 
</ins><span class="cx"> A  #
</span><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsimportswat"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat   2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat      2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -2,7 +2,9 @@
</span><span class="cx">     (import "./re-export.js" "sum" (func $sum (param i32 i32) (result i32)))
</span><span class="cx">     (import "./re-export.js" "answer" (global i32))
</span><span class="cx">     (import "./re-export.js" "table" (table $table 4 funcref))
</span><ins>+    (import "./re-export.js" "memory" (memory $memory 1 1))
</ins><span class="cx">     (export "table" (table $table))
</span><ins>+    (export "memory" (memory $memory))
</ins><span class="cx">     (type $t0 (func (param i32) (result i32)))
</span><span class="cx">     (func $addOne (export "addOne") (type $t0) (param $p0 i32) (result i32)
</span><span class="cx">         i32.const 1
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsreexportjs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js  2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js     2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -1 +1 @@
</span><del>-export { sum, answer, table } from "./sum.wasm"
</del><ins>+export { sum, answer, table, memory } from "./sum.wasm"
</ins></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportssumwasm"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm      2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -1,2 +1,3 @@
</span><del>-asm`pA* sumanswertable       A 
-         j
</del><span class="cx">\ No newline at end of file
</span><ins>+asm`pA* !sumanswertablememory       A 
+         j
+A 
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportssumwat"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat       2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat  2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -6,4 +6,6 @@
</span><span class="cx">         i32.add)
</span><span class="cx">     (global (export "answer") i32 i32.const 42)
</span><span class="cx">     (table $table (export "table") 4 funcref)
</span><ins>+    (memory $memory (export "memory") 1 1)
+    (data (i32.const 4) "\10\00\10\00")
</ins><span class="cx">     (elem (i32.const 0) $sum))
</span></span></pre></div>
<a id="trunkJSTestswasmmoduleswasmimportsjsreexportswasmexportsjs"></a>
<div class="modfile"><h4>Modified: trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports.js (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports.js    2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports.js       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -1,4 +1,4 @@
</span><del>-import { addOne, getAnswer, table } from "./wasm-imports-js-re-exports-wasm-exports/imports.wasm"
</del><ins>+import { addOne, getAnswer, table, memory } from "./wasm-imports-js-re-exports-wasm-exports/imports.wasm"
</ins><span class="cx"> import * as assert from '../assert.js';
</span><span class="cx"> 
</span><span class="cx"> assert.isFunction(addOne);
</span><span class="lines">@@ -13,3 +13,13 @@
</span><span class="cx"> assert.eq(table.get(1)(-1), 0);
</span><span class="cx"> assert.eq(table.get(2), null);
</span><span class="cx"> assert.eq(table.get(3), null);
</span><ins>+
+assert.eq(memory.buffer.byteLength, 65536);
+const buf = new Uint8Array(memory.buffer);
+assert.eq(buf[4], 0x10);
+assert.eq(buf[5], 0x00);
+assert.eq(buf[6], 0x10);
+assert.eq(buf[7], 0x00);
+buf[0] = 0x42;
+assert.eq(buf[0], 0x42);
+assert.eq(buf[65536], undefined);
</ins></span></pre></div>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog    2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/ChangeLog       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -1,3 +1,42 @@
</span><ins>+2021-12-08  Asumu Takikawa  <asumu@igalia.com>
+
+        Support WebAssembly.Memory imports in Wasm/ESM modules.
+        https://bugs.webkit.org/show_bug.cgi?id=184745
+
+        Reviewed by Yusuke Suzuki.
+
+        Changes how Wasm code is initialized for modules that are loaded
+        by the module loader and have memory imports. The initial code for
+        the LLInt tier is copied to all memory modes, so that the memory
+        import can be initialized after compilation. When LLInt is turned
+        off, the compilation of B3/Air code is delayed until the memory is
+        initialized.
+
+        * wasm/WasmCodeBlock.cpp:
+        (JSC::Wasm::CodeBlock::createFromExisting):
+        (JSC::Wasm::CodeBlock::CodeBlock):
+        * wasm/WasmCodeBlock.h:
+        * wasm/WasmInstance.h:
+        (JSC::Wasm::Instance::finalizeCreation):
+        (JSC::Wasm::Instance::module const):
+        (JSC::Wasm::Instance::codeBlock const):
+        (JSC::Wasm::Instance::memory const):
+        (JSC::Wasm::Instance::isImportFunction const):
+        (JSC::Wasm::Instance::module): Deleted.
+        (JSC::Wasm::Instance::codeBlock): Deleted.
+        (JSC::Wasm::Instance::memory): Deleted.
+        * wasm/WasmModule.cpp:
+        (JSC::Wasm::Module::copyInitialCodeBlockToAllMemoryModes):
+        * wasm/WasmModule.h:
+        * wasm/js/JSWebAssembly.cpp:
+        (JSC::instantiate):
+        * wasm/js/JSWebAssemblyInstance.cpp:
+        (JSC::JSWebAssemblyInstance::finalizeCreation):
+        (JSC::JSWebAssemblyInstance::tryCreate):
+        * wasm/js/JSWebAssemblyInstance.h:
+        * wasm/js/WebAssemblyModuleRecord.cpp:
+        (JSC::WebAssemblyModuleRecord::initializeImportsAndExports):
+
</ins><span class="cx"> 2021-12-08  Yusuke Suzuki  <ysuzuki@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [JSC] Introduce WriteBarrierStructureID
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmCodeBlockcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.cpp (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.cpp       2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.cpp  2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -37,10 +37,30 @@
</span><span class="cx"> 
</span><span class="cx"> Ref<CodeBlock> CodeBlock::create(Context* context, MemoryMode mode, ModuleInformation& moduleInformation, RefPtr<LLIntCallees> llintCallees)
</span><span class="cx"> {
</span><del>-    auto* result = new (NotNull, fastMalloc(sizeof(CodeBlock))) CodeBlock(context, mode, moduleInformation, llintCallees);
-    return adoptRef(*result);
</del><ins>+    return adoptRef(*new CodeBlock(context, mode, moduleInformation, llintCallees));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+Ref<CodeBlock> CodeBlock::createFromExisting(MemoryMode mode, const CodeBlock& other)
+{
+    return adoptRef(*new CodeBlock(mode, other));
+}
+
+CodeBlock::CodeBlock(MemoryMode mode, const CodeBlock& other)
+    : m_calleeCount(other.m_calleeCount)
+    , m_mode(mode)
+    , m_llintCallees(other.m_llintCallees)
+    , m_embedderCallees(other.m_embedderCallees)
+    , m_wasmIndirectCallEntryPoints(other.m_wasmIndirectCallEntryPoints)
+    , m_wasmToWasmCallsites(other.m_wasmToWasmCallsites)
+    , m_wasmToWasmExitStubs(other.m_wasmToWasmExitStubs)
+{
+#if ENABLE(WEBASSEMBLY_B3JIT)
+    m_bbqCallees.resize(m_calleeCount);
+    m_omgCallees.resize(m_calleeCount);
+#endif
+    setCompilationFinished();
+}
+
</ins><span class="cx"> CodeBlock::CodeBlock(Context* context, MemoryMode mode, ModuleInformation& moduleInformation, RefPtr<LLIntCallees> llintCallees)
</span><span class="cx">     : m_calleeCount(moduleInformation.internalFunctionCount())
</span><span class="cx">     , m_mode(mode)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmCodeBlockh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.h (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.h 2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/WasmCodeBlock.h    2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -54,6 +54,7 @@
</span><span class="cx">     typedef void CallbackType(Ref<CodeBlock>&&);
</span><span class="cx">     using AsyncCompilationCallback = RefPtr<WTF::SharedTask<CallbackType>>;
</span><span class="cx">     static Ref<CodeBlock> create(Context*, MemoryMode, ModuleInformation&, RefPtr<LLIntCallees>);
</span><ins>+    static Ref<CodeBlock> createFromExisting(MemoryMode, const CodeBlock&);
</ins><span class="cx"> 
</span><span class="cx">     void waitUntilFinished();
</span><span class="cx">     void compileAsync(Context*, AsyncCompilationCallback&&);
</span><span class="lines">@@ -137,6 +138,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     CodeBlock(Context*, MemoryMode, ModuleInformation&, RefPtr<LLIntCallees>);
</span><ins>+    CodeBlock(MemoryMode, const CodeBlock&);
</ins><span class="cx">     void setCompilationFinished();
</span><span class="cx">     unsigned m_calleeCount;
</span><span class="cx">     MemoryMode m_mode;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmInstanceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmInstance.h (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmInstance.h  2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/WasmInstance.h     2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -56,10 +56,9 @@
</span><span class="cx"> 
</span><span class="cx">     static Ref<Instance> create(Context*, Ref<Module>&&, EntryFrame** pointerToTopEntryFrame, void** pointerToActualStackLimit, StoreTopCallFrameCallback&&);
</span><span class="cx"> 
</span><del>-    void finalizeCreation(void* owner, Ref<CodeBlock>&& codeBlock)
</del><ins>+    void finalizeCreation(void* owner)
</ins><span class="cx">     {
</span><span class="cx">         m_owner = owner;
</span><del>-        m_codeBlock = WTFMove(codeBlock);
</del><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     JS_EXPORT_PRIVATE ~Instance();
</span><span class="lines">@@ -71,9 +70,9 @@
</span><span class="cx"> 
</span><span class="cx">     Wasm::Context* context() const { return m_context; }
</span><span class="cx"> 
</span><del>-    Module& module() { return m_module.get(); }
-    CodeBlock* codeBlock() { return m_codeBlock.get(); }
-    Memory* memory() { return m_memory.get(); }
</del><ins>+    Module& module() const { return m_module.get(); }
+    CodeBlock* codeBlock() const { return module().codeBlockFor(memory()->mode()); }
+    Memory* memory() const { return m_memory.get(); }
</ins><span class="cx">     Table* table(unsigned);
</span><span class="cx">     void setTable(unsigned, Ref<Table>&&);
</span><span class="cx">     const Element* elementAt(unsigned) const;
</span><span class="lines">@@ -82,7 +81,7 @@
</span><span class="cx"> 
</span><span class="cx">     bool isImportFunction(uint32_t functionIndex) const
</span><span class="cx">     {
</span><del>-        return functionIndex < m_codeBlock->functionImportCount();
</del><ins>+        return functionIndex < codeBlock()->functionImportCount();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     void tableInit(uint32_t dstOffset, uint32_t srcOffset, uint32_t length, uint32_t elementIndex, uint32_t tableIndex);
</span><span class="lines">@@ -226,7 +225,6 @@
</span><span class="cx">     CagedPtr<Gigacage::Primitive, void, tagCagedPtr> m_cachedMemory;
</span><span class="cx">     size_t m_cachedBoundsCheckingSize { 0 };
</span><span class="cx">     Ref<Module> m_module;
</span><del>-    RefPtr<CodeBlock> m_codeBlock;
</del><span class="cx">     RefPtr<Memory> m_memory;
</span><span class="cx"> 
</span><span class="cx">     MallocPtr<Global::Value, VMMalloc> m_globals;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmModulecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmModule.cpp (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmModule.cpp  2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/WasmModule.cpp     2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -111,6 +111,18 @@
</span><span class="cx">     codeBlock->compileAsync(context, WTFMove(task));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void Module::copyInitialCodeBlockToAllMemoryModes(MemoryMode initialMode)
+{
+    ASSERT(m_codeBlocks[static_cast<uint8_t>(initialMode)]);
+    const CodeBlock& initialBlock = *m_codeBlocks[static_cast<uint8_t>(initialMode)];
+    for (unsigned i = 0; i < Wasm::NumberOfMemoryModes; i++) {
+        if (i == static_cast<uint8_t>(initialMode))
+            continue;
+        Ref<CodeBlock> newBlock = CodeBlock::createFromExisting(static_cast<MemoryMode>(i), initialBlock);
+        m_codeBlocks[i] = WTFMove(newBlock);
+    }
+}
+
</ins><span class="cx"> } } // namespace JSC::Wasm
</span><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(WEBASSEMBLY)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmWasmModuleh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/WasmModule.h (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/WasmModule.h    2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/WasmModule.h       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -65,6 +65,9 @@
</span><span class="cx">     JS_EXPORT_PRIVATE ~Module();
</span><span class="cx"> 
</span><span class="cx">     CodeBlock* codeBlockFor(MemoryMode mode) { return m_codeBlocks[static_cast<uint8_t>(mode)].get(); }
</span><ins>+
+    void copyInitialCodeBlockToAllMemoryModes(MemoryMode);
+
</ins><span class="cx"> private:
</span><span class="cx">     Ref<CodeBlock> getOrCreateCodeBlock(Context*, MemoryMode);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsJSWebAssemblycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/JSWebAssembly.cpp (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/JSWebAssembly.cpp    2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/js/JSWebAssembly.cpp       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -190,6 +190,7 @@
</span><span class="cx"> {
</span><span class="cx">     auto scope = DECLARE_THROW_SCOPE(vm);
</span><span class="cx">     // In order to avoid potentially recompiling a module. We first gather all the import/memory information prior to compiling code.
</span><ins>+    // When called via the module loader, the memory is not available yet at this step, so we skip initializing the memory here.
</ins><span class="cx">     JSWebAssemblyInstance* instance = JSWebAssemblyInstance::tryCreate(vm, globalObject, moduleKey, module, importObject, globalObject->webAssemblyInstanceStructure(), Ref<Wasm::Module>(module->module()), creationMode);
</span><span class="cx">     if (UNLIKELY(scope.exception())) {
</span><span class="cx">         promise->rejectWithCaughtException(globalObject, scope);
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsJSWebAssemblyInstancecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp    2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp       2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -107,7 +107,7 @@
</span><span class="cx"> 
</span><span class="cx"> void JSWebAssemblyInstance::finalizeCreation(VM& vm, JSGlobalObject* globalObject, Ref<Wasm::CodeBlock>&& wasmCodeBlock, JSObject* importObject, Wasm::CreationMode creationMode)
</span><span class="cx"> {
</span><del>-    m_instance->finalizeCreation(this, wasmCodeBlock.copyRef());
</del><ins>+    m_instance->finalizeCreation(this);
</ins><span class="cx"> 
</span><span class="cx">     auto scope = DECLARE_THROW_SCOPE(vm);
</span><span class="cx"> 
</span><span class="lines">@@ -135,6 +135,29 @@
</span><span class="cx">         m_module->setCodeBlock(vm, memoryMode(), jsCodeBlock);
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    // In the module loader case, we will initialize all memory modes with the initial LLInt compilation
+    // results, so that later when memory imports become available, the appropriate CodeBlock can be used.
+    // If LLInt is disabled, we instead defer compilation to module evaluation.
+    bool hasMemoryImport = module()->moduleInformation().memory.isImport();
+    if (creationMode == Wasm::CreationMode::FromModuleLoader && Options::useWasmLLInt() && hasMemoryImport) {
+        Wasm::MemoryMode initialMode = Wasm::MemoryMode::BoundsChecking;
+        ASSERT(memoryMode() == initialMode);
+        module()->module().copyInitialCodeBlockToAllMemoryModes(initialMode);
+
+        for (unsigned i = 0; i < Wasm::NumberOfMemoryModes; i++) {
+            if (i == static_cast<uint8_t>(initialMode))
+                continue;
+            Wasm::MemoryMode memoryMode = static_cast<Wasm::MemoryMode>(i);
+            RefPtr<Wasm::CodeBlock> codeBlock = module()->module().codeBlockFor(memoryMode);
+            jsCodeBlock = JSWebAssemblyCodeBlock::create(vm, codeBlock.releaseNonNull(), module()->module().moduleInformation());
+            if (UNLIKELY(!jsCodeBlock->runnable())) {
+                throwException(globalObject, scope, createJSWebAssemblyLinkError(globalObject, vm, jsCodeBlock->errorMessage()));
+                return;
+            }
+            m_module->setCodeBlock(vm, memoryMode, jsCodeBlock);
+        }
+    }
+
</ins><span class="cx">     for (unsigned importFunctionNum = 0; importFunctionNum < instance().numImportFunctions(); ++importFunctionNum) {
</span><span class="cx">         auto* info = instance().importFunctionInfo(importFunctionNum);
</span><span class="cx">         info->wasmToEmbedderStub = m_codeBlock->wasmToEmbedderStub(importFunctionNum);
</span><span class="lines">@@ -212,8 +235,8 @@
</span><span class="cx">             Identifier::fromUid(PrivateName(PrivateName::Description, "WebAssemblyImportName")),
</span><span class="cx">         });
</span><span class="cx"> 
</span><del>-        // Skip Wasm::ExternalKind::Function validation here. It will be done in WebAssemblyModuleRecord::link.
-        // Eventually we will move all the linking code here to WebAssemblyModuleRecord::link.
</del><ins>+        // Validation for most types are done in WebAssemblyModuleRecord::initializeImportsAndExports and skipped here.
+        // Eventually we will move all the linking code here to WebAssemblyModuleRecord::initializeImportsAndExports.
</ins><span class="cx">         switch (import.kind) {
</span><span class="cx">         case Wasm::ExternalKind::Function:
</span><span class="cx">         case Wasm::ExternalKind::Global:
</span><span class="lines">@@ -253,6 +276,10 @@
</span><span class="cx">             RELEASE_ASSERT(!hasMemoryImport); // This should be guaranteed by a validation failure.
</span><span class="cx">             RELEASE_ASSERT(moduleInformation.memory);
</span><span class="cx">             hasMemoryImport = true;
</span><ins>+            // For the module loader, we cannot initialize the memory here so we delay this
+            // until WebAssemblyModuleRecord's initialization operation.
+            if (creationMode == Wasm::CreationMode::FromModuleLoader)
+                break;
</ins><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></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsJSWebAssemblyInstanceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.h (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.h      2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.h 2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -75,7 +75,6 @@
</span><span class="cx"> 
</span><span class="cx">     JSWebAssemblyMemory* memory() { return m_memory.get(); }
</span><span class="cx">     void setMemory(VM& vm, JSWebAssemblyMemory* value) {
</span><del>-        ASSERT(!memory());
</del><span class="cx">         m_memory.set(vm, this, value);
</span><span class="cx">         instance().setMemory(memory()->memory());
</span><span class="cx">     }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorewasmjsWebAssemblyModuleRecordcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp (286702 => 286703)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp  2021-12-08 20:27:32 UTC (rev 286702)
+++ trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp     2021-12-08 20:27:51 UTC (rev 286703)
</span><span class="lines">@@ -121,7 +121,6 @@
</span><span class="cx"> 
</span><span class="cx">     RELEASE_ASSERT(m_instance);
</span><span class="cx"> 
</span><del>-    Wasm::CodeBlock* codeBlock = m_instance->instance().codeBlock();
</del><span class="cx">     JSWebAssemblyModule* module = m_instance->module();
</span><span class="cx">     const Wasm::ModuleInformation& moduleInformation = module->moduleInformation();
</span><span class="cx"> 
</span><span class="lines">@@ -143,7 +142,9 @@
</span><span class="cx">         case Wasm::ExternalKind::Exception:
</span><span class="cx">             break;
</span><span class="cx">         case Wasm::ExternalKind::Memory:
</span><del>-            continue;
</del><ins>+            if (creationMode == Wasm::CreationMode::FromJS)
+                continue;
+            break;
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         Identifier moduleName = Identifier::fromString(vm, String::fromUTF8(import.module));
</span><span class="lines">@@ -408,7 +409,46 @@
</span><span class="cx">             break;
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        // Memory initialization will only occur here if the creation mode was through the module loader.
</ins><span class="cx">         case Wasm::ExternalKind::Memory:
</span><ins>+            JSWebAssemblyMemory* memory = jsDynamicCast<JSWebAssemblyMemory*>(vm, value);
+            // i. If v is not a WebAssembly.Memory object, throw a WebAssembly.LinkError.
+            if (!memory)
+                return exception(createJSWebAssemblyLinkError(globalObject, vm, importFailMessage(import, "Memory import", "is not an instance of WebAssembly.Memory")));
+
+            Wasm::PageCount declaredInitial = moduleInformation.memory.initial();
+            Wasm::PageCount importedInitial = memory->memory().initial();
+            if (importedInitial < declaredInitial)
+                return exception(createJSWebAssemblyLinkError(globalObject, vm, importFailMessage(import, "Memory import", "provided an 'initial' that is smaller than the module's declared 'initial' import memory size")));
+
+            if (Wasm::PageCount declaredMaximum = moduleInformation.memory.maximum()) {
+                Wasm::PageCount importedMaximum = memory->memory().maximum();
+                if (!importedMaximum)
+                    return exception(createJSWebAssemblyLinkError(globalObject, vm, importFailMessage(import, "Memory import", "did not have a 'maximum' but the module requires that it does")));
+
+                if (importedMaximum > declaredMaximum)
+                    return exception(createJSWebAssemblyLinkError(globalObject, vm, importFailMessage(import, "Memory import", "provided a 'maximum' that is larger than the module's declared 'maximum' import memory size")));
+            }
+
+            if ((memory->memory().sharingMode() == Wasm::MemorySharingMode::Shared) != moduleInformation.memory.isShared())
+                return exception(createJSWebAssemblyLinkError(globalObject, vm, importFailMessage(import, "Memory import", "provided a 'shared' that is different from the module's declared 'shared' import memory attribute")));
+
+            // ii. Append v to memories.
+            // iii. Append v.[[Memory]] to imports.
+            m_instance->setMemory(vm, memory);
+            RETURN_IF_EXCEPTION(scope, void());
+
+            // Usually at this point the module's code block in any memory mode should be
+            // runnable due to the LLint tier code being shared among all modes. However,
+            // if LLInt is disabled, it is possible that the code needs to be compiled at
+            // this point when we know which memory mode to use.
+            Wasm::CodeBlock* codeBlock = m_instance->instance().codeBlock();
+            if (!codeBlock || !codeBlock->runnable()) {
+                codeBlock = m_instance->module()->module().compileSync(&vm.wasmContext, memory->memory().mode()).ptr();
+                if (!codeBlock->runnable())
+                    return exception(createJSWebAssemblyLinkError(globalObject, vm, codeBlock->errorMessage()));
+            }
+            RELEASE_ASSERT(codeBlock->isSafeToRun(memory->memory().mode()));
</ins><span class="cx">             break;
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="lines">@@ -435,6 +475,9 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    // This needs to be looked up after the memory is initialized, as the codeBlock depends on the memory mode.
+    Wasm::CodeBlock* codeBlock = m_instance->instance().codeBlock();
+
</ins><span class="cx">     for (Wasm::SignatureIndex signatureIndex : moduleInformation.internalExceptionSignatureIndices)
</span><span class="cx">         m_instance->instance().addTag(Wasm::Tag::create(Wasm::SignatureInformation::get(signatureIndex)));
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>