aboutsummaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-06-28 20:30:54 +0100
committerMichael Williamson <mike@zwobble.org>2023-06-28 20:30:54 +0100
commit970da5376491712089e46b4ff4d27291a02bfa18 (patch)
treec9a494c7ffa412808d3253e83e3b96b834093f73 /runtime/src
parent42c1a57cd441d1cefd0aea277758b5be83ecf97e (diff)
Get hello-world example working on WASM
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/shed.c27
-rw-r--r--runtime/src/shed.h8
2 files changed, 34 insertions, 1 deletions
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 <stdint.h>
+#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[];
};