First message routed
This commit is contained in:
parent
34aaaa4ccb
commit
9497747148
@ -5,4 +5,4 @@ class BaseRouter:
|
||||
|
||||
class StdOutRouter(BaseRouter):
|
||||
def post_message(self, namespace: bytes, topic: bytes, kind: bytes, body: bytes) -> None:
|
||||
print(f'{namespace.decode()}: {topic.decode()}: {kind.decode()}: {body.decode()}')
|
||||
print(f'ns={namespace.decode()},t={topic.decode()},k={kind.decode()} {body.decode()}')
|
||||
|
||||
@ -17,7 +17,7 @@ def main() -> int:
|
||||
|
||||
foo: BaseRunner
|
||||
|
||||
with open('/home/johan/projects/idea/phasm/examples/crc32.wasm', 'rb') as fil:
|
||||
with open('/home/johan/projects/idea/phasm/examples/platform.wasm', 'rb') as fil:
|
||||
foo = WasmTimeRunner(stdout_router, fil.read())
|
||||
|
||||
namespace = b'test-namespace'
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
from typing import TextIO
|
||||
|
||||
from phasmplatform.common.router import BaseRouter
|
||||
|
||||
|
||||
@ -31,3 +33,25 @@ class BaseRunner:
|
||||
body = self.read_bytes(body_ptr)
|
||||
|
||||
self.router.post_message(namespace, topic, kind, body)
|
||||
|
||||
|
||||
def dump_memory(textio: TextIO, mem: bytes) -> None:
|
||||
line_width = 16
|
||||
|
||||
prev_line = None
|
||||
skip = False
|
||||
for idx in range(0, len(mem), line_width):
|
||||
line = ''
|
||||
for idx2 in range(0, line_width):
|
||||
line += f'{mem[idx + idx2]:02X}'
|
||||
if idx2 % 2 == 1:
|
||||
line += ' '
|
||||
|
||||
if prev_line == line:
|
||||
if not skip:
|
||||
textio.write('**\n')
|
||||
skip = True
|
||||
else:
|
||||
textio.write(f'{idx:08x} {line}\n')
|
||||
|
||||
prev_line = line
|
||||
|
||||
@ -30,18 +30,19 @@ class WasmTimeRunner(BaseRunner):
|
||||
self.exports = self.instance.exports(self.store)
|
||||
|
||||
def alloc_bytes(self, data: bytes) -> int:
|
||||
alloc_bytes = self.exports['stdlib.types.__alloc_bytes__']
|
||||
assert isinstance(alloc_bytes, wasmtime.Func)
|
||||
ptr = alloc_bytes(self.store, len(data))
|
||||
assert isinstance(ptr, int) # type hint
|
||||
|
||||
memory = self.exports['memory']
|
||||
assert isinstance(memory, wasmtime.Memory) # type hint
|
||||
|
||||
data_ptr = memory.data_ptr(self.store)
|
||||
data_len = memory.data_len(self.store)
|
||||
|
||||
idx = ptr + 8 # This is the header from alloc plus the header from __alloc_bytes__
|
||||
alloc_bytes = self.exports['stdlib.types.__alloc_bytes__']
|
||||
assert isinstance(alloc_bytes, wasmtime.Func)
|
||||
|
||||
ptr = alloc_bytes(self.store, len(data))
|
||||
assert isinstance(ptr, int) # type hint
|
||||
|
||||
idx = ptr + 4 # Skip the header from header from __alloc_bytes__
|
||||
for byt in data:
|
||||
assert idx < data_len
|
||||
data_ptr[idx] = ctypes.c_ubyte(byt)
|
||||
@ -58,13 +59,9 @@ class WasmTimeRunner(BaseRunner):
|
||||
|
||||
raw = ctypes.string_at(data_ptr, data_len)
|
||||
|
||||
ptr = ptr + 4 # This is the header from alloc
|
||||
length, = struct.unpack('<I', raw[ptr:ptr + 4]) # Header prefixed by __alloc_bytes__
|
||||
|
||||
print('raw[ptr:ptr + 4]', raw[ptr:ptr + 4])
|
||||
length = struct.unpack('<I', raw[ptr:ptr + 4])
|
||||
print('length', length)
|
||||
|
||||
return b'?'
|
||||
return raw[ptr + 4:ptr + 4 + length]
|
||||
|
||||
def handle_message(self, namespace: bytes, topic: bytes, kind: bytes, body: bytes) -> None:
|
||||
namespace_ptr = self.alloc_bytes(namespace)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user