Search in sources :

Example 1 with IncludeException

use of org.phoenicis.scripts.exceptions.IncludeException in project POL-POM-5 by PlayOnLinux.

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);
}
Also used : IncludeException(org.phoenicis.scripts.exceptions.IncludeException) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) Value(org.graalvm.polyglot.Value) Map(java.util.Map) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) HashMap(java.util.HashMap) Function(java.util.function.Function) Stack(java.util.Stack) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) HashMap(java.util.HashMap) Value(org.graalvm.polyglot.Value) IncludeException(org.phoenicis.scripts.exceptions.IncludeException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) Stack(java.util.Stack)

Example 2 with IncludeException

use of org.phoenicis.scripts.exceptions.IncludeException 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);
}
Also used : IncludeException(org.phoenicis.scripts.exceptions.IncludeException) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) Value(org.graalvm.polyglot.Value) Map(java.util.Map) PhoenicisScriptEngine(org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) ScriptFetcher(org.phoenicis.scripts.interpreter.ScriptFetcher) HashMap(java.util.HashMap) Function(java.util.function.Function) Stack(java.util.Stack) ScriptException(org.phoenicis.scripts.exceptions.ScriptException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) HashMap(java.util.HashMap) Value(org.graalvm.polyglot.Value) IncludeException(org.phoenicis.scripts.exceptions.IncludeException) CircularIncludeException(org.phoenicis.scripts.exceptions.CircularIncludeException) ScriptNotFoundException(org.phoenicis.scripts.exceptions.ScriptNotFoundException) Stack(java.util.Stack)

Aggregations

HashMap (java.util.HashMap)2 Map (java.util.Map)2 Stack (java.util.Stack)2 Function (java.util.function.Function)2 Value (org.graalvm.polyglot.Value)2 PhoenicisScriptEngine (org.phoenicis.scripts.engine.implementation.PhoenicisScriptEngine)2 CircularIncludeException (org.phoenicis.scripts.exceptions.CircularIncludeException)2 IncludeException (org.phoenicis.scripts.exceptions.IncludeException)2 ScriptException (org.phoenicis.scripts.exceptions.ScriptException)2 ScriptNotFoundException (org.phoenicis.scripts.exceptions.ScriptNotFoundException)2 ScriptFetcher (org.phoenicis.scripts.interpreter.ScriptFetcher)2