refactoring
This commit is contained in:
parent
0670ffcdd1
commit
e2a8a382cc
@ -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 {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package me.topchetoeu.jscript.runtime.exceptions;
|
||||
package me.topchetoeu.jscript.common;
|
||||
|
||||
import me.topchetoeu.jscript.common.parsing.Location;
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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<>();
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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(); }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user