From bf38587271e4cd216729990f7560becb2da54125 Mon Sep 17 00:00:00 2001 From: TopchetoEU <36534413+TopchetoEU@users.noreply.github.com> Date: Wed, 27 Dec 2023 13:15:19 +0200 Subject: [PATCH] feat: readd module API via env API --- src/me/topchetoeu/jscript/engine/Context.java | 2 +- .../jscript/engine/debug/DebugContext.java | 4 ++-- src/me/topchetoeu/jscript/lib/Internals.java | 17 ++++++++++------- .../topchetoeu/jscript/modules/ModuleRepo.java | 14 +++++++++++++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/me/topchetoeu/jscript/engine/Context.java b/src/me/topchetoeu/jscript/engine/Context.java index bd991a8..4924971 100644 --- a/src/me/topchetoeu/jscript/engine/Context.java +++ b/src/me/topchetoeu/jscript/engine/Context.java @@ -45,7 +45,7 @@ public class Context extends ExtensionStack { var result = Environment.compileFunc(this).call(this, null, raw, filename.toString(), env); var function = (FunctionValue)Values.getMember(this, result, "function"); - if (!engine.debugging) return function; + if (!has(DebugContext.ENV_KEY)) return function; var rawMapChain = ((ArrayValue)Values.getMember(this, result, "mapChain")).toArray(); var breakpoints = new TreeSet<>( diff --git a/src/me/topchetoeu/jscript/engine/debug/DebugContext.java b/src/me/topchetoeu/jscript/engine/debug/DebugContext.java index 6baae19..1906c65 100644 --- a/src/me/topchetoeu/jscript/engine/debug/DebugContext.java +++ b/src/me/topchetoeu/jscript/engine/debug/DebugContext.java @@ -14,7 +14,7 @@ import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.mapping.SourceMap; public class DebugContext implements DebugController { - public static final Symbol DEBUG_CTX = Symbol.get("Engine.debug"); + public static final Symbol ENV_KEY = Symbol.get("Engine.debug"); private HashMap sources; private HashMap> bpts; @@ -90,7 +90,7 @@ public class DebugContext implements DebugController { } public static DebugContext get(Extensions exts) { - if (exts.has(DEBUG_CTX)) return exts.get(DEBUG_CTX); + if (exts.has(ENV_KEY)) return exts.get(ENV_KEY); else return new DebugContext(false); } } diff --git a/src/me/topchetoeu/jscript/lib/Internals.java b/src/me/topchetoeu/jscript/lib/Internals.java index c1582a2..c36df08 100644 --- a/src/me/topchetoeu/jscript/lib/Internals.java +++ b/src/me/topchetoeu/jscript/lib/Internals.java @@ -1,28 +1,31 @@ package me.topchetoeu.jscript.lib; import java.io.IOException; -import java.util.HashMap; import me.topchetoeu.jscript.Buffer; import me.topchetoeu.jscript.Reading; import me.topchetoeu.jscript.engine.Context; -import me.topchetoeu.jscript.engine.DataKey; import me.topchetoeu.jscript.engine.Environment; import me.topchetoeu.jscript.engine.scope.GlobalScope; import me.topchetoeu.jscript.engine.values.FunctionValue; -import me.topchetoeu.jscript.engine.values.ObjectValue; import me.topchetoeu.jscript.engine.values.Values; import me.topchetoeu.jscript.exceptions.EngineException; import me.topchetoeu.jscript.interop.Native; import me.topchetoeu.jscript.interop.NativeGetter; +import me.topchetoeu.jscript.modules.ModuleRepo; import me.topchetoeu.jscript.parsing.Parsing; public class Internals { @Native public static Object require(Context ctx, String name) { - var env = ctx.environment(); - var res = env.modules.getModule(ctx, env.moduleCwd, name); - res.load(ctx); - return res.value(); + var repo = ModuleRepo.get(ctx); + + if (repo != null) { + var res = repo.getModule(ctx, ModuleRepo.cwd(ctx), name); + res.load(ctx); + return res.value(); + } + + else throw EngineException.ofError("Modules are not supported."); } @Native public static Object log(Context ctx, Object ...args) { diff --git a/src/me/topchetoeu/jscript/modules/ModuleRepo.java b/src/me/topchetoeu/jscript/modules/ModuleRepo.java index 54a98e1..9a14d23 100644 --- a/src/me/topchetoeu/jscript/modules/ModuleRepo.java +++ b/src/me/topchetoeu/jscript/modules/ModuleRepo.java @@ -4,10 +4,15 @@ import java.util.HashMap; import me.topchetoeu.jscript.Filename; import me.topchetoeu.jscript.engine.Context; +import me.topchetoeu.jscript.engine.Extensions; +import me.topchetoeu.jscript.engine.values.Symbol; import me.topchetoeu.jscript.filesystem.Filesystem; import me.topchetoeu.jscript.filesystem.Mode; public interface ModuleRepo { + public static final Symbol ENV_KEY = Symbol.get("Environment.modules"); + public static final Symbol CWD = Symbol.get("Environment.moduleCwd"); + public Module getModule(Context ctx, String cwd, String name); public static ModuleRepo ofFilesystem(Filesystem fs) { @@ -21,7 +26,7 @@ public interface ModuleRepo { if (modules.containsKey(name)) return modules.get(name); var env = ctx.environment().child(); - env.moduleCwd = fs.normalize(name, ".."); + env.add(CWD, fs.normalize(name, "..")); var mod = new SourceModule(filename, src, env); modules.put(name, mod); @@ -29,4 +34,11 @@ public interface ModuleRepo { return mod; }; } + + public static String cwd(Extensions exts) { + return exts.init(CWD, "/"); + } + public static ModuleRepo get(Extensions exts) { + return exts.get(ENV_KEY); + } }