Skip to content

Commit

Permalink
Improve Nashorn support for scripting, make getBehaviors() public in …
Browse files Browse the repository at this point in the history
…Composite
  • Loading branch information
fullwall committed Nov 14, 2013
1 parent 4f9907e commit 0374471
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/citizensnpcs/api/ai/tree/Composite.java
Expand Up @@ -47,7 +47,7 @@ protected void addParallel(Behavior behavior) {
}
}

protected List<Behavior> getBehaviors() {
public List<Behavior> getBehaviors() {
return behaviors;
}

Expand Down
39 changes: 27 additions & 12 deletions src/main/java/net/citizensnpcs/api/scripting/ScriptCompiler.java
Expand Up @@ -21,6 +21,7 @@
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
Expand Down Expand Up @@ -115,8 +116,11 @@ private ScriptEngine loadEngine(String extension) {
if (engine != null)
return engine;
ScriptEngine search = engineManager.getEngineByExtension(extension);
if (search != null && (!(search instanceof Compilable) || !(search instanceof Invocable)))
if (search != null && (!(search instanceof Compilable) || !(search instanceof Invocable))) {
search = null;
} else if (search != null) {
search = tryUpdateClassLoader(search);
}
engines.put(extension, search);
ClassLoader cl = classLoader.get();
if (cl != null) {
Expand Down Expand Up @@ -152,17 +156,31 @@ public void run(String code, String extension, Map<String, Object> vars) throws
engine.eval(extension, context);
}

private ScriptEngine tryUpdateClassLoader(ScriptEngine search) {
ScriptEngineFactory factory = search.getFactory();
try {
Method method = factory.getClass().getMethod("getScriptEngine", ClassLoader.class);
ClassLoader loader = classLoader.get();
if (loader == null)
return search;
return (ScriptEngine) method.invoke(factory, classLoader.get());
} catch (Exception e) {
return search;
}
}

private void updateSunClassLoader(ClassLoader cl) {
if (CLASSLOADER_OVERRIDE_ENABLED) {
try {
Object global = GET_GLOBAL.invoke(null);
if (GET_APPLICATION_CLASS_LOADER.invoke(global) == null) {
INIT_APPLICATION_CLASS_LOADER.invoke(global, cl);
}
} catch (Exception e) {
e.printStackTrace();
if (!CLASSLOADER_OVERRIDE_ENABLED)
return;
try {
Object global = GET_GLOBAL.invoke(null);
if (GET_APPLICATION_CLASS_LOADER.invoke(global) == null) {
INIT_APPLICATION_CLASS_LOADER.invoke(global, cl);
}
} catch (Exception e) {
e.printStackTrace();
}

}

private class CompileTask implements Callable<ScriptFactory> {
Expand Down Expand Up @@ -271,9 +289,6 @@ public Reader getReader() throws FileNotFoundException {
}
}

public static void main(String[] args) {
}

private static final Map<String, ScriptFactory> CACHE = new MapMaker().weakValues().makeMap();

private static boolean CLASSLOADER_OVERRIDE_ENABLED;
Expand Down

0 comments on commit 0374471

Please sign in to comment.