Changeset 189846 in webkit


Ignore:
Timestamp:
Sep 15, 2015 10:49:11 PM (9 years ago)
Author:
commit-queue@webkit.org
Message:

Implement imported global variables in WebAssembly
https://bugs.webkit.org/show_bug.cgi?id=149206

Patch by Sukolsak Sakshuwong <Sukolsak Sakshuwong> on 2015-09-15
Reviewed by Filip Pizlo.

Values can now be imported to a WebAssembly module through properties of
the imports object that is passed to loadWebAssembly(). In order to
avoid any side effect when accessing the imports object, we check that
the properties are data properties. We also check that each value is a
primitive and is not a Symbol. According to the ECMA262 6.0 spec,
calling ToNumber() on a primitive that is not a Symbol should not cause
any side effect.[1]

[1]: http://www.ecma-international.org/ecma-262/6.0/#sec-tonumber

  • tests/stress/wasm-globals.js:
  • tests/stress/wasm/globals.wasm:
  • wasm/WASMModuleParser.cpp:

(JSC::WASMModuleParser::parseModule):
(JSC::WASMModuleParser::parseGlobalSection):

  • wasm/WASMModuleParser.h:
Location:
trunk/Source/JavaScriptCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r189844 r189846  
     12015-09-15  Sukolsak Sakshuwong  <sukolsak@gmail.com>
     2
     3        Implement imported global variables in WebAssembly
     4        https://bugs.webkit.org/show_bug.cgi?id=149206
     5
     6        Reviewed by Filip Pizlo.
     7
     8        Values can now be imported to a WebAssembly module through properties of
     9        the imports object that is passed to loadWebAssembly(). In order to
     10        avoid any side effect when accessing the imports object, we check that
     11        the properties are data properties. We also check that each value is a
     12        primitive and is not a Symbol. According to the ECMA262 6.0 spec,
     13        calling ToNumber() on a primitive that is not a Symbol should not cause
     14        any side effect.[1]
     15
     16        [1]: http://www.ecma-international.org/ecma-262/6.0/#sec-tonumber
     17
     18        * tests/stress/wasm-globals.js:
     19        * tests/stress/wasm/globals.wasm:
     20        * wasm/WASMModuleParser.cpp:
     21        (JSC::WASMModuleParser::parseModule):
     22        (JSC::WASMModuleParser::parseGlobalSection):
     23        * wasm/WASMModuleParser.h:
     24
    1252015-09-15  Sukolsak Sakshuwong  <sukolsak@gmail.com>
    226
  • trunk/Source/JavaScriptCore/tests/stress/wasm-globals.js

    r189844 r189846  
    99wasm/globals.wasm is generated by pack-asmjs <https://github.com/WebAssembly/polyfill-prototype-1> from the following script:
    1010
    11 function asmModule(global, env, buffer) {
     11function asmModule(global, imports, buffer) {
    1212    "use asm";
    1313
    1414    var fround = global.Math.fround;
     15    var a = imports.a | 0;
     16    var b = fround(imports.b);
     17    var c = +imports.c;
    1518    var x = 0;
    1619    var y = fround(0);
    1720    var z = 0.0;
     21
     22    function getA() {
     23        return a | 0;
     24    }
     25
     26    function getB() {
     27        return b;
     28    }
     29
     30    function getC() {
     31        return c;
     32    }
    1833
    1934    function getX() {
     
    4560
    4661    return {
     62        getA: getA,
     63        getB: getB,
     64        getC: getC,
    4765        getX: getX,
    4866        getY: getY,
     
    5573*/
    5674
    57 var module = loadWebAssembly("wasm/globals.wasm");
     75var imports = {
     76    a: 42,
     77    b: 4.2,
     78    c: 4.2,
     79};
     80var module = loadWebAssembly("wasm/globals.wasm", imports);
     81
     82shouldBe(module.getA(), 42);
     83shouldBe(module.getB(), 4.199999809265137);
     84shouldBe(module.getC(), 4.2);
    5885
    5986shouldBe(module.getX(), 0);
  • trunk/Source/JavaScriptCore/wasm/WASMModuleParser.cpp

    r189822 r189846  
    8585    parseFunctionImportSection(exec);
    8686    PROPAGATE_ERROR();
    87     parseGlobalSection();
     87    parseGlobalSection(exec);
    8888    PROPAGATE_ERROR();
    8989    parseFunctionDeclarationSection();
     
    182182}
    183183
    184 void WASMModuleParser::parseGlobalSection()
     184void WASMModuleParser::parseGlobalSection(ExecState* exec)
    185185{
    186186    uint32_t numberOfInternalI32GlobalVariables;
     
    219219        READ_STRING_OR_FAIL(importName, "Cannot read the import name of an int32 global variable.");
    220220        globalVariableTypes.uncheckedAppend(WASMType::I32);
    221         globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0)); // FIXME: Import the value.
     221        JSValue value;
     222        getImportedValue(exec, importName, value);
     223        PROPAGATE_ERROR();
     224        FAIL_IF_FALSE(value.isPrimitive() && !value.isSymbol(), "\"" + importName + "\" is not a primitive or is a Symbol.");
     225        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(value.toInt32(exec)));
    222226    }
    223227    for (uint32_t i = 0; i < numberOfImportedF32GlobalVariables; ++i) {
     
    225229        READ_STRING_OR_FAIL(importName, "Cannot read the import name of a float32 global variable.");
    226230        globalVariableTypes.uncheckedAppend(WASMType::F32);
    227         globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0f)); // FIXME: Import the value.
     231        JSValue value;
     232        getImportedValue(exec, importName, value);
     233        PROPAGATE_ERROR();
     234        FAIL_IF_FALSE(value.isPrimitive() && !value.isSymbol(), "\"" + importName + "\" is not a primitive or is a Symbol.");
     235        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(static_cast<float>(value.toNumber(exec))));
    228236    }
    229237    for (uint32_t i = 0; i < numberOfImportedF64GlobalVariables; ++i) {
     
    231239        READ_STRING_OR_FAIL(importName, "Cannot read the import name of a float64 global variable.");
    232240        globalVariableTypes.uncheckedAppend(WASMType::F64);
    233         globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0)); // FIXME: Import the value.
     241        JSValue value;
     242        getImportedValue(exec, importName, value);
     243        PROPAGATE_ERROR();
     244        FAIL_IF_FALSE(value.isPrimitive() && !value.isSymbol(), "\"" + importName + "\" is not a primitive or is a Symbol.");
     245        globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(value.toNumber(exec)));
    234246    }
    235247}
  • trunk/Source/JavaScriptCore/wasm/WASMModuleParser.h

    r189822 r189846  
    5151    void parseSignatureSection();
    5252    void parseFunctionImportSection(ExecState*);
    53     void parseGlobalSection();
     53    void parseGlobalSection(ExecState*);
    5454    void parseFunctionDeclarationSection();
    5555    void parseFunctionPointerTableSection();
Note: See TracChangeset for help on using the changeset viewer.