ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/ManglingSink.java
changeset 1431 6ceb7c457073
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ide/editor/src/main/java/org/apidesign/bck2brwsr/ide/editor/ManglingSink.java	Mon Jan 13 12:37:03 2014 +0100
     1.3 @@ -0,0 +1,71 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.ide.editor;
    1.22 +
    1.23 +/**
    1.24 + * An implementation of {@linkplain JsniCommentTokenizer.Sink} that generates B2B
    1.25 + */
    1.26 +class ManglingSink implements JsniCommentTokenizer.Sink {
    1.27 +
    1.28 +    final StringBuilder out = new StringBuilder();
    1.29 +
    1.30 +    public void javascript(String s) {
    1.31 +        out.append(s);
    1.32 +    }
    1.33 +
    1.34 +    public void method(String clazz, String method, String signature) {
    1.35 +        out.append(mangle(clazz, method, signature));
    1.36 +    }
    1.37 +
    1.38 +    public void field(String clazz, String field) {
    1.39 +//        out.append(field);
    1.40 +        out.append('_').append(field).append('(').append(')');
    1.41 +    }
    1.42 +
    1.43 +
    1.44 +    @Override
    1.45 +    public String toString() {
    1.46 +        return out.toString();
    1.47 +    }
    1.48 +
    1.49 +
    1.50 +    static String mangle(String clazz, String method, String signature) {
    1.51 +        final StringBuilder builder = new StringBuilder();
    1.52 +        builder.append(method);
    1.53 +        builder.append("__");
    1.54 +        builder.append(mangle(JNIHelper.signature(JNIHelper.method(clazz, method, signature).getReturnType())));
    1.55 +        builder.append(mangle(signature));
    1.56 +        return builder.toString();
    1.57 +    }
    1.58 +
    1.59 +
    1.60 +    static String mangle(String txt) {
    1.61 +        final StringBuilder sb = new StringBuilder();
    1.62 +        for (int i = 0; i < txt.length(); i++) {
    1.63 +            final char ch = txt.charAt(i);
    1.64 +            switch (ch) {
    1.65 +                case '/': sb.append('_'); break;
    1.66 +                case '_': sb.append("_1"); break;
    1.67 +                case ';': sb.append("_2"); break;
    1.68 +                case '[': sb.append("_3"); break;
    1.69 +                default: sb.append(ch); break;
    1.70 +            }
    1.71 +        }
    1.72 +        return sb.toString();
    1.73 +    }
    1.74 +}