Sanguis in Web Pages
Write Sanguis inside a web page, and run it there
How it works
The kit has a folder called web with three files in it. sanguis-floor.wasm is the Sanguis floor, the same C program your computer runs, built for the browser. compiler.bc is the compiler. sanguis.js connects the two to your page.
Because it's the same code, a page gets the same answers your computer does, byte for byte. The compiler can even rebuild itself in the browser and come out identical.
A script tag
Put the three files next to your page, then write:
<script src="sanguis.js"></script>
<script type="text/sanguis">
cell main() -> float {
print("Hello from Sanguis.");
output 0.0;
}
</script>When the page loads, your program is compiled and run in the background, so the page never freezes. What it prints shows up right below it. To send it somewhere else, add data-out="#id". To start from a different cell, add data-entry. To keep your program in its own file, use src="app.sanguis".
Want it to load instantly? Compile it on your computer first with sanguis bleed, then point the page at the result: <script type="text/sanguis" data-bc="app.bc"></script>.
From JavaScript
const s = Sanguis.background();
const bc = await s.compile(source); // BloodCode, as bytes
const r = await s.run(bc, 'main'); // { code, out, err }
const both = await s.exec(source, 'main'); // compile, then runA program only sees what your page gives it: its files, its arguments and its output. It has no clock and no network.
Content-Security-Policy
If your page has a strict security policy, you don't need to change it. Script tags and Sanguis.background() do their work in a background worker. Our demo page runs with script-src 'self' and nothing else.
The one exception is calling Sanguis.load() on the page itself. That needs 'wasm-unsafe-eval' in script-src. It lets WebAssembly run, and JavaScript eval stays blocked.
Try it
The live demo runs right here on our site. To run the kit's own demo, serve its web folder, for example with python -m http.server, and open demo/index.html. Browsers won't run WebAssembly straight from a file on your disk.