From 40f6cfe6164d65019d4afaeb8d4a367d1354e023 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 22 Jan 2025 20:34:00 +0200 Subject: [PATCH] fix: make Function.compile do less stuff --- lib/src/lib/values/string.d.ts | 2 ++ lib/src/stdlib/primordials.ts | 2 +- lib/src/stdlib/values/function.ts | 19 ++---------- lib/src/transpiler/typescript.ts | 4 ++- lib/tsconfig.json | 30 +++++++++---------- .../me/topchetoeu/j2s/repl/SimpleRepl.java | 7 ++++- 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/src/lib/values/string.d.ts b/lib/src/lib/values/string.d.ts index e4fa52d..6c73d7a 100644 --- a/lib/src/lib/values/string.d.ts +++ b/lib/src/lib/values/string.d.ts @@ -10,6 +10,8 @@ declare interface String { charCodeAt(i: number): number; codePointAt(i: number): number; + startsWith(search: string): boolean; + endsWith(search: string): boolean; includes(search: string, offset?: number): number; indexOf(search: string, offset?: number): number; lastIndexOf(search: string, offset?: number): number; diff --git a/lib/src/stdlib/primordials.ts b/lib/src/stdlib/primordials.ts index a7c170d..455feb9 100644 --- a/lib/src/stdlib/primordials.ts +++ b/lib/src/stdlib/primordials.ts @@ -105,7 +105,7 @@ export interface Primordials { exec(target: string, offset: number, indices: boolean): { matches: RegExpMatchArray, end: number } | null; groupCount(): number; }; - compile(src: string): Function; + compile(src: string, filename?: string): Function; setGlobalPrototypes(prototype: Record): void; now(): number; next(func: () => void): void; diff --git a/lib/src/stdlib/values/function.ts b/lib/src/stdlib/values/function.ts index e801293..cf7e630 100644 --- a/lib/src/stdlib/values/function.ts +++ b/lib/src/stdlib/values/function.ts @@ -55,23 +55,8 @@ export const Function = (() => { return res; } - public static compile(src = "", { globals = [], wrap = false }: { globals?: string[], wrap?: boolean } = {}) { - const parts = []; - - 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; + public static compile(src: string, filename?: string) { + return compile(String(src), filename); } } diff --git a/lib/src/transpiler/typescript.ts b/lib/src/transpiler/typescript.ts index 9a76861..02c0f7a 100644 --- a/lib/src/transpiler/typescript.ts +++ b/lib/src/transpiler/typescript.ts @@ -109,10 +109,12 @@ export default function typescript(next: Compiler): Compiler { registerSource(filename, code); 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); if (declaration !== '') files["/src." + declI++ + ".d.ts"] = ScriptSnapshot.fromString(declaration); return res; }; + func.name = filename; + return func; }; } diff --git a/lib/tsconfig.json b/lib/tsconfig.json index 0ca823e..2a4bfd5 100644 --- a/lib/tsconfig.json +++ b/lib/tsconfig.json @@ -1,17 +1,17 @@ { - "include": ["**/*.ts"], - "compilerOptions": { - "strict": true, - "esModuleInterop": true, - "skipLibCheck": true, - "moduleResolution": "Bundler", - "module": "ESNext", - "target": "ESNext", - "noLib": true, - "forceConsistentCasingInFileNames": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "allowImportingTsExtensions": true, - "noEmit": true - } + "include": ["**/*.ts"], + "compilerOptions": { + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "moduleResolution": "Bundler", + "module": "ESNext", + "target": "ESNext", + "noLib": true, + "forceConsistentCasingInFileNames": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowImportingTsExtensions": true, + "noEmit": true + } } diff --git a/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java b/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java index 9047c44..b002c50 100644 --- a/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java +++ b/repl/src/main/java/me/topchetoeu/j2s/repl/SimpleRepl.java @@ -786,7 +786,12 @@ public class SimpleRepl { return Value.UNDEFINED; })); 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 -> { return NumberValue.of(System.currentTimeMillis());