Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pepelsbey committed Oct 9, 2024
1 parent eff6da1 commit 9cf8a59
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions live-examples/wat-examples/statements/call.wat
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
(module
;; Import the `greet` function from the environment
(import "env" "greet" (func $greet))

(func
;; call the greet function
;; Call the imported `greet` function
call $greet
)

(start 1) ;; run the first function automatically
;; Automatically run the first function when the module starts
(start 1)
)
1 change: 1 addition & 0 deletions live-examples/wat-examples/statements/return_call.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ const { instance } = await WebAssembly.instantiateStreaming(fetch(url));
const result = instance.exports.fac(5n);

console.log(result);
// Expected output: 120n
5 changes: 5 additions & 0 deletions live-examples/wat-examples/statements/return_call.wat
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
(module
;; Calculate the factorial of a number
(func $fac (export "fac") (param $x i64) (result i64)
;; Call the `fac-aux` function with $x and 1 parameters
(return_call $fac-aux (local.get $x) (i64.const 1))
)

;; Perform the factorial calculation
(func $fac-aux (param $x i64) (param $r i64) (result i64)
;; If $x is zero, return the accumulated result $r
(if (result i64) (i64.eqz (local.get $x))
(then (return (local.get $r)))
(else
;; Otherwise, recursively call `fac-aux` with $x-1 and $x*$r
(return_call $fac-aux
(i64.sub (local.get $x) (i64.const 1))
(i64.mul (local.get $x) (local.get $r))
Expand Down

0 comments on commit 9cf8a59

Please sign in to comment.