Skip to content

Commit 856bd21

Browse files
committed
Make Truffle::Interop.export specs conditional on Env#isPolyglotAccessAllowed()
1 parent 4ea55a8 commit 856bd21

File tree

2 files changed

+90
-78
lines changed

2 files changed

+90
-78
lines changed

spec/truffle/interop/export_spec.rb

Lines changed: 80 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -8,84 +8,86 @@
88

99
require_relative '../../ruby/spec_helper'
1010

11-
describe "Truffle::Interop.export" do
11+
guard -> { Truffle::Interop.polyglot_access? } do
12+
describe "Truffle::Interop.export" do
13+
14+
it "exports an object" do
15+
object = Object.new
16+
Truffle::Interop.export :exports_an_object, object
17+
Truffle::Interop.import(:exports_an_object).should == object
18+
end
19+
20+
it "exports a primitive boolean" do
21+
Truffle::Interop.export :exports_a_primitive_number, true
22+
Truffle::Interop.import(:exports_a_primitive_number).should be_true
23+
end
24+
25+
it "exports a primitive number" do
26+
Truffle::Interop.export :exports_a_primitive_number, 14
27+
Truffle::Interop.import(:exports_a_primitive_number).should == 14
28+
end
29+
30+
it "exports a string" do
31+
Truffle::Interop.export :exports_a_string, 'hello'
32+
(Truffle::Interop.import(:exports_a_string) == 'hello').should be_true
33+
end
34+
35+
it "exports a symbol, getting back a string" do
36+
Truffle::Interop.export :exports_a_symbol, :hello
37+
(Truffle::Interop.import(:exports_a_symbol) == 'hello').should be_true
38+
end
39+
40+
it "exports a foreign object" do
41+
foreign_object = Truffle::Debug.foreign_object
42+
Truffle::Interop.export :exports_a_foreign_object, foreign_object
43+
Truffle::Interop.import(:exports_a_foreign_object).equal?(foreign_object).should be_true
44+
end
45+
46+
it "exports a Java string" do
47+
Truffle::Interop.export :exports_a_java_string, Truffle::Interop.to_java_string('hello')
48+
Truffle::Interop.import(:exports_a_java_string).should == 'hello'
49+
end
50+
51+
it "converts to Java when exporting a string" do
52+
Truffle::Interop.export :exports_a_string_with_conversion, 'hello'
53+
imported = Truffle::Interop.import_without_conversion(:exports_a_string_with_conversion)
54+
Truffle::Interop.java_string?(imported).should be_true
55+
Truffle::Interop.from_java_string(imported).should == 'hello'
56+
end
57+
58+
it "can export a string without conversion to Java" do
59+
Truffle::Interop.export_without_conversion :exports_a_string_without_conversion, 'hello'
60+
imported = Truffle::Interop.import_without_conversion(:exports_a_string_without_conversion)
61+
Truffle::Interop.java_string?(imported).should be_false
62+
imported.should == 'hello'
63+
end
64+
65+
it "can import a string without conversion from Java" do
66+
Truffle::Interop.export :imports_a_string_without_conversion, 'hello'
67+
imported = Truffle::Interop.import_without_conversion(:imports_a_string_without_conversion)
68+
Truffle::Interop.java_string?(imported).should be_true
69+
Truffle::Interop.from_java_string(imported).should == 'hello'
70+
end
71+
72+
it "can be used with a string name" do
73+
Truffle::Interop.export 'string_name', 'hello'
74+
Truffle::Interop.import('string_name').should == 'hello'
75+
end
76+
77+
it "can be used with a symbol name" do
78+
Truffle::Interop.export :symbol_name, 'hello'
79+
Truffle::Interop.import(:symbol_name).should == 'hello'
80+
end
81+
82+
it "can be used with a mix of string and symbol names" do
83+
Truffle::Interop.export :mixed_name, 'hello'
84+
Truffle::Interop.import('mixed_name').should == 'hello'
85+
end
86+
87+
it "returns the original exported value" do
88+
string = 'hello'
89+
Truffle::Interop.export(:returns_original_value, string).equal?(string).should be_true
90+
end
1291

