[WebAssembly][Modules] Implement function import from wasm modules
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 Apr 2018 18:00:07 +0000 (18:00 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 17 Apr 2018 18:00:07 +0000 (18:00 +0000)
commitc58390dc1e2379157f2cd4ccc36b330734c5df59
treef0c4c7abbf599535757cac46291dffb4e0e11a83
parentb0e8ecd06abdebb0e706368ddf571ae98c4bf4b5
[WebAssembly][Modules] Implement function import from wasm modules
https://bugs.webkit.org/show_bug.cgi?id=184689

Reviewed by JF Bastien.

JSTests:

* wasm.yaml:
* wasm/modules/js-wasm-cycle.js: Added.
* wasm/modules/js-wasm-cycle/entry.js: Added.
(from.string_appeared_here.export.return42):
* wasm/modules/js-wasm-cycle/sum.wasm: Added.
* wasm/modules/js-wasm-cycle/sum.wat: Added.
* wasm/modules/run-from-wasm.wasm: Added.
* wasm/modules/run-from-wasm.wat: Added.
* wasm/modules/run-from-wasm/check.js: Added.
(export.check):
* wasm/modules/wasm-imports-js-exports.js: Added.
* wasm/modules/wasm-imports-js-exports/imports.wasm: Added.
* wasm/modules/wasm-imports-js-exports/imports.wat: Added.
* wasm/modules/wasm-imports-js-exports/sum.js: Added.
(export.sum):
* wasm/modules/wasm-imports-js-re-exports-wasm-exports.js: Added.
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm: Added.
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat: Added.
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js: Added.
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm: Added.
* wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat: Added.
* wasm/modules/wasm-imports-wasm-exports.js: Added.
* wasm/modules/wasm-imports-wasm-exports/imports.wasm: Added.
* wasm/modules/wasm-imports-wasm-exports/imports.wat: Added.
* wasm/modules/wasm-imports-wasm-exports/sum.wasm: Added.
* wasm/modules/wasm-imports-wasm-exports/sum.wat: Added.
* wasm/modules/wasm-js-cycle.js: Added.
* wasm/modules/wasm-js-cycle/entry.wasm: Added.
* wasm/modules/wasm-js-cycle/entry.wat: Added.
* wasm/modules/wasm-js-cycle/sum.js: Added.
(from.string_appeared_here.export.sum):
* wasm/modules/wasm-wasm-cycle.js: Added.
* wasm/modules/wasm-wasm-cycle/entry.wasm: Added.
* wasm/modules/wasm-wasm-cycle/entry.wat: Added.
* wasm/modules/wasm-wasm-cycle/sum.wasm: Added.
* wasm/modules/wasm-wasm-cycle/sum.wat: Added.

Source/JavaScriptCore:

This patch implements function import from wasm modules. We move function importing part
from JSWebAssemblyInstance's creation function to WebAssemblyModuleRecord::link. This
is because linking these functions requires that all the dependent modules are created.
While we want to move all the linking functionality from JSWebAssemblyInstance to
WebAssemblyModuleRecord::link, we do not that in this patch.  In this patch, we move only
function importing part because efficient compilation of WebAssembly needs to know
the type of WebAssemblyMemory (signaling or bound checking). This needs to know imported
or attached WebAssembly memory object. So we cannot defer this linking to
WebAssemblyModuleRecord::link now.

The largest difference from JS module linking is that WebAssembly module linking links
function from the module by snapshotting. When you have a cyclic module graph like this,

-> JS1 (export "fun") -> Wasm1 (import "fun from JS1) -+
    ^                                                  |
    +--------------------------------------------------+

we fail to link this since "fun" is not instantiated when Wasm1 is first linked. This behavior
is described in [1], and tested in this patch.

[1]: https://github.com/WebAssembly/esm-integration/tree/master/proposals/esm-integration#js---wasm-cycle-where-js-is-higher-in-the-module-graph

* JavaScriptCore.xcodeproj/project.pbxproj:
* jsc.cpp:
(functionDollarAgentStart):
(checkException):
(runWithOptions):
Small fixes for wasm module loading.

* parser/NodesAnalyzeModule.cpp:
(JSC::ImportDeclarationNode::analyzeModule):
* runtime/AbstractModuleRecord.cpp:
(JSC::AbstractModuleRecord::resolveImport):
(JSC::AbstractModuleRecord::link):
* runtime/AbstractModuleRecord.h:
(JSC::AbstractModuleRecord::moduleEnvironmentMayBeNull):
(JSC::AbstractModuleRecord::ImportEntry::isNamespace const): Deleted.
Now, wasm modules can have import which is named "*". So this function does not work.
Since wasm modules never have namespace importing, we check this in JS's module analyzer.

* runtime/JSModuleEnvironment.cpp:
(JSC::JSModuleEnvironment::getOwnNonIndexPropertyNames):
* runtime/JSModuleRecord.cpp:
(JSC::JSModuleRecord::instantiateDeclarations):
* wasm/WasmCreationMode.h: Added.
* wasm/js/JSWebAssemblyInstance.cpp:
(JSC::JSWebAssemblyInstance::finalizeCreation):
(JSC::JSWebAssemblyInstance::create):
* wasm/js/JSWebAssemblyInstance.h:
* wasm/js/WebAssemblyInstanceConstructor.cpp:
(JSC::constructJSWebAssemblyInstance):
* wasm/js/WebAssemblyModuleRecord.cpp:
(JSC::WebAssemblyModuleRecord::link):
* wasm/js/WebAssemblyModuleRecord.h:
* wasm/js/WebAssemblyPrototype.cpp:
(JSC::resolve):
(JSC::instantiate):
(JSC::compileAndInstantiate):
(JSC::WebAssemblyPrototype::instantiate):
(JSC::webAssemblyInstantiateFunc):

Tools:

Add runWebAssemblyDirect, which runs wasm file directly.

* Scripts/run-jsc-stress-tests:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@230720 268f45cc-cd09-0410-ab3c-d52691b4dbfc
56 files changed:
JSTests/ChangeLog
JSTests/wasm.yaml
JSTests/wasm/modules/default-import-star-error.js [new file with mode: 0644]
JSTests/wasm/modules/default-import-star-error/entry.wasm [new file with mode: 0644]
JSTests/wasm/modules/default-import-star-error/entry.wat [new file with mode: 0644]
JSTests/wasm/modules/default-import-star-error/t0.js [new file with mode: 0644]
JSTests/wasm/modules/default-import-star-error/t1.js [new file with mode: 0644]
JSTests/wasm/modules/default-import-star-error/t2.js [new file with mode: 0644]
JSTests/wasm/modules/js-wasm-cycle.js [new file with mode: 0644]
JSTests/wasm/modules/js-wasm-cycle/entry.js [new file with mode: 0644]
JSTests/wasm/modules/js-wasm-cycle/sum.wasm [new file with mode: 0644]
JSTests/wasm/modules/js-wasm-cycle/sum.wat [new file with mode: 0644]
JSTests/wasm/modules/run-from-wasm.wasm [new file with mode: 0644]
JSTests/wasm/modules/run-from-wasm.wat [new file with mode: 0644]
JSTests/wasm/modules/run-from-wasm/check.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-exports.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-exports/imports.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-exports/imports.wat [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-exports/sum.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/imports.wat [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/re-export.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-js-re-exports-wasm-exports/sum.wat [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-wasm-exports.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-wasm-exports/imports.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-wasm-exports/imports.wat [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-wasm-exports/sum.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-imports-wasm-exports/sum.wat [new file with mode: 0644]
JSTests/wasm/modules/wasm-js-cycle.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-js-cycle/entry.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-js-cycle/entry.wat [new file with mode: 0644]
JSTests/wasm/modules/wasm-js-cycle/sum.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-wasm-cycle.js [new file with mode: 0644]
JSTests/wasm/modules/wasm-wasm-cycle/entry.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-wasm-cycle/entry.wat [new file with mode: 0644]
JSTests/wasm/modules/wasm-wasm-cycle/sum.wasm [new file with mode: 0644]
JSTests/wasm/modules/wasm-wasm-cycle/sum.wat [new file with mode: 0644]
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/JavaScriptCore.xcodeproj/project.pbxproj
Source/JavaScriptCore/jsc.cpp
Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp
Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp
Source/JavaScriptCore/runtime/AbstractModuleRecord.h
Source/JavaScriptCore/runtime/JSModuleEnvironment.cpp
Source/JavaScriptCore/runtime/JSModuleRecord.cpp
Source/JavaScriptCore/wasm/WasmCreationMode.h [new file with mode: 0644]
Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp
Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.h
Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp
Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp
Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.h
Source/JavaScriptCore/wasm/js/WebAssemblyPrototype.cpp
Tools/ChangeLog
Tools/Scripts/run-jsc-stress-tests