aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-07-10 19:30:09 +0900
committernsfisis <nsfisis@gmail.com>2024-07-10 19:30:20 +0900
commit2984d445831ebf0cd8c909f589dc203018ae2b0b (patch)
tree465c6e304b85fcd322389dd8b530cea5acabd929
parent1011da8798ba7370d914dbce9feac1b06ab81ed5 (diff)
downloadphp-waddiwasi-2984d445831ebf0cd8c909f589dc203018ae2b0b.tar.gz
php-waddiwasi-2984d445831ebf0cd8c909f589dc203018ae2b0b.tar.zst
php-waddiwasi-2984d445831ebf0cd8c909f589dc203018ae2b0b.zip
test: implement "register" action of WAST
-rw-r--r--BUGS.txt6
-rw-r--r--examples/php-on-wasm/php-wasm.php204
-rw-r--r--examples/rubyvm-on-php-on-wasm/php-wasm.php204
-rw-r--r--src/Execution/Extern.php28
-rw-r--r--src/Execution/Externs/Func.php16
-rw-r--r--src/Execution/Externs/Global_.php16
-rw-r--r--src/Execution/Externs/Mem.php16
-rw-r--r--src/Execution/Externs/Table.php16
-rw-r--r--src/Execution/Runtime.php21
-rw-r--r--src/Execution/Store.php19
-rw-r--r--tests/src/SpecTestsuites/SpecTestsuiteBase.php20
11 files changed, 358 insertions, 208 deletions
diff --git a/BUGS.txt b/BUGS.txt
index 0026112..e5d5f63 100644
--- a/BUGS.txt
+++ b/BUGS.txt
@@ -20,12 +20,6 @@
* NamesTest
* StartTest
-## Register
-
-* RefFuncTest
-* TableCopyTest
-* TableInitTest
-
## Misc.
* CustomTest
diff --git a/examples/php-on-wasm/php-wasm.php b/examples/php-on-wasm/php-wasm.php
index 96625b8..336602c 100644
--- a/examples/php-on-wasm/php-wasm.php
+++ b/examples/php-on-wasm/php-wasm.php
@@ -6,6 +6,7 @@ require_once __DIR__ . '/../../vendor/autoload.php';
use Nsfisis\Waddiwasi\BinaryFormat\Decoder;
use Nsfisis\Waddiwasi\BinaryFormat\InvalidBinaryFormatException;
+use Nsfisis\Waddiwasi\Execution\Extern;
use Nsfisis\Waddiwasi\Execution\FuncInst;
use Nsfisis\Waddiwasi\Execution\Refs;
use Nsfisis\Waddiwasi\Execution\Runtime;
@@ -31,107 +32,114 @@ try {
exit(1);
}
-$hostFuncs = [
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__invoke_iii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiii(...)),
- makeHostFunc('(i32) -> ()', hostFunc__env__invoke_v(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__invoke_ii(...)),
- makeHostFunc('() -> ()', hostFunc__env__abort(...)),
- makeHostFunc('(i32) -> ()', hostFunc__env__exit(...)),
- makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viii(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__invoke_vii(...)),
- makeHostFunc('(i32, i32) -> ()', hostFunc__env__invoke_vi(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__invoke_i(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiii(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiii(...)),
- makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env____assert_fail(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiiiiii(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__strftime(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__getaddrinfo(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__gethostbyname_r(...)),
- makeHostFunc('() -> (i32)', hostFunc__env__getdtablesize(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobyname(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobynumber(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__strptime(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__getnameinfo(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiiii(...)),
- makeHostFunc('(i32, i32, i32, f64, i32, i32) -> ()', hostFunc__env__invoke_viidii(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__getcontext(...)),
- makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__makecontext(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__swapcontext(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_sizes_get(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_get(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_fcntl64(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ioctl(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_close(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_read(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_write(...)),
- makeHostFunc('(i32) -> ()', hostFunc__wasi_snapshot_preview1__proc_exit(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_faccessat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_chdir(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_chmod(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_fchownat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_dup(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_dup3(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__emscripten_memcpy_js(...)),
- makeHostFunc('() -> (f64)', hostFunc__env__emscripten_date_now(...)),
- makeHostFunc('() -> (i32)', hostFunc__env___emscripten_get_now_is_monotonic(...)),
- makeHostFunc('() -> (f64)', hostFunc__env__emscripten_get_now(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_fdatasync(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_openat(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_fstat64(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_stat64(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_newfstatat(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_lstat64(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_sync(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_getcwd(...)),
- makeHostFunc('(i32) -> ()', hostFunc__env__emscripten_err(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_fdstat_get(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_mkdirat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_pipe(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_poll(...)),
- makeHostFunc('(i32, i32) -> ()', hostFunc__env____call_sighandler(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_getdents64(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_readlinkat(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_renameat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_rmdir(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall__newselect(...)),
- makeHostFunc('(i32, f64) -> (i32)', hostFunc__env___setitimer_js(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_statfs64(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_symlink(...)),
- makeHostFunc('() -> (i32)', hostFunc__env__emscripten_get_heap_max(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___tzset_js(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_unlinkat(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_utimensat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__emscripten_resize_heap(...)),
- makeHostFunc('() -> ()', hostFunc__env___emscripten_throw_longjmp(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_accept4(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_bind(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_connect(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getpeername(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockname(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockopt(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_listen(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_recvfrom(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_sendto(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_socket(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ftruncate64(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_seek(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env___mktime_js(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___localtime_js(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___gmtime_js(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___munmap_js(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___mmap_js(...)),
+$importModules = [
+ 'env' => [
+ 'invoke_iii' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__invoke_iii(...)),
+ 'invoke_iiiii' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiii(...)),
+ 'invoke_v' => makeHostFunc('(i32) -> ()', hostFunc__env__invoke_v(...)),
+ 'invoke_ii' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__invoke_ii(...)),
+ 'abort' => makeHostFunc('() -> ()', hostFunc__env__abort(...)),
+ 'exit' => makeHostFunc('(i32) -> ()', hostFunc__env__exit(...)),
+ 'invoke_viii' => makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viii(...)),
+ 'invoke_vii' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__invoke_vii(...)),
+ 'invoke_vi' => makeHostFunc('(i32, i32) -> ()', hostFunc__env__invoke_vi(...)),
+ 'invoke_i' => makeHostFunc('(i32) -> (i32)', hostFunc__env__invoke_i(...)),
+ 'invoke_iiiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiii(...)),
+ 'invoke_iiii' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiii(...)),
+ '__assert_fail' => makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env____assert_fail(...)),
+ 'invoke_viiii' => makeHostFunc('(i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiii(...)),
+ 'invoke_viiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiii(...)),
+ 'invoke_iiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiii(...)),
+ 'invoke_iiiiiiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiiiiii(...)),
+ 'strftime' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__strftime(...)),
+ 'getaddrinfo' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__getaddrinfo(...)),
+ 'gethostbyname_r' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__gethostbyname_r(...)),
+ 'getdtablesize' => makeHostFunc('() -> (i32)', hostFunc__env__getdtablesize(...)),
+ 'getprotobyname' => makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobyname(...)),
+ 'getprotobynumber' => makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobynumber(...)),
+ 'strptime' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__strptime(...)),
+ 'getnameinfo' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__getnameinfo(...)),
+ 'invoke_viiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiiii(...)),
+ 'invoke_viidii' => makeHostFunc('(i32, i32, i32, f64, i32, i32) -> ()', hostFunc__env__invoke_viidii(...)),
+ 'getcontext' => makeHostFunc('(i32) -> (i32)', hostFunc__env__getcontext(...)),
+ 'makecontext' => makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__makecontext(...)),
+ 'swapcontext' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__swapcontext(...)),
+ '__syscall_fcntl64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_fcntl64(...)),
+ '__syscall_ioctl' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ioctl(...)),
+ '__syscall_faccessat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_faccessat(...)),
+ '__syscall_chdir' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_chdir(...)),
+ '__syscall_chmod' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_chmod(...)),
+ '__syscall_fchownat' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_fchownat(...)),
+ '__syscall_dup' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_dup(...)),
+ '__syscall_dup3' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_dup3(...)),
+ 'emscripten_memcpy_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__emscripten_memcpy_js(...)),
+ 'emscripten_date_now' => makeHostFunc('() -> (f64)', hostFunc__env__emscripten_date_now(...)),
+ '_emscripten_get_now_is_monotonic' => makeHostFunc('() -> (i32)', hostFunc__env___emscripten_get_now_is_monotonic(...)),
+ 'emscripten_get_now' => makeHostFunc('() -> (f64)', hostFunc__env__emscripten_get_now(...)),
+ '__syscall_fdatasync' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_fdatasync(...)),
+ '__syscall_openat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_openat(...)),
+ '__syscall_fstat64' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_fstat64(...)),
+ '__syscall_stat64' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_stat64(...)),
+ '__syscall_newfstatat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_newfstatat(...)),
+ '__syscall_lstat64' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_lstat64(...)),
+ '__syscall_getcwd' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_getcwd(...)),
+ 'emscripten_err' => makeHostFunc('(i32) -> ()', hostFunc__env__emscripten_err(...)),
+ '__syscall_mkdirat' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_mkdirat(...)),
+ '__syscall_pipe' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_pipe(...)),
+ '__syscall_poll' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_poll(...)),
+ '__call_sighandler' => makeHostFunc('(i32, i32) -> ()', hostFunc__env____call_sighandler(...)),
+ '__syscall_getdents64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_getdents64(...)),
+ '__syscall_readlinkat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_readlinkat(...)),
+ '__syscall_renameat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_renameat(...)),
+ '__syscall_rmdir' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_rmdir(...)),
+ '__syscall__newselect' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall__newselect(...)),
+ '_setitimer_js' => makeHostFunc('(i32, f64) -> (i32)', hostFunc__env___setitimer_js(...)),
+ '__syscall_statfs64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_statfs64(...)),
+ '__syscall_symlink' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_symlink(...)),
+ 'emscripten_get_heap_max' => makeHostFunc('() -> (i32)', hostFunc__env__emscripten_get_heap_max(...)),
+ '_tzset_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___tzset_js(...)),
+ '__syscall_unlinkat' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_unlinkat(...)),
+ '__syscall_utimensat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_utimensat(...)),
+ 'emscripten_resize_heap' => makeHostFunc('(i32) -> (i32)', hostFunc__env__emscripten_resize_heap(...)),
+ '_emscripten_throw_longjmp' => makeHostFunc('() -> ()', hostFunc__env___emscripten_throw_longjmp(...)),
+ '__syscall_accept4' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_accept4(...)),
+ '__syscall_bind' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_bind(...)),
+ '__syscall_connect' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_connect(...)),
+ '__syscall_getpeername' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getpeername(...)),
+ '__syscall_getsockname' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockname(...)),
+ '__syscall_getsockopt' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockopt(...)),
+ '__syscall_listen' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_listen(...)),
+ '__syscall_recvfrom' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_recvfrom(...)),
+ '__syscall_sendto' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_sendto(...)),
+ '__syscall_socket' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_socket(...)),
+ '__syscall_ftruncate64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ftruncate64(...)),
+ '_mktime_js' => makeHostFunc('(i32) -> (i32)', hostFunc__env___mktime_js(...)),
+ '_localtime_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___localtime_js(...)),
+ '_gmtime_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___gmtime_js(...)),
+ '_munmap_js' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___munmap_js(...)),
+ '_mmap_js' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___mmap_js(...)),
+ ],
+ 'wasi_snapshot_preview1' => [
+ 'environ_sizes_get' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_sizes_get(...)),
+ 'environ_get' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_get(...)),
+ 'fd_close' => makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_close(...)),
+ 'fd_read' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_read(...)),
+ 'fd_write' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_write(...)),
+ 'proc_exit' => makeHostFunc('(i32) -> ()', hostFunc__wasi_snapshot_preview1__proc_exit(...)),
+ 'fd_sync' => makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_sync(...)),
+ 'fd_fdstat_get' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_fdstat_get(...)),
+ 'fd_seek' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_seek(...)),
+ ],
];
+
$store = Store::empty();
-$externVals = [];
-foreach ($hostFuncs as $hostFunc) {
- $externVals[] = $store->registerFunc($hostFunc);
+$importObj = [];
+foreach ($importModules as $importModuleName => $importModule) {
+ foreach ($importModule as $importName => $import) {
+ $importObj[$importModuleName][$importName] = Extern::Func($import);
+ }
}
-$runtime = Runtime::instantiate($store, $module, $externVals);
+$runtime = Runtime::instantiate($store, $module, $importObj);
$codePtr = allocateStringOnWasmMemory($runtime, PHP_HELLO_WORLD);
$results = $runtime->invoke("php_wasm_run", [$codePtr]);
diff --git a/examples/rubyvm-on-php-on-wasm/php-wasm.php b/examples/rubyvm-on-php-on-wasm/php-wasm.php
index 1efb89f..5d411cc 100644
--- a/examples/rubyvm-on-php-on-wasm/php-wasm.php
+++ b/examples/rubyvm-on-php-on-wasm/php-wasm.php
@@ -6,6 +6,7 @@ require_once __DIR__ . '/vendor/autoload.php';
use Nsfisis\Waddiwasi\BinaryFormat\Decoder;
use Nsfisis\Waddiwasi\BinaryFormat\InvalidBinaryFormatException;
+use Nsfisis\Waddiwasi\Execution\Extern;
use Nsfisis\Waddiwasi\Execution\FuncInst;
use Nsfisis\Waddiwasi\Execution\MemInst;
use Nsfisis\Waddiwasi\Execution\Refs;
@@ -32,107 +33,114 @@ try {
exit(1);
}
-$hostFuncs = [
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__invoke_iii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiii(...)),
- makeHostFunc('(i32) -> ()', hostFunc__env__invoke_v(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__invoke_ii(...)),
- makeHostFunc('() -> ()', hostFunc__env__abort(...)),
- makeHostFunc('(i32) -> ()', hostFunc__env__exit(...)),
- makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viii(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__invoke_vii(...)),
- makeHostFunc('(i32, i32) -> ()', hostFunc__env__invoke_vi(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__invoke_i(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiii(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiii(...)),
- makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env____assert_fail(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiii(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiiiiii(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__strftime(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__getaddrinfo(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__gethostbyname_r(...)),
- makeHostFunc('() -> (i32)', hostFunc__env__getdtablesize(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobyname(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobynumber(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__strptime(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__getnameinfo(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiiii(...)),
- makeHostFunc('(i32, i32, i32, f64, i32, i32) -> ()', hostFunc__env__invoke_viidii(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__getcontext(...)),
- makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__makecontext(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__swapcontext(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_sizes_get(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_get(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_fcntl64(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ioctl(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_close(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_read(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_write(...)),
- makeHostFunc('(i32) -> ()', hostFunc__wasi_snapshot_preview1__proc_exit(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_faccessat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_chdir(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_chmod(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_fchownat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_dup(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_dup3(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__emscripten_memcpy_js(...)),
- makeHostFunc('() -> (f64)', hostFunc__env__emscripten_date_now(...)),
- makeHostFunc('() -> (i32)', hostFunc__env___emscripten_get_now_is_monotonic(...)),
- makeHostFunc('() -> (f64)', hostFunc__env__emscripten_get_now(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_fdatasync(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_openat(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_fstat64(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_stat64(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_newfstatat(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_lstat64(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_sync(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_getcwd(...)),
- makeHostFunc('(i32) -> ()', hostFunc__env__emscripten_err(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_fdstat_get(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_mkdirat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_pipe(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_poll(...)),
- makeHostFunc('(i32, i32) -> ()', hostFunc__env____call_sighandler(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_getdents64(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_readlinkat(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_renameat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_rmdir(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall__newselect(...)),
- makeHostFunc('(i32, f64) -> (i32)', hostFunc__env___setitimer_js(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_statfs64(...)),
- makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_symlink(...)),
- makeHostFunc('() -> (i32)', hostFunc__env__emscripten_get_heap_max(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___tzset_js(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_unlinkat(...)),
- makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_utimensat(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env__emscripten_resize_heap(...)),
- makeHostFunc('() -> ()', hostFunc__env___emscripten_throw_longjmp(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_accept4(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_bind(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_connect(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getpeername(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockname(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockopt(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_listen(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_recvfrom(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_sendto(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_socket(...)),
- makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ftruncate64(...)),
- makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_seek(...)),
- makeHostFunc('(i32) -> (i32)', hostFunc__env___mktime_js(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___localtime_js(...)),
- makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___gmtime_js(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___munmap_js(...)),
- makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___mmap_js(...)),
+$importModules = [
+ 'env' => [
+ 'invoke_iii' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__invoke_iii(...)),
+ 'invoke_iiiii' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiii(...)),
+ 'invoke_v' => makeHostFunc('(i32) -> ()', hostFunc__env__invoke_v(...)),
+ 'invoke_ii' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__invoke_ii(...)),
+ 'abort' => makeHostFunc('() -> ()', hostFunc__env__abort(...)),
+ 'exit' => makeHostFunc('(i32) -> ()', hostFunc__env__exit(...)),
+ 'invoke_viii' => makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viii(...)),
+ 'invoke_vii' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__invoke_vii(...)),
+ 'invoke_vi' => makeHostFunc('(i32, i32) -> ()', hostFunc__env__invoke_vi(...)),
+ 'invoke_i' => makeHostFunc('(i32) -> (i32)', hostFunc__env__invoke_i(...)),
+ 'invoke_iiiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiii(...)),
+ 'invoke_iiii' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiii(...)),
+ '__assert_fail' => makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env____assert_fail(...)),
+ 'invoke_viiii' => makeHostFunc('(i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiii(...)),
+ 'invoke_viiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiii(...)),
+ 'invoke_iiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiii(...)),
+ 'invoke_iiiiiiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__invoke_iiiiiiiiii(...)),
+ 'strftime' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__strftime(...)),
+ 'getaddrinfo' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env__getaddrinfo(...)),
+ 'gethostbyname_r' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__gethostbyname_r(...)),
+ 'getdtablesize' => makeHostFunc('() -> (i32)', hostFunc__env__getdtablesize(...)),
+ 'getprotobyname' => makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobyname(...)),
+ 'getprotobynumber' => makeHostFunc('(i32) -> (i32)', hostFunc__env__getprotobynumber(...)),
+ 'strptime' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env__strptime(...)),
+ 'getnameinfo' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env__getnameinfo(...)),
+ 'invoke_viiiiii' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> ()', hostFunc__env__invoke_viiiiii(...)),
+ 'invoke_viidii' => makeHostFunc('(i32, i32, i32, f64, i32, i32) -> ()', hostFunc__env__invoke_viidii(...)),
+ 'getcontext' => makeHostFunc('(i32) -> (i32)', hostFunc__env__getcontext(...)),
+ 'makecontext' => makeHostFunc('(i32, i32, i32, i32) -> ()', hostFunc__env__makecontext(...)),
+ 'swapcontext' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env__swapcontext(...)),
+ '__syscall_fcntl64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_fcntl64(...)),
+ '__syscall_ioctl' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ioctl(...)),
+ '__syscall_faccessat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_faccessat(...)),
+ '__syscall_chdir' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_chdir(...)),
+ '__syscall_chmod' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_chmod(...)),
+ '__syscall_fchownat' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_fchownat(...)),
+ '__syscall_dup' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_dup(...)),
+ '__syscall_dup3' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_dup3(...)),
+ 'emscripten_memcpy_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env__emscripten_memcpy_js(...)),
+ 'emscripten_date_now' => makeHostFunc('() -> (f64)', hostFunc__env__emscripten_date_now(...)),
+ '_emscripten_get_now_is_monotonic' => makeHostFunc('() -> (i32)', hostFunc__env___emscripten_get_now_is_monotonic(...)),
+ 'emscripten_get_now' => makeHostFunc('() -> (f64)', hostFunc__env__emscripten_get_now(...)),
+ '__syscall_fdatasync' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_fdatasync(...)),
+ '__syscall_openat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_openat(...)),
+ '__syscall_fstat64' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_fstat64(...)),
+ '__syscall_stat64' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_stat64(...)),
+ '__syscall_newfstatat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_newfstatat(...)),
+ '__syscall_lstat64' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_lstat64(...)),
+ '__syscall_getcwd' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_getcwd(...)),
+ 'emscripten_err' => makeHostFunc('(i32) -> ()', hostFunc__env__emscripten_err(...)),
+ '__syscall_mkdirat' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_mkdirat(...)),
+ '__syscall_pipe' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_pipe(...)),
+ '__syscall_poll' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_poll(...)),
+ '__call_sighandler' => makeHostFunc('(i32, i32) -> ()', hostFunc__env____call_sighandler(...)),
+ '__syscall_getdents64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_getdents64(...)),
+ '__syscall_readlinkat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_readlinkat(...)),
+ '__syscall_renameat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_renameat(...)),
+ '__syscall_rmdir' => makeHostFunc('(i32) -> (i32)', hostFunc__env____syscall_rmdir(...)),
+ '__syscall__newselect' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall__newselect(...)),
+ '_setitimer_js' => makeHostFunc('(i32, f64) -> (i32)', hostFunc__env___setitimer_js(...)),
+ '__syscall_statfs64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_statfs64(...)),
+ '__syscall_symlink' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__env____syscall_symlink(...)),
+ 'emscripten_get_heap_max' => makeHostFunc('() -> (i32)', hostFunc__env__emscripten_get_heap_max(...)),
+ '_tzset_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___tzset_js(...)),
+ '__syscall_unlinkat' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_unlinkat(...)),
+ '__syscall_utimensat' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_utimensat(...)),
+ 'emscripten_resize_heap' => makeHostFunc('(i32) -> (i32)', hostFunc__env__emscripten_resize_heap(...)),
+ '_emscripten_throw_longjmp' => makeHostFunc('() -> ()', hostFunc__env___emscripten_throw_longjmp(...)),
+ '__syscall_accept4' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_accept4(...)),
+ '__syscall_bind' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_bind(...)),
+ '__syscall_connect' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_connect(...)),
+ '__syscall_getpeername' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getpeername(...)),
+ '__syscall_getsockname' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockname(...)),
+ '__syscall_getsockopt' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_getsockopt(...)),
+ '__syscall_listen' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_listen(...)),
+ '__syscall_recvfrom' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_recvfrom(...)),
+ '__syscall_sendto' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_sendto(...)),
+ '__syscall_socket' => makeHostFunc('(i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env____syscall_socket(...)),
+ '__syscall_ftruncate64' => makeHostFunc('(i32, i32, i32) -> (i32)', hostFunc__env____syscall_ftruncate64(...)),
+ '_mktime_js' => makeHostFunc('(i32) -> (i32)', hostFunc__env___mktime_js(...)),
+ '_localtime_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___localtime_js(...)),
+ '_gmtime_js' => makeHostFunc('(i32, i32, i32) -> ()', hostFunc__env___gmtime_js(...)),
+ '_munmap_js' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___munmap_js(...)),
+ '_mmap_js' => makeHostFunc('(i32, i32, i32, i32, i32, i32, i32, i32) -> (i32)', hostFunc__env___mmap_js(...)),
+ ],
+ 'wasi_snapshot_preview1' => [
+ 'environ_sizes_get' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_sizes_get(...)),
+ 'environ_get' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__environ_get(...)),
+ 'fd_close' => makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_close(...)),
+ 'fd_read' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_read(...)),
+ 'fd_write' => makeHostFunc('(i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_write(...)),
+ 'proc_exit' => makeHostFunc('(i32) -> ()', hostFunc__wasi_snapshot_preview1__proc_exit(...)),
+ 'fd_sync' => makeHostFunc('(i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_sync(...)),
+ 'fd_fdstat_get' => makeHostFunc('(i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_fdstat_get(...)),
+ 'fd_seek' => makeHostFunc('(i32, i32, i32, i32, i32) -> (i32)', hostFunc__wasi_snapshot_preview1__fd_seek(...)),
+ ],
];
+
$store = Store::empty();
-$externVals = [];
-foreach ($hostFuncs as $hostFunc) {
- $externVals[] = $store->registerFunc($hostFunc);
+$importObj = [];
+foreach ($importModules as $importModuleName => $importModule) {
+ foreach ($importModule as $importName => $import) {
+ $importObj[$importModuleName][$importName] = Extern::Func($import);
+ }
}
-$runtime = Runtime::instantiate($store, $module, $externVals);
+$runtime = Runtime::instantiate($store, $module, $importObj);
$codePtr = allocateStringOnWasmMemory($runtime, strtr(PHP_HELLO_WORLD, ['%DIR%' => __DIR__]));
$results = $runtime->invoke("php_wasm_run", [$codePtr]);
diff --git a/src/Execution/Extern.php b/src/Execution/Extern.php
new file mode 100644
index 0000000..e0b695e
--- /dev/null
+++ b/src/Execution/Extern.php
@@ -0,0 +1,28 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution;
+
+abstract readonly class Extern
+{
+ final public static function Func(FuncInst $func): Externs\Func
+ {
+ return new Externs\Func($func);
+ }
+
+ final public static function Table(TableInst $table): Externs\Table
+ {
+ return new Externs\Table($table);
+ }
+
+ final public static function Mem(MemInst $mem): Externs\Mem
+ {
+ return new Externs\Mem($mem);
+ }
+
+ final public static function Global_(GlobalInst $global): Externs\Global_
+ {
+ return new Externs\Global_($global);
+ }
+}
diff --git a/src/Execution/Externs/Func.php b/src/Execution/Externs/Func.php
new file mode 100644
index 0000000..124309c
--- /dev/null
+++ b/src/Execution/Externs/Func.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\FuncInst;
+
+final readonly class Func extends Extern
+{
+ protected function __construct(
+ public FuncInst $func,
+ ) {
+ }
+}
diff --git a/src/Execution/Externs/Global_.php b/src/Execution/Externs/Global_.php
new file mode 100644
index 0000000..ccf9ad0
--- /dev/null
+++ b/src/Execution/Externs/Global_.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\GlobalInst;
+
+final readonly class Global_ extends Extern
+{
+ protected function __construct(
+ public GlobalInst $global,
+ ) {
+ }
+}
diff --git a/src/Execution/Externs/Mem.php b/src/Execution/Externs/Mem.php
new file mode 100644
index 0000000..bd1f119
--- /dev/null
+++ b/src/Execution/Externs/Mem.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\MemInst;
+
+final readonly class Mem extends Extern
+{
+ protected function __construct(
+ public MemInst $mem,
+ ) {
+ }
+}
diff --git a/src/Execution/Externs/Table.php b/src/Execution/Externs/Table.php
new file mode 100644
index 0000000..fde1e32
--- /dev/null
+++ b/src/Execution/Externs/Table.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Waddiwasi\Execution\Externs;
+
+use Nsfisis\Waddiwasi\Execution\Extern;
+use Nsfisis\Waddiwasi\Execution\TableInst;
+
+final readonly class Table extends Extern
+{
+ protected function __construct(
+ public TableInst $table,
+ ) {
+ }
+}
diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php
index 02c526c..2cc37b2 100644
--- a/src/Execution/Runtime.php
+++ b/src/Execution/Runtime.php
@@ -56,11 +56,30 @@ final class Runtime
}
/**
- * @param list<ExternVal> $externVals
+ * @param array<string, array<string, Extern>> $imports
*/
public static function instantiate(
Store $store,
Module $module,
+ array $imports,
+ ): self {
+ $externVals = [];
+ foreach ($module->imports as $import) {
+ $extern = $imports[$import->module][$import->name] ?? null;
+ if ($extern === null) {
+ throw new RuntimeException("instantiate: import not found: {$import->module}::{$import->name}");
+ }
+ $externVals[] = $store->register($extern);
+ }
+ return self::doInstantiate($store, $module, $externVals);
+ }
+
+ /**
+ * @param list<ExternVal> $externVals
+ */
+ private static function doInstantiate(
+ Store $store,
+ Module $module,
array $externVals,
): self {
$allocator = new Allocator($store);
diff --git a/src/Execution/Store.php b/src/Execution/Store.php
index 2b1a26d..52a8ce1 100644
--- a/src/Execution/Store.php
+++ b/src/Execution/Store.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Nsfisis\Waddiwasi\Execution;
+use RuntimeException;
use function count;
final class Store
@@ -31,9 +32,21 @@ final class Store
return new self([], [], [], [], [], []);
}
- public function registerFunc(FuncInst $func): ExternVals\Func
+ public function register(Extern $extern): ExternVal
{
- $this->funcs[] = $func;
- return ExternVal::Func(count($this->funcs) - 1);
+ match ($extern::class) {
+ Externs\Func::class => $this->funcs[] = $extern->func,
+ Externs\Table::class => $this->tables[] = $extern->table,
+ Externs\Mem::class => $this->mems[] = $extern->mem,
+ Externs\Global_::class => $this->globals[] = $extern->global,
+ default => throw new RuntimeException("unreachable"),
+ };
+ return match ($extern::class) {
+ Externs\Func::class => ExternVal::Func(count($this->funcs) - 1),
+ Externs\Table::class => ExternVal::Table(count($this->tables) - 1),
+ Externs\Mem::class => ExternVal::Mem(count($this->mems) - 1),
+ Externs\Global_::class => ExternVal::Global_(count($this->globals) - 1),
+ default => throw new RuntimeException("unreachable"),
+ };
}
}
diff --git a/tests/src/SpecTestsuites/SpecTestsuiteBase.php b/tests/src/SpecTestsuites/SpecTestsuiteBase.php
index 65176a5..2d37d2e 100644
--- a/tests/src/SpecTestsuites/SpecTestsuiteBase.php
+++ b/tests/src/SpecTestsuites/SpecTestsuiteBase.php
@@ -6,6 +6,7 @@ namespace Nsfisis\Waddiwasi\Tests\SpecTestsuites;
use Nsfisis\Waddiwasi\BinaryFormat\Decoder;
use Nsfisis\Waddiwasi\BinaryFormat\InvalidBinaryFormatException;
+use Nsfisis\Waddiwasi\Execution\Extern;
use Nsfisis\Waddiwasi\Execution\Ref;
use Nsfisis\Waddiwasi\Execution\Refs\RefExtern;
use Nsfisis\Waddiwasi\Execution\Refs\RefFunc;
@@ -25,6 +26,8 @@ abstract class SpecTestsuiteBase extends TestCase
{
private static $modules = [];
private static $runtimes = [];
+ private static $registeredModules = [];
+ private static $registeredRuntimes = [];
protected function runModuleCommand(
string $filename,
@@ -36,7 +39,15 @@ abstract class SpecTestsuiteBase extends TestCase
$wasmBinary = file_get_contents($filePath);
$module = (new Decoder($wasmBinary))->decode();
self::$modules[$moduleName] = $module;
- $runtime = Runtime::instantiate(Store::empty(), $module, []);
+ $importObj = [];
+ foreach (self::$registeredModules as $registeredModuleName => $registeredModule) {
+ $registeredRuntime = self::$registeredRuntimes[$registeredModuleName];
+ foreach ($registeredModule->exports as $export) {
+ $fn = $registeredRuntime->store->funcs[$registeredRuntime->getExport($export->name)->addr];
+ $importObj[$registeredModuleName][$export->name] = Extern::Func($fn);
+ }
+ }
+ $runtime = Runtime::instantiate(Store::empty(), $module, $importObj);
self::$runtimes[$moduleName] = $runtime;
$this->assertTrue(true);
}
@@ -146,7 +157,12 @@ abstract class SpecTestsuiteBase extends TestCase
string $as,
int $line,
): void {
- $this->assertTrue(false, "register");
+ $targetModuleName = $name ?? '_';
+ $targetModule = self::$modules[$targetModuleName];
+ $runtime = self::$runtimes[$targetModuleName];
+ self::$registeredModules[$as] = $targetModule;
+ self::$registeredRuntimes[$as] = $runtime;
+ $this->assertTrue(true);
}
/**