feat: readd module API via env API

This commit is contained in:
TopchetoEU 2023-12-27 13:15:19 +02:00
parent 21534efd60
commit bf38587271
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
4 changed files with 26 additions and 11 deletions

View File

@ -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<>(

View File

@ -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<Filename, String> sources;
private HashMap<Filename, TreeSet<Location>> 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);
}
}

View File

@ -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) {

View File

@ -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);
}
}