Remove jit.literal and handle literal embedding automatically

This commit is contained in:
luaneko 2025-01-10 03:58:36 +11:00
parent 328cb0c655
commit dfae22ac70
Signed by: luaneko
GPG Key ID: 406809B8763FF07A
2 changed files with 16 additions and 6 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"
} }

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>,