예제 #1
0
 private void generateStringToJavaScript(GeneratorContext context, SourceWriter writer)
     throws IOException {
   FieldReference charsField = new FieldReference("java.lang.String", "characters");
   writer.append("var result = \"\";").softNewLine();
   writer
       .append("var data = ")
       .append(context.getParameterName(1))
       .append('.')
       .appendField(charsField)
       .append(".data;")
       .softNewLine();
   writer.append("for (var i = 0; i < data.length; i = (i + 1) | 0) {").indent().softNewLine();
   writer.append("result += String.fromCharCode(data[i]);").softNewLine();
   writer.outdent().append("}").softNewLine();
   writer.append("return result;").softNewLine();
 }
예제 #2
0
  private void generateToJavaScript(GeneratorContext context, SourceWriter writer)
      throws IOException {
    String obj = context.getParameterName(1);
    writer
        .append("if (" + obj + " === null || " + obj + " === undefined) {")
        .softNewLine()
        .indent();
    writer.append("return " + obj + ";").softNewLine();

    writer.outdent().append("} else if (typeof " + obj + " === 'number') {").indent().softNewLine();
    writer.append("return " + obj + ";").softNewLine();

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor.$meta && " + obj + ".constructor.$meta.item) {")
        .indent()
        .softNewLine();
    writer.append("var arr = new Array(" + obj + ".data.length);").softNewLine();
    writer.append("for (var i = 0; i < arr.length; ++i) {").indent().softNewLine();
    writer
        .append("arr[i] = ")
        .appendMethodBody(toJsMethod)
        .append("(" + obj + ".data[i]);")
        .softNewLine();
    writer.outdent().append("}").softNewLine();
    writer.append("return arr;").softNewLine();

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor === ")
        .appendClass("java.lang.String")
        .append(") {")
        .indent()
        .softNewLine();
    generateStringToJavaScript(context, writer);

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor === ")
        .appendClass("java.lang.Byte")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(byteValueMethod)
        .append("(" + obj + ")|0;")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor === ")
        .appendClass("java.lang.Short")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(shortValueMethod)
        .append("(" + obj + ")|0;")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor === ")
        .appendClass("java.lang.Integer")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(intValueMethod)
        .append("(" + obj + ")|0;")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor === ")
        .appendClass("java.lang.Boolean")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(booleanValueMethod)
        .append("(" + obj + ")!==0;")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor === ")
        .appendClass("java.lang.Double")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(doubleValueMethod)
        .append("(" + obj + ");")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + obj + ".constructor === ")
        .appendClass("java.lang.Character")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(charValueMethod)
        .append("(" + obj + ");")
        .softNewLine();

    writer.outdent().append("} else {").indent().softNewLine();
    writer.append("return " + obj + ";").softNewLine();

    writer.outdent().append("}").softNewLine();
  }
예제 #3
0
  private void generateFromJavaScript(GeneratorContext context, SourceWriter writer)
      throws IOException {
    String obj = context.getParameterName(1);
    String type = context.getParameterName(2);
    writer
        .append("if (" + obj + " === null || " + obj + " === undefined)")
        .ws()
        .append("{")
        .softNewLine()
        .indent();
    writer.append("return " + obj + ";").softNewLine();
    writer.outdent().append("} else if (" + type + ".$meta.item) {").indent().softNewLine();
    writer
        .append("var arr = $rt_createArray(" + type + ".$meta.item, " + obj + ".length);")
        .softNewLine();
    writer.append("for (var i = 0; i < arr.data.length; ++i) {").indent().softNewLine();
    writer
        .append("arr.data[i] = ")
        .appendMethodBody(fromJsMethod)
        .append("(" + obj + "[i], " + type + ".$meta.item);")
        .softNewLine();
    writer.outdent().append("}").softNewLine();
    writer.append("return arr;").softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.String")
        .append(") {")
        .indent()
        .softNewLine();
    writer.append("return $rt_str(" + obj + ");").softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.Boolean")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfBooleanMethod)
        .append("(" + obj + "?1:0);")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.Character")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfCharMethod)
        .append(
            "(typeof " + obj + " === 'number' ? " + obj + " & 0xFFFF : " + obj + ".charCodeAt(0));")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.Byte")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfByteMethod)
        .append("(" + obj + "|0);")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.Short")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfShortMethod)
        .append("(" + obj + "|0);")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.Integer")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfIntMethod)
        .append("(" + obj + "|0);")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.Long")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfLongMethod)
        .append("(Long_fromInt(" + obj + "|0));")
        .softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === ")
        .appendClass("java.lang.Double")
        .append(") {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfDoubleMethod)
        .append("(" + obj + ");")
        .softNewLine();

    writer
        .outdent()
        .append(
            "} else if ("
                + type
                + " === $rt_intcls() || "
                + type
                + " === $rt_bytecls() || "
                + type
                + " === $rt_shortcls() ||"
                + type
                + " == $rt_charcls()) {")
        .indent()
        .softNewLine();
    writer.append("return " + obj + "|0;").softNewLine();

    writer.outdent().append("} else if (" + type + " === $rt_longcls()) {").indent().softNewLine();
    writer.append("return Long_fromInt(" + obj + "|0);").softNewLine();

    writer
        .outdent()
        .append("} else if (" + type + " === $rt_doublecls() || " + type + " == $rt_floatcls()) {")
        .indent()
        .softNewLine();
    writer.append("return " + obj + ";").softNewLine();
    writer
        .outdent()
        .append("} else if (" + type + " === $rt_booleancls()) {")
        .indent()
        .softNewLine();
    writer.append("return " + obj + "?1:0;").softNewLine();

    writer.outdent().append("} else if (" + obj + " instanceof Array) {").indent().softNewLine();
    writer.append("var arr = $rt_createArray($rt_objcls(), " + obj + ".length);").softNewLine();
    writer.append("for (var i = 0; i < arr.data.length; ++i) {").indent().softNewLine();
    writer
        .append("arr.data[i] = ")
        .appendMethodBody(fromJsMethod)
        .append("(" + obj + "[i], $rt_objcls());")
        .softNewLine();
    writer.outdent().append("}").softNewLine();
    writer.append("return arr;").softNewLine();

    writer.outdent().append("} else if (typeof " + obj + " === 'string') {").indent().softNewLine();
    writer.append("return $rt_str(" + obj + ");").softNewLine();

    writer.outdent().append("} else if (typeof " + obj + " === 'number') {").indent().softNewLine();
    writer.append("if ((" + obj + "|0) === " + obj + ") {").indent().softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfIntMethod)
        .append("(" + obj + ");")
        .softNewLine();
    writer.outdent().append("} else {").indent().softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfDoubleMethod)
        .append("(" + obj + ");")
        .softNewLine();
    writer.outdent().append("}").softNewLine();

    writer
        .outdent()
        .append("} else if (typeof " + obj + " === 'boolean') {")
        .indent()
        .softNewLine();
    writer
        .append("return ")
        .appendMethodBody(valueOfBooleanMethod)
        .append("(" + obj + "?1:0);")
        .softNewLine();
    writer.outdent().append("} else {").indent().softNewLine();
    writer.append("return ").append(obj).append(";").softNewLine();
    writer.outdent().append("}").softNewLine();
  }