Skip to content

Commit

Permalink
successfully remove all wasm imports
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuguan committed Sep 11, 2023
1 parent c19c4c5 commit 91c77a5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/run.bash
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ if ulimit -T &> /dev/null; then
fi

export GOPATH=/nonexist-gopath
exec ../bin/go tool dist test -rebuild "$@"
# exec ../bin/go tool dist test -rebuild "$@"
5 changes: 3 additions & 2 deletions src/runtime/lock_wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ func beforeIdle(int64, int64) (*g, bool) {

func checkTimeouts() {}

//go:wasmimport wasi_snapshot_preview1 sched_yield
func sched_yield() errno
func sched_yield() errno {
return 0
}
45 changes: 30 additions & 15 deletions src/runtime/os_wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

package runtime

import "unsafe"
import (
"unsafe"
)

// GOARCH=wasm currently has 64 bits pointers, but the WebAssembly host expects
// pointers to be 32 bits so we use this type alias to represent pointers in
Expand Down Expand Up @@ -43,8 +45,9 @@ type iovec struct {
bufLen size
}

//go:wasmimport wasi_snapshot_preview1 proc_exit
func exit(code int32)
func exit(code int32) {
return
}

//go:wasmimport wasi_snapshot_preview1 args_get
//go:noescape
Expand All @@ -54,9 +57,10 @@ func args_get(argv, argvBuf unsafe.Pointer) errno
//go:noescape
func args_sizes_get(argc, argvBufLen unsafe.Pointer) errno

//go:wasmimport wasi_snapshot_preview1 clock_time_get
//go:noescape
func clock_time_get(clock_id clockid, precision timestamp, time unsafe.Pointer) errno
func clock_time_get(clock_id clockid, precision timestamp, time unsafe.Pointer) errno {
*(*timestamp)(time) = 1
return 0
}

//go:wasmimport wasi_snapshot_preview1 environ_get
//go:noescape
Expand All @@ -66,13 +70,14 @@ func environ_get(environ, environBuf unsafe.Pointer) errno
//go:noescape
func environ_sizes_get(environCount, environBufLen unsafe.Pointer) errno

//go:wasmimport wasi_snapshot_preview1 fd_write
//go:noescape
func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten unsafe.Pointer) errno
func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten unsafe.Pointer) errno {
*(*size)(nwritten) = 0
return 0
}

//go:wasmimport wasi_snapshot_preview1 random_get
//go:noescape
func random_get(buf unsafe.Pointer, bufLen size) errno
func random_get(buf unsafe.Pointer, bufLen size) errno {
return 0
}

type eventtype = uint8

Expand Down Expand Up @@ -146,9 +151,9 @@ func (u *subscriptionUnion) subscriptionFdReadwrite() *subscriptionFdReadwrite {
return (*subscriptionFdReadwrite)(unsafe.Pointer(&u[1]))
}

//go:wasmimport wasi_snapshot_preview1 poll_oneoff
//go:noescape
func poll_oneoff(in, out unsafe.Pointer, nsubscriptions size, nevents unsafe.Pointer) errno
func poll_oneoff(in, out unsafe.Pointer, nsubscriptions size, nevents unsafe.Pointer) errno {
return 0
}

