|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2018, Oracle and/or its affiliates. |
| 3 | + * Copyright (c) 2013, 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 | +package com.oracle.graal.python.builtins.objects.memoryview; |
| 27 | + |
| 28 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__; |
| 29 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__; |
| 30 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__; |
| 31 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__; |
| 32 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__; |
| 33 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
| 34 | + |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +import com.oracle.graal.python.builtins.Builtin; |
| 38 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 39 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 40 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 41 | +import com.oracle.graal.python.builtins.objects.ints.PInt; |
| 42 | +import com.oracle.graal.python.nodes.PGuards; |
| 43 | +import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; |
| 44 | +import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; |
| 45 | +import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
| 46 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 47 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 48 | +import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes; |
| 49 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 50 | +import com.oracle.truffle.api.dsl.Cached; |
| 51 | +import com.oracle.truffle.api.dsl.Fallback; |
| 52 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 53 | +import com.oracle.truffle.api.dsl.Specialization; |
| 54 | +import com.oracle.truffle.api.dsl.TypeSystemReference; |
| 55 | + |
| 56 | +@CoreFunctions(extendClasses = PBuffer.class) |
| 57 | +public class BufferBuiltins extends PythonBuiltins { |
| 58 | + |
| 59 | + @Override |
| 60 | + protected List<com.oracle.truffle.api.dsl.NodeFactory<? extends PythonBuiltinNode>> getNodeFactories() { |
| 61 | + return BufferBuiltinsFactory.getFactories(); |
| 62 | + } |
| 63 | + |
| 64 | + @Builtin(name = __REPR__, fixedNumOfArguments = 1) |
| 65 | + @GenerateNodeFactory |
| 66 | + public abstract static class ReprNode extends PythonUnaryBuiltinNode { |
| 67 | + |
| 68 | + @Specialization |
| 69 | + @TruffleBoundary |
| 70 | + public Object repr(PBuffer self, |
| 71 | + @Cached("create(__REPR__)") LookupAndCallUnaryNode repr) { |
| 72 | + return "buffer(" + repr.executeObject(self.getDelegate()) + ")"; |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @Builtin(name = __GETITEM__, fixedNumOfArguments = 2) |
| 78 | + @TypeSystemReference(PythonArithmeticTypes.class) |
| 79 | + @GenerateNodeFactory |
| 80 | + public abstract static class GetItemNode extends PythonBinaryBuiltinNode { |
| 81 | + |
| 82 | + @Specialization |
| 83 | + public Object iter(PBuffer self, boolean key, |
| 84 | + @Cached("create(__GETITEM__)") LookupAndCallBinaryNode callGetItemNode) { |
| 85 | + return callGetItemNode.executeObject(self.getDelegate(), key); |
| 86 | + } |
| 87 | + |
| 88 | + @Specialization |
| 89 | + public Object iter(PBuffer self, int key, |
| 90 | + @Cached("create(__GETITEM__)") LookupAndCallBinaryNode callGetItemNode) { |
| 91 | + return callGetItemNode.executeObject(self.getDelegate(), key); |
| 92 | + } |
| 93 | + |
| 94 | + @Specialization |
| 95 | + public Object iter(PBuffer self, long key, |
| 96 | + @Cached("create(__GETITEM__)") LookupAndCallBinaryNode callGetItemNode) { |
| 97 | + return callGetItemNode.executeObject(self.getDelegate(), key); |
| 98 | + } |
| 99 | + |
| 100 | + @Specialization |
| 101 | + public Object iter(PBuffer self, PInt key, |
| 102 | + @Cached("create(__GETITEM__)") LookupAndCallBinaryNode callGetItemNode) { |
| 103 | + return callGetItemNode.executeObject(self.getDelegate(), key); |
| 104 | + } |
| 105 | + |
| 106 | + @Fallback |
| 107 | + protected Object doGeneric(Object self, Object idx) { |
| 108 | + if (!PGuards.isInteger(idx)) { |
| 109 | + throw raise(TypeError, "buffer indices must be integers, not %p", idx); |
| 110 | + } |
| 111 | + throw raise(TypeError, "descriptor '__getitem__' requires a 'buffer' object but received a '%p'", self); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Builtin(name = __LEN__, fixedNumOfArguments = 1) |
| 116 | + @GenerateNodeFactory |
| 117 | + public abstract static class LenNode extends PythonUnaryBuiltinNode { |
| 118 | + |
| 119 | + @Specialization |
| 120 | + public Object iter(PBuffer self, |
| 121 | + @Cached("create(__LEN__)") LookupAndCallUnaryNode callLenNode) { |
| 122 | + return callLenNode.executeObject(self.getDelegate()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + @Builtin(name = __ITER__, fixedNumOfArguments = 1) |
| 127 | + @GenerateNodeFactory |
| 128 | + public abstract static class IterNode extends PythonBuiltinNode { |
| 129 | + |
| 130 | + @Specialization |
| 131 | + public Object iter(PBuffer self, |
| 132 | + @Cached("create(__ITER__)") LookupAndCallUnaryNode callIterNode) { |
| 133 | + return callIterNode.executeObject(self.getDelegate()); |
| 134 | + } |
| 135 | + |
| 136 | + @Fallback |
| 137 | + Object doGeneric(@SuppressWarnings("unused") Object self) { |
| 138 | + return PNone.NONE; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Builtin(name = __HASH__, fixedNumOfArguments = 1) |
| 143 | + @GenerateNodeFactory |
| 144 | + public abstract static class HashNode extends PythonBuiltinNode { |
| 145 | + @Specialization |
| 146 | + Object doGeneric(Object self) { |
| 147 | + throw raise(TypeError, "unhashable type: '%p'", self); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments