-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add return_call example for WASM (#2693)
* Add return_call example for WASM * Apply suggestions from code review Co-authored-by: Yury Delendik <[email protected]> * Update return_call.js * Add comments * Add the demo to the meta file --------- Co-authored-by: Yury Delendik <[email protected]> Co-authored-by: Brian Thomas Smith <[email protected]>
- Loading branch information
1 parent
71e5651
commit b6ed883
Showing
4 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const url = '{%wasm-url%}'; | ||
const { instance } = await WebAssembly.instantiateStreaming(fetch(url)); | ||
const result = instance.exports.fac(5n); | ||
|
||
console.log(result); | ||
// Expected output: 120n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(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)) | ||
) | ||
) | ||
) | ||
) | ||
) |