summaryrefslogtreecommitdiff
path: root/test/binaryen.js/copy-expression.js
diff options
context:
space:
mode:
authorDaniel Wirtz <dcode@dcode.io>2020-05-11 20:48:51 +0200
committerGitHub <noreply@github.com>2020-05-11 11:48:51 -0700
commit655fd6b5cd1e3dd7441139cfca95a905004d2209 (patch)
tree58c688490493b5beb2ba3b273d13668cb4888065 /test/binaryen.js/copy-expression.js
parent3de8c98b682e1347e5c50c58eaddc4b01f3e26ab (diff)
downloadbinaryen-655fd6b5cd1e3dd7441139cfca95a905004d2209.tar.gz
binaryen-655fd6b5cd1e3dd7441139cfca95a905004d2209.tar.bz2
binaryen-655fd6b5cd1e3dd7441139cfca95a905004d2209.zip
Add C/JS APIs to copy expressions (#2840)
This API enables use cases where we want to keep the original expression, yet utilize passes like `vacuum` or `precompute` to evaluate it without implicitly modifying the original. C-API: **BinaryenExpressionCopy**(expr, module) JS-API: **Module#copyExpression**(expr)
Diffstat (limited to 'test/binaryen.js/copy-expression.js')
-rw-r--r--test/binaryen.js/copy-expression.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/test/binaryen.js/copy-expression.js b/test/binaryen.js/copy-expression.js
new file mode 100644
index 000000000..261c028fa
--- /dev/null
+++ b/test/binaryen.js/copy-expression.js
@@ -0,0 +1,31 @@
+var module = new binaryen.Module();
+
+// Create an expression and copy it
+var original = module.block(null, [
+ module.if(
+ module.local.get(0, binaryen.i32),
+ module.i32.const(1),
+ module.i32.const(2)
+ )
+], binaryen.i32);
+var copy = module.copyExpression(original);
+
+// Check that the expression incl. sub-expressions are copies
+assert(original !== copy);
+
+var originalInfo = binaryen.getExpressionInfo(original);
+assert(originalInfo.children.length === 1);
+
+var copyInfo = binaryen.getExpressionInfo(copy);
+assert(originalInfo.children.length === copyInfo.children.length);
+assert(originalInfo.children[0] !== copyInfo.children[0]);
+
+var originalIfInfo = binaryen.getExpressionInfo(originalInfo.children[0]);
+var copyIfInfo = binaryen.getExpressionInfo(copyInfo.children[0]);
+
+assert(originalIfInfo.condition !== copyIfInfo.condition);
+assert(originalIfInfo.ifTrue !== copyIfInfo.ifTrue);
+assert(originalIfInfo.ifFalse !== copyIfInfo.ifFalse);
+
+// Check that both are otherwise identical
+assert(binaryen.emitText(original) === binaryen.emitText(copy));