summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-11-01 05:04:25 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-11-01 05:04:25 +0100
commit63c3b9bda3fe7eb8750f084ff070f40b967f9532 (patch)
tree8b027ed24c91ea4de2479108d5886c6675c0504c
parenteb0c1592d49de956c8c4d83a831fab3cf0372acb (diff)
downloadreduced_system_layer-63c3b9bda3fe7eb8750f084ff070f40b967f9532.zip
Build wasm without Emscripten
-rw-r--r--Dockerfile5
-rw-r--r--index.htm22
-rwxr-xr-xreduced_system_layer.c66
3 files changed, 54 insertions, 39 deletions
diff --git a/Dockerfile b/Dockerfile
index 4b0f30f..1967156 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,6 +1,7 @@
-FROM emscripten/emsdk as build
+FROM alpine as build
+RUN apk add clang lld
COPY reduced_system_layer.c /usr/reduced_system_layer.c
-RUN emcc -D REDUCED_SYSTEM_LAYER_EXAMPLE -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s EXPORTED_FUNCTIONS=_js_main,_js_init,_js_pixels,_js_frame -o /usr/index.wasm /usr/reduced_system_layer.c
+RUN clang -D REDUCED_SYSTEM_LAYER_EXAMPLE --target=wasm32 -nostdlib -Wl,--no-entry,--allow-undefined -o /usr/index.wasm /usr/reduced_system_layer.c
FROM nginx:alpine
EXPOSE 80
diff --git a/index.htm b/index.htm
index 35fd8ca..0ffcf89 100644
--- a/index.htm
+++ b/index.htm
@@ -24,13 +24,13 @@
fetch("index.wasm"),
{
"wasi_snapshot_preview1" : {
- args_sizes_get : (...args) => { console.log(`args_sizes_get: ${args}`); },
- args_get : (...args) => { console.log(`args_get: ${args}`); },
- proc_exit : (...args) => { console.log(`proc_exit: ${args}`); },
clock_time_get : () => { return Date.now(); },
- fd_close : (...args) => { console.log(`fd_close: ${args}`); },
- fd_write : (...args) => { console.log(`fd_write: ${args}`); },
- fd_seek : (...args) => { console.log(`fd_seek: ${args}`); },
+ args_sizes_get : () => { throw new Error("Unexpected args_sizes_get call"); },
+ args_get : () => { throw new Error("Unexpected args_get call"); },
+ proc_exit : () => { throw new Error("Unexpected proc_exit call"); },
+ fd_close : () => { throw new Error("Unexpected fd_close call"); },
+ fd_write : () => { throw new Error("Unexpected fd_write call"); },
+ fd_seek : () => { throw new Error("Unexpected fd_seek call"); },
},
"env" : {
p_init : () => {
@@ -60,6 +60,16 @@
p_wait_events : () => {},
p_sleep_for : (time) => {},
p_time : Date.now,
+ memset : (dst, val, num) => {
+ // FIXME PERF
+ for (let i = 0; i < num; ++i)
+ dst[i] = val;
+ },
+ memcpy : (dst, src, num) => {
+ // FIXME PERF
+ for (let i = 0; i < num; ++i)
+ dst[i] = src[i];
+ },
},
}
);
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index 19c8a0c..5ddd3ab 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -102,17 +102,6 @@ typedef double f64;
#ifndef REDUCED_SYSTEM_LAYER_HEADER_GUARD_
#define REDUCED_SYSTEM_LAYER_HEADER_GUARD_
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE
-#endif
-
-#include <time.h>
-#include <math.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-
// ================================================================
//
// PLATFORM API
@@ -232,6 +221,8 @@ void p_render_frame(void);
// User-defined proc
void p_frame(void);
+void p_event_loop(void);
+
// Clipboard
void p_clipboard_write(i64 size, c8 *data);
@@ -245,6 +236,10 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port);
extern Platform platform;
+#ifdef __wasm__
+i32 main(i32 argc, c8 **argv);
+#endif
+
#ifdef __cplusplus
}
#endif
@@ -292,13 +287,8 @@ i32 main(i32 argc, c8 **argv) {
.frame_height = 720,
};
- p_init();
-
- while (!platform.done) {
- p_frame();
- }
+ p_event_loop();
- p_cleanup();
return 0;
}
@@ -316,6 +306,12 @@ i32 main(i32 argc, c8 **argv) {
Platform platform = {0};
+void p_event_loop(void) {
+ p_init();
+ while (!platform.done) p_frame();
+ p_cleanup();
+}
+
// ================================================================
//
// Utilities
@@ -394,8 +390,13 @@ i32 utf8_write(c32 c, c8 *buffer) {
//
// ================================================================
-#ifdef __unix__
+#if defined(__unix__)
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+#include <time.h>
#include <sched.h>
#include <unistd.h>
@@ -424,7 +425,7 @@ void p_sleep_for(i64 duration) {
usleep((duration % 1000) * 1000);
}
-#endif
+#endif // defined(__unix__)
// ================================================================
//
@@ -432,8 +433,11 @@ void p_sleep_for(i64 duration) {
//
// ================================================================
-#ifdef __unix__
+#if defined(__unix__)
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <netinet/in.h>
@@ -651,7 +655,7 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) {
return sent;
}
-#endif
+#endif // defined(__unix__)
// ================================================================
//
@@ -659,7 +663,7 @@ i64 p_send(u16 slot, IP_Address address, i64 size, u8 *data, u16 *local_port) {
//
// ================================================================
-#ifdef __linux__
+#if defined(__linux__)
#include <X11/Xlib.h>
#include <X11/Xutil.h>
@@ -1169,7 +1173,7 @@ void p_clipboard_write(i64 size, c8 *data) {
memcpy(platform.clipboard, data, platform.clipboard_size);
}
-#endif
+#endif // defined(__linux__)
// ================================================================
//
@@ -1177,7 +1181,7 @@ void p_clipboard_write(i64 size, c8 *data) {
//
// ================================================================
-#ifdef __linux__
+#if defined(__linux__)
void p_handle_audio(i64 time_elapsed) {
(void) time_elapsed;
@@ -1191,7 +1195,7 @@ void p_queue_sound(i64 delay, i64 num_samples, f32 *samples) {
assert(0);
}
-#endif
+#endif // defined(__linux__)
// ================================================================
//
@@ -1199,7 +1203,7 @@ void p_queue_sound(i64 delay, i64 num_samples, f32 *samples) {
//
// ================================================================
-#ifdef __EMSCRIPTEN__
+#ifdef __wasm__
static u32 _buffer[MAX_NUM_PIXELS] = {0};
@@ -1214,27 +1218,27 @@ void p_render_frame(void) {
p_render_frame_impl();
}
-void js_main(c8 *href) {
+__attribute__((export_name("js_main"))) void js_main(c8 *href) {
main(1, &href);
}
-void js_init(void) {
+__attribute__((export_name("js_init"))) void js_init(void) {
platform.pixels = _buffer;
platform.done = 1;
}
-void *js_pixels(void) {
+__attribute__((export_name("js_pixels"))) void *js_pixels(void) {
return platform.pixels;
}
-void js_frame(i32 frame_width, i32 frame_height) {
+__attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 frame_height) {
platform.frame_width = frame_width;
platform.frame_height = frame_height;
p_frame();
}
-#endif // __EMSCRIPTEN__
+#endif // __wasm__
// ================================================================