Ignore:
Timestamp:
Apr 16, 2018, 7:38:59 PM (7 years ago)
Author:
Yusuke Suzuki
Message:

[WebAssembly][Modules] Prototype wasm import
https://bugs.webkit.org/show_bug.cgi?id=184600

Reviewed by JF Bastien.

JSTests:

Add wasm and wat files since module loader want to load wasm files from FS.
Currently, importing the other modules from wasm is not supported.

  • wasm.yaml:
  • wasm/modules/constant.wasm: Added.
  • wasm/modules/constant.wat: Added.
  • wasm/modules/js-wasm-function-namespace.js: Added.

(assert.throws):

  • wasm/modules/js-wasm-function.js: Added.

(assert.throws):

  • wasm/modules/js-wasm-global-namespace.js: Added.

(assert.throws):

  • wasm/modules/js-wasm-global.js: Added.

(assert.throws):

  • wasm/modules/js-wasm-memory-namespace.js: Added.

(assert.throws):

  • wasm/modules/js-wasm-memory.js: Added.

(assert.throws):

  • wasm/modules/js-wasm-start.js: Added.

(then):

  • wasm/modules/js-wasm-table-namespace.js: Added.

(assert.throws):

  • wasm/modules/js-wasm-table.js: Added.

(assert.throws):

  • wasm/modules/memory.wasm: Added.
  • wasm/modules/memory.wat: Added.
  • wasm/modules/start.wasm: Added.
  • wasm/modules/start.wat: Added.
  • wasm/modules/sum.wasm: Added.
  • wasm/modules/sum.wat: Added.
  • wasm/modules/table.wasm: Added.
  • wasm/modules/table.wat: Added.

Source/JavaScriptCore:

This patch is an initial attempt to implement Wasm loading in module pipeline.
Currently,

  1. We only support Wasm loading in the JSC shell. Once loading mechanism is specified in whatwg HTML, we should integrate this into WebCore.
  1. We only support exporting values from Wasm. Wasm module cannot import anything from the other modules now.

When loading a file, JSC shell checks wasm magic. If the wasm magic is found, JSC shell
loads the file with WebAssemblySourceProvider. It is wrapped into JSSourceCode and
module loader pipeline just handles it as the same to JS. When parsing a module, we
checks the type of JSSourceCode. If the source code is Wasm source code, we create a
WebAssemblyModuleRecord instead of JSModuleRecord. Our module pipeline handles
AbstractModuleRecord and Wasm module is instantiated, linked, and evaluated.

  • builtins/ModuleLoaderPrototype.js:

(globalPrivate.newRegistryEntry):
(requestInstantiate):
(link):

  • jsc.cpp:

(convertShebangToJSComment):
(fillBufferWithContentsOfFile):
(fetchModuleFromLocalFileSystem):
(GlobalObject::moduleLoaderFetch):

  • parser/SourceProvider.h:

(JSC::WebAssemblySourceProvider::create):
(JSC::WebAssemblySourceProvider::WebAssemblySourceProvider):

  • runtime/AbstractModuleRecord.cpp:

(JSC::AbstractModuleRecord::hostResolveImportedModule):
(JSC::AbstractModuleRecord::link):
(JSC::AbstractModuleRecord::evaluate):
(JSC::identifierToJSValue): Deleted.

  • runtime/AbstractModuleRecord.h:
  • runtime/JSModuleLoader.cpp:

(JSC::JSModuleLoader::evaluate):

  • runtime/JSModuleRecord.cpp:

(JSC::JSModuleRecord::link):
(JSC::JSModuleRecord::instantiateDeclarations):

  • runtime/JSModuleRecord.h:
  • runtime/ModuleLoaderPrototype.cpp:

(JSC::moduleLoaderPrototypeParseModule):
(JSC::moduleLoaderPrototypeRequestedModules):
(JSC::moduleLoaderPrototypeModuleDeclarationInstantiation):

  • wasm/js/JSWebAssemblyHelpers.h:

(JSC::getWasmBufferFromValue):
(JSC::createSourceBufferFromValue):

  • wasm/js/JSWebAssemblyInstance.cpp:

