Skip to content

Add C/JS APIs to copy expressions #2840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,11 @@ void BinaryenExpressionPrint(BinaryenExpressionRef expr) {
std::cout << '\n';
}

BinaryenExpressionRef BinaryenExpressionCopy(BinaryenExpressionRef expr,
BinaryenModuleRef module) {
return ExpressionManipulator::copy(expr, *(Module*)module);
}

// Specific expression utility

// Block
Expand Down
2 changes: 2 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,8 @@ BINARYEN_API BinaryenExpressionId
BinaryenExpressionGetId(BinaryenExpressionRef expr);
BINARYEN_API BinaryenType BinaryenExpressionGetType(BinaryenExpressionRef expr);
BINARYEN_API void BinaryenExpressionPrint(BinaryenExpressionRef expr);
BINARYEN_API BinaryenExpressionRef
BinaryenExpressionCopy(BinaryenExpressionRef expr, BinaryenModuleRef module);

BINARYEN_API const char* BinaryenBlockGetName(BinaryenExpressionRef expr);
BINARYEN_API BinaryenIndex
Expand Down
3 changes: 3 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2407,6 +2407,9 @@ function wrapModule(module, self) {
self['setDebugLocation'] = function(func, expr, fileIndex, lineNumber, columnNumber) {
return Module['_BinaryenFunctionSetDebugLocation'](func, expr, fileIndex, lineNumber, columnNumber);
};
self['copyExpression'] = function(expr) {
return Module['_BinaryenExpressionCopy'](expr, module);
};

return self;
}
Expand Down
31 changes: 31 additions & 0 deletions test/binaryen.js/copy-expression.js
Original file line number Diff line number Diff line change
@@ -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));
Empty file.