|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2025, Oracle and/or its affiliates. |
| 3 | + * Copyright (c) 2014, Regents of the University of California |
| 4 | + * |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without modification, are |
| 8 | + * permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, this list of |
| 11 | + * conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 13 | + * conditions and the following disclaimer in the documentation and/or other materials provided |
| 14 | + * with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 17 | + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 21 | + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 22 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 23 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 24 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +package com.oracle.graal.python.builtins.objects.foreign; |
| 28 | + |
| 29 | +import com.oracle.graal.python.builtins.Builtin; |
| 30 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 31 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 32 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 33 | +import com.oracle.graal.python.nodes.functi
F438
on.PythonBuiltinBaseNode; |
| 34 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 35 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 36 | +import com.oracle.graal.python.runtime.GilNode; |
| 37 | +import com.oracle.graal.python.runtime.object.PythonObjectFactory; |
| 38 | +import com.oracle.graal.python.util.PythonUtils; |
| 39 | +import com.oracle.truffle.api.CompilerDirectives; |
| 40 | +import com.oracle.truffle.api.dsl.Cached; |
| 41 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 42 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 43 | +import com.oracle.truffle.api.dsl.Specialization; |
| 44 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 45 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 46 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 47 | + |
| 48 | +import java.util.List; |
| 49 | + |
| 50 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___BASES__; |
| 51 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___INSTANCECHECK__; |
| 52 | + |
| 53 | +/* |
| 54 | + * NOTE: We are not using IndirectCallContext here in this file (except for CallNode) |
| 55 | + * because it seems unlikely that these interop messages would call back to Python |
| 56 | + * and that we would also need precise frame info for that case. |
| 57 | + * Adding it shouldn't hurt peak, but might be a non-trivial overhead in interpreter. |
| 58 | + */ |
| 59 | +@CoreFunctions(extendClasses = PythonBuiltinClassType.ForeignAbstractClass) |
| 60 | +public final class ForeignAbstractClassBuiltins extends PythonBuiltins { |
| 61 | + @Override |
| 62 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 63 | + return ForeignAbstractClassBuiltinsFactory.getFactories(); |
| 64 | + } |
| 65 | + |
| 66 | + @Builtin(name = J___BASES__, minNumOfPositionalArgs = 1, isGetter = true, isSetter = false) |
| 67 | + @GenerateNodeFactory |
| 68 | + abstract static class BasesNode extends PythonUnaryBuiltinNode { |
| 69 | + @Specialization |
| 70 | + static Object getBases(Object self, |
| 71 | + @Cached PythonObjectFactory factory) { |
| 72 | + return factory.createTuple(PythonUtils.EMPTY_OBJECT_ARRAY); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Builtin(name = J___INSTANCECHECK__, minNumOfPositionalArgs = 2) |
| 77 | + @GenerateNodeFactory |
| 78 | + abstract static class InstancecheckNode extends PythonBinaryBuiltinNode { |
| 79 | + @Specialization(limit = "3") |
| 80 | + static Object check(Object self, Object instance, |
| 81 | + @CachedLibrary("self") InteropLibrary lib, |
| 82 | + @Cached GilNode gil) { |
| 83 | + gil.release(true); |
| 84 | + try { |
| 85 | + return lib.isMetaInstance(self, instance); |
| 86 | + } catch (UnsupportedMessageException e) { |
| 87 | + throw CompilerDirectives.shouldNotReachHere(); |
| 88 | + } finally { |
| 89 | + gil.acquire(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments