Skip to content

Commit

Permalink
ScriptEngine: added method to execute closure
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 12, 2020
1 parent bc331f8 commit 02c7f67
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Utils {
private Utils() {}

static String toString(Object object) {
object ? object.toString() : 'null'
object != null ? object.toString() : 'null'
}

static Object toObject(String json) {
Expand All @@ -29,7 +29,7 @@ public class Utils {
}

static Map<String,Object> toMap(Object object) {
object ? object.properties : null
object != null ? object.properties : null
}

static String toJson(Object object) {
Expand Down
9 changes: 9 additions & 0 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.jline.reader.ScriptEngine;

import groovy.lang.Binding;
import groovy.lang.Closure;
import groovy.lang.GroovyShell;
import groovy.lang.Script;

Expand Down Expand Up @@ -165,6 +166,14 @@ public Object execute(String statement) throws Exception {
return out;
}

@Override
public Object execute(Object closure, Object... args) {
if (!(closure instanceof Closure)) {
throw new IllegalArgumentException();
}
return ((Closure<?>)closure).call(args);
}

@Override
public String getEngineName() {
return this.getClass().getSimpleName();
Expand Down
9 changes: 9 additions & 0 deletions reader/src/main/java/org/jline/reader/ScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,13 @@ default Object execute(File script) throws Exception {
*/
Object execute(File script, Object[] args) throws Exception;

/**
* Executes scriptEngine closure
* @param closure
* @param args
* @return
* @throws Exception
*/
Object execute(Object closure, Object... args);

}

0 comments on commit 02c7f67

Please sign in to comment.