From 970da5376491712089e46b4ff4d27291a02bfa18 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 28 Jun 2023 20:30:54 +0100 Subject: Get hello-world example working on WASM --- runtime/src/shed.c | 27 +++++++++++++++++++++++++++ runtime/src/shed.h | 8 +++++++- 2 files changed, 34 insertions(+), 1 deletion(-) (limited to 'runtime/src') diff --git a/runtime/src/shed.c b/runtime/src/shed.c index cf5af0bd..2b69eeae 100644 --- a/runtime/src/shed.c +++ b/runtime/src/shed.c @@ -24,6 +24,33 @@ void* shed__runtime__alloc(uint32_t size) { return wasm_memory_grow((size + PAGE_SIZE - 1) / PAGE_SIZE); } +typedef struct __wasi_ciovec_t { + uint8_t * buf; + size_t buf_len; +} __wasi_ciovec_t; + +uint16_t wasi_fd_write(uint32_t fd, __wasi_ciovec_t* iovs_ptr, uint32_t iovs_len, uint32_t *nwritten) + __attribute__(( + __import_module__("wasi_snapshot_preview1"), + __import_name__("fd_write"), + )); + +ShedObject shed__runtime__print(ShedClosurePointer closurePointer, ShedString value) { + uint32_t stdout = 1; + __wasi_ciovec_t iov = {.buf = value->data, .buf_len = value->length}; + uint32_t iovs_len = 1; + uint32_t nwritten; + wasi_fd_write(stdout, &iov, iovs_len, &nwritten); + return shed_unit; +} + +// TODO: Work out a good way to access the variable directly from our generated +// WASM (or perhaps just expose this as a builtin module and use the import +// machinery)? +ShedObject shed__runtime__print_closure__get() { + return (ShedObject) &shed__runtime__print_closure; +} + #else void* shed__runtime__alloc(uint64_t size) { diff --git a/runtime/src/shed.h b/runtime/src/shed.h index 1fd8f44d..313dd3bf 100644 --- a/runtime/src/shed.h +++ b/runtime/src/shed.h @@ -3,12 +3,18 @@ #include +#ifdef __WASM__ +typedef uint32_t ShedObject; +typedef uint32_t StringLength; +#else typedef uint64_t ShedObject; +typedef uint64_t StringLength; +#endif extern ShedObject const shed_unit; struct ShedString { - uint64_t length; + StringLength length; uint8_t data[]; }; -- cgit v1.2.3