refactor: rewrite some code for java 8 compatibility

This commit is contained in:
TopchetoEU 2024-09-14 18:45:20 +03:00
parent b5b7781136
commit e4166fe450
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
18 changed files with 102 additions and 36 deletions

View File

@ -5,6 +5,9 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Reading { public class Reading {
private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
@ -13,12 +16,66 @@ public class Reading {
return reader.readLine(); return reader.readLine();
} }
public static String streamToString(InputStream in) { public static byte[] streamToBytes(InputStream in) {
try { try {
return new String(in.readAllBytes()); List<byte[]> 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); } catch (IOException e) { throw new UncheckedIOException(e); }
} }
public static String streamToString(InputStream in) {
return new String(streamToBytes(in));
}
public static InputStream resourceToStream(String name) { public static InputStream resourceToStream(String name) {
return Reading.class.getResourceAsStream("/" + name); return Reading.class.getResourceAsStream("/" + name);
} }

View File

@ -35,7 +35,7 @@ public class Environment {
} }
if (!forceClone) { 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 child;
if (parent != null && child == null) return parent; if (parent != null && child == null) return parent;
} }

View File

@ -1,7 +1,7 @@
package me.topchetoeu.jscript.common.json; package me.topchetoeu.jscript.common.json;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Map; import java.util.HashMap;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import me.topchetoeu.jscript.common.parsing.Filename; import me.topchetoeu.jscript.common.parsing.Filename;
@ -49,7 +49,7 @@ public class JSON {
var values = new JSONMap(); 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) { while (true) {
var name = parseString(src, i + n); var name = parseString(src, i + n);
if (!name.isSuccess()) return name.chainError(src.loc(i + n), "Expected an index"); if (!name.isSuccess()) return name.chainError(src.loc(i + n), "Expected an index");

View File

@ -1,14 +1,14 @@
package me.topchetoeu.jscript.common.json; package me.topchetoeu.jscript.common.json;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
public class JSONList extends ArrayList<JSONElement> { public class JSONList extends ArrayList<JSONElement> {
public JSONList() {} public JSONList() {}
public JSONList(JSONElement ...els) { public JSONList(JSONElement ...els) {
super(List.of(els)); super(Arrays.asList(els));
} }
public JSONList(Collection<JSONElement> els) { public JSONList(Collection<JSONElement> els) {
super(els); super(els);

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.common.mapping; package me.topchetoeu.jscript.common.mapping;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -111,9 +112,9 @@ public class FunctionMap {
return res; return res;
} }
public List<Location> breakpoints(Location start, Location end) { public List<Location> 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<Location> set = bpLocs.get(start.filename()); NavigableSet<Location> 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 (start != null) set = set.tailSet(start, true);
if (end != null) set = set.headSet(end, true); if (end != null) set = set.headSet(end, true);
@ -153,7 +154,7 @@ public class FunctionMap {
// } // }
public FunctionMap clone() { 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.pcToLoc.putAll(this.pcToLoc);
res.bps.putAll(bps); res.bps.putAll(bps);
res.bpLocs.putAll(bpLocs); res.bpLocs.putAll(bpLocs);

View File

@ -1,7 +1,6 @@
package me.topchetoeu.jscript.common.parsing; package me.topchetoeu.jscript.common.parsing;
import java.io.File; import java.io.File;
import java.nio.file.Path;
public class Filename { public class Filename {
public final String protocol; 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()); if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim());
else return new Filename("file", val.trim()); else return new Filename("file", val.trim());
} }
public static Path normalize(String path) { // public static Path normalize(String path) {
return Path.of(Path.of("/" + path.trim().replace("\\", "/")).normalize().toString().substring(1)); // // 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) { public static Filename fromFile(File file) {
return new Filename("file", file.getAbsolutePath()); return new Filename("file", file.getAbsolutePath());
} }

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.compilation; package me.topchetoeu.jscript.compilation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import me.topchetoeu.jscript.common.Instruction; import me.topchetoeu.jscript.common.Instruction;
@ -91,10 +92,10 @@ public class CompoundNode extends Node {
if (prev instanceof CompoundNode comp) { if (prev instanceof CompoundNode comp) {
var children = new ArrayList<Node>(); var children = new ArrayList<Node>();
children.addAll(List.of(comp.statements)); children.addAll(Arrays.asList(comp.statements));
children.add(curr.result); 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); 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); 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);
} }
} }

View File

@ -1,6 +1,6 @@
package me.topchetoeu.jscript.compilation; 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;
import me.topchetoeu.jscript.common.Instruction.BreakpointType; import me.topchetoeu.jscript.common.Instruction.BreakpointType;
@ -48,7 +48,7 @@ public class FunctionArrowNode extends FunctionNode {
n += singleParam.n; n += singleParam.n;
n += Parsing.skipEmpty(src, i + 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(); if (!src.is(i + n, "=>")) return ParseRes.failed();

View File

@ -1,7 +1,8 @@
package me.topchetoeu.jscript.compilation; package me.topchetoeu.jscript.compilation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.Arrays;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import me.topchetoeu.jscript.common.environment.Environment; import me.topchetoeu.jscript.common.environment.Environment;
@ -57,13 +58,13 @@ public final class JavaScript {
} }
} }
static final Set<String> reserved = Set.of( static final Set<String> reserved = new HashSet<>(Arrays.asList(
"true", "false", "void", "null", "this", "if", "else", "try", "catch", "true", "false", "void", "null", "this", "if", "else", "try", "catch",
"finally", "for", "do", "while", "switch", "case", "default", "new", "finally", "for", "do", "while", "switch", "case", "default", "new",
"function", "var", "return", "throw", "typeof", "delete", "break", "function", "var", "return", "throw", "typeof", "delete", "break",
"continue", "debugger", "implements", "interface", "package", "private", "continue", "debugger", "implements", "interface", "package", "private",
"protected", "public", "static", "arguments" "protected", "public", "static", "arguments"
); ));
public static ParseRes<? extends Node> parseParens(Source src, int i) { public static ParseRes<? extends Node> parseParens(Source src, int i) {
int n = 0; int n = 0;
@ -252,7 +253,7 @@ public final class JavaScript {
list.add(res.result); list.add(res.result);
} }
return list.toArray(Node[]::new); return list.toArray(new Node[0]);
} }
public static boolean checkVarName(String name) { public static boolean checkVarName(String name) {
@ -260,7 +261,7 @@ public final class JavaScript {
} }
public static CompileResult compile(Environment env, Node ...statements) { 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); var res = func.compileBody(env, new FunctionScope(true), true, null, null);
res.buildTask.run(); res.buildTask.run();
return res; return res;

View File

@ -87,7 +87,7 @@ public final class NodeChildren implements Iterable<Node> {
} }
public final NodeChildren build() { public final NodeChildren build() {
return new NodeChildren(slots.toArray(Slot[]::new)); return new NodeChildren(slots.toArray(new Slot[0]));
} }
} }
} }

