diff --git a/examples/echoclient.py b/examples/echoclient.py index 47ff799..d3bab95 100644 --- a/examples/echoclient.py +++ b/examples/echoclient.py @@ -6,7 +6,14 @@ def echo(msg: bytes) -> bytes: def log_bytes(data: bytes) -> None: pass +@imported('prelude') +def sleep(seconds: u32) -> None: + pass + @exported def on_module_loaded() -> None: log_bytes(b'Echo client starting up, calling server') + sleep(8) log_bytes(echo(b'Hello, world!')) + sleep(8) + log_bytes(echo(b'Bye, world!')) diff --git a/examples/echoclient.toml b/examples/echoclient.toml index fee6ef0..f3c66d0 100644 --- a/examples/echoclient.toml +++ b/examples/echoclient.toml @@ -6,6 +6,7 @@ path = "examples/echoclient.wasm" hash = "sha256@84cb22d12dfdd6b05cb906f6db83d59f473c9df85a33822f696344af2b92b502" imports = [ + { service = "prelude", method = "sleep", arg_types = ["u32"], return_type = "none"}, { service = "echoserver", method = "echo", arg_types = ["bytes"], return_type = "bytes"}, ] diff --git a/phasmplatform/common/value.py b/phasmplatform/common/value.py index 28c7547..d953057 100644 --- a/phasmplatform/common/value.py +++ b/phasmplatform/common/value.py @@ -2,7 +2,7 @@ from typing import Any, Union from .valuetype import ValueType, none -ValueData = Union[None, bytes] +ValueData = Union[None, int, float, bytes] class Value: diff --git a/phasmplatform/common/valuetype.py b/phasmplatform/common/valuetype.py index 2726fa1..c7153fb 100644 --- a/phasmplatform/common/valuetype.py +++ b/phasmplatform/common/valuetype.py @@ -19,11 +19,14 @@ class ValueType: return f'valuetype.{self.name}' +u32 = ValueType('u32') + bytes = ValueType('bytes') none = ValueType('none') LOOKUP_TABLE: Dict[str, ValueType] = { + u32.name: u32, bytes.name: bytes, none.name: none, } diff --git a/phasmplatform/worker/__main__.py b/phasmplatform/worker/__main__.py index 9f9fecf..334a675 100644 --- a/phasmplatform/worker/__main__.py +++ b/phasmplatform/worker/__main__.py @@ -201,9 +201,9 @@ class LocalhostMethodCallRouter(MethodCallRouterInterface): def make_prelude() -> Service: - methods: List[Method] = [] - - methods.append(Method('log_bytes', [valuetype.bytes], valuetype.none)) + methods: List[Method] = [ + Method('sleep', [valuetype.u32], valuetype.none), + ] return Service('prelude', ContainerMatch('__prelude__'), methods) diff --git a/phasmplatform/worker/runners/base.py b/phasmplatform/worker/runners/base.py index 7478459..dfaa621 100644 --- a/phasmplatform/worker/runners/base.py +++ b/phasmplatform/worker/runners/base.py @@ -63,6 +63,10 @@ class BaseRunner(RunnerInterface): assert val is None # type hint return Value(value_type, None) + if value_type is valuetype.u32: + assert isinstance(val, int) # type hint + return Value(value_type, val) + if value_type is valuetype.bytes: assert isinstance(val, int) # type hint return Value(value_type, self.read_bytes(val)) diff --git a/phasmplatform/worker/runners/prelude.py b/phasmplatform/worker/runners/prelude.py index eedf266..55b1bc0 100644 --- a/phasmplatform/worker/runners/prelude.py +++ b/phasmplatform/worker/runners/prelude.py @@ -1,5 +1,7 @@ from typing import Union +import time + from phasmplatform.common.methodcall import MethodCall, MethodCallError from phasmplatform.common.value import Value, NoneValue @@ -14,4 +16,12 @@ class PreludeRunner(BaseRunner): self.container_log[0]('PreludeRunner loaded') return NoneValue + if call.method.name == 'sleep': + # This will block this thread + # Which is has to do until we can get the async really working + seconds = call.args[0].data + assert isinstance(seconds, int) # type hint + time.sleep(seconds) + return NoneValue + raise NotImplementedError(call) diff --git a/phasmplatform/worker/runners/wasmtime.py b/phasmplatform/worker/runners/wasmtime.py index 96b2f71..deb9d88 100644 --- a/phasmplatform/worker/runners/wasmtime.py +++ b/phasmplatform/worker/runners/wasmtime.py @@ -152,6 +152,9 @@ def build_func_type(method: Method) -> wasmtime.FuncType: def build_wasm_type(value_type: ValueType) -> wasmtime.ValType: + if value_type is valuetype.u32: + return wasmtime.ValType.i32() # Signed-ness is in the operands + if value_type is valuetype.bytes: return wasmtime.ValType.i32() # Bytes are passed as pointer