|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * The Universal Permissive License (UPL), Version 1.0 |
| 5 | + * |
| 6 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 7 | + * person obtaining a copy of this software, associated documentation and/or data |
| 8 | + * (collectively the "Software"), free of charge and under any and all copyright |
| 9 | + * rights in the Software, and any and all patent rights owned or freely |
| 10 | + * licensable by each licensor hereunder covering either (i) the unmodified |
| 11 | + * Software as contributed to or provided by such licensor, or (ii) the Larger |
| 12 | + * Works (as defined below), to deal in both |
| 13 | + * |
| 14 | + * (a) the Software, and |
| 15 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 16 | + * one is included with the Software (each a "Larger Work" to which the |
| 17 | + * Software is contributed by such licensors), |
| 18 | + * |
| 19 | + * without restriction, including without limitation the rights to copy, create |
| 20 | + * derivative works of, display, perform, and distribute the Software and make, |
| 21 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 22 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 23 | + * either these or other terms. |
| 24 | + * |
| 25 | + * This license is subject to the following condition: |
| 26 | + * |
| 27 | + * The above copyright notice and either this complete permission notice or at a |
| 28 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 29 | + * portions of the Software. |
| 30 | + * |
| 31 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 32 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 33 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 34 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 35 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 36 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 37 | + * SOFTWARE. |
| 38 | + */ |
| 39 | +package com.oracle.graal.python.builtins.objects.cext; |
| 40 | + |
| 41 | +import com.oracle.graal.python.builtins.objects.PythonAbstractObject; |
| 42 | +import com.oracle.graal.python.builtins.objects.type.PythonClass; |
| 43 | +import com.oracle.truffle.api.interop.ForeignAccess; |
| 44 | +import com.oracle.truffle.api.interop.TruffleObject; |
| 45 | + |
| 46 | +public abstract class NativeWrappers { |
| 47 | + public abstract static class PythonNativeWrapper implements TruffleObject { |
| 48 | + |
| 49 | + private Object nativePointer; |
| 50 | + |
| 51 | + public abstract Object getDelegate(); |
| 52 | + |
| 53 | + public Object getNativePointer() { |
| 54 | + return nativePointer; |
| 55 | + } |
| 56 | + |
| 57 | + public void setNativePointer(Object nativePointer) { |
| 58 | + // we should set the pointer just once |
| 59 | + assert this.nativePointer == null || this.nativePointer.equals(nativePointer); |
| 60 | + this.nativePointer = nativePointer; |
| 61 | + } |
| 62 | + |
| 63 | + public boolean isNative() { |
| 64 | + return nativePointer != null; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public ForeignAccess getForeignAccess() { |
| 69 | + return PythonObjectNativeWrapperMRForeign.ACCESS; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Used to wrap {@link PythonAbstractObject} when used in native code. This wrapper mimics the |
| 75 | + * correct shape of the corresponding native type {@code struct _object}. |
| 76 | + */ |
| 77 | + public static class PythonObjectNativeWrapper extends PythonNativeWrapper { |
| 78 | + private final PythonAbstractObject pythonObject; |
| 79 | + |
| 80 | + public PythonObjectNativeWrapper(PythonAbstractObject object) { |
| 81 | + this.pythonObject = object; |
| 82 | + } |
| 83 | + |
| 84 | + public PythonAbstractObject getPythonObject() { |
| 85 | + return pythonObject; |
| 86 | + } |
| 87 | + |
| 88 | + public static boolean isInstance(TruffleObject o) { |
| 89 | + return o instanceof PythonObjectNativeWrapper; |
| 90 | + } |
| 91 | + |
| 92 | + public static PythonObjectNativeWrapper wrap(PythonAbstractObject obj) { |
| 93 | + // important: native wrappers are cached |
| 94 | + PythonObjectNativeWrapper nativeWrapper = obj.getNativeWrapper(); |
| 95 | + if (nativeWrapper == null) { |
| 96 | + nativeWrapper = new PythonObjectNativeWrapper(obj); |
| 97 | + obj.setNativeWrapper(nativeWrapper); |
| 98 | + } |
| 99 | + return nativeWrapper; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Object getDelegate() { |
| 104 | + return pythonObject; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Used to wrap {@link PythonClass} when used in native code. This wrapper mimics the correct |
| 110 | + * shape of the corresponding native type {@code struct _typeobject}. |
| 111 | + */ |
| 112 | + public static class PythonClassNativeWrapper extends PythonObjectNativeWrapper { |
| 113 | + private final CStringWrapper nameWrapper; |
| 114 | + private Object getBufferProc; |
| 115 | + private Object releaseBufferProc; |
| 116 | + |
| 117 | + public PythonClassNativeWrapper(PythonClass object) { |
| 118 | + super(object); |
| 119 | + this.nameWrapper = new CStringWrapper(object.getName()); |
| 120 | + } |
| 121 | + |
| 122 | + public CStringWrapper getNameWrapper() { |
| 123 | + return nameWrapper; |
| 124 | + } |
| 125 | + |
| 126 | + public Object getGetBufferProc() { |
| 127 | + return getBufferProc; |
| 128 | + } |
| 129 | + |
| 130 | + public void setGetBufferProc(Object getBufferProc) { |
| 131 | + this.getBufferProc = getBufferProc; |
| 132 | + } |
| 133 | + |
| 134 | + public Object getReleaseBufferProc() { |
| 135 | + return releaseBufferProc; |
| 136 | + } |
| 137 | + |
| 138 | + public void setReleaseBufferProc(Object releaseBufferProc) { |
| 139 | + this.releaseBufferProc = releaseBufferProc; |
| 140 | + } |
| 141 | + |
| 142 | + public static PythonClassNativeWrapper wrap(PythonClass obj) { |
| 143 | + // important: native wrappers are cached |
| 144 | + PythonClassNativeWrapper nativeWrapper = obj.getNativeWrapper(); |
| 145 | + if (nativeWrapper == null) { |
| 146 | + nativeWrapper = new PythonClassNativeWrapper(obj); |
| 147 | + obj.setNativeWrapper(nativeWrapper); |
| 148 | + } |
| 149 | + return nativeWrapper; |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Wraps a sequence object (like a list) such that it behaves like a bare C array. |
| 156 | + */ |
| 157 | + public static class PySequenceArrayWrapper extends PythonNativeWrapper { |
| 158 | + |
| 159 | + private final Object delegate; |
| 160 | + |
| 161 | + public PySequenceArrayWrapper(Object delegate) { |
| 162 | + this.delegate = delegate; |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public Object getDelegate() { |
| 167 | + return delegate; |
| 168 | + } |
| 169 | + |
| 170 | + static boolean isInstance(TruffleObject o) { |
| 171 | + return o instanceof PySequenceArrayWrapper; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public ForeignAccess getForeignAccess() { |
| 176 | + return PySequenceArrayWrapperMRForeign.ACCESS; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public static class TruffleObjectNativeWrapper extends PythonNativeWrapper { |
| 181 | + private final TruffleObject foreignObject; |
| 182 | + |
| 183 | + public TruffleObjectNativeWrapper(TruffleObject foreignObject) { |
| 184 | + this.foreignObject = foreignObject; |
| 185 | + } |
| 186 | + |
| 187 | + public TruffleObject getForeignObject() { |
| 188 | + return foreignObject; |
| 189 | + } |
| 190 | + |
| 191 | + public static TruffleObjectNativeWrapper wrap(TruffleObject foreignObject) { |
| 192 | + return new TruffleObjectNativeWrapper(foreignObject); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public Object getDelegate() { |
| 197 | + return foreignObject; |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments