GroovyEvaluator.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Tue, 05 Feb 2013 23:46:53 +0100
branchrefactoring-javalibrary
changeset 2026 ef494c184c8b
parent 1864 60a8dc26c8c6
child 2069 75d40b7b986f
permissions -rw-r--r--
Closing branch refactoring-javalibrary

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010
"
"{ Package: 'stx:libjava' }"

Object subclass:#GroovyEvaluator
	instanceVariableNames:'requestor source'
	classVariableNames:'WorkspaceBinding'
	poolDictionaries:''
	category:'Languages-Groovy-Compiler'
!

!GroovyEvaluator class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010

"
! !

!GroovyEvaluator class methodsFor:'* As yet uncategorized *'!

evaluate: source in: context receiver: receiver  notifying: requestor logged: log ifFail: failblock
    ^self new evaluate: source in: context receiver: receiver  notifying: requestor logged: log ifFail: failblock

    "Created: / 13-04-2012 / 17:24:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GroovyEvaluator class methodsFor:'utilities'!

flushWorkspaceBinding

    WorkspaceBinding := nil

    "Created: / 02-12-2011 / 23:23:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GroovyEvaluator methodsFor:'accessing'!

currentNameSpace: ns
    ns notNil ifTrue:[
        self error:'Smalltalk namespaces not supported by Groovy'
    ].

    "Created: / 01-08-2012 / 11:18:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

currentNamespace: ns
    self currentNameSpace: ns

    "Created: / 07-04-2012 / 09:26:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GroovyEvaluator methodsFor:'compiler interface'!

evaluate:src
    | result |

    result := self evaluate:src in: nil receiver: nil notifying: nil logged: false ifFail: [self error:'Evaluation failed'].
    ^result.

    "Created: / 02-09-2011 / 10:24:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

evaluate: sourceObj in: context receiver: receiver notifying: requestorObj logged: logged ifFail: fail 
    | binding result wsForWorkspace |

    JavaVM booted ifFalse: [
        Java initialize.
        JavaVM initializeVM.
    ].

    requestor := requestorObj.
    source := sourceObj.
    wsForWorkspace := requestor notNil and: [ requestor application isKindOf: WorkspaceApplication ].
    binding := (Java classForName: 'groovy.lang.Binding') new.

    wsForWorkspace ifTrue: [
        WorkspaceBinding isNil ifTrue: [
            WorkspaceBinding := (JavaVM classForName: 'groovy.lang.Binding') new.
        ].
        binding := WorkspaceBinding
    ] ifFalse: [ 

        binding setVariable: 'this' to: receiver.
        binding setVariable: 'self' to: receiver.
        context notNil ifTrue:[
            | pc |

            binding setVariable: 'thisContext' to: context.
            pc := context pc.
            context method localVariableTable do:[:entry|
                (pc between: entry startPC and: entry endPC) ifTrue:[
                    binding setVariable: entry name to: (context at: entry slot + 1)
                ].
            ].
        ]

    ].

    result := self evaluate: source withBinding: binding.
    ^ result class javaUnwrap: result.

    "Created: / 17-08-2011 / 08:54:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

evaluate: source withBinding: binding
    "Given a binding, evaluates given a code"

    | shell result |

    [
        shell := (Java classForName: 'groovy.lang.GroovyShell') newCleared.
        shell perform: #'<init>(Lgroovy/lang/Binding;)V' with: binding.
        result := shell 
                    perform: #'evaluate(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object;'
                    with: (Java as_String: source string)
                    with: (Java as_String: 'doIt')
    ] on: JAVA org codehaus groovy control CompilationFailedException do:[:ex|
        self syntaxError: ex.    
    ].

    ^ result class javaUnwrap: result.

    "Created: / 21-08-2012 / 14:08:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

moreSharedPools: pools

    "Ignored for Java"

    "Created: / 16-08-2011 / 10:15:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GroovyEvaluator methodsFor:'error reporting'!

syntaxError: exception
    | error cause |

    error := exception getErrorCollector getError: 0.
    cause := error getCause.
    self syntaxError: exception cause: cause.

    "Created: / 21-08-2012 / 14:17:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

syntaxError:message cause: cause
    "Notify requestor of an error - if there is no requestor
     put it on the transcript. Requestor is typically the CodeView
     in which the accept/doIt was triggered, or the PositionableStream
     which does the fileIn. The requestor may decide how to highlight the
     error (and/or to abort the compile).
     Return the result passed back by the requestor."

    | sourceStream startCol endCol |

    (Smalltalk isInitialized not and:[Smalltalk isStandAloneDebug]) ifTrue:[
        "/ error during startup, but sometimes we expect an error and want to supress it
        (Smalltalk at:#Parser) parseWarningSignal query ~~ #ignore ifTrue:[
            thisContext fullPrintAll.
        ].
    ].


    "/ backward compatibility - will vanish eventually (use a handler, Luke)

    sourceStream := source string readStream.
    cause getLine - 1 timesRepeat:[ sourceStream nextLine ].
    startCol := sourceStream position - 1 + cause getStartColumn.
    endCol := sourceStream position - 1 + cause getEndColumn.

    (requestor notNil and:[requestor isTextView]) ifTrue:[
        requestor error: cause getOriginalMessage position: startCol to: endCol from: self.
        ^ self
    ].
    (Smalltalk at:#'Parser::ParseError') isHandled ifTrue:[
        (Smalltalk at:#'Parser::ParseError') new
            errorMessage: cause getOriginalMessage startPosition:startCol endPosition:endCol;
            parameter:self;
            lineNumber:cause getLine;
            raiseRequest.
    ].

    "Created: / 21-08-2012 / 17:10:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GroovyEvaluator class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '§Id§'
! !