View File

@ -189,8 +189,8 @@ public class SwitchNode extends Node {
return ParseRes.res(new SwitchNode( return ParseRes.res(new SwitchNode(
loc, label.result, val.result, defaultI, loc, label.result, val.result, defaultI,
cases.toArray(SwitchCase[]::new), cases.toArray(new SwitchCase[0]),
statements.toArray(Node[]::new) statements.toArray(new Node[0])
), n); ), n);
} }
} }

View File

@ -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);
} }
} }

View File

@ -1,5 +1,6 @@
package me.topchetoeu.jscript.compilation.values; package me.topchetoeu.jscript.compilation.values;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; 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) { 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.key = key;
this.argument = argument; this.argument = argument;
} }

View File

@ -159,7 +159,7 @@ public class CallNode extends Node {
else return ParseRes.error(src.loc(i + n), "Expected an expression or a closing paren"); 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<CallNode> parseNew(Source src, int i) { public static ParseRes<CallNode> parseNew(Source src, int i) {
var n = Parsing.skipEmpty(src, i); var n = Parsing.skipEmpty(src, i);

View File

@ -1,8 +1,8 @@
package me.topchetoeu.jscript.compilation.values.operations; package me.topchetoeu.jscript.compilation.values.operations;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import me.topchetoeu.jscript.common.Instruction; import me.topchetoeu.jscript.common.Instruction;
@ -120,7 +120,7 @@ public class OperationNode extends Node {
this.args = args; this.args = args;
} }
private static final Map<String, OperatorFactory> factories = Set.of( private static final Map<String, OperatorFactory> factories = Arrays.asList(
new NormalOperatorFactory("*", 13, Operation.MULTIPLY), new NormalOperatorFactory("*", 13, Operation.MULTIPLY),
new NormalOperatorFactory("/", 12, Operation.DIVIDE), new NormalOperatorFactory("/", 12, Operation.DIVIDE),
new NormalOperatorFactory("%", 12, Operation.MODULO), new NormalOperatorFactory("%", 12, Operation.MODULO),

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.runtime.debug; package me.topchetoeu.jscript.runtime.debug;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -71,7 +72,7 @@ public class DebugContext {
return getMapOrEmpty(((CodeFunction)func).body); return getMapOrEmpty(((CodeFunction)func).body);
} }
public List<Frame> getStackFrames() { public List<Frame> getStackFrames() {
if (debugger == null) return List.of(); if (debugger == null) return Arrays.asList();
return this.debugger.getStackFrame(); return this.debugger.getStackFrame();
} }

View File

@ -1,5 +1,6 @@
package me.topchetoeu.jscript.runtime.debug; package me.topchetoeu.jscript.runtime.debug;
import java.util.Arrays;
import java.util.List; import java.util.List;
import me.topchetoeu.jscript.common.FunctionBody; import me.topchetoeu.jscript.common.FunctionBody;
@ -73,7 +74,7 @@ public interface DebugHandler {
} }
@Override public void onSourceLoad(Filename filename, String source) { } @Override public void onSourceLoad(Filename filename, String source) { }
@Override public void onFunctionLoad(FunctionBody body, FunctionMap map) { } @Override public void onFunctionLoad(FunctionBody body, FunctionMap map) { }
@Override public List<Frame> getStackFrame() { return List.of(); } @Override public List<Frame> getStackFrame() { return Arrays.asList(); }
}; };
} }
} }

View File

@ -1,5 +1,6 @@
package me.topchetoeu.jscript.runtime.values.primitives; package me.topchetoeu.jscript.runtime.values.primitives;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import me.topchetoeu.jscript.common.environment.Environment; 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 final boolean setPrototype(Environment env, ObjectValue val) { return false; }
@Override public Member getOwnMember(Environment env, KeyCache key) { return null; } @Override public Member getOwnMember(Environment env, KeyCache key) { return null; }
@Override public Set<String> getOwnMembers(Environment env, boolean onlyEnumerable) { return Set.of(); } @Override public Set<String> getOwnMembers(Environment env, boolean onlyEnumerable) { return new HashSet<>(); }
@Override public Set<SymbolValue> getOwnSymbolMembers(Environment env, boolean onlyEnumerable) { return Set.of(); } @Override public Set<SymbolValue> getOwnSymbolMembers(Environment env, boolean onlyEnumerable) { return new HashSet<>(); }
@Override public State getState() { return State.FROZEN; } @Override public State getState() { return State.FROZEN; }