From fbbd26bf7d41dde37873d0a21dbbeca5216c150f Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 14 Sep 2024 22:08:33 +0300 Subject: [PATCH] fix: remove unneeded comments --- .../jscript/common/Instruction.java | 7 +- .../jscript/compilation/FunctionNode.java | 23 ------- .../compilation/control/SwitchNode.java | 2 - .../compilation/patterns/AssignPattern.java | 2 - .../compilation/values/ObjectNode.java | 5 -- .../compilation/values/VariableNode.java | 12 ---- .../values/operations/IndexNode.java | 3 - .../values/operations/OperationNode.java | 5 -- .../me/topchetoeu/jscript/runtime/Frame.java | 67 ------------------- .../jscript/runtime/InstructionRunner.java | 9 --- .../jscript/runtime/SimpleRepl.java | 21 ------ .../runtime/values/objects/ArrayValue.java | 1 - .../values/objects/ByteBufferValue.java | 35 +++------- 13 files changed, 11 insertions(+), 181 deletions(-) diff --git a/src/main/java/me/topchetoeu/jscript/common/Instruction.java b/src/main/java/me/topchetoeu/jscript/common/Instruction.java index 2956c7c..b53cd49 100644 --- a/src/main/java/me/topchetoeu/jscript/common/Instruction.java +++ b/src/main/java/me/topchetoeu/jscript/common/Instruction.java @@ -65,8 +65,7 @@ public class Instruction { GLOB_DEF(0x62), STACK_ALLOC(0x70), - STACK_REALLOC(0x71), - STACK_FREE(0x72); + STACK_REALLOC(0x71); private static final HashMap types = new HashMap<>(); public final int numeric; @@ -470,10 +469,6 @@ public class Instruction { public static Instruction stackRealloc(int start, int n) { return new Instruction(Type.STACK_REALLOC, start, start + n); } - @Deprecated - public static Instruction stackFree(int n) { - return new Instruction(Type.STACK_FREE, n); - } @Override public String toString() { var res = type.toString(); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java b/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java index b8b2bed..e37a9cd 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/FunctionNode.java @@ -38,35 +38,12 @@ public abstract class FunctionNode extends Node { for (var param : params.params) { target.add(Instruction.loadMember(i++)); param.destruct(target, DeclarationType.VAR, true); - // if (scope.has(param.name, false)) throw new SyntaxException(param.loc, "Duplicate parameter name not allowed"); - // if (!JavaScript.checkVarName(param.name)) { - // throw new SyntaxException(param.loc, String.format("Unexpected identifier '%s'", param.name)); - // } - // var varI = scope.define(new Variable(param.name, false), param.loc); - - // if (param.node != null) { - // var end = new DeferredIntSupplier(); - - // target.add(Instruction.dup()); - // target.add(Instruction.pushUndefined()); - // target.add(Instruction.operation(Operation.EQUALS)); - // target.add(Instruction.jmpIfNot(end)); - // target.add(Instruction.discard()); - // param.node.compile(target, true); - - // end.set(target.size()); - // } - - // target.add(_i -> varI.index().toSet(false)); } } if (params.rest != null) { target.add(Instruction.loadRestArgs(params.params.size())); params.rest.destruct(target, DeclarationType.VAR, true); - // if (scope.has(params.restName, false)) throw new SyntaxException(params.restLocation, "Duplicate parameter name not allowed"); - // var restVar = scope.define(new Variable(params.restName, false), params.restLocation); - // target.add(_i -> restVar.index().toSet(false)); } if (selfName != null && !scope.has(name, false)) { diff --git a/src/main/java/me/topchetoeu/jscript/compilation/control/SwitchNode.java b/src/main/java/me/topchetoeu/jscript/compilation/control/SwitchNode.java index c082b63..e8a729b 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/SwitchNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/SwitchNode.java @@ -167,8 +167,6 @@ public class SwitchNode extends Node { SwitchNode::parseSwitchCase ); - // Parsing::parseStatement - if (caseRes.isSuccess()) { n += caseRes.n; diff --git a/src/main/java/me/topchetoeu/jscript/compilation/patterns/AssignPattern.java b/src/main/java/me/topchetoeu/jscript/compilation/patterns/AssignPattern.java index 60bd776..c4d1c11 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/patterns/AssignPattern.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/patterns/AssignPattern.java @@ -27,8 +27,6 @@ public class AssignPattern implements Pattern { throw new SyntaxException(loc(), "Expected an assignment value for destructor declaration"); } @Override public void destruct(CompileResult target, DeclarationType decl, boolean shouldDeclare) { - // if (assignable instanceof AssignPattern other) throw new SyntaxException(other.loc(), "Unexpected destruction target"); - target.add(Instruction.dup()); target.add(Instruction.pushUndefined()); target.add(Instruction.operation(Operation.EQUALS)); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/ObjectNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/ObjectNode.java index 35ddbab..02b770f 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/ObjectNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/ObjectNode.java @@ -286,11 +286,6 @@ public class ObjectNode extends Node implements AssignTargetLike { // target.scope.end(); // } - // @Override public void destruct(CompileResult target, DeclarationType decl) { - // if (getters.size() > 0) throw new SyntaxException(getters.values().iterator().next().loc(), "Unexpected getter in destructor"); - // if (setters.size() > 0) throw new SyntaxException(setters.values().iterator().next().loc(), "Unexpected setter in destructor"); - // } - @Override public void compile(CompileResult target, boolean pollute) { target.add(Instruction.loadObj()); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java index 5c519ee..17f8bcf 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/VariableNode.java @@ -21,22 +21,10 @@ public class VariableNode extends Node implements Pattern, ChangeTarget { public String assignName() { return name; } - // @Override public void compileBeforeAssign(CompileResult target, boolean operator) { - // if (operator) { - // target.add(VariableNode.toGet(target, loc(), name)); - // } - // } - // @Override public void compileAfterAssign(CompileResult target, boolean operator, boolean pollute) { - // target.add(VariableNode.toSet(target, loc(), name, pollute, false)); - // } - @Override public void beforeChange(CompileResult target) { target.add(VariableNode.toGet(target, loc(), name)); } - // @Override public void destructArg(CompileResult target) { - // target.add(_i -> target.scope.define(new Variable(name, false), loc()).index().toSet(false)); - // } @Override public void destructDeclResolve(CompileResult target) { target.scope.define(new Variable(name, false), loc()); } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java index 6299813..826b8cd 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/IndexNode.java @@ -53,9 +53,6 @@ public class IndexNode extends Node implements ChangeTarget { indexStore(target, index, pollute); } - // @Override public Node toAssign(Node val, Operation operation) { - // return new IndexAssignNode(loc(), object, index, val, operation); - // } public void compile(CompileResult target, boolean dupObj, boolean pollute) { object.compile(target, true); if (dupObj) target.add(Instruction.dup()); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/OperationNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/OperationNode.java index 53e4422..5189330 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/OperationNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/OperationNode.java @@ -226,11 +226,6 @@ public class OperationNode extends Node { var res = factory.construct(src, i + n, prev); return res.addN(n); - // var res = Parsing.parseValue(src, i + n, prec + 1); - // if (!res.isSuccess()) return res.chainError(src.loc(i + n), String.format("Expected a value after the '%s' operator.", token)); - // n += res.n; - - // return ParseRes.res(new OperationStatement(loc, factories.get(token), prev, res.result), n); } return ParseRes.failed(); diff --git a/src/main/java/me/topchetoeu/jscript/runtime/Frame.java b/src/main/java/me/topchetoeu/jscript/runtime/Frame.java index c6fb662..4fc615a 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/Frame.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/Frame.java @@ -222,13 +222,10 @@ public final class Frame { if (newCtx != tryCtx) { switch (newCtx.state) { case CATCH: - // TODO: may cause problems - // if (tryCtx.state != TryState.CATCH) locals.add(new Value[] { error.value }); codePtr = tryCtx.catchStart; stackPtr = tryCtx.restoreStackPtr; break; case FINALLY: - // if (tryCtx.state == TryState.CATCH) locals.remove(locals.size() - 1); codePtr = tryCtx.finallyStart; stackPtr = tryCtx.restoreStackPtr; default: @@ -244,7 +241,6 @@ public final class Frame { } else { popTryFlag = false; - // if (tryCtx.state == TryState.CATCH) locals.remove(locals.size() - 1); if (tryCtx.state != TryState.FINALLY && tryCtx.hasFinally()) { codePtr = tryCtx.finallyStart; @@ -339,44 +335,6 @@ public final class Frame { DebugContext.get(env).onFramePop(env, this); } - /** - * Gets an object proxy of the local locals - */ - public ObjectValue getLocalScope() { - throw new RuntimeException("Not supported"); - - // var names = new String[locals.locals.length]; - // var map = DebugContext.get(env).getMapOrEmpty(function); - - // for (int i = 0; i < locals.locals.length; i++) { - // var name = "local_" + (i - 2); - - // if (i == 0) name = "this"; - // else if (i == 1) name = "arguments"; - // else if (i < map.localNames.length) name = map.localNames[i]; - - // names[i] = name; - // } - - // return new ScopeValue(locals, names); - } - /** - * Gets an object proxy of the capture locals - */ - public ObjectValue getCaptureScope() { - throw new RuntimeException("Not supported"); - - // var names = new String[captures.length]; - // var map = DebugContext.get(env).getMapOrEmpty(function); - - // for (int i = 0; i < captures.length; i++) { - // var name = "capture_" + (i - 2); - // if (i < map.captureNames.length) name = map.captureNames[i]; - // names[i] = name; - // } - - // return new ScopeValue(captures, names); - } /** * Gets an array proxy of the local locals */ @@ -389,31 +347,6 @@ public final class Frame { @Override public int size() { return stackPtr; } @Override public boolean setSize(int val) { return false; } - - // @Override public Member getOwnMember(Environment env, KeyCache key) { - // var res = super.getOwnMember(env, key); - // if (res != null) return res; - - // var num = key.toNumber(env); - // var i = key.toInt(env); - - // if (num != i || i < 0 || i >= stackPtr) return null; - // else return new FieldMember(this, false, true, true) { - // @Override public Value get(Environment env, Value self) { return stack[i]; } - // @Override public boolean set(Environment env, Value val, Value self) { - // stack[i] = val; - // return true; - // } - // }; - // } - // @Override public Set getOwnMembers(Environment env, boolean onlyEnumerable) { - // var res = new LinkedHashSet(); - // res.addAll(super.getOwnMembers(env, onlyEnumerable)); - - // for (var i = 0; i < stackPtr; i++) res.add(i + ""); - - // return res; - // } }; } diff --git a/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java b/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java index de79882..1fa9ecf 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/InstructionRunner.java @@ -534,14 +534,6 @@ public class InstructionRunner { frame.codePtr++; return null; } - private static Value execStackFree(Environment env, Instruction instr, Frame frame) { - // int n = instr.get(0); - - // TODO: Remove if safe to do so - - frame.codePtr++; - return null; - } public static Value exec(Environment env, Instruction instr, Frame frame) { switch (instr.type) { @@ -602,7 +594,6 @@ public class InstructionRunner { case STACK_ALLOC: return execStackAlloc(env, instr, frame); case STACK_REALLOC: return execStackRealloc(env, instr, frame); - case STACK_FREE: return execStackFree(env, instr, frame); default: throw EngineException.ofSyntax("Invalid instruction " + instr.type.name() + "."); } diff --git a/src/main/java/me/topchetoeu/jscript/runtime/SimpleRepl.java b/src/main/java/me/topchetoeu/jscript/runtime/SimpleRepl.java index 8c4adcd..6dc1b3a 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/SimpleRepl.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/SimpleRepl.java @@ -345,29 +345,8 @@ public class SimpleRepl { } private static void initEnv() { - // glob.define(null, false, new NativeFunction("go", args -> { - // try { - // var f = Path.of("do.js"); - // var func = Compiler.compile(args.env, new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f))); - // return func.call(args.env); - // } - // catch (IOException e) { - // throw new EngineException("Couldn't open do.js"); - // } - // })); - - // var fs = new RootFilesystem(PermissionsProvider.get(environment)); - // fs.protocols.put("temp", new MemoryFilesystem(Mode.READ_WRITE)); - // fs.protocols.put("file", new PhysicalFilesystem(".")); - // fs.protocols.put("std", new STDFilesystem(System.in, System.out, System.err)); - - // environment.add(PermissionsProvider.KEY, PermissionsManager.ALL_PERMS); - // environment.add(Filesystem.KEY, fs); - // environment.add(ModuleRepo.KEY, ModuleRepo.ofFilesystem(fs)); - // environment.add(Compiler.KEY, new JSCompiler(environment)); environment.add(EventLoop.KEY, engine); environment.add(DebugContext.KEY, new DebugContext()); - // environment.add(EventLoop.KEY, engine); environment.add(Compiler.KEY, Compiler.DEFAULT); var glob = Value.global(environment); diff --git a/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ArrayValue.java b/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ArrayValue.java index 32be51e..e855c4e 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ArrayValue.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ArrayValue.java @@ -9,7 +9,6 @@ import me.topchetoeu.jscript.common.environment.Environment; import me.topchetoeu.jscript.runtime.values.Value; import me.topchetoeu.jscript.runtime.values.primitives.VoidValue; -// TODO: Make methods generic public class ArrayValue extends ArrayLikeValue implements Iterable { private Value[] values; private int size; diff --git a/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ByteBufferValue.java b/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ByteBufferValue.java index 3ce3b90..aac4d1e 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ByteBufferValue.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/values/objects/ByteBufferValue.java @@ -1,26 +1,15 @@ package me.topchetoeu.jscript.runtime.values.objects; -import java.util.Comparator; +import java.util.Arrays; import java.util.Iterator; import me.topchetoeu.jscript.common.environment.Environment; import me.topchetoeu.jscript.runtime.values.Value; import me.topchetoeu.jscript.runtime.values.primitives.numbers.NumberValue; -// TODO: Make methods generic public class ByteBufferValue extends ArrayLikeValue implements Iterable { public final byte[] values; - // private Value[] alloc(int index) { - // index++; - // if (index < values.length) return values; - // if (index < values.length * 2) index = values.length * 2; - - // var arr = new Value[index]; - // System.arraycopy(values, 0, arr, 0, values.length); - // return values = arr; - // } - public int size() { return values.length; } public boolean setSize(int val) { return false; } @@ -54,22 +43,18 @@ public class ByteBufferValue extends ArrayLikeValue implements Iterable { System.arraycopy(values, srcI, values, dstI, n); } - public void sort(Comparator comparator) { - throw new RuntimeException("not supported"); - // Arrays.sort(values, 0, values.length, (a, b) -> { - // var _a = 0; - // var _b = 0; + public void sort() { + var buckets = new int[256]; - // if (a == null) _a = 2; - // if (a instanceof VoidValue) _a = 1; + for (var i = 0; i < values.length; i++) { + buckets[values[i] + 128]++; + } - // if (b == null) _b = 2; - // if (b instanceof VoidValue) _b = 1; + var offset = 0; - // if (_a != 0 || _b != 0) return Integer.compare(_a, _b); - - // return comparator.compare(a, b); - // }); + for (var i = 0; i < values.length; i++) { + Arrays.fill(values, offset, offset += buckets[i], (byte)(i - 128)); + } } @Override public Iterator iterator() {