do_call ideas

This commit is contained in:
Johan B.W. de Vries 2023-04-10 16:49:22 +02:00
parent 90531a5b99
commit 2485ccba40
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,26 @@
from typing import Any, Generic, TypeVar
T = TypeVar('T')
class BaseValue(Generic[T]):
__slots__ = ('data', )
data: T
def __init__(self, data: T) -> None:
self.data = data
def __eq__(self, other: Any) -> bool:
return self.__class__ is other.__class__ and self.data == other.data
def __repr__(self) -> str:
return f'{self.__class__.__name__}({repr(self.data)})'
class UntypedValue(BaseValue[Any]):
pass
class BytesValue(BaseValue[bytes]):
pass

View File

@ -1,7 +1,10 @@
from typing import Any
import sys import sys
from phasmplatform.common.config import from_toml from phasmplatform.common.config import from_toml
from phasmplatform.common.router import StdOutRouter from phasmplatform.common.router import StdOutRouter
from phasmplatform.common.value import BaseValue, BytesValue
from .runners.base import BaseRunner from .runners.base import BaseRunner
from .runners.wasmtime import WasmTimeRunner from .runners.wasmtime import WasmTimeRunner
@ -27,6 +30,15 @@ def main() -> int:
foo.handle_message(namespace, topic, kind, body) foo.handle_message(namespace, topic, kind, body)
inp = BytesValue(b'Hello, world!')
print('inp', inp)
def on_respond(out: BaseValue[Any]) -> None:
print('out', out)
assert out == inp
foo.do_call('echo', [inp], on_respond)
return 0 return 0

View File

@ -1,9 +1,12 @@
from typing import Any, Callable, List, Union
import ctypes import ctypes
import struct import struct
import wasmtime import wasmtime
from phasmplatform.common.router import BaseRouter from phasmplatform.common.router import BaseRouter
from phasmplatform.common.value import BaseValue, BytesValue, UntypedValue
from .base import BaseRunner from .base import BaseRunner
@ -63,6 +66,21 @@ class WasmTimeRunner(BaseRunner):
return raw[ptr + 4:ptr + 4 + length] return raw[ptr + 4:ptr + 4 + length]
def convert_value(self, val: BaseValue[Any]) -> Union[int, float]:
if isinstance(val, BytesValue):
return self.alloc_bytes(val.data)
raise NotImplementedError(val)
def do_call(self, method_name: str, args: List[BaseValue[Any]], callback: Callable[[BaseValue[Any]], None]) -> None:
method = self.exports[method_name]
assert isinstance(method, wasmtime.Func)
act_args = [self.convert_value(x) for x in args]
result = method(self.store, *act_args)
callback(UntypedValue(result)) # TODO: This returns a bytes pointer, but we can't detect that in advance
def handle_message(self, namespace: bytes, topic: bytes, kind: bytes, body: bytes) -> None: def handle_message(self, namespace: bytes, topic: bytes, kind: bytes, body: bytes) -> None:
namespace_ptr = self.alloc_bytes(namespace) namespace_ptr = self.alloc_bytes(namespace)
topic_ptr = self.alloc_bytes(topic) topic_ptr = self.alloc_bytes(topic)