src/GroovyEvaluator.st
author vranyj1
Mon, 09 Apr 2012 09:51:32 +0000
branchjk_new_structure
changeset 1459 9e24f4337d78
parent 1370 0708027f69b0
child 1478 329a15eedd36
permissions -rw-r--r--
Tests for finally
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
     1
"
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
     2
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
1152
040cba55a7d2 Copyright fixes
vranyj1
parents: 1044
diff changeset
     3
1155
vranyj1
parents: 1152
diff changeset
     4
 New code and modifications done at SWING Research Group [1]:
1152
040cba55a7d2 Copyright fixes
vranyj1
parents: 1044
diff changeset
     5
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
     6
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
     7
                            SWING Research Group, Czech Technical University in Prague
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
     8
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
     9
 This software is furnished under a license and may be used
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    10
 only in accordance with the terms of that license and with the
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    11
 inclusion of the above copyright notice.   This software may not
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    12
 be provided or otherwise made available to, or used by, any
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    13
 other person.  No title to or ownership of the software is
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    14
 hereby transferred.
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    15
1155
vranyj1
parents: 1152
diff changeset
    16
 [1] Code written at SWING Research Group contains a signature
vranyj1
parents: 1152
diff changeset
    17
     of one of the above copright owners. For exact set of such code,
vranyj1
parents: 1152
diff changeset
    18
     see the differences between this version and version stx:libjava
1152
040cba55a7d2 Copyright fixes
vranyj1
parents: 1044
diff changeset
    19
     as of 1.9.2010
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    20
"
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    21
"{ Package: 'stx:libjava' }"
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    22
1370
0708027f69b0 oops, class rename failed. Fixed now
vranyj1
parents: 1368
diff changeset
    23
Object subclass:#GroovyEvaluator
1044
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    24
	instanceVariableNames:''
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    25
	classVariableNames:'WorkspaceBinding'
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    26
	poolDictionaries:''
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    27
	category:'Languages-Java-Support'
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    28
!
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    29
1370
0708027f69b0 oops, class rename failed. Fixed now
vranyj1
parents: 1368
diff changeset
    30
!GroovyEvaluator class methodsFor:'documentation'!
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    31
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    32
copyright
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    33
"
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    34
 COPYRIGHT (c) 1996-2011 by Claus Gittinger
1152
040cba55a7d2 Copyright fixes
vranyj1
parents: 1044
diff changeset
    35
1155
vranyj1
parents: 1152
diff changeset
    36
 New code and modifications done at SWING Research Group [1]:
1152
040cba55a7d2 Copyright fixes
vranyj1
parents: 1044
diff changeset
    37
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    38
 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    39
                            SWING Research Group, Czech Technical University in Prague
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    40
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    41
 This software is furnished under a license and may be used
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    42
 only in accordance with the terms of that license and with the
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    43
 inclusion of the above copyright notice.   This software may not
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    44
 be provided or otherwise made available to, or used by, any
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    45
 other person.  No title to or ownership of the software is
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    46
 hereby transferred.
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    47
1155
vranyj1
parents: 1152
diff changeset
    48
 [1] Code written at SWING Research Group contains a signature
vranyj1
parents: 1152
diff changeset
    49
     of one of the above copright owners. For exact set of such code,
vranyj1
parents: 1152
diff changeset
    50
     see the differences between this version and version stx:libjava
1152
040cba55a7d2 Copyright fixes
vranyj1
parents: 1044
diff changeset
    51
     as of 1.9.2010
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    52
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    53
"
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    54
! !
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    55
1370
0708027f69b0 oops, class rename failed. Fixed now
vranyj1
parents: 1368
diff changeset
    56
!GroovyEvaluator class methodsFor:'utilities'!
1204
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    57
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    58
flushWorkspaceBinding
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    59
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    60
    WorkspaceBinding := nil
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    61
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    62
    "Created: / 02-12-2011 / 23:23:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    63
! !
6c2b887399e8 Few small fixes
vranyj1
parents: 1202
diff changeset
    64
1459
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    65
!GroovyEvaluator methodsFor:'* As yet uncategorized *'!
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    66
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    67
currentNamespace: ns
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    68
    ns notNil ifTrue:[
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    69
        self error:'Smalltalk namespaces not supported by Groovy'
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    70
    ].
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    71
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    72
    "Created: / 07-04-2012 / 09:26:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    73
! !
9e24f4337d78 Tests for finally
vranyj1
parents: 1370
diff changeset
    74
1370
0708027f69b0 oops, class rename failed. Fixed now
vranyj1
parents: 1368
diff changeset
    75
!GroovyEvaluator methodsFor:'compiler interface'!
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
    76
971
937ed5f288a7 added: #evaluate:
vranyj1
parents: 960
diff changeset
    77
evaluate:source 
937ed5f288a7 added: #evaluate:
vranyj1
parents: 960
diff changeset
    78
937ed5f288a7 added: #evaluate:
vranyj1
parents: 960
diff changeset
    79
    ^self evaluate:source in: nil receiver: nil notifying: nil logged: false ifFail: [self error:'Evaluation failed'].
937ed5f288a7 added: #evaluate:
vranyj1
parents: 960
diff changeset
    80
937ed5f288a7 added: #evaluate:
vranyj1
parents: 960
diff changeset
    81
    "Created: / 02-09-2011 / 10:24:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
937ed5f288a7 added: #evaluate:
vranyj1
parents: 960
diff changeset
    82
