Skip to content

Commit b697730

Browse files
author
Kalle Fagerberg
committed
Use address of Bytes instead of copying into a FixedArray[Byte]
1 parent 035c02c commit b697730

File tree

6 files changed

+70
-39
lines changed

6 files changed

+70
-39
lines changed

src/ffi/top.wasm-gc.mbt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
///|
2-
pub fn get_addr(_ : FixedArray[Byte]) -> UInt {
2+
/// Returns the WASM object address to a `FixedArray[Byte]` object.
3+
///
4+
/// > Warning: not implemented in `--target wasm-gc`. This function is only here
5+
/// > to make intellisense happy in code editors that don't understand we are
6+
/// > using `wasm` and not `wasm-gc`.
7+
/// >
8+
/// > This function does have an implementation for `--target wasm`.
9+
pub fn get_addr_fixedbytes(_ : FixedArray[Byte]) -> UInt {
10+
panic() // unsupported on wasm-gc, but this function is here to satisfy my stupid editor
11+
}
12+
13+
///|
14+
/// Returns the WASM object address to a `Bytes` object.
15+
///
16+
/// > Warning: not implemented in `--target wasm-gc`. This function is only here
17+
/// > to make intellisense happy in code editors that don't understand we are
18+
/// > using `wasm` and not `wasm-gc`.
19+
/// >
20+
/// > This function does have an implementation for `--target wasm`.
21+
pub fn get_addr_bytes(_ : Bytes) -> UInt {
322
panic() // unsupported on wasm-gc, but this function is here to satisfy my stupid editor
423
}

src/ffi/top.wasm.mbt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
///|
2+
/// Returns the WASM object address to a `FixedArray[Byte]` object.
3+
#inline
24
#borrow(bytes)
3-
pub extern "wasm" fn get_addr(bytes : FixedArray[Byte]) -> UInt =
5+
pub extern "wasm" fn addr_of_fixedbytes(bytes : FixedArray[Byte]) -> UInt =
6+
#|(func (param i32) (result i32)
7+
#| (i32.add (local.get 0) (i32.const 8)))
8+
9+
///|
10+
/// Returns the WASM object address to a `Bytes` object.
11+
#inline
12+
#borrow(bytes)
13+
pub extern "wasm" fn addr_of_bytes(bytes : Bytes) -> UInt =
414
#|(func (param i32) (result i32)
515
#| (i32.add (local.get 0) (i32.const 8)))

src/fs.mbt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
///
66
/// Returns 0 if the file does not exist.
77
pub fn get_file_size(path : String) -> UInt {
8-
let path_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
8+
let path_arr = @encoding.encode(path, encoding=UTF8)
99
let size = @ffi.get_file_size(
10-
@ffi.get_addr(path_arr),
10+
@ffi.addr_of_bytes(path_arr),
1111
path_arr.length().reinterpret_as_uint(),
1212
)
1313
ignore(path_arr)
@@ -30,9 +30,9 @@ pub fn is_file(path : String) -> Bool {
3030
///
3131
/// Example: loading fonts, images, or save files.
3232
pub fn load_file(path : String) -> File? {
33-
let path_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
33+
let path_arr = @encoding.encode(path, encoding=UTF8)
3434
let size = @ffi.get_file_size(
35-
@ffi.get_addr(path_arr),
35+
@ffi.addr_of_bytes(path_arr),
3636
path_arr.length().reinterpret_as_uint(),
3737
)
3838
match size {
@@ -43,9 +43,9 @@ pub fn load_file(path : String) -> File? {
4343
Byte::default(),
4444
)
4545
@ffi.load_file(
46-
@ffi.get_addr(path_arr),
46+
@ffi.addr_of_bytes(path_arr),
4747
path_arr.length().reinterpret_as_uint(),
48-
@ffi.get_addr(file_arr),
48+
@ffi.addr_of_fixedbytes(file_arr),
4949
file_arr.length().reinterpret_as_uint(),
5050
)
5151
|> ignore
@@ -63,11 +63,11 @@ pub fn load_file(path : String) -> File? {
6363
///
6464
/// Example: loading fonts, images, or save files.
6565
pub fn load_file_to(path : String, output : FixedArray[Byte]) -> UInt {
66-
let text_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
66+
let text_arr = @encoding.encode(path, encoding=UTF8)
6767
let size = @ffi.load_file(
68-
@ffi.get_addr(text_arr),
68+
@ffi.addr_of_bytes(text_arr),
6969
text_arr.length().reinterpret_as_uint(),
70-
@ffi.get_addr(output),
70+
@ffi.addr_of_fixedbytes(output),
7171
output.length().reinterpret_as_uint(),
7272
)
7373
ignore(text_arr)
@@ -125,11 +125,11 @@ pub fn load_file_to_image(path : String) -> Image? {
125125
///
126126
/// Returns the number of bytes written.
127127
pub fn dump_file(path : String, file : File) -> UInt {
128-
let text_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
128+
let text_arr = @encoding.encode(path, encoding=UTF8)
129129
let size = @ffi.load_file(
130-
@ffi.get_addr(text_arr),
130+
@ffi.addr_of_bytes(text_arr),
131131
text_arr.length().reinterpret_as_uint(),
132-
@ffi.get_addr(file.inner()),
132+
@ffi.addr_of_fixedbytes(file.inner()),
133133
file.get_size(),
134134
)
135135
ignore(text_arr)
@@ -207,9 +207,9 @@ pub fn dump_image_to_file(path : String, image : Image) -> UInt {
207207
///
208208
/// Example: removing save files.
209209
pub fn remove_file(path : String) -> Unit {
210-
let path_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
210+
let path_arr = @encoding.encode(path, encoding=UTF8)
211211
@ffi.remove_file(
212-
@ffi.get_addr(path_arr),
212+
@ffi.addr_of_bytes(path_arr),
213213
path_arr.length().reinterpret_as_uint(),
214214
)
215215
ignore(path_arr)

src/graphics.mbt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ pub fn draw_text(
195195
point : Point,
196196
color : Color,
197197
) -> Unit {
198-
let text_arr = @encoding.encode(text, encoding=UTF8).to_fixedarray()
198+
let text_arr = @encoding.encode(text, encoding=UTF8)
199199
@ffi.draw_text(
200-
@ffi.get_addr(text_arr),
200+
@ffi.addr_of_bytes(text_arr),
201201
text_arr.length().reinterpret_as_uint(),
202-
@ffi.get_addr(font.inner()),
202+
@ffi.addr_of_fixedbytes(font.inner()),
203203
font.inner().length().reinterpret_as_uint(),
204204
point.x,
205205
point.y,
@@ -216,9 +216,9 @@ pub fn draw_qr(
216216
black : Color,
217217
white : Color,
218218
) -> Unit {
219-
let text_arr = @encoding.encode(text, encoding=UTF8).to_fixedarray()
219+
let text_arr = @encoding.encode(text, encoding=UTF8)
220220
@ffi.draw_qr(
221-
@ffi.get_addr(text_arr),
221+
@ffi.addr_of_bytes(text_arr),
222222
text_arr.length().reinterpret_as_uint(),
223223
point.x,
224224
point.y,
@@ -232,7 +232,7 @@ pub fn draw_qr(
232232
/// Render an image.
233233
pub fn draw_image(image : Image, point : Point) -> Unit {
234234
@ffi.draw_image(
235-
@ffi.get_addr(image.inner()),
235+
@ffi.addr_of_fixedbytes(image.inner()),
236236
image.inner().length().reinterpret_as_uint(),
237237
point.x,
238238
point.y,
@@ -250,7 +250,7 @@ pub fn draw_sub_image(
250250
sub_size : Size,
251251
) -> Unit {
252252
@ffi.draw_sub_image(
253-
@ffi.get_addr(image.inner()),
253+
@ffi.addr_of_fixedbytes(image.inner()),
254254
image.inner().length().reinterpret_as_uint(),
255255
point.x,
256256
point.y,
@@ -273,7 +273,7 @@ let current_canvas : Ref[Canvas?] = Ref::new(None)
273273
pub fn set_canvas(canvas : Canvas) -> Unit {
274274
current_canvas.val = Some(canvas)
275275
@ffi.set_canvas(
276-
@ffi.get_addr(canvas.inner()),
276+
@ffi.addr_of_fixedbytes(canvas.inner()),
277277
canvas.inner().length().reinterpret_as_uint(),
278278
)
279279
}

src/misc.mbt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
///|
22
/// Log a debug message.
33
pub fn log_debug(s : String) -> Unit {
4-
let arr = @encoding.encode(s, encoding=UTF8).to_fixedarray()
5-
@ffi.log_debug(@ffi.get_addr(arr), arr.length().reinterpret_as_uint())
4+
let arr = @encoding.encode(s, encoding=UTF8)
5+
@ffi.log_debug(@ffi.addr_of_bytes(arr), arr.length().reinterpret_as_uint())
66
ignore(arr)
77
}
88

99
///|
1010
/// Log an error message.
1111
pub fn log_error(s : String) -> Unit {
12-
let arr = @encoding.encode(s, encoding=UTF8).to_fixedarray()
13-
@ffi.log_error(@ffi.get_addr(arr), arr.length().reinterpret_as_uint())
12+
let arr = @encoding.encode(s, encoding=UTF8)
13+
@ffi.log_error(@ffi.addr_of_bytes(arr), arr.length().reinterpret_as_uint())
1414
ignore(arr)
1515
}
1616

@@ -45,19 +45,21 @@ pub fn get_random() -> UInt {
4545
/// Get name of the device.
4646
pub fn get_name(peer : UInt) -> String {
4747
let arr = FixedArray::make(120, Byte::default())
48-
let len = @ffi.get_name(peer, @ffi.get_addr(arr))
48+
let len = @ffi.get_name(peer, @ffi.addr_of_fixedbytes(arr))
4949
let bytes = Bytes::from_fixedarray(arr, len=len.reinterpret_as_int())
5050
@encoding.decode_lossy(bytes[:])
5151
}
5252
5353
///|
5454
/// Restart the app after the current update is finished.
55+
#inline
5556
pub fn restart() -> Unit {
5657
@ffi.restart()
5758
}
5859
5960
///|
6061
/// Exit the app after the current update is finished.
62+
#inline
6163
pub fn quit() -> Unit {
6264
@ffi.quit()
6365
}

src/sudo/top.mbt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
///
44
/// NOTE: this requires that the app has sudo access.
55
pub fn get_file_size(path : String) -> UInt {
6-
let path_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
6+
let path_arr = @encoding.encode(path, encoding=UTF8)
77
let size = @ffi.sudo_get_file_size(
8-
@ffi.get_addr(path_arr),
8+
@ffi.addr_of_bytes(path_arr),
99
path_arr.length().reinterpret_as_uint(),
1010
)
1111
ignore(path_arr) // prevent it from getting cleaned up before here
@@ -17,9 +17,9 @@ pub fn get_file_size(path : String) -> UInt {
1717
///
1818
/// NOTE: this requires that the app has sudo access.
1919
pub fn load_file(path : String) -> @firefly.File? {
20-
let path_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
20+
let path_arr = @encoding.encode(path, encoding=UTF8)
2121
let size = @ffi.sudo_get_file_size(
22-
@ffi.get_addr(path_arr),
22+
@ffi.addr_of_bytes(path_arr),
2323
path_arr.length().reinterpret_as_uint(),
2424
)
2525
match size {
@@ -30,9 +30,9 @@ pub fn load_file(path : String) -> @firefly.File? {
3030
Byte::default(),
3131
)
3232
@ffi.sudo_load_file(
33-
@ffi.get_addr(path_arr),
33+
@ffi.addr_of_bytes(path_arr),
3434
path_arr.length().reinterpret_as_uint(),
35-
@ffi.get_addr(file_arr),
35+
@ffi.addr_of_fixedbytes(file_arr),
3636
file_arr.length().reinterpret_as_uint(),
3737
)
3838
|> ignore
@@ -61,9 +61,9 @@ pub fn load_file(path : String) -> @firefly.File? {
6161
///
6262
/// NOTE: this requires that the app has sudo access.
6363
pub fn list_dirs(path : String) -> Array[String] {
64-
let path_arr = @encoding.encode(path, encoding=UTF8).to_fixedarray()
64+
let path_arr = @encoding.encode(path, encoding=UTF8)
6565
let size = @ffi.sudo_list_dirs_buf_size(
66-
@ffi.get_addr(path_arr),
66+
@ffi.addr_of_bytes(path_arr),
6767
path_arr.length().reinterpret_as_uint(),
6868
)
6969
match size {
@@ -74,9 +74,9 @@ pub fn list_dirs(path : String) -> Array[String] {
7474
Byte::default(),
7575
)
7676
let read_bytes = @ffi.sudo_list_dirs(
77-
@ffi.get_addr(path_arr),
77+
@ffi.addr_of_bytes(path_arr),
7878
path_arr.length().reinterpret_as_uint(),
79-
@ffi.get_addr(dirs_buf),
79+
@ffi.addr_of_fixedbytes(dirs_buf),
8080
dirs_buf.length().reinterpret_as_uint(),
8181
).reinterpret_as_int()
8282
ignore(path_arr) // prevent it from getting cleaned up before here

0 commit comments

Comments
 (0)