Moving the definition of Array.prototype.fillNulls() outside of the generator into the emulation .js file
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 06 Dec 2012 16:11:48 +0100
changeset 272a6a23aa7a546
parent 267 79697fa91bfc
child 273 59b9f9f5364f
Moving the definition of Array.prototype.fillNulls() outside of the generator into the emulation .js file
emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java
     1.1 --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Wed Dec 05 10:52:50 2012 +0100
     1.2 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Thu Dec 06 16:11:48 2012 +0100
     1.3 @@ -1,2 +1,9 @@
     1.4  // initialize methods on String constants
     1.5  java_lang_String(false);
     1.6 +
     1.7 +// we need initialized arrays
     1.8 +Array.prototype.fillNulls = function() {
     1.9 +  for(var i = 0; i < this.length; i++) this[i] = null;
    1.10 +  return this;
    1.11 +};
    1.12 +
     2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Wed Dec 05 10:52:50 2012 +0100
     2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Thu Dec 06 16:11:48 2012 +0100
     2.3 @@ -31,7 +31,7 @@
     2.4   */
     2.5  public abstract class ByteCodeToJavaScript {
     2.6      private ClassData jc;
     2.7 -    private final Appendable out;
     2.8 +    final Appendable out;
     2.9  
    2.10      protected ByteCodeToJavaScript(Appendable out) {
    2.11          this.out = out;
     3.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Wed Dec 05 10:52:50 2012 +0100
     3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Thu Dec 06 16:11:48 2012 +0100
     3.3 @@ -26,7 +26,7 @@
     3.4   *
     3.5   * @author Jaroslav Tulach <jtulach@netbeans.org>
     3.6   */
     3.7 -final class GenJS extends ByteCodeToJavaScript {
     3.8 +class GenJS extends ByteCodeToJavaScript {
     3.9      public GenJS(Appendable out) {
    3.10          super(out);
    3.11      }
    3.12 @@ -41,22 +41,16 @@
    3.13          compile(GenJS.class.getClassLoader(), out, names);
    3.14      }
    3.15      static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
    3.16 -        out.append("Array.prototype.fillNulls = function() {\n" +
    3.17 -             "  for(var i = 0; i < this.length; i++) {\n" +
    3.18 -             "    this[i] = null;\n" +
    3.19 -             "  }\n" +
    3.20 -             "  return this;\n" +
    3.21 -             "};");
    3.22 -        
    3.23 -        
    3.24 +        new GenJS(out).doCompile(l, names);
    3.25 +    }
    3.26 +    protected void doCompile(ClassLoader l, StringArray names) throws IOException {
    3.27          StringArray processed = new StringArray();
    3.28          StringArray initCode = new StringArray();
    3.29          for (String baseClass : names.toArray()) {
    3.30 -            GenJS js = new GenJS(out);
    3.31 -            js.references.add(baseClass);
    3.32 +            references.add(baseClass);
    3.33              for (;;) {
    3.34                  String name = null;
    3.35 -                for (String n : js.references.toArray()) {
    3.36 +                for (String n : references.toArray()) {
    3.37                      if (processed.contains(n)) {
    3.38                          continue;
    3.39                      }
    3.40 @@ -70,7 +64,7 @@
    3.41                      throw new IOException("Can't find class " + name); 
    3.42                  }
    3.43                  try {
    3.44 -                    String ic = js.compile(is);
    3.45 +                    String ic = compile(is);
    3.46                      processed.add(name);
    3.47                      initCode.add(ic == null ? "" : ic);
    3.48                  } catch (RuntimeException ex) {
    3.49 @@ -93,7 +87,7 @@
    3.50                  }
    3.51              }
    3.52  
    3.53 -            for (String resource : js.scripts.toArray()) {
    3.54 +            for (String resource : scripts.toArray()) {
    3.55                  while (resource.startsWith("/")) {
    3.56                      resource = resource.substring(1);
    3.57                  }
    3.58 @@ -103,9 +97,9 @@
    3.59                  }
    3.60                  readResource(emul, out);
    3.61              }
    3.62 -            js.scripts = new StringArray();
    3.63 +            scripts = new StringArray();
    3.64              
    3.65 -            StringArray toInit = StringArray.asList(js.references.toArray());
    3.66 +            StringArray toInit = StringArray.asList(references.toArray());
    3.67              toInit.reverse();
    3.68  
    3.69              for (String ic : toInit.toArray()) {
     4.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java	Wed Dec 05 10:52:50 2012 +0100
     4.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/StringArray.java	Thu Dec 06 16:11:48 2012 +0100
     4.3 @@ -48,7 +48,7 @@
     4.4          return arr == null ? new String[0] : arr;
     4.5      }
     4.6      
     4.7 -    static StringArray asList(String[] names) {
     4.8 +    static StringArray asList(String... names) {
     4.9          return new StringArray(names);
    4.10      }
    4.11