Skip to content

Commit 1c9bfc6

Browse files
refactor: Remove inline wasm ABI pointer shifts (#1616)
* fix: remove inline wasm ABI pointer shifts * docs: note MoonBit ABI layout change
1 parent 95995ce commit 1c9bfc6

3 files changed

Lines changed: 81 additions & 68 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,19 @@ This can be useful having one project that exports multiple worlds.
410410
When using `wit-bindgen moonbit`, you may use `--derive-show` or `--derive-eq` to derive `Show` or `Eq` traits for all types.
411411
You may also use `--derive-error`, which will make types containing `Error` as error types in MoonBit.
412412

413+
#### MoonBit ABI compatibility
414+
415+
The MoonBit generator emits inline wasm helpers for strings, bytes, and arrays.
416+
MoonBit changed the ABI layout for these values so the data pointer no longer
417+
needs the old 8-byte offset. This is a breaking change for generated MoonBit
418+
bindings that use inline wasm, but it is not something the MoonBit compiler can
419+
detect at the type level.
420+
421+
Regenerate MoonBit bindings with a `wit-bindgen` version that matches the
422+
MoonBit toolchain ABI layout. If old generated bindings or an older MoonBit
423+
toolchain are mixed with new bindings, components may still compile but fail at
424+
runtime with corrupted strings/bytes or traps.
425+
413426
You will find the files to be modified with the name `**/stub.mbt`.
414427
To avoid touching the files during regeneration (including `moon.pkg.json` or `moon.mod.json`) you may use `--ignore-stub`.
415428

crates/moonbit/src/ffi.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -98,101 +98,101 @@ pub(crate) const STR2PTR: &str = r#"
9898
///|
9999
#owned(str)
100100
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
101-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
101+
#|(func (param i32) (result i32) local.get 0)
102102
"#;
103103

104104
pub(crate) const PTR2STR: &str = r#"
105105
///|
106106
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
107+
#|(func (param i32) (param i32) (result i32)
108+
#| local.get 0
109109
#| local.get 1 call $moonbit.init_array16
110-
#| local.get 2)
110+
#| local.get 0)
111111
"#;
112112

113113
pub(crate) const BYTES2PTR: &str = r#"
114114
///|
115115
#owned(bytes)
116116
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
117-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
117+
#|(func (param i32) (result i32) local.get 0)
118118
"#;
119119

120120
pub(crate) const PTR2BYTES: &str = r#"
121121
///|
122122
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
123+
#|(func (param i32) (param i32) (result i32)
124+
#| local.get 0
125125
#| local.get 1 call $moonbit.init_array8
126-
#| local.get 2)
126+
#| local.get 0)
127127
"#;
128128

129129
pub(crate) const UINT_ARRAY2PTR: &str = r#"
130130
///|
131131
#owned(array)
132132
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)
133+
#|(func (param i32) (result i32) local.get 0)
134134
"#;
135135

136136
pub(crate) const UINT64_ARRAY2PTR: &str = r#"
137137
///|
138138
#owned(array)
139139
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)
140+
#|(func (param i32) (result i32) local.get 0)
141141
"#;
142142

143143
pub(crate) const INT_ARRAY2PTR: &str = r#"
144144
///|
145145
#owned(array)
146146
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)
147+
#|(func (param i32) (result i32) local.get 0)
148148
"#;
149149

150150
pub(crate) const INT64_ARRAY2PTR: &str = r#"
151151
///|
152152
#owned(array)
153153
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)
154+
#|(func (param i32) (result i32) local.get 0)
155155
"#;
156156

157157
pub(crate) const FLOAT_ARRAY2PTR: &str = r#"
158158
///|
159159
#owned(array)
160160
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)
161+
#|(func (param i32) (result i32) local.get 0)
162162
"#;
163163

164164
pub(crate) const DOUBLE_ARRAY2PTR: &str = r#"
165165
///|
166166
#owned(array)
167167
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)
168+
#|(func (param i32) (result i32) local.get 0)
169169
"#;
170170

171171
pub(crate) const PTR2UINT_ARRAY: &str = r#"
172172
///|
173173
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
174+
#|(func (param i32) (param i32) (result i32)
175+
#| local.get 0
176176
#| local.get 1 call $moonbit.init_array32
177-
#| local.get 2)
177+
#| local.get 0)
178178
"#;
179179

180180
pub(crate) const PTR2INT_ARRAY: &str = r#"
181181
///|
182182
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
183+
#|(func (param i32) (param i32) (result i32)
184+
#| local.get 0
185185
#| local.get 1 call $moonbit.init_array32
186-
#| local.get 2)
186+
#| local.get 0)
187187
"#;
188188