13-
it "exports an object" do
14-
object = Object.new
15-
Truffle::Interop.export :exports_an_object, object
16-
Truffle::Interop.import(:exports_an_object).should == object
1792
end
18-
19-
it "exports a primitive boolean" do
20-
Truffle::Interop.export :exports_a_primitive_number, true
21-
Truffle::Interop.import(:exports_a_primitive_number).should be_true
22-
end
23-
24-
it "exports a primitive number" do
25-
Truffle::Interop.export :exports_a_primitive_number, 14
26-
Truffle::Interop.import(:exports_a_primitive_number).should == 14
27-
end
28-
29-
it "exports a string" do
30-
Truffle::Interop.export :exports_a_string, 'hello'
31-
(Truffle::Interop.import(:exports_a_string) == 'hello').should be_true
32-
end
33-
34-
it "exports a symbol, getting back a string" do
35-
Truffle::Interop.export :exports_a_symbol, :hello
36-
(Truffle::Interop.import(:exports_a_symbol) == 'hello').should be_true
37-
end
38-
39-
it "exports a foreign object" do
40-
foreign_object = Truffle::Debug.foreign_object
41-
Truffle::Interop.export :exports_a_foreign_object, foreign_object
42-
Truffle::Interop.import(:exports_a_foreign_object).equal?(foreign_object).should be_true
43-
end
44-
45-
it "exports a Java string" do
46-
Truffle::Interop.export :exports_a_java_string, Truffle::Interop.to_java_string('hello')
47-
Truffle::Interop.import(:exports_a_java_string).should == 'hello'
48-
end
49-
50-
it "converts to Java when exporting a string" do
51-
Truffle::Interop.export :exports_a_string_with_conversion, 'hello'
52-
imported = Truffle::Interop.import_without_conversion(:exports_a_string_with_conversion)
53-
Truffle::Interop.java_string?(imported).should be_true
54-
Truffle::Interop.from_java_string(imported).should == 'hello'
55-
end
56-
57-
it "can export a string without conversion to Java" do
58-
Truffle::Interop.export_without_conversion :exports_a_string_without_conversion, 'hello'
59-
imported = Truffle::Interop.import_without_conversion(:exports_a_string_without_conversion)
60-
Truffle::Interop.java_string?(imported).should be_false
61-
imported.should == 'hello'
62-
end
63-
64-
it "can import a string without conversion from Java" do
65-
Truffle::Interop.export :imports_a_string_without_conversion, 'hello'
66-
imported = Truffle::Interop.import_without_conversion(:imports_a_string_without_conversion)
67-
Truffle::Interop.java_string?(imported).should be_true
68-
Truffle::Interop.from_java_string(imported).should == 'hello'
69-
end
70-
71-
it "can be used with a string name" do
72-
Truffle::Interop.export 'string_name', 'hello'
73-
Truffle::Interop.import('string_name').should == 'hello'
74-
end
75-
76-
it "can be used with a symbol name" do
77-
Truffle::Interop.export :symbol_name, 'hello'
78-
Truffle::Interop.import(:symbol_name).should == 'hello'
79-
end
80-
81-
it "can be used with a mix of string and symbol names" do
82-
Truffle::Interop.export :mixed_name, 'hello'
83-
Truffle::Interop.import('mixed_name').should == 'hello'
84-
end
85-
86-
it "returns the original exported value" do
87-
string = 'hello'
88-
Truffle::Interop.export(:returns_original_value, string).equal?(string).should be_true
89-
end
90-
9193
end

src/main/java/org/truffleruby/interop/InteropNodes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,4 +1060,14 @@ public int identityHashCode(Object value) {
10601060

10611061
}
10621062

1063+
@CoreMethod(names = "polyglot_access?", onSingleton = true)
1064+
public abstract static class IsPolyglotAccessAllowedNode extends CoreMethodArrayArgumentsNode {
1065+
1066+
@Specialization
1067+
public boolean isPolyglotAccessAllowed() {
1068+
return getContext().getEnv().isPolyglotAccessAllowed();
1069+
}
1070+
1071+
}
1072+
10631073
}

0 commit comments

Comments
 (0)