|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package com.oracle.truffle.espresso.nodes; |
| 24 | + |
| 25 | +import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; |
| 26 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 27 | +import com.oracle.truffle.api.TruffleLanguage; |
| 28 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 29 | +import com.oracle.truffle.api.interop.TruffleObject; |
| 30 | +import com.oracle.truffle.api.interop.UnknownIdentifierException; |
| 31 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 32 | +import com.oracle.truffle.api.library.ExportLibrary; |
| 33 | +import com.oracle.truffle.api.library.ExportMessage; |
| 34 | +import com.oracle.truffle.espresso.EspressoLanguage; |
| 35 | +import com.oracle.truffle.espresso.classfile.ConstantPool; |
| 36 | +import com.oracle.truffle.espresso.classfile.attributes.MethodParametersAttribute; |
| 37 | +import com.oracle.truffle.espresso.descriptors.Symbol; |
| 38 | +import com.oracle.truffle.espresso.impl.Method; |
| 39 | + |
| 40 | +@ExportLibrary(InteropLibrary.class) |
| 41 | +final class SubstitutionScope implements TruffleObject { |
| 42 | + @CompilationFinal(dimensions = 1) private final Object[] args; |
| 43 | + private final Method method; |
| 44 | + private String[] paramNames; |
| 45 | + |
| 46 | + SubstitutionScope(Object[] arguments, Method.MethodVersion method) { |
| 47 | + this.args = arguments; |
| 48 | + this.method = method.getMethod(); |
| 49 | + } |
| 50 | + |
| 51 | + @ExportMessage |
| 52 | + @SuppressWarnings("static-method") |
| 53 | + boolean hasMembers() { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + @ExportMessage |
| 58 | + @SuppressWarnings("static-method") |
| 59 | + boolean isScope() { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + @ExportMessage |
| 64 | + @SuppressWarnings("static-method") |
| 65 | + boolean hasLanguage() { |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + @ExportMessage |
| 70 | + @SuppressWarnings("static-method") |
| 71 | + Class<? extends TruffleLanguage<?>> getLanguage() { |
| 72 | + return EspressoLanguage.class; |
| 73 | + } |
| 74 | + |
| 75 | + @ExportMessage |
| 76 | + @TruffleBoundary |
| 77 | + Object readMember(String member) throws UnknownIdentifierException { |
| 78 | + try { |
| 79 | + int index = Integer.parseInt(member); |
| 80 | + if (index < 0 || index >= args.length) { |
| 81 | + throw UnknownIdentifierException.create(member); |
| 82 | + } |
| 83 | + return args[index]; |
| 84 | + } catch (NumberFormatException e) { |
| 85 | + // OK, see if we have parameter names as a fallback. |
| 86 | + // The main use case which is JDWP doesn't use anything but slots, |
| 87 | + // so this branch should rarely be taken. |
| 88 | + String[] names = paramNames; |
| 89 | + if (names == null) { |
| 90 | + names = paramNames = fetchNames(); |
| 91 | + } |
| 92 | + for (int i = 0; i < names.length; i++) { |
| 93 | + if (names[i].equals(member)) { |
| 94 | + return args[i]; |
| 95 | + } |
| 96 | + } |
| 97 | + throw UnknownIdentifierException.create(member); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private String[] fetchNames() { |
| 102 | + MethodParametersAttribute methodParameters = (MethodParametersAttribute) method.getAttribute(Symbol.Name.MethodParameters); |
| 103 | + |
| 104 | + if (methodParameters == null) { |
| 105 | + return new String[0]; |
| 106 | + } |
| 107 | + // verify parameter attribute first |
| 108 | + MethodParametersAttribute.Entry[] entries = methodParameters.getEntries(); |
| 109 | + int cpLength = method.getConstantPool().length(); |
| 110 | + for (MethodParametersAttribute.Entry entry : entries) { |
| 111 | + int nameIndex = entry.getNameIndex(); |
| 112 | + if (nameIndex < 0 || nameIndex >= cpLength) { |
| 113 | + return new String[0]; |
| 114 | + } |
| 115 | + if (nameIndex != 0 && method.getConstantPool().tagAt(nameIndex) != ConstantPool.Tag.UTF8) { |
| 116 | + return new String[0]; |
| 117 | + } |
| 118 | + } |
| 119 | + String[] result = new String[entries.length]; |
| 120 | + for (int i = 0; i < entries.length; i++) { |
| 121 | + MethodParametersAttribute.Entry entry = entries[i]; |
| 122 | + // For a 0 index, give an empty name. |
| 123 | + String name; |
| 124 | + if (entry.getNameIndex() != 0) { |
| 125 | + name = method.getConstantPool().symbolAt(entry.getNameIndex(), "parameter name").toString(); |
| 126 | + } else { |
| 127 | + name = ""; |
| 128 | + } |
| 129 | + result[i] = name; |
| 130 | + } |
| 131 | + return result; |
| 132 | + } |
| 133 | + |
| 134 | + @ExportMessage |
| 135 | + @SuppressWarnings("static-method") |
| 136 | + Object getMembers(@SuppressWarnings("unused") boolean includeInternal) throws UnsupportedMessageException { |
| 137 | + throw UnsupportedMessageException.create(); |
| 138 | + } |
| 139 | + |
| 140 | + @ExportMessage |
| 141 | + @TruffleBoundary |
| 142 | + boolean isMemberReadable(String member) { |
| 143 | + try { |
| 144 | + int index = Integer.parseInt(member); |
| 145 | + return 0 <= index && index < args.length; |
| 146 | + } catch (NumberFormatException e) { |
| 147 | + // OK, see if we have parameter names as a fallback. |
| 148 | + // The main use case which is JDWP doesn't use anything but slots, |
| 149 | + // so this branch should rarely be taken. |
| 150 | + String[] names = paramNames; |
| 151 | + if (names == null) { |
| 152 | + names = paramNames = fetchNames(); |
| 153 | + } |
| 154 | + for (String name : names) { |
| 155 | + if (name.equals(member)) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + } |
| 159 | + return false; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @ExportMessage |
| 164 | + @SuppressWarnings("static-method") |
| 165 | + Object toDisplayString(@SuppressWarnings("unused") boolean allowSideEffects) { |
| 166 | + return method.getNameAsString(); |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments