Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 423da52

Browse files
committed
[Truffle] Fix last Array language spec!
1 parent 6f49dd7 commit 423da52

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

core/src/main/java/org/jruby/truffle/nodes/cast/SplatCastNode.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.oracle.truffle.api.dsl.*;
1515
import com.oracle.truffle.api.frame.*;
1616
import org.jruby.truffle.nodes.*;
17+
import org.jruby.truffle.nodes.core.ArrayDupNode;
18+
import org.jruby.truffle.nodes.core.ArrayDupNodeFactory;
1719
import org.jruby.truffle.nodes.dispatch.Dispatch;
1820
import org.jruby.truffle.nodes.dispatch.DispatchHeadNode;
1921
import org.jruby.truffle.runtime.*;
@@ -34,6 +36,7 @@ public static enum NilBehavior {
3436

3537
private final NilBehavior nilBehavior;
3638

39+
@Child protected ArrayDupNode dup;
3740
@Child protected DispatchHeadNode respondToToA;
3841
@Child protected BooleanCastNode respondToCast;
3942
@Child protected DispatchHeadNode toA;
@@ -42,13 +45,15 @@ public SplatCastNode(RubyContext context, SourceSection sourceSection, NilBehavi
4245
super(context, sourceSection);
4346
this.nilBehavior = nilBehavior;
4447
// Calling private #to_a is allowed for the *splat operator.
48+
dup = ArrayDupNodeFactory.create(context, sourceSection, null);
4549
respondToToA = new DispatchHeadNode(context, true, Dispatch.MissingBehavior.RETURN_MISSING);
4650
respondToCast = BooleanCastNodeFactory.create(context, sourceSection, null);
4751
toA = new DispatchHeadNode(context, true, Dispatch.MissingBehavior.RETURN_MISSING);
4852
}
4953

5054
public SplatCastNode(SplatCastNode prev) {
5155
super(prev);
56+
dup = prev.dup;
5257
nilBehavior = prev.nilBehavior;
5358
respondToToA = prev.respondToToA;
5459
respondToCast = prev.respondToCast;
@@ -74,8 +79,10 @@ public RubyArray splat(RubyNilClass nil) {
7479
}
7580

7681
@Specialization
77-
public RubyArray splat(RubyArray array) {
78-
return array;
82+
public RubyArray splat(VirtualFrame frame, RubyArray array) {
83+
// TODO(cs): is it necessary to dup here in all cases?
84+
// It is needed at least for [*ary] (translated as just a SplatNode) and b = *ary.
85+
return dup.executeDup(frame, array);
7986
}
8087

8188
@Specialization(guards = {"!isRubyNilClass", "!isRubyArray"})
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2014 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 1.0
7+
* GNU General Public License version 2
8+
* GNU Lesser General Public License version 2.1
9+
*/
10+
package org.jruby.truffle.nodes.core;
11+
12+
import com.oracle.truffle.api.dsl.ImportGuards;
13+
import com.oracle.truffle.api.dsl.NodeChild;
14+
import com.oracle.truffle.api.dsl.NodeChildren;
15+
import com.oracle.truffle.api.dsl.Specialization;
16+
import com.oracle.truffle.api.frame.VirtualFrame;
17+
import com.oracle.truffle.api.source.SourceSection;
18+
import org.jruby.truffle.nodes.RubyNode;
19+
import org.jruby.truffle.runtime.RubyContext;
20+
import org.jruby.truffle.runtime.core.RubyArray;
21+
22+
import java.util.Arrays;
23+
24+
/**
25+
* Dup an array, without using any method lookup. This isn't a call - it's an operation on a core class.
26+
*/
27+
@NodeChildren({@NodeChild(value = "array", type = RubyNode.class)})
28+
@ImportGuards(ArrayGuards.class)
29+
public abstract class ArrayDupNode extends RubyNode {
30+
31+
public ArrayDupNode(RubyContext context, SourceSection sourceSection) {
32+
super(context, sourceSection);
33+
}
34+
35+
public ArrayDupNode(ArrayDupNode prev) {
36+
super(prev);
37+
}
38+
39+
public abstract RubyArray executeDup(VirtualFrame frame, RubyArray array);
40+
41+
@Specialization(guards = "isNull")
42+
public RubyArray dupNull(RubyArray from) {
43+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), null, 0);
44+
}
45+
46+
@Specialization(guards = "isIntegerFixnum")
47+
public RubyArray dupIntegerFixnum(RubyArray from) {
48+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((int[]) from.getStore(), from.getSize()), from.getSize());
49+
}
50+
51+
@Specialization(guards = "isLongFixnum")
52+
public RubyArray dupLongFixnum(RubyArray from) {
53+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((long[]) from.getStore(), from.getSize()), from.getSize());
54+
}
55+
56+
@Specialization(guards = "isFloat")
57+
public RubyArray dupFloat(RubyArray from) {
58+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((double[]) from.getStore(), from.getSize()), from.getSize());
59+
}
60+
61+
@Specialization(guards = "isObject")
62+
public RubyArray dupObject(RubyArray from) {
63+
return new RubyArray(getContext().getCoreLibrary().getArrayClass(), Arrays.copyOf((Object[]) from.getStore(), from.getSize()), from.getSize());
64+
}
65+
66+
}

spec/truffle/tags/language/array_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)