diff options
author | Daniel Wirtz <dcode@dcode.io> | 2020-05-11 20:48:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-11 11:48:51 -0700 |
commit | 655fd6b5cd1e3dd7441139cfca95a905004d2209 (patch) | |
tree | 58c688490493b5beb2ba3b273d13668cb4888065 /test/binaryen.js/copy-expression.js | |
parent | 3de8c98b682e1347e5c50c58eaddc4b01f3e26ab (diff) | |
download | binaryen-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.js | 31 |
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)); |