10
10
package org .truffleruby ;
11
11
12
12
import static org .junit .Assert .assertEquals ;
13
+ import static org .junit .Assert .assertTrue ;
14
+ import static org .junit .Assert .fail ;
13
15
14
16
import java .util .List ;
15
17
16
18
import javax .script .Bindings ;
17
19
import javax .script .Compilable ;
18
20
import javax .script .CompiledScript ;
19
21
import javax .script .Invocable ;
22
+ import javax .script .ScriptEngine ;
20
23
import javax .script .ScriptEngineManager ;
21
24
import javax .script .ScriptException ;
22
25
26
+ import org .junit .After ;
23
27
import org .junit .Test ;
24
28
import org .truffleruby .fixtures .FluidForce ;
25
- import org .truffleruby .services .scriptengine .TruffleRubyScriptEngine ;
26
29
import org .truffleruby .services .scriptengine .TruffleRubyScriptEngineFactory ;
27
30
import org .truffleruby .shared .TruffleRuby ;
28
31
29
32
public class JSR223InteropTest {
30
33
34
+ private ScriptEngine scriptEngine = null ;
35
+
36
+ @ After
37
+ public void after () {
38
+ if (scriptEngine != null ) {
39
+ close (scriptEngine );
40
+ scriptEngine = null ;
41
+ }
42
+ }
43
+
44
+ private static void close (ScriptEngine scriptEngine ) {
45
+ try {
46
+ ((AutoCloseable ) scriptEngine ).close ();
47
+ } catch (Exception e ) {
48
+ throw new Error (e );
49
+ }
50
+ }
51
+
31
52
@ Test
32
53
public void testVersion () {
33
54
assertEquals (TruffleRuby .getEngineVersion (), new TruffleRubyScriptEngineFactory ().getEngineVersion ());
@@ -36,91 +57,93 @@ public void testVersion() {
36
57
@ Test
37
58
public void testCreateEngine () throws ScriptException {
38
59
final ScriptEngineManager m = new ScriptEngineManager ();
39
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
40
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
41
- assertEquals (14 , scriptEngine .eval ("14" ));
60
+ scriptEngine = m .getEngineByName ("ruby" );
61
+ assertEquals (42 , scriptEngine .eval ("6 * 7" ));
62
+ }
63
+
64
+ @ Test
65
+ public void testNoPermissionsByDefault () {
66
+ final ScriptEngineManager m = new ScriptEngineManager ();
67
+ scriptEngine = m .getEngineByName ("ruby" );
68
+ try {
69
+ scriptEngine .eval ("Process.pid" );
70
+ fail ("should have thrown" );
71
+ } catch (ScriptException scriptException ) {
72
+ assertEquals ("org.graalvm.polyglot.PolyglotException: native access is not allowed (SecurityError)" ,
73
+ scriptException .getMessage ());
42
74
}
43
75
}
44
76
77
+ @ Test
78
+ public void testAllAccess () throws ScriptException {
79
+ scriptEngine = new TruffleRubyScriptEngineFactory ().getScriptEngine (true );
80
+ assertTrue (scriptEngine .eval ("Process.pid" ) instanceof Integer );
81
+ }
82
+
45
83
@ Test
46
84
public void testParameters () throws ScriptException {
47
85
final ScriptEngineManager m = new ScriptEngineManager ();
48
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
49
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
50
- final Bindings bindings = scriptEngine .createBindings ();
51
- bindings .put ("a" , 14 );
52
- bindings .put ("b" , 2 );
53
- assertEquals (16 , scriptEngine .eval ("a + b" , bindings ));
54
- }
86
+ scriptEngine = m .getEngineByName ("ruby" );
87
+ final Bindings bindings = scriptEngine .createBindings ();
88
+ bindings .put ("a" , 14 );
89
+ bindings .put ("b" , 2 );
90
+ assertEquals (16 , scriptEngine .eval ("a + b" , bindings ));
55
91
}
56
92
57
93
@ Test
58
94
public void testCallingMethods () throws ScriptException , NoSuchMethodException {
59
95
final ScriptEngineManager m = new ScriptEngineManager ();
60
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
61
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
62
- assertEquals (
63
- 0.909 ,
64
- (double ) ((Invocable ) scriptEngine ).invokeMethod (scriptEngine .eval ("Math" ), "sin" , 2 ),
65
- 0.01 );
66
- }
96
+ scriptEngine = m .getEngineByName ("ruby" );
97
+ assertEquals (
98
+ 0.909 ,
99
+ (double ) ((Invocable ) scriptEngine ).invokeMethod (scriptEngine .eval ("Math" ), "sin" , 2 ),
100
+ 0.01 );
67
101
}
68
102
69
103
@ Test
70
104
public void testCreatingObjects () throws ScriptException , NoSuchMethodException {
71
105
final ScriptEngineManager m = new ScriptEngineManager ();
72
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
73
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
74
- final Object time = ((Invocable ) scriptEngine ).invokeMethod (scriptEngine .eval ("Time" ), "new" , 2021 , 3 , 18 );
75
- final Object year = ((Invocable ) scriptEngine ).invokeMethod (time , "year" );
76
- assertEquals (2021 , year );
77
- }
106
+ scriptEngine = m .getEngineByName ("ruby" );
107
+ final Object time = ((Invocable ) scriptEngine ).invokeMethod (scriptEngine .eval ("Time" ), "new" , 2021 , 3 , 18 );
108
+ final Object year = ((Invocable ) scriptEngine ).invokeMethod (time , "year" );
109
+ assertEquals (2021 , year );
78
110
}
79
111
80
112
@ SuppressWarnings ("unchecked" )
81
113
@ Test
82
114
public void testAccessingArrays () throws ScriptException {
83
115
final ScriptEngineManager m = new ScriptEngineManager ();
84
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
85
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
86
- assertEquals (4 , ((List <Object >) scriptEngine .eval ("[3, 4, 5]" )).get (1 ));
87
- }
116
+ scriptEngine = m .getEngineByName ("ruby" );
117
+ assertEquals (4 , ((List <Object >) scriptEngine .eval ("[3, 4, 5]" )).get (1 ));
88
118
}
89
119
90
- @ SuppressWarnings ("unchecked" )
91
120
@ Test
92
121
public void testAccessingHashes () throws ScriptException , NoSuchMethodException {
93
122
final ScriptEngineManager m = new ScriptEngineManager ();
94
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
95
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
96
- assertEquals (
97
- 4 ,
98
- (int ) ((Invocable ) scriptEngine ).invokeMethod (
99
- scriptEngine .eval ("{'a' => 3, 'b' => 4, 'c' => 5}" ),
100
- "fetch" ,
101
- 'b' ));
102
- }
123
+ scriptEngine = m .getEngineByName ("ruby" );
124
+ assertEquals (
125
+ 4 ,
126
+ (int ) ((Invocable ) scriptEngine ).invokeMethod (
127
+ scriptEngine .eval ("{'a' => 3, 'b' => 4, 'c' => 5}" ),
128
+ "fetch" ,
129
+ 'b' ));
103
130
}
104
131
105
132
@ Test
106
133
public void testImplementInterface () throws ScriptException {
107
134
final ScriptEngineManager m = new ScriptEngineManager ();
108
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
109
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
110
- final FluidForce fluidForce = ((Invocable ) scriptEngine )
111
- .getInterface (scriptEngine .eval (FluidForce .RUBY_SOURCE ), FluidForce .class );
112
- assertEquals (5587.008375144088 , fluidForce .getFluidForce (2.0 , 3.0 , 6.0 ), 0.01 );
113
- }
135
+ scriptEngine = m .getEngineByName ("ruby" );
136
+ final FluidForce fluidForce = ((Invocable ) scriptEngine )
137
+ .getInterface (scriptEngine .eval (FluidForce .RUBY_SOURCE ), FluidForce .class );
138
+ assertEquals (5587.008375144088 , fluidForce .getFluidForce (2.0 , 3.0 , 6.0 ), 0.01 );
114
139
}
115
140
116
141
@ Test
117
142
public void testParseOnceRunMany () throws ScriptException {
118
143
final ScriptEngineManager m = new ScriptEngineManager ();
119
- try (TruffleRubyScriptEngine scriptEngine = (TruffleRubyScriptEngine ) m
120
- .getEngineByName (TruffleRuby .LANGUAGE_ID )) {
121
- final CompiledScript compiled = ((Compilable ) scriptEngine ).compile ("14" );
122
- assertEquals (14 , compiled .eval ());
123
- }
144
+ scriptEngine = m .getEngineByName ("ruby" );
145
+ final CompiledScript compiled = ((Compilable ) scriptEngine ).compile ("14" );
146
+ assertEquals (14 , compiled .eval ());
124
147
}
125
148
126
149
}
0 commit comments