71 lines
2.3 KiB
HTML
71 lines
2.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Pybricks Compiler</title>
|
|
</head>
|
|
|
|
<body>
|
|
<pre id="out">Compiling...</pre>
|
|
<script>
|
|
const WASM_URL = 'https://cdn.jsdelivr.net/npm/@pybricks/mpy-cross-v6@2.0.0/build/mpy-cross-v6.wasm';
|
|
|
|
async function run() {
|
|
const out = document.getElementById('out');
|
|
|
|
// Load mpy-cross JS from CDN
|
|
await new Promise((resolve, reject) => {
|
|
const s = document.createElement('script');
|
|
s.src = 'https://cdn.jsdelivr.net/npm/@pybricks/mpy-cross-v6@2.0.0/build/mpy-cross-v6.js';
|
|
s.onload = resolve;
|
|
s.onerror = reject;
|
|
document.head.appendChild(s);
|
|
});
|
|
|
|
// Fetch main.py from same directory as this HTML file
|
|
const code = await fetch('./main.py').then(r => r.text());
|
|
|
|
// Compile
|
|
const result = await new Promise((resolve, reject) => {
|
|
try {
|
|
MpyCross({
|
|
arguments: ['main.py'],
|
|
inputFileContents: code,
|
|
locateFile: (path) => path === 'mpy-cross-v6.wasm' ? WASM_URL : path,
|
|
callback: (status, mpy, out, err) => resolve({ status, mpy, out, err }),
|
|
});
|
|
} catch (e) { reject(e); }
|
|
});
|
|
|
|
if (result.status !== 0) {
|
|
out.textContent = 'Error:\n' + result.err;
|
|
return;
|
|
}
|
|
|
|
// Wrap with __main__ header to match pybricksdev multi-file format
|
|
const name = '__main__';
|
|
const nameBytes = new TextEncoder().encode(name + '\x00');
|
|
const sizeBytes = new Uint8Array(4);
|
|
new DataView(sizeBytes.buffer).setUint32(0, result.mpy.length, true);
|
|
const wrapped = new Uint8Array(sizeBytes.length + nameBytes.length + result.mpy.length);
|
|
wrapped.set(sizeBytes, 0);
|
|
wrapped.set(nameBytes, sizeBytes.length);
|
|
wrapped.set(result.mpy, sizeBytes.length + nameBytes.length);
|
|
|
|
// Auto-download the .mpy
|
|
const blob = new Blob([wrapped], { type: 'application/octet-stream' });
|
|
const a = document.createElement('a');
|
|
a.href = URL.createObjectURL(blob);
|
|
a.download = 'main.mpy';
|
|
a.click();
|
|
URL.revokeObjectURL(a.href);
|
|
|
|
out.textContent = 'Done! ' + wrapped.length + ' bytes — check your downloads.';
|
|
}
|
|
|
|
run().catch(e => document.getElementById('out').textContent = 'Error: ' + (e?.stack || e?.message || JSON.stringify(e)));
|
|
</script>
|
|
</body>
|
|
|
|
</html> |