Permissions and filesystems #9

Merged
TopchetoEU merged 36 commits from TopchetoEU/perms-and-fs into master 2023-11-25 18:10:59 +00:00
3 changed files with 46 additions and 18 deletions
Showing only changes of commit 488deea164 - Show all commits

View File

@ -33,7 +33,8 @@ public class Context {
} }
public FunctionValue compile(Filename filename, String raw) { public FunctionValue compile(Filename filename, String raw) {
var transpiled = environment().compile.call(this, null, raw, filename.toString()); var env = environment();
var transpiled = env.compile.call(this, null, raw, filename.toString(), env);
String source = null; String source = null;
FunctionValue runner = null; FunctionValue runner = null;
@ -45,7 +46,7 @@ public class Context {
else source = Values.toString(this, transpiled); else source = Values.toString(this, transpiled);
var breakpoints = new TreeSet<Location>(); var breakpoints = new TreeSet<Location>();
FunctionValue res = Parsing.compile(Engine.functions, breakpoints, environment(), filename, source); FunctionValue res = Parsing.compile(Engine.functions, breakpoints, env, filename, source);
engine.onSource(filename, source, breakpoints); engine.onSource(filename, source, breakpoints);
if (runner != null) res = (FunctionValue)runner.call(this, null, res); if (runner != null) res = (FunctionValue)runner.call(this, null, res);

View File

@ -22,6 +22,10 @@ public class Environment {
public GlobalScope global; public GlobalScope global;
public WrappersProvider wrappers; public WrappersProvider wrappers;
private static int nextId = 0;
@Native public int id = ++nextId;
@Native public FunctionValue compile; @Native public FunctionValue compile;
@Native public FunctionValue regexConstructor = new NativeFunction("RegExp", (ctx, thisArg, args) -> { @Native public FunctionValue regexConstructor = new NativeFunction("RegExp", (ctx, thisArg, args) -> {
throw EngineException.ofError("Regular expressions not supported.").setCtx(ctx.environment(), ctx.engine); throw EngineException.ofError("Regular expressions not supported.").setCtx(ctx.environment(), ctx.engine);

View File

@ -1,7 +1,13 @@
(function (_arguments) { (function (_arguments) {
var ts = _arguments[0]; var ts = _arguments[0];
var src = '', lib = _arguments[2].concat([ 'declare const exit: never; declare const go: any;' ]).join(''), decls = '', version = 0; var src = '', version = 0;
var lib = _arguments[2].concat([
'declare const exit: never; declare const go: any;',
'declare function getTsDeclarations(): string[];'
]).join('');
var libSnapshot = ts.ScriptSnapshot.fromString(lib); var libSnapshot = ts.ScriptSnapshot.fromString(lib);
var environments = {};
var declSnapshots = [];
var settings = { var settings = {
outDir: "/out", outDir: "/out",
@ -21,18 +27,27 @@
var service = ts.createLanguageService({ var service = ts.createLanguageService({
getCurrentDirectory: function() { return "/"; }, getCurrentDirectory: function() { return "/"; },
getDefaultLibFileName: function() { return "/lib.d.ts"; }, getDefaultLibFileName: function() { return "/lib.d.ts"; },
getScriptFileNames: function() { return [ "/src.ts", "/lib.d.ts", "/glob.d.ts" ]; }, getScriptFileNames: function() {
var res = [ "/src.ts", "/lib.d.ts" ];
for (var i = 0; i < declSnapshots.length; i++) res.push("/glob." + (i + 1) + ".d.ts");
return res;
},
getCompilationSettings: function () { return settings; }, getCompilationSettings: function () { return settings; },
fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; }, fileExists: function(filename) { return filename === "/lib.d.ts" || filename === "/src.ts" || filename === "/glob.d.ts"; },
getScriptSnapshot: function(filename) { getScriptSnapshot: function(filename) {
if (filename === "/lib.d.ts") return libSnapshot; if (filename === "/lib.d.ts") return libSnapshot;
if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src); if (filename === "/src.ts") return ts.ScriptSnapshot.fromString(src);
if (filename === "/glob.d.ts") return ts.ScriptSnapshot.fromString(decls);
var index = /\/glob\.(\d+)\.d\.ts/g.exec(filename);
if (index && index[1] && (index = Number(index[1])) && index > 0 && index <= declSnapshots.length) {
return declSnapshots[index - 1];
}
throw new Error("File '" + filename + "' doesn't exist."); throw new Error("File '" + filename + "' doesn't exist.");
}, },
getScriptVersion: function (filename) { getScriptVersion: function (filename) {
if (filename === "/lib.d.ts") return 0; if (filename === "/lib.d.ts" || filename.startsWith("/glob.")) return 0;
else return version; else return version;
}, },
}, reg); }, reg);
@ -40,10 +55,12 @@
service.getEmitOutput("/lib.d.ts"); service.getEmitOutput("/lib.d.ts");
log("Loaded libraries!"); log("Loaded libraries!");
function compile(code, filename) { function compile(code, filename, env) {
src = code; src = code;
version++; version++;
if (!environments[env.id]) environments[env.id] = []
declSnapshots = environments[env.id];
var emit = service.getEmitOutput("/src.ts"); var emit = service.getEmitOutput("/src.ts");
var diagnostics = [] var diagnostics = []
@ -58,31 +75,37 @@
if (file === "src.ts") file = filename; if (file === "src.ts") file = filename;
return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message; return file + ":" + (pos.line + 1) + ":" + (pos.character + 1) + ": " + message;
} }
else return "Error: " + message; else return message;
}); });
if (diagnostics.length > 0) { if (diagnostics.length > 0) {
throw new SyntaxError(diagnostics.join("\n")); throw new SyntaxError(diagnostics.join("\n"));
} }
return { var result = emit.outputFiles[0].text;
result: emit.outputFiles[0].text, var declaration = emit.outputFiles[1].text;
declaration: emit.outputFiles[1].text
};
}
_arguments[1].compile = function (filename, code) {
var res = compile(filename, code);
return { return {
source: res.result, source: result,
runner: function(func) { runner: function(func) {
return function() { return function() {
var val = func.apply(this, arguments); var val = func.apply(this, arguments);
decls += res.declaration; if (declaration !== '') {
declSnapshots.push(ts.ScriptSnapshot.fromString(declaration));
}
return val; return val;
} }
} }
};
}
function apply(env) {
env.compile = compile;
env.global.getTsDeclarations = function() {
return environments[env.id];
} }
} }
apply(_arguments[1]);
})(arguments); })(arguments);