Ignore:
Timestamp:
Apr 17, 2018, 11:00:07 AM (7 years ago)
Author:
Yusuke Suzuki
Message:

[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

(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:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/jsc.cpp

    r230697 r230720  
    197197template<typename Func>
    198198int runJSC(CommandLine, bool isWorker, const Func&);
    199 static void checkException(GlobalObject*, bool isLastFile, bool hasException, JSValue, CommandLine&, bool& success);
     199static void checkException(ExecState*, GlobalObject*, bool isLastFile, bool hasException, JSValue, CommandLine&, bool& success);
    200200
    201201class Message : public ThreadSafeRefCounted<Message> {
     
    16331633                    if (evaluationException)
    16341634                        result = evaluationException->value();
    1635                     checkException(globalObject, true, evaluationException, result, commandLine, success);
     1635                    checkException(globalObject->globalExec(), globalObject, true, evaluationException, result, commandLine, success);
    16361636                    if (!success)
    16371637                        exit(1);
     
    22602260}
    22612261
    2262 static void checkException(GlobalObject* globalObject, bool isLastFile, bool hasException, JSValue value, CommandLine& options, bool& success)
     2262static void checkException(ExecState* exec, GlobalObject* globalObject, bool isLastFile, bool hasException, JSValue value, CommandLine& options, bool& success)
    22632263{
    22642264    VM& vm = globalObject->vm();
     
    22722272        success = success && !hasException;
    22732273        if (options.m_dump && !hasException)
    2274             printf("End: %s\n", value.toWTFString(globalObject->globalExec()).utf8().data());
     2274            printf("End: %s\n", value.toWTFString(exec).utf8().data());
    22752275        if (hasException)
    22762276            dumpException(globalObject, value);
     
    23252325
    23262326            JSFunction* fulfillHandler = JSNativeStdFunction::create(vm, globalObject, 1, String(), [&, isLastFile](ExecState* exec) {
    2327                 checkException(globalObject, isLastFile, false, exec->argument(0), options, success);
     2327                checkException(exec, globalObject, isLastFile, false, exec->argument(0), options, success);
    23282328                return JSValue::encode(jsUndefined());
    23292329            });
    23302330
    23312331            JSFunction* rejectHandler = JSNativeStdFunction::create(vm, globalObject, 1, String(), [&, isLastFile](ExecState* exec) {
    2332                 checkException(globalObject, isLastFile, true, exec->argument(0), options, success);
     2332                checkException(exec, globalObject, isLastFile, true, exec->argument(0), options, success);
    23332333                return JSValue::encode(jsUndefined());
    23342334            });
     
    23432343            if (evaluationException)
    23442344                returnValue = evaluationException->value();
    2345             checkException(globalObject, isLastFile, evaluationException, returnValue, options, success);
     2345            checkException(globalObject->globalExec(), globalObject, isLastFile, evaluationException, returnValue, options, success);
    23462346        }
    23472347
Note: See TracChangeset for help on using the changeset viewer.