Debian Codegraph @

crates/wit-component/tests/components/adapt-inject-stack/adapt-old.wat

raw
1 (module
2 (import "new" "get-two" (func $get_two (param i32)))
3 (import "env" "memory" (memory 0))
4
5 (global $__stack_pointer (mut i32) i32.const 0)
6 (global $some_other_mutable_global (mut i32) i32.const 0)
7
8 ;; This is a sample adapter which is adapting between ABI. This exact function
9 ;; signature is imported by `module.wat` and we're implementing it here with a
10 ;; canonical-abi function that returns two integers. The canonical ABI for
11 ;; returning two integers is different than the ABI of this function, hence
12 ;; the adapter here.
13 ;;
14 ;; The purpose of this test case is to exercise the `$__stack_pointer` global.
15 ;; The stack pointer here needs to be initialized to something valid for
16 ;; this adapter module which is done with an injected `start` function into
17 ;; this adapter module when it's bundled into a component.
18 (func (export "get_sum") (result i32)
19 (local i32 i32)
20
21 ;; Allocate 8 bytes of stack space for the two u32 return values. The
22 ;; original stack pointer is saved in local 0 and the stack frame for this
23 ;; function is saved in local 1.
24 global.get $__stack_pointer
25 local.tee 0
26 i32.const 8
27 i32.sub
28 local.tee 1
29 global.set $__stack_pointer
30
31 ;; Call the imported function which will return two u32 values into the
32 ;; return pointer specified here, our stack frame.
33 local.get 1
34 call $get_two
35
36 ;; Compute the result of this function by adding together the two return
37 ;; values.
38 (i32.add
39 (i32.load (local.get 1))
40 (i32.load offset=4 (local.get 1)))
41
42 ;; Test that if there is another mutable global in this module that it
43 ;; doesn't affect the detection of the stack pointer. This extra mutable
44 ;; global should not be initialized or tampered with as part of the
45 ;; initialize-the-stack-pointer injected function
46 (global.set $some_other_mutable_global (global.get $some_other_mutable_global))
47
48 ;; Restore the stack pointer to the value it was at prior to entering this
49 ;; function.
50 local.get 0
51 global.set $__stack_pointer
52 )
53
54 )