Skip to content

Commit bdf0785

Browse files
committed
[GR-18163] Expose all InteropLibrary messages as Truffle::Interop methods consistently (#2139)
PullRequest: truffleruby/2135
2 parents 2930581 + 57471f2 commit bdf0785

File tree

10 files changed

+1148
-527
lines changed

10 files changed

+1148
-527
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Performance:
1717

1818
Changes:
1919

20+
* All `InteropLibrary` messages are now exposed consistently as methods on `Truffle::Interop` (#2139). Some methods were renamed to match the scheme described in the documentation.
2021

2122
# 20.3.0
2223

spec/tags/truffle/interop/invoke_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/truffle/interop/new_spec.rb renamed to spec/truffle/interop/instantiate_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@
99
require_relative '../../ruby/spec_helper'
1010
require_relative 'fixtures/classes'
1111

12-
describe "Truffle::Interop.new" do
13-
12+
describe "Truffle::Interop.instantiate" do
1413
it "creates new instances of objects" do
15-
obj = Truffle::Interop.new(TruffleInteropSpecs::NewTestClass, 14, 2)
14+
obj = Truffle::Interop.instantiate(TruffleInteropSpecs::NewTestClass, 14, 2)
1615
obj.should be_an_instance_of(TruffleInteropSpecs::NewTestClass)
1716
end
1817

1918
it "calls initialize" do
20-
Truffle::Interop.new(TruffleInteropSpecs::NewTestClass, 14, 2).x.should == 16
19+
Truffle::Interop.instantiate(TruffleInteropSpecs::NewTestClass, 14, 2).x.should == 16
2120
end
22-
2321
end

spec/truffle/interop/invoke_spec.rb renamed to spec/truffle/interop/invoke_member_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
require_relative '../../ruby/spec_helper'
1010
require_relative 'fixtures/classes'
1111

12-
describe "Truffle::Interop.invoke" do
12+
describe "Truffle::Interop.invoke_member" do
1313

1414
it "invokes methods using symbols" do
15-
Truffle::Interop.invoke(TruffleInteropSpecs::InvokeTestClass.new, :add, 14, 2).should == 16
15+
Truffle::Interop.invoke_member(TruffleInteropSpecs::InvokeTestClass.new, :add, 14, 2).should == 16
1616
end
1717

1818
it "invokes methods using strings" do
19-
Truffle::Interop.invoke(TruffleInteropSpecs::InvokeTestClass.new, 'add', 14, 2).should == 16
19+
Truffle::Interop.invoke_member(TruffleInteropSpecs::InvokeTestClass.new, 'add', 14, 2).should == 16
2020
end
2121

2222
it "raises a NoMethodError when the method is not found on a foreign object" do

spec/truffle/interop/java_string_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
describe "Java strings" do
1212

1313
it "is String" do
14-
Truffle::Interop.is_string?(Truffle::Interop.to_java_string('test')).should be_true
14+
Truffle::Interop.string?(Truffle::Interop.to_java_string('test')).should be_true
1515
end
1616

1717
it "return the same object if attempted to be unboxed" do

spec/truffle/interop/key_info_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ def key_info(object, index)
1818
*(:insertable if Truffle::Interop.array_element_insertable?(object, index)),
1919
*(:removable if Truffle::Interop.array_element_removable?(object, index))]
2020
else
21-
[*(:readable if Truffle::Interop.is_member_readable?(object, index)),
22-
*(:modifiable if Truffle::Interop.is_member_modifiable?(object, index)),
23-
*(:insertable if Truffle::Interop.is_member_insertable?(object, index)),
24-
*(:removable if Truffle::Interop.is_member_removable?(object, index)),
25-
*(:internal if Truffle::Interop.is_member_internal?(object, index)),
26-
*(:invocable if Truffle::Interop.is_member_invocable?(object, index))]
21+
[*(:readable if Truffle::Interop.member_readable?(object, index)),
22+
*(:modifiable if Truffle::Interop.member_modifiable?(object, index)),
23+
*(:insertable if Truffle::Interop.member_insertable?(object, index)),
24+
*(:removable if Truffle::Interop.member_removable?(object, index)),
25+
*(:internal if Truffle::Interop.member_internal?(object, index)),
26+
*(:invocable if Truffle::Interop.member_invocable?(object, index))]
2727
end
2828
end
2929

spec/truffle/interop/methods_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# truffleruby_primitives: true
2+
3+
# Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. This
4+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
5+
# redistribute it and/or modify it under the terms of the:
6+
#
7+
# Eclipse Public License version 2.0, or
8+
# GNU General Public License version 2, or
9+
# GNU Lesser General Public License version 2.1.
10+
11+
require_relative '../../ruby/spec_helper'
12+
13+
describe "Truffle::Interop" do
14+
it "has a method for each InteropLibrary message" do
15+
all_methods = Primitive.interop_library_all_methods
16+
all_methods -= %w[getFactory getUncached]
17+
expected = all_methods.map do |name|
18+
name = name.gsub(/([a-z])([A-Z])/) { "#{$1}_#{$2.downcase}" }
19+
if name.start_with?('is_', 'has_', 'fits_')
20+
name += '?'
21+
end
22+
if name.start_with?('is_')
23+
name = name[3..-1]
24+
elsif name.start_with?('get_')
25+
name = name[4..-1]
26+
end
27+
name.to_sym
28+
end.sort
29+
30+
actual = Truffle::Interop.methods.sort
31+
32+
# pp expected
33+
# pp actual
34+
(expected - actual).should == []
35+
end
36+
end

0 commit comments

Comments
 (0)