You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/04/17 16:53:03 UTC

[tinkerpop] branch master updated: TINKERPOP-2183 Interpreter mode not handling class definitions

This is an automated email from the ASF dual-hosted git repository.

spmallette pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git


The following commit(s) were added to refs/heads/master by this push:
     new 5e60558  TINKERPOP-2183 Interpreter mode not handling class definitions
     new 5f7b7d2  Merge branch 'tp33'
5e60558 is described below

commit 5e60558d0f6fe79249b25e9afbbf1c679f4aa671
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Wed Apr 17 12:51:17 2019 -0400

    TINKERPOP-2183 Interpreter mode not handling class definitions
    
    Without a check for a "def class" the ast transform breaks as it tries to call a method not present on the defined class. CTR
---
 CHANGELOG.asciidoc                                               | 1 +
 .../groovy/jsr223/ast/InterpreterModeASTTransformation.groovy    | 9 +++++++--
 .../gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java     | 2 ++
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 9a33554..c5582e3 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -27,6 +27,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima
 * Removed `gperfutils` dependencies from Gremlin Console.
 * Ensure `gremlin.sh` works when directories contain spaces
 * Enabled `ctrl+c` to interrupt long running processes in Gremlin Console.
+* Fixed bug in `GremlinGroovyScriptEngine` interpreter mode around class definitions.
 * Implemented `EdgeLabelVerificationStrategy`
 
 [[release-3-3-6]]
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/ast/InterpreterModeASTTransformation.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/ast/InterpreterModeASTTransformation.groovy
index 66638b8..e7a3c62 100644
--- a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/ast/InterpreterModeASTTransformation.groovy
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/ast/InterpreterModeASTTransformation.groovy
@@ -59,8 +59,13 @@ class InterpreterModeASTTransformation implements ASTTransformation {
     @Override
     void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
         ClassNode scriptNode = (ClassNode) astNodes[1]
-        def runMethodOfScript = scriptNode.declaredMethodsMap["java.lang.Object run()"]
-        runMethodOfScript.code = wrap(runMethodOfScript)
+
+        // need to check that object is a Script to call run(). this scriptNode may be a user defined class via
+        // "def class" in which case it can be ignored as there are no variables to promote to global status there
+        if (scriptNode.isDerivedFrom(ClassHelper.make(Script))) {
+            def runMethodOfScript = scriptNode.declaredMethodsMap["java.lang.Object run()"]
+            runMethodOfScript.code = wrap(runMethodOfScript)
+        }
     }
 
     private static BlockStatement wrap(MethodNode method) {
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 6fc7269..d40dc19 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -103,6 +103,7 @@ public class GremlinGroovyScriptEngineTest {
     public void shouldPromoteDefinedVarsInInterpreterModeWithNoBindings() throws Exception {
         final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(new InterpreterModeGroovyCustomizer());
         engine.eval("def addItUp = { x, y -> x + y }");
+        engine.eval("def class A { def sub(int x, int y) {x - y}}");
         assertEquals(3, engine.eval("int xxx = 1 + 2"));
         assertEquals(4, engine.eval("yyy = xxx + 1"));
         assertEquals(7, engine.eval("def zzz = yyy + xxx"));
@@ -118,6 +119,7 @@ public class GremlinGroovyScriptEngineTest {
             assertThat(root, instanceOf(MissingPropertyException.class));
         }
 
+        assertEquals(9, engine.eval("new A().sub(10, 1)"));
         assertEquals(10, engine.eval("addItUp(zzz,xxx)"));
     }