Ignore:
Timestamp:
Dec 14, 2016, 1:29:14 PM (9 years ago)
Author:
[email protected]
Message:

WebAssembly JS API: implement Global
https://bugs.webkit.org/show_bug.cgi?id=164133

Reviewed by Saam Barati.

JSTests:

  • wasm/Builder.js:

(export.default.Builder.prototype._registerSectionBuilders.switch.case.string_appeared_here.this.section):

  • wasm/Builder_WebAssemblyBinary.js:

(const.valueType.WASM.description.type.i32.type.const.putGlobalType):
(const.putOp):
(const.putInitExpr):
(const.emitters.Import):
(const.emitters.Global):
(const.emitters.Export):
(const.emitters.Code):

  • wasm/LowLevelBinary.js:

(export.default.LowLevelBinary.prototype.varuint32):
(export.default.LowLevelBinary.prototype.varint32):

  • wasm/js-api/global-error.js: Added.

(catch):
(assert.truthy):

  • wasm/js-api/global-external-init-from-import.js: Added.
  • wasm/js-api/global-internal-init-from-import.js: Added.
  • wasm/js-api/global-mutate.js: Added.

(createInternalGlobalModule):

  • wasm/js-api/globals-export.js: Added.
  • wasm/js-api/globals-import.js: Added.
  • wasm/wasm.json:

Source/JavaScriptCore:

This patch adds support for globals. It handles imports, exports
and internal globals. In the MVP only internal globals are allowed
to be mutable. This means we can store a C-array of 64-bit slots
off the instance holding them. When globals are exported to JS
they are done so as numbers. This means that i64 globals cannot be
imported or exported.

  • wasm/WasmB3IRGenerator.cpp:

(JSC::Wasm::B3IRGenerator::B3IRGenerator):
(JSC::Wasm::B3IRGenerator::getGlobal):
(JSC::Wasm::B3IRGenerator::setGlobal):
(JSC::Wasm::B3IRGenerator::addCallIndirect):
(JSC::Wasm::parseAndCompile):

  • wasm/WasmFormat.h:
  • wasm/WasmFunctionParser.h:

(JSC::Wasm::FunctionParser<Context>::parseExpression):

  • wasm/WasmModuleParser.cpp:

(JSC::Wasm::ModuleParser::parseImport):
(JSC::Wasm::ModuleParser::parseGlobal):
(JSC::Wasm::ModuleParser::parseExport):
(JSC::Wasm::ModuleParser::parseElement):
(JSC::Wasm::ModuleParser::parseInitExpr):
(JSC::Wasm::ModuleParser::parseGlobalType):
(JSC::Wasm::ModuleParser::parseData):

  • wasm/WasmModuleParser.h:
  • wasm/WasmParser.h:

(JSC::Wasm::Parser::parseVarInt32):
(JSC::Wasm::Parser::parseVarInt64):
(JSC::Wasm::Parser::parseUInt64):

  • wasm/WasmValidate.cpp:

(JSC::Wasm::Validate::hasMemory):
(JSC::Wasm::Validate::Validate):
(JSC::Wasm::Validate::getGlobal):
(JSC::Wasm::Validate::setGlobal):
(JSC::Wasm::validateFunction):

  • wasm/generateWasmOpsHeader.py:
  • wasm/js/JSWebAssemblyInstance.cpp:

(JSC::JSWebAssemblyInstance::create):
(JSC::JSWebAssemblyInstance::finishCreation):
(JSC::JSWebAssemblyInstance::visitChildren):

  • wasm/js/JSWebAssemblyInstance.h:

(JSC::JSWebAssemblyInstance::loadI32Global):
(JSC::JSWebAssemblyInstance::loadI64Global):
(JSC::JSWebAssemblyInstance::loadF32Global):
(JSC::JSWebAssemblyInstance::loadF64Global):
(JSC::JSWebAssemblyInstance::setGlobal):
(JSC::JSWebAssemblyInstance::offsetOfGlobals):

  • wasm/js/WebAssemblyInstanceConstructor.cpp:

