Skip to content

Commit 3bb5991

Browse files
kkratterfclaude
andcommitted
feat(playground): add WASM compiler build
Build FratmScript compiler to WebAssembly using wasm-pack. This enables the playground to use the real compiler instead of the demo fallback transpiler. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 626c202 commit 3bb5991

File tree

5 files changed

+415
-0
lines changed

5 files changed

+415
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
4+
/**
5+
* Compile FratmScript source to JavaScript
6+
*
7+
* Returns a JSON object with:
8+
* - `success`: boolean
9+
* - `code`: string (if success)
10+
* - `sourceMap`: string (if success and requested)
11+
* - `error`: string (if failure)
12+
* - `line`: number (if failure)
13+
* - `column`: number (if failure)
14+
*/
15+
export function compile(source: string, generate_source_map: boolean): any;
16+
17+
/**
18+
* Initialize panic hook for better error messages in browser console
19+
*/
20+
export function init(): void;
21+
22+
/**
23+
* Tokenize source code (for syntax highlighting)
24+
*/
25+
export function tokenize(source: string): any;
26+
27+
/**
28+
* Get the compiler version
29+
*/
30+
export function version(): string;
31+
32+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
33+
34+
export interface InitOutput {
35+
readonly memory: WebAssembly.Memory;
36+
readonly compile: (a: number, b: number, c: number) => any;
37+
readonly tokenize: (a: number, b: number) => any;
38+
readonly version: () => [number, number];
39+
readonly init: () => void;
40+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
41+
readonly __wbindgen_malloc: (a: number, b: number) => number;
42+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
43+
readonly __wbindgen_externrefs: WebAssembly.Table;
44+
readonly __wbindgen_start: () => void;
45+
}
46+
47+
export type SyncInitInput = BufferSource | WebAssembly.Module;
48+
49+
/**
50+
* Instantiates the given `module`, which can either be bytes or
51+
* a precompiled `WebAssembly.Module`.
52+
*
53+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
54+
*
55+
* @returns {InitOutput}
56+
*/
57+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
58+
59+
/**
60+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
61+
* for everything else, calls `WebAssembly.instantiate` directly.
62+
*
63+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
64+
*
65+
* @returns {Promise<InitOutput>}
66+
*/
67+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
/* @ts-self-types="./fratm_wasm.d.ts" */
2+
3+
/**
4+
* Compile FratmScript source to JavaScript
5+
*
6+
* Returns a JSON object with:
7+
* - `success`: boolean
8+
* - `code`: string (if success)
9+
* - `sourceMap`: string (if success and requested)
10+
* - `error`: string (if failure)
11+
* - `line`: number (if failure)
12+
* - `column`: number (if failure)
13+
* @param {string} source
14+
* @param {boolean} generate_source_map
15+
* @returns {any}
16+
*/
17+
export function compile(source, generate_source_map) {
18+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
19+
const len0 = WASM_VECTOR_LEN;
20+
const ret = wasm.compile(ptr0, len0, generate_source_map);
21+
return ret;
22+
}
23+
24+
/**
25+
* Initialize panic hook for better error messages in browser console
26+
*/
27+
export function init() {
28+
wasm.init();
29+
}
30+
31+
/**
32+
* Tokenize source code (for syntax highlighting)
33+
* @param {string} source
34+
* @returns {any}
35+
*/
36+
export function tokenize(source) {
37+
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
38+
const len0 = WASM_VECTOR_LEN;
39+
const ret = wasm.tokenize(ptr0, len0);
40+
return ret;
41+
}
42+
43+
/**
44+
* Get the compiler version
45+
* @returns {string}
46+
*/
47+
export function version() {
48+
let deferred1_0;
49+
let deferred1_1;
50+
try {
51+
const ret = wasm.version();
52+
deferred1_0 = ret[0];
53+
deferred1_1 = ret[1];
54+
return getStringFromWasm0(ret[0], ret[1]);
55+
} finally {
56+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
57+
}
58+
}
59+
60+
function __wbg_get_imports() {
61+
const import0 = {
62+
__proto__: null,
63+
__wbg_Error_8c4e43fe74559d73: function(arg0, arg1) {
64+
const ret = Error(getStringFromWasm0(arg0, arg1));
65+
return ret;
66+
},
67+
__wbg_Number_04624de7d0e8332d: function(arg0) {
68+
const ret = Number(arg0);
69+
return ret;
70+
},
71+
__wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
72+
throw new Error(getStringFromWasm0(arg0, arg1));
73+
},
74+
__wbg_error_7534b8e9a36f1ab4: function(arg0, arg1) {
75+
let deferred0_0;
76+
let deferred0_1;
77+
try {
78+
deferred0_0 = arg0;
79+
deferred0_1 = arg1;
80+
console.error(getStringFromWasm0(arg0, arg1));
81+
} finally {
82+
wasm.__wbindgen_free(deferred0_0, deferred0_1, 1);
83+
}
84+
},
85+
__wbg_new_361308b2356cecd0: function() {
86+
const ret = new Object();
87+
return ret;
88+
},
89+
__wbg_new_3eb36ae241fe6f44: function() {
90+
const ret = new Array();
91+
return ret;
92+
},
93+
__wbg_new_8a6f238a6ece86ea: function() {
94+
const ret = new Error();
95+
return ret;
96+
},
97+
__wbg_set_3f1d0b984ed272ed: function(arg0, arg1, arg2) {
98+
arg0[arg1] = arg2;
99+
},
100+
__wbg_set_f43e577aea94465b: function(arg0, arg1, arg2) {
101+
arg0[arg1 >>> 0] = arg2;
102+
},
103+
__wbg_stack_0ed75d68575b0f3c: function(arg0, arg1) {
104+
const ret = arg1.stack;
105+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
106+
const len1 = WASM_VECTOR_LEN;
107+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
108+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
109+
},
110+
__wbindgen_cast_0000000000000001: function(arg0) {
111+
// Cast intrinsic for `F64 -> Externref`.
112+
const ret = arg0;
113+
return ret;
114+
},
115+
__wbindgen_cast_0000000000000002: function(arg0, arg1) {
116+
// Cast intrinsic for `Ref(String) -> Externref`.
117+
const ret = getStringFromWasm0(arg0, arg1);
118+
return ret;
119+
},
120+
__wbindgen_cast_0000000000000003: function(arg0) {
121+
// Cast intrinsic for `U64 -> Externref`.
122+
const ret = BigInt.asUintN(64, arg0);
123+
return ret;
124+
},
125+
__wbindgen_init_externref_table: function() {
126+
const table = wasm.__wbindgen_externrefs;
127+
const offset = table.grow(4);
128+
table.set(0, undefined);
129+
table.set(offset + 0, undefined);
130+
table.set(offset + 1, null);
131+
table.set(offset + 2, true);
132+
table.set(offset + 3, false);
133+
},
134+
};
135+
return {
136+
__proto__: null,
137+
"./fratm_wasm_bg.js": import0,
138+
};
139+
}
140+
141+
let cachedDataViewMemory0 = null;
142+
function getDataViewMemory0() {
143+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
144+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
145+
}
146+
return cachedDataViewMemory0;
147+
}
148+
149+
function getStringFromWasm0(ptr, len) {
150+
ptr = ptr >>> 0;
151+
return decodeText(ptr, len);
152+
}
153+
154+
let cachedUint8ArrayMemory0 = null;
155+
function getUint8ArrayMemory0() {
156+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
157+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
158+
}
159+
return cachedUint8ArrayMemory0;
160+
}
161+
162+
function passStringToWasm0(arg, malloc, realloc) {
163+
if (realloc === undefined) {
164+
const buf = cachedTextEncoder.encode(arg);
165+
const ptr = malloc(buf.length, 1) >>> 0;
166+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
167+
WASM_VECTOR_LEN = buf.length;
168+
return ptr;
169+
}
170+
171+
let len = arg.length;
172+
let ptr = malloc(len, 1) >>> 0;
173+
174+
const mem = getUint8ArrayMemory0();
175+
176+
let offset = 0;
177+
178+
for (; offset < len; offset++) {
179+
const code = arg.charCodeAt(offset);
180+
if (code > 0x7F) break;
181+
mem[ptr + offset] = code;
182+
}
183+
if (offset !== len) {
184+
if (offset !== 0) {
185+
arg = arg.slice(offset);
186+
}
187+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
188+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
189+
const ret = cachedTextEncoder.encodeInto(arg, view);
190+
191+
offset += ret.written;
192+
ptr = realloc(ptr, len, offset, 1) >>> 0;
193+
}
194+
195+
WASM_VECTOR_LEN = offset;
196+
return ptr;
197+
}
198+
199+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
200+
cachedTextDecoder.decode();
201+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
202+
let numBytesDecoded = 0;
203+
function decodeText(ptr, len) {
204+
numBytesDecoded += len;
205+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
206+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
207+
cachedTextDecoder.decode();
208+
numBytesDecoded = len;
209+
}
210+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
211+
}
212+
213+
const cachedTextEncoder = new TextEncoder();
214+
215+
if (!('encodeInto' in cachedTextEncoder)) {
216+
cachedTextEncoder.encodeInto = function (arg, view) {
217+
const buf = cachedTextEncoder.encode(arg);
218+
view.set(buf);
219+
return {
220+
read: arg.length,
221+
written: buf.length
222+
};
223+
};
224+
}
225+
226+
let WASM_VECTOR_LEN = 0;
227+
228+
let wasmModule, wasm;
229+
function __wbg_finalize_init(instance, module) {
230+
wasm = instance.exports;
231+
wasmModule = module;
232+
cachedDataViewMemory0 = null;
233+
cachedUint8ArrayMemory0 = null;
234+
wasm.__wbindgen_start();
235+
return wasm;
236+
}
237+
238+
async function __wbg_load(module, imports) {
239+
if (typeof Response === 'function' && module instanceof Response) {
240+
if (typeof WebAssembly.instantiateStreaming === 'function') {
241+
try {
242+
return await WebAssembly.instantiateStreaming(module, imports);
243+
} catch (e) {
244+
const validResponse = module.ok && expectedResponseType(module.type);
245+
246+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
247+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
248+
249+
} else { throw e; }
250+
}
251+
}
252+
253+
const bytes = await module.arrayBuffer();
254+
return await WebAssembly.instantiate(bytes, imports);
255+
} else {
256+
const instance = await WebAssembly.instantiate(module, imports);
257+
258+
if (instance instanceof WebAssembly.Instance) {
259+
return { instance, module };
260+
} else {
261+
return instance;
262+
}
263+
}
264+
265+
function expectedResponseType(type) {
266+
switch (type) {
267+
case 'basic': case 'cors': case 'default': return true;
268+
}
269+
return false;
270+
}
271+
}
272+
273+
function initSync(module) {
274+
if (wasm !== undefined) return wasm;
275+
276+
277+
if (module !== undefined) {
278+
if (Object.getPrototypeOf(module) === Object.prototype) {
279+
({module} = module)
280+
} else {
281+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
282+
}
283+
}
284+
285+
const imports = __wbg_get_imports();
286+
if (!(module instanceof WebAssembly.Module)) {
287+
module = new WebAssembly.Module(module);
288+
}
289+
const instance = new WebAssembly.Instance(module, imports);
290+
return __wbg_finalize_init(instance, module);
291+
}
292+
293+
async function __wbg_init(module_or_path) {
294+
if (wasm !== undefined) return wasm;
295+
296+
297+
if (module_or_path !== undefined) {
298+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
299+
({module_or_path} = module_or_path)
300+
} else {
301+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
302+
}
303+
}
304+
305+
if (module_or_path === undefined) {
306+
module_or_path = new URL('fratm_wasm_bg.wasm', import.meta.url);
307+
}
308+
const imports = __wbg_get_imports();
309+
310+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
311+
module_or_path = fetch(module_or_path);
312+
}
313+
314+
const { instance, module } = await __wbg_load(await module_or_path, imports);
315+
316+
return __wbg_finalize_init(instance, module);
317+
}
318+
319+
export { initSync, __wbg_init as default };
173 KB
Binary file not shown.

0 commit comments

Comments
 (0)