use of org.phoenicis.scripts.exceptions.ScriptException in project POL-POM-5 by PhoenicisOrg.
the class IncludeInjector method injectInto.
@Override
public void injectInto(PhoenicisScriptEngine phoenicisScriptEngine) {
// store scripts that are currently in progress
final Stack<String> includeStack = new Stack<>();
// store included scripts (include path -> JS object)
final Map<String, Object> includedScripts = new HashMap<>();
phoenicisScriptEngine.put("include", (Function<String, Object>) argument -> {
if (includeStack.contains(argument)) {
throw new CircularIncludeException(argument, includeStack);
}
includeStack.push(argument);
if (!includedScripts.containsKey(argument)) {
final String script = scriptFetcher.getScript(argument);
if (script == null) {
throw new ScriptNotFoundException(argument);
}
try {
String extendedString = String.format("(module) => { %s }", script);
Value includeFunction = (Value) phoenicisScriptEngine.evalAndReturn(extendedString, this::throwException);
Value module = (Value) phoenicisScriptEngine.evalAndReturn("({})", this::throwException);
includeFunction.execute(module);
if (module.hasMember("default")) {
includedScripts.put(argument, module.getMember("default"));
} else {
includedScripts.put(argument, module);
}
} catch (ScriptException se) {
throw new IncludeException(argument, se);
}
}
includeStack.pop();
return includedScripts.get(argument);
}, this::throwException);
}
Aggregations