|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved. This |
| 3 | + * code is released under a tri EPL/GPL/LGPL license. You can use it, |
| 4 | + * redistribute it and/or modify it under the terms of the: |
| 5 | + * |
| 6 | + * Eclipse Public License version 2.0, or |
| 7 | + * GNU General Public License version 2, or |
| 8 | + * GNU Lesser General Public License version 2.1. |
| 9 | + */ |
| 10 | +package org.truffleruby.core.format.convert; |
| 11 | + |
| 12 | +import static org.truffleruby.language.dispatch.DispatchConfiguration.PRIVATE_RETURN_MISSING; |
| 13 | + |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | + |
| 16 | +import org.truffleruby.core.array.RubyArray; |
| 17 | +import org.truffleruby.core.encoding.Encodings; |
| 18 | +import org.truffleruby.core.format.FormatNode; |
| 19 | +import org.truffleruby.core.format.exceptions.NoImplicitConversionException; |
| 20 | +import org.truffleruby.core.kernel.KernelNodes; |
| 21 | +import org.truffleruby.core.klass.RubyClass; |
| 22 | +import org.truffleruby.core.string.RubyString; |
| 23 | +import org.truffleruby.core.string.TStringConstants; |
| 24 | +import org.truffleruby.language.dispatch.DispatchNode; |
| 25 | +import org.truffleruby.language.library.RubyStringLibrary; |
| 26 | + |
| 27 | +import com.oracle.truffle.api.CompilerDirectives; |
| 28 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 29 | +import com.oracle.truffle.api.dsl.Cached; |
| 30 | +import com.oracle.truffle.api.dsl.Cached.Exclusive; |
| 31 | +import com.oracle.truffle.api.dsl.Cached.Shared; |
| 32 | +import com.oracle.truffle.api.dsl.NodeChild; |
| 33 | +import com.oracle.truffle.api.dsl.Specialization; |
| 34 | +import com.oracle.truffle.api.strings.TruffleString; |
| 35 | + |
| 36 | +@NodeChild("value") |
| 37 | +public abstract class ToStringNode extends FormatNode { |
| 38 | + |
| 39 | + protected final boolean convertNumbersToStrings; |
| 40 | + private final String conversionMethod; |
| 41 | + private final boolean inspectOnConversionFailure; |
| 42 | + protected final boolean specialClassBehaviour; |
| 43 | + |
| 44 | + @Child private DispatchNode toStrNode; |
| 45 | + @Child private DispatchNode toSNode; |
| 46 | + @Child private KernelNodes.ToSNode inspectNode; |
| 47 | + |
| 48 | + public ToStringNode( |
| 49 | + boolean convertNumbersToStrings, |
| 50 | + String conversionMethod, |
| 51 | + boolean inspectOnConversionFailure) { |
| 52 | + this(convertNumbersToStrings, conversionMethod, inspectOnConversionFailure, false); |
| 53 | + } |
| 54 | + |
| 55 | + public ToStringNode( |
| 56 | + boolean convertNumbersToStrings, |
| 57 | + String conversionMethod, |
| 58 | + boolean inspectOnConversionFailure, |
| 59 | + boolean specialClassBehaviour) { |
| 60 | + this.convertNumbersToStrings = convertNumbersToStrings; |
| 61 | + this.conversionMethod = conversionMethod; |
| 62 | + this.inspectOnConversionFailure = inspectOnConversionFailure; |
| 63 | + this.specialClassBehaviour = specialClassBehaviour; |
| 64 | + } |
| 65 | + |
| 66 | + public abstract Object executeToString(Object object); |
| 67 | + |
| 68 | + @Specialization(guards = "convertNumbersToStrings") |
| 69 | + RubyString toString(long value, |
| 70 | + @Cached TruffleString.FromLongNode fromLongNode) { |
| 71 | + var tstring = fromLongNode.execute(value, Encodings.US_ASCII.tencoding, true); |
| 72 | + return createString(tstring, Encodings.US_ASCII); |
| 73 | + } |
| 74 | + |
| 75 | + @TruffleBoundary |
| 76 | + @Specialization(guards = "convertNumbersToStrings") |
| 77 | + RubyString toString(double value, |
| 78 | + @Cached TruffleString.FromJavaStringNode fromJavaStringNode) { |
| 79 | + return createString(fromJavaStringNode, Double.toString(value), Encodings.US_ASCII); |
| 80 | + } |
| 81 | + |
| 82 | + @TruffleBoundary |
| 83 | + @Specialization(guards = "specialClassBehaviour") |
| 84 | + Object toStringSpecialClass(RubyClass rubyClass, |
| 85 | + @Cached @Shared RubyStringLibrary libString) { |
| 86 | + if (rubyClass == getContext().getCoreLibrary().trueClass) { |
| 87 | + return createString(TStringConstants.TRUE, Encodings.US_ASCII); |
| 88 | + } else if (rubyClass == getContext().getCoreLibrary().falseClass) { |
| 89 | + return createString(TStringConstants.FALSE, Encodings.US_ASCII); |
| 90 | + } else if (rubyClass == getContext().getCoreLibrary().nilClass) { |
| 91 | + return createString(TStringConstants.NIL, Encodings.US_ASCII); |
| 92 | + } else { |
| 93 | + return toString(rubyClass, libString); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Specialization(guards = "argLibString.isRubyString(this, string)", limit = "1") |
| 98 | + Object toStringString(Object string, |
| 99 | + @Cached @Shared RubyStringLibrary libString, |
| 100 | + @Cached @Exclusive RubyStringLibrary argLibString) { |
| 101 | + if ("inspect".equals(conversionMethod)) { |
| 102 | + final Object value = getToStrNode().call(PRIVATE_RETURN_MISSING, string, conversionMethod); |
| 103 | + |
| 104 | + if (libString.isRubyString(this, value)) { |
| 105 | + return value; |
| 106 | + } else { |
| 107 | + throw new NoImplicitConversionException(string, "String"); |
| 108 | + } |
| 109 | + } |
| 110 | + return string; |
| 111 | + } |
| 112 | + |
| 113 | + @Specialization |
| 114 | + Object toString(RubyArray array, |
| 115 | + @Cached @Shared RubyStringLibrary libString) { |
| 116 | + if (toSNode == null) { |
| 117 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 118 | + toSNode = insert(DispatchNode.create()); |
| 119 | + } |
| 120 | + |
| 121 | + final Object value = toSNode.call(PRIVATE_RETURN_MISSING, array, "to_s"); |
| 122 | + |
| 123 | + if (libString.isRubyString(this, value)) { |
| 124 | + return value; |
| 125 | + } else { |
| 126 | + throw new NoImplicitConversionException(array, "String"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Specialization( |
| 131 | + guards = { "isNotRubyString(object)", "!isRubyArray(object)", "!isForeignObject(object)" }) |
| 132 | + Object toString(Object object, |
| 133 | + @Cached @Shared RubyStringLibrary libString) { |
| 134 | + final Object value = getToStrNode().call(PRIVATE_RETURN_MISSING, object, conversionMethod); |
| 135 | + |
| 136 | + if (libString.isRubyString(this, value)) { |
| 137 | + return value; |
| 138 | + } |
| 139 | + |
| 140 | + if (inspectOnConversionFailure) { |
| 141 | + if (inspectNode == null) { |
| 142 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 143 | + inspectNode = insert(KernelNodes.ToSNode.create()); |
| 144 | + } |
| 145 | + |
| 146 | + return inspectNode.execute(object); |
| 147 | + } else { |
| 148 | + throw new NoImplicitConversionException(object, "String"); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @TruffleBoundary |
| 153 | + @Specialization(guards = "isForeignObject(object)") |
| 154 | + RubyString toStringForeign(Object object, |
| 155 | + @Cached TruffleString.FromByteArrayNode fromByteArrayNode) { |
| 156 | + return createString(fromByteArrayNode, |
| 157 | + object.toString().getBytes(StandardCharsets.UTF_8), |
| 158 | + Encodings.UTF_8); |
| 159 | + } |
| 160 | + |
| 161 | + private DispatchNode getToStrNode() { |
| 162 | + if (toStrNode == null) { |
| 163 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 164 | + toStrNode = insert(DispatchNode.create()); |
| 165 | + } |
| 166 | + return toStrNode; |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments