]> git.eli173.com Git - klapaucius/commitdiff
added easier way to compile webasm version
authorElijah Cohen <eli@eli173.com>
Thu, 14 Nov 2024 03:05:15 +0000 (21:05 -0600)
committerElijah Cohen <eli@eli173.com>
Thu, 14 Nov 2024 03:05:15 +0000 (21:05 -0600)
also rediscovered there's a major bug in strings where I'm not
terminating them properly, appears on webasm version

.gitignore
Makefile
src/Makefile
src/emscr.c [new file with mode: 0644]
src/repl.c
src/test.c
web/kl.png [new file with mode: 0644]
web/manifest.json [new file with mode: 0644]
web/shell.html [new file with mode: 0644]

index b8678e10ecca680766057edad085ea879ea6a3ee..28518dddfa6dfea804a25f19a9f352e157726503 100644 (file)
@@ -1,3 +1,6 @@
 *~
 /src/linenoise.c
 /src/linenoise.h
+/build/
+/web/static/
+/web/build/
\ No newline at end of file
index db9b141a4ff689854bda8089302e2ea423632b9e..aec84289722da00ba196dcdad30f8589943bb83f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,3 +9,6 @@ test:
 .PHONY: clean
 clean:
        cd src; $(MAKE) clean
+.PHONY: web
+web:
+       cd src; $(MAKE) web
index ff4cd89edb19053ac018da04886c5a0f369f5bfc..4246aba6660a6414be88348787dce828a4e9dbf6 100644 (file)
@@ -13,6 +13,9 @@ DEPS:= $(OBJS:%.o=%.d)
 BIN_OBJS:= $(filter-out $(BUILD)/test.o,$(OBJS))
 TEST_OBJS:= $(filter-out $(BUILD)/repl.o,$(OBJS))
 
+WEB_BUILD:= ../web/build
+WEB_OBJS:= $(patsubst %.c,$(WEB_BUILD)/%.o,$(SRCS))
+WEB_SHELL:= ../web/shell.html
 
 #LIBRARIES = poppler-glib MagickWand libexif taglib_c libmagic libavcodec libavformat sqlite3 uuid fuse3 libbsd-overlay
 
@@ -33,6 +36,22 @@ $(OBJS): $(BUILD)/%.o:%.c Makefile
        $(CC) -c $< -MMD -o $@ $(CFLAGS)
 
 
+
+$(WEB_OBJS): $(WEB_BUILD)/%.o:%.c Makefile
+       mkdir -p $(WEB_BUILD)/builtins
+       emcc -c $< -MMD -o $@ $(CFLAGS)
+
+.PHONY: web
+web: $(WEB_OBJS)
+       emcc $(WEB_OBJS) --shell-file $(WEB_SHELL) -o $(WEB_BUILD)/klrepl.html -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall']" --preload-file ../demos.kl@demos.kl
+       mkdir -p $(WEB_BUILD)/../static
+       cp $(WEB_BUILD)/../kl.png $(WEB_BUILD)/../static
+       cp $(WEB_BUILD)/../manifest.json $(WEB_BUILD)/../static
+       cp $(WEB_BUILD)/klrepl.data $(WEB_BUILD)/../static
+       cp $(WEB_BUILD)/klrepl.js $(WEB_BUILD)/../static
+       cp $(WEB_BUILD)/klrepl.wasm $(WEB_BUILD)/../static
+       cp $(WEB_BUILD)/klrepl.html $(WEB_BUILD)/../static/index.html
+
 .PHONY: test
 test: $(TEST_OBJS)
        $(CC) $(TEST_OBJS) $(LDFLAGS) -o $(BUILD)/$(TEST_BIN)
