Path: blob/aarch64-shenandoah-jdk8u272-b10/nashorn/src/jdk/internal/dynalink/support/NameCodec.java
48580 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file, and Oracle licenses the original version of this file under the BSD30* license:31*/32/*33Copyright 2009-2013 Attila Szegedi3435Licensed under both the Apache License, Version 2.0 (the "Apache License")36and the BSD License (the "BSD License"), with licensee being free to37choose either of the two at their discretion.3839You may not use this file except in compliance with either the Apache40License or the BSD License.4142If you choose to use this file in compliance with the Apache License, the43following notice applies to you:4445You may obtain a copy of the Apache License at4647http://www.apache.org/licenses/LICENSE-2.04849Unless required by applicable law or agreed to in writing, software50distributed under the License is distributed on an "AS IS" BASIS,51WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or52implied. See the License for the specific language governing53permissions and limitations under the License.5455If you choose to use this file in compliance with the BSD License, the56following notice applies to you:5758Redistribution and use in source and binary forms, with or without59modification, are permitted provided that the following conditions are60met:61* Redistributions of source code must retain the above copyright62notice, this list of conditions and the following disclaimer.63* Redistributions in binary form must reproduce the above copyright64notice, this list of conditions and the following disclaimer in the65documentation and/or other materials provided with the distribution.66* Neither the name of the copyright holder nor the names of67contributors may be used to endorse or promote products derived from68this software without specific prior written permission.6970THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS71IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED72TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A73PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER74BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR75CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF76SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR77BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,78WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR79OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF80ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.81*/8283package jdk.internal.dynalink.support;8485import jdk.internal.dynalink.CallSiteDescriptor;8687/**88* Implements the name mangling and demangling as specified by John Rose's89* <a href="https://blogs.oracle.com/jrose/entry/symbolic_freedom_in_the_vm" target="_blank">"Symbolic Freedom in the90* VM"</a> article. It is recommended that implementers of languages on the JVM uniformly adopt this for symbolic91* interoperability between languages. Normally, you would mangle the names as you're generating bytecode, and then92* demangle them when you're creating {@link CallSiteDescriptor} objects. Note that you are expected to mangle93* individual tokens, and not the whole name at the call site, i.e. the colon character normally separating the tokens94* is never mangled. I.e. you wouldn't mangle {@code dyn:getProp:color} into {@code dyn\!getProp\!color}, but you would95* mangle {@code dyn:getProp:color$} into {@code dyn:getProp:\=color\%} (only mangling the individual token containing96* the symbol {@code color$}). {@link CallSiteDescriptorFactory#tokenizeName(String)} (and by implication, all call site97* descriptors it creates) will automatically perform demangling on the passed names. If you use this factory, or you98* have your own way of creating call site descriptors, but you still delegate to this method of the default factory99* (it is recommended that you do), then you have demangling handled for you already, and only need to ensure that you100* mangle the names when you're emitting them in the bytecode.101*102* @author Attila Szegedi103*/104public class NameCodec {105private static final char ESCAPE_CHAR = '\\';106private static final char EMPTY_ESCAPE = '=';107private static final String EMPTY_NAME = new String(new char[] { ESCAPE_CHAR, EMPTY_ESCAPE });108private static final char EMPTY_CHAR = 0xFEFF;109110private static final int MIN_ENCODING = '$';111private static final int MAX_ENCODING = ']';112private static final char[] ENCODING = new char[MAX_ENCODING - MIN_ENCODING + 1];113private static final int MIN_DECODING = '!';114private static final int MAX_DECODING = '}';115private static final char[] DECODING = new char[MAX_DECODING - MIN_DECODING + 1];116117static {118addEncoding('/', '|');119addEncoding('.', ',');120addEncoding(';', '?');121addEncoding('$', '%');122addEncoding('<', '^');123addEncoding('>', '_');124addEncoding('[', '{');125addEncoding(']', '}');126addEncoding(':', '!');127addEncoding('\\', '-');128DECODING[EMPTY_ESCAPE - MIN_DECODING] = EMPTY_CHAR;129}130131private NameCodec() {132}133134/**135* Encodes ("mangles") an unencoded symbolic name.136* @param name the symbolic name to mangle137* @return the mangled form of the symbolic name.138*/139public static String encode(final String name) {140final int l = name.length();141if(l == 0) {142return EMPTY_NAME;143}144StringBuilder b = null;145int lastEscape = -1;146for(int i = 0; i < l; ++i) {147final int encodeIndex = name.charAt(i) - MIN_ENCODING;148if(encodeIndex >= 0 && encodeIndex < ENCODING.length) {149final char e = ENCODING[encodeIndex];150if(e != 0) {151if(b == null) {152b = new StringBuilder(name.length() + 3);153if(name.charAt(0) != ESCAPE_CHAR && i > 0) {154b.append(EMPTY_NAME);155}156b.append(name, 0, i);157} else {158b.append(name, lastEscape + 1, i);159}160b.append(ESCAPE_CHAR).append(e);161lastEscape = i;162}163}164}165if(b == null) {166return name.toString();167}168assert lastEscape != -1;169b.append(name, lastEscape + 1, l);170return b.toString();171}172173/**174* Decodes ("demangles") an encoded symbolic name.175* @param name the symbolic name to demangle176* @return the demangled form of the symbolic name.177*/178public static String decode(final String name) {179if(name.charAt(0) != ESCAPE_CHAR) {180return name;181}182final int l = name.length();183if(l == 2 && name.charAt(1) == EMPTY_CHAR) {184return "";185}186final StringBuilder b = new StringBuilder(name.length());187int lastEscape = -2;188int lastBackslash = -1;189for(;;) {190final int nextBackslash = name.indexOf(ESCAPE_CHAR, lastBackslash + 1);191if(nextBackslash == -1 || nextBackslash == l - 1) {192break;193}194final int decodeIndex = name.charAt(nextBackslash + 1) - MIN_DECODING;195if(decodeIndex >= 0 && decodeIndex < DECODING.length) {196final char d = DECODING[decodeIndex];197if(d == EMPTY_CHAR) {198// "\=" is only valid at the beginning of a mangled string199if(nextBackslash == 0) {200lastEscape = 0;201}202} else if(d != 0) {203b.append(name, lastEscape + 2, nextBackslash).append(d);204lastEscape = nextBackslash;205}206}207lastBackslash = nextBackslash;208}209b.append(name, lastEscape + 2, l);210return b.toString();211}212213private static void addEncoding(final char from, final char to) {214ENCODING[from - MIN_ENCODING] = to;215DECODING[to - MIN_DECODING] = from;216}217}218219220