1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! Some functions in the site component wasm ABI pass context to the Afia host.
//!
//! The Afia host will then pass the context back when it calls the site component's exported
//! functions.
//!
//! ```
//! // TODO: This `afia-component` crate will eventually abstract over these `extern`
//! // functions. When that happens we'll want to update this example to use the abstraction.
//! // Users of this crate won't be working directly with the low-level C ABI, they'll be using
//! // higher level abstractions that this crate exposes.
//!
//! use afia_component::ComponentImports;
//!
//! #[export_name = "__afia__$create_instance"]
//! pub extern "C" fn __afia_create_instance() -> isize {
//! 123
//! }
//!
//! #[export_name = "__afia__$output$123$create_element"]
//! pub extern "C" fn create_element(context: isize, _inputs_ptr: isize, _inputs_len: usize) -> i64 {
//! assert_eq!(context, 123);
//!
//! let div = ComponentImports::new_dynamically_linked().create_element("div").unwrap();
//! div.temporary_way_to_get_i64()
//! }
//! ```
//!
//! This module contains types that are useful when passing and receiving context from the Afia host.
use crateCratePrivate;
/// Some `afia-component` functions have a `context` parameter.
///
/// This `context` gets passed to Afia, and then Afia later passes the context back to the site
/// component.
///
/// Types that implement this `Context` trait can be used as `context`.
pub
/// Provides a safe abstraction over context that is a mutable pointer.
///
/// TODO: Not sure whether the `afia-component` will eventually abstract this away such that
/// the user does not need to use it... Still feeling out this `site-componenet-utils` crate...
// rustc regression is making this trigger a dead code warning even though it shouldn't.
// can remove this `#[allow(dead_code)]` once the regression is fixed
// https://github.com/rust-lang/rust/issues/126706
;
/// Wraps a context pointer such that it can be passed to the Afia host, but it cannot be accessed
/// by the site component.
///
/// This is useful when the component is already holding an `&mut T` reference and wishes to avoid
/// unintentionally creating two mutable references.
///
/// TODO: Not sure whether the `afia-component` will eventually abstract this away such that
/// the user does not need to use it... so... not including an example for now.
// rustc regression is making this trigger a dead code warning even though it shouldn't.
// can remove this `#[allow(dead_code)]` once the regression is fixed
// https://github.com/rust-lang/rust/issues/126706
;