189189
pub(crate) const PTR2FLOAT_ARRAY: &str = r#"
190190
///|
191191
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
192+
#|(func (param i32) (param i32) (result i32)
193+
#| local.get 0
194194
#| local.get 1 call $moonbit.init_array32
195-
#| local.get 2)
195+
#| local.get 0)
196196
"#;
197197

198198
pub(crate) const PTR2UINT64_ARRAY: &str = r#"
@@ -201,19 +201,19 @@ extern "wasm" fn mbt_ffi_ptr2uint64_array(
201201
ptr : Int,
202202
len : Int,
203203
) -> FixedArray[UInt64] =
204-
#|(func (param i32) (param i32) (result i32) (local i32)
205-
#| local.get 0 i32.const 8 i32.sub local.tee 2
204+
#|(func (param i32) (param i32) (result i32)
205+
#| local.get 0
206206
#| local.get 1 call $moonbit.init_array64
207-
#| local.get 2)
207+
#| local.get 0)
208208
"#;
209209

210210
pub(crate) const PTR2INT64_ARRAY: &str = r#"
211211
///|
212212
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
213+
#|(func (param i32) (param i32) (result i32)
214+
#| local.get 0
215215
#| local.get 1 call $moonbit.init_array64
216-
#| local.get 2)
216+
#| local.get 0)
217217
"#;
218218

219219
pub(crate) const PTR2DOUBLE_ARRAY: &str = r#"
@@ -222,10 +222,10 @@ extern "wasm" fn mbt_ffi_ptr2double_array(
222222
ptr : Int,
223223
len : Int,
224224
) -> FixedArray[Double] =
225-
#|(func (param i32) (param i32) (result i32) (local i32)
226-
#| local.get 0 i32.const 8 i32.sub local.tee 2
225+
#|(func (param i32) (param i32) (result i32)
226+
#| local.get 0
227227
#| local.get 1 call $moonbit.init_array64
228-
#| local.get 2)
228+
#| local.get 0)
229229
"#;
230230

231231
pub(crate) const MALLOC: &str = r#"
@@ -234,13 +234,13 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
234234
#|(func (param i32) (result i32) (local i32)
235235
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
236236
#| local.tee 1 i32.const 0 call $moonbit.init_array8
237-
#| local.get 1 i32.const 8 i32.add)
237+
#| local.get 1)
238238
"#;
239239

240240
pub(crate) const FREE: &str = r#"
241241
///|
242242
extern "wasm" fn mbt_ffi_free(position : Int) =
243-
#|(func (param i32) local.get 0 i32.const 8 i32.sub call $moonbit.decref)
243+
#|(func (param i32) local.get 0 call $moonbit.decref)
244244
"#;
245245

246246
pub(crate) const CABI_REALLOC: &str = r#"

crates/moonbit/src/ffi/ffi.mbt

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ pub extern "wasm" fn malloc(size : Int) -> Int =
7777
#|(func (param i32) (result i32) (local i32)
7878
#| local.get 0 i32.const 4 i32.add call $moonbit.gc.malloc
7979
#| local.tee 1 i32.const 0 call $moonbit.init_array8
80-
#| local.get 1 i32.const 8 i32.add)
80+
#| local.get 1)
8181

8282
///|
8383
pub extern "wasm" fn free(position : Int) =
84-
#|(func (param i32) local.get 0 i32.const 8 i32.sub call $moonbit.decref)
84+
#|(func (param i32) local.get 0 call $moonbit.decref)
8585

8686
///|
8787
extern "wasm" fn copy(dest : Int, src : Int, len : Int) =
@@ -90,104 +90,104 @@ extern "wasm" fn copy(dest : Int, src : Int, len : Int) =
9090
///|
9191
#owned(str)
9292
pub extern "wasm" fn str2ptr(str : String) -> Int =
93-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
93+
#|(func (param i32) (result i32) local.get 0)
9494

9595
///|
9696
pub extern "wasm" fn ptr2str(ptr : Int, len : Int) -> String =
97-
#|(func (param i32) (param i32) (result i32) (local i32)
98-
#| local.get 0 i32.const 8 i32.sub local.tee 2
97+
#|(func (param i32) (param i32) (result i32)
98+
#| local.get 0
9999
#| local.get 1 call $moonbit.init_array16
100-
#| local.get 2)
100+
#| local.get 0)
101101

102102
///|
103103
#owned(bytes)
104104
pub extern "wasm" fn bytes2ptr(bytes : FixedArray[Byte]) -> Int =
105-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
105+
#|(func (param i32) (result i32) local.get 0)
106106

