requireScript needs to work also in lazy mode
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 13:39:23 +0100
changeset 50380a388c8c27b
parent 502 a5cd79ee1d96
child 504 974bc55865c7
requireScript needs to work also in lazy mode
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java
vm/src/test/java/org/apidesign/vm4brwsr/Script.java
vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
vm/src/test/resources/org/apidesign/vm4brwsr/ko.js
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Jan 21 13:23:23 2013 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Jan 21 13:39:23 2013 +0100
     1.3 @@ -52,7 +52,7 @@
     1.4      /*
     1.5       * @param resourcePath name of resources to read
     1.6       */
     1.7 -    protected abstract void requireScript(String resourcePath);
     1.8 +    protected abstract void requireScript(String resourcePath) throws IOException;
     1.9      
    1.10      /** Allows subclasses to redefine what field a function representing a
    1.11       * class gets assigned. By default it returns the suggested name followed
     2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java	Mon Jan 21 13:23:23 2013 +0100
     2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java	Mon Jan 21 13:39:23 2013 +0100
     2.3 @@ -19,6 +19,7 @@
     2.4  
     2.5  import java.io.ByteArrayInputStream;
     2.6  import java.io.IOException;
     2.7 +import java.io.InputStream;
     2.8  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     2.9  
    2.10  /**
    2.11 @@ -131,7 +132,17 @@
    2.12          }
    2.13  
    2.14          @Override
    2.15 -        protected void requireScript(String resourcePath) {
    2.16 +        protected void requireScript(String resourcePath) throws IOException {
    2.17 +            InputStream is = getClass().getResourceAsStream(resourcePath);
    2.18 +            StringBuilder sb = new StringBuilder();
    2.19 +            for (;;) {
    2.20 +                int ch = is.read();
    2.21 +                if (ch == -1) {
    2.22 +                    break;
    2.23 +                }
    2.24 +                sb.append((char)ch);
    2.25 +            }
    2.26 +            applyCode(lazy.loader, null, sb.toString(), false);
    2.27          }
    2.28  
    2.29          @Override
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Script.java	Mon Jan 21 13:39:23 2013 +0100
     3.3 @@ -0,0 +1,31 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.vm4brwsr;
    3.22 +
    3.23 +import org.apidesign.bck2brwsr.core.ExtraJavaScript;
    3.24 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    3.25 +
    3.26 +/** Test to verify external scripts are processed in lazy mode.
    3.27 + *
    3.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.29 + */
    3.30 +@ExtraJavaScript(resource = "/org/apidesign/vm4brwsr/ko.js")
    3.31 +public class Script {
    3.32 +    @JavaScriptBody(args = {  }, body = "return ko !== null;")
    3.33 +    public static native boolean checkNotNull();
    3.34 +}
     4.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java	Mon Jan 21 13:23:23 2013 +0100
     4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java	Mon Jan 21 13:39:23 2013 +0100
     4.3 @@ -67,6 +67,12 @@
     4.4          );
     4.5      }
     4.6  
     4.7 +    @Test public void loadClassWithAssociatedScript() throws Exception {
     4.8 +        assertExec("ko is defined", "test", true,
     4.9 +            Script.class.getName(), "checkNotNull__Z"
    4.10 +        );
    4.11 +    }
    4.12 +
    4.13      private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    4.14          Object ret = null;
    4.15          try {
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/vm/src/test/resources/org/apidesign/vm4brwsr/ko.js	Mon Jan 21 13:39:23 2013 +0100
     5.3 @@ -0,0 +1,21 @@
     5.4 +/**
     5.5 + * Back 2 Browser Bytecode Translator
     5.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, version 2 of the License.
    5.11 + *
    5.12 + * This program is distributed in the hope that it will be useful,
    5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.15 + * GNU General Public License for more details.
    5.16 + *
    5.17 + * You should have received a copy of the GNU General Public License
    5.18 + * along with this program. Look for COPYING file in the top folder.
    5.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    5.20 + */
    5.21 +
    5.22 +this.ko = {};
    5.23 +
    5.24 +