Skip to content

Commit 655fd6b

Browse files
authored
Add C/JS APIs to copy expressions (WebAssembly#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)
1 parent 3de8c98 commit 655fd6b

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

src/binaryen-c.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,11 @@ void BinaryenExpressionPrint(BinaryenExpressionRef expr) {
13191319
std::cout << '\n';
13201320
}
13211321

1322+
BinaryenExpressionRef BinaryenExpressionCopy(BinaryenExpressionRef expr,
1323+
BinaryenModuleRef module) {
1324+
return ExpressionManipulator::copy(expr, *(Module*)module);
1325+
}
1326+
13221327
// Specific expression utility
13231328

13241329
// Block

src/binaryen-c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ BINARYEN_API BinaryenExpressionId
848848
BinaryenExpressionGetId(BinaryenExpressionRef expr);
849849
BINARYEN_API BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr);
850850
BINARYEN_API void BinaryenExpressionPrint(BinaryenExpressionRef expr);
851+
BINARYEN_API BinaryenExpressionRef
852+
BinaryenExpressionCopy(BinaryenExpressionRef expr, BinaryenModuleRef module);
851853

852854
BINARYEN_API const char* BinaryenBlockGetName(BinaryenExpressionRef expr);
853855
BINARYEN_API BinaryenIndex

src/js/binaryen.js-post.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,9 @@ function wrapModule(module, self) {
24072407
self['setDebugLocation'] = function(func, expr, fileIndex, lineNumber, columnNumber) {
24082408
return Module['_BinaryenFunctionSetDebugLocation'](func, expr, fileIndex, lineNumber, columnNumber);
24092409
};
2410+
self['copyExpression'] = function(expr) {
2411+
return Module['_BinaryenExpressionCopy'](expr, module);
2412+
};
24102413

24112414
return self;
24122415
}

test/binaryen.js/copy-expression.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var module = new binaryen.Module();
2+
3+
// Create an expression and copy it
4+
var original = module.block(null, [
5+
module.if(
6+
module.local.get(0, binaryen.i32),
7+
module.i32.const(1),
8+
module.i32.const(2)
9+
)
10+
], binaryen.i32);
11+
var copy = module.copyExpression(original);
12+
13+
// Check that the expression incl. sub-expressions are copies
14+
assert(original !== copy);
15+
16+
var originalInfo = binaryen.getExpressionInfo(original);
17+
assert(originalInfo.children.length === 1);
18+
19+
var copyInfo = binaryen.getExpressionInfo(copy);
20+
assert(originalInfo.children.length === copyInfo.children.length);
21+
assert(originalInfo.children[0] !== copyInfo.children[0]);
22+
23+
var originalIfInfo = binaryen.getExpressionInfo(originalInfo.children[0]);
24+
var copyIfInfo = binaryen.getExpressionInfo(copyInfo.children[0]);
25+
26+
assert(originalIfInfo.condition !== copyIfInfo.condition);
27+
assert(originalIfInfo.ifTrue !== copyIfInfo.ifTrue);
28+
assert(originalIfInfo.ifFalse !== copyIfInfo.ifFalse);
29+
30+
// Check that both are otherwise identical
31+
assert(binaryen.emitText(original) === binaryen.emitText(copy));

test/binaryen.js/copy-expression.js.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)