(JSC::constructJSWebAssemblyInstance):

  • wasm/js/WebAssemblyModuleRecord.cpp:

(JSC::WebAssemblyModuleRecord::finishCreation):
(JSC::WebAssemblyModuleRecord::link):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/wasm/WasmValidate.cpp

    r209652 r209830  
    8787    bool WARN_UNUSED_RETURN setLocal(uint32_t index, ExpressionType value);
    8888
     89    // Globals
     90    bool WARN_UNUSED_RETURN getGlobal(uint32_t index, ExpressionType& result);
     91    bool WARN_UNUSED_RETURN setGlobal(uint32_t index, ExpressionType value);
     92
    8993    // Memory
    9094    bool WARN_UNUSED_RETURN load(LoadOpType, ExpressionType pointer, ExpressionType& result, uint32_t offset);
     
    117121    void dump(const Vector<ControlEntry>& controlStack, const ExpressionList& expressionStack);
    118122
    119     bool hasMemory() const { return !!m_memory; }
     123    bool hasMemory() const { return !!m_module.memory; }
    120124
    121125    void setErrorMessage(String&& message) { ASSERT(m_errorMessage.isNull()); m_errorMessage = WTFMove(message); }
    122126    String errorMessage() const { return m_errorMessage; }
    123     Validate(ExpressionType returnType, const MemoryInformation& memory)
     127    Validate(ExpressionType returnType, const ModuleInformation& module)
    124128        : m_returnType(returnType)
    125         , m_memory(memory)
     129        , m_module(module)
    126130    {
    127131    }
     
    136140    Vector<Type> m_locals;
    137141    String m_errorMessage;
    138     const MemoryInformation& m_memory;
     142    const ModuleInformation& m_module;
    139143};
    140144
     
    178182
    179183    m_errorMessage = makeString("Attempt to set local with type: ", toString(localType), " with a variable of type: ", toString(value));
     184    return false;
     185}
     186
     187bool Validate::getGlobal(uint32_t index, ExpressionType& result)
     188{
     189    if (index < m_module.globals.size()) {
     190        result = m_module.globals[index].type;
     191        ASSERT(isValueType(result));
     192        return true;
     193    }
     194    m_errorMessage = ASCIILiteral("Attempt to use unknown global.");
     195    return false;
     196}
     197
     198bool Validate::setGlobal(uint32_t index, ExpressionType value)
     199{
     200    if (index >= m_module.globals.size()) {
     201        m_errorMessage = ASCIILiteral("Attempt to use unknown global.");
     202        return false;
     203    }
     204
     205    if (m_module.globals[index].mutability == Global::Immutable) {
     206        m_errorMessage = ASCIILiteral("Attempt to store to immutable global.");
     207        return false;
     208    }
     209
     210    ExpressionType globalType = m_module.globals[index].type;
     211    ASSERT(isValueType(globalType));
     212    if (globalType == value)
     213        return true;
     214
     215    m_errorMessage = makeString("Attempt to set global with type: ", toString(globalType), " with a variable of type: ", toString(value));
    180216    return false;
    181217}
     
    402438}
    403439
    404 String validateFunction(const uint8_t* source, size_t length, const Signature* signature, const ImmutableFunctionIndexSpace& functionIndexSpace, const ModuleInformation& info)
    405 {
    406     Validate context(signature->returnType, info.memory);
    407     FunctionParser<Validate> validator(context, source, length, signature, functionIndexSpace, info);
     440String validateFunction(const uint8_t* source, size_t length, const Signature* signature, const ImmutableFunctionIndexSpace& functionIndexSpace, const ModuleInformation& module)
     441{
     442    Validate context(signature->returnType, module);
     443    FunctionParser<Validate> validator(context, source, length, signature, functionIndexSpace, module);
    408444
    409445    if (!validator.parse()) {
Note: See TracChangeset for help on using the changeset viewer.