Skip to content

Commit 2f6d1dc

Browse files
committed
Clarify why BoxedValue should not support invokeMember() and readMember()
1 parent e66724f commit 2f6d1dc

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

spec/truffle/interop/polyglot/foreign_number_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,10 @@
7070
(foreign ** 2).should == (ruby ** 2)
7171
end
7272
end
73+
74+
it "does not support odd? yet" do
75+
@numbers.each do |foreign, _ruby|
76+
-> { foreign.odd? }.should raise_error(Polyglot::UnsupportedMessageError)
77+
end
78+
end
7379
end

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1313
import com.oracle.truffle.api.interop.InteropLibrary;
1414
import com.oracle.truffle.api.interop.TruffleObject;
15-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
15+
import com.oracle.truffle.api.library.CachedLibrary;
1616
import com.oracle.truffle.api.library.ExportLibrary;
1717
import com.oracle.truffle.api.library.ExportMessage;
1818
import com.oracle.truffle.api.library.Message;
1919
import com.oracle.truffle.api.library.ReflectionLibrary;
20+
import org.truffleruby.RubyContext;
21+
import org.truffleruby.language.control.RaiseException;
2022

2123
@ExportLibrary(ReflectionLibrary.class)
2224
public class BoxedValue implements TruffleObject {
@@ -32,11 +34,13 @@ public BoxedValue(Object value) {
3234

3335
@TruffleBoundary
3436
@ExportMessage
35-
protected Object send(Message message, Object[] args) throws Exception {
36-
if (message == READ_MEMBER) {
37-
throw UnsupportedMessageException.create();
38-
} else if (message == INVOKE_MEMBER) {
39-
throw UnsupportedMessageException.create();
37+
protected Object send(Message message, Object[] args,
38+
@CachedLibrary("this") ReflectionLibrary node) throws Exception {
39+
if (message == READ_MEMBER || message == INVOKE_MEMBER) {
40+
RubyContext context = RubyContext.get(node);
41+
throw new RaiseException(context, context.getCoreExceptions().unsupportedMessageError(
42+
"Methods should not be called on a BoxedValue as that would expose the potential Ruby object behind rather than relying on interop messages",
43+
node));
4044
}
4145
ReflectionLibrary reflection = ReflectionLibrary.getFactory().getUncached();
4246
return reflection.send(value, message, args);

0 commit comments

Comments
 (0)