Skip to content

Commit 36c73cf

Browse files
kulcsaradamksh8281
authored andcommitted
Add wasi function required to run ripgrep
Signed-off-by: Ádám László Kulcsár <[email protected]>
1 parent ffbc351 commit 36c73cf

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

src/wasi/WASI.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ void WASI::fd_seek(ExecutionState& state, Value* argv, Value* result, Instance*
289289
result[0] = Value(uvwasi_fd_seek(WASI::g_uvwasi, fd, fileDelta, whence, file_size));
290290
}
291291

292+
void WASI::fd_filestat_get(ExecutionState& state, Value* argv, Value* result, Instance* instance)
293+
{
294+
uint32_t fd = argv[0].asI32();
295+
uvwasi_filestat_t* buf = reinterpret_cast<uvwasi_filestat_t*>(get_memory_pointer(instance, argv[1], sizeof(uvwasi_filestat_t)));
296+
297+
result[0] = Value(uvwasi_fd_filestat_get(WASI::g_uvwasi, fd, buf));
298+
}
299+
292300
void WASI::fd_advise(ExecutionState& state, Value* argv, Value* result, Instance* instance)
293301
{
294302
uint32_t fd = argv[0].asI32();
@@ -433,6 +441,11 @@ void WASI::random_get(ExecutionState& state, Value* argv, Value* result, Instanc
433441
result[0] = Value(uvwasi_random_get(WASI::g_uvwasi, buf, length));
434442
}
435443

444+
void WASI::sched_yield(ExecutionState& state, Value* argv, Value* result, Instance* instance)
445+
{
446+
result[0] = Value(uvwasi_sched_yield(WASI::g_uvwasi));
447+
}
448+
436449
} // namespace Walrus
437450

438451
#endif

src/wasi/WASI.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class WASI {
5151
F(fd_fdstat_set_flags, I32I32_RI32) \
5252
F(fd_prestat_get, I32I32_RI32) \
5353
F(fd_prestat_dir_name, I32I32I32_RI32) \
54+
F(fd_filestat_get, I32I32_RI32) \
5455
F(fd_seek, I32I64I32I32_RI32) \
5556
F(fd_advise, I32I64I64I32_RI32) \
5657
F(path_open, I32I32I32I32I32I64I64I32I32_RI32) \
@@ -62,7 +63,8 @@ class WASI {
6263
F(path_unlink_file, I32I32I32_RI32) \
6364
F(poll_oneoff, I32I32I32I32_RI32) \
6465
F(environ_get, I32I32_RI32) \
65-
F(environ_sizes_get, I32I32_RI32)
66+
F(environ_sizes_get, I32I32_RI32) \
67+
F(sched_yield, RI32)
6668

6769
#define ERRORS(ERR) \
6870
ERR(success, "no error") \

test/wasi/fd_filestat_get.wast

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(module
2+
(import "wasi_snapshot_preview1" "path_open" (func $path_open (param i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i32)))
3+
(import "wasi_snapshot_preview1" "fd_close" (func $fd_close (param i32) (result i32)))
4+
(import "wasi_snapshot_preview1" "fd_filestat_get" (func $fd_filestat_get (param i32 i32) (result i32)))
5+
6+
(memory 1 1)
7+
(data (i32.const 100) "./fd_filestat_get.wast")
8+
9+
(func (export "fd_filestat_get") (result i32 i32 i32)
10+
i32.const 3 ;; Directory file descriptior, by default 3 is the first opened directory
11+
i32.const 1 ;; uvwasi_lookupflags_t: UVWASI_LOOKUP_SYMLINK_FOLLOW
12+
i32.const 100 ;; Offset of file name in memory
13+
i32.const 23 ;; Length of file name
14+
i32.const 0 ;; uvwasi_oflags_t: none
15+
i64.const 0x202000 ;; base uvwasi_rights_t: UVWASI_RIGHT_PATH_OPEN, UVWASI_RIGHT_FD_FILESTAT_GET
16+
i64.const 0x202000 ;; inherited uvwasi_rights_t: UVWASI_RIGHT_PATH_OPEN, UVWASI_RIGHT_FD_FILESTAT_GET
17+
i32.const 0 ;; uvwasi_fdflags_t: none
18+
i32.const 0 ;; Offset to store at the opened file descriptor in memory
19+
call $path_open
20+
21+
i32.const 0
22+
i32.load
23+
i32.const 1000
24+
call $fd_filestat_get
25+
26+
i32.const 0
27+
i32.load
28+
call $fd_close
29+
)
30+
31+
(func (export "fd_filestat_get_bad") (result i32 i32 i32 i32)
32+
i32.const 3 ;; Directory file descriptior, by default 3 is the first opened directory
33+
i32.const 1 ;; uvwasi_lookupflags_t: UVWASI_LOOKUP_SYMLINK_FOLLOW
34+
i32.const 100 ;; Offset of file name in memory
35+
i32.const 23 ;; Length of file name
36+
i32.const 0 ;; uvwasi_oflags_t: none
37+
i64.const 0x202000 ;; base uvwasi_rights_t: UVWASI_RIGHT_PATH_OPEN, UVWASI_RIGHT_FD_FILESTAT_GET
38+
i64.const 0x202000 ;; inherited uvwasi_rights_t: UVWASI_RIGHT_PATH_OPEN, UVWASI_RIGHT_FD_FILESTAT_GET
39+
i32.const 0 ;; uvwasi_fdflags_t: none
40+
i32.const 0 ;; Offset to store at the opened file descriptor in memory
41+
call $path_open
42+
43+
i32.const 0
44+
i32.load
45+
i32.const 65535
46+
call $fd_filestat_get
47+
48+
i32.const -1
49+
i32.const 1000
50+
call $fd_filestat_get
51+
52+
i32.const 0
53+
i32.load
54+
call $fd_close
55+
)
56+
)
57+
58+
(assert_return (invoke "fd_filestat_get") (i32.const 0) (i32.const 0) (i32.const 0))
59+
(assert_return (invoke "fd_filestat_get_bad") (i32.const 0) (i32.const 28) (i32.const 8) (i32.const 0))

test/wasi/sched_yield.wast

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(import "wasi_snapshot_preview1" "sched_yield" (func $sched_yield (result i32)))
3+
(func (export "sched_yield") (result i32)
4+
call $sched_yield
5+
)
6+
)
7+
8+
(assert_return (invoke "sched_yield") (i32.const 0))

0 commit comments

Comments
 (0)