Changeset 209171 in webkit


Ignore:
Timestamp:
Nov 30, 2016 7:54:24 PM (7 years ago)
Author:
Yusuke Suzuki
Message:

WebAssembly JS API: export a module namespace object instead of a module environment
https://bugs.webkit.org/show_bug.cgi?id=165121

Reviewed by Saam Barati.

This patch setup AbstractModuleRecord further for WebAssemblyModuleRecord.
For exported entries in a wasm instance, we set up exported entries for
AbstractModuleRecord. This allows us to export WASM exported functions in
the module handling code.

Since the exported entries in the abstract module record are correctly
instantiated, the module namespace object for WASM module also starts
working correctly. So we start exposing the module namespace object
as instance.exports instead of the module environment object.

And we move SourceCode, lexicalVariables, and declaredVariables fields to
JSModuleRecord since they are related to JS source code (in the spec words,
they are related to the source text module record).

  • runtime/AbstractModuleRecord.cpp:

(JSC::AbstractModuleRecord::AbstractModuleRecord):

  • runtime/AbstractModuleRecord.h:

(JSC::AbstractModuleRecord::sourceCode): Deleted.
(JSC::AbstractModuleRecord::declaredVariables): Deleted.
(JSC::AbstractModuleRecord::lexicalVariables): Deleted.

  • runtime/JSModuleRecord.cpp:

(JSC::JSModuleRecord::JSModuleRecord):

  • runtime/JSModuleRecord.h:

(JSC::JSModuleRecord::sourceCode):
(JSC::JSModuleRecord::declaredVariables):
(JSC::JSModuleRecord::lexicalVariables):

  • wasm/WasmFormat.cpp:
  • wasm/js/JSWebAssemblyInstance.cpp:

(JSC::JSWebAssemblyInstance::finishCreation):

  • wasm/js/WebAssemblyFunction.cpp:
  • wasm/js/WebAssemblyInstanceConstructor.cpp:

(JSC::constructJSWebAssemblyInstance):

  • wasm/js/WebAssemblyModuleRecord.cpp:

(JSC::WebAssemblyModuleRecord::create):
(JSC::WebAssemblyModuleRecord::WebAssemblyModuleRecord):
(JSC::WebAssemblyModuleRecord::finishCreation):
WebAssemblyModuleRecord::link should perform linking things.
So allocating exported entries should be done here.
(JSC::WebAssemblyModuleRecord::link):

  • wasm/js/WebAssemblyModuleRecord.h:
