aboutsummaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-19 15:27:16 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-19 15:27:16 +0000
commitc690c58c4e4c88f5b1a0d29a49a5f941f051b372 (patch)
tree2ee314c3deaa4d9644b84cb03b8b09b3e1467f00 /runtime/src
parentd95eb004715ce2dc033cb2fef276b596e90fe1a8 (diff)
Split out runtime implementation of Shed.Builtins namespace
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/namespaces/Shed/Builtins.c56
-rw-r--r--runtime/src/shed.c51
-rw-r--r--runtime/src/shed.h14
3 files changed, 64 insertions, 57 deletions
diff --git a/runtime/src/namespaces/Shed/Builtins.c b/runtime/src/namespaces/Shed/Builtins.c
new file mode 100644
index 00000000..10953d62
--- /dev/null
+++ b/runtime/src/namespaces/Shed/Builtins.c
@@ -0,0 +1,56 @@
+#include "../../shed.h"
+
+#ifdef __WASM__
+
+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;
+}
+
+#else
+
+ShedObject shed__runtime__print(ShedClosurePointer closurePointer, ShedString value) {
+ size_t total_bytes_written = 0;
+ while (total_bytes_written < value->length) {
+ ssize_t bytes_written = write(STDOUT_FILENO, &value->data[total_bytes_written], value->length - total_bytes_written);
+ if (bytes_written == -1) {
+ break;
+ }
+ total_bytes_written += bytes_written;
+ }
+ return shed_unit;
+}
+
+#endif
+
+ShedClosureStruct shed__runtime__print_closure = {
+ .function = shed__runtime__print,
+ .freeVariables = {},
+};
+
+struct shed__namespace__Shed_Builtins {
+ ShedObject print;
+};
+
+struct shed__namespace__Shed_Builtins shed__namespace__Shed_Builtins = {
+ .print = (ShedObject) &shed__runtime__print_closure
+};
+
+ShedObject shed__namespaceImport__Shed_Builtins() {
+ return (ShedObject) &shed__namespace__Shed_Builtins;
+}
diff --git a/runtime/src/shed.c b/runtime/src/shed.c
index f16e937f..05d10c7d 100644
--- a/runtime/src/shed.c
+++ b/runtime/src/shed.c
@@ -1,12 +1,7 @@
-#include <stdbool.h>
#include <stdint.h>
-#ifdef __WASM__
-typedef uint32_t size_t;
-typedef int32_t ssize_t;
-#else
+#ifndef __WASM__
#include <stdlib.h>
-#include <unistd.h>
#endif
#include "./shed.h"
@@ -25,58 +20,14 @@ void* shed__runtime__alloc(uint32_t size) {
return (void*) (previous_page_count * 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;
-}
-
#else
void* shed__runtime__alloc(uint64_t size) {
return malloc(size);
}
-ShedObject shed__runtime__print(ShedClosurePointer closurePointer, ShedString value) {
- size_t total_bytes_written = 0;
- while (total_bytes_written < value->length) {
- ssize_t bytes_written = write(STDOUT_FILENO, &value->data[total_bytes_written], value->length - total_bytes_written);
- if (bytes_written == -1) {
- break;
- }
- total_bytes_written += bytes_written;
- }
- return shed_unit;
-}
-
#endif
-struct shed__namespace__Shed_Builtins {
- ShedObject print;
-};
-
-struct shed__namespace__Shed_Builtins shed__namespace__Shed_Builtins = {
- .print = (ShedObject) &shed__runtime__print_closure
-};
-
-ShedObject shed__namespaceImport__Shed_Builtins() {
- return (ShedObject) &shed__namespace__Shed_Builtins;
-}
-
ShedString shed__runtime__string_alloc(ShedStringLength length) {
return shed__runtime__alloc(sizeof(ShedStringLength) + length);
}
diff --git a/runtime/src/shed.h b/runtime/src/shed.h
index 2d2ba64c..913bb843 100644
--- a/runtime/src/shed.h
+++ b/runtime/src/shed.h
@@ -4,6 +4,13 @@
#include <stdint.h>
#ifdef __WASM__
+typedef uint32_t size_t;
+typedef int32_t ssize_t;
+#else
+#include <unistd.h>
+#endif
+
+#ifdef __WASM__
typedef uint32_t ShedObject;
typedef uint32_t ShedStringLength;
#else
@@ -31,11 +38,4 @@ typedef struct ShedClosure* ShedClosurePointer;
typedef ShedObject* ShedEnv;
-ShedObject shed__runtime__print(ShedClosurePointer closurePointer, ShedString value);
-
-ShedClosureStruct shed__runtime__print_closure = {
- .function = shed__runtime__print,
- .freeVariables = {},
-};
-
#endif