-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 in G/truffleruby from is-a-foreign to master
* commit 'f7e37516f63fbc20e65e433d968d450e8412919c': Tidy up guards Combine specs Run some foreign is_a? in native mode Fixes to is_a? Native guard Foreign is_a? Truffle::Interop.java_instanceof?
- Loading branch information
Showing
10 changed files
with
264 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. This | ||
# code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
# redistribute it and/or modify it under the terms of the: | ||
# | ||
# Eclipse Public License version 1.0, or | ||
# GNU General Public License version 2, or | ||
# GNU Lesser General Public License version 2.1. | ||
|
||
require_relative '../../ruby/spec_helper' | ||
|
||
guard -> { !TruffleRuby.native? } do | ||
describe "Truffle::Interop.java_instanceof?" do | ||
|
||
it "returns true for a directly matching Java object and class" do | ||
big_integer_class = Truffle::Interop.java_type("java.math.BigInteger") | ||
big_integer = big_integer_class.new("14") | ||
Truffle::Interop.java_instanceof?(big_integer, big_integer_class).should be_true | ||
end | ||
|
||
it "returns true for a matching Java object and superclass" do | ||
big_integer_class = Truffle::Interop.java_type("java.math.BigInteger") | ||
big_integer = big_integer_class.new("14") | ||
number_class = Truffle::Interop.java_type("java.lang.Number") | ||
Truffle::Interop.java_instanceof?(big_integer, number_class).should be_true | ||
end | ||
|
||
it "returns true for a matching Java object and interface" do | ||
big_integer_class = Truffle::Interop.java_type("java.math.BigInteger") | ||
big_integer = big_integer_class.new("14") | ||
serializable_interface = Truffle::Interop.java_type("java.io.Serializable") | ||
Truffle::Interop.java_instanceof?(big_integer, serializable_interface).should be_true | ||
end | ||
|
||
it "returns false for an unrelated Java object and Java class" do | ||
big_integer_class = Truffle::Interop.java_type("java.math.BigInteger") | ||
big_integer = big_integer_class.new("14") | ||
big_decimal_class = Truffle::Interop.java_type("java.math.BigDecimal") | ||
Truffle::Interop.java_instanceof?(big_integer, big_decimal_class).should be_false | ||
end | ||
|
||
it "returns false for an unrelated Ruby object and Java class" do | ||
big_integer_class = Truffle::Interop.java_type("java.math.BigInteger") | ||
Truffle::Interop.java_instanceof?(Object.new, big_integer_class).should be_false | ||
end | ||
|
||
it "handles boxing of primitives like Java does" do | ||
integer_class = Truffle::Interop.java_type("java.lang.Integer") | ||
Truffle::Interop.java_instanceof?(14, integer_class).should be_true | ||
double_class = Truffle::Interop.java_type("java.lang.Double") | ||
Truffle::Interop.java_instanceof?(14.2, double_class).should be_true | ||
end | ||
|
||
it "raises a type error if passed something that is not a Java class" do | ||
lambda { Truffle::Interop.java_instanceof?(14, nil) }.should raise_error(TypeError) | ||
lambda { Truffle::Interop.java_instanceof?(14, String) }.should raise_error(TypeError) | ||
lambda { Truffle::Interop.java_instanceof?(14, Truffle::Debug.java_object) }.should raise_error(TypeError) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/org/truffleruby/debug/ForeignObjectMessageResolution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. This | ||
* code is released under a tri EPL/GPL/LGPL license. You can use it, | ||
* redistribute it and/or modify it under the terms of the: | ||
* | ||
* Eclipse Public License version 1.0, or | ||
* GNU General Public License version 2, or | ||
* GNU Lesser General Public License version 2.1. | ||
*/ | ||
package org.truffleruby.debug; | ||
|
||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.interop.CanResolve; | ||
import com.oracle.truffle.api.interop.MessageResolution; | ||
import com.oracle.truffle.api.interop.Resolve; | ||
import com.oracle.truffle.api.interop.TruffleObject; | ||
import com.oracle.truffle.api.nodes.Node; | ||
import org.truffleruby.debug.TruffleDebugNodes.ForeignObjectNode.ForeignObject; | ||
|
||
@MessageResolution(receiverType = ForeignObject.class) | ||
public class ForeignObjectMessageResolution { | ||
|
||
@CanResolve | ||
public abstract static class Check extends Node { | ||
|
||
protected static boolean test(TruffleObject receiver) { | ||
return receiver instanceof ForeignObject; | ||
} | ||
|
||
} | ||
|
||
@Resolve(message = "IS_BOXED") | ||
public static abstract class ForeignIsBoxedNode extends Node { | ||
|
||
protected Object access(VirtualFrame frame, ForeignObject number) { | ||
return false; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters