refactoring

This commit is contained in:
TopchetoEU 2024-09-14 21:33:33 +03:00
parent 0670ffcdd1
commit e2a8a382cc
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
21 changed files with 24 additions and 74 deletions

View File

@ -5,7 +5,6 @@ import java.util.function.IntFunction;
import java.util.function.IntSupplier;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class Instruction {
public static enum Type {

View File

@ -1,4 +1,4 @@
package me.topchetoeu.jscript.runtime.exceptions;
package me.topchetoeu.jscript.common;
import me.topchetoeu.jscript.common.parsing.Location;

View File

@ -4,11 +4,11 @@ import java.math.BigDecimal;
import java.util.HashMap;
import java.util.stream.Collectors;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Filename;
import me.topchetoeu.jscript.common.parsing.ParseRes;
import me.topchetoeu.jscript.common.parsing.Parsing;
import me.topchetoeu.jscript.common.parsing.Source;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class JSON {
public static ParseRes<JSONElement> parseString(Source src, int i) {

View File

@ -1,7 +1,7 @@
package me.topchetoeu.jscript.common.parsing;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.compilation.values.constants.NumberNode;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class Parsing {
public static boolean isDigit(Character c) {

View File

@ -5,6 +5,7 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.environment.Environment;
import me.topchetoeu.jscript.common.parsing.Filename;
import me.topchetoeu.jscript.common.parsing.ParseRes;
@ -42,7 +43,6 @@ import me.topchetoeu.jscript.compilation.values.operations.IndexNode;
import me.topchetoeu.jscript.compilation.values.operations.OperationNode;
import me.topchetoeu.jscript.compilation.values.operations.PostfixNode;
import me.topchetoeu.jscript.compilation.values.operations.TypeofNode;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public final class JavaScript {
public static enum DeclarationType {

View File

@ -6,10 +6,10 @@ import java.util.function.IntFunction;
import java.util.function.IntSupplier;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.environment.Environment;
import me.topchetoeu.jscript.common.environment.Key;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class LabelContext {
public static final Key<LabelContext> BREAK_CTX = Key.of();

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.compilation.control;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.common.parsing.ParseRes;
import me.topchetoeu.jscript.common.parsing.Parsing;
@ -9,7 +10,6 @@ import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.JavaScript;
import me.topchetoeu.jscript.compilation.LabelContext;
import me.topchetoeu.jscript.compilation.Node;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class BreakNode extends Node {
public final String label;

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.compilation.control;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.common.parsing.ParseRes;
import me.topchetoeu.jscript.common.parsing.Parsing;
@ -9,7 +10,6 @@ import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.JavaScript;
import me.topchetoeu.jscript.compilation.LabelContext;
import me.topchetoeu.jscript.compilation.Node;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class ContinueNode extends Node {
public final String label;

View File

@ -2,6 +2,7 @@ package me.topchetoeu.jscript.compilation.patterns;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.Operation;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.common.parsing.ParseRes;
import me.topchetoeu.jscript.common.parsing.Parsing;
@ -10,7 +11,6 @@ import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.JavaScript;
import me.topchetoeu.jscript.compilation.Node;
import me.topchetoeu.jscript.compilation.JavaScript.DeclarationType;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class AssignPattern implements Pattern {
public final Location loc;

View File

@ -3,6 +3,7 @@ package me.topchetoeu.jscript.compilation.patterns;
import java.util.LinkedList;
import java.util.List;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.common.parsing.ParseRes;
import me.topchetoeu.jscript.common.parsing.Parsing;
@ -12,7 +13,6 @@ import me.topchetoeu.jscript.compilation.JavaScript.DeclarationType;
import me.topchetoeu.jscript.compilation.values.ObjectNode;
import me.topchetoeu.jscript.compilation.values.VariableNode;
import me.topchetoeu.jscript.compilation.values.constants.StringNode;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class ObjectPattern extends ObjectDestructor<Pattern> implements Pattern {
@Override public void destructDeclResolve(CompileResult target) {

View File

@ -3,9 +3,9 @@ package me.topchetoeu.jscript.compilation.scope;
import java.util.HashMap;
import java.util.LinkedList;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.compilation.JavaScript.DeclarationType;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class Scope {
protected final HashMap<String, Variable> strictVarMap = new HashMap<>();

View File

@ -5,6 +5,7 @@ import java.util.LinkedList;
import java.util.List;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.Instruction.BreakpointType;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.common.parsing.ParseRes;
@ -24,7 +25,6 @@ import me.topchetoeu.jscript.compilation.patterns.ObjectDestructor.Member;
import me.topchetoeu.jscript.compilation.values.constants.NumberNode;
import me.topchetoeu.jscript.compilation.values.constants.StringNode;
import me.topchetoeu.jscript.compilation.values.operations.AssignNode;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class ObjectNode extends Node implements AssignTargetLike {
public static class PropertyMemberNode extends FunctionNode {

View File

@ -3,6 +3,7 @@ package me.topchetoeu.jscript.compilation.values;
import java.util.function.IntFunction;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.common.parsing.ParseRes;
import me.topchetoeu.jscript.common.parsing.Parsing;
@ -14,7 +15,6 @@ import me.topchetoeu.jscript.compilation.JavaScript.DeclarationType;
import me.topchetoeu.jscript.compilation.patterns.ChangeTarget;
import me.topchetoeu.jscript.compilation.patterns.Pattern;
import me.topchetoeu.jscript.compilation.scope.Variable;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class VariableNode extends Node implements Pattern, ChangeTarget {
public final String name;

View File

@ -2,11 +2,11 @@ package me.topchetoeu.jscript.compilation.values.operations;
import me.topchetoeu.jscript.common.Instruction;
import me.topchetoeu.jscript.common.Operation;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.Node;
import me.topchetoeu.jscript.compilation.patterns.AssignTarget;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
public class AssignNode extends Node implements AssignTarget {
public final AssignTarget assignable;

View File

@ -1,6 +1,7 @@
package me.topchetoeu.jscript.runtime;
import me.topchetoeu.jscript.common.FunctionBody;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.environment.Environment;
import me.topchetoeu.jscript.common.environment.Key;
import me.topchetoeu.jscript.common.parsing.Filename;
@ -8,7 +9,6 @@ import me.topchetoeu.jscript.compilation.CompileResult;
import me.topchetoeu.jscript.compilation.JavaScript;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.values.Value;
import me.topchetoeu.jscript.runtime.values.functions.CodeFunction;

View File

@ -8,12 +8,12 @@ import java.util.concurrent.ExecutionException;
import me.topchetoeu.jscript.common.Metadata;
import me.topchetoeu.jscript.common.Reading;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.environment.Environment;
import me.topchetoeu.jscript.common.json.JSON;
import me.topchetoeu.jscript.common.parsing.Filename;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.values.Member.FieldMember;
import me.topchetoeu.jscript.runtime.values.Member.PropertyMember;
import me.topchetoeu.jscript.runtime.values.Value;

View File

@ -1,6 +1,5 @@
package me.topchetoeu.jscript.runtime.debug;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@ -12,7 +11,6 @@ import me.topchetoeu.jscript.common.environment.Environment;
import me.topchetoeu.jscript.common.environment.Key;
import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.common.parsing.Filename;
import me.topchetoeu.jscript.common.parsing.Location;
import me.topchetoeu.jscript.runtime.Frame;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.values.functions.CodeFunction;
@ -49,8 +47,7 @@ public class DebugContext {
}
public DebugHandler debugger() {
if (debugger == null) return DebugHandler.empty();
else return debugger;
return debugger;
}
public FunctionMap getMap(FunctionBody func) {
@ -118,32 +115,4 @@ public class DebugContext {
if (enabled(exts)) return exts.get(KEY);
else return new DebugContext(false);
}
public static List<String> stackTrace(Environment env) {
var res = new ArrayList<String>();
var dbgCtx = get(env);
for (var frame : dbgCtx.getStackFrames()) {
var name = frame.function.name;
var map = dbgCtx.getMapOrEmpty(frame.function);
Location loc = null;
if (map != null) {
loc = map.toLocation(frame.codePtr, true);
if (loc == null) loc = map.start();
}
var trace = "";
if (loc != null) trace += "at " + loc.toString() + " ";
if (name != null && !name.equals("")) trace += "in " + name + " ";
trace = trace.trim();
if (!trace.equals("")) res.add(trace);
}
return res;
}
}

View File

@ -1,6 +1,5 @@
package me.topchetoeu.jscript.runtime.debug;
import java.util.Arrays;
import java.util.List;
import me.topchetoeu.jscript.common.FunctionBody;
@ -28,13 +27,6 @@ public interface DebugHandler {
*/
void onFunctionLoad(FunctionBody body, FunctionMap map);
// /**
// * Called when a function body has been loaded
// * @param body The body loaded
// * @param map The map of the function
// */
// void onFunctionUnload(FunctionBody body, FunctionMap map);
/**
* Called immediatly before an instruction is executed, as well as after an instruction, if it has threw or returned.
* This function might pause in order to await debugging commands.
@ -64,17 +56,4 @@ public interface DebugHandler {
void onFramePop(Environment env, Frame frame);
List<Frame> getStackFrame();
public static DebugHandler empty() {
return new DebugHandler () {
@Override public void onFramePop(Environment env, Frame frame) { }
@Override public void onFramePush(Environment env, Frame frame) { }
@Override public boolean onInstruction(Environment env, Frame frame, Instruction instruction, Object returnVal, EngineException error, boolean caught) {
return false;
}
@Override public void onSourceLoad(Filename filename, String source) { }
@Override public void onFunctionLoad(FunctionBody body, FunctionMap map) { }
@Override public List<Frame> getStackFrame() { return Arrays.asList(); }
};
}
}

View File

@ -57,12 +57,14 @@ public final class KeyCache {
}
public KeyCache(int value) {
this.value = NumberValue.of(value);
this.isInt = true;
this.intCache = value;
this.doubleCache = (double)value;
this.booleanCache = value != 0;
}
public KeyCache(double value) {
this.value = NumberValue.of(value);
this.isInt = (int)value == value;
this.intCache = (int)value;
this.doubleCache = value;
this.booleanCache = value != 0;

View File

@ -11,6 +11,7 @@ import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import me.topchetoeu.jscript.common.SyntaxException;
import me.topchetoeu.jscript.common.environment.Environment;
import me.topchetoeu.jscript.common.environment.Key;
import me.topchetoeu.jscript.common.json.JSON;
@ -18,7 +19,6 @@ import me.topchetoeu.jscript.common.json.JSONElement;
import me.topchetoeu.jscript.runtime.EventLoop;
import me.topchetoeu.jscript.runtime.debug.DebugContext;
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
import me.topchetoeu.jscript.runtime.values.Member.FieldMember;
import me.topchetoeu.jscript.runtime.values.Member.PropertyMember;
import me.topchetoeu.jscript.runtime.values.functions.FunctionValue;
@ -239,8 +239,8 @@ public abstract class Value {
public final boolean setMember(Environment env, KeyCache key, Value val) {
for (Value obj = this; obj != null; obj = obj.getPrototype(env)) {
var member = obj.getOwnMember(env, key);
if (member instanceof PropertyMember prop) {
if (prop.set(env, val, obj)) {
if (member != null && (member instanceof PropertyMember || obj == this)) {
if (member.set(env, val, obj)) {
if (val instanceof FunctionValue) ((FunctionValue)val).setName(key.toString(env));
return true;
}

View File

@ -72,8 +72,9 @@ public class ObjectValue extends Value {
@Override public final void freeze() { state = State.FROZEN; }
@Override public Member getOwnMember(Environment env, KeyCache key) {
if (key.isSymbol()) return symbolMembers.get(key.toSymbol());
else return members.get(key.toString(env));
if (symbolMembers.size() > 0 && key.isSymbol()) return symbolMembers.get(key.toSymbol());
else if (members.size() > 0) return members.get(key.toString(env));
else return null;
}
@Override public boolean defineOwnMember(Environment env, KeyCache key, Member member) {
var old = getOwnMember(env, key);