Debian Codegraph @

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