- Revision
- 189846
- Author
- commit-qu...@webkit.org
- Date
- 2015-09-15 22:49:11 -0700 (Tue, 15 Sep 2015)
Log Message
Implement imported global variables in WebAssembly
https://bugs.webkit.org/show_bug.cgi?id=149206
Patch by Sukolsak Sakshuwong <sukol...@gmail.com> 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:
Modified Paths
Diff
Modified: trunk/Source/_javascript_Core/ChangeLog (189845 => 189846)
--- trunk/Source/_javascript_Core/ChangeLog 2015-09-16 04:22:21 UTC (rev 189845)
+++ trunk/Source/_javascript_Core/ChangeLog 2015-09-16 05:49:11 UTC (rev 189846)
@@ -1,5 +1,29 @@
2015-09-15 Sukolsak Sakshuwong <sukol...@gmail.com>
+ Implement imported global variables in WebAssembly
+ https://bugs.webkit.org/show_bug.cgi?id=149206
+
+ 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:
+
+2015-09-15 Sukolsak Sakshuwong <sukol...@gmail.com>
+
Fix asm.js errors in WebAssembly tests
https://bugs.webkit.org/show_bug.cgi?id=149203
Modified: trunk/Source/_javascript_Core/tests/stress/wasm/globals.wasm (189845 => 189846)
--- trunk/Source/_javascript_Core/tests/stress/wasm/globals.wasm 2015-09-16 04:22:21 UTC (rev 189845)
+++ trunk/Source/_javascript_Core/tests/stress/wasm/globals.wasm 2015-09-16 05:49:11 UTC (rev 189846)
@@ -1 +1 @@
-wasm\xD2\x80\x80\x80\x80\xA0\xC0\x80\xA1\xA0\x80\xA2\xA0getXgetYgetZsetXsetYsetZ
\ No newline at end of file
+wasmmabc \x80\x80\x80\x80\x80\x80\x80\xA0\xC0\x80\xA1\xA0\x80\xA2\xA0 getAgetBgetCgetXgetYgetZsetXsetYsetZ
\ No newline at end of file
Modified: trunk/Source/_javascript_Core/tests/stress/wasm-globals.js (189845 => 189846)
--- trunk/Source/_javascript_Core/tests/stress/wasm-globals.js 2015-09-16 04:22:21 UTC (rev 189845)
+++ trunk/Source/_javascript_Core/tests/stress/wasm-globals.js 2015-09-16 05:49:11 UTC (rev 189846)
@@ -8,14 +8,29 @@
/*
wasm/globals.wasm is generated by pack-asmjs <https://github.com/WebAssembly/polyfill-prototype-1> from the following script:
-function asmModule(global, env, buffer) {
+function asmModule(global, imports, buffer) {
"use asm";
var fround = global.Math.fround;
+ var a = imports.a | 0;
+ var b = fround(imports.b);
+ var c = +imports.c;
var x = 0;
var y = fround(0);
var z = 0.0;
+ function getA() {
+ return a | 0;
+ }
+
+ function getB() {
+ return b;
+ }
+
+ function getC() {
+ return c;
+ }
+
function getX() {
return x | 0;
}
@@ -44,6 +59,9 @@
}
return {
+ getA: getA,
+ getB: getB,
+ getC: getC,
getX: getX,
getY: getY,
getZ: getZ,
@@ -54,8 +72,17 @@
}
*/
-var module = loadWebAssembly("wasm/globals.wasm");
+var imports = {
+ a: 42,
+ b: 4.2,
+ c: 4.2,
+};
+var module = loadWebAssembly("wasm/globals.wasm", imports);
+shouldBe(module.getA(), 42);
+shouldBe(module.getB(), 4.199999809265137);
+shouldBe(module.getC(), 4.2);
+
shouldBe(module.getX(), 0);
shouldBe(module.getY(), 0);
shouldBe(module.getZ(), 0);
Modified: trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp (189845 => 189846)
--- trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp 2015-09-16 04:22:21 UTC (rev 189845)
+++ trunk/Source/_javascript_Core/wasm/WASMModuleParser.cpp 2015-09-16 05:49:11 UTC (rev 189846)
@@ -84,7 +84,7 @@
PROPAGATE_ERROR();
parseFunctionImportSection(exec);
PROPAGATE_ERROR();
- parseGlobalSection();
+ parseGlobalSection(exec);
PROPAGATE_ERROR();
parseFunctionDeclarationSection();
PROPAGATE_ERROR();
@@ -181,7 +181,7 @@
FAIL_IF_FALSE(m_module->functionImportSignatures().size() == numberOfFunctionImportSignatures, "The number of function import signatures is incorrect.");
}
-void WASMModuleParser::parseGlobalSection()
+void WASMModuleParser::parseGlobalSection(ExecState* exec)
{
uint32_t numberOfInternalI32GlobalVariables;
uint32_t numberOfInternalF32GlobalVariables;
@@ -218,19 +218,31 @@
String importName;
READ_STRING_OR_FAIL(importName, "Cannot read the import name of an int32 global variable.");
globalVariableTypes.uncheckedAppend(WASMType::I32);
- globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0)); // FIXME: Import the value.
+ JSValue value;
+ getImportedValue(exec, importName, value);
+ PROPAGATE_ERROR();
+ FAIL_IF_FALSE(value.isPrimitive() && !value.isSymbol(), "\"" + importName + "\" is not a primitive or is a Symbol.");
+ globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(value.toInt32(exec)));
}
for (uint32_t i = 0; i < numberOfImportedF32GlobalVariables; ++i) {
String importName;
READ_STRING_OR_FAIL(importName, "Cannot read the import name of a float32 global variable.");
globalVariableTypes.uncheckedAppend(WASMType::F32);
- globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0f)); // FIXME: Import the value.
+ JSValue value;
+ getImportedValue(exec, importName, value);
+ PROPAGATE_ERROR();
+ FAIL_IF_FALSE(value.isPrimitive() && !value.isSymbol(), "\"" + importName + "\" is not a primitive or is a Symbol.");
+ globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(static_cast<float>(value.toNumber(exec))));
}
for (uint32_t i = 0; i < numberOfImportedF64GlobalVariables; ++i) {
String importName;
READ_STRING_OR_FAIL(importName, "Cannot read the import name of a float64 global variable.");
globalVariableTypes.uncheckedAppend(WASMType::F64);
- globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(0.0)); // FIXME: Import the value.
+ JSValue value;
+ getImportedValue(exec, importName, value);
+ PROPAGATE_ERROR();
+ FAIL_IF_FALSE(value.isPrimitive() && !value.isSymbol(), "\"" + importName + "\" is not a primitive or is a Symbol.");
+ globalVariables.uncheckedAppend(JSWASMModule::GlobalVariable(value.toNumber(exec)));
}
}
Modified: trunk/Source/_javascript_Core/wasm/WASMModuleParser.h (189845 => 189846)
--- trunk/Source/_javascript_Core/wasm/WASMModuleParser.h 2015-09-16 04:22:21 UTC (rev 189845)
+++ trunk/Source/_javascript_Core/wasm/WASMModuleParser.h 2015-09-16 05:49:11 UTC (rev 189846)
@@ -50,7 +50,7 @@
void parseConstantPoolSection();
void parseSignatureSection();
void parseFunctionImportSection(ExecState*);
- void parseGlobalSection();
+ void parseGlobalSection(ExecState*);
void parseFunctionDeclarationSection();
void parseFunctionPointerTableSection();
void parseFunctionDefinitionSection();