Skip to content

Commit b17203c

Browse files
committed
Do not try to read member on proc because of the [] method
1 parent 9f7339e commit b17203c

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Bug fixes:
99
* Fixed `Addrinfo.new(String)` to reliably find the address family (#1702).
1010
* Fixed argument checks in `BasicSocket#setsockopt` (#1460).
1111
* Fixed `ObjectSpace.trace_object_allocations` (#1456).
12+
* `Truffle::Interop.read(a_proc, key)` will no longer call the proc.
1213

1314
Compatibility:
1415

doc/contributor/interop.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ If the receiver is a Ruby `Array` or `Hash`, call `receiver[name/index]`.
165165

166166
Otherwise if the name starts with an `@` it is read as an instance variable.
167167

168-
Otherwise, if there is a method `[]` defined on the receiver, call
169-
`receiver[name/index]`.
168+
Otherwise, if there is a method `[]` defined on the receiver and the receiver
169+
is not a Proc, call `receiver[name/index]`.
170170

171171
Otherwise, if there is a method defined on the object with the same name, return
172172
it as a (bound) `Method`.

spec/truffle/interop/read_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,15 @@
9999

100100
end
101101

102+
describe "with a Proc" do
103+
104+
it "does not call the proc" do
105+
proc = -> { raise 'called' }
106+
-> { Truffle::Interop.read(proc, :key) }.should raise_error NameError
107+
Truffle::Interop.read(proc, :@var).should be_nil
108+
Truffle::Interop.read(proc, 'call').should == proc.method(:call)
109+
end
110+
111+
end
112+
102113
end

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Object readInstanceVariable(
5555
}
5656

5757
@Specialization(guards = {
58-
"!isRubyArray(receiver)", "!isRubyHash(receiver)", "!isIVar",
58+
"!isRubyArray(receiver)", "!isRubyHash(receiver)", "!isIVar", "!isRubyProc(receiver)",
5959
"methodDefined(frame, receiver, INDEX_METHOD_NAME, getIndexDefinedNode())"
6060
})
6161
public Object callIndex(
@@ -70,7 +70,7 @@ public Object callIndex(
7070

7171
@Specialization(guards = {
7272
"!isRubyArray(receiver)", "!isRubyHash(receiver)", "!isIVar",
73-
"!methodDefined(frame, receiver, INDEX_METHOD_NAME, getIndexDefinedNode())",
73+
"!methodDefined(frame, receiver, INDEX_METHOD_NAME, getIndexDefinedNode()) || isRubyProc(receiver)",
7474
"methodDefined(frame, receiver, stringName, getDefinedNode())"
7575
})
7676
public Object getBoundMethod(
@@ -85,7 +85,7 @@ public Object getBoundMethod(
8585

8686
@Specialization(guards = {
8787
"!isRubyArray(receiver)", "!isRubyHash(receiver)", "!isIVar",
88-
"!methodDefined(frame, receiver, INDEX_METHOD_NAME, getIndexDefinedNode())",
88+
"!methodDefined(frame, receiver, INDEX_METHOD_NAME, getIndexDefinedNode()) || isRubyProc(receiver)",
8989
"!methodDefined(frame, receiver, stringName, getDefinedNode())"
9090
})
9191
public Object unknownIdentifier(

0 commit comments

Comments
 (0)