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