import reports imports

This commit is contained in:
gnembon committed 2020-01-10 17:31:26 -04:00
1 parent a841755d61
commit 953dd1c2a5
2 files changed
+23 -5

No files matched your search

+7 -3
View File
@@ -857,11 +857,15 @@ public class Expression
// artificial construct to handle user defined functions and function definitions // artificial construct to handle user defined functions and function definitions
addLazyFunction("import", -1, (c, t, lv) -> addLazyFunction("import", -1, (c, t, lv) ->
{ {
if (lv.size() < 2) throw new InternalExpressionException("'import' needs at least a module name to import, and list of values to import"); if (lv.size() < 1) throw new InternalExpressionException("'import' needs at least a module name to import, and list of values to import");
String moduleName = lv.get(0).evalValue(c).getString(); String moduleName = lv.get(0).evalValue(c).getString();
c.host.importModule(c, moduleName); c.host.importModule(c, moduleName);
c.host.importNames(c, module, moduleName, lv.subList(1, lv.size()).stream().map((l) -> l.evalValue(c).getString()).collect(Collectors.toList())); if (lv.size() > 1)
return (cc, tt) -> Value.NULL; c.host.importNames(c, module, moduleName, lv.subList(1, lv.size()).stream().map((l) -> l.evalValue(c).getString()).collect(Collectors.toList()));
if (t == Context.VOID)
return LazyValue.NULL;
ListValue list = ListValue.wrap(c.host.availableImports(moduleName).map(StringValue::new).collect(Collectors.toList()));
return (cc, tt) -> list;
}); });
addLazyFunctionWithDelegation("call",-1, (c, t, expr, tok, lv) -> { // adjust based on c addLazyFunctionWithDelegation("call",-1, (c, t, expr, tok, lv) -> { // adjust based on c
+16 -2
View File
@@ -14,7 +14,7 @@ import java.util.stream.Stream;
public abstract class ScriptHost public abstract class ScriptHost
{ {
public class ModuleData public static class ModuleData
{ {
Module parent; Module parent;
public Map<String, FunctionValue> globalFunctions = new Object2ObjectOpenHashMap<>(); public Map<String, FunctionValue> globalFunctions = new Object2ObjectOpenHashMap<>();
@@ -96,7 +96,7 @@ public abstract class ScriptHost
Module module = getModuleOrLibraryByName(moduleName); Module module = getModuleOrLibraryByName(moduleName);
if (modules.containsKey(module.getName())) return; // aready imported, once again, in case some discrepancies in names? if (modules.containsKey(module.getName())) return; // aready imported, once again, in case some discrepancies in names?
modules.put(module.getName(), module); modules.put(module.getName(), module);
ModuleData data = new ModuleData(module); ModuleData data = new ModuleData(module);
initializeModuleGlobals(data); initializeModuleGlobals(data);
moduleData.put(module, data); moduleData.put(module, data);
runModuleCode(c, module); runModuleCode(c, module);
@@ -134,6 +134,20 @@ public abstract class ScriptHost
} }
} }
public Stream<String> availableImports(String moduleName)
{
Module source = modules.get(moduleName);
ModuleData sourceData = moduleData.get(source);
if (sourceData == null)
{
throw new InternalExpressionException("Cannot import from module that is not imported");
}
return Stream.concat(
globaVariableNames(source, s -> s.startsWith("global_")),
globaFunctionNames(source, s -> true)
).distinct().sorted();
}
protected abstract Module getModuleOrLibraryByName(String name); // this should be shell out in the executor protected abstract Module getModuleOrLibraryByName(String name); // this should be shell out in the executor
protected abstract void runModuleCode(Context c, Module module); // this should be shell out in the executor protected abstract void runModuleCode(Context c, Module module); // this should be shell out in the executor