@@ -753,6 +753,33 @@ class WasmFoundationTests: XCTestCase {
753753 testForOutput ( program: jsProg, runner: runner, outputString: " 1368 \n " )
754754 }
755755
756+ func testLoopWithParametersAndResult( ) throws {
757+ let runner = try GetJavaScriptExecutorOrSkipTest ( )
758+ let liveTestConfig = Configuration ( logLevel: . error, enableInspection: true )
759+ let fuzzer = makeMockFuzzer ( config: liveTestConfig, environment: JavaScriptEnvironment ( ) )
760+ let b = fuzzer. makeBuilder ( )
761+ let outputFunc = b. createNamedVariable ( forBuiltin: " output " )
762+ let module = b. buildWasmModule { wasmModule in
763+ wasmModule. addWasmFunction ( with: [ . wasmi32, . wasmi32] => . wasmi32) { function, args in
764+ let loopResult = function. wasmBuildLoop ( with: [ . wasmi32, . wasmi32] => . wasmi32, args: args) { loopLabel, loopArgs in
765+ let incFirst = function. wasmi32BinOp ( loopArgs [ 0 ] , function. consti32 ( 1 ) , binOpKind: . Add)
766+ let incSecond = function. wasmi32BinOp ( loopArgs [ 1 ] , function. consti32 ( 2 ) , binOpKind: . Add)
767+ let condition = function. wasmi32CompareOp ( incFirst, incSecond, using: . Gt_s)
768+ function. wasmBranchIf ( condition, to: loopLabel, args: [ incFirst, incSecond] )
769+ return incFirst
770+ }
771+ function. wasmReturn ( loopResult)
772+ }
773+ }
774+ let exports = module. loadExports ( )
775+ let wasmOut = b. callMethod ( module. getExportedMethod ( at: 0 ) , on: exports, withArgs: [ b. loadInt ( 10 ) , b. loadInt ( 0 ) ] )
776+ b. callFunction ( outputFunc, withArgs: [ b. callMethod ( " toString " , on: wasmOut) ] )
777+
778+ let prog = b. finalize ( )
779+ let jsProg = fuzzer. lifter. lift ( prog)
780+ testForOutput ( program: jsProg, runner: runner, outputString: " 20 \n " )
781+ }
782+
756783 func testIfs( ) throws {
757784 let runner = try GetJavaScriptExecutorOrSkipTest ( )
758785 let liveTestConfig = Configuration ( logLevel: . error, enableInspection: true )
0 commit comments