Skip to content

Commit d333137

Browse files
refactor: MoonBit support update (#1464)
* chore: fmt & improve code * test: add more tests * test: add resource test * chore: store ffi files separately * chore: move files * refactor: fix warnings * chore: simplify function bindgen * refactor: inline ffi * chore: separate derive_opts * chore: cleanup * refactor: extract pkg resolver * refactor: separate cases for type and type constructor * refactor: simplify InterfaceGenerator::finish * refactor: simplify code snippet structures * refactor: extract async related code * chore: fmt
1 parent f63ee7e commit d333137

File tree

14 files changed

+2157
-1352
lines changed

14 files changed

+2157
-1352
lines changed

crates/moonbit/src/async_support.rs

Lines changed: 518 additions & 0 deletions
Large diffs are not rendered by default.

crates/moonbit/src/ffi.rs

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
pub(crate) const EXTEND16: &str = r#"
2+
///|
3+
extern "wasm" fn mbt_ffi_extend16(value : Int) -> Int =
4+
#|(func (param i32) (result i32) local.get 0 i32.extend16_s)
5+
"#;
6+
7+
pub(crate) const EXTEND8: &str = r#"
8+
///|
9+
extern "wasm" fn mbt_ffi_extend8(value : Int) -> Int =
10+
#|(func (param i32) (result i32) local.get 0 i32.extend8_s)
11+
"#;
12+
13+
pub(crate) const STORE8: &str = r#"
14+
///|
15+
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
16+
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
17+
"#;
18+
19+
pub(crate) const LOAD8_U: &str = r#"
20+
///|
21+
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
22+
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
23+
"#;
24+
25+
pub(crate) const LOAD8: &str = r#"
26+
///|
27+
extern "wasm" fn mbt_ffi_load8(offset : Int) -> Int =
28+
#|(func (param i32) (result i32) local.get 0 i32.load8_s)
29+
"#;
30+
31+
pub(crate) const STORE16: &str = r#"
32+
///|
33+
extern "wasm" fn mbt_ffi_store16(offset : Int, value : Int) =
34+
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store16)
35+
"#;
36+
37+
pub(crate) const LOAD16: &str = r#"
38+
///|
39+
extern "wasm" fn mbt_ffi_load16(offset : Int) -> Int =
40+
#|(func (param i32) (result i32) local.get 0 i32.load16_s)
41+
"#;
42+
43+
pub(crate) const LOAD16_U: &str = r#"
44+
///|
45+
extern "wasm" fn mbt_ffi_load16_u(offset : Int) -> Int =
46+
#|(func (param i32) (result i32) local.get 0 i32.load16_u)
47+
"#;
48+
49+
pub(crate) const STORE32: &str = r#"
50+
///|
51+
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
52+
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
53+
"#;
54+
55+
pub(crate) const LOAD32: &str = r#"
56+
///|
57+
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
58+
#|(func (param i32) (result i32) local.get 0 i32.load)
59+
"#;
60+
61+
pub(crate) const STORE64: &str = r#"
62+
///|
63+
extern "wasm" fn mbt_ffi_store64(offset : Int, value : Int64) =
64+
#|(func (param i32) (param i64) local.get 0 local.get 1 i64.store)
65+
"#;
66+
67+
pub(crate) const LOAD64: &str = r#"
68+
///|
69+
extern "wasm" fn mbt_ffi_load64(offset : Int) -> Int64 =
70+
#|(func (param i32) (result i64) local.get 0 i64.load)
71+
"#;
72+
73+
pub(crate) const STOREF32: &str = r#"
74+
///|
75+
extern "wasm" fn mbt_ffi_storef32(offset : Int, value : Float) =
76+
#|(func (param i32) (param f32) local.get 0 local.get 1 f32.store)
77+
"#;
78+
79+
pub(crate) const LOADF32: &str = r#"
80+
///|
81+
extern "wasm" fn mbt_ffi_loadf32(offset : Int) -> Float =
82+
#|(func (param i32) (result f32) local.get 0 f32.load)
83+
"#;
84+
85+
pub(crate) const STOREF64: &str = r#"
86+
///|
87+
extern "wasm" fn mbt_ffi_storef64(offset : Int, value : Double) =
88+
#|(func (param i32) (param f64) local.get 0 local.get 1 f64.store)
89+
"#;
90+
91+
pub(crate) const LOADF64: &str = r#"
92+
///|
93+
extern "wasm" fn mbt_ffi_loadf64(offset : Int) -> Double =
94+
#|(func (param i32) (result f64) local.get 0 f64.load)
95+
"#;
96+
97+
pub(crate) const STR2PTR: &str = r#"
98+
///|
99+
#owned(str)
100+
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
101+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
102+
"#;
103+
104+
pub(crate) const PTR2STR: &str = r#"
105+
///|
106+
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
107+
#|(func (param i32) (param i32) (result i32) (local i32)
108+
#| local.get 0 i32.const 8 i32.sub local.tee 2
109+
#| local.get 1 call $moonbit.init_array16
110+
#| local.get 2)
111+
"#;
112+
113+
pub(crate) const BYTES2PTR: &str = r#"
114+
///|
115+
#owned(bytes)
116+
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
117+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
118+
"#;
119+
120+
pub(crate) const PTR2BYTES: &str = r#"
121+
///|
122+
extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
123+
#|(func (param i32) (param i32) (result i32) (local i32)
124+
#| local.get 0 i32.const 8 i32.sub local.tee 2
125+
#| local.get 1 call $moonbit.init_array8
126+
#| local.get 2)
127+
"#;
128+
129+
pub(crate) const UINT_ARRAY2PTR: &str = r#"
130+
///|
131+
#owned(array)
132+
extern "wasm" fn mbt_ffi_uint_array2ptr(array : FixedArray[UInt]) -> Int =
133+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
134+
"#;
135+
136+
pub(crate) const UINT64_ARRAY2PTR: &str = r#"
137+
///|
138+
#owned(array)
139+
extern "wasm" fn mbt_ffi_uint64_array2ptr(array : FixedArray[UInt64]) -> Int =
140+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
141+
"#;
142+
143+
pub(crate) const INT_ARRAY2PTR: &str = r#"
144+
///|
145+
#owned(array)
146+
extern "wasm" fn mbt_ffi_int_array2ptr(array : FixedArray[Int]) -> Int =
147+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
148+
"#;
149+
150+
pub(crate) const INT64_ARRAY2PTR: &str = r#"
151+
///|
152+
#owned(array)
153+
extern "wasm" fn mbt_ffi_int64_array2ptr(array : FixedArray[Int64]) -> Int =
154+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
155+
"#;
156+
157+
pub(crate) const FLOAT_ARRAY2PTR: &str = r#"
158+
///|
159+
#owned(array)
160+
extern "wasm" fn mbt_ffi_float_array2ptr(array : FixedArray[Float]) -> Int =
161+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
162+
"#;
163+
164+
pub(crate) const DOUBLE_ARRAY2PTR: &str = r#"
165+
///|
166+
#owned(array)
167+
extern "wasm" fn mbt_ffi_double_array2ptr(array : FixedArray[Double]) -> Int =
168+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
169+
"#;
170+
171+
pub(crate) const PTR2UINT_ARRAY: &str = r#"
172+
///|
173+
extern "wasm" fn mbt_ffi_ptr2uint_array(ptr : Int, len : Int) -> FixedArray[UInt] =
174+
#|(func (param i32) (param i32) (result i32) (local i32)
175+
#| local.get 0 i32.const 8 i32.sub local.tee 2
176+
#| local.get 1 call $moonbit.init_array32
177+
#| local.get 2)
178+
"#;
179+
180+
pub(crate) const PTR2INT_ARRAY: &str = r#"
181+
///|
182+
extern "wasm" fn mbt_ffi_ptr2int_array(ptr : Int, len : Int) -> FixedArray[Int] =
183+
#|(func (param i32) (param i32) (result i32) (local i32)
184+
#| local.get 0 i32.const 8 i32.sub local.tee 2
185+
#| local.get 1 call $moonbit.init_array32
186+
#| local.get 2)
187+
"#;
188+
189+
pub(crate) const PTR2FLOAT_ARRAY: &str = r#"
190+
///|
191+
extern "wasm" fn mbt_ffi_ptr2float_array(ptr : Int, len : Int) -> FixedArray[Float] =
192+
#|(func (param i32) (param i32) (result i32) (local i32)
193+
#| local.get 0 i32.const 8 i32.sub local.tee 2
194+
#| local.get 1 call $moonbit.init_array32
195+
#| local.get 2)
196+
"#;
197+
198+
pub(crate) const PTR2UINT64_ARRAY: &str = r#"
199+
///|
200+
extern "wasm" fn mbt_ffi_ptr2uint64_array(
201+
ptr : Int,
202+
len : Int,
203+
) -> FixedArray[UInt64] =
204+
#|(func (param i32) (param i32) (result i32) (local i32)
205+
#| local.get 0 i32.const 8 i32.sub local.tee 2
206+
#| local.get 1 call $moonbit.init_array64
207+
#| local.get 2)
208+
"#;
209+
210+
pub(crate) const PTR2INT64_ARRAY: &str = r#"
211+
///|
212+
extern "wasm" fn mbt_ffi_ptr2int64_array(ptr : Int, len : Int) -> FixedArray[Int64] =
213+
#|(func (param i32) (param i32) (result i32) (local i32)
214+
#| local.get 0 i32.const 8 i32.sub local.tee 2
215+
#| local.get 1 call $moonbit.init_array64
216+
#| local.get 2)
217+
"#;
218+
219+
pub(crate) const PTR2DOUBLE_ARRAY: &str = r#"
220+
///|
221+
extern "wasm" fn mbt_ffi_ptr2double_array(
222+
ptr : Int,
223+
len : Int,
224+
) -> FixedArray[Double] =
225+
#|(func (param i32) (param i32) (result i32) (local i32)
226+
#| local.get 0 i32.const 8 i32.sub local.tee 2
227+
#| local.get 1 call $moonbit.init_array64
228+
#| local.get 2)
229+
"#;
230+
231+
pub(crate) const MALLOC: &str = r#"
232+
///|
233+
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
234+
#|(func (param i32) (result i32) (local i32)
235+
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
236+
#| local.tee 1 i32.const 0 call $moonbit.init_array8
237+
#| local.get 1 i32.const 8 i32.add)
238+
"#;
239+
240+
pub(crate) const FREE: &str = r#"
241+
///|
242+
extern "wasm" fn mbt_ffi_free(position : Int) =
243+
#|(func (param i32) local.get 0 i32.const 8 i32.sub call $moonbit.decref)
244+
"#;
245+
246+
pub(crate) const CABI_REALLOC: &str = r#"
247+
///|
248+
pub fn mbt_ffi_cabi_realloc(
249+
src_offset : Int,
250+
src_size : Int,
251+
_dst_alignment : Int,
252+
dst_size : Int,
253+
) -> Int {
254+
// malloc
255+
if src_offset == 0 && src_size == 0 {
256+
return mbt_ffi_malloc(dst_size)
257+
}
258+
// free
259+
if dst_size == 0 {
260+
mbt_ffi_free(src_offset)
261+
return 0
262+
}
263+
// realloc
264+
let dst = mbt_ffi_malloc(dst_size)
265+
mbt_ffi_copy(dst, src_offset, if src_size < dst_size { src_size } else { dst_size })
266+
mbt_ffi_free(src_offset)
267+
dst
268+
}
269+
270+
///|
271+
extern "wasm" fn mbt_ffi_copy(dest : Int, src : Int, len : Int) =
272+
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
273+
"#;

