/ simulated_instruments / triangle
Sign in
simulated_instruments / triangle
triangle / webapp / src / mfiles.ts
44 lines · 1.4 KBBlameHistoryRaw
1// The MATLAB implementation, loaded from ../matlab at build time. The app
2// keeps an editable in-memory copy of these sources (see App.tsx); edits feed
3// straight into the render worker and are deliberately not persisted, so a
4// reload restores the shipped code.
6export interface MFile {
7 name: string;
8 source: string;
9}
11const modules = import.meta.glob('../../matlab/*.m', {
12 eager: true,
13 query: '?raw',
14 import: 'default',
15}) as Record<string, string>;
17export const DEFAULT_MFILES: MFile[] = Object.entries(modules)
18 .map(([path, source]) => ({ name: path.split('/').pop()!, source }))
19 .sort((a, b) => a.name.localeCompare(b.name));
21// Files the app itself executes. The others are entry points for running the
22// model from the numbl CLI, and editing them changes nothing in the browser.
23export const EXECUTED_BY_APP = new Set([
24 'triangle_geometry.m',
25 'triangle_fem.m',
26 'triangle_basis.m',
27 'triangle_strikes.m',
28 'triangle_rng.m',
29 'triangle_radiate.m',
30 'triangle_output.m',
31]);
33// Cheap content hash (FNV-1a) so the worker can key its caches on the code.
34export function hashMFiles(files: MFile[]): string {
35 let h = 0x811c9dc5;
36 for (const f of files) {
37 const s = f.name + ' ' + f.source + '';
38 for (let i = 0; i < s.length; i++) {
39 h ^= s.charCodeAt(i);
40 h = Math.imul(h, 0x01000193);
41 }
42 }
43 return (h >>> 0).toString(36);
↑↓ move↵openescclose