Skip to content

Commit f89c33a

Browse files
committed
[GR-25085] Update Truffle to profile arguments of IndirectCallNode
PullRequest: truffleruby/2420
2 parents 3d8f768 + 7bd3a98 commit f89c33a

File tree

6 files changed

+117
-12
lines changed

6 files changed

+117
-12
lines changed

mx.truffleruby/suite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{
88
"name": "regex",
99
"subdir": True,
10-
"version": "95a9dddea10b7a8704e66aa0050d49ce354e6ae6",
10+
"version": "8c1abd10ad2992580462d7629db316177a256f82",
1111
"urls": [
1212
{"url": "https://github.com/oracle/graal.git", "kind" : "git"},
1313
{"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind" : "binary"},
@@ -16,7 +16,7 @@
1616
{
1717
"name": "sulong",
1818
"subdir": True,
19-
"version": "95a9dddea10b7a8704e66aa0050d49ce354e6ae6",
19+
"version": "8c1abd10ad2992580462d7629db316177a256f82",
2020
"urls": [
2121
{"url": "https://github.com/oracle/graal.git", "kind": "git"},
2222
{"url": "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind": "binary"},

spec/truffle/interop/iterator_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 2.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
require_relative 'fixtures/classes'
11+
12+
describe "Truffle::Interop iterator messages" do
13+
it "allow to iterate an array" do
14+
obj = [1, 2]
15+
Truffle::Interop.should.has_iterator?(obj)
16+
iterator = Truffle::Interop.iterator(obj)
17+
Truffle::Interop.should.iterator?(iterator)
18+
19+
Truffle::Interop.has_iterator_next_element?(iterator).should == true
20+
Truffle::Interop.iterator_next_element(iterator).should == 1
21+
Truffle::Interop.has_iterator_next_element?(iterator).should == true
22+
Truffle::Interop.iterator_next_element(iterator).should == 2
23+
Truffle::Interop.has_iterator_next_element?(iterator).should == false
24+
-> { Truffle::Interop.iterator_next_element(iterator) }.should raise_error(StopIteration)
25+
end
26+
end

src/main/java/org/truffleruby/core/CoreLibrary.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public class CoreLibrary {
161161
public final RubyClass systemStackErrorClass;
162162
public final RubyClass securityErrorClass;
163163
public final RubyClass standardErrorClass;
164+
public final RubyClass stopIterationClass;
164165
public final RubyModule polyglotModule;
165166
public final RubyClass unsupportedMessageErrorClass;
166167
public final RubyClass stringClass;
@@ -360,7 +361,7 @@ public CoreLibrary(RubyContext context, RubyLanguage language) {
360361
// StandardError > IndexError
361362
indexErrorClass = defineClass(standardErrorClass, "IndexError");
362363
keyErrorClass = defineClass(indexErrorClass, "KeyError");
363-
RubyClass stopIterationClass = defineClass(indexErrorClass, "StopIteration");
364+
stopIterationClass = defineClass(indexErrorClass, "StopIteration");
364365
closedQueueErrorClass = defineClass(stopIterationClass, "ClosedQueueError");
365366

366367
// StandardError > IOError

src/main/java/org/truffleruby/core/exception/CoreExceptions.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,6 @@ public RubyException argumentError(Rope message, Node currentNode, Throwable jav
254254
javaThrowable);
255255
}
256256

257-
public RubyException argumentErrorInvalidBigDecimal(String string, Node currentNode) {
258-
return argumentError(StringUtils.format("invalid value for BigDecimal(): \"%s\"", string), currentNode);
259-
}
260-
261257
@TruffleBoundary
262258
public RubyException argumentErrorCantUnfreeze(Object self, Node currentNode) {
263259
String className = LogicalClassNode.getUncached().execute(self).fields.getName();
@@ -451,6 +447,16 @@ public RubyException indexErrorInvalidBufferOffsetException(InvalidBufferOffsetE
451447
return indexError("invalid buffer offset " + exception.getByteOffset(), currentNode);
452448
}
453449

450+
// StopIteration
451+
452+
@TruffleBoundary
453+
public RubyException stopIteration(String message, Node currentNode) {
454+
RubyClass exceptionClass = context.getCoreLibrary().stopIterationClass;
455+
RubyString errorMessage = StringOperations
456+
.createString(context, language, StringOperations.encodeRope(message, UTF8Encoding.INSTANCE));
457+
return ExceptionOperations.createRubyException(context, exceptionClass, errorMessage, currentNode, null);
458+
}
459+
454460
// LocalJumpError
455461

456462
@TruffleBoundary

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

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,10 +2097,9 @@ protected Object getTopScope(
20972097
return context.getTopScopeObject();
20982098
}
20992099
}
2100-
// endregion scope
2100+
// endregion
21012101

21022102
// region Buffer Messages
2103-
21042103
@CoreMethod(names = "has_buffer_elements?", onSingleton = true, required = 1)
21052104
public abstract static class HasBufferElementsNode extends InteropCoreMethodArrayArgumentsNode {
21062105

@@ -2438,7 +2437,68 @@ protected Object writeBufferDouble(Object receiver, ByteOrder order, long byteOf
24382437
}
24392438

24402439
}
2440+
// endregion
2441+
2442+
// region Iterator
2443+
@CoreMethod(names = "has_iterator?", onSingleton = true, required = 1)
2444+
public abstract static class HasIteratorNode extends InteropCoreMethodArrayArgumentsNode {
2445+
@Specialization(limit = "getCacheLimit()")
2446+
protected boolean hasIterator(Object receiver,
2447+
@CachedLibrary("receiver") InteropLibrary interop) {
2448+
return interop.hasIterator(receiver);
2449+
}
2450+
}
2451+
2452+
@CoreMethod(names = "iterator?", onSingleton = true, required = 1)
2453+
public abstract static class IsIteratorNode extends InteropCoreMethodArrayArgumentsNode {
2454+
@Specialization(limit = "getCacheLimit()")
2455+
protected boolean isIterator(Object receiver,
2456+
@CachedLibrary("receiver") InteropLibrary interop) {
2457+
return interop.isIterator(receiver);
2458+
}
2459+
}
24412460

2442-
// endregion Buffer Messages
2461+
@CoreMethod(names = "iterator", onSingleton = true, required = 1)
2462+
public abstract static class GetIteratorNode extends InteropCoreMethodArrayArgumentsNode {
2463+
@Specialization(limit = "getCacheLimit()")
2464+
protected Object getIterator(Object receiver,
2465+
@CachedLibrary("receiver") InteropLibrary interop,
2466+
@Cached TranslateInteropExceptionNode translateInteropException) {
2467+
try {
2468+
return interop.getIterator(receiver);
2469+
} catch (UnsupportedMessageException e) {
2470+
throw translateInteropException.execute(e);
2471+
}
2472+
}
2473+
}
2474+
2475+
@CoreMethod(names = "has_iterator_next_element?", onSingleton = true, required = 1)
2476+
public abstract static class HasIteratorNextElementNode extends InteropCoreMethodArrayArgumentsNode {
2477+
@Specialization(limit = "getCacheLimit()")
2478+
protected boolean hasIteratorNextElement(Object receiver,
2479+
@CachedLibrary("receiver") InteropLibrary interop,
2480+
@Cached TranslateInteropExceptionNode translateInteropException) {
2481+
try {
2482+
return interop.hasIteratorNextElement(receiver);
2483+
} catch (UnsupportedMessageException e) {
2484+
throw translateInteropException.execute(e);
2485+
}
2486+
}
2487+
}
2488+
2489+
@CoreMethod(names = "iterator_next_element", onSingleton = true, required = 1)
2490+
public abstract static class GetIteratorNextElementNode extends InteropCoreMethodArrayArgumentsNode {
2491+
@Specialization(limit = "getCacheLimit()")
2492+
protected Object getIteratorNextElement(Object receiver,
2493+
@CachedLibrary("receiver") InteropLibrary interop,
2494+
@Cached TranslateInteropExceptionNode translateInteropException) {
2495+
try {
2496+
return interop.getIteratorNextElement(receiver);
2497+
} catch (InteropException e) {
2498+
throw translateInteropException.execute(e);
2499+
}
2500+
}
2501+
}
2502+
// endregion
24432503

24442504
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
package org.truffleruby.interop;
1111

1212
import com.oracle.truffle.api.interop.InvalidBufferOffsetException;
13+
import com.oracle.truffle.api.interop.StopIterationException;
1314
import org.truffleruby.RubyContext;
1415
import org.truffleruby.RubyLanguage;
1516
import org.truffleruby.language.RubyBaseNode;
1617
import org.truffleruby.language.control.RaiseException;
1718

18-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1919
import com.oracle.truffle.api.dsl.CachedContext;
2020
import com.oracle.truffle.api.dsl.GenerateUncached;
2121
import com.oracle.truffle.api.dsl.Specialization;
@@ -111,7 +111,6 @@ protected RuntimeException handle(
111111
}
112112
}
113113

114-
@TruffleBoundary // Throwable#initCause
115114
@Specialization
116115
protected RuntimeException handle(
117116
UnsupportedTypeException exception,
@@ -137,4 +136,17 @@ protected RuntimeException handle(ArityException exception, boolean inInvokeMemb
137136
exception);
138137
}
139138

139+
@Specialization
140+
protected RuntimeException handle(
141+
StopIterationException exception,
142+
boolean inInvokeMember,
143+
Object receiver,
144+
Object[] args,
145+
@CachedContext(RubyLanguage.class) RubyContext context) {
146+
return new RaiseException(
147+
context,
148+
context.getCoreExceptions().stopIteration(exception.getMessage(), this),
149+
exception);
150+
}
151+
140152
}

0 commit comments

Comments
 (0)