-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement file related function and simplify function declaration
Implement fd_prestat_get, fd_prestat_dir_name and add tests for them. Also simplify function declarations with a marco. Signed-off-by: Ádám László Kulcsár <[email protected]>
- Loading branch information
1 parent
e31ecbc
commit 977dce0
Showing
5 changed files
with
133 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
(module | ||
(import "wasi_snapshot_preview1" "path_open" (func $path_open (param i32 i32 i32 i32 i32 i64 i64 i32 i32) (result i32))) | ||
(import "wasi_snapshot_preview1" "fd_advise" (func $fd_advise (param i32 i64 i64 i32) (result i32))) | ||
|
||
(memory $mem 1) | ||
(data (i32.const 300) "./write_to_this.txt") ;; Filename in current directory (where Walrus is ran from) | ||
|
||
(func (export "test_advise") (param i32) (result i32) ;; advice is passed as param, return is if the function returned succesfully | ||
i32.const 3 ;; Directory file descriptior, by default 3 is the first opened directory | ||
i32.const 1 ;; lookupflags: directory | ||
i32.const 300 ;; Offset of file name in memory | ||
i32.const 19 ;; Length of file name | ||
i32.const 0 ;; oflags: none | ||
i64.const 128 ;; rights: fd_advise | ||
i64.const 128 ;; rights_inheriting: fd_advise | ||
i32.const 0 ;; fdflags: none | ||
i32.const 0 ;; Offset to store at the opened file descriptor in memory | ||
call $path_open | ||
|
||
i32.eqz ;; fail if file could not be opened | ||
(if | ||
(then) | ||
(else | ||
i32.const 1 | ||
return | ||
) | ||
) | ||
|
||
i32.const 0 | ||
i32.load ;; Get the file descriptor | ||
i64.const 0 ;; Start for 0 offset | ||
i64.const 0 ;; For the entire file | ||
local.get 0 ;; Advise | ||
call $fd_advise | ||
return | ||
) | ||
|
||
(export "memory" (memory 0)) | ||
) | ||
|
||
(assert_return (invoke "test_advise" (i32.const 0)) (i32.const 0)) | ||
(assert_return (invoke "test_advise" (i32.const 1)) (i32.const 0)) | ||
(assert_return (invoke "test_advise" (i32.const 2)) (i32.const 0)) | ||
(assert_return (invoke "test_advise" (i32.const 3)) (i32.const 0)) | ||
(assert_return (invoke "test_advise" (i32.const 4)) (i32.const 0)) | ||
(assert_return (invoke "test_advise" (i32.const 5)) (i32.const 0)) | ||
|
||
(assert_return (invoke "test_advise" (i32.const 6)) (i32.const 28)) ;; Returns invalid function argument, as it should | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(module | ||
(import "wasi_snapshot_preview1" "fd_prestat_get" (func $fd_prestat_get (param i32 i32) (result i32))) | ||
(import "wasi_snapshot_preview1" "fd_prestat_dir_name" (func $fd_prestat_dir_name (param i32 i32 i32) (result i32))) | ||
(memory $mem 1) | ||
|
||
(; fd_prestat_get return information about a preopened file, | ||
meaning one that was passed with '--mapdirs' ;) | ||
|
||
(func (export "test_prestat_get") (param i32) (result i32) | ||
local.get 0 ;; file descriptor | ||
i32.const 0 ;; stored information offset in memory | ||
call $fd_prestat_get | ||
) | ||
|
||
(func (export "test_prestat_dir_name") (param i32) (result i32) | ||
local.get 0 ;; file descriptor | ||
i32.const 0 ;; stored information offset in memory | ||
i32.const 128 ;; max lenght of file path | ||
call $fd_prestat_dir_name | ||
) | ||
) | ||
|
||
(assert_return (invoke "test_prestat_get" (i32.const 3)) (i32.const 0)) | ||
(assert_return (invoke "test_prestat_get" (i32.const 4)) (i32.const 8)) ;; Error 8: bad file descriptor because it does not exist | ||
(assert_return (invoke "test_prestat_dir_name" (i32.const 3)) (i32.const 0)) ;; mapped directory | ||
(assert_return (invoke "test_prestat_dir_name" (i32.const 0)) (i32.const 8)) ;; Error 8: bad file descriptor because stdin is not a mapped directory | ||
(assert_return (invoke "test_prestat_dir_name" (i32.const 4)) (i32.const 8)) ;; Error 8: bad file descriptor because it does not exist | ||
|