crates/moonbit/src/async-wasm/async_primitive.mbt renamed to crates/moonbit/src/ffi/async_primitive.mbt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ async fn[T, E : Error] async_suspend(
66
///|
77
fn run_async(f : async () -> Unit noraise) = "%async.run"
88

9-
109
///|
1110
priv enum State {
1211
Done
@@ -93,7 +92,7 @@ pub async fn pause() -> Unit {
9392
}
9493

9594
///|
96-
pub async fn suspend() -> Unit raise {
95+
pub async fn suspend() -> Unit {
9796
guard scheduler.curr_coro is Some(coro)
9897
if coro.cancelled && not(coro.shielded) {
9998
raise Cancelled::Cancelled
@@ -109,7 +108,7 @@ pub async fn suspend() -> Unit raise {
109108
}
110109

111110
///|
112-
pub fn spawn(f : async () -> Unit raise) -> Coroutine {
111+
pub fn spawn(f : async () -> Unit) -> Coroutine {
113112
scheduler.coro_id += 1
114113
let coro = {
115114
state: Running,
@@ -149,7 +148,7 @@ pub fn Coroutine::unwrap(self : Coroutine) -> Unit raise {
149148
}
150149

151150
///|
152-
pub async fn Coroutine::wait(target : Coroutine) -> Unit raise {
151+
pub async fn Coroutine::wait(target : Coroutine) -> Unit {
153152
guard scheduler.curr_coro is Some(coro)
154153
guard not(physical_equal(coro, target))
155154
match target.state {
@@ -169,7 +168,7 @@ pub async fn Coroutine::wait(target : Coroutine) -> Unit raise {
169168
}
170169

171170
///|
172-
pub async fn protect_from_cancel(f : async () -> Unit raise) -> Unit raise {
171+
pub async fn protect_from_cancel(f : async () -> Unit) -> Unit {
173172
guard scheduler.curr_coro is Some(coro)
174173
if coro.shielded {
175174
// already in a shield, do nothing
@@ -186,7 +185,6 @@ pub async fn protect_from_cancel(f : async () -> Unit raise) -> Unit raise {
186185
}
187186
}
188187

189-
190188
///|
191189
priv struct Scheduler {
192190
mut coro_id : Int

0 commit comments

Comments
 (0)