Skip to content

Commit c3726f6

Browse files
committed
feat: Add CallRef functions to js api.
1 parent 3ff3762 commit c3726f6

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

src/js/binaryen.js-post.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,6 +2465,12 @@ function wrapModule(module, self = {}) {
24652465
}
24662466
};
24672467

2468+
self['call_ref'] = function(target, operands, type, isReturn) {
2469+
return preserveStack(() =>
2470+
Module['_BinaryenCallRef'](module, target, i32sToStack(operands), operands.length, type, isReturn)
2471+
);
2472+
};
2473+
24682474
self['any'] = {
24692475
'convert_extern'() {
24702476
return Module['_BinaryenRefAsAnyConvertExtern']();
@@ -5146,6 +5152,39 @@ Module['I31Get'] = makeExpressionWrapper(Module['_BinaryenI31GetId'](), {
51465152
}
51475153
});
51485154

5155+
Module['CallRef'] = makeExpressionWrapper(Module['_BinaryenCallRefId'](), {
5156+
'getNumOperands'(expr) {
5157+
return Module['_BinaryenCallRefGetNumOperands'](expr);
5158+
},
5159+
'getOperandAt'(expr, index) {
5160+
return Module['_BinaryenCallRefGetOperandAt'](expr, index);
5161+
},
5162+
'setOperandAt'(expr, index, operandExpr) {
5163+
Module['_BinaryenCallRefSetOperandAt'](expr, index, operandExpr);
5164+
},
5165+
'appendOperand'(expr, operandExpr) {
5166+
return Module['_BinaryenCallRefAppendOperand'](expr, operandExpr);
5167+
},
5168+
'insertOperandAt'(expr, index, operandExpr) {
5169+
Module['_BinaryenCallRefInsertOperandAt'](expr, index, operandExpr);
5170+
},
5171+
'removeOperandAt'(expr, index) {
5172+
return Module['_BinaryenCallRefRemoveOperandAt'](expr, index);
5173+
},
5174+
'getTarget'(expr) {
5175+
return Module['_BinaryenCallRefGetTarget'](expr);
5176+
},
5177+
'setTarget'(expr, targetExpr) {
5178+
Module['_BinaryenCallRefSetTarget'](expr, targetExpr);
5179+
},
5180+
'isReturn'(expr) {
5181+
return Boolean(Module['_BinaryenCallRefIsReturn'](expr));
5182+
},
5183+
'setReturn'(expr, isReturn) {
5184+
Module['_BinaryenCallRefSetReturn'](expr, isReturn);
5185+
}
5186+
});
5187+
51495188
// Function wrapper
51505189

51515190
Module['Function'] = (() => {

test/binaryen.js/expressions.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,3 +3409,90 @@ console.log("# I31Get");
34093409

34103410
module.dispose();
34113411
})();
3412+
3413+
console.log("# CallRef");
3414+
(function testCallRef() {
3415+
const module = new binaryen.Module();
3416+
3417+
const funcName = "tiny";
3418+
module.addFunction(funcName, binaryen.createType([binaryen.i32, binaryen.i32]), binaryen.none, [], module.nop());
3419+
const funcType = binaryen.Function(module.getFunction(funcName)).type;
3420+
const funcRef = binaryen.RefFunc(module.ref.func(funcName, funcType));
3421+
3422+
const operands = [
3423+
module.i32.const(6),
3424+
module.i32.const(7)
3425+
];
3426+
3427+
const theCallRef = binaryen.CallRef(module.call_ref(funcRef, operands, binaryen.none, false));
3428+
assert(theCallRef instanceof binaryen.CallRef);
3429+
assert(theCallRef instanceof binaryen.Expression);
3430+
assert(theCallRef.numOperands === operands.length);
3431+
assert(theCallRef.type === binaryen.none);
3432+
assert(binaryen.RefFunc(theCallRef.target).func === funcName);
3433+
3434+
const info = binaryen.getExpressionInfo(theCallRef);
3435+
assert(info.id === theCallRef.id);
3436+
assert(info.type === theCallRef.type);
3437+
assert(info.target === theCallRef.target);
3438+
assert(info.isReturn === theCallRef.isReturn());
3439+
3440+
assert(theCallRef.getNumOperands() === operands.length);
3441+
3442+
assert(theCallRef.getOperandAt(0) === operands[0]);
3443+
assert(theCallRef.getOperandAt(1) === operands[1]);
3444+
3445+
theCallRef.setOperandAt(0, operands[1]);
3446+
assert(theCallRef.getOperandAt(0), operands[1]);
3447+
theCallRef.setOperandAt(0, operands[0]);
3448+
assert(theCallRef.getOperandAt(0), operands[0]);
3449+
3450+
const newOperand = module.i32.const(8);
3451+
theCallRef.appendOperand(newOperand);
3452+
assert(theCallRef.getNumOperands() == 3);
3453+
assert(theCallRef.getOperandAt(2) === newOperand);
3454+
3455+
theCallRef.removeOperandAt(2);
3456+
assert(theCallRef.getNumOperands() == 2);
3457+
assert(theCallRef.getOperandAt(0) === operands[0]);
3458+
assert(theCallRef.getOperandAt(1) === operands[1]);
3459+
3460+
theCallRef.insertOperandAt(1, newOperand);
3461+
assert(theCallRef.getNumOperands() == 3);
3462+
assert(theCallRef.getOperandAt(0) === operands[0]);
3463+
assert(theCallRef.getOperandAt(1) === newOperand);
3464+
assert(theCallRef.getOperandAt(2) === operands[1]);
3465+
3466+
theCallRef.removeOperandAt(1);
3467+
assert(theCallRef.getNumOperands() == 2);
3468+
assert(theCallRef.getOperandAt(0) === operands[0]);
3469+
assert(theCallRef.getOperandAt(1) === operands[1]);
3470+
3471+
assert(theCallRef.isReturn() === false);
3472+
theCallRef.setReturn(true);
3473+
assert(theCallRef.isReturn() === true);
3474+
theCallRef.setReturn(false);
3475+
assert(theCallRef.isReturn() === false);
3476+
3477+
3478+
const targetRef = binaryen.RefFunc(theCallRef.getTarget());
3479+
assert(theCallRef.getTarget() == theCallRef.target);
3480+
assert(targetRef.func === funcName);
3481+
3482+
const newTargetName = "newTarget";
3483+
const newTargetRef = binaryen.RefFunc(module.ref.func(newTargetName, funcType));
3484+
theCallRef.setTarget(newTargetRef);
3485+
assert(binaryen.RefFunc(theCallRef.getTarget()).func === newTargetName);
3486+
3487+
theCallRef.setTarget(funcRef);
3488+
assert(binaryen.RefFunc(theCallRef.getTarget()).func === funcName);
3489+
3490+
console.log(theCallRef.toText());
3491+
assert(
3492+
theCallRef.toText()
3493+
==
3494+
"(call_ref $func.0\n (i32.const 6)\n (i32.const 7)\n (ref.func $tiny)\n)\n"
3495+
);
3496+
3497+
module.dispose();
3498+
})();

test/binaryen.js/expressions.js.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,9 @@
453453
(local.get $2)
454454
)
455455

456+
# CallRef
457+
(call_ref $func.0
458+
(i32.const 6)
459+
(i32.const 7)
460+
(ref.func $tiny)
461+
)

0 commit comments

Comments
 (0)