@@ -315,41 +315,22 @@ impl Module {
315315 hello_func_body. push ( Instruction :: I32Const ( 1 ) ) ; // Message ID 1 (see below)
316316 hello_func_body. push ( Instruction :: Call ( 1 ) ) ; // Call log function (index 1)
317317
318- // Loop 100 times
319- // Set up block and loop ( loop 0..100)
318+ // Simplified approach: use a block with conditional branch
319+ // This is more predictable than loop instruction for our simple case
320320 hello_func_body. push ( Instruction :: Block ( BlockType :: Empty ) ) ;
321- hello_func_body. push ( Instruction :: Loop ( BlockType :: Empty ) ) ;
322-
323- // Check if counter < 1 (Single iteration for quick execution)
324- hello_func_body. push ( Instruction :: LocalGet ( 0 ) ) ;
325- hello_func_body. push ( Instruction :: I32Const ( 1 ) ) ;
326- hello_func_body. push ( Instruction :: I32LtS ) ;
327-
328- // Per WebAssembly spec:
329- // - !I32LtS -> counter >= 1
330- // - BrIf(1) with !I32LtS: branch to outer block if counter >= 1
331- // (exit the loop when counter >= 1)
332- hello_func_body. push ( Instruction :: BrIf ( 1 ) ) ; // Break out of loop if counter >= 1
333- // (i.e., when the LtS comparison is false)
334-
335- // Loop body: increment counter
336- hello_func_body. push ( Instruction :: LocalGet ( 0 ) ) ;
337- hello_func_body. push ( Instruction :: I32Const ( 1 ) ) ;
338- hello_func_body. push ( Instruction :: I32Add ) ;
339- hello_func_body. push ( Instruction :: LocalSet ( 0 ) ) ;
340321
341322 // Log iteration (DEBUG level)
342323 hello_func_body. push ( Instruction :: I32Const ( 1 ) ) ; // DEBUG level
343324 hello_func_body. push ( Instruction :: I32Const ( 2 ) ) ; // Message ID 2
344325 hello_func_body. push ( Instruction :: Call ( 1 ) ) ; // Call log function
345326
346- // The counter has already been incremented above, now it's time to check
347- // if we should continue the loop. Since we're at the end of the loop body,
348- // branch back to the loop instruction to run the loop condition again.
349- hello_func_body. push ( Instruction :: Br ( 0 ) ) ; // Branch back to loop start
327+ // Increment counter (just once)
328+ hello_func_body. push ( Instruction :: LocalGet ( 0 ) ) ;
329+ hello_func_body. push ( Instruction :: I32Const ( 1 ) ) ;
330+ hello_func_body. push ( Instruction :: I32Add ) ;
331+ hello_func_body. push ( Instruction :: LocalSet ( 0 ) ) ;
350332
351- // End loop and block
352- hello_func_body. push ( Instruction :: End ) ;
333+ // End the block - no loops or branches needed for single iteration
353334 hello_func_body. push ( Instruction :: End ) ;
354335
355336 // Log completion message
0 commit comments