From f8553b79f9f34a09509d566d5269c43e4b0d96a1 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Sat, 20 Apr 2024 22:22:55 +0300 Subject: [PATCH] fix: run module in an isolated context --- src/java/me/topchetoeu/jscript/runtime/Context.java | 2 +- .../me/topchetoeu/jscript/runtime/scope/GlobalScope.java | 2 +- src/java/me/topchetoeu/jscript/utils/modules/ModuleRepo.java | 5 ++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/java/me/topchetoeu/jscript/runtime/Context.java b/src/java/me/topchetoeu/jscript/runtime/Context.java index 8f51cf5..e20a62b 100644 --- a/src/java/me/topchetoeu/jscript/runtime/Context.java +++ b/src/java/me/topchetoeu/jscript/runtime/Context.java @@ -92,7 +92,7 @@ public class Context implements Extensions { return new Context(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; } } diff --git a/src/java/me/topchetoeu/jscript/runtime/scope/GlobalScope.java b/src/java/me/topchetoeu/jscript/runtime/scope/GlobalScope.java index d68d24f..63eed8e 100644 --- a/src/java/me/topchetoeu/jscript/runtime/scope/GlobalScope.java +++ b/src/java/me/topchetoeu/jscript/runtime/scope/GlobalScope.java @@ -20,7 +20,7 @@ public class GlobalScope { return Values.hasMember(ext, obj, name, false); } - public GlobalScope globalChild() { + public GlobalScope child() { var obj = new ObjectValue(); Values.setPrototype(null, obj, this.obj); return new GlobalScope(obj); diff --git a/src/java/me/topchetoeu/jscript/utils/modules/ModuleRepo.java b/src/java/me/topchetoeu/jscript/utils/modules/ModuleRepo.java index f7da8a0..1138485 100644 --- a/src/java/me/topchetoeu/jscript/utils/modules/ModuleRepo.java +++ b/src/java/me/topchetoeu/jscript/utils/modules/ModuleRepo.java @@ -6,6 +6,7 @@ import me.topchetoeu.jscript.common.Filename; import me.topchetoeu.jscript.runtime.Context; import me.topchetoeu.jscript.runtime.Extensions; 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.Mode; @@ -25,8 +26,10 @@ public interface ModuleRepo { 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, "..")); + var glob = env.get(GlobalScope.KEY); + env.add(GlobalScope.KEY, glob.child()); var mod = new SourceModule(filename, src, env); modules.put(name, mod);