-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from escitalopram/github-sandboxed-invocations
Allow sandboxed Invocable::invoke*
- Loading branch information
Showing
8 changed files
with
218 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/delight/nashornsandbox/internal/EvaluateOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package delight.nashornsandbox.internal; | ||
|
||
import static delight.nashornsandbox.internal.NashornSandboxImpl.LOG; | ||
|
||
import javax.script.ScriptContext; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptException; | ||
|
||
public class EvaluateOperation implements ScriptEngineOperation { | ||
|
||
private final String js; | ||
private final ScriptContext scriptContext; | ||
|
||
public String getJs() { | ||
return js; | ||
} | ||
|
||
public ScriptContext getScriptContext() { | ||
return scriptContext; | ||
} | ||
|
||
public EvaluateOperation(String js, ScriptContext scriptContext) { | ||
this.js = js; | ||
this.scriptContext = scriptContext; | ||
} | ||
|
||
@Override | ||
public Object executeScriptEngineOperation(ScriptEngine scriptEngine) throws ScriptException { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("--- Running JS ---"); | ||
LOG.debug(js); | ||
LOG.debug("--- JS END ---"); | ||
} | ||
|
||
if (scriptContext != null) { | ||
return scriptEngine.eval(js, scriptContext); | ||
} else { | ||
return scriptEngine.eval(js); | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/delight/nashornsandbox/internal/InvokeOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package delight.nashornsandbox.internal; | ||
|
||
import javax.script.Invocable; | ||
import javax.script.ScriptEngine; | ||
|
||
public class InvokeOperation implements ScriptEngineOperation { | ||
|
||
private final Object thisObj; | ||
private final String name; | ||
private final Object[] args; | ||
|
||
public InvokeOperation(Object thisObj, String name, Object[] args) { | ||
this.thisObj = thisObj; | ||
this.name = name; | ||
this.args = args; | ||
} | ||
|
||
@Override | ||
public Object executeScriptEngineOperation(ScriptEngine scriptEngine) throws Exception { | ||
if (thisObj == null) { | ||
return ((Invocable)scriptEngine).invokeFunction(name, args); | ||
} else { | ||
return ((Invocable)scriptEngine).invokeMethod(thisObj, name, args); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/delight/nashornsandbox/internal/ScriptEngineOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package delight.nashornsandbox.internal; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptException; | ||
|
||
public interface ScriptEngineOperation { | ||
|
||
Object executeScriptEngineOperation(ScriptEngine scriptEngine) throws ScriptException, Exception; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package delight.nashornsandbox; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import javax.script.Invocable; | ||
import javax.script.ScriptException; | ||
|
||
import org.junit.Test; | ||
|
||
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException; | ||
|
||
public class TestInvocable { | ||
|
||
@Test | ||
public void testInvokeFunction() throws ScriptCPUAbuseException, ScriptException, NoSuchMethodException { | ||
final NashornSandbox sandbox = NashornSandboxes.create(); | ||
final String script = "function x(){return 1;}\n"; | ||
sandbox.eval(script); | ||
Invocable invocable = sandbox.getSandboxedInvocable(); | ||
assertEquals(1, invocable.invokeFunction("x")); | ||
} | ||
|
||
@Test | ||
public void testInvokeMethod() throws ScriptCPUAbuseException, ScriptException, NoSuchMethodException { | ||
final NashornSandbox sandbox = NashornSandboxes.create(); | ||
final String script = "var obj = {n: 1, x:function(arg){return this.n + arg;}};"; | ||
sandbox.eval(script); | ||
Object thisobj = sandbox.get("obj"); | ||
Invocable invocable = sandbox.getSandboxedInvocable(); | ||
|
||
assertEquals(3.0, invocable.invokeMethod(thisobj, "x", 2)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters