Skip to content

Commit 6473334

Browse files
committed
Implement hasMetaParents/getMetaParents for RubyClass
1 parent 9900266 commit 6473334

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

spec/truffle/interop/meta_object_spec.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
require_relative '../../ruby/spec_helper'
1010

1111
describe "Truffle::Interop.meta_object" do
12-
1312
it "returns Integer class for an integer" do
1413
Truffle::Interop.meta_object(14).should == Integer
1514
end
@@ -30,4 +29,15 @@
3029
Truffle::Interop.meta_object([1, 2, 3]).should == Array
3130
end
3231

32+
it "returns a Ruby class implementing all meta objects methods" do
33+
meta = Truffle::Interop.meta_object("string")
34+
Truffle::Interop.meta_simple_name(meta).should == 'String'
35+
Truffle::Interop.meta_qualified_name(meta).should == 'String'
36+
Truffle::Interop.meta_parents(meta).to_a.should == [Object]
37+
38+
Truffle::Interop.meta_simple_name(Enumerator::Lazy).should == 'Lazy'
39+
Truffle::Interop.meta_qualified_name(Enumerator::Lazy).should == 'Enumerator::Lazy'
40+
41+
Truffle::Interop.should_not.has_meta_parents?(Enumerable)
42+
end
3343
end

src/main/java/org/truffleruby/core/klass/RubyClass.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
import java.util.Set;
1414

1515
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
16+
import com.oracle.truffle.api.interop.InteropLibrary;
17+
import com.oracle.truffle.api.library.ExportLibrary;
18+
import com.oracle.truffle.api.library.ExportMessage;
1619
import com.oracle.truffle.api.source.SourceSection;
1720
import org.truffleruby.RubyLanguage;
1821
import org.truffleruby.core.module.RubyModule;
22+
import org.truffleruby.debug.SingleElementArray;
1923
import org.truffleruby.language.RubyDynamicObject;
2024
import org.truffleruby.language.objects.ObjectGraph;
2125
import org.truffleruby.language.objects.ObjectGraphNode;
@@ -24,6 +28,7 @@
2428

2529
import static org.truffleruby.language.RubyBaseNode.nil;
2630

31+
@ExportLibrary(InteropLibrary.class)
2732
public final class RubyClass extends RubyModule implements ObjectGraphNode {
2833

2934
private static final RubyClass[] EMPTY_CLASS_ARRAY = new RubyClass[0];
@@ -118,4 +123,16 @@ public void getAdjacentObjects(Set<Object> reachable) {
118123
ObjectGraph.addProperty(reachable, superclass);
119124
}
120125

126+
// region MetaObject
127+
@ExportMessage
128+
public boolean hasMetaParents() {
129+
return true;
130+
}
131+
132+
@ExportMessage
133+
public Object getMetaParents() {
134+
return new SingleElementArray(superclass);
135+
}
136+
// endregion
137+
121138
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
package org.truffleruby.debug;
11+
12+
import com.oracle.truffle.api.dsl.Cached;
13+
import com.oracle.truffle.api.interop.InteropLibrary;
14+
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
15+
import com.oracle.truffle.api.interop.TruffleObject;
16+
import com.oracle.truffle.api.library.ExportLibrary;
17+
import com.oracle.truffle.api.library.ExportMessage;
18+
import com.oracle.truffle.api.profiles.BranchProfile;
19+
20+
@ExportLibrary(InteropLibrary.class)
21+
public class SingleElementArray implements TruffleObject {
22+
23+
private final Object element;
24+
25+
public SingleElementArray(Object element) {
26+
this.element = element;
27+
}
28+
29+
@ExportMessage
30+
protected boolean hasArrayElements() {
31+
return true;
32+
}
33+
34+
@ExportMessage
35+
protected long getArraySize() {
36+
return 1;
37+
}
38+
39+
@ExportMessage
40+
protected Object readArrayElement(long index,
41+
@Cached BranchProfile errorProfile) throws InvalidArrayIndexException {
42+
if (isArrayElementReadable(index)) {
43+
return element;
44+
} else {
45+
errorProfile.enter();
46+
throw InvalidArrayIndexException.create(index);
47+
}
48+
}
49+
50+
@ExportMessage
51+
protected boolean isArrayElementReadable(long index) {
52+
return index == 0;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)