Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -2465,6 +2465,12 @@ function wrapModule(module, self = {}) {
}
};

self['call_ref'] = function(target, operands, type, isReturn) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like call_indirect handles return calls differently - there is self.return_call_indirect instead of a param. But I see we have the same difference in the C API, so this PR is following that.

I think we might want to change the C API, but for now, how about making the JS API consistent with itself? That is, add self.return_call_ref.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made that change. Might be smart to open an issue for tracking the change on the c api.

return preserveStack(() =>
Module['_BinaryenCallRef'](module, target, i32sToStack(operands), operands.length, type, isReturn)
);
};

self['any'] = {
'convert_extern'() {
return Module['_BinaryenRefAsAnyConvertExtern']();
Expand Down Expand Up @@ -5146,6 +5152,39 @@ Module['I31Get'] = makeExpressionWrapper(Module['_BinaryenI31GetId'](), {
}
});

Module['CallRef'] = makeExpressionWrapper(Module['_BinaryenCallRefId'](), {
'getNumOperands'(expr) {
return Module['_BinaryenCallRefGetNumOperands'](expr);
},
'getOperandAt'(expr, index) {
return Module['_BinaryenCallRefGetOperandAt'](expr, index);
},
'setOperandAt'(expr, index, operandExpr) {
Module['_BinaryenCallRefSetOperandAt'](expr, index, operandExpr);
},
'appendOperand'(expr, operandExpr) {
return Module['_BinaryenCallRefAppendOperand'](expr, operandExpr);
},
'insertOperandAt'(expr, index, operandExpr) {
Module['_BinaryenCallRefInsertOperandAt'](expr, index, operandExpr);
},
'removeOperandAt'(expr, index) {
return Module['_BinaryenCallRefRemoveOperandAt'](expr, index);
},
'getTarget'(expr) {
return Module['_BinaryenCallRefGetTarget'](expr);
},
'setTarget'(expr, targetExpr) {
Module['_BinaryenCallRefSetTarget'](expr, targetExpr);
},
'isReturn'(expr) {
return Boolean(Module['_BinaryenCallRefIsReturn'](expr));
},
'setReturn'(expr, isReturn) {
Module['_BinaryenCallRefSetReturn'](expr, isReturn);
}
});

// Function wrapper

Module['Function'] = (() => {
Expand Down
87 changes: 87 additions & 0 deletions test/binaryen.js/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3409,3 +3409,90 @@ console.log("# I31Get");

module.dispose();
})();

console.log("# CallRef");
(function testCallRef() {
const module = new binaryen.Module();

const funcName = "tiny";
module.addFunction(funcName, binaryen.createType([binaryen.i32, binaryen.i32]), binaryen.none, [], module.nop());
const funcType = binaryen.Function(module.getFunction(funcName)).type;
const funcRef = binaryen.RefFunc(module.ref.func(funcName, funcType));

const operands = [
module.i32.const(6),
module.i32.const(7)
];

const theCallRef = binaryen.CallRef(module.call_ref(funcRef, operands, binaryen.none, false));
assert(theCallRef instanceof binaryen.CallRef);
assert(theCallRef instanceof binaryen.Expression);
assert(theCallRef.numOperands === operands.length);
assert(theCallRef.type === binaryen.none);
assert(binaryen.RefFunc(theCallRef.target).func === funcName);

const info = binaryen.getExpressionInfo(theCallRef);
assert(info.id === theCallRef.id);
assert(info.type === theCallRef.type);
assert(info.target === theCallRef.target);
assert(info.isReturn === theCallRef.isReturn());

assert(theCallRef.getNumOperands() === operands.length);

assert(theCallRef.getOperandAt(0) === operands[0]);
assert(theCallRef.getOperandAt(1) === operands[1]);

theCallRef.setOperandAt(0, operands[1]);
assert(theCallRef.getOperandAt(0), operands[1]);
theCallRef.setOperandAt(0, operands[0]);
assert(theCallRef.getOperandAt(0), operands[0]);

const newOperand = module.i32.const(8);
theCallRef.appendOperand(newOperand);
assert(theCallRef.getNumOperands() == 3);
assert(theCallRef.getOperandAt(2) === newOperand);

theCallRef.removeOperandAt(2);
assert(theCallRef.getNumOperands() == 2);
assert(theCallRef.getOperandAt(0) === operands[0]);
assert(theCallRef.getOperandAt(1) === operands[1]);

theCallRef.insertOperandAt(1, newOperand);
assert(theCallRef.getNumOperands() == 3);
assert(theCallRef.getOperandAt(0) === operands[0]);
assert(theCallRef.getOperandAt(1) === newOperand);
assert(theCallRef.getOperandAt(2) === operands[1]);

theCallRef.removeOperandAt(1);
assert(theCallRef.getNumOperands() == 2);
assert(theCallRef.getOperandAt(0) === operands[0]);
assert(theCallRef.getOperandAt(1) === operands[1]);

assert(theCallRef.isReturn() === false);
theCallRef.setReturn(true);
assert(theCallRef.isReturn() === true);
theCallRef.setReturn(false);
assert(theCallRef.isReturn() === false);


const targetRef = binaryen.RefFunc(theCallRef.getTarget());
assert(theCallRef.getTarget() == theCallRef.target);
assert(targetRef.func === funcName);

const newTargetName = "newTarget";
const newTargetRef = binaryen.RefFunc(module.ref.func(newTargetName, funcType));
theCallRef.setTarget(newTargetRef);
assert(binaryen.RefFunc(theCallRef.getTarget()).func === newTargetName);

theCallRef.setTarget(funcRef);
assert(binaryen.RefFunc(theCallRef.getTarget()).func === funcName);

console.log(theCallRef.toText());
assert(
theCallRef.toText()
==
"(call_ref $func.0\n (i32.const 6)\n (i32.const 7)\n (ref.func $tiny)\n)\n"
);

module.dispose();
})();
6 changes: 6 additions & 0 deletions test/binaryen.js/expressions.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,9 @@
(local.get $2)
)

# CallRef
(call_ref $func.0
(i32.const 6)
(i32.const 7)
(ref.func $tiny)
)