Skip to content

Commit

Permalink
Merge pull request #74 from PlaceholderAPI/fix/issue-#73
Browse files Browse the repository at this point in the history
fix for #73
  • Loading branch information
PiggyPiglet authored Jul 13, 2023
2 parents e20da1e + c98705e commit 12207ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ allprojects {
group = 'com.extendedclip.papi.expansion.javascript'
version = '2.1.2'
description = 'PAPI-Expansion-Javascript'

apply(plugin: 'java')
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package com.extendedclip.papi.expansion.javascript.evaluator;

import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.Map;

public final class NashornScriptEvaluator implements ScriptEvaluator {
private final NashornScriptEngineFactory scriptEngineFactory;

private final ScriptEngine scriptEngine;
private final Map<String, Object> bindings;

public NashornScriptEvaluator(final NashornScriptEngineFactory scriptEngineFactory, final Map<String, Object> bindings) {
this.scriptEngineFactory = scriptEngineFactory;
public NashornScriptEvaluator(final ScriptEngine scriptEngine, final Map<String, Object> bindings) {
this.scriptEngine = scriptEngine;
this.bindings = bindings;
}

@Override
public Object execute(final Map<String, Object> additionalBindings, final String script) throws EvaluatorException, ScriptException {
final ScriptEngine engine = scriptEngineFactory.getScriptEngine("--no-java");
final Bindings globalBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
final Bindings globalBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
globalBindings.putAll(bindings);
globalBindings.putAll(additionalBindings);
engine.setBindings(globalBindings, ScriptContext.GLOBAL_SCOPE);
return engine.eval(script);
scriptEngine.setBindings(globalBindings, ScriptContext.ENGINE_SCOPE);
return scriptEngine.eval(script);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.extendedclip.papi.expansion.javascript.evaluator.util.InjectionUtil;
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;

import javax.script.ScriptEngine;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -17,16 +18,16 @@ public final class NashornScriptEvaluatorFactory implements ScriptEvaluatorFacto
"asm-util-9.2.isolated-jar",
"asm-9.2.isolated-jar"
);
private final NashornScriptEngineFactory engineFactory;

private final ThreadLocal<ScriptEngine> engines;

private NashornScriptEvaluatorFactory(final NashornScriptEngineFactory engineFactory) {
this.engineFactory = engineFactory;
this.engines = ThreadLocal.withInitial(() -> engineFactory.getScriptEngine("--no-java"));
}


@Override
public ScriptEvaluator create(final Map<String, Object> bindings) {
return new NashornScriptEvaluator(engineFactory, bindings);
return new NashornScriptEvaluator(engines.get(), bindings);
}

public static ScriptEvaluatorFactory create() throws URISyntaxException, ReflectiveOperationException, NoSuchAlgorithmException, IOException {
Expand Down

0 comments on commit 12207ba

Please sign in to comment.