@@ -87,35 +87,43 @@ pub fn malloc(size : Int) -> Int {
87
87
let address = malloc_inline(8 + words * 4)
88
88
store32(address, 1)
89
89
store32(address + 4, (words << 8) | 246)
90
- store8(address + words * 4 + 7, 3 - size % 4)
91
90
address + 8
92
91
}
93
92
94
93
pub extern "wasm" fn free(position : Int) =
95
94
#|(func (param i32) local.get 0 i32.const 8 i32.sub call $moonbit.decref)
96
95
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) =
105
97
#|(func (param i32) (param i32) (param i32) local.get 0 local.get 1 local.get 2 memory.copy)
106
98
107
99
pub extern "wasm" fn str2ptr(str : String) -> Int =
108
100
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
109
101
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
+ }
112
112
113
113
pub extern "wasm" fn bytes2ptr(bytes : FixedArray[Byte]) -> Int =
114
114
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
115
115
116
- pub extern "wasm" fn ptr2bytes (ptr : Int, _len : Int) -> FixedArray[Byte] =
116
+ extern "wasm" fn ptr2bytes_ffi (ptr : Int) -> FixedArray[Byte] =
117
117
#|(func (param i32) (param i32) (result i32) local.get 0 i32.const 8 i32.sub)
118
118
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
+
119
127
pub extern "wasm" fn uint_array2ptr(array : FixedArray[UInt]) -> Int =
120
128
#|(func (param i32) (result i32) local.get 0 i32.const 8 i32.add)
121
129
@@ -196,13 +204,13 @@ pub fn cabi_realloc(
196
204
return malloc(dst_size)
197
205
}}
198
206
// free
199
- if dst_size < = 0 {{
207
+ if dst_size = = 0 {{
200
208
free(src_offset)
201
209
return 0
202
210
}}
203
211
// realloc
204
212
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 } )
206
214
free(src_offset)
207
215
dst
208
216
}}
@@ -2244,7 +2252,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2244
2252
"{}str2ptr({op})" ,
2245
2253
self . gen . qualify_package( FFI_DIR )
2246
2254
) ) ;
2247
- results. push ( format ! ( "{op}.iter().count ()" ) ) ;
2255
+ results. push ( format ! ( "{op}.charcode_length ()" ) ) ;
2248
2256
if realloc. is_none ( ) {
2249
2257
self . cleanup . push ( Cleanup :: Object ( op. clone ( ) ) ) ;
2250
2258
}
@@ -2258,8 +2266,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2258
2266
uwrite ! (
2259
2267
self . src,
2260
2268
"
2261
- ignore({length})
2262
- let {result} = {}ptr2str({address})
2269
+ let {result} = {}ptr2str({address}, {length})
2263
2270
" ,
2264
2271
self . gen . qualify_package( FFI_DIR )
2265
2272
) ;
0 commit comments