fix: separate scope records from scopes

This commit is contained in:
TopchetoEU 2024-03-05 15:45:02 +02:00
parent 70d5871091
commit 7c6622c53d
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
8 changed files with 29 additions and 40 deletions

View File

@ -14,7 +14,7 @@ import java.util.stream.Collectors;
import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.common.Instruction.BreakpointType;
import me.topchetoeu.jscript.core.scope.LocalScopeRecord;
import me.topchetoeu.jscript.compilation.scope.LocalScopeRecord;
import me.topchetoeu.jscript.utils.mapping.SourceMap;
public class FunctionMap {

View File

@ -10,7 +10,7 @@ import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.common.Instruction.BreakpointType;
import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.common.mapping.FunctionMap.FunctionMapBuilder;
import me.topchetoeu.jscript.core.scope.LocalScopeRecord;
import me.topchetoeu.jscript.compilation.scope.LocalScopeRecord;
public class CompileResult {
public final Vector<Instruction> instructions = new Vector<>();

View File

@ -16,8 +16,8 @@ import me.topchetoeu.jscript.compilation.VariableDeclareStatement.Pair;
import me.topchetoeu.jscript.compilation.control.*;
import me.topchetoeu.jscript.compilation.control.SwitchStatement.SwitchCase;
import me.topchetoeu.jscript.compilation.parsing.ParseRes.State;
import me.topchetoeu.jscript.compilation.scope.LocalScopeRecord;
import me.topchetoeu.jscript.compilation.values.*;
import me.topchetoeu.jscript.core.scope.LocalScopeRecord;
import me.topchetoeu.jscript.core.exceptions.SyntaxException;
// TODO: this has to be rewritten

View File

@ -1,9 +1,7 @@
package me.topchetoeu.jscript.core.scope;
package me.topchetoeu.jscript.compilation.scope;
import java.util.ArrayList;
import me.topchetoeu.jscript.common.ScopeRecord;
public class LocalScopeRecord implements ScopeRecord {
public final LocalScopeRecord parent;

View File

@ -1,6 +1,4 @@
package me.topchetoeu.jscript.common;
import me.topchetoeu.jscript.core.scope.LocalScopeRecord;
package me.topchetoeu.jscript.compilation.scope;
public interface ScopeRecord {
public Object getKey(String name);

View File

@ -46,7 +46,7 @@ public class InstructionRunner {
private static Object execMakeVar(Context ctx, Instruction instr, Frame frame) {
var name = (String)instr.get(0);
ctx.environment.global.define(name);
ctx.environment.global.define(ctx, name);
frame.codePtr++;
return Values.NO_RETURN;
}

View File

@ -3,7 +3,6 @@ package me.topchetoeu.jscript.core.scope;
import java.util.HashSet;
import java.util.Set;
import me.topchetoeu.jscript.common.ScopeRecord;
import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction;
@ -11,31 +10,25 @@ import me.topchetoeu.jscript.core.values.ObjectValue;
import me.topchetoeu.jscript.core.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException;
public class GlobalScope implements ScopeRecord {
public class GlobalScope {
public final ObjectValue obj;
public boolean has(Context ctx, String name) {
return Values.hasMember(null, obj, name, false);
}
public Object getKey(String name) {
return name;
}
public GlobalScope globalChild() {
var obj = new ObjectValue();
Values.setPrototype(null, obj, this.obj);
return new GlobalScope(obj);
}
public LocalScopeRecord child() {
return new LocalScopeRecord();
}
public Object define(String name) {
public Object define(Context ctx, String name) {
if (Values.hasMember(Context.NULL, obj, name, false)) return name;
obj.defineProperty(Context.NULL, name, null);
return name;
}
public void define(String name, Variable val) {
public void define(Context ctx, String name, Variable val) {
obj.defineProperty(Context.NULL, name,
new NativeFunction("get " + name, args -> val.get(args.ctx)),
new NativeFunction("set " + name, args -> { val.set(args.ctx, args.get(0)); return null; }),
@ -45,10 +38,10 @@ public class GlobalScope implements ScopeRecord {
public void define(Context ctx, String name, boolean readonly, Object val) {
obj.defineProperty(ctx, name, val, readonly, true, true);
}
public void define(String ...names) {
for (var n : names) define(n);
public void define(Context ctx, String ...names) {
for (var n : names) define(ctx, n);
}
public void define(boolean readonly, FunctionValue val) {
public void define(Context ctx, boolean readonly, FunctionValue val) {
define(null, val.name, readonly, val);
}

View File

@ -173,27 +173,27 @@ public class Internals {
glob.define(null, "Encoding", false, wp.getNamespace(EncodingLib.class));
glob.define(null, "Filesystem", false, wp.getNamespace(FilesystemLib.class));
glob.define(false, wp.getConstr(FileLib.class));
glob.define(null, false, wp.getConstr(FileLib.class));
glob.define(false, wp.getConstr(DateLib.class));
glob.define(false, wp.getConstr(ObjectLib.class));
glob.define(false, wp.getConstr(FunctionLib.class));
glob.define(false, wp.getConstr(ArrayLib.class));
glob.define(null, false, wp.getConstr(DateLib.class));
glob.define(null, false, wp.getConstr(ObjectLib.class));
glob.define(null, false, wp.getConstr(FunctionLib.class));
glob.define(null, false, wp.getConstr(ArrayLib.class));
glob.define(false, wp.getConstr(BooleanLib.class));
glob.define(false, wp.getConstr(NumberLib.class));
glob.define(false, wp.getConstr(StringLib.class));
glob.define(false, wp.getConstr(SymbolLib.class));
glob.define(null, false, wp.getConstr(BooleanLib.class));
glob.define(null, false, wp.getConstr(NumberLib.class));
glob.define(null, false, wp.getConstr(StringLib.class));
glob.define(null, false, wp.getConstr(SymbolLib.class));
glob.define(false, wp.getConstr(PromiseLib.class));
glob.define(false, wp.getConstr(RegExpLib.class));
glob.define(false, wp.getConstr(MapLib.class));
glob.define(false, wp.getConstr(SetLib.class));
glob.define(null, false, wp.getConstr(PromiseLib.class));
glob.define(null, false, wp.getConstr(RegExpLib.class));
glob.define(null, false, wp.getConstr(MapLib.class));
glob.define(null, false, wp.getConstr(SetLib.class));
glob.define(false, wp.getConstr(ErrorLib.class));
glob.define(false, wp.getConstr(SyntaxErrorLib.class));
glob.define(false, wp.getConstr(TypeErrorLib.class));
glob.define(false, wp.getConstr(RangeErrorLib.class));
glob.define(null, false, wp.getConstr(ErrorLib.class));
glob.define(null, false, wp.getConstr(SyntaxErrorLib.class));
glob.define(null, false, wp.getConstr(TypeErrorLib.class));
glob.define(null, false, wp.getConstr(RangeErrorLib.class));
env.add(Environment.OBJECT_PROTO, wp.getProto(ObjectLib.class));
env.add(Environment.FUNCTION_PROTO, wp.getProto(FunctionLib.class));