feat(wasm): C0/C1 browser Marqdo interpreter on wasm32

Feature-gate host/CLI deps for wasm-core, add run_source, and ship
crates/marqdo-wasm with a raw mq_run ABI plus examples/browser-hello.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-01 10:37:44 +08:00
1 parent f2fd4fb34e
commit a75b7a1c87
22 files changed
+904 -151

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
# browser-hello(WASM C1)
在浏览器里跑 Marqdo `print`(路线 C)。
```bash
# 仓库根目录
cargo build -p marqdo-wasm --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/marqdo_wasm.wasm examples/browser-hello/
cd examples/browser-hello
python3 -m http.server 8765
# 打开 http://127.0.0.1:8765/
```
- `loader.js` 仅为官方宿主桥(分配内存 / 调 `mq_run`),不是业务语言。
- 设计:[browser-marqdo-wasm.md](../../doc/design/browser-marqdo-wasm.md)
+82
View File
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Marqdo browser hello (WASM C1)</title>
<style>
:root {
--ink: #1a1f1c;
--paper: #e8efe6;
--accent: #2f6f4e;
--muted: #5a6b60;
}
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
font-family: "IBM Plex Sans", "Noto Sans SC", sans-serif;
color: var(--ink);
background:
radial-gradient(1200px 600px at 10% -10%, #c5dcc8 0%, transparent 55%),
linear-gradient(165deg, #f3f7f2, var(--paper));
padding: 2rem clamp(1rem, 4vw, 3rem);
}
h1 {
font-family: "IBM Plex Serif", "Noto Serif SC", serif;
font-weight: 600;
font-size: clamp(1.6rem, 3vw, 2.2rem);
margin: 0 0 0.35rem;
letter-spacing: -0.02em;
}
.lead { color: var(--muted); margin: 0 0 1.5rem; max-width: 36rem; }
label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.35rem; }
textarea {
width: min(40rem, 100%);
min-height: 10rem;
padding: 0.75rem 0.9rem;
border: 1px solid #b7c7bb;
border-radius: 2px;
background: rgba(255,255,255,0.7);
font-family: "IBM Plex Mono", ui-monospace, monospace;
font-size: 0.9rem;
line-height: 1.45;
}
button {
margin-top: 0.85rem;
padding: 0.55rem 1.1rem;
border: 0;
background: var(--accent);
color: #f5faf6;
font: inherit;
cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: wait; }
pre {
margin-top: 1.25rem;
width: min(40rem, 100%);
padding: 0.9rem 1rem;
background: #122018;
color: #d7eadc;
overflow: auto;
font-size: 0.85rem;
line-height: 1.4;
}
.err { color: #ffb4a8; }
</style>
</head>
<body>
<h1>Marqdo</h1>
<p class="lead">浏览器内 WASM 解释器(路线 C / C1):源码是 <code>.mq.md</code>,无业务手写 JS。</p>
<label for="src">程序</label>
<textarea id="src"># main
> print text=Hello from browser WASM!
</textarea>
<div>
<button id="run" disabled>运行</button>
</div>
<pre id="out">loading wasm…</pre>
<script type="module" src="./loader.js"></script>
</body>
</html>
+72
View File
@@ -0,0 +1,72 @@
/**
* Thin loader for marqdo_wasm.wasm (C1 raw ABI).
* Not application logic — only host bridge.
*/
const DEFAULT_SRC = `# main
> print text=Hello from browser WASM!
`;
function readCString(memory, ptr) {
const bytes = new Uint8Array(memory.buffer);
let end = ptr;
while (bytes[end] !== 0) end += 1;
return new TextDecoder().decode(bytes.subarray(ptr, end));
}
function runSource(exports, source) {
const encoded = new TextEncoder().encode(source);
const inPtr = exports.mq_alloc(encoded.length || 1);
if (!inPtr) throw new Error("mq_alloc failed");
new Uint8Array(exports.memory.buffer, inPtr, encoded.length).set(encoded);
const outPtr = exports.mq_run(inPtr, encoded.length);
exports.mq_dealloc(inPtr, encoded.length || 1);
if (!outPtr) throw new Error("mq_run returned null");
const view = new DataView(exports.memory.buffer);
const jsonLen = view.getUint32(outPtr, true);
const jsonBytes = new Uint8Array(exports.memory.buffer, outPtr + 4, jsonLen);
const text = new TextDecoder().decode(jsonBytes);
exports.mq_dealloc(outPtr, 4 + jsonLen);
return JSON.parse(text);
}
async function main() {
const out = document.getElementById("out");
const btn = document.getElementById("run");
const src = document.getElementById("src");
if (!src.value.trim()) src.value = DEFAULT_SRC;
const wasmPath = "./marqdo_wasm.wasm";
const res = await fetch(wasmPath);
if (!res.ok) {
out.classList.add("err");
out.textContent =
`failed to fetch ${wasmPath} (${res.status}).\n` +
`Build: cargo build -p marqdo-wasm --target wasm32-unknown-unknown --release\n` +
`Copy: cp target/wasm32-unknown-unknown/release/marqdo_wasm.wasm examples/browser-hello/`;
return;
}
const { instance } = await WebAssembly.instantiateStreaming(res, {});
const exports = instance.exports;
const ver = readCString(exports.memory, exports.mq_version());
out.classList.remove("err");
out.textContent = `marqdo-wasm ${ver} ready.\n`;
btn.disabled = false;
btn.addEventListener("click", () => {
try {
const result = runSource(exports, src.value);
out.classList.toggle("err", !result.ok);
out.textContent = result.ok
? (result.stdout || "(empty stdout)") +
(result.value && result.value !== "None" ? `\n[return] ${result.value}` : "")
: result.error || "error";
} catch (e) {
out.classList.add("err");
out.textContent = String(e);
}
});
}
main();