Skip to content

Commit 4027efa

Browse files
authored
[WasmFS] Use stack allocation for in FS.stat/lstat. NFC (#23910)
1 parent 59f9b09 commit 4027efa

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

src/lib/libwasmfs.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,7 @@ addToLibrary({
180180
create: (path, mode) => FS_create(path, mode),
181181
close: (stream) => FS.handleError(-__wasmfs_close(stream.fd)),
182182
unlink: (path) => FS_unlink(path),
183-
chdir: (path) => withStackSave(() => {
184-
var buffer = stringToUTF8OnStack(path);
185-
return __wasmfs_chdir(buffer);
186-
}),
183+
chdir: (path) => withStackSave(() => __wasmfs_chdir(stringToUTF8OnStack(path))),
187184
read(stream, buffer, offset, length, position) {
188185
var seeking = typeof position != 'undefined';
189186

@@ -263,25 +260,19 @@ addToLibrary({
263260
ino: {{{ makeGetValue('statBuf', C_STRUCTS.stat.st_ino, "u53") }}}
264261
}
265262
},
266-
stat(path) {
267-
var statBuf = _malloc({{{ C_STRUCTS.stat.__size__ }}});
268-
FS.handleError(withStackSave(() =>
269-
__wasmfs_stat(stringToUTF8OnStack(path), statBuf)
270-
));
271-
var stats = FS.statBufToObject(statBuf);
272-
_free(statBuf);
273-
274-
return stats;
263+
stat(path) {
264+
return withStackSave(() => {
265+
var statBuf = stackAlloc({{{ C_STRUCTS.stat.__size__ }}});
266+
FS.handleError(__wasmfs_stat(stringToUTF8OnStack(path), statBuf));
267+
return FS.statBufToObject(statBuf);
268+
});
275269
},
276270
lstat(path) {
277-
var statBuf = _malloc({{{ C_STRUCTS.stat.__size__ }}});
278-
FS.handleError(withStackSave(() =>
279-
__wasmfs_lstat(stringToUTF8OnStack(path), statBuf)
280-
));
281-
var stats = FS.statBufToObject(statBuf);
282-
_free(statBuf);
283-
284-
return stats;
271+
return withStackSave(() => {
272+
var statBuf = stackAlloc({{{ C_STRUCTS.stat.__size__ }}});
273+
FS.handleError(__wasmfs_lstat(stringToUTF8OnStack(path), statBuf));
274+
return FS.statBufToObject(statBuf);
275+
});
285276
},
286277
chmod(path, mode) {
287278
return FS.handleError(withStackSave(() => {

0 commit comments

Comments
 (0)