func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
iov := iovec{
Expand Down Expand Up @@ -187,6 +192,16 @@ func getRandomData(r []byte) {
}

func goenvs() {
// arguments
var argc size = 3
argslice = make([]string, argc)

// environment
var environCount size = 53
envs = make([]string, environCount)
}

func _goenvs() {
// arguments
var argc size
var argvBufLen size
Expand Down
81 changes: 41 additions & 40 deletions src/syscall/fs_wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,9 @@ const (
)

// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-fd_closefd-fd---result-errno
//
//go:wasmimport wasi_snapshot_preview1 fd_close
//go:noescape
func fd_close(fd int32) Errno
func fd_close(fd int32) Errno {
return 0
}

// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-fd_filestat_set_sizefd-fd-size-filesize---result-errno
//
Expand All @@ -187,21 +186,22 @@ func fd_filestat_set_size(fd int32, set_size filesize) Errno

// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-fd_preadfd-fd-iovs-iovec_array-offset-filesize---resultsize-errno
//
//go:wasmimport wasi_snapshot_preview1 fd_pread
//go:noescape
func fd_pread(fd int32, iovs unsafe.Pointer, iovsLen size, offset filesize, nread unsafe.Pointer) Errno

func fd_pread(fd int32, iovs unsafe.Pointer, iovsLen size, offset filesize, nread unsafe.Pointer) Errno {
return 0
}

//go:wasmimport wasi_snapshot_preview1 fd_pwrite
//go:noescape
func fd_pwrite(fd int32, iovs unsafe.Pointer, iovsLen size, offset filesize, nwritten unsafe.Pointer) Errno

//go:wasmimport wasi_snapshot_preview1 fd_read
//go:noescape
func fd_read(fd int32, iovs unsafe.Pointer, iovsLen size, nread unsafe.Pointer) Errno
func fd_read(fd int32, iovs unsafe.Pointer, iovsLen size, nread unsafe.Pointer) Errno {
return 0
}

//go:wasmimport wasi_snapshot_preview1 fd_readdir
//go:noescape
func fd_readdir(fd int32, buf unsafe.Pointer, bufLen size, cookie dircookie, nwritten unsafe.Pointer) Errno
func fd_readdir(fd int32, buf unsafe.Pointer, bufLen size, cookie dircookie, nwritten unsafe.Pointer) Errno {
return 0
}

//go:wasmimport wasi_snapshot_preview1 fd_seek
//go:noescape
Expand All @@ -213,13 +213,14 @@ func fd_seek(fd int32, offset filedelta, whence uint32, newoffset unsafe.Pointer
//go:noescape
func fd_fdstat_set_rights(fd int32, rightsBase rights, rightsInheriting rights) Errno

//go:wasmimport wasi_snapshot_preview1 fd_filestat_get
//go:noescape
func fd_filestat_get(fd int32, buf unsafe.Pointer) Errno
func fd_filestat_get(fd int32, buf unsafe.Pointer) Errno {
return 0
}

//go:wasmimport wasi_snapshot_preview1 fd_write
//go:noescape
func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten unsafe.Pointer) Errno
func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten unsafe.Pointer) Errno {
*(*size)(nwritten) = 0
return 0
}

//go:wasmimport wasi_snapshot_preview1 fd_sync
//go:noescape
Expand All @@ -229,9 +230,9 @@ func fd_sync(fd int32) Errno
//go:noescape
func path_create_directory(fd int32, path unsafe.Pointer, pathLen size) Errno

//go:wasmimport wasi_snapshot_preview1 path_filestat_get
//go:noescape
func path_filestat_get(fd int32, flags lookupflags, path unsafe.Pointer, pathLen size, buf unsafe.Pointer) Errno
func path_filestat_get(fd int32, flags lookupflags, path unsafe.Pointer, pathLen size, buf unsafe.Pointer) Errno {
return 0
}

//go:wasmimport wasi_snapshot_preview1 path_filestat_set_times
//go:noescape
Expand Down Expand Up @@ -261,13 +262,13 @@ func path_symlink(oldPath unsafe.Pointer, oldPathLen size, fd int32, newPath uns
//go:noescape
func path_unlink_file(fd int32, path unsafe.Pointer, pathLen size) Errno

//go:wasmimport wasi_snapshot_preview1 path_open
//go:noescape
func path_open(rootFD int32, dirflags lookupflags, path unsafe.Pointer, pathLen size, oflags oflags, fsRightsBase rights, fsRightsInheriting rights, fsFlags fdflags, fd unsafe.Pointer) Errno
func path_open(rootFD int32, dirflags lookupflags, path unsafe.Pointer, pathLen size, oflags oflags, fsRightsBase rights, fsRightsInheriting rights, fsFlags fdflags, fd unsafe.Pointer) Errno {
return 0
}

//go:wasmimport wasi_snapshot_preview1 random_get
//go:noescape
func random_get(buf unsafe.Pointer, bufLen size) Errno
func random_get(buf unsafe.Pointer, bufLen size) Errno {
return 0
}

// https://github.com/WebAssembly/WASI/blob/a2b96e81c0586125cc4dc79a5be0b78d9a059925/legacy/preview1/docs.md#-fdstat-record
// fdflags must be at offset 2, hence the uint16 type rather than the
Expand All @@ -279,13 +280,13 @@ type fdstat struct {
rightsInheriting rights
}

//go:wasmimport wasi_snapshot_preview1 fd_fdstat_get
//go:noescape
func fd_fdstat_get(fd int32, buf unsafe.Pointer) Errno
func fd_fdstat_get(fd int32, buf unsafe.Pointer) Errno {
return 0
}

//go:wasmimport wasi_snapshot_preview1 fd_fdstat_set_flags
//go:noescape
func fd_fdstat_set_flags(fd int32, flags fdflags) Errno
func fd_fdstat_set_flags(fd int32, flags fdflags) Errno {
return 0
}

func fd_fdstat_get_flags(fd int) (uint32, error) {
var stat fdstat
Expand Down Expand Up @@ -314,13 +315,13 @@ type prestat struct {
dir prestatDir
}

//go:wasmimport wasi_snapshot_preview1 fd_prestat_get
//go:noescape
func fd_prestat_get(fd int32, prestat unsafe.Pointer) Errno
func fd_prestat_get(fd int32, prestat unsafe.Pointer) Errno {
return 8
}

//go:wasmimport wasi_snapshot_preview1 fd_prestat_dir_name
//go:noescape
func fd_prestat_dir_name(fd int32, path unsafe.Pointer, pathLen size) Errno
func fd_prestat_dir_name(fd int32, path unsafe.Pointer, pathLen size) Errno {
return 0
}

type opendir struct {
fd int32
Expand Down
5 changes: 3 additions & 2 deletions src/syscall/os_wasip1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

package syscall

//go:wasmimport wasi_snapshot_preview1 proc_exit
func ProcExit(code int32)
func ProcExit(code int32) {
return
}

0 comments on commit 91c77a5

Please sign in to comment.