From e4166fe4502d3314d1ce2070aaa8b8e9a2f904ea Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 14 Sep 2024 18:45:20 +0300 Subject: [PATCH] refactor: rewrite some code for java 8 compatibility --- .../me/topchetoeu/jscript/common/Reading.java | 61 ++++++++++++++++++- .../common/environment/Environment.java | 2 +- .../topchetoeu/jscript/common/json/JSON.java | 4 +- .../jscript/common/json/JSONList.java | 4 +- .../jscript/common/mapping/FunctionMap.java | 7 ++- .../jscript/common/parsing/Filename.java | 10 +-- .../jscript/compilation/CompoundNode.java | 7 ++- .../compilation/FunctionArrowNode.java | 4 +- .../jscript/compilation/JavaScript.java | 11 ++-- .../jscript/compilation/NodeChildren.java | 2 +- .../compilation/control/SwitchNode.java | 4 +- .../jscript/compilation/values/ArrayNode.java | 2 +- .../compilation/values/ObjectNode.java | 3 +- .../values/operations/CallNode.java | 2 +- .../values/operations/OperationNode.java | 4 +- .../jscript/runtime/debug/DebugContext.java | 3 +- .../jscript/runtime/debug/DebugHandler.java | 3 +- .../values/primitives/PrimitiveValue.java | 5 +- 18 files changed, 102 insertions(+), 36 deletions(-) diff --git a/src/main/java/me/topchetoeu/jscript/common/Reading.java b/src/main/java/me/topchetoeu/jscript/common/Reading.java index 30c0d5e..6c6e6e8 100644 --- a/src/main/java/me/topchetoeu/jscript/common/Reading.java +++ b/src/main/java/me/topchetoeu/jscript/common/Reading.java @@ -5,6 +5,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UncheckedIOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class Reading { private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); @@ -13,12 +16,66 @@ public class Reading { return reader.readLine(); } - public static String streamToString(InputStream in) { + public static byte[] streamToBytes(InputStream in) { try { - return new String(in.readAllBytes()); + List bufs = null; + byte[] result = null; + int total = 0; + int n; + + do { + var buf = new byte[8192]; + int nread = 0; + + // read to EOF which may read more or less than buffer size + while ((n = in.read(buf, nread, buf.length - nread)) > 0) { + nread += n; + } + + if (nread > 0) { + if (Integer.MAX_VALUE - 8 - total < nread) throw new OutOfMemoryError("Required array size too large"); + if (nread < buf.length) buf = Arrays.copyOfRange(buf, 0, nread); + total += nread; + + if (result == null) result = buf; + else { + if (bufs == null) { + bufs = new ArrayList<>(); + bufs.add(result); + } + + bufs.add(buf); + } + } + // if the last call to read returned -1 or the number of bytes + // requested have been read then break + } while (n >= 0); + + if (bufs == null) { + if (result == null) return new byte[0]; + return result.length == total ? result : Arrays.copyOf(result, total); + } + + result = new byte[total]; + + int offset = 0; + int remaining = total; + + for (byte[] b : bufs) { + int count = Math.min(b.length, remaining); + System.arraycopy(b, 0, result, offset, count); + offset += count; + remaining -= count; + } + + return result; } catch (IOException e) { throw new UncheckedIOException(e); } } + public static String streamToString(InputStream in) { + return new String(streamToBytes(in)); + } + public static InputStream resourceToStream(String name) { return Reading.class.getResourceAsStream("/" + name); } diff --git a/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java b/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java index 7efce20..0a33cec 100644 --- a/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java +++ b/src/main/java/me/topchetoeu/jscript/common/environment/Environment.java @@ -35,7 +35,7 @@ public class Environment { } if (!forceClone) { - if (parent == null && child == null) return Set.of(); + if (parent == null && child == null) return new HashSet<>(); if (parent == null && child != null) return child; if (parent != null && child == null) return parent; } diff --git a/src/main/java/me/topchetoeu/jscript/common/json/JSON.java b/src/main/java/me/topchetoeu/jscript/common/json/JSON.java index e93f3f3..49aeb29 100644 --- a/src/main/java/me/topchetoeu/jscript/common/json/JSON.java +++ b/src/main/java/me/topchetoeu/jscript/common/json/JSON.java @@ -1,7 +1,7 @@ package me.topchetoeu.jscript.common.json; import java.math.BigDecimal; -import java.util.Map; +import java.util.HashMap; import java.util.stream.Collectors; import me.topchetoeu.jscript.common.parsing.Filename; @@ -49,7 +49,7 @@ public class JSON { var values = new JSONMap(); - if (src.is(i + n, "}")) return ParseRes.res(new JSONMap(Map.of()), n + 1); + if (src.is(i + n, "}")) return ParseRes.res(new JSONMap(new HashMap<>()), n + 1); while (true) { var name = parseString(src, i + n); if (!name.isSuccess()) return name.chainError(src.loc(i + n), "Expected an index"); diff --git a/src/main/java/me/topchetoeu/jscript/common/json/JSONList.java b/src/main/java/me/topchetoeu/jscript/common/json/JSONList.java index e2b4e63..e18ba73 100644 --- a/src/main/java/me/topchetoeu/jscript/common/json/JSONList.java +++ b/src/main/java/me/topchetoeu/jscript/common/json/JSONList.java @@ -1,14 +1,14 @@ package me.topchetoeu.jscript.common.json; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; -import java.util.List; import java.util.Map; public class JSONList extends ArrayList { public JSONList() {} public JSONList(JSONElement ...els) { - super(List.of(els)); + super(Arrays.asList(els)); } public JSONList(Collection els) { super(els); diff --git a/src/main/java/me/topchetoeu/jscript/common/mapping/FunctionMap.java b/src/main/java/me/topchetoeu/jscript/common/mapping/FunctionMap.java index fea96ff..5b87d81 100644 --- a/src/main/java/me/topchetoeu/jscript/common/mapping/FunctionMap.java +++ b/src/main/java/me/topchetoeu/jscript/common/mapping/FunctionMap.java @@ -1,6 +1,7 @@ package me.topchetoeu.jscript.common.mapping; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -111,9 +112,9 @@ public class FunctionMap { return res; } public List breakpoints(Location start, Location end) { - if (!Objects.equals(start.filename(), end.filename())) return List.of(); + if (!Objects.equals(start.filename(), end.filename())) return Arrays.asList(); NavigableSet set = bpLocs.get(start.filename()); - if (set == null) return List.of(); + if (set == null) return Arrays.asList(); if (start != null) set = set.tailSet(start, true); if (end != null) set = set.headSet(end, true); @@ -153,7 +154,7 @@ public class FunctionMap { // } public FunctionMap clone() { - var res = new FunctionMap(Map.of(), Map.of(), localNames, captureNames); + var res = new FunctionMap(new HashMap<>(), new HashMap<>(), localNames, captureNames); res.pcToLoc.putAll(this.pcToLoc); res.bps.putAll(bps); res.bpLocs.putAll(bpLocs); diff --git a/src/main/java/me/topchetoeu/jscript/common/parsing/Filename.java b/src/main/java/me/topchetoeu/jscript/common/parsing/Filename.java index 8e57751..890b8e9 100644 --- a/src/main/java/me/topchetoeu/jscript/common/parsing/Filename.java +++ b/src/main/java/me/topchetoeu/jscript/common/parsing/Filename.java @@ -1,7 +1,6 @@ package me.topchetoeu.jscript.common.parsing; import java.io.File; -import java.nio.file.Path; public class Filename { public final String protocol; @@ -48,9 +47,12 @@ public class Filename { if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim()); else return new Filename("file", val.trim()); } - public static Path normalize(String path) { - return Path.of(Path.of("/" + path.trim().replace("\\", "/")).normalize().toString().substring(1)); - } + // public static Path normalize(String path) { + // // File file = new File("/" + path.trim().replace("\\", "/")); + // // String normalizedPath = new File("/" + path.trim().replace("\\", "/")).getAbsolutePath().replaceFirst("^/", "").replace("\\", "/"); + // // return normalizedPath; + // return Path.of(Path.of("/" + path.trim().replace("\\", "/")).normalize().toString().substring(1)); + // } public static Filename fromFile(File file) { return new Filename("file", file.getAbsolutePath()); } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/CompoundNode.java b/src/main/java/me/topchetoeu/jscript/compilation/CompoundNode.java index 9d8bd93..f32edbf 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/CompoundNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/CompoundNode.java @@ -1,6 +1,7 @@ package me.topchetoeu.jscript.compilation; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import me.topchetoeu.jscript.common.Instruction; @@ -91,10 +92,10 @@ public class CompoundNode extends Node { if (prev instanceof CompoundNode comp) { var children = new ArrayList(); - children.addAll(List.of(comp.statements)); + children.addAll(Arrays.asList(comp.statements)); children.add(curr.result); - return ParseRes.res(new CompoundNode(loc, comp.hasScope, children.toArray(Node[]::new)), n); + return ParseRes.res(new CompoundNode(loc, comp.hasScope, children.toArray(new Node[0])), n); } else return ParseRes.res(new CompoundNode(loc, false, prev, curr.result), n); } @@ -126,6 +127,6 @@ public class CompoundNode extends Node { statements.add(res.result); } - return ParseRes.res(new CompoundNode(loc, true, statements.toArray(Node[]::new)).setEnd(src.loc(i + n - 1)), n); + return ParseRes.res(new CompoundNode(loc, true, statements.toArray(new Node[0])).setEnd(src.loc(i + n - 1)), n); } } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java b/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java index aa5ca8c..83cf64e 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/FunctionArrowNode.java @@ -1,6 +1,6 @@ package me.topchetoeu.jscript.compilation; -import java.util.List; +import java.util.Arrays; import me.topchetoeu.jscript.common.Instruction; import me.topchetoeu.jscript.common.Instruction.BreakpointType; @@ -48,7 +48,7 @@ public class FunctionArrowNode extends FunctionNode { n += singleParam.n; n += Parsing.skipEmpty(src, i + n); - params = new Parameters(List.of(singleParam.result)); + params = new Parameters(Arrays.asList(singleParam.result)); } if (!src.is(i + n, "=>")) return ParseRes.failed(); diff --git a/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java b/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java index cab60c3..620c3db 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/JavaScript.java @@ -1,7 +1,8 @@ package me.topchetoeu.jscript.compilation; import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; +import java.util.HashSet; import java.util.Set; import me.topchetoeu.jscript.common.environment.Environment; @@ -57,13 +58,13 @@ public final class JavaScript { } } - static final Set reserved = Set.of( + static final Set reserved = new HashSet<>(Arrays.asList( "true", "false", "void", "null", "this", "if", "else", "try", "catch", "finally", "for", "do", "while", "switch", "case", "default", "new", "function", "var", "return", "throw", "typeof", "delete", "break", "continue", "debugger", "implements", "interface", "package", "private", "protected", "public", "static", "arguments" - ); + )); public static ParseRes parseParens(Source src, int i) { int n = 0; @@ -252,7 +253,7 @@ public final class JavaScript { list.add(res.result); } - return list.toArray(Node[]::new); + return list.toArray(new Node[0]); } public static boolean checkVarName(String name) { @@ -260,7 +261,7 @@ public final class JavaScript { } public static CompileResult compile(Environment env, Node ...statements) { - var func = new FunctionValueNode(null, null, new Parameters(List.of()), new CompoundNode(null, true, statements), null); + var func = new FunctionValueNode(null, null, new Parameters(Arrays.asList()), new CompoundNode(null, true, statements), null); var res = func.compileBody(env, new FunctionScope(true), true, null, null); res.buildTask.run(); return res; diff --git a/src/main/java/me/topchetoeu/jscript/compilation/NodeChildren.java b/src/main/java/me/topchetoeu/jscript/compilation/NodeChildren.java index 920c6d0..6080fbc 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/NodeChildren.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/NodeChildren.java @@ -87,7 +87,7 @@ public final class NodeChildren implements Iterable { } public final NodeChildren build() { - return new NodeChildren(slots.toArray(Slot[]::new)); + return new NodeChildren(slots.toArray(new Slot[0])); } } } \ No newline at end of file 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 773e34b..c082b63 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/control/SwitchNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/control/SwitchNode.java @@ -189,8 +189,8 @@ public class SwitchNode extends Node { return ParseRes.res(new SwitchNode( loc, label.result, val.result, defaultI, - cases.toArray(SwitchCase[]::new), - statements.toArray(Node[]::new) + cases.toArray(new SwitchCase[0]), + statements.toArray(new Node[0]) ), n); } } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/ArrayNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/ArrayNode.java index 53ba93d..44db447 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/ArrayNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/ArrayNode.java @@ -76,6 +76,6 @@ public class ArrayNode extends Node { } } - return ParseRes.res(new ArrayNode(loc, values.toArray(Node[]::new)), n); + return ParseRes.res(new ArrayNode(loc, values.toArray(new Node[0])), n); } } 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 3fc9d3b..3a05ab8 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/ObjectNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/ObjectNode.java @@ -1,5 +1,6 @@ package me.topchetoeu.jscript.compilation.values; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; @@ -51,7 +52,7 @@ public class ObjectNode extends Node implements AssignTargetLike { } public PropertyMemberNode(Location loc, Location end, Node key, Pattern argument, CompoundNode body) { - super(loc, end, argument == null ? new Parameters(List.of()) : new Parameters(List.of(argument)), body); + super(loc, end, argument == null ? new Parameters(Arrays.asList()) : new Parameters(Arrays.asList(argument)), body); this.key = key; this.argument = argument; } diff --git a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/CallNode.java b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/CallNode.java index 5aa242b..d1414bb 100644 --- a/src/main/java/me/topchetoeu/jscript/compilation/values/operations/CallNode.java +++ b/src/main/java/me/topchetoeu/jscript/compilation/values/operations/CallNode.java @@ -159,7 +159,7 @@ public class CallNode extends Node { else return ParseRes.error(src.loc(i + n), "Expected an expression or a closing paren"); } - return ParseRes.res(new CallNode(loc, false, prev, args.toArray(Node[]::new)), n); + return ParseRes.res(new CallNode(loc, false, prev, args.toArray(new Node[0])), n); } public static ParseRes parseNew(Source src, int i) { var n = Parsing.skipEmpty(src, i); 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 fa3f77e..53e4422 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 @@ -1,8 +1,8 @@ package me.topchetoeu.jscript.compilation.values.operations; +import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; import me.topchetoeu.jscript.common.Instruction; @@ -120,7 +120,7 @@ public class OperationNode extends Node { this.args = args; } - private static final Map factories = Set.of( + private static final Map factories = Arrays.asList( new NormalOperatorFactory("*", 13, Operation.MULTIPLY), new NormalOperatorFactory("/", 12, Operation.DIVIDE), new NormalOperatorFactory("%", 12, Operation.MODULO), diff --git a/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugContext.java b/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugContext.java index d8f709b..b8d6127 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugContext.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugContext.java @@ -1,6 +1,7 @@ package me.topchetoeu.jscript.runtime.debug; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.WeakHashMap; @@ -71,7 +72,7 @@ public class DebugContext { return getMapOrEmpty(((CodeFunction)func).body); } public List getStackFrames() { - if (debugger == null) return List.of(); + if (debugger == null) return Arrays.asList(); return this.debugger.getStackFrame(); } diff --git a/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugHandler.java b/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugHandler.java index 2d67c6f..ea325ca 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugHandler.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/debug/DebugHandler.java @@ -1,5 +1,6 @@ package me.topchetoeu.jscript.runtime.debug; +import java.util.Arrays; import java.util.List; import me.topchetoeu.jscript.common.FunctionBody; @@ -73,7 +74,7 @@ public interface DebugHandler { } @Override public void onSourceLoad(Filename filename, String source) { } @Override public void onFunctionLoad(FunctionBody body, FunctionMap map) { } - @Override public List getStackFrame() { return List.of(); } + @Override public List getStackFrame() { return Arrays.asList(); } }; } } diff --git a/src/main/java/me/topchetoeu/jscript/runtime/values/primitives/PrimitiveValue.java b/src/main/java/me/topchetoeu/jscript/runtime/values/primitives/PrimitiveValue.java index 5709b3d..919d99d 100644 --- a/src/main/java/me/topchetoeu/jscript/runtime/values/primitives/PrimitiveValue.java +++ b/src/main/java/me/topchetoeu/jscript/runtime/values/primitives/PrimitiveValue.java @@ -1,5 +1,6 @@ package me.topchetoeu.jscript.runtime.values.primitives; +import java.util.HashSet; import java.util.Set; import me.topchetoeu.jscript.common.environment.Environment; @@ -17,8 +18,8 @@ public abstract class PrimitiveValue extends Value { @Override public final boolean setPrototype(Environment env, ObjectValue val) { return false; } @Override public Member getOwnMember(Environment env, KeyCache key) { return null; } - @Override public Set getOwnMembers(Environment env, boolean onlyEnumerable) { return Set.of(); } - @Override public Set getOwnSymbolMembers(Environment env, boolean onlyEnumerable) { return Set.of(); } + @Override public Set getOwnMembers(Environment env, boolean onlyEnumerable) { return new HashSet<>(); } + @Override public Set getOwnSymbolMembers(Environment env, boolean onlyEnumerable) { return new HashSet<>(); } @Override public State getState() { return State.FROZEN; }