Remove jit.literal and handle literal embedding automatically

This commit is contained in:
luaneko 2025-01-10 04:05:35 +11:00
parent 328cb0c655
commit dff22f2b41
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
3 changed files with 17 additions and 7 deletions

View File

@ -1,5 +1,5 @@
{ {
"name": "@luaneko/lstd", "name": "@luaneko/lstd",
"version": "0.1.6", "version": "0.2.0",
"exports": "./mod.ts" "exports": "./mod.ts"
} }

View File

@ -72,7 +72,7 @@ export class Emitter<F extends (...args: any) => void> {
${jit.map(" ", ls, ({ f }) => { ${jit.map(" ", ls, ({ f }) => {
return jit`${f}(${jit.fragment(", ", ...ps.slice(0, f.length))});`; return jit`${f}(${jit.fragment(", ", ...ps.slice(0, f.length))});`;
})} })}
return ${jit.literal(ls.length !== 0)}; return ${ls.length !== 0};
}`; }`;
} }
} }

20
jit.ts
View File

@ -18,6 +18,21 @@ export class JitCompiler {
} }
enclose(x: unknown) { enclose(x: unknown) {
switch (typeof x) {
case "boolean":
case "number":
return this.write(`${x}`);
case "bigint":
return this.write(`${x}n`);
case "string":
return this.write(JSON.stringify(x));
case "object":
if (x === null) return this.write(`null`);
break;
case "undefined":
return this.write(`undefined`);
}
let name = this.#args.get(x); let name = this.#args.get(x);
if (!name) this.#args.set(x, (name = `__x${this.#args.size}`)); if (!name) this.#args.set(x, (name = `__x${this.#args.size}`));
this.write(name); this.write(name);
@ -52,7 +67,6 @@ export function jit(
jit.compiled = compiled; jit.compiled = compiled;
jit.raw = raw; jit.raw = raw;
jit.fragment = fragment; jit.fragment = fragment;
jit.literal = literal;
jit.map = map; jit.map = map;
jit.if = condition; jit.if = condition;
@ -92,10 +106,6 @@ export function fragment(
}; };
} }
export function literal(x: unknown) {
return raw(typeof x === "undefined" ? "undefined" : JSON.stringify(x));
}
export function map<T>( export function map<T>(
sep: string | JitFragment, sep: string | JitFragment,
xs: Iterable<T>, xs: Iterable<T>,