107107
///|
108108
pub extern "wasm" fn ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
109-
#|(func (param i32) (param i32) (result i32) (local i32)
110-
#| local.get 0 i32.const 8 i32.sub local.tee 2
109+
#|(func (param i32) (param i32) (result i32)
110+
#| local.get 0
111111
#| local.get 1 call $moonbit.init_array8
112-
#| local.get 2)
112+
#| local.get 0)
113113

114114
///|
115115
#owned(array)
116116
pub extern "wasm" fn uint_array2ptr(array : FixedArray[UInt]) -> Int =
117-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
117+
#|(func (param i32) (result i32) local.get 0)
118118

119119
///|
120120
#owned(array)
121121
pub extern "wasm" fn uint64_array2ptr(array : FixedArray[UInt64]) -> Int =
122-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
122+
#|(func (param i32) (result i32) local.get 0)
123123

124124
///|
125125
#owned(array)
126126
pub extern "wasm" fn int_array2ptr(array : FixedArray[Int]) -> Int =
127-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
127+
#|(func (param i32) (result i32) local.get 0)
128128

129129
///|
130130
#owned(array)
131131
pub extern "wasm" fn int64_array2ptr(array : FixedArray[Int64]) -> Int =
132-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
132+
#|(func (param i32) (result i32) local.get 0)
133133

134134
///|
135135
#owned(array)
136136
pub extern "wasm" fn float_array2ptr(array : FixedArray[Float]) -> Int =
137-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
137+
#|(func (param i32) (result i32) local.get 0)
138138

139139
///|
140140
#owned(array)
141141
pub extern "wasm" fn double_array2ptr(array : FixedArray[Double]) -> Int =
142-
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
142+
#|(func (param i32) (result i32) local.get 0)
143143

144144
///|
145145
pub extern "wasm" fn ptr2uint_array(ptr : Int, len : Int) -> FixedArray[UInt] =
146-
#|(func (param i32) (param i32) (result i32) (local i32)
147-
#| local.get 0 i32.const 8 i32.sub local.tee 2
146+
#|(func (param i32) (param i32) (result i32)
147+
#| local.get 0
148148
#| local.get 1 call $moonbit.init_array32
149-
#| local.get 2)
149+
#| local.get 0)
150150

151151
///|
152152
pub extern "wasm" fn ptr2int_array(ptr : Int, len : Int) -> FixedArray[Int] =
153-
#|(func (param i32) (param i32) (result i32) (local i32)
154-
#| local.get 0 i32.const 8 i32.sub local.tee 2
153+
#|(func (param i32) (param i32) (result i32)
154+
#| local.get 0
155155
#| local.get 1 call $moonbit.init_array32
156-
#| local.get 2)
156+
#| local.get 0)
157157

158158
///|
159159
pub extern "wasm" fn ptr2float_array(ptr : Int, len : Int) -> FixedArray[Float] =
160-
#|(func (param i32) (param i32) (result i32) (local i32)
161-
#| local.get 0 i32.const 8 i32.sub local.tee 2
160+
#|(func (param i32) (param i32) (result i32)
161+
#| local.get 0
162162
#| local.get 1 call $moonbit.init_array32
163-
#| local.get 2)
163+
#| local.get 0)
164164

165165
///|
166166
pub extern "wasm" fn ptr2uint64_array(
167167
ptr : Int,
168168
len : Int,
169169
) -> FixedArray[UInt64] =
170-
#|(func (param i32) (param i32) (result i32) (local i32)
171-
#| local.get 0 i32.const 8 i32.sub local.tee 2
170+
#|(func (param i32) (param i32) (result i32)
171+
#| local.get 0
172172
#| local.get 1 call $moonbit.init_array64
173-
#| local.get 2)
173+
#| local.get 0)
174174

175175
///|
176176
pub extern "wasm" fn ptr2int64_array(ptr : Int, len : Int) -> FixedArray[Int64] =
177-
#|(func (param i32) (param i32) (result i32) (local i32)
178-
#| local.get 0 i32.const 8 i32.sub local.tee 2
177+
#|(func (param i32) (param i32) (result i32)
178+
#| local.get 0
179179
#| local.get 1 call $moonbit.init_array64
180-
#| local.get 2)
180+
#| local.get 0)
181181

182182
///|
183183
pub extern "wasm" fn ptr2double_array(
184184
ptr : Int,
185185
len : Int,
186186
) -> FixedArray[Double] =
187-
#|(func (param i32) (param i32) (result i32) (local i32)
188-
#| local.get 0 i32.const 8 i32.sub local.tee 2
187+
#|(func (param i32) (param i32) (result i32)
188+
#| local.get 0
189189
#| local.get 1 call $moonbit.init_array64
190-
#| local.get 2)
190+
#| local.get 0)
191191

192192
///|
193193
pub fn cabi_realloc(

0 commit comments

Comments
 (0)