fix: make Function.compile do less stuff

This commit is contained in:
TopchetoEU 2025-01-22 20:34:00 +02:00
parent f712fb09ae
commit 40f6cfe616
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
6 changed files with 29 additions and 35 deletions

View File

@ -10,6 +10,8 @@ declare interface String {
charCodeAt(i: number): number; charCodeAt(i: number): number;
codePointAt(i: number): number; codePointAt(i: number): number;
startsWith(search: string): boolean;
endsWith(search: string): boolean;
includes(search: string, offset?: number): number; includes(search: string, offset?: number): number;
indexOf(search: string, offset?: number): number; indexOf(search: string, offset?: number): number;
lastIndexOf(search: string, offset?: number): number; lastIndexOf(search: string, offset?: number): number;

View File

@ -105,7 +105,7 @@ export interface Primordials {
exec(target: string, offset: number, indices: boolean): { matches: RegExpMatchArray, end: number } | null; exec(target: string, offset: number, indices: boolean): { matches: RegExpMatchArray, end: number } | null;
groupCount(): number; groupCount(): number;
}; };
compile(src: string): Function; compile(src: string, filename?: string): Function;
setGlobalPrototypes(prototype: Record<string, any>): void; setGlobalPrototypes(prototype: Record<string, any>): void;
now(): number; now(): number;
next(func: () => void): void; next(func: () => void): void;

View File

@ -55,23 +55,8 @@ export const Function = (() => {
return res; return res;
} }
public static compile(src = "", { globals = [], wrap = false }: { globals?: string[], wrap?: boolean } = {}) { public static compile(src: string, filename?: string) {
const parts = []; return compile(String(src), filename);
if (wrap) parts[parts.length] = "return (function() {\n";
if (globals.length > 0) {
parts[parts.length] = "let {";
for (let i = 0; i < globals.length; i++) {
if (i > 0) parts[parts.length] = ",";
parts[parts.length] = globals[i];
}
parts[parts.length] = "} = arguments[0];";
}
parts[parts.length] = src;
if (wrap) parts[parts.length] = "\n})(arguments[0])";
const res = compile(string.stringBuild(parts));
return res;
} }
} }

View File

@ -109,10 +109,12 @@ export default function typescript(next: Compiler): Compiler {
registerSource(filename, code); registerSource(filename, code);
const compiled = next("ts-internal://" + filename, result, SourceMap.chain(map, prevMap)); const compiled = next("ts-internal://" + filename, result, SourceMap.chain(map, prevMap));
return function (this: any) { const func = function (this: any) {
const res = compiled.apply(this, arguments); const res = compiled.apply(this, arguments);
if (declaration !== '') files["/src." + declI++ + ".d.ts"] = ScriptSnapshot.fromString(declaration); if (declaration !== '') files["/src." + declI++ + ".d.ts"] = ScriptSnapshot.fromString(declaration);
return res; return res;
}; };
func.name = filename;
return func;
}; };
} }

View File

@ -786,7 +786,12 @@ public class SimpleRepl {
return Value.UNDEFINED; return Value.UNDEFINED;
})); }));
res.defineOwnField(env, "compile", new NativeFunction(args -> { res.defineOwnField(env, "compile", new NativeFunction(args -> {
return Compiler.compileFunc(env, new Filename(Metadata.name(), "func" + i[0]++ + ".js"), args.get(0).toString(env)); var nameVal = args.get(1);
var name = nameVal instanceof VoidValue ?
new Filename(Metadata.name(), "func" + i[0]++ + ".js") :
Filename.parse(nameVal.toString(args.env));
return Compiler.compileFunc(env, name, args.get(0).toString(env));
})); }));
res.defineOwnField(env, "now", new NativeFunction(args -> { res.defineOwnField(env, "now", new NativeFunction(args -> {
return NumberValue.of(System.currentTimeMillis()); return NumberValue.of(System.currentTimeMillis());