File size: 892 Bytes
161cb23
 
 
 
7667cef
 
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { loadWeights, scoreCode } from "shared:engine";

const WARMUP_CODE = ["def warm(items):", "    for item in items:", "        return item", ""].join("\n");

const ASSET_QUERY = new URL(import.meta.url).search;
const weightsReady = loadWeights(new URL(`./weights.npz${ASSET_QUERY}`, import.meta.url).href).then((weights) => {
  scoreCode(WARMUP_CODE, weights);
  return weights;
});

globalThis.addEventListener("message", async (event) => {
  const { id, code, threshold } = event.data;
  try {
    const weights = await weightsReady;
    const started = performance.now();
    const { score, squiggles } = scoreCode(code, weights, threshold);
    const ms = performance.now() - started;
    globalThis.postMessage({ id, result: { score, squiggles, ms } });
  } catch (error) {
    globalThis.postMessage({ id, error: String(error && error.message ? error.message : error) });
  }
});