@@ -3409,3 +3409,97 @@ 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 ) ) ;
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+ const theReturnCallRef = binaryen . CallRef ( module . return_call_ref ( funcRef , operands , binaryen . none ) ) ;
3478+ assert ( theReturnCallRef instanceof binaryen . CallRef ) ;
3479+ assert ( theReturnCallRef instanceof binaryen . Expression ) ;
3480+ assert ( theReturnCallRef . numOperands === operands . length ) ;
3481+ assert ( binaryen . RefFunc ( theReturnCallRef . target ) . func === funcName ) ;
3482+ assert ( theReturnCallRef . isReturn ( ) === true ) ;
3483+
3484+
3485+ const targetRef = binaryen . RefFunc ( theCallRef . getTarget ( ) ) ;
3486+ assert ( theCallRef . getTarget ( ) == theCallRef . target ) ;
3487+ assert ( targetRef . func === funcName ) ;
3488+
3489+ const newTargetName = "newTarget" ;
3490+ const newTargetRef = binaryen . RefFunc ( module . ref . func ( newTargetName , funcType ) ) ;
3491+ theCallRef . setTarget ( newTargetRef ) ;
3492+ assert ( binaryen . RefFunc ( theCallRef . getTarget ( ) ) . func === newTargetName ) ;
3493+
3494+ theCallRef . setTarget ( funcRef ) ;
3495+ assert ( binaryen . RefFunc ( theCallRef . getTarget ( ) ) . func === funcName ) ;
3496+
3497+ console . log ( theCallRef . toText ( ) ) ;
3498+ assert (
3499+ theCallRef . toText ( )
3500+ ==
3501+ "(call_ref $func.0\n (i32.const 6)\n (i32.const 7)\n (ref.func $tiny)\n)\n"
3502+ ) ;
3503+
3504+ module . dispose ( ) ;
3505+ } ) ( ) ;
0 commit comments