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
2 changes: 2 additions & 0 deletions example/wasi_httpbin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pywasm
import socket

Expand All @@ -16,6 +17,7 @@
wasi = pywasm.wasi.Preview1(['wasi_httpbin.wasm'], {}, {})
wasi.fd.append(pywasm.wasi.Preview1.File(
host_fd=client_socket.fileno(),
host_flag=os.O_RDWR,
host_name=client_socket.getsockname(),
host_status=pywasm.wasi.Preview1.FILE_STATUS_OPENED,
pipe=None,
Expand Down
17 changes: 15 additions & 2 deletions pywasm/wasi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dataclasses
import fcntl
import os
import platform
import pywasm.core
Expand Down Expand Up @@ -395,6 +396,7 @@ class Preview1:
@dataclasses.dataclass
class File:
host_fd: int
host_flag: int
host_name: str
host_status: int
pipe: typing.Optional[typing.BinaryIO]
Expand All @@ -414,6 +416,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty
self.fd: typing.List[Preview1.File] = []
self.fd.append(self.File(
host_fd=sys.stdin.fileno(),
host_flag=0,
host_name=sys.stdin.name,
host_status=self.FILE_STATUS_OPENED,
pipe=None,
Expand All @@ -427,6 +430,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty
))
self.fd.append(self.File(
host_fd=sys.stdout.fileno(),
host_flag=0,
host_name=sys.stdout.name,
host_status=self.FILE_STATUS_OPENED,
pipe=None,
Expand All @@ -440,6 +444,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty
))
self.fd.append(self.File(
host_fd=sys.stderr.fileno(),
host_flag=0,
host_name=sys.stderr.name,
host_status=self.FILE_STATUS_OPENED,
pipe=None,
Expand All @@ -456,6 +461,7 @@ def __init__(self, args: typing.List[str], dirs: typing.Dict[str, str], envs: ty
v = os.path.abspath(os.path.normpath(v))
self.fd.append(self.File(
host_fd=os.open(v, os.O_RDONLY | os.O_DIRECTORY),
host_flag=os.O_RDONLY | os.O_DIRECTORY,
host_name=v,
host_status=self.FILE_STATUS_OPENED,
pipe=None,
Expand Down Expand Up @@ -652,12 +658,17 @@ def fd_fdstat_set_flags(self, _: pywasm.core.Machine, args: typing.List[int]) ->
if file.wasm_flag ^ args[1] | fyes != fyes:
# Only support changing the NONBLOCK or APPEND flags.
return [self.ERRNO_INVAL]
file.host_flag &= ~os.O_APPEND
file.host_flag &= ~os.O_NONBLOCK
file.wasm_flag &= ~self.FDFLAGS_APPEND
file.wasm_flag &= ~self.FDFLAGS_NONBLOCK
if args[1] & self.FDFLAGS_APPEND:
file.host_flag |= os.O_APPEND
file.wasm_flag |= self.FDFLAGS_APPEND
if args[1] & self.FDFLAGS_NONBLOCK:
file.host_flag |= os.O_NONBLOCK
file.wasm_flag |= self.FDFLAGS_NONBLOCK
fcntl.fcntl(file.host_fd, fcntl.F_SETFL, file.host_flag)
return [self.ERRNO_SUCCESS]

def fd_fdstat_set_rights(self, _: pywasm.core.Machine, args: typing.List[int]) -> typing.List[int]:
Expand Down Expand Up @@ -871,6 +882,7 @@ def fd_renumber(self, _: pywasm.core.Machine, args: typing.List[int]) -> typing.
if dest.wasm_type != self.FILETYPE_CHARACTER_DEVICE:
os.close(dest.host_fd)
dest.host_fd = stem.host_fd
dest.host_flag = stem.host_flag
dest.host_name = stem.host_name
dest.host_status = stem.host_status
dest.pipe = stem.pipe
Expand Down Expand Up @@ -951,8 +963,6 @@ def fd_write(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Lis
elem = mems.get(elem_ptr, elem_len)
iovs_ptr += 8
data.extend(elem)
if file.wasm_flag & self.FDFLAGS_APPEND:
os.lseek(file.host_fd, 0, os.SEEK_END)
size = [
lambda: os.write(file.host_fd, data),
lambda: file.pipe.write(data),
Expand Down Expand Up @@ -1171,6 +1181,8 @@ def path_open(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Li
flag |= os.O_WRONLY
if args[5] & self.RIGHTS_FD_READ + self.RIGHTS_FD_WRITE == self.RIGHTS_FD_READ + self.RIGHTS_FD_WRITE:
flag |= os.O_RDWR
if args[7] & self.FDFLAGS_APPEND:
flag |= os.O_APPEND
wasm_fd = len(self.fd)
try:
host_fd = os.open(name, flag, 0o644, dir_fd=self.fd[args[0]].host_fd)
Expand Down Expand Up @@ -1217,6 +1229,7 @@ def path_open(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Li
rights_root &= ~void
self.fd.append(Preview1.File(
host_fd=host_fd,
host_flag=flag,
host_name=host_name,
host_status=self.FILE_STATUS_OPENED,
pipe=None,
Expand Down
9 changes: 4 additions & 5 deletions test/wasi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ def cd(dst: str) -> typing.Generator[None, typing.Any, None]:
if os.path.exists(e) and os.path.isdir(e):
shutil.rmtree(e)
case = []
case.extend(sorted(glob.glob('res/wasi-testsuite/tests/assemblyscript/testsuite/*.wasm')))
case.extend(sorted(glob.glob('res/wasi-testsuite/tests/c/testsuite/*.wasm')))
case.extend(sorted(glob.glob('res/wasi-testsuite/tests/rust/testsuite/*.wasm')))
skip = [
]
case.extend(sorted(glob.glob('res/wasi-testsuite/tests/assemblyscript/testsuite/wasm32-wasip1/*.wasm')))
case.extend(sorted(glob.glob('res/wasi-testsuite/tests/c/testsuite/wasm32-wasip1/*.wasm')))
case.extend(sorted(glob.glob('res/wasi-testsuite/tests/rust/testsuite/wasm32-wasip1/*.wasm')))
skip = []
for wasm_path in case:
if wasm_path in skip:
continue
Expand Down
Loading