🚧 This is an extremely hacky proof of concept, mostly to show that it can be done for anyone who knows more about C/++ to implement in a cleaner way.
🚧 This probably isn't suitable for your next big project right now, but hey, I'm not your mom.
How?
This assumes you're on a Unix-y system (though I've only tested this on Linux). You need to have make
, gcc
, and g++
installed. If you're on Windows, uhhhhhhh use WSL I guess?
You also need to have Emscripten installed, which probably doesn't come by default. Emscripten compiles C/++ and any other languages that use LLVM to WebAssembly.
We can elegantly port shoehorn the MiniScript interpreter to running in the browser with Emscripten
Clone this repo, we need to build the interpreter ourselves:
git clone https://github.com/JoeStrout/miniscript
We only care about the stuff in the MiniScript-cpp
folder inside that repo for this, so open that folder in your editor of choice.
First, we need to comment out everything related to the filesystem in ShellIntrinsics.cpp
. There's a whole lot to comment out here, so you can just replace that file with the contents of this file. This, as you can probably guess, breaks anything that has to use the filesystem.
Next, we need to change the Makefile
:
- CC=gcc
+ CC=emcc
CFLAGS=-c -Isrc -Isrc/editline -Isrc/MiniScript
LIBS=-lstdc++ -lm
miniscript: src/*.h src/*.cpp src/MiniScript/*.h src/MiniScript/*.cpp src/editline/*.h src/editline/*.c
- $(CC) -o miniscript src/*.cpp src/editline/complete.c src/editline/editline.c src/editline/sysunix.c src/MiniScript/*.cpp -Isrc -Isrc/editline -Isrc/MiniScript $(LIBS)
+ $(CC) -o index.html src/*.cpp src/editline/complete.c src/editline/editline.c src/editline/sysunix.c src/MiniScript/*.cpp -Isrc -Isrc/editline -Isrc/MiniScript $(LIBS)
- .PHONY: clean
- install:
- chmod ugo+x miniscript
- mv miniscript /usr/local/bin
- clean:
- rm -f miniscript /usr/local/bin/miniscript
The resulting file looks like this: https://gist.github.com/EmeraldSnorlax/12f56f965ab1f2c8d3a0a44cefc73e5c
Now, just run make
and wait. It should output index.html
, index.js
, and index.wasm
For our proof of concept, we can emrun index.html
to open a browser that has the MiniScript REPL running inside of it!
A little demo video: https://cdn.discordapp.com/attachments/646000800279166976/994262151801872504/Peek_2022-07-06_16-22.mp4
What now?
Well, this should also mean that it's possible to pass MiniScript scripts to the browser and have it run there. You can run MiniScript files through WebAssembly with Node by doing node index.js -c "$(cat test.ms)"
, so it should also be possible to pass files as strings to be interpreted in the browser.
With a bit more work, Soda games could be made to run in the browser too!