Skip to content

Commit 5a253b4

Browse files
fix(moonbit): set correct string length (#1209)
* fix(moonbit): string and reallocation * fix(moonbit): unify pointer conversion * fix(moonbit): should return charcode length * fix(moonbit): simplify copy and fix allocation
1 parent d8994a3 commit 5a253b4

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

crates/moonbit/src/lib.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,35 +87,43 @@ pub fn malloc(size : Int) -> Int {
8787
let address = malloc_inline(8 + words * 4)
8888
store32(address, 1)
8989
store32(address + 4, (words << 8) | 246)
90-
store8(address + words * 4 + 7, 3 - size % 4)
9190
address + 8
9291
}
9392
9493
pub extern "wasm" fn free(position : Int) =
9594
#|(func (param i32) local.get 0 i32.const 8 i32.sub call $moonbit.decref)
9695
97-
pub fn copy(dest : Int, src : Int) -> Unit {
98-
let src_len = (load32(src - 12) >> 2) - 4
99-
let dest_len = (load32(dest - 12) >> 2) - 4
100-
let min = if src_len < dest_len { src_len } else { dest_len }
101-
copy_inline(dest, src, min)
102-
}
103-
104-
extern "wasm" fn copy_inline(dest : Int, src : Int, len : Int) =
96+
extern "wasm" fn copy(dest : Int, src : Int, len : Int) =
10597
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
10698
10799
pub extern "wasm" fn str2ptr(str : String) -> Int =
108100
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
109101
110-
pub extern "wasm" fn ptr2str(ptr : Int) -> String =
111-
#|(func (param i32) (result i32) local.get 0 i32.const 4 i32.sub i32.const 243 i32.store8 local.get 0 i32.const 8 i32.sub)
102+
extern "wasm" fn ptr2str_ffi(ptr : Int) -> String =
103+
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.sub)
104+
105+
pub fn ptr2str(ptr : Int, len : Int) -> String {
106+
let words = len * 2 / 4 + 1
107+
let address = ptr - 8
108+
store32(address + 4, (words << 8) | 243)
109+
store8(address + words * 4 + 7, 3 - len * 2 % 4)
110+
ptr2str_ffi(ptr)
111+
}
112112
113113
pub extern "wasm" fn bytes2ptr(bytes : FixedArray[Byte]) -> Int =
114114
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
115115
116-
pub extern "wasm" fn ptr2bytes(ptr : Int, _len : Int) -> FixedArray[Byte] =
116+
extern "wasm" fn ptr2bytes_ffi(ptr : Int) -> FixedArray[Byte] =
117117
#|(func (param i32) (param i32) (result i32) local.get 0 i32.const 8 i32.sub)
118118
119+
pub fn ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
120+
let words = len / 4 + 1
121+
let address = ptr - 8
122+
store32(address + 4, (words << 8) | 246)
123+
store8(address + words * 4 + 7, 3 - len % 4)
124+
ptr2bytes_ffi(ptr)
125+
}
126+
119127
pub extern "wasm" fn uint_array2ptr(array : FixedArray[UInt]) -> Int =
120128
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
121129
@@ -196,13 +204,13 @@ pub fn cabi_realloc(
196204
return malloc(dst_size)
197205
}}
198206
// free
199-
if dst_size <= 0 {{
207+
if dst_size == 0 {{
200208
free(src_offset)
201209
return 0
202210
}}
203211
// realloc
204212
let dst = malloc(dst_size)
205-
copy(dst, src_offset)
213+
copy(dst, src_offset, if src_size < dst_size { src_size } else { dst_size })
206214
free(src_offset)
207215
dst
208216
}}
@@ -2244,7 +2252,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22442252
"{}str2ptr({op})",
22452253
self.gen.qualify_package(FFI_DIR)
22462254
));
2247-
results.push(format!("{op}.iter().count()"));
2255+
results.push(format!("{op}.charcode_length()"));
22482256
if realloc.is_none() {
22492257
self.cleanup.push(Cleanup::Object(op.clone()));
22502258
}
@@ -2258,8 +2266,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
22582266
uwrite!(
22592267
self.src,
22602268
"
2261-
ignore({length})
2262-
let {result} = {}ptr2str({address})
2269+
let {result} = {}ptr2str({address}, {length})
22632270
",
22642271
self.gen.qualify_package(FFI_DIR)
22652272
);

0 commit comments

Comments
 (0)