|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * The Universal Permissive License (UPL), Version 1.0
|
|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.objects.method;
|
42 | 42 |
|
| 43 | +import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__; |
43 | 44 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT__;
|
44 | 45 | import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FUNC__;
|
45 | 46 |
|
|
48 | 49 | import com.oracle.graal.python.builtins.Builtin;
|
49 | 50 | import com.oracle.graal.python.builtins.CoreFunctions;
|
50 | 51 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
| 52 | +import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError; |
51 | 53 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
52 | 54 | import com.oracle.graal.python.builtins.objects.PNone;
|
| 55 | +import com.oracle.graal.python.builtins.objects.common.PHashingCollection; |
| 56 | +import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; |
| 57 | +import com.oracle.graal.python.nodes.ErrorMessages; |
| 58 | +import com.oracle.graal.python.nodes.PGuards; |
53 | 59 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
|
54 | 60 | import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
|
55 | 61 | import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
|
| 62 | +import com.oracle.graal.python.runtime.object.PythonObjectFactory; |
| 63 | +import com.oracle.truffle.api.CompilerDirectives; |
| 64 | +import com.oracle.truffle.api.dsl.Cached; |
56 | 65 | import com.oracle.truffle.api.dsl.GenerateNodeFactory;
|
| 66 | +import com.oracle.truffle.api.dsl.ImportStatic; |
57 | 67 | import com.oracle.truffle.api.dsl.NodeFactory;
|
58 | 68 | import com.oracle.truffle.api.dsl.Specialization;
|
| 69 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 70 | +import com.oracle.truffle.api.library.CachedLibrary; |
59 | 71 |
|
60 | 72 | @CoreFunctions(extendClasses = {PythonBuiltinClassType.PStaticmethod, PythonBuiltinClassType.PClassmethod})
|
61 | 73 | public class DecoratedMethodBuiltins extends PythonBuiltins {
|
@@ -83,4 +95,43 @@ protected Object func(PDecoratedMethod self) {
|
83 | 95 | return self.getCallable();
|
84 | 96 | }
|
85 | 97 | }
|
| 98 | + |
| 99 | + @Builtin(name = __DICT__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true) |
| 100 | + @GenerateNodeFactory |
| 101 | + @ImportStatic(PGuards.class) |
| 102 | + public abstract static class DictNode extends PythonBinaryBuiltinNode { |
| 103 | + @Specialization(limit = "1") |
| 104 | + protected Object getDict(PDecoratedMethod self, @SuppressWarnings("unused") PNone mapping, |
| 105 | + @CachedLibrary("self") PythonObjectLibrary lib, |
| 106 | + @Cached PythonObjectFactory factory) { |
| 107 | + PHashingCollection dict = lib.getDict(self); |
| 108 | + if (dict == null) { |
| 109 | + dict = factory.createDictFixedStorage(self); |
| 110 | + try { |
| 111 | + lib.setDict(self, dict); |
| 112 | + } catch (UnsupportedMessageException e) { |
| 113 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 114 | + throw new IllegalStateException(e); |
| 115 | + } |
| 116 | + } |
| 117 | + return dict; |
| 118 | + } |
| 119 | + |
| 120 | + @Specialization(limit = "1") |
| 121 | + protected Object setDict(PDecoratedMethod self, PHashingCollection mapping, |
| 122 | + @CachedLibrary("self") PythonObjectLibrary lib) { |
| 123 | + try { |
| 124 | + lib.setDict(self, mapping); |
| 125 | + } catch (UnsupportedMessageException ex) { |
| 126 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 127 | + throw new IllegalStateException(ex); |
| 128 | + } |
| 129 | + return PNone.NONE; |
| 130 | + } |
| 131 | + |
| 132 | + @Specialization(guards = "!isDict(value)") |
| 133 | + protected Object setDict(@SuppressWarnings("unused") PDecoratedMethod self, Object value) { |
| 134 | + throw raise(TypeError, ErrorMessages.MUST_BE_SET_TO_S_NOT_P, __DICT__, "dictionary", value); |
| 135 | + } |
| 136 | + } |
86 | 137 | }
|
0 commit comments