From: Elijah Cohen Date: Thu, 14 Nov 2024 03:05:15 +0000 (-0600) Subject: added easier way to compile webasm version X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=bc0c740cc1f6d470b4a182bea3d65a65d9011e75;p=klapaucius added easier way to compile webasm version also rediscovered there's a major bug in strings where I'm not terminating them properly, appears on webasm version --- diff --git a/.gitignore b/.gitignore index b8678e1..28518dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ *~ /src/linenoise.c /src/linenoise.h +/build/ +/web/static/ +/web/build/ \ No newline at end of file diff --git a/Makefile b/Makefile index db9b141..aec8428 100644 --- a/Makefile +++ b/Makefile @@ -9,3 +9,6 @@ test: .PHONY: clean clean: cd src; $(MAKE) clean +.PHONY: web +web: + cd src; $(MAKE) web diff --git a/src/Makefile b/src/Makefile index ff4cd89..4246aba 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 index 0000000..f312ade --- /dev/null +++ b/src/emscr.c @@ -0,0 +1,34 @@ +#ifdef __EMSCRIPTEN__ + +#include +#include +#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 diff --git a/src/repl.c b/src/repl.c index 9bc64d8..d1c7940 100644 --- a/src/repl.c +++ b/src/repl.c @@ -1,4 +1,5 @@ +#ifndef __EMSCRIPTEN__ #include #include @@ -77,3 +78,5 @@ int main(int argc, char** argv) { free(histfile); return 0; } + +#endif diff --git a/src/test.c b/src/test.c index 672bc8e..db89c82 100644 --- a/src/test.c +++ b/src/test.c @@ -1,3 +1,4 @@ +#ifndef __EMSCRIPTEN__ #include #include @@ -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 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 index 0000000..3feab3a --- /dev/null +++ b/web/manifest.json @@ -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 index 0000000..3226da4 --- /dev/null +++ b/web/shell.html @@ -0,0 +1,56 @@ + + + + + + + + + klrepl + + + +
+
+
+ +
+
+
+ + {{{ SCRIPT }}} + +