fix: make Function.compile do less stuff
This commit is contained in:
parent
f712fb09ae
commit
40f6cfe616
2
lib/src/lib/values/string.d.ts
vendored
2
lib/src/lib/values/string.d.ts
vendored
@ -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;
|
||||
|
@ -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<string, any>): void;
|
||||
now(): number;
|
||||
next(func: () => void): void;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user