Skip to content

Commit fba33d3

Browse files
committed
fix: string reading
1 parent bcf715b commit fba33d3

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/abi.ts

+8
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ export class WASIAbi {
122122
];
123123

124124
private encoder: TextEncoder;
125+
private decoder: TextDecoder;
125126

126127
constructor() {
127128
this.encoder = new TextEncoder();
129+
this.decoder = new TextDecoder();
128130
}
129131

130132
writeString(memory: DataView, value: string, offset: number): number {
@@ -133,6 +135,12 @@ export class WASIAbi {
133135
buffer.set(bytes);
134136
return bytes.length;
135137
}
138+
139+
readString(memory: DataView, ptr: number, len: number): string {
140+
const buffer = new Uint8Array(memory.buffer, ptr, len);
141+
return this.decoder.decode(buffer);
142+
}
143+
136144
byteLength(value: string): number {
137145
return this.encoder.encode(value).length;
138146
}

src/features/fd.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -814,17 +814,15 @@ export function useMemoryFS(
814814
opened_fd: number
815815
) => {
816816
const view = memoryView();
817-
let path = "";
818-
for (let i = 0; i < pathLen; i++) {
819-
path += String.fromCharCode(view.getUint8(pathPtr + i));
820-
}
821817

822818
if (dirfd < 3) return WASIAbi.WASI_ERRNO_NOTDIR;
823819

824820
const dirEntry = getFileFromFD(dirfd);
825821
if (!dirEntry || dirEntry.node.type !== "dir")
826822
return WASIAbi.WASI_ERRNO_NOTDIR;
827823

824+
const path = abi.readString(view, pathPtr, pathLen);
825+
828826
const guestPath =
829827
(dirEntry.path.endsWith("/") ? dirEntry.path : dirEntry.path + "/") +
830828
path;
@@ -876,17 +874,15 @@ export function useMemoryFS(
876874
opened_fd: number
877875
) => {
878876
const view = memoryView();
879-
let path = "";
880-
for (let i = 0; i < pathLen; i++) {
881-
path += String.fromCharCode(view.getUint8(pathPtr + i));
882-
}
883877

884878
if (dirfd < 3) return WASIAbi.WASI_ERRNO_NOTDIR;
885879

886880
const dirEntry = getFileFromFD(dirfd);
887881
if (!dirEntry || dirEntry.node.type !== "dir")
888882
return WASIAbi.WASI_ERRNO_NOTDIR;
889883

884+
const path = abi.readString(view, pathPtr, pathLen);
885+
890886
const guestPath =
891887
(dirEntry.path.endsWith("/") ? dirEntry.path : dirEntry.path + "/") +
892888
path;
@@ -938,19 +934,15 @@ export function useMemoryFS(
938934
) => {
939935
const view = memoryView();
940936

941-
// Read the relative path from WASM memory.
942-
let guestRelPath = "";
943-
for (let i = 0; i < pathLen; i++) {
944-
guestRelPath += String.fromCharCode(view.getUint8(pathPtr + i));
945-
}
946-
947937
// Get the base FD entry; it must be a directory.
948938
const file = getFileFromFD(fd);
949939
if (!file) return WASIAbi.WASI_ERRNO_BADF;
950940
if (file.node.type !== "dir") {
951941
return WASIAbi.WASI_ERRNO_NOTDIR;
952942
}
953943

944+
const guestRelPath = abi.readString(view, pathPtr, pathLen);
945+
954946
// Compute the full guest path.
955947
const basePath = file.path;
956948
const fullGuestPath = basePath.endsWith("/")

0 commit comments

Comments
 (0)