!
937ed5f288a7 added: #evaluate:
vranyj1
parents: 960
diff changeset
    83
1033
a31de6a4cfa4 refactoringl, moved registry to JavaVM
hlopkmar
parents: 972
diff changeset
    84
evaluate: source in: context receiver: receiver notifying: requestor logged: logged ifFail: fail 
a31de6a4cfa4 refactoringl, moved registry to JavaVM
hlopkmar
parents: 972
diff changeset
    85
    | binding  shell  result |
918
73bd57f8d3b0 - fixes in natives (reflective support for fields)
vranyj1
parents: 917
diff changeset
    86
1033
a31de6a4cfa4 refactoringl, moved registry to JavaVM
hlopkmar
parents: 972
diff changeset
    87
    JavaVM booted ifFalse: [
959
1f3342b2e728 Fixes...
vranyj1
parents: 918
diff changeset
    88
        Java initialize.
1f3342b2e728 Fixes...
vranyj1
parents: 918
diff changeset
    89
        JavaVM initializeVM.
1f3342b2e728 Fixes...
vranyj1
parents: 918
diff changeset
    90
    ].
1044
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    91
    (requestor notNil and: [ requestor application isKindOf: WorkspaceApplication ]) ifTrue: [
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    92
        WorkspaceBinding isNil ifTrue: [
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    93
            WorkspaceBinding := (JavaVM classForName: 'groovy.lang.Binding') new.
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    94
        ].
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    95
        binding := WorkspaceBinding
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    96
    ] ifFalse: [ 
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    97
        binding := (Java classForName: 'groovy.lang.Binding') new. 
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    98
    ].
84b7d94d9eb8 Java Workspace now share same binding (i.e., workspace variables are remembered)
vranyj1
parents: 1033
diff changeset
    99
1033
a31de6a4cfa4 refactoringl, moved registry to JavaVM
hlopkmar
parents: 972
diff changeset
   100
    shell := (Java classForName: 'groovy.lang.GroovyShell') newCleared.
918
73bd57f8d3b0 - fixes in natives (reflective support for fields)
vranyj1
parents: 917
diff changeset
   101
    shell perform: #'<init>(Lgroovy/lang/Binding;)V' with: binding.
73bd57f8d3b0 - fixes in natives (reflective support for fields)
vranyj1
parents: 917
diff changeset
   102
    result := shell perform: #'evaluate(Ljava/lang/String;)Ljava/lang/Object;'
1033
a31de6a4cfa4 refactoringl, moved registry to JavaVM
hlopkmar
parents: 972
diff changeset
   103
                with: (Java 
a31de6a4cfa4 refactoringl, moved registry to JavaVM
hlopkmar
parents: 972
diff changeset
   104
                        as_String: (source isText ifTrue: [ source string ] ifFalse: [ source ])).
1202
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   105
    ^ self unbox: result.
918
73bd57f8d3b0 - fixes in natives (reflective support for fields)
vranyj1
parents: 917
diff changeset
   106
73bd57f8d3b0 - fixes in natives (reflective support for fields)
vranyj1
parents: 917
diff changeset
   107
    "Created: / 17-08-2011 / 08:54:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
73bd57f8d3b0 - fixes in natives (reflective support for fields)
vranyj1
parents: 917
diff changeset
   108
!
73bd57f8d3b0 - fixes in natives (reflective support for fields)
vranyj1
parents: 917
diff changeset
   109
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   110
moreSharedPools: pools
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   111
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   112
    "Ignored for Java"
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   113
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   114
    "Created: / 16-08-2011 / 10:15:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   115
! !
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   116
1370
0708027f69b0 oops, class rename failed. Fixed now
vranyj1
parents: 1368
diff changeset
   117
!GroovyEvaluator methodsFor:'private'!
1202
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   118
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   119
unbox: anObject
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   120
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   121
    | nm |
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   122
    nm := anObject class name.
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   123
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   124
    nm == #'java/lang/Byte' ifTrue:[
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   125
        ^anObject instVarNamed: #value.
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   126
    ].
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   127
    nm == #'java/lang/Short' ifTrue:[
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   128
        ^anObject instVarNamed: #value.    
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   129
    ].
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   130
    nm == #'java/lang/Integer' ifTrue:[
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   131
        ^anObject instVarNamed: #value.
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   132
    ].
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   133
    nm == #'java/lang/Long' ifTrue:[
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   134
        ^anObject instVarNamed: #value.
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   135
    ].
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   136
    nm == #'java/lang/Character' ifTrue:[
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   137
        ^Character codePoint: (anObject instVarNamed: #value).
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   138
    ].
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   139
    nm == #'java/lang/Boolean' ifTrue:[
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   140
        ^ (anObject instVarNamed: #value) == 1
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   141
    ].
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   142
    nm == #'java/lang/String' ifTrue:[
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   143
        ^ Java as_ST_String: anObject.
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   144
    ].
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   145
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   146
    ^anObject
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   147
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   148
    "Created: / 01-12-2011 / 23:47:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   149
! !
7d9f24940ed5 - JavaVM: fix in method/ctor invocation using reflection
vranyj1
parents: 1155
diff changeset
   150
1370
0708027f69b0 oops, class rename failed. Fixed now
vranyj1
parents: 1368
diff changeset
   151
!GroovyEvaluator class methodsFor:'documentation'!
917
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   152
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   153
version_SVN
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   154
    ^ '$Id$'
ecd1a00132f1 - added JavaEvaluator
vranyj1
parents:
diff changeset
   155
! !