summaryrefslogtreecommitdiff
path: root/test/binaryen.js
diff options
context:
space:
mode:
authorHeejin Ahn <aheejin@gmail.com>2019-05-13 10:33:55 -0700
committerGitHub <noreply@github.com>2019-05-13 10:33:55 -0700
commit67019f9a72175bc7a098d72aa14a6f6afeb2efce (patch)
tree83adc3d4a235c00f6bf545fc5aeeeadb8f1a5663 /test/binaryen.js
parenta1ff274b6bca0ff8d1635c32a6d206863f0a2fc3 (diff)
downloadbinaryen-67019f9a72175bc7a098d72aa14a6f6afeb2efce.tar.gz
binaryen-67019f9a72175bc7a098d72aa14a6f6afeb2efce.tar.bz2
binaryen-67019f9a72175bc7a098d72aa14a6f6afeb2efce.zip
Add missing methods for globals to binaryen.js (#2099)
- Print `globals` array in the tracing mode like other arrays (`functions`, `exports`, `imports`, ...) - Add accessor functions for globals
Diffstat (limited to 'test/binaryen.js')
-rw-r--r--test/binaryen.js/functions.js2
-rw-r--r--test/binaryen.js/functions.js.txt1
-rw-r--r--test/binaryen.js/global.js33
-rw-r--r--test/binaryen.js/global.js.txt14
-rw-r--r--test/binaryen.js/kitchen-sink.js10
-rw-r--r--test/binaryen.js/kitchen-sink.js.txt21
6 files changed, 74 insertions, 7 deletions
diff --git a/test/binaryen.js/functions.js b/test/binaryen.js/functions.js
index a02a7873a..3c57a6232 100644
--- a/test/binaryen.js/functions.js
+++ b/test/binaryen.js/functions.js
@@ -33,8 +33,6 @@ console.log(Binaryen.emitText(funcInfo.body));
module.removeFunction("a-function");
-module.addGlobal("a-global", Binaryen.i32, false, funcInfo.body);
-
module.validate();
console.log(module.emitText());
diff --git a/test/binaryen.js/functions.js.txt b/test/binaryen.js/functions.js.txt
index 58558bcd5..15c719555 100644
--- a/test/binaryen.js/functions.js.txt
+++ b/test/binaryen.js/functions.js.txt
@@ -6,6 +6,5 @@ getExpressionInfo(body)={"id":14,"value":3}
(module
(type $i (func (result i32)))
- (global $a-global i32 (i32.const 3))
)
diff --git a/test/binaryen.js/global.js b/test/binaryen.js/global.js
new file mode 100644
index 000000000..f24331d82
--- /dev/null
+++ b/test/binaryen.js/global.js
@@ -0,0 +1,33 @@
+function cleanInfo(info) {
+ var ret = {};
+ for (var x in info) {
+ if (x !== 'name' && x !== 'body' && x !== 'type' && x !== 'init') {
+ ret[x] = info[x];
+ }
+ }
+ return ret;
+}
+
+var module = new Binaryen.Module();
+
+var initExpr = module.i32.const(1);
+var global = module.addGlobal("a-global", Binaryen.i32, false, initExpr);
+
+var globalInfo = Binaryen.getGlobalInfo(global);
+console.log("getGlobalInfo=" + JSON.stringify(cleanInfo(globalInfo)));
+
+var initExpInfo = Binaryen.getExpressionInfo(globalInfo.init);
+console.log("getExpressionInfo(init)=" + JSON.stringify(cleanInfo(initExpInfo)));
+console.log(Binaryen.emitText(globalInfo.init));
+
+module.addGlobalExport("a-global", "a-global-exp");
+module.addGlobalImport("a-global-imp", "module", "base", Binaryen.i32);
+
+module.validate();
+console.log(module.emitText());
+
+module.removeGlobal("a-global");
+module.removeExport("a-global-exp");
+
+module.validate();
+console.log(module.emitText());
diff --git a/test/binaryen.js/global.js.txt b/test/binaryen.js/global.js.txt
new file mode 100644
index 000000000..49a0a4b0b
--- /dev/null
+++ b/test/binaryen.js/global.js.txt
@@ -0,0 +1,14 @@
+getGlobalInfo={"module":"","base":"","mutable":false}
+getExpressionInfo(init)={"id":14,"value":1}
+(i32.const 1)
+
+(module
+ (import "module" "base" (global $a-global-imp i32))
+ (global $a-global i32 (i32.const 1))
+ (export "a-global-exp" (global $a-global))
+)
+
+(module
+ (import "module" "base" (global $a-global-imp i32))
+)
+
diff --git a/test/binaryen.js/kitchen-sink.js b/test/binaryen.js/kitchen-sink.js
index 8b80e52ef..9e376e436 100644
--- a/test/binaryen.js/kitchen-sink.js
+++ b/test/binaryen.js/kitchen-sink.js
@@ -407,14 +407,20 @@ function test_core() {
// Create the function
var sinker = module.addFunction("kitchen()sinker", iiIfF, [ Binaryen.i32 ], body);
+ // Create a global
+ var initExpr = module.i32.const(1);
+ var global = module.addGlobal("a-global", Binaryen.i32, false, initExpr)
+
// Imports
var fiF = module.addFunctionType("fiF", Binaryen.f32, [ Binaryen.i32, Binaryen.f64 ]);
module.addFunctionImport("an-imported", "module", "base", fiF);
+ module.addGlobalImport("a-global-imp", "module", "base", Binaryen.i32);
// Exports
module.addFunctionExport("kitchen()sinker", "kitchen_sinker");
+ module.addGlobalExport("a-global", "a-global-exp");
// Function table. One per module
@@ -666,6 +672,8 @@ function test_binaries() {
y = module.getLocal(1, Binaryen.i32);
var add = module.i32.add(x, y);
var adder = module.addFunction("adder", iii, [], add);
+ var initExpr = module.i32.const(3);
+ var global = module.addGlobal("a-global", Binaryen.i32, false, initExpr)
Binaryen.setDebugInfo(true); // include names section
buffer = module.emitBinary();
Binaryen.setDebugInfo(false);
@@ -736,6 +744,8 @@ function test_parsing() {
y = module.getLocal(1, Binaryen.i32);
var add = module.i32.add(x, y);
var adder = module.addFunction("adder", iii, [], add);
+ var initExpr = module.i32.const(3);
+ var global = module.addGlobal("a-global", Binaryen.i32, false, initExpr)
text = module.emitText();
module.dispose();
module = null;
diff --git a/test/binaryen.js/kitchen-sink.js.txt b/test/binaryen.js/kitchen-sink.js.txt
index d13519f09..a1e8df2b4 100644
--- a/test/binaryen.js/kitchen-sink.js.txt
+++ b/test/binaryen.js/kitchen-sink.js.txt
@@ -57,13 +57,16 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
(type $fiF (func (param i32 f64) (result f32)))
(type $v (func))
(type $3 (func))
+ (import "module" "base" (global $a-global-imp i32))
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
(memory $0 1 256)
(data (i32.const 10) "hello, world")
(data passive "I am passive")
(table $0 1 funcref)
(elem (i32.const 0) "$kitchen()sinker")
+ (global $a-global i32 (i32.const 1))
(export "kitchen_sinker" (func "$kitchen()sinker"))
+ (export "a-global-exp" (global $a-global))
(export "mem" (memory $0))
(start $starter)
(func "$kitchen()sinker" (; 1 ;) (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
@@ -1886,6 +1889,7 @@ optimized:
module loaded from binary form:
(module
(type $0 (func (param i32 i32) (result i32)))
+ (global $global$0 i32 (i32.const 3))
(func $adder (; 0 ;) (type $0) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(local.get $0)
@@ -3327,12 +3331,16 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
BinaryenType varTypes[] = { 1 };
functions[0] = BinaryenAddFunction(the_module, "kitchen()sinker", functionTypes[0], varTypes, 1, expressions[655]);
}
+ expressions[656] = BinaryenConst(the_module, BinaryenLiteralInt32(1));
+ globals[0] = BinaryenAddGlobal(the_module, "a-global", 1, 0, expressions[656]);
{
BinaryenType paramTypes[] = { 1, 4 };
functionTypes[1] = BinaryenAddFunctionType(the_module, "fiF", 3, paramTypes, 2);
}
BinaryenAddFunctionImport(the_module, "an-imported", "module", "base", functionTypes[1]);
+ BinaryenAddGlobalImport(the_module, "a-global-imp", "module", "base", 1);
exports[0] = BinaryenAddFunctionExport(the_module, "kitchen()sinker", "kitchen_sinker");
+ exports[1] = BinaryenAddGlobalExport(the_module, "a-global", "a-global-exp");
BinaryenFunctionGetName(functions[0]);
BinaryenFunctionImportGetModule(functions[0]);
BinaryenFunctionImportGetBase(functions[0]);
@@ -3350,13 +3358,13 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
const char* funcNames[] = { "kitchen()sinker" };
BinaryenSetFunctionTable(the_module, 1, 4294967295, funcNames, 1);
}
- expressions[656] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
+ expressions[657] = BinaryenConst(the_module, BinaryenLiteralInt32(10));
{
const char segment0[] = { 104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100 };
const char segment1[] = { 73, 32, 97, 109, 32, 112, 97, 115, 115, 105, 118, 101 };
const char* segments[] = { segment0, segment1 };
int8_t segmentPassive[] = { 0, 1 };
- BinaryenExpressionRef segmentOffsets[] = { expressions[656], expressions[0] };
+ BinaryenExpressionRef segmentOffsets[] = { expressions[657], expressions[0] };
BinaryenIndex segmentSizes[] = { 12, 12 };
BinaryenSetMemory(the_module, 1, 256, "mem", segments, segmentPassive, segmentOffsets, segmentSizes, 2, 0);
}
@@ -3364,10 +3372,10 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
BinaryenType paramTypes[] = { 0 };
functionTypes[2] = BinaryenAddFunctionType(the_module, "v", 0, paramTypes, 0);
}
- expressions[657] = BinaryenNop(the_module);
+ expressions[658] = BinaryenNop(the_module);
{
BinaryenType varTypes[] = { 0 };
- functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[2], varTypes, 0, expressions[657]);
+ functions[1] = BinaryenAddFunction(the_module, "starter", functionTypes[2], varTypes, 0, expressions[658]);
}
BinaryenSetStart(the_module, functions[1]);
{
@@ -3382,13 +3390,16 @@ getExpressionInfo(f64.const)={"id":14,"type":4,"value":9.5}
(type $fiF (func (param i32 f64) (result f32)))
(type $v (func))
(type $3 (func))
+ (import "module" "base" (global $a-global-imp i32))
(import "module" "base" (func $an-imported (param i32 f64) (result f32)))
(memory $0 1 256)
(data (i32.const 10) "hello, world")
(data passive "I am passive")
(table $0 1 funcref)
(elem (i32.const 0) "$kitchen()sinker")
+ (global $a-global i32 (i32.const 1))
(export "kitchen_sinker" (func "$kitchen()sinker"))
+ (export "a-global-exp" (global $a-global))
(export "mem" (memory $0))
(start $starter)
(func "$kitchen()sinker" (; 1 ;) (type $iiIfF) (param $0 i32) (param $1 i64) (param $2 f32) (param $3 f64) (result i32)
@@ -5691,6 +5702,7 @@ optimized:
test_parsing text:
(module
(type $iii (func (param i32 i32) (result i32)))
+ (global $a-global i32 (i32.const 3))
(func $adder (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(local.get $0)
@@ -5702,6 +5714,7 @@ test_parsing text:
module loaded from text form:
(module
(type $iii (func (param i32 i32) (result i32)))
+ (global $a-global i32 (i32.const 3))
(func $ADD_ER (; 0 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(local.get $0)