-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
radix-engine-tests/assets/wasm/reference_types_ref_func.wat
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,59 @@ | ||
(module | ||
;; Define a function table with an initial size of 3 and a maximum size of 4 | ||
(table $t 3 4 funcref) | ||
(type $func_type (func (param i32 i32) (result i32))) | ||
|
||
;; Initialize the table with the 3 functions | ||
(elem (i32.const 0) $add $multiply $subtract) | ||
|
||
;; Function 0: Adds two numbers | ||
(func $add (type $func_type) | ||
(local.get 0) | ||
(local.get 1) | ||
(i32.add) | ||
) | ||
|
||
;; Function 1: Multiplies two numbers | ||
(func $multiply (type $func_type) | ||
(local.get 0) | ||
(local.get 1) | ||
(i32.mul) | ||
) | ||
|
||
;; Function 2: Subtracts two numbers | ||
(func $subtract (type $func_type) | ||
(local.get 0) | ||
(local.get 1) | ||
(i32.sub) | ||
) | ||
|
||
;; Function that overwrites entry at given index in function table | ||
(func $Test_f (param $0 i64) (result i64) | ||
(local $result i32) | ||
|
||
;; Overwrite function at given index | ||
(table.set $t | ||
(i32.const ${index}) ;; Index | ||
(ref.func $add) ;; Reference to $add function | ||
) | ||
|
||
;; Encode () in SBOR at address 0x0 | ||
(i32.const 0) | ||
(i32.const 92) ;; prefix | ||
(i32.store8) | ||
(i32.const 1) | ||
(i32.const 4) ;; i32 value kind | ||
(i32.store8) | ||
(i32.const 2) | ||
(i32.const 0) ;; value | ||
(i32.store) | ||
|
||
;; Return slice (ptr = 0, len = 6) | ||
(i64.const 6) | ||
) | ||
|
||
;; Define memory with an initial size of 1 page (64 KiB) | ||
(memory $0 1) | ||
(export "memory" (memory $0)) | ||
(export "Test_f" (func $Test_f)) | ||
) |
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,86 @@ | ||
(module | ||
;; Define a function table with an initial size of 3 and a maximum size of 4 | ||
(table $t 3 4 funcref) | ||
(type $func_type (func (param i32 i32) (result i32))) | ||
|
||
;; Initialize the table with the 3 functions | ||
(elem (i32.const 0) $add $multiply $subtract) | ||
|
||
;; Function 0: Adds two numbers | ||
(func $add (type $func_type) | ||
(local.get 0) | ||
(local.get 1) | ||
(i32.add) | ||
) | ||
|
||
;; Function 1: Multiplies two numbers | ||
(func $multiply (type $func_type) | ||
(local.get 0) | ||
(local.get 1) | ||
(i32.mul) | ||
) | ||
|
||
;; Function 2: Subtracts two numbers | ||
(func $subtract (type $func_type) | ||
(local.get 0) | ||
(local.get 1) | ||
(i32.sub) | ||
) | ||
|
||
;; Function to grow the table and add a new element | ||
(func $grow_table | ||
(local $ref_func funcref) | ||
;; ref.func not yet supported in 'wasm-instrument' | ||
;; see https://github.com/radixdlt/wasm-instrument/blob/405166c526aa60fa2af4e4b1122b156dbcc1bb15/src/stack_limiter/max_height.rs#L455 | ||
;; (local.set $ref_func | ||
;; (ref.func $add) | ||
;; ) | ||
;; use table.get instead to get funcref | ||
(local.set $ref_func | ||
(table.get $t | ||
;; Get $add function at the index 0 | ||
(i32.const 0) | ||
) | ||
) | ||
(table.grow $t | ||
(local.get $ref_func) ;; Initial value of the new entries | ||
(i32.const 1) ;; Number of entries to grow | ||
) | ||
;; table.grow returns previous size, drop it | ||
(drop) | ||
) | ||
|
||
;; Function that grows table and calls some function in the table | ||
(func $Test_f (param $0 i64) (result i64) | ||
(local $result i32) | ||
|
||
(call $grow_table) | ||
|
||
(i32.const ${a}) | ||
(i32.const ${b}) | ||
(call_indirect | ||
(type $func_type) | ||
(i32.const ${index}) ;; Call function at given index | ||
) | ||
(local.set $result) ;; value | ||
|
||
;; Encode () in SBOR at address 0x0 | ||
(i32.const 0) | ||
(i32.const 92) ;; prefix | ||
(i32.store8) | ||
(i32.const 1) | ||
(i32.const 4) ;; i32 value kind | ||
(i32.store8) | ||
(i32.const 2) | ||
(local.get $result) ;; value | ||
(i32.store) | ||
|
||
;; Return slice (ptr = 0, len = 6) | ||
(i64.const 6) | ||
) | ||
|
||
;; Define memory with an initial size of 1 page (64 KiB) | ||
(memory $0 1) | ||
(export "memory" (memory $0)) | ||
(export "Test_f" (func $Test_f)) | ||
) |
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