fix: run module in an isolated context

This commit is contained in:
TopchetoEU 2024-04-20 22:22:55 +03:00
parent ba6462458c
commit f8553b79f9
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
3 changed files with 6 additions and 3 deletions

View File

@ -92,7 +92,7 @@ public class Context implements Extensions {
return new Context(ext); return new Context(ext);
} }
public static Extensions clean(Extensions ext) { public static Extensions clean(Extensions ext) {
if (ext instanceof Context) return ((Context)ext).extensions; if (ext instanceof Context) return clean(((Context)ext).extensions);
else return ext; else return ext;
} }
} }

View File

@ -20,7 +20,7 @@ public class GlobalScope {
return Values.hasMember(ext, obj, name, false); return Values.hasMember(ext, obj, name, false);
} }
public GlobalScope globalChild() { public GlobalScope child() {
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);

View File

@ -6,6 +6,7 @@ import me.topchetoeu.jscript.common.Filename;
import me.topchetoeu.jscript.runtime.Context; import me.topchetoeu.jscript.runtime.Context;
import me.topchetoeu.jscript.runtime.Extensions; import me.topchetoeu.jscript.runtime.Extensions;
import me.topchetoeu.jscript.runtime.Key; import me.topchetoeu.jscript.runtime.Key;
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
import me.topchetoeu.jscript.utils.filesystem.Filesystem; import me.topchetoeu.jscript.utils.filesystem.Filesystem;
import me.topchetoeu.jscript.utils.filesystem.Mode; import me.topchetoeu.jscript.utils.filesystem.Mode;
@ -25,8 +26,10 @@ public interface ModuleRepo {
if (modules.containsKey(name)) return modules.get(name); if (modules.containsKey(name)) return modules.get(name);
var env = ctx.extensions.child(); var env = Context.clean(ctx.extensions).child();
env.add(CWD, fs.normalize(name, "..")); env.add(CWD, fs.normalize(name, ".."));
var glob = env.get(GlobalScope.KEY);
env.add(GlobalScope.KEY, glob.child());
var mod = new SourceModule(filename, src, env); var mod = new SourceModule(filename, src, env);
modules.put(name, mod); modules.put(name, mod);