Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/library_wasmfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mergeInto(LibraryManager.library, {
'$PATH',
'$allocateUTF8',
'$allocateUTF8OnStack',
'$readI53FromI64',
],
$FS : {
// TODO: Clean up the following functions - currently copied from library_fs.js directly.
Expand Down Expand Up @@ -88,8 +89,8 @@ mergeInto(LibraryManager.library, {

// Copy the file into a JS buffer on the heap.
var buf = __wasmfs_read_file(pathName);
// The integer length is returned in the first 8 bytes of the buffer.
var length = {{{ makeGetValue('buf', '0', 'i64') }}};
// The signed integer length resides in the first 8 bytes of the buffer.
var length = {{{ makeGetValue('buf', '0', 'i53') }}};

// Default return type is binary.
// The buffer contents exist 8 bytes after the returned pointer.
Expand Down
3 changes: 3 additions & 0 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,9 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned, ignore, align) {
}

const offset = calcFastOffset(ptr, pos, noNeedFirst);
if (type === 'i53' || type === 'u53') {
return 'readI53From' + (unsigned ? 'U' : 'I') + '64(' + offset + ')';
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already have another use of this in #17459 !


const slab = getHeapForType(type);
let ret = slab + '[' + getHeapOffset(offset, type) + ']';
Expand Down
6 changes: 4 additions & 2 deletions system/lib/wasmfs/js_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ __wasi_fd_t wasmfs_create_file(char* pathname, mode_t mode, backend_t backend);
// The buffer will also contain the file length.
// Caller must free the returned pointer.
void* _wasmfs_read_file(char* path) {
static_assert(sizeof(off_t) == 8, "File offset type must be 64-bit");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this assert here.. I'm pretty sure if this ever changed all kind of stuff would break.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly the assert is a little redundant, but it's static so no overhead, and also it is helpful for the reader, I think, as it indicates this is a contract with the JS side that must not change.


struct stat file;
int err = 0;
err = stat(path, &file);
Expand All @@ -34,8 +36,8 @@ void* _wasmfs_read_file(char* path) {
// first 8 bytes. The remaining bytes will contain the buffer contents. This
// allows the caller to use HEAPU8.subarray(buf + 8, buf + 8 + length).
off_t size = file.st_size;
uint8_t* result = (uint8_t*)malloc((size + sizeof(size)));
*(uint32_t*)result = size;
uint8_t* result = (uint8_t*)malloc(size + sizeof(size));
*(off_t*)result = size;

int fd = open(path, O_RDONLY);
if (fd < 0) {
Expand Down
6 changes: 6 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -11865,6 +11865,12 @@ def test_wasmfs_getdents(self):
def test_wasmfs_readfile(self):
self.do_run_in_out_file_test(test_file('wasmfs/wasmfs_readfile.c'))

@wasmfs_all_backends
def test_wasmfs_readfile_bigint(self):
self.set_setting('WASM_BIGINT')
self.node_args += ['--experimental-wasm-bigint']
self.do_run_in_out_file_test(test_file('wasmfs/wasmfs_readfile.c'))

def test_wasmfs_jsfile(self):
self.set_setting('WASMFS')
self.do_run_in_out_file_test('wasmfs/wasmfs_jsfile.c')
Expand Down
8 changes: 6 additions & 2 deletions tests/wasmfs/wasmfs_readfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Expand All @@ -25,10 +26,13 @@ int main() {
errno = 0;
write(fd, msg, strlen(msg));
assert(errno == 0);
err = close(fd);
assert(err == 0);

EM_ASM({
var output = FS.readFile("/root/test", {encoding : 'utf8'});
out(output);
var output = FS.readFile("/root/test");
out(UTF8ArrayToString(output, 0));
out("Length: " + output.byteLength);
});

EM_ASM({
Expand Down
1 change: 1 addition & 0 deletions tests/wasmfs/wasmfs_readfile.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Success

Length: 8
Fatal error in FS.readFile
Aborted(native code called abort())
Fatal error in FS.readFile
Expand Down