diff --git a/src/emscr.c b/src/emscr.c
new file mode 100644 (file)
index 0000000..f312ade
--- /dev/null
@@ -0,0 +1,34 @@
+#ifdef __EMSCRIPTEN__
+
+#include <stdio.h>
+#include <emscripten/emscripten.h>
+#include "types.h"
+#include "sexpr.h"
+#include "dict.h"
+#include "parser.h"
+#include "builtins.h"
+#include "eval.h"
+#include "util.h"
+
+Sexpr* my_env = NULL;
+
+int main() {
+       my_env = init_dict();
+       load_env(my_env);
+       load_file(my_env, "/demos.kl");
+       printf(" ready\n");
+       return 0;
+}
+
+EMSCRIPTEN_KEEPALIVE char* ems_eval(char* s) {
+       //printf("called wasm with %s\n", s);
+       Sexpr* in = parse(s);
+       if(in == NULL) return "bad input\n";
+       Sexpr* out = eval(clone(in), my_env);
+       char* outstr = sprint_sexpr(out);
+       sexpr_free(in);
+       sexpr_free(out);
+       return outstr;
+}
+
+#endif
index 9bc64d8402f94f49399562697e99413f8dcf7268..d1c79408c6a8a6de565a4c2b9c463a349e7c6b6c 100644 (file)
@@ -1,4 +1,5 @@
 
+#ifndef __EMSCRIPTEN__
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -77,3 +78,5 @@ int main(int argc, char** argv) {
        free(histfile);
        return 0;
 }
+
+#endif
index 672bc8ecbd369fd986d270ace01517754fe2dbd0..db89c8287f93230b3590ac999c823a81c8c539f9 100644 (file)
@@ -1,3 +1,4 @@
+#ifndef __EMSCRIPTEN__
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -414,3 +415,5 @@ int main() {
        run_tests();
        return 0;
 }
+
+#endif
diff --git a/web/kl.png b/web/kl.png
new file mode 100644 (file)
index 0000000..0ffbc05
Binary files /dev/null and b/web/kl.png differ
diff --git a/web/manifest.json b/web/manifest.json
new file mode 100644 (file)
index 0000000..3feab3a
--- /dev/null
@@ -0,0 +1,15 @@
+{
+  "background_color": "#8E5DCE",
+  "description": "klklkl",
+  "display": "fullscreen",
+  "icons": [
+    {
+      "src": "kl.png",
+      "sizes": "512x512",
+      "type": "image/png"
+    }
+  ],
+  "name": "kl",
+  "short_name": "kl",
+  "start_url": "."
+}
diff --git a/web/shell.html b/web/shell.html
new file mode 100644 (file)
index 0000000..3226da4
--- /dev/null
@@ -0,0 +1,56 @@
+<!doctype html>
+<html lang="en-us">
+  <head>
+    <meta charset="utf-8">
+    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+               <link rel="manifest" href="manifest.json" />
+               <link rel="icon" href="kl.png" />
+    <title>klrepl</title>
+               <style>
+                       .console {
+                                       white-space: pre-wrap;
+                       }
+               </style>
+  </head>
+  <body>
+               <div id="repl" style="font-family: monospace;">
+                       <div id="replout"></div>
+                       <form id="replform">
+                               <input id="replin" type="text" autocorrect="off" autocapitalize="off"></input>
+                       </form>
+               </div>
+    <div class="console" id="output" rows="8"></div>
+               <script type='text/javascript'>
+                       var Module = {
+                                       print: (function() {
+                                                       var element = document.getElementById('output');
+                                                       if (element) element.textContent = 'loading...';
+                                                       return (...args) => {
+                                                                       var text = args.join(' ');
+                                                                       console.log(text);
+                                                                       if (element) {
+                                                                                       element.textContent += text + "\n";
+                                                                       }
+                                                       };
+                                       })(),
+                       };
+                       document.getElementById("replform").addEventListener("submit", e => {
+                                       e.preventDefault();
+                                       let outzone = document.getElementById("replout");
+                                       let newelt = document.createElement('div');
+                                       let inelt = document.createElement('div');
+                                       let input = e.target[0].value
+                                       inelt.textContent += "<- " + input + "\n"
+                                       let outelt = document.createElement('div');
+                                       let output = Module.ccall("ems_eval", 'string', ['string'], [input])
+                                       outelt.textContent += " -> " + output + "\n"
+                                       newelt.appendChild(inelt);
+                                       newelt.appendChild(outelt);
+                                       outzone.appendChild(newelt);
+                                       document.getElementById('replin').value = ""
+                       });
+                       </script>
+               {{{ SCRIPT }}}
+       </body>
+</html>