aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/shed/namespaces
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-02-11 21:29:15 +0000
committerMichael Williamson <mike@zwobble.org>2024-02-11 21:29:15 +0000
commita01ee103f9bc0c6defe9ce8cd85651f19f7127bd (patch)
tree8634fd33dde74b4aab1519e1b61bc3343d99606f /runtime/src/shed/namespaces
parent3d4e3f4a54d128a2a280b5adc921f3d95fb963bb (diff)
Create separate lib for patio runtime
Diffstat (limited to 'runtime/src/shed/namespaces')
-rw-r--r--runtime/src/shed/namespaces/Shed/Builtins.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/runtime/src/shed/namespaces/Shed/Builtins.c b/runtime/src/shed/namespaces/Shed/Builtins.c
new file mode 100644
index 00000000..142a1317
--- /dev/null
+++ b/runtime/src/shed/namespaces/Shed/Builtins.c
@@ -0,0 +1,59 @@
+#include "../../std/unistd.h"
+
+#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 {
+ struct ShedGcObjectHeader gc_object_header;
+ 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;
+}