1;;! component_model_async = true
2;;! component_model_error_context = true
3
4;; error-context.new
5(component
6  (core module $libc (memory (export "memory") 1))
7  (core instance $libc (instantiate $libc))
8  (core module $m
9    (import "" "error-context.new" (func $error-context-new (param i32 i32) (result i32)))
10  )
11  (core func $error-context-new (canon error-context.new (memory $libc "memory")))
12  (core instance $i (instantiate $m (with "" (instance (export "error-context.new" (func $error-context-new))))))
13)
14
15;; error-context.debug-message
16(component
17  (core module $libc
18    (func (export "realloc") (param i32 i32 i32 i32) (result i32) unreachable)
19    (memory (export "memory") 1)
20  )
21  (core instance $libc (instantiate $libc))
22  (core module $m
23    (import "" "error-context.debug-message" (func $error-context-debug-message (param i32 i32)))
24  )
25  (core func $error-context-debug-message (canon error-context.debug-message (memory $libc "memory") (realloc (func $libc "realloc"))))
26  (core instance $i (instantiate $m (with "" (instance (export "error-context.debug-message" (func $error-context-debug-message))))))
27)
28
29;; error-context.drop
30(component
31  (core module $m
32    (import "" "error-context.drop" (func $error-context-drop (param i32)))
33  )
34  (core func $error-context-drop (canon error-context.drop))
35  (core instance $i (instantiate $m (with "" (instance (export "error-context.drop" (func $error-context-drop))))))
36)
37
38;; Test edge-case behavior of `error-context.debug-message`.
39(component definition $A
40  (core module $libc
41    (memory (export "memory") 1)
42    (global $bump (mut i32) (i32.const 100))
43    (func (export "realloc") (param i32 i32 i32 i32) (result i32)
44      (local $ret i32)
45      (if (local.get 0) (then unreachable))
46      (if (local.get 1) (then unreachable))
47      (if (i32.ne (local.get 2) (i32.const 1)) (then unreachable))
48      (local.set $ret (global.get $bump))
49      (global.set $bump (i32.add (global.get $bump) (local.get 3)))
50      (local.get $ret)
51    )
52  )
53  (core instance $libc (instantiate $libc))
54
55  (core module $Core
56    (import "" "mem" (memory 1))
57    (import "" "error-context.new" (func $error-context.new (param i32 i32) (result i32)))
58    (import "" "error-context.debug-message" (func $error-context.debug-message (param i32 i32)))
59    (import "" "error-context.drop" (func $error-context.drop (param i32)))
60
61    (func (export "run") (param $dst i32)
62      (local $handle i32)
63      (i32.store8 (i32.const 0) (i32.const 0x61)) ;; 'a'
64      (local.set $handle (call $error-context.new (i32.const 0) (i32.const 1)))
65      (call $error-context.debug-message (local.get $handle) (local.get $dst))
66      (call $error-context.drop (local.get $handle))
67    )
68  )
69
70  (core func $error-context.new (canon error-context.new (memory $libc "memory")))
71  (core func $error-context.debug-message (canon error-context.debug-message (memory $libc "memory") (realloc (func $libc "realloc"))))
72  (core func $error-context.drop (canon error-context.drop))
73
74  (core instance $core (instantiate $Core (with "" (instance
75    (export "mem" (memory $libc "memory"))
76    (export "error-context.new" (func $error-context.new))
77    (export "error-context.debug-message" (func $error-context.debug-message))
78    (export "error-context.drop" (func $error-context.drop))
79  ))))
80
81  (func (export "run") (param "p" u32) (canon lift (core func $core "run")))
82)
83
84(component instance $A $A)
85(assert_return (invoke "run" (u32.const 65528)))
86(assert_trap (invoke "run" (u32.const 65532)) "invalid debug message pointer")
87