|
| 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 | +} |
0 commit comments