|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 3 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 3 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 3 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package com.oracle.truffle.r.runtime.data; |
| 24 | + |
| 25 | +import com.oracle.truffle.api.dsl.Cached; |
| 26 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
| 27 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 28 | +import com.oracle.truffle.api.interop.InvalidArrayIndexException; |
| 29 | +import com.oracle.truffle.api.interop.TruffleObject; |
| 30 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 31 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 32 | +import com.oracle.truffle.api.library.ExportLibrary; |
| 33 | +import com.oracle.truffle.api.library.ExportMessage; |
| 34 | +import com.oracle.truffle.api.profiles.LoopConditionProfile; |
| 35 | +import com.oracle.truffle.r.runtime.RInternalError; |
| 36 | +import com.oracle.truffle.r.runtime.RType; |
| 37 | +import com.oracle.truffle.r.runtime.data.VectorDataLibrary.RandomAccessIterator; |
| 38 | +import com.oracle.truffle.r.runtime.data.VectorDataLibrary.SeqIterator; |
| 39 | +import com.oracle.truffle.r.runtime.interop.Foreign2R; |
| 40 | +import com.oracle.truffle.r.runtime.ops.na.NACheck; |
| 41 | + |
| 42 | +@ExportLibrary(VectorDataLibrary.class) |
| 43 | +class RListForeignObjData implements TruffleObject { |
| 44 | + protected final Object foreign; |
| 45 | + |
| 46 | + RListForeignObjData(Object foreign) { |
| 47 | + this.foreign = foreign; |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings("static-method") |
| 51 | + @ExportMessage |
| 52 | + public final RType getType() { |
| 53 | + return RType.List; |
| 54 | + } |
| 55 | + |
| 56 | + @SuppressWarnings("static-method") |
| 57 | + @ExportMessage |
| 58 | + public NACheck getNACheck() { |
| 59 | + return NACheck.getEnabled(); |
| 60 | + } |
| 61 | + |
| 62 | + @ExportMessage |
| 63 | + public int getLength(@CachedLibrary("this.foreign") InteropLibrary interop) { |
| 64 | + try { |
| 65 | + long result = interop.getArraySize(foreign); |
| 66 | + return (int) result; |
| 67 | + } catch (UnsupportedMessageException e) { |
| 68 | + throw RInternalError.shouldNotReachHere(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @ExportMessage |
| 73 | + public Object[] materialize(@CachedLibrary("this.foreign") InteropLibrary interop, |
| 74 | + @Shared("foreign2R") @Cached Foreign2R foreign2R) { |
| 75 | + return getListDataCopy(interop, foreign2R); |
| 76 | + } |
| 77 | + |
| 78 | + @ExportMessage |
| 79 | + public boolean isWriteable() { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + @ExportMessage |
| 84 | + public RListForeignObjData copy(@SuppressWarnings("unused") boolean deep) { |
| 85 | + return new RListForeignObjData(foreign); |
| 86 | + } |
| 87 | + |
| 88 | + @ExportMessage |
| 89 | + @SuppressWarnings("unused") |
| 90 | + public RListForeignObjData copyResized(int newSize, boolean deep, boolean fillNA) { |
| 91 | + throw RInternalError.shouldNotReachHere("this method will be removed"); |
| 92 | + } |
| 93 | + |
| 94 | + @ExportMessage |
| 95 | + public Object[] getListDataCopy(@CachedLibrary("this.foreign") InteropLibrary interop, |
| 96 | + @Shared("foreign2R") @Cached Foreign2R foreign2R) { |
| 97 | + int length = getLength(interop); |
| 98 | + Object[] result = new Object[length]; |
| 99 | + Object foreignObj = foreign; |
| 100 | + for (int i = 0; i < length; i++) { |
| 101 | + result[i] = getElementAtImpl(foreignObj, i, interop, foreign2R); |
| 102 | + } |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + @ExportMessage |
| 107 | + public SeqIterator iterator(@Shared("SeqItLoopProfile") @Cached("createCountingProfile()") LoopConditionProfile loopProfile, |
| 108 | + @CachedLibrary("this.foreign") InteropLibrary interop) { |
| 109 | + SeqIterator it = new SeqIterator(foreign, getLength(interop)); |
| 110 | + it.initLoopConditionProfile(loopProfile); |
| 111 | + return it; |
| 112 | + } |
| 113 | + |
| 114 | + @ExportMessage |
| 115 | + public boolean next(SeqIterator it, boolean withWrap, |
| 116 | + @Shared("SeqItLoopProfile") @Cached("createCountingProfile()") LoopConditionProfile loopProfile) { |
| 117 | + return it.next(loopProfile, withWrap); |
| 118 | + } |
| 119 | + |
| 120 | + @ExportMessage |
| 121 | + public RandomAccessIterator randomAccessIterator() { |
| 122 | + return new RandomAccessIterator(foreign); |
| 123 | + } |
| 124 | + |
| 125 | + @ExportMessage |
| 126 | + public Object getElementAt(int index, |
| 127 | + @CachedLibrary("this.foreign") InteropLibrary interop, |
| 128 | + @Shared("foreign2R") @Cached Foreign2R foreign2R) { |
| 129 | + return getElementAtImpl(foreign, index, interop, foreign2R); |
| 130 | + } |
| 131 | + |
| 132 | + @ExportMessage |
| 133 | + public Object getNextElement(SeqIterator it, |
| 134 | + @CachedLibrary("this.foreign") InteropLibrary interop, |
| 135 | + @Shared("foreign2R") @Cached Foreign2R foreign2R) { |
| 136 | + return getElementAtImpl(it.getStore(), it.getIndex(), interop, foreign2R); |
| 137 | + } |
| 138 | + |
| 139 | + @ExportMessage |
| 140 | + public Object getElement(@SuppressWarnings("unused") RandomAccessIterator it, int index, |
| 141 | + @CachedLibrary("this.foreign") InteropLibrary interop, |
| 142 | + @Shared("foreign2R") @Cached Foreign2R foreign2R) { |
| 143 | + return getElementAtImpl(it.getStore(), index, interop, foreign2R); |
| 144 | + } |
| 145 | + |
| 146 | + private static Object getElementAtImpl(Object foreign, int index, InteropLibrary interop, Foreign2R foreign2R) { |
| 147 | + try { |
| 148 | + return foreign2R.convert(interop.readArrayElement(foreign, index)); |
| 149 | + } catch (UnsupportedMessageException | InvalidArrayIndexException e) { |
| 150 | + throw RInternalError.shouldNotReachHere(e); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments