|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2023, 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.lib; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.objects.PNone.NO_VALUE; |
| 44 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.T___FORMAT__; |
| 45 | +import static com.oracle.graal.python.nodes.StringLiterals.T_EMPTY_STRING; |
| 46 | +import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError; |
| 47 | + |
| 48 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 49 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 50 | +import com.oracle.graal.python.builtins.objects.type.SpecialMethodSlot; |
| 51 | +import com.oracle.graal.python.nodes.ErrorMessages; |
| 52 | +import com.oracle.graal.python.nodes.PGuards; |
| 53 | +import com.oracle.graal.python.nodes.PNodeWithContext; |
| 54 | +import com.oracle.graal.python.nodes.PRaiseNode; |
| 55 | +import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode; |
| 56 | +import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode; |
| 57 | +import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; |
| 58 | +import com.oracle.graal.python.nodes.object.GetClassNode; |
| 59 | +import com.oracle.truffle.api.dsl.Cached; |
| 60 | +import com.oracle.truffle.api.dsl.Cached.Exclusive; |
| 61 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
| 62 | +import com.oracle.truffle.api.dsl.Fallback; |
| 63 | +import com.oracle.truffle.api.dsl.GenerateCached; |
| 64 | +import com.oracle.truffle.api.dsl.GenerateInline; |
| 65 | +import com.oracle.truffle.api.dsl.GenerateUncached; |
| 66 | +import com.oracle.truffle.api.dsl.Specialization; |
| 67 | +import com.oracle.truffle.api.frame.Frame; |
| 68 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 69 | +import com.oracle.truffle.api.nodes.Node; |
| 70 | +import com.oracle.truffle.api.strings.TruffleString; |
| 71 | + |
| 72 | +/** |
| 73 | + * Equivalent of CPython's {@code PyObject_Format}. The {@code formatSpec} argument must be a Python |
| 74 | + * string, otherwise this raises a SystemError. Unlike CPython's {@code PyObject_Format}, this does |
| 75 | + * not accept native {@code NULL} as {@code formatSpec}. Convert it to an empty string to get |
| 76 | + * equivalent behavior. |
| 77 | + */ |
| 78 | +@GenerateInline |
| 79 | +@GenerateUncached |
| 80 | +public abstract class PyObjectFormat extends PNodeWithContext { |
| 81 | + public final Object executeNonInlined(VirtualFrame frame, Object obj, Object formatSpec) { |
| 82 | + return execute(frame, null, obj, formatSpec); |
| 83 | + } |
| 84 | + |
| 85 | + public abstract Object execute(VirtualFrame frame, Node node, Object obj, Object formatSpec); |
| 86 | + |
| 87 | + @Specialization |
| 88 | + static Object doNone(VirtualFrame frame, Node inliningTarget, Object obj, PNone formatSpec, |
| 89 | + @Shared("impl") @Cached PyObjectFormatStr formatStr) { |
| 90 | + return formatStr.execute(frame, inliningTarget, obj, T_EMPTY_STRING); |
| 91 | + } |
| 92 | + |
| 93 | + @Fallback |
| 94 | + static Object doOthers(VirtualFrame frame, Node inliningTarget, Object obj, Object formatSpec, |
| 95 | + @Shared("impl") @Cached PyObjectFormatStr formatStr) { |
| 96 | + return formatStr.execute(frame, inliningTarget, obj, formatSpec); |
| 97 | + } |
| 98 | + |
| 99 | + @GenerateInline |
| 100 | + @GenerateUncached |
| 101 | + @GenerateCached(false) |
| 102 | + public abstract static class PyObjectFormatStr extends PNodeWithContext { |
| 103 | + public abstract Object execute(Frame frame, Node inliningTarget, Object obj, Object formatSpec); |
| 104 | + |
| 105 | + static boolean isEmptyString(Object formatSpec) { |
| 106 | + // to keep the fast-path optimization guard simple, we ignore empty PStrings |
| 107 | + return (formatSpec instanceof TruffleString && ((TruffleString) formatSpec).isEmpty()); |
| 108 | + } |
| 109 | + |
| 110 | + @Specialization(guards = {"isString(obj)", "isEmptyString(formatSpec)"}) |
| 111 | + static Object doString(Object obj, Object formatSpec) { |
| 112 | + return obj; |
| 113 | + } |
| 114 | + |
| 115 | + @Specialization(guards = {"isEmptyString(formatSpec)"}) |
| 116 | + static Object doLong(long obj, Object formatSpec) { |
| 117 | + return obj; |
| 118 | + } |
| 119 | + |
| 120 | + // Note: PRaiseNode is @Exclusive to workaround a bug in DSL |
| 121 | + @Specialization(guards = "isString(formatSpec)") |
| 122 | + static Object doGeneric(VirtualFrame frame, Object obj, Object formatSpec, |
| 123 | + @Cached(parameters = "Format", inline = false) LookupAndCallBinaryNode callFormat, |
| 124 | + @Exclusive @Cached(inline = false) PRaiseNode raiseNode) { |
| 125 | + Object res = callFormat.executeObject(frame, obj, formatSpec); |
| 126 | + if (res == NO_VALUE) { |
| 127 | + throw raiseNode.raise(TypeError, ErrorMessages.TYPE_DOESNT_DEFINE_FORMAT, obj); |
| 128 | + } |
| 129 | + if (!PGuards.isString(res)) { |
| 130 | + throw raiseNode.raise(TypeError, ErrorMessages.S_MUST_RETURN_S_NOT_P, T___FORMAT__, "str", res); |
| 131 | + } |
| 132 | + return res; |
| 133 | + } |
| 134 | + |
| 135 | + @Specialization(guards = "isString(formatSpec)", replaces = "doGeneric") |
| 136 | + static Object doGenericUncahed(VirtualFrame frame, Object obj, Object formatSpec) { |
| 137 | + Object klass = GetClassNode.getUncached().execute(obj); |
| 138 | + Object slot = LookupCallableSlotInMRONode.getUncached(SpecialMethodSlot.Format).execute(klass); |
| 139 | + return CallBinaryMethodNode.getUncached().executeObject(frame, slot, obj, formatSpec); |
| 140 | + } |
| 141 | + |
| 142 | + // Note: PRaiseNode is @Exclusive to workaround a bug in DSL |
| 143 | + @Fallback |
| 144 | + static Object doNonStringFormat(Object obj, Object formatSpec, |
| 145 | + @Exclusive @Cached(inline = false) PRaiseNode raiseNode) { |
| 146 | + throw raiseNode.raise(PythonBuiltinClassType.SystemError, ErrorMessages.S_MUST_BE_S_NOT_P, "Format specifier", "string", formatSpec); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments