10
10
package org .truffleruby ;
11
11
12
12
import org .graalvm .polyglot .Context ;
13
+ import org .graalvm .polyglot .HostAccess ;
13
14
import org .graalvm .polyglot .Source ;
14
15
import org .graalvm .polyglot .Value ;
15
16
import org .junit .Test ;
@@ -28,46 +29,28 @@ public class PolyglotInteropTest {
28
29
29
30
@ Test
30
31
public void testCreateContext () {
31
- try (Context polyglot = Context .newBuilder ()
32
- .allowAllAccess (true )
33
- .build ()) {
34
- assertEquals (14 , polyglot .eval (TruffleRuby .LANGUAGE_ID , "14" ).asInt ());
35
- }
36
- }
37
-
38
- @ Test
39
- public void testCreateContextNoAccess () {
40
- try (Context polyglot = Context .newBuilder ()
41
- .allowExperimentalOptions (true )
42
- .option (OptionsCatalog .NATIVE_PLATFORM .getName (), Boolean .FALSE .toString ())
43
- .build ()) {
32
+ try (Context polyglot = Context .create ()) {
44
33
assertEquals (14 , polyglot .eval (TruffleRuby .LANGUAGE_ID , "14" ).asInt ());
45
34
}
46
35
}
47
36
48
37
@ Test
49
38
public void testParameters () {
50
- try (Context polyglot = Context .newBuilder ()
51
- .allowAllAccess (true )
52
- .build ()) {
39
+ try (Context polyglot = Context .create ()) {
53
40
assertEquals (16 , polyglot .eval ("ruby" , "lambda { |a, b| a + b }" ).execute (14 , 2 ).asInt ());
54
41
}
55
42
}
56
43
57
44
@ Test
58
45
public void testCallingMethods () {
59
- try (Context polyglot = Context .newBuilder ()
60
- .allowAllAccess (true )
61
- .build ()) {
46
+ try (Context polyglot = Context .create ()) {
62
47
assertEquals (0.909 , polyglot .eval ("ruby" , "Math" ).getMember ("sin" ).execute (2 ).asDouble (), 0.01 );
63
48
}
64
49
}
65
50
66
51
@ Test
67
52
public void testPassingBlocks () {
68
- try (Context polyglot = Context .newBuilder ()
69
- .allowAllAccess (true )
70
- .build ()) {
53
+ try (Context polyglot = Context .newBuilder ().allowHostAccess (HostAccess .ALL ).build ()) {
71
54
final int [] counter = new int []{0 };
72
55
73
56
polyglot .eval ("ruby" , "lambda { |block| (1..3).each { |n| block.call n } }" )
@@ -79,28 +62,23 @@ public void testPassingBlocks() {
79
62
80
63
@ Test
81
64
public void testCreatingObjects () {
82
- try (Context polyglot = Context .newBuilder ()
83
- .allowAllAccess (true )
84
- .build ()) {
65
+ // Native access needed for ENV['TZ']
66
+ try (Context polyglot = Context .newBuilder ().allowNativeAccess (true ).build ()) {
85
67
assertEquals (2021 , polyglot .eval ("ruby" , "Time" ).newInstance (2021 , 3 , 18 ).getMember ("year" ).execute ().asInt ());
86
68
}
87
69
}
88
70
89
71
@ Test
90
72
public void testAccessingArrays () {
91
- try (Context polyglot = Context .newBuilder ()
92
- .allowAllAccess (true )
93
- .build ()) {
73
+ try (Context polyglot = Context .create ()) {
94
74
assertEquals (4 , polyglot .eval ("ruby" , "[3, 4, 5]" ).getArrayElement (1 ).asInt ());
95
75
assertEquals (4 , polyglot .eval ("ruby" , "[3, 4, 5]" ).as (List .class ).get (1 ));
96
76
}
97
77
}
98
78
99
79
@ Test
100
80
public void testAccessingHashes () {
101
- try (Context polyglot = Context .newBuilder ()
102
- .allowAllAccess (true )
103
- .build ()) {
81
+ try (Context polyglot = Context .create ()) {
104
82
final Value access = polyglot .eval ("ruby" , "->(hash, key) { hash[key] }" );
105
83
final Value hash = polyglot .eval ("ruby" , "{'a' => 3, 'b' => 4, 'c' => 5}" );
106
84
assertEquals (4 , access .execute (hash , "b" ).asInt ());
@@ -109,9 +87,7 @@ public void testAccessingHashes() {
109
87
110
88
@ Test
111
89
public void testImplementInterface () {
112
- try (Context polyglot = Context .newBuilder ()
113
- .allowAllAccess (true )
114
- .build ()) {
90
+ try (Context polyglot = Context .newBuilder ().allowHostAccess (HostAccess .EXPLICIT ).build ()) {
115
91
final FluidForce fluidForce = polyglot .eval ("ruby" , FluidForce .RUBY_SOURCE ).as (FluidForce .class );
116
92
assertEquals (5587.008375144088 , fluidForce .getFluidForce (2.0 , 3.0 , 6.0 ), 0.01 );
117
93
}
@@ -120,39 +96,31 @@ public void testImplementInterface() {
120
96
@ SuppressWarnings ({"rawtypes" , "unchecked" })
121
97
@ Test
122
98
public void testImplementLambda () {
123
- try (Context polyglot = Context .newBuilder ()
124
- .allowAllAccess (true )
125
- .build ()) {
99
+ try (Context polyglot = Context .create ()) {
126
100
final BiFunction adder = polyglot .eval ("ruby" , "lambda { |a, b| a + b }" ).as (BiFunction .class );
127
101
assertEquals (16 , (int ) adder .apply (14 , 2 ));
128
102
}
129
103
}
130
104
131
105
@ Test
132
106
public void testParseOnceRunMany () {
133
- try (Context polyglot = Context .newBuilder ()
134
- .allowAllAccess (true )
135
- .build ()) {
107
+ try (Context polyglot = Context .create ()) {
136
108
final Value parsedOnce = polyglot .eval ("ruby" , "lambda { 14 }" );
137
109
assertEquals (14 , parsedOnce .execute ().asInt ());
138
110
}
139
111
}
140
112
141
113
@ Test
142
114
public void testLocalVariablesNotSharedBetweenNonInteractiveEval () {
143
- try (Context polyglot = Context .newBuilder ()
144
- .allowAllAccess (true )
145
- .build ()) {
115
+ try (Context polyglot = Context .create ()) {
146
116
polyglot .eval ("ruby" , "a = 14" );
147
117
assertTrue (polyglot .eval ("ruby" , "defined?(a).nil?" ).asBoolean ());
148
118
}
149
119
}
150
120
151
121
@ Test
152
122
public void testLocalVariablesSharedBetweenInteractiveEval () {
153
- try (Context polyglot = Context .newBuilder ()
154
- .allowAllAccess (true )
155
- .build ()) {
123
+ try (Context polyglot = Context .create ()) {
156
124
polyglot .eval (Source .newBuilder ("ruby" , "a = 14" , "test" ).interactive (true ).buildLiteral ());
157
125
assertFalse (polyglot .eval (Source .newBuilder ("ruby" , "defined?(a).nil?" , "test" ).interactive (true ).buildLiteral ()).asBoolean ());
158
126
polyglot .eval (Source .newBuilder ("ruby" , "b = 2" , "test" ).interactive (true ).buildLiteral ());
@@ -162,9 +130,7 @@ public void testLocalVariablesSharedBetweenInteractiveEval() {
162
130
163
131
@ Test
164
132
public void testLocalVariablesSharedBetweenInteractiveEvalChangesParsing () {
165
- try (Context polyglot = Context .newBuilder ()
166
- .allowAllAccess (true )
167
- .build ()) {
133
+ try (Context polyglot = Context .create ()) {
168
134
polyglot .eval (Source .newBuilder ("ruby" , "def foo; 12; end" , "test" ).interactive (true ).buildLiteral ());
169
135
assertEquals (12 , polyglot .eval (Source .newBuilder ("ruby" , "foo" , "test" ).interactive (true ).buildLiteral ()).asInt ());
170
136
polyglot .eval (Source .newBuilder ("ruby" , "foo = 42" , "test" ).interactive (true ).buildLiteral ());
@@ -174,9 +140,7 @@ public void testLocalVariablesSharedBetweenInteractiveEvalChangesParsing() {
174
140
175
141
@ Test
176
142
public void testLocalVariablesAreNotSharedBetweenInteractiveAndNonInteractive () {
177
- try (Context polyglot = Context .newBuilder ()
178
- .allowAllAccess (true )
179
- .build ()) {
143
+ try (Context polyglot = Context .create ()) {
180
144
polyglot .eval (Source .newBuilder ("ruby" , "a = 14" , "test" ).interactive (false ).buildLiteral ());
181
145
polyglot .eval (Source .newBuilder ("ruby" , "b = 2" , "test" ).interactive (true ).buildLiteral ());
182
146
assertTrue (polyglot .eval (Source .newBuilder ("ruby" , "defined?(a).nil?" , "test" ).interactive (true ).buildLiteral ()).asBoolean ());
0 commit comments