From 42f443572aab80529c31ee38c991386a0a87daf2 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Mon, 18 Dec 2023 21:53:00 +0200 Subject: [PATCH] fix: how the hell did i fix this (it was the cache all along) --- src/assets/js/bootstrap.js | 1 - src/assets/js/lib.d.ts | 7 +- .../jscript/compilation/CompileTarget.java | 13 --- .../compilation/CompoundStatement.java | 15 ++-- .../compilation/VariableDeclareStatement.java | 8 +- .../compilation/control/ForInStatement.java | 7 +- .../compilation/control/TryStatement.java | 2 +- .../compilation/values/CallStatement.java | 2 +- .../compilation/values/FunctionStatement.java | 19 ++++- .../jscript/engine/frame/CodeFrame.java | 1 + .../topchetoeu/jscript/parsing/Parsing.java | 2 +- test.txt | 80 ------------------- 12 files changed, 42 insertions(+), 115 deletions(-) delete mode 100644 test.txt diff --git a/src/assets/js/bootstrap.js b/src/assets/js/bootstrap.js index 82ac036..74f854d 100644 --- a/src/assets/js/bootstrap.js +++ b/src/assets/js/bootstrap.js @@ -64,7 +64,6 @@ if (!environments[env.id]) environments[env.id] = [] declSnapshots = environments[env.id]; - debugger; var emit = service.getEmitOutput("/src.ts"); var diagnostics = [] diff --git a/src/assets/js/lib.d.ts b/src/assets/js/lib.d.ts index 377d7aa..b843aba 100644 --- a/src/assets/js/lib.d.ts +++ b/src/assets/js/lib.d.ts @@ -458,11 +458,16 @@ interface SymbolConstructor { readonly asyncIterator: unique symbol; } + interface Promise extends Thenable { catch(func: (err: unknown) => ResT): Promise; finally(func: () => void): Promise; + constructor: PromiseConstructor; } -interface PromiseConstructor { +interface PromiseConstructorLike { + new (func: (res: (val: T) => void, rej: (err: unknown) => void) => void): Thenable>; +} +interface PromiseConstructor extends PromiseConstructorLike { prototype: Promise; new (func: (res: (val: T) => void, rej: (err: unknown) => void) => void): Promise>; diff --git a/src/me/topchetoeu/jscript/compilation/CompileTarget.java b/src/me/topchetoeu/jscript/compilation/CompileTarget.java index 06edb94..da3166b 100644 --- a/src/me/topchetoeu/jscript/compilation/CompileTarget.java +++ b/src/me/topchetoeu/jscript/compilation/CompileTarget.java @@ -15,15 +15,9 @@ public class CompileTarget { public final Map functions; public final TreeSet breakpoints; private final HashMap bpToInstr = new HashMap<>(); - private BreakpointType queueType = BreakpointType.NONE; - private Location queueLoc = null; public Instruction add(Instruction instr) { target.add(instr); - if (queueType != BreakpointType.NONE) setDebug(queueType); - if (queueLoc != null) instr.locate(queueLoc); - queueType = BreakpointType.NONE; - queueLoc = null; return instr; } public Instruction set(int i, Instruction instr) { @@ -56,13 +50,6 @@ public class CompileTarget { else return target.get(target.size() - 1).location; } - public void queueDebug(BreakpointType type) { - queueType = type; - } - public void queueDebug(BreakpointType type, Location loc) { - queueType = type; - } - public Instruction[] array() { return target.toArray(Instruction[]::new); } public FunctionBody body() { diff --git a/src/me/topchetoeu/jscript/compilation/CompoundStatement.java b/src/me/topchetoeu/jscript/compilation/CompoundStatement.java index 38f9dfe..010ece3 100644 --- a/src/me/topchetoeu/jscript/compilation/CompoundStatement.java +++ b/src/me/topchetoeu/jscript/compilation/CompoundStatement.java @@ -1,5 +1,8 @@ package me.topchetoeu.jscript.compilation; +import java.util.List; +import java.util.Vector; + import me.topchetoeu.jscript.Location; import me.topchetoeu.jscript.compilation.Instruction.BreakpointType; import me.topchetoeu.jscript.compilation.values.FunctionStatement; @@ -25,19 +28,21 @@ public class CompoundStatement extends Statement { @Override public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType type) { - if (separateFuncs) for (var stm : statements) { + List statements = new Vector(); + if (separateFuncs) for (var stm : this.statements) { if (stm instanceof FunctionStatement && ((FunctionStatement)stm).statement) { stm.compile(target, scope, false); } + else statements.add(stm); } + else statements = List.of(this.statements); var polluted = false; - for (var i = 0; i < statements.length; i++) { - var stm = statements[i]; + for (var i = 0; i < statements.size(); i++) { + var stm = statements.get(i); - if (separateFuncs && stm instanceof FunctionStatement) continue; - if (i != statements.length - 1) stm.compile(target, scope, false, BreakpointType.STEP_OVER); + if (i != statements.size() - 1) stm.compile(target, scope, false, BreakpointType.STEP_OVER); else stm.compile(target, scope, polluted = pollute, BreakpointType.STEP_OVER); } diff --git a/src/me/topchetoeu/jscript/compilation/VariableDeclareStatement.java b/src/me/topchetoeu/jscript/compilation/VariableDeclareStatement.java index 7554060..4c3b731 100644 --- a/src/me/topchetoeu/jscript/compilation/VariableDeclareStatement.java +++ b/src/me/topchetoeu/jscript/compilation/VariableDeclareStatement.java @@ -36,10 +36,10 @@ public class VariableDeclareStatement extends Statement { if (key instanceof String) target.add(Instruction.makeVar(entry.location, (String)key)); - target.queueDebug(BreakpointType.STEP_OVER, entry.location); - if (entry.value != null) FunctionStatement.compileWithName(entry.value, target, scope, true, entry.name); - else target.add(Instruction.loadValue(entry.location, null)); - target.add(Instruction.storeVar(entry.location, key)); + if (entry.value != null) { + FunctionStatement.compileWithName(entry.value, target, scope, true, entry.name, BreakpointType.STEP_OVER); + target.add(Instruction.storeVar(entry.location, key)); + } } if (pollute) target.add(Instruction.loadValue(loc(), null)); diff --git a/src/me/topchetoeu/jscript/compilation/control/ForInStatement.java b/src/me/topchetoeu/jscript/compilation/control/ForInStatement.java index e6e9825..ae40175 100644 --- a/src/me/topchetoeu/jscript/compilation/control/ForInStatement.java +++ b/src/me/topchetoeu/jscript/compilation/control/ForInStatement.java @@ -44,11 +44,10 @@ public class ForInStatement extends Statement { target.add(Instruction.nop(loc())); target.add(Instruction.loadMember(varLocation, "value")); - target.queueDebug(BreakpointType.STEP_OVER, object.loc()); - target.add(Instruction.storeVar(varLocation, key)); + target.add(Instruction.storeVar(object.loc(), key)); + target.setDebug(BreakpointType.STEP_OVER); - target.queueDebug(BreakpointType.STEP_OVER, body.loc()); - body.compile(target, scope, false); + body.compile(target, scope, false, BreakpointType.STEP_OVER); int end = target.size(); diff --git a/src/me/topchetoeu/jscript/compilation/control/TryStatement.java b/src/me/topchetoeu/jscript/compilation/control/TryStatement.java index 30b6c29..f906d9e 100644 --- a/src/me/topchetoeu/jscript/compilation/control/TryStatement.java +++ b/src/me/topchetoeu/jscript/compilation/control/TryStatement.java @@ -46,8 +46,8 @@ public class TryStatement extends Statement { target.add(Instruction.tryEnd(loc())); } - target.queueDebug(BreakpointType.STEP_OVER); target.set(start - 1, Instruction.tryStart(loc(), catchStart, finallyStart, target.size() - start)); + target.setDebug(start - 1, BreakpointType.STEP_OVER); if (pollute) target.add(Instruction.loadValue(loc(), null)); } diff --git a/src/me/topchetoeu/jscript/compilation/values/CallStatement.java b/src/me/topchetoeu/jscript/compilation/values/CallStatement.java index e10e1c2..2ccacb0 100644 --- a/src/me/topchetoeu/jscript/compilation/values/CallStatement.java +++ b/src/me/topchetoeu/jscript/compilation/values/CallStatement.java @@ -25,9 +25,9 @@ public class CallStatement extends Statement { for (var arg : args) arg.compile(target, scope, true); - target.queueDebug(type); if (isNew) target.add(Instruction.callNew(loc(), args.length)); else target.add(Instruction.call(loc(), args.length)); + target.setDebug(type); if (!pollute) target.add(Instruction.discard(loc())); } diff --git a/src/me/topchetoeu/jscript/compilation/values/FunctionStatement.java b/src/me/topchetoeu/jscript/compilation/values/FunctionStatement.java index b315423..06d1540 100644 --- a/src/me/topchetoeu/jscript/compilation/values/FunctionStatement.java +++ b/src/me/topchetoeu/jscript/compilation/values/FunctionStatement.java @@ -42,7 +42,7 @@ public class FunctionStatement extends Statement { } } - private long compileBody(CompileTarget target, ScopeRecord scope, boolean polute) { + private long compileBody(CompileTarget target, ScopeRecord scope, boolean polute, BreakpointType bp) { for (var i = 0; i < args.length; i++) { for (var j = 0; j < i; j++) { if (args[i].equals(args[j])) { @@ -68,6 +68,7 @@ public class FunctionStatement extends Statement { if (!statement && this.varName != null) { subtarget.add(Instruction.storeSelfFunc(loc(), (int)subscope.define(this.varName))); + subtarget.setDebug(bp); } body.declare(subscope); @@ -84,13 +85,13 @@ public class FunctionStatement extends Statement { return id; } - public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, String name) { + public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, String name, BreakpointType bp) { if (this.varName != null) name = this.varName; var hasVar = this.varName != null && statement; var hasName = name != null; - compileBody(target, scope, pollute || hasVar || hasName); + compileBody(target, scope, pollute || hasVar || hasName, bp); if (hasName) { if (pollute || hasVar) target.add(Instruction.dup(loc())); @@ -106,8 +107,14 @@ public class FunctionStatement extends Statement { target.add(Instruction.storeVar(loc(), scope.getKey(this.varName), false)); } } + public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, String name) { + compile(target, scope, pollute, name, BreakpointType.NONE); + } + @Override public void compile(CompileTarget target, ScopeRecord scope, boolean pollute, BreakpointType bp) { + compile(target, scope, pollute, (String)null, bp); + } @Override public void compile(CompileTarget target, ScopeRecord scope, boolean pollute) { - compile(target, scope, pollute, BreakpointType.NONE); + compile(target, scope, pollute, (String)null, BreakpointType.NONE); } public FunctionStatement(Location loc, Location end, String varName, String[] args, boolean statement, CompoundStatement body) { @@ -125,4 +132,8 @@ public class FunctionStatement extends Statement { if (stm instanceof FunctionStatement) ((FunctionStatement)stm).compile(target, scope, pollute, name); else stm.compile(target, scope, pollute); } + public static void compileWithName(Statement stm, CompileTarget target, ScopeRecord scope, boolean pollute, String name, BreakpointType bp) { + if (stm instanceof FunctionStatement) ((FunctionStatement)stm).compile(target, scope, pollute, name, bp); + else stm.compile(target, scope, pollute, bp); + } } diff --git a/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java b/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java index b817a89..284336f 100644 --- a/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java +++ b/src/me/topchetoeu/jscript/engine/frame/CodeFrame.java @@ -198,6 +198,7 @@ public class CodeFrame { if (instr == null) returnValue = null; else { + // System.out.println(instr + "@" + instr.location); ctx.engine.onInstruction(ctx, this, instr, Runners.NO_RETURN, null, false); if (instr.location != null) prevLoc = instr.location; diff --git a/src/me/topchetoeu/jscript/parsing/Parsing.java b/src/me/topchetoeu/jscript/parsing/Parsing.java index dcb43f2..e0fb24e 100644 --- a/src/me/topchetoeu/jscript/parsing/Parsing.java +++ b/src/me/topchetoeu/jscript/parsing/Parsing.java @@ -880,7 +880,7 @@ public class Parsing { var callRes = parseCall(filename, tokens, i + n, valRes.result, 0); n += callRes.n; if (callRes.isError()) return callRes.transform(); - else if (callRes.isFailed()) return ParseRes.res(new CallStatement(loc, false, valRes.result), n); + else if (callRes.isFailed()) return ParseRes.res(new CallStatement(loc, true, valRes.result), n); var call = (CallStatement)callRes.result; return ParseRes.res(new CallStatement(loc, true, call.func, call.args), n); diff --git a/test.txt b/test.txt deleted file mode 100644 index 858066e..0000000 --- a/test.txt +++ /dev/null @@ -1,80 +0,0 @@ -const { spawn } = require('child_process'); -const fs = require('fs/promises'); -const pt = require('path'); -const { argv, exit } = require('process'); - -const conf = { - name: "java-jscript", - author: "TopchetoEU", - javahome: "", - version: argv[3] -}; - -if (conf.version.startsWith('refs/tags/')) conf.version = conf.version.substring(10); -if (conf.version.startsWith('v')) conf.version = conf.version.substring(1); - -async function* find(src, dst, wildcard) { - const stat = await fs.stat(src); - - if (stat.isDirectory()) { - for (const el of await fs.readdir(src)) { - for await (const res of find(pt.join(src, el), dst ? pt.join(dst, el) : undefined, wildcard)) yield res; - } - } - else if (stat.isFile() && wildcard(src)) yield dst ? { src, dst } : src; -} -async function copy(src, dst, wildcard) { - const promises = []; - - for await (const el of find(src, dst, wildcard)) { - promises.push((async () => { - await fs.mkdir(pt.dirname(el.dst), { recursive: true }); - await fs.copyFile(el.src, el.dst); - })()); - } - - await Promise.all(promises); -} - -function run(cmd, ...args) { - return new Promise((res, rej) => { - const proc = spawn(cmd, args, { stdio: 'inherit' }); - proc.once('exit', code => { - if (code === 0) res(code); - else rej(new Error(`Process ${cmd} exited with code ${code}.`)); - }); - }) -} - -async function compileJava() { - try { - await fs.writeFile('Metadata.java', (await fs.readFile('src/me/topchetoeu/jscript/Metadata.java')).toString() - .replace('${VERSION}', conf.version) - .replace('${NAME}', conf.name) - .replace('${AUTHOR}', conf.author) - ); - const args = ['--release', '11', ]; - if (argv[2] === 'debug') args.push('-g'); - args.push('-d', 'dst/classes', 'Metadata.java'); - - for await (const path of find('src', undefined, v => v.endsWith('.java') && !v.endsWith('Metadata.java'))) args.push(path); - await run(conf.javahome + 'javac', ...args); - } - finally { - await fs.rm('Metadata.java'); - } -} - -(async () => { - try { - try { await fs.rm('dst', { recursive: true }); } catch {} - await copy('src', 'dst/classes', v => !v.endsWith('.java')); - await compileJava(); - await run('jar', '-c', '-f', 'dst/jscript.jar', '-e', 'me.topchetoeu.jscript.Main', '-C', 'dst/classes', '.'); - } - catch (e) { - if (argv[2] === 'debug') throw e; - console.log(e.toString()); - exit(-1); - } -})();