Compare commits

...

3 Commits

Author SHA1 Message Date
3e6214659b fix: use new global API 2024-03-05 15:54:51 +02:00
7c6622c53d fix: separate scope records from scopes 2024-03-05 15:45:02 +02:00
70d5871091 fix: properly check permissions 2024-03-03 20:47:54 +02:00
10 changed files with 38 additions and 45 deletions

View File

@@ -14,7 +14,7 @@ import java.util.stream.Collectors;
import me.topchetoeu.jscript.common.Filename; import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.common.Location; import me.topchetoeu.jscript.common.Location;
import me.topchetoeu.jscript.common.Instruction.BreakpointType; 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; import me.topchetoeu.jscript.utils.mapping.SourceMap;
public class FunctionMap { 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.Instruction.BreakpointType;
import me.topchetoeu.jscript.common.mapping.FunctionMap; import me.topchetoeu.jscript.common.mapping.FunctionMap;
import me.topchetoeu.jscript.common.mapping.FunctionMap.FunctionMapBuilder; 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 class CompileResult {
public final Vector<Instruction> instructions = new Vector<>(); 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.*;
import me.topchetoeu.jscript.compilation.control.SwitchStatement.SwitchCase; import me.topchetoeu.jscript.compilation.control.SwitchStatement.SwitchCase;
import me.topchetoeu.jscript.compilation.parsing.ParseRes.State; 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.compilation.values.*;
import me.topchetoeu.jscript.core.scope.LocalScopeRecord;
import me.topchetoeu.jscript.core.exceptions.SyntaxException; import me.topchetoeu.jscript.core.exceptions.SyntaxException;
// TODO: this has to be rewritten // 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 java.util.ArrayList;
import me.topchetoeu.jscript.common.ScopeRecord;
public class LocalScopeRecord implements ScopeRecord { public class LocalScopeRecord implements ScopeRecord {
public final LocalScopeRecord parent; public final LocalScopeRecord parent;

View File

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

View File

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

View File

@@ -3,7 +3,6 @@ package me.topchetoeu.jscript.core.scope;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import me.topchetoeu.jscript.common.ScopeRecord;
import me.topchetoeu.jscript.core.Context; import me.topchetoeu.jscript.core.Context;
import me.topchetoeu.jscript.core.values.FunctionValue; import me.topchetoeu.jscript.core.values.FunctionValue;
import me.topchetoeu.jscript.core.values.NativeFunction; 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.values.Values;
import me.topchetoeu.jscript.core.exceptions.EngineException; import me.topchetoeu.jscript.core.exceptions.EngineException;
public class GlobalScope implements ScopeRecord { public class GlobalScope {
public final ObjectValue obj; public final ObjectValue obj;
public boolean has(Context ctx, String name) { public boolean has(Context ctx, String name) {
return Values.hasMember(null, obj, name, false); return Values.hasMember(null, obj, name, false);
} }
public Object getKey(String name) {
return name;
}
public GlobalScope globalChild() { public GlobalScope globalChild() {
var obj = new ObjectValue(); var obj = new ObjectValue();
Values.setPrototype(null, obj, this.obj); Values.setPrototype(null, obj, this.obj);
return new GlobalScope(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; if (Values.hasMember(Context.NULL, obj, name, false)) return name;
obj.defineProperty(Context.NULL, name, null); obj.defineProperty(Context.NULL, name, null);
return name; return name;
} }
public void define(String name, Variable val) { public void define(Context ctx, String name, Variable val) {
obj.defineProperty(Context.NULL, name, obj.defineProperty(Context.NULL, name,
new NativeFunction("get " + name, args -> val.get(args.ctx)), new NativeFunction("get " + name, args -> val.get(args.ctx)),
new NativeFunction("set " + name, args -> { val.set(args.ctx, args.get(0)); return null; }), 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) { public void define(Context ctx, String name, boolean readonly, Object val) {
obj.defineProperty(ctx, name, val, readonly, true, true); obj.defineProperty(ctx, name, val, readonly, true, true);
} }
public void define(String ...names) { public void define(Context ctx, String ...names) {
for (var n : names) define(n); 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); 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, "Encoding", false, wp.getNamespace(EncodingLib.class));
glob.define(null, "Filesystem", false, wp.getNamespace(FilesystemLib.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(null, false, wp.getConstr(DateLib.class));
glob.define(false, wp.getConstr(ObjectLib.class)); glob.define(null, false, wp.getConstr(ObjectLib.class));
glob.define(false, wp.getConstr(FunctionLib.class)); glob.define(null, false, wp.getConstr(FunctionLib.class));
glob.define(false, wp.getConstr(ArrayLib.class)); glob.define(null, false, wp.getConstr(ArrayLib.class));
glob.define(false, wp.getConstr(BooleanLib.class)); glob.define(null, false, wp.getConstr(BooleanLib.class));
glob.define(false, wp.getConstr(NumberLib.class)); glob.define(null, false, wp.getConstr(NumberLib.class));
glob.define(false, wp.getConstr(StringLib.class)); glob.define(null, false, wp.getConstr(StringLib.class));
glob.define(false, wp.getConstr(SymbolLib.class)); glob.define(null, false, wp.getConstr(SymbolLib.class));
glob.define(false, wp.getConstr(PromiseLib.class)); glob.define(null, false, wp.getConstr(PromiseLib.class));
glob.define(false, wp.getConstr(RegExpLib.class)); glob.define(null, false, wp.getConstr(RegExpLib.class));
glob.define(false, wp.getConstr(MapLib.class)); glob.define(null, false, wp.getConstr(MapLib.class));
glob.define(false, wp.getConstr(SetLib.class)); glob.define(null, false, wp.getConstr(SetLib.class));
glob.define(false, wp.getConstr(ErrorLib.class)); glob.define(null, false, wp.getConstr(ErrorLib.class));
glob.define(false, wp.getConstr(SyntaxErrorLib.class)); glob.define(null, false, wp.getConstr(SyntaxErrorLib.class));
glob.define(false, wp.getConstr(TypeErrorLib.class)); glob.define(null, false, wp.getConstr(TypeErrorLib.class));
glob.define(false, wp.getConstr(RangeErrorLib.class)); glob.define(null, false, wp.getConstr(RangeErrorLib.class));
env.add(Environment.OBJECT_PROTO, wp.getProto(ObjectLib.class)); env.add(Environment.OBJECT_PROTO, wp.getProto(ObjectLib.class));
env.add(Environment.FUNCTION_PROTO, wp.getProto(FunctionLib.class)); env.add(Environment.FUNCTION_PROTO, wp.getProto(FunctionLib.class));

View File

@@ -87,10 +87,10 @@ public class JScriptRepl {
private static void initEnv() { private static void initEnv() {
environment = Internals.apply(environment); environment = Internals.apply(environment);
environment.global.define(false, new NativeFunction("exit", args -> { environment.global.define(null, false, new NativeFunction("exit", args -> {
throw new InterruptException(); throw new InterruptException();
})); }));
environment.global.define(false, new NativeFunction("go", args -> { environment.global.define(null, false, new NativeFunction("go", args -> {
try { try {
var f = Path.of("do.js"); var f = Path.of("do.js");
var func = args.ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f))); var func = args.ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f)));
@@ -100,7 +100,7 @@ public class JScriptRepl {
throw new EngineException("Couldn't open do.js"); throw new EngineException("Couldn't open do.js");
} }
})); }));
environment.global.define(false, new NativeFunction("log", args -> { environment.global.define(null, false, new NativeFunction("log", args -> {
for (var el : args.args) { for (var el : args.args) {
Values.printValue(args.ctx, el); Values.printValue(args.ctx, el);
} }

View File

@@ -5,17 +5,21 @@ import java.util.Map;
import me.topchetoeu.jscript.common.Filename; import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.utils.permissions.Matcher; import me.topchetoeu.jscript.utils.permissions.Matcher;
import me.topchetoeu.jscript.utils.permissions.Permission;
import me.topchetoeu.jscript.utils.permissions.PermissionsProvider; import me.topchetoeu.jscript.utils.permissions.PermissionsProvider;
public class RootFilesystem implements Filesystem { public class RootFilesystem implements Filesystem {
public final Map<String, Filesystem> protocols = new HashMap<>(); public final Map<String, Filesystem> protocols = new HashMap<>();
public final PermissionsProvider perms; public final PermissionsProvider perms;
public static final Permission PERM_READ = new Permission("jscript.file.read", Matcher.fileWildcard());
public static final Permission PERM_WRITE = new Permission("jscript.file.read", Matcher.fileWildcard());
private boolean canRead(String _path) { private boolean canRead(String _path) {
return perms.hasPermission("jscript.file.read:" + _path, Matcher.fileWildcard()); return perms.hasPermission(PERM_READ, _path);
} }
private boolean canWrite(String _path) { private boolean canWrite(String _path) {
return perms.hasPermission("jscript.file.write:" + _path, Matcher.fileWildcard()); return perms.hasPermission(PERM_WRITE, _path);
} }
private void modeAllowed(String _path, Mode mode) throws FilesystemException { private void modeAllowed(String _path, Mode mode) throws FilesystemException {