Location:
trunk/Source/JavaScriptCore
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r209162 r209171  
     12016-11-30  Yusuke Suzuki  <utatane.tea@gmail.com>
     2
     3        WebAssembly JS API: export a module namespace object instead of a module environment
     4        https://bugs.webkit.org/show_bug.cgi?id=165121
     5
     6        Reviewed by Saam Barati.
     7
     8        This patch setup AbstractModuleRecord further for WebAssemblyModuleRecord.
     9        For exported entries in a wasm instance, we set up exported entries for
     10        AbstractModuleRecord. This allows us to export WASM exported functions in
     11        the module handling code.
     12
     13        Since the exported entries in the abstract module record are correctly
     14        instantiated, the module namespace object for WASM module also starts
     15        working correctly. So we start exposing the module namespace object
     16        as `instance.exports` instead of the module environment object.
     17
     18        And we move SourceCode, lexicalVariables, and declaredVariables fields to
     19        JSModuleRecord since they are related to JS source code (in the spec words,
     20        they are related to the source text module record).
     21
     22        * runtime/AbstractModuleRecord.cpp:
     23        (JSC::AbstractModuleRecord::AbstractModuleRecord):
     24        * runtime/AbstractModuleRecord.h:
     25        (JSC::AbstractModuleRecord::sourceCode): Deleted.
     26        (JSC::AbstractModuleRecord::declaredVariables): Deleted.
     27        (JSC::AbstractModuleRecord::lexicalVariables): Deleted.
     28        * runtime/JSModuleRecord.cpp:
     29        (JSC::JSModuleRecord::JSModuleRecord):
     30        * runtime/JSModuleRecord.h:
     31        (JSC::JSModuleRecord::sourceCode):
     32        (JSC::JSModuleRecord::declaredVariables):
     33        (JSC::JSModuleRecord::lexicalVariables):
     34        * wasm/WasmFormat.cpp:
     35        * wasm/js/JSWebAssemblyInstance.cpp:
     36        (JSC::JSWebAssemblyInstance::finishCreation):
     37        * wasm/js/WebAssemblyFunction.cpp:
     38        * wasm/js/WebAssemblyInstanceConstructor.cpp:
     39        (JSC::constructJSWebAssemblyInstance):
     40        * wasm/js/WebAssemblyModuleRecord.cpp:
     41        (JSC::WebAssemblyModuleRecord::create):
     42        (JSC::WebAssemblyModuleRecord::WebAssemblyModuleRecord):
     43        (JSC::WebAssemblyModuleRecord::finishCreation):
     44        WebAssemblyModuleRecord::link should perform linking things.
     45        So allocating exported entries should be done here.
     46        (JSC::WebAssemblyModuleRecord::link):
     47        * wasm/js/WebAssemblyModuleRecord.h:
     48
    1492016-11-30  Mark Lam  <mark.lam@apple.com>
    250
  • trunk/Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp

    r209123 r209171  
    3939const ClassInfo AbstractModuleRecord::s_info = { "AbstractModuleRecord", &Base::s_info, 0, CREATE_METHOD_TABLE(AbstractModuleRecord) };
    4040
    41 AbstractModuleRecord::AbstractModuleRecord(VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables)
     41AbstractModuleRecord::AbstractModuleRecord(VM& vm, Structure* structure, const Identifier& moduleKey)
    4242    : Base(vm, structure)
    4343    , m_moduleKey(moduleKey)
    44     , m_sourceCode(sourceCode)
    45     , m_declaredVariables(declaredVariables)
    46     , m_lexicalVariables(lexicalVariables)
    4744{
    4845}
  • trunk/Source/JavaScriptCore/runtime/AbstractModuleRecord.h

    r209123 r209171  
    8989    std::optional<ExportEntry> tryGetExportEntry(UniquedStringImpl* exportName);
    9090
    91     const SourceCode& sourceCode() const { return m_sourceCode; }
    9291    const Identifier& moduleKey() const { return m_moduleKey; }
    9392    const OrderedIdentifierSet& requestedModules() const { return m_requestedModules; }
     
    9594    const ImportEntries& importEntries() const { return m_importEntries; }
    9695    const OrderedIdentifierSet& starExportEntries() const { return m_starExportEntries; }
    97 
    98     const VariableEnvironment& declaredVariables() const { return m_declaredVariables; }
    99     const VariableEnvironment& lexicalVariables() const { return m_lexicalVariables; }
    10096
    10197    void dump();
     
    127123
    128124protected:
    129     AbstractModuleRecord(VM&, Structure*, const Identifier&, const SourceCode&, const VariableEnvironment&, const VariableEnvironment&);
     125    AbstractModuleRecord(VM&, Structure*, const Identifier&);
    130126    void finishCreation(ExecState*, VM&);
    131127
     
    143139    // The loader resolves the given module name to the module key. The module key is the unique value to represent this module.
    144140    Identifier m_moduleKey;
    145 
    146     SourceCode m_sourceCode;
    147 
    148     VariableEnvironment m_declaredVariables;
    149     VariableEnvironment m_lexicalVariables;
    150141
    151142    // Currently, we don't keep the occurrence order of the import / export entries.
  • trunk/Source/JavaScriptCore/runtime/JSModuleRecord.cpp

    r209123 r209171  
    5151}
    5252JSModuleRecord::JSModuleRecord(VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables)
    53     : Base(vm, structure, moduleKey, sourceCode, declaredVariables, lexicalVariables)
     53    : Base(vm, structure, moduleKey)
     54    , m_sourceCode(sourceCode)
     55    , m_declaredVariables(declaredVariables)
     56    , m_lexicalVariables(lexicalVariables)
    5457{
    5558}
  • trunk/Source/JavaScriptCore/runtime/JSModuleRecord.h

    r209123 r209171  
    5252    JS_EXPORT_PRIVATE JSValue evaluate(ExecState*);
    5353
     54    const SourceCode& sourceCode() const { return m_sourceCode; }
     55    const VariableEnvironment& declaredVariables() const { return m_declaredVariables; }
     56    const VariableEnvironment& lexicalVariables() const { return m_lexicalVariables; }
     57
    5458    ModuleProgramExecutable* moduleProgramExecutable() const { return m_moduleProgramExecutable.get(); }
    5559
     
    6468    void instantiateDeclarations(ExecState*, ModuleProgramExecutable*);
    6569
     70    SourceCode m_sourceCode;
     71    VariableEnvironment m_declaredVariables;
     72    VariableEnvironment m_lexicalVariables;
    6673    WriteBarrier<ModuleProgramExecutable> m_moduleProgramExecutable;
    6774};
  • trunk/Source/JavaScriptCore/wasm/WasmFormat.cpp

    r208401 r209171  
    3232#include "WasmMemory.h"
    3333
     34#if COMPILER(GCC) && ASSERT_DISABLED
     35#pragma GCC diagnostic push
     36#pragma GCC diagnostic ignored "-Wreturn-type"
     37#endif // COMPILER(GCC) && ASSERT_DISABLED
     38
    3439namespace JSC { namespace Wasm {
    3540
     
    5459} } // namespace JSC::Wasm
    5560
     61#if COMPILER(GCC) && ASSERT_DISABLED
     62#pragma GCC diagnostic pop
     63#endif // COMPILER(GCC) && ASSERT_DISABLED
     64
    5665#endif // ENABLE(WEBASSEMBLY)
  • trunk/Source/JavaScriptCore/wasm/js/JSWebAssemblyInstance.cpp

    r209123 r209171  
    5757{
    5858    Base::finishCreation(vm);
     59    ASSERT(inherits(info()));
    5960    m_module.set(vm, this, module);
    6061    m_moduleNamespaceObject.set(vm, this, moduleNamespaceObject);
    61     // FIXME this should put the module namespace object onto the exports object, instead of moduleEnvironment in WebAssemblyInstanceConstructor. https://bugs.webkit.org/show_bug.cgi?id=165121
    62     ASSERT(inherits(info()));
     62    putDirect(vm, Identifier::fromString(&vm, "exports"), moduleNamespaceObject, None);
    6363}
    6464
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyFunction.cpp

    r209123 r209171  
    3434#include "JSObject.h"
    3535#include "JSWebAssemblyInstance.h"
    36 #include "LLintThunks.h"
     36#include "LLIntThunks.h"
    3737#include "ProtoCallFrame.h"
    3838#include "VM.h"
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyInstanceConstructor.cpp

    r209123 r209171  
    7373
    7474    Identifier moduleKey = Identifier::fromUid(PrivateName(PrivateName::Description, "WebAssemblyInstance"));
    75     SourceCode sourceCode;
    76     VariableEnvironment declaredVariables;
    77     VariableEnvironment lexicalVariables;
    78     WebAssemblyModuleRecord* moduleRecord = WebAssemblyModuleRecord::create(state, vm, globalObject->webAssemblyModuleRecordStructure(), moduleKey, sourceCode, declaredVariables, lexicalVariables);
     75    WebAssemblyModuleRecord* moduleRecord = WebAssemblyModuleRecord::create(state, vm, globalObject->webAssemblyModuleRecordStructure(), moduleKey, moduleInformation);
    7976    RETURN_IF_EXCEPTION(scope, encodedJSValue());
    8077
     
    8986    if (verbose)
    9087        moduleRecord->dump();
    91     // FIXME the following should be in JSWebAssemblyInstance instead of here. https://bugs.webkit.org/show_bug.cgi?id=165121
    92     instance->putDirect(vm, Identifier::fromString(&vm, "exports"), JSValue(moduleRecord->moduleEnvironment()), None);
    9388    JSValue startResult = moduleRecord->evaluate(state);
    9489    UNUSED_PARAM(startResult);
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.cpp

    r209123 r209171  
    4747}
    4848
    49 WebAssemblyModuleRecord* WebAssemblyModuleRecord::create(ExecState* exec, VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables)
     49WebAssemblyModuleRecord* WebAssemblyModuleRecord::create(ExecState* exec, VM& vm, Structure* structure, const Identifier& moduleKey, const Wasm::ModuleInformation& moduleInformation)
    5050{
    51     WebAssemblyModuleRecord* instance = new (NotNull, allocateCell<WebAssemblyModuleRecord>(vm.heap)) WebAssemblyModuleRecord(vm, structure, moduleKey, sourceCode, declaredVariables, lexicalVariables);
    52     instance->finishCreation(exec, vm);
     51    WebAssemblyModuleRecord* instance = new (NotNull, allocateCell<WebAssemblyModuleRecord>(vm.heap)) WebAssemblyModuleRecord(vm, structure, moduleKey);
     52    instance->finishCreation(exec, vm, moduleInformation);
    5353    return instance;
    5454}
    5555
    56 WebAssemblyModuleRecord::WebAssemblyModuleRecord(VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables)
    57     : Base(vm, structure, moduleKey, sourceCode, declaredVariables, lexicalVariables)
     56WebAssemblyModuleRecord::WebAssemblyModuleRecord(VM& vm, Structure* structure, const Identifier& moduleKey)
     57    : Base(vm, structure, moduleKey)
    5858{
    5959}
     
    6565}
    6666
    67 void WebAssemblyModuleRecord::finishCreation(ExecState* exec, VM& vm)
     67void WebAssemblyModuleRecord::finishCreation(ExecState* exec, VM& vm, const Wasm::ModuleInformation& moduleInformation)
    6868{
    6969    Base::finishCreation(exec, vm);
    7070    ASSERT(inherits(info()));
     71    for (const auto& exp : moduleInformation.exports) {
     72        switch (exp.kind) {
     73        case Wasm::External::Function: {
     74            addExportEntry(ExportEntry::createLocal(exp.field, exp.field));
     75            break;
     76        }
     77        case Wasm::External::Table: {
     78            // FIXME https://bugs.webkit.org/show_bug.cgi?id=164135
     79            break;
     80        }
     81        case Wasm::External::Memory: {
     82            // FIXME https://bugs.webkit.org/show_bug.cgi?id=164134
     83            break;
     84        }
     85        case Wasm::External::Global: {
     86            // FIXME https://bugs.webkit.org/show_bug.cgi?id=164133
     87            // In the MVP, only immutable global variables can be exported.
     88            break;
     89        }
     90        }
     91    }
    7192}
    7293
     
    93114    // Let exports be a list of (string, JS value) pairs that is mapped from each external value e in instance.exports as follows:
    94115    JSModuleEnvironment* moduleEnvironment = JSModuleEnvironment::create(vm, globalObject, nullptr, exportSymbolTable, JSValue(), this);
    95     unsigned offset = 0;
    96116    for (const auto& exp : moduleInformation.exports) {
    97117        JSValue exportedValue;
    98         PutPropertySlot slot(this);
    99         slot.setNewProperty(this, offset++);
    100118        switch (exp.kind) {
    101119        case Wasm::External::Function: {
  • trunk/Source/JavaScriptCore/wasm/js/WebAssemblyModuleRecord.h

    r209123 r209171  
    2929
    3030#include "AbstractModuleRecord.h"
     31#include "WasmFormat.h"
    3132
    3233namespace JSC {
     
    4445
    4546    static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
    46     static WebAssemblyModuleRecord* create(ExecState*, VM&, Structure*, const Identifier&, const SourceCode&, const VariableEnvironment&, const VariableEnvironment&);
     47    static WebAssemblyModuleRecord* create(ExecState*, VM&, Structure*, const Identifier&, const Wasm::ModuleInformation&);
    4748
    4849    void link(ExecState*, JSWebAssemblyInstance*);
     
    5051
    5152private:
    52     WebAssemblyModuleRecord(VM&, Structure*, const Identifier&, const SourceCode&, const VariableEnvironment&, const VariableEnvironment&);
     53    WebAssemblyModuleRecord(VM&, Structure*, const Identifier&);
    5354
    54     void finishCreation(ExecState*, VM&);
     55    void finishCreation(ExecState*, VM&, const Wasm::ModuleInformation&);
    5556    static void destroy(JSCell*);
    5657
Note: See TracChangeset for help on using the changeset viewer.