|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.objects.cext.capi; |
| 42 | + |
| 43 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode; |
| 44 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToSulongNode; |
| 45 | +import com.oracle.graal.python.builtins.objects.cext.common.CArrayWrappers.CStringWrapper; |
| 46 | +import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction; |
| 47 | +import com.oracle.graal.python.builtins.objects.function.PKeyword; |
| 48 | +import com.oracle.graal.python.builtins.objects.ints.PInt; |
| 49 | +import com.oracle.graal.python.runtime.GilNode; |
| 50 | +import com.oracle.graal.python.runtime.interop.InteropArray; |
| 51 | +import com.oracle.graal.python.util.OverflowException; |
| 52 | +import com.oracle.truffle.api.CompilerDirectives; |
| 53 | +import com.oracle.truffle.api.dsl.Cached; |
| 54 | +import com.oracle.truffle.api.dsl.Cached.Exclusive; |
| 55 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 56 | +import com.oracle.truffle.api.interop.InvalidArrayIndexException; |
| 57 | +import com.oracle.truffle.api.interop.UnknownIdentifierException; |
| 58 | +import com.oracle.truffle.api.library.ExportLibrary; |
| 59 | +import com.oracle.truffle.api.library.ExportMessage; |
| 60 | +import com.oracle.truffle.llvm.spi.NativeTypeLibrary; |
| 61 | + |
| 62 | +/** |
| 63 | + * Wrapper object that emulates the ABI for {@code struct wrapperbase} (aka. slot descriptor). |
| 64 | + * |
| 65 | + * <pre> |
| 66 | + * struct wrapperbase { |
| 67 | + * const char *name; |
| 68 | + * int offset; |
| 69 | + * void *function; |
| 70 | + * wrapperfunc wrapper; |
| 71 | + * const char *doc; |
| 72 | + * int flags; |
| 73 | + * PyObject *name_strobj; |
| 74 | + * }; |
| 75 | + * </pre> |
| 76 | + */ |
| 77 | +@ExportLibrary(InteropLibrary.class) |
| 78 | +@ExportLibrary(NativeTypeLibrary.class) |
| 79 | +public final class StructWrapperBaseWrapper extends PythonNativeWrapper { |
| 80 | + public static final String NAME = "name"; |
| 81 | + public static final String OFFSET = "offset"; |
| 82 | + public static final String FUNCTION = "function"; |
| 83 | + public static final String WRAPPER = "wrapper"; |
| 84 | + public static final String DOC = "doc"; |
| 85 | + public static final String FLAGS = "flags"; |
| 86 | + public static final String NAME_STROBJ = "name_strobj"; |
| 87 | + |
| 88 | + public StructWrapperBaseWrapper(PBuiltinFunction delegate) { |
| 89 | + super(delegate); |
| 90 | + } |
| 91 | + |
| 92 | + PBuiltinFunction getBuiltinFunction() { |
| 93 | + return (PBuiltinFunction) getDelegateSlowPath(); |
| 94 | + } |
| 95 | + |
| 96 | + @ExportMessage |
| 97 | + boolean hasMembers() { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + @ExportMessage |
| 102 | + protected Object getMembers(@SuppressWarnings("unused") boolean includeInternal) { |
| 103 | + return new InteropArray(new Object[]{NAME, OFFSET, FUNCTION, WRAPPER, DOC, FLAGS, NAME_STROBJ}); |
| 104 | + } |
| 105 | + |
| 106 | + @ExportMessage |
| 107 | + boolean isMemberReadable(String member) { |
| 108 | + switch (member) { |
| 109 | + case NAME: |
| 110 | + case OFFSET: |
| 111 | + case FUNCTION: |
| 112 | + case WRAPPER: |
| 113 | + case DOC: |
| 114 | + case FLAGS: |
| 115 | + case NAME_STROBJ: |
| 116 | + return true; |
| 117 | + default: |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @ExportMessage |
| 123 | + protected Object readMember(String member, |
| 124 | + @Exclusive @Cached GilNode gil, |
| 125 | + @Cached GetNativeNullNode getNativeNullNode, |
| 126 | + @Cached ToSulongNode toSulongNode) throws UnknownIdentifierException { |
| 127 | + boolean mustRelease = gil.acquire(); |
| 128 | + try { |
| 129 | + switch (member) { |
| 130 | + case NAME: |
| 131 | + return new CStringWrapper(getBuiltinFunction().getName()); |
| 132 | + case OFFSET: |
| 133 | + return 0; |
| 134 | + case FUNCTION: |
| 135 | + PKeyword[] kwDefaults = getBuiltinFunction().getKwDefaults(); |
| 136 | + return ExternalFunctionNodes.getHiddenCallable(kwDefaults); |
| 137 | + case WRAPPER: |
| 138 | + case DOC: |
| 139 | + case NAME_STROBJ: |
| 140 | + // TODO(fa): if ever necessary, provide proper values here |
| 141 | + return toSulongNode.execute(getNativeNullNode.execute()); |
| 142 | + case FLAGS: |
| 143 | + return getBuiltinFunction().getFlags(); |
| 144 | + } |
| 145 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 146 | + throw UnknownIdentifierException.create(member); |
| 147 | + } finally { |
| 148 | + gil.release(mustRelease); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // for memcpy, we also expose this as an interop array |
| 153 | + |
| 154 | + @ExportMessage |
| 155 | + boolean hasArrayElements() { |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + @ExportMessage |
| 160 | + long getArraySize() { |
| 161 | + return 7 * Long.BYTES; |
| 162 | + } |
| 163 | + |
| 164 | + @ExportMessage |
| 165 | + boolean isArrayElementReadable(long index) { |
| 166 | + return 0 <= index && index < 7; |
| 167 | + } |
| 168 | + |
| 169 | + @ExportMessage |
| 170 | + Object readArrayElement(long index, |
| 171 | + @Exclusive @Cached GilNode gil, |
| 172 | + @Cached GetNativeNullNode getNativeNullNode, |
| 173 | + @Cached ToSulongNode toSulongNode) throws InvalidArrayIndexException { |
| 174 | + boolean mustRelease = gil.acquire(); |
| 175 | + try { |
| 176 | + int i = PInt.intValueExact(index); |
| 177 | + switch (i) { |
| 178 | + case 0: |
| 179 | + return new CStringWrapper(getBuiltinFunction().getName()); |
| 180 | + case Long.BYTES: |
| 181 | + return 0; |
| 182 | + case 2 * Long.BYTES: |
| 183 | + PKeyword[] kwDefaults = getBuiltinFunction().getKwDefaults(); |
| 184 | + return ExternalFunctionNodes.getHiddenCallable(kwDefaults); |
| 185 | + case 3 * Long.BYTES: |
| 186 | + case 4 * Long.BYTES: |
| 187 | + case 6 * Long.BYTES: |
| 188 | + // TODO(fa): if ever necessary, provide proper values here |
| 189 | + return toSulongNode.execute(getNativeNullNode.execute()); |
| 190 | + case 5 * Long.BYTES: |
| 191 | + return getBuiltinFunction().getFlags(); |
| 192 | + } |
| 193 | + // fall through |
| 194 | + } catch (OverflowException e) { |
| 195 | + // fall through |
| 196 | + } finally { |
| 197 | + gil.release(mustRelease); |
| 198 | + } |
| 199 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 200 | + throw InvalidArrayIndexException.create(index); |
| 201 | + } |
| 202 | + |
| 203 | + @ExportMessage |
| 204 | + @SuppressWarnings("static-method") |
| 205 | + protected boolean hasNativeType() { |
| 206 | + // TODO implement native type |
| 207 | + return false; |
| 208 | + } |
| 209 | + |
| 210 | + @ExportMessage |
| 211 | + @SuppressWarnings("static-method") |
| 212 | + public Object getNativeType() { |
| 213 | + // TODO implement native type |
| 214 | + return null; |
| 215 | + } |
| 216 | +} |
0 commit comments