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.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<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); }
}
public static String streamToString(InputStream in) {
return new String(streamToBytes(in));
}
public static InputStream resourceToStream(String name) {
return Reading.class.getResourceAsStream("/" + name);
}

View File

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

View File

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

View File

@ -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<JSONElement> {
public JSONList() {}
public JSONList(JSONElement ...els) {
super(List.of(els));
super(Arrays.asList(els));
}
public JSONList(Collection<JSONElement> els) {
super(els);

View File

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

View File

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

View File

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

View File

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

View File

@ -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<String> reserved = Set.of(
static final Set<String> 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<? extends Node> 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;

View File

@ -87,7 +87,7 @@ public final class NodeChildren implements Iterable<Node> {
}
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(
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);
}
}

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

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");
}
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) {
var n = Parsing.skipEmpty(src, i);

View File

@ -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<String, OperatorFactory> factories = Set.of(
private static final Map<String, OperatorFactory> factories = Arrays.asList(
new NormalOperatorFactory("*", 13, Operation.MULTIPLY),
new NormalOperatorFactory("/", 12, Operation.DIVIDE),
new NormalOperatorFactory("%", 12, Operation.MODULO),

View File

@ -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<Frame> getStackFrames() {
if (debugger == null) return List.of();
if (debugger == null) return Arrays.asList();
return this.debugger.getStackFrame();
}

View File

@ -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<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;
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<String> getOwnMembers(Environment env, boolean onlyEnumerable) { return Set.of(); }
@Override public Set<SymbolValue> getOwnSymbolMembers(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 new HashSet<>(); }
@Override public State getState() { return State.FROZEN; }