(JSC::JSWebAssemblyInstance::finalizeCreation):
(JSC::JSWebAssemblyInstance::createPrivateModuleKey):
(JSC::JSWebAssemblyInstance::create):

  • wasm/js/JSWebAssemblyInstance.h:
  • wasm/js/WebAssemblyInstanceConstructor.cpp:

(JSC::constructJSWebAssemblyInstance):

  • wasm/js/WebAssemblyModuleRecord.cpp:

(JSC::WebAssemblyModuleRecord::prepareLink):
(JSC::WebAssemblyModuleRecord::link):

  • wasm/js/WebAssemblyModuleRecord.h:
  • wasm/js/WebAssemblyPrototype.cpp:

(JSC::resolve):
(JSC::instantiate):
(JSC::compileAndInstantiate):
(JSC::WebAssemblyPrototype::instantiate):
(JSC::webAssemblyInstantiateFunc):
(JSC::webAssemblyValidateFunc):

  • wasm/js/WebAssemblyPrototype.h:
File:
1 edited

Legend:

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

    r229605 r230697  
    187187}
    188188
    189 static bool fillBufferWithContentsOfFile(const String& fileName, Vector<char>& buffer);
     189template<typename Vector>
     190static bool fillBufferWithContentsOfFile(const String& fileName, Vector& buffer);
    190191static RefPtr<Uint8Array> fillBufferWithContentsOfFile(const String& fileName);
    191192
     
    845846}
    846847
    847 static void convertShebangToJSComment(Vector<char>& buffer)
     848template<typename Vector>
     849static void convertShebangToJSComment(Vector& buffer)
    848850{
    849851    if (buffer.size() >= 2) {
     
    883885}
    884886
    885 static bool fillBufferWithContentsOfFile(FILE* file, Vector<char>& buffer)
     887template<typename Vector>
     888static bool fillBufferWithContentsOfFile(FILE* file, Vector& buffer)
    886889{
    887890    // We might have injected "use strict"; at the top.
     
    921924}
    922925
    923 static bool fetchModuleFromLocalFileSystem(const String& fileName, Vector<char>& buffer)
     926template<typename Vector>
     927static bool fetchModuleFromLocalFileSystem(const String& fileName, Vector& buffer)
    924928{
    925929    // We assume that fileName is always an absolute path.
     
    972976
    973977    // Here, now we consider moduleKey as the fileName.
    974     Vector<char> utf8;
    975     if (!fetchModuleFromLocalFileSystem(moduleKey, utf8)) {
     978    Vector<uint8_t> buffer;
     979    if (!fetchModuleFromLocalFileSystem(moduleKey, buffer)) {
    976980        auto result = deferred->reject(exec, createError(exec, makeString("Could not open file '", moduleKey, "'.")));
    977981        scope.releaseAssertNoException();
     
    979983    }
    980984
    981     auto result = deferred->resolve(exec, JSSourceCode::create(vm, makeSource(stringFromUTF(utf8), SourceOrigin { moduleKey }, moduleKey, TextPosition(), SourceProviderSourceType::Module)));
     985#if ENABLE(WEBASSEMBLY)
     986    // FileSystem does not have mime-type header. The JSC shell recognizes WebAssembly's magic header.
     987    if (buffer.size() >= 4) {
     988        if (buffer[0] == '\0' && buffer[1] == 'a' && buffer[2] == 's' && buffer[3] == 'm') {
     989            auto result = deferred->resolve(exec, JSSourceCode::create(vm, SourceCode(WebAssemblySourceProvider::create(WTFMove(buffer), SourceOrigin { moduleKey }, moduleKey))));
     990            scope.releaseAssertNoException();
     991            return result;
     992        }
     993    }
     994#endif
     995
     996    auto result = deferred->resolve(exec, JSSourceCode::create(vm, makeSource(stringFromUTF(buffer), SourceOrigin { moduleKey }, moduleKey, TextPosition(), SourceProviderSourceType::Module)));
    982997    scope.releaseAssertNoException();
    983998    return result;
Note: See TracChangeset for help on using the changeset viewer.