vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
branchemul
changeset 94 19497b4312bb
parent 93 a236a9f137ac
child 96 519bcc6999c4
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Oct 08 17:10:27 2012 -0700
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Oct 09 11:11:58 2012 -0700
     1.3 @@ -23,8 +23,10 @@
     1.4  import java.util.Collection;
     1.5  import java.util.List;
     1.6  import org.apidesign.bck2brwsr.core.ExtraJavaScript;
     1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.8  import org.netbeans.modules.classfile.Annotation;
     1.9  import org.netbeans.modules.classfile.AnnotationComponent;
    1.10 +import org.netbeans.modules.classfile.ArrayElementValue;
    1.11  import static org.netbeans.modules.classfile.ByteCodes.*;
    1.12  import org.netbeans.modules.classfile.CPClassInfo;
    1.13  import org.netbeans.modules.classfile.CPEntry;
    1.14 @@ -135,6 +137,9 @@
    1.15          }
    1.16      }
    1.17      private void generateStaticMethod(Method m, List<String> toInitilize) throws IOException {
    1.18 +        if (javaScriptBody(m, true)) {
    1.19 +            return;
    1.20 +        }
    1.21          final String mn = findMethodName(m);
    1.22          out.append("\nfunction ").append(
    1.23              jc.getName().getInternalName().replace('/', '_')
    1.24 @@ -180,6 +185,9 @@
    1.25      }
    1.26      
    1.27      private void generateInstanceMethod(Method m) throws IOException {
    1.28 +        if (javaScriptBody(m, false)) {
    1.29 +            return;
    1.30 +        }
    1.31          out.append("\nfunction ").append(
    1.32              jc.getName().getInternalName().replace('/', '_')
    1.33          ).append('_').append(findMethodName(m));
    1.34 @@ -900,4 +908,45 @@
    1.35      private String findDescriptor(String d) {
    1.36          return d.replace('[', 'A');
    1.37      }
    1.38 +
    1.39 +    private boolean javaScriptBody(Method m, boolean isStatic) throws IOException {
    1.40 +        final ClassName extraAnn = ClassName.getClassName(JavaScriptBody.class.getName().replace('.', '/'));
    1.41 +        Annotation a = m.getAnnotation(extraAnn);
    1.42 +        if (a != null) {
    1.43 +            final ElementValue annVal = a.getComponent("body").getValue();
    1.44 +            String body = ((PrimitiveElementValue) annVal).getValue().getValue().toString();
    1.45 +            
    1.46 +            final ArrayElementValue arrVal = (ArrayElementValue) a.getComponent("args").getValue();
    1.47 +            final int len = arrVal.getValues().length;
    1.48 +            String[] names = new String[len];
    1.49 +            for (int i = 0; i < len; i++) {
    1.50 +                names[i] = ((PrimitiveElementValue) arrVal.getValues()[i]).getValue().getValue().toString();
    1.51 +            }
    1.52 +            out.append("\nfunction ").append(
    1.53 +                jc.getName().getInternalName().replace('/', '_')).append('_').append(findMethodName(m));
    1.54 +            out.append("(");
    1.55 +            String space;
    1.56 +            int index;
    1.57 +            if (!isStatic) {                
    1.58 +                out.append(names[0]);
    1.59 +                space = ",";
    1.60 +                index = 1;
    1.61 +            } else {
    1.62 +                space = "";
    1.63 +                index = 0;
    1.64 +            }
    1.65 +            List<Parameter> args = m.getParameters();
    1.66 +            for (int i = 0; i < args.size(); i++) {
    1.67 +                out.append(space);
    1.68 +                out.append(names[index]);
    1.69 +                final String desc = findDescriptor(args.get(i).getDescriptor());
    1.70 +                index++;
    1.71 +            }
    1.72 +            out.append(") {").append("\n");
    1.73 +            out.append(body);
    1.74 +            out.append("\n}\n");
    1.75 +            return true;
    1.76 +        }
    1.77 +        return false;
    1.78 +    }
    1.79  }