From 7e8f9dc9b15ca05afef4f27ddb7dcd3cb19f71d1 Mon Sep 17 00:00:00 2001 From: mathetake Date: Mon, 12 Oct 2020 09:58:28 +0900 Subject: [PATCH] export wasi_clock_time_get Signed-off-by: mathetake --- include/proxy-wasm/exports.h | 2 +- include/proxy-wasm/wasm_vm.h | 8 +++++--- src/exports.cc | 16 ++++++++++++++++ src/wasm.cc | 1 + 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/include/proxy-wasm/exports.h b/include/proxy-wasm/exports.h index a2d7df842..6482ba35e 100644 --- a/include/proxy-wasm/exports.h +++ b/include/proxy-wasm/exports.h @@ -144,7 +144,7 @@ Word wasi_unstable_environ_sizes_get(void *raw_context, Word count_ptr, Word buf Word wasi_unstable_args_get(void *raw_context, Word argc_ptr, Word argv_buf_size_ptr); Word wasi_unstable_args_sizes_get(void *raw_context, Word argc_ptr, Word argv_buf_size_ptr); void wasi_unstable_proc_exit(void *, Word); -void wasi_unstable_proc_exit(void *, Word); +Word wasi_unstable_clock_time_get(void *, Word, int64_t, Word); Word pthread_equal(void *, Word left, Word right); // Support for embedders, not exported to Wasm. diff --git a/include/proxy-wasm/wasm_vm.h b/include/proxy-wasm/wasm_vm.h index ba6aaeb7a..d4f9f8023 100644 --- a/include/proxy-wasm/wasm_vm.h +++ b/include/proxy-wasm/wasm_vm.h @@ -76,6 +76,7 @@ template using WasmCallbackWord = WasmFuncType // Extended with W = Word // Z = void, j = uint32_t, l = int64_t, m = uint64_t using WasmCallback_WWl = Word (*)(void *, Word, int64_t); +using WasmCallback_WWlW = Word (*)(void *, Word, int64_t, Word); using WasmCallback_WWlWW = Word (*)(void *, Word, int64_t, Word, Word); using WasmCallback_WWm = Word (*)(void *, Word, uint64_t); using WasmCallback_dd = double (*)(void *, double); @@ -92,9 +93,10 @@ using WasmCallback_dd = double (*)(void *, double); _f(proxy_wasm::WasmCallbackWord<10>) \ _f(proxy_wasm::WasmCallbackWord<12>) \ _f(proxy_wasm::WasmCallback_WWl) \ - _f(proxy_wasm::WasmCallback_WWlWW) \ - _f(proxy_wasm::WasmCallback_WWm) \ - _f(proxy_wasm::WasmCallback_dd) + _f(proxy_wasm::WasmCallback_WWlW) \ + _f(proxy_wasm::WasmCallback_WWlWW) \ + _f(proxy_wasm::WasmCallback_WWm) \ + _f(proxy_wasm::WasmCallback_dd) enum class Cloneable { NotCloneable, // VMs can not be cloned and should be created from scratch. diff --git a/src/exports.cc b/src/exports.cc index 72cebaeb7..6484671e6 100644 --- a/src/exports.cc +++ b/src/exports.cc @@ -798,6 +798,22 @@ Word wasi_unstable_args_sizes_get(void *raw_context, Word argc_ptr, Word argv_bu return 0; // __WASI_ESUCCESS } +// __wasi_errno_t __wasi_args_sizes_get(uint32_t *id, uint64_t precision, uint64_t* time); +Word wasi_unstable_clock_time_get(void *raw_context, Word clock_id, int64_t precision, + Word result_time_uint64_ptr) { + + if (clock_id != 0 /* realtime */) { + return 58; // __WASI_ENOTSUP + } + + auto context = WASM_CONTEXT(raw_context); + uint64_t result = context->getCurrentTimeNanoseconds(); + if (!context->wasm()->setDatatype(result_time_uint64_ptr, result)) { + return 21; // __WASI_EFAULT + } + return 0; // __WASI_ESUCCESS +} + // void __wasi_proc_exit(__wasi_exitcode_t rval); void wasi_unstable_proc_exit(void *raw_context, Word) { auto context = WASM_CONTEXT(raw_context); diff --git a/src/wasm.cc b/src/wasm.cc index 70f758057..493ef7bac 100644 --- a/src/wasm.cc +++ b/src/wasm.cc @@ -131,6 +131,7 @@ void WasmBase::registerCallbacks() { _REGISTER_WASI(environ_sizes_get); _REGISTER_WASI(args_get); _REGISTER_WASI(args_sizes_get); + _REGISTER_WASI(clock_time_get); _REGISTER_WASI(proc_exit); #undef _REGISTER_WASI