aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/shed.c
blob: 7dd5ea14e2f39e2c1a1eaf39fe643f61e634ccd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdbool.h>
#include <stdint.h>

#ifdef __WASM__
typedef uint32_t size_t;
typedef int32_t ssize_t;
#else
#include <stdlib.h>
#include <unistd.h>
#endif

#include "./shed.h"

ShedObject const shed_unit = 0;

#ifdef __WASM__

uint32_t const PAGE_SIZE = 65536;

uint32_t wasm_memory_grow(uint32_t pages);

void* shed__runtime__alloc(uint32_t size) {
    // TODO: implement a more sensible scheme. This allocates a new page on every request.
    uint32_t previous_page_count = wasm_memory_grow((size + PAGE_SIZE - 1) / PAGE_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;
}

// 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) {
    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);
}

ShedString shed__runtime__string_add(ShedString left, ShedString right) {
    ShedStringLength length = left->length + right->length;
    ShedString string = shed__runtime__string_alloc(length);
    string->length = length;

    for(ShedStringLength i = 0; i < left->length; i++) {
        string->data[i] = left->data[i];
    }

    for(ShedStringLength i = 0; i < right->length; i++) {
        string->data[left->length + i] = right->data[i];
    }

    return string;
}