summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2024-11-02 00:00:35 +0100
committerMitya Selivanov <automainint@guattari.tech>2024-11-02 00:00:35 +0100
commit48c5d9b83afd1cd56e73449aebaf461cb48360d9 (patch)
tree41127704d20b1ee53fa1034e53339c22ca4c820d
parent2afdeaedbfb488f20cdc1cca7ce01a915df4d53a (diff)
downloadreduced_system_layer-48c5d9b83afd1cd56e73449aebaf461cb48360d9.zip
wasm: Fix memcpy, memset
-rw-r--r--index.htm16
-rwxr-xr-xreduced_system_layer.c27
2 files changed, 27 insertions, 16 deletions
diff --git a/index.htm b/index.htm
index e3e6872..3b1fd3b 100644
--- a/index.htm
+++ b/index.htm
@@ -9,13 +9,11 @@
<body style="margin: 0; height: 100%; overflow: hidden; overflow-x: hidden;">
<canvas style="margin: 0; width: 100%; height: 100%;" id="frame" oncontextmenu="event.preventDefault()"></canvas>
<script type="text/javascript">
- function string_from_memory(memory, offset) {
+ function string_from_memory(bytes, offset) {
let len = 0;
- console.log(memory);
- while (memory[offset + len] != 0) {
+ while (bytes[offset + len] != 0)
++len;
- }
- return new TextDecoder().decode(memory.subarray(offset, offset + len));
+ return new TextDecoder("utf8").decode(bytes.subarray(offset, offset + len));
}
async function run(attrs) {
@@ -78,18 +76,16 @@
floor : Math.floor,
ceil : Math.ceil,
memset : (dst, val, num) => {
- for (let i = 0; i < num; ++i)
- dst[i] = val;
+ program.instance.exports.js_memset(dst, val, num);
},
memcpy : (dst, src, num) => {
- for (let i = 0; i < num; ++i)
- dst[i] = src[i];
+ program.instance.exports.js_memcpy(dst, src, num);
},
},
}
);
- program.instance.exports.js_main();
+ program.instance.exports.js_main(document.location.href);
animation_frame = (time) => {
if (attrs.fit_window) {
diff --git a/reduced_system_layer.c b/reduced_system_layer.c
index 7cbfd93..b7b0d5a 100755
--- a/reduced_system_layer.c
+++ b/reduced_system_layer.c
@@ -226,10 +226,11 @@ i32 p_handle_events(void);
i32 p_wait_events(void);
void p_render_frame(void);
-// User-defined proc
+// User-defined frame updating procedure
+// NOTE This procedure is required for the WebAssembly compatibility.
void update_and_render_frame(void);
-// Convenient helper for the event loop
+// Convenient helper procedure for the event loop
void p_event_loop(void);
// Clipboard
@@ -267,14 +268,12 @@ f64 ceil(f64 x);
// ================================================================
//
-// WRITE YOUR CODE HERE
+// EXAMPLE CODE
//
// ================================================================
#ifdef REDUCED_SYSTEM_LAYER_EXAMPLE
-// NOTE
-// This procedure is required for the WebAssembly compatibility.
void update_and_render_frame(void) {
p_handle_events();
@@ -325,8 +324,10 @@ Platform platform = {0};
void p_event_loop(void) {
p_init();
+#if !defined(__wasm__)
while (!platform.done) update_and_render_frame();
p_cleanup();
+#endif
}
// ================================================================
@@ -1250,6 +1251,18 @@ void p_render_frame(void) {
p_render_frame_impl();
}
+__attribute__((export_name("js_memset"))) void js_memset(void *dst, i32 val, u32 num) {
+ if (dst == NULL) return;
+ for (u32 i = 0; i < num; ++i)
+ ((u8 *) dst)[i] = (u8) val;
+}
+
+__attribute__((export_name("js_memcpy"))) void js_memcpy(void *dst, void const *src, u32 num) {
+ if (dst == NULL || src == NULL) return;
+ for (u32 i = 0; i < num; ++i)
+ ((u8 *) dst)[i] = ((u8 const *) src)[i];
+}
+
__attribute__((export_name("js_main"))) void js_main(c8 *href) {
main(1, &href);
}
@@ -1259,7 +1272,7 @@ __attribute__((export_name("js_init"))) void js_init(void) {
platform.done = 1;
}
-__attribute__((export_name("js_title"))) c8 *js_title(void) {
+__attribute__((export_name("js_title"))) void *js_title(void) {
return platform.title;
}
@@ -1274,6 +1287,8 @@ __attribute__((export_name("js_frame"))) void js_frame(i32 frame_width, i32 fram
platform.frame_height = frame_height;
}
+ platform.done = 0;
+
update_and_render_frame();
}