holey-bytes/depell/src/index.js

263 lines
8.1 KiB
JavaScript
Raw Normal View History

2024-10-10 01:35:17 -05:00
/// @ts-check
2024-10-08 17:17:13 -05:00
2024-10-10 01:35:17 -05:00
/** @return {never} */
function never() { throw new Error() }
2024-10-12 06:07:49 -05:00
/**@type{WebAssembly.Instance}*/ let hbcInstance;
/**@type{Promise<WebAssembly.WebAssemblyInstantiatedSource>}*/ let hbcInstaceFuture;
2024-10-12 14:25:37 -05:00
async function getHbcInstance() {
hbcInstaceFuture ??= WebAssembly.instantiateStreaming(fetch("/hbc.wasm"), {});
return hbcInstance ??= (await hbcInstaceFuture).instance;
2024-10-12 06:07:49 -05:00
}
2024-10-12 14:25:37 -05:00
const stack_pointer_offset = 1 << 20;
/** @param {WebAssembly.Instance} instance @param {Uint8Array} code @param {number} fuel
* @returns {string} */
function compileCode(instance, code, fuel) {
2024-10-12 06:07:49 -05:00
let {
INPUT, INPUT_LEN,
LOG_MESSAGES, LOG_MESSAGES_LEN,
PANIC_MESSAGE, PANIC_MESSAGE_LEN,
2024-10-12 14:25:37 -05:00
memory, compile_and_run,
2024-10-12 06:07:49 -05:00
} = instance.exports;
if (!(true
&& INPUT instanceof WebAssembly.Global
&& INPUT_LEN instanceof WebAssembly.Global
&& LOG_MESSAGES instanceof WebAssembly.Global
&& LOG_MESSAGES_LEN instanceof WebAssembly.Global
&& memory instanceof WebAssembly.Memory
&& typeof compile_and_run === "function"
)) console.log(instance.exports), never();
const dw = new DataView(memory.buffer);
dw.setUint32(INPUT_LEN.value, code.length, true);
new Uint8Array(memory.buffer, INPUT.value).set(code);
try {
compile_and_run(fuel);
return bufToString(memory, LOG_MESSAGES, LOG_MESSAGES_LEN);
} catch (e) {
if (PANIC_MESSAGE instanceof WebAssembly.Global
&& PANIC_MESSAGE_LEN instanceof WebAssembly.Global) {
2024-10-12 14:25:37 -05:00
console.error(e, bufToString(memory, PANIC_MESSAGE, PANIC_MESSAGE_LEN));
2024-10-12 06:07:49 -05:00
}
2024-10-12 14:25:37 -05:00
return bufToString(memory, LOG_MESSAGES, LOG_MESSAGES_LEN);
}
}
/** @typedef {Object} Post
* @property {string} path
* @property {string} code */
/** @param {Post[]} posts @returns {Uint8Array} */
function packPosts(posts) {
let len = 0; for (const post of posts) len += 2 + post.path.length + 2 + post.code.length;
const buf = new Uint8Array(len), view = new DataView(buf.buffer), enc = new TextEncoder();
len = 0; for (const post of posts) {
view.setUint16(len, post.path.length, true); len += 2;
buf.set(enc.encode(post.path), len); len += post.path.length;
view.setUint16(len, post.code.length, true); len += 2;
buf.set(enc.encode(post.code), len); len += post.code.length;
2024-10-12 06:07:49 -05:00
}
2024-10-12 14:25:37 -05:00
return buf;
2024-10-12 06:07:49 -05:00
}
/** @param {WebAssembly.Memory} mem
* @param {WebAssembly.Global} ptr
* @param {WebAssembly.Global} len
* @return {string} */
function bufToString(mem, ptr, len) {
2024-10-12 14:25:37 -05:00
const res = new TextDecoder()
2024-10-12 06:07:49 -05:00
.decode(new Uint8Array(mem.buffer, ptr.value,
new DataView(mem.buffer).getUint32(len.value, true)));
2024-10-12 14:25:37 -05:00
new DataView(mem.buffer).setUint32(len.value, 0, true);
return res;
2024-10-12 06:07:49 -05:00
}
/**@type{WebAssembly.Instance}*/ let fmtInstance;
/**@type{Promise<WebAssembly.WebAssemblyInstantiatedSource>}*/ let fmtInstaceFuture;
2024-10-10 01:35:17 -05:00
/** @param {string} code @param {"fmt" | "minify"} action
2024-10-12 14:25:37 -05:00
* @returns {Promise<string | undefined>} */
async function modifyCode(code, action) {
fmtInstaceFuture ??= WebAssembly.instantiateStreaming(fetch("/hbfmt.wasm"), {});
fmtInstance ??= (await fmtInstaceFuture).instance;
2024-10-08 17:17:13 -05:00
2024-10-10 01:35:17 -05:00
let {
INPUT, INPUT_LEN,
OUTPUT, OUTPUT_LEN,
PANIC_MESSAGE, PANIC_MESSAGE_LEN,
memory, fmt, minify
2024-10-12 14:25:37 -05:00
} = fmtInstance.exports;
2024-10-08 17:17:13 -05:00
2024-10-10 01:35:17 -05:00
if (!(true
&& INPUT instanceof WebAssembly.Global
&& INPUT_LEN instanceof WebAssembly.Global
&& OUTPUT instanceof WebAssembly.Global
&& OUTPUT_LEN instanceof WebAssembly.Global
&& memory instanceof WebAssembly.Memory
&& typeof fmt === "function"
&& typeof minify === "function"
)) never();
if (action !== "fmt") {
INPUT = OUTPUT;
INPUT_LEN = OUTPUT_LEN;
}
let dw = new DataView(memory.buffer);
dw.setUint32(INPUT_LEN.value, code.length, true);
new Uint8Array(memory.buffer, INPUT.value)
.set(new TextEncoder().encode(code));
try {
if (action === "fmt") fmt(); else minify();
let result = new TextDecoder()
.decode(new Uint8Array(memory.buffer, OUTPUT.value,
dw.getUint32(OUTPUT_LEN.value, true)));
return result;
} catch (e) {
if (PANIC_MESSAGE instanceof WebAssembly.Global
&& PANIC_MESSAGE_LEN instanceof WebAssembly.Global) {
let message = new TextDecoder()
.decode(new Uint8Array(memory.buffer, PANIC_MESSAGE.value,
dw.getUint32(PANIC_MESSAGE_LEN.value, true)));
console.error(message, e);
} else {
console.error(e);
}
return undefined;
}
}
2024-10-08 17:17:13 -05:00
/** @param {HTMLElement} target */
function wireUp(target) {
execApply(target);
cacheInputs(target);
bindTextareaAutoResize(target);
2024-10-12 14:25:37 -05:00
bindCodeEdit(target);
2024-10-08 17:17:13 -05:00
}
2024-10-10 01:35:17 -05:00
/** @type {{ [key: string]: (content: string) => Promise<string> | string }} */
const applyFns = {
timestamp: (content) => new Date(parseInt(content) * 1000).toLocaleString(),
2024-10-12 14:25:37 -05:00
fmt: (content) => modifyCode(content, "fmt").then(c => c ?? "post has invalid code"),
2024-10-10 01:35:17 -05:00
};
2024-10-08 17:17:13 -05:00
2024-10-12 14:25:37 -05:00
/** @param {HTMLElement} target */
async function bindCodeEdit(target) {
const edit = target.querySelector("#code-edit");
if (!(edit instanceof HTMLTextAreaElement)) return;
const errors = target.querySelector("#compiler-output");
if (!(errors instanceof HTMLPreElement)) never();
const hbc = await getHbcInstance();
const debounce = 0;
let timeout = 0;
edit.addEventListener("input", () => {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
const buf = packPosts([{ path: "local.hb", code: edit.value }]);
errors.textContent = compileCode(hbc, buf, 1);
timeout = 0;
}, debounce);
});
}
2024-10-08 17:17:13 -05:00
/** @param {HTMLElement} target */
function execApply(target) {
2024-10-10 01:35:17 -05:00
for (const elem of target.querySelectorAll('[apply]')) {
if (!(elem instanceof HTMLElement)) continue;
const funcname = elem.getAttribute('apply') ?? never();
let res = applyFns[funcname](elem.textContent ?? "");
if (res instanceof Promise) res.then(c => elem.textContent = c);
else elem.textContent = res;
2024-10-08 17:17:13 -05:00
}
}
/** @param {HTMLElement} target */
function bindTextareaAutoResize(target) {
2024-10-10 01:35:17 -05:00
for (const textarea of target.querySelectorAll("textarea")) {
if (!(textarea instanceof HTMLTextAreaElement)) never();
2024-10-12 06:07:49 -05:00
const taCssMap = window.getComputedStyle(textarea);
const padding = parseInt(taCssMap.getPropertyValue('padding-top') ?? "0")
+ parseInt(taCssMap.getPropertyValue('padding-top') ?? "0");
textarea.style.height = "auto";
textarea.style.height = (textarea.scrollHeight - padding) + "px";
2024-10-08 17:17:13 -05:00
textarea.style.overflowY = "hidden";
textarea.addEventListener("input", function() {
textarea.style.height = "auto";
2024-10-12 06:07:49 -05:00
textarea.style.height = (textarea.scrollHeight - padding) + "px";
2024-10-08 17:17:13 -05:00
});
textarea.onkeydown = (ev) => {
if (ev.key === "Tab") {
ev.preventDefault();
2024-10-12 14:25:37 -05:00
document.execCommand('insertText', false, "\t");
2024-10-08 17:17:13 -05:00
}
}
}
}
/** @param {HTMLElement} target */
function cacheInputs(target) {
/**@type {HTMLFormElement}*/ let form;
for (form of target.querySelectorAll('form')) {
const path = form.getAttribute('hx-post') || form.getAttribute('hx-delete');
if (!path) {
console.warn('form does not have a hx-post or hx-delete attribute', form);
continue;
}
2024-10-10 01:35:17 -05:00
for (const input of form.elements) {
if (input instanceof HTMLInputElement || input instanceof HTMLTextAreaElement) {
if ('password submit button'.includes(input.type)) continue;
const key = path + input.name;
input.value = localStorage.getItem(key) ?? '';
input.addEventListener("input", () => localStorage.setItem(key, input.value));
} else {
console.warn("unhandled form element: ", input);
}
2024-10-08 17:17:13 -05:00
}
}
}
2024-10-10 01:35:17 -05:00
if (window.location.hostname === 'localhost') {
let id; setInterval(async () => {
let new_id = await fetch('/hot-reload').then(reps => reps.text());
id ??= new_id;
if (id !== new_id) window.location.reload();
}, 300);
2024-10-12 06:07:49 -05:00
(async function test() {
{
const code = "main:=fn():void{return}";
const fmtd = await modifyCode(code, "fmt") ?? never();
const prev = await modifyCode(fmtd, "minify") ?? never();
if (code != prev) console.error(code, prev);
}
{
2024-10-12 14:25:37 -05:00
const posts = [{
path: "foo.hb",
code: "main:=fn():int{return 42}",
}];
const buf = packPosts(posts);
const res = compileCode(await getHbcInstance(), buf, 1) ?? never();
const expected = "exit code: 42\n";
2024-10-12 06:07:49 -05:00
if (expected != res) console.error(expected, res);
}
2024-10-10 01:35:17 -05:00
})()
}
document.body.addEventListener('htmx:afterSwap', (ev) => {
if (!(ev.target instanceof HTMLElement)) never();
wireUp(ev.target);
});
wireUp(document.body);