# HG changeset patch # User Jaroslav Tulach # Date 1358771963 -3600 # Node ID 80a388c8c27bf3eee44ce865783aee743aa8d622 # Parent a5cd79ee1d96ae568646157df1520b0ad116742b requireScript needs to work also in lazy mode diff -r a5cd79ee1d96 -r 80a388c8c27b vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Jan 21 13:23:23 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Jan 21 13:39:23 2013 +0100 @@ -52,7 +52,7 @@ /* * @param resourcePath name of resources to read */ - protected abstract void requireScript(String resourcePath); + protected abstract void requireScript(String resourcePath) throws IOException; /** Allows subclasses to redefine what field a function representing a * class gets assigned. By default it returns the suggested name followed diff -r a5cd79ee1d96 -r 80a388c8c27b vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Mon Jan 21 13:23:23 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VMLazy.java Mon Jan 21 13:39:23 2013 +0100 @@ -19,6 +19,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import org.apidesign.bck2brwsr.core.JavaScriptBody; /** @@ -131,7 +132,17 @@ } @Override - protected void requireScript(String resourcePath) { + protected void requireScript(String resourcePath) throws IOException { + InputStream is = getClass().getResourceAsStream(resourcePath); + StringBuilder sb = new StringBuilder(); + for (;;) { + int ch = is.read(); + if (ch == -1) { + break; + } + sb.append((char)ch); + } + applyCode(lazy.loader, null, sb.toString(), false); } @Override diff -r a5cd79ee1d96 -r 80a388c8c27b vm/src/test/java/org/apidesign/vm4brwsr/Script.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Script.java Mon Jan 21 13:39:23 2013 +0100 @@ -0,0 +1,31 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import org.apidesign.bck2brwsr.core.ExtraJavaScript; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + +/** Test to verify external scripts are processed in lazy mode. + * + * @author Jaroslav Tulach + */ +@ExtraJavaScript(resource = "/org/apidesign/vm4brwsr/ko.js") +public class Script { + @JavaScriptBody(args = { }, body = "return ko !== null;") + public static native boolean checkNotNull(); +} diff -r a5cd79ee1d96 -r 80a388c8c27b vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Mon Jan 21 13:23:23 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Mon Jan 21 13:39:23 2013 +0100 @@ -67,6 +67,12 @@ ); } + @Test public void loadClassWithAssociatedScript() throws Exception { + assertExec("ko is defined", "test", true, + Script.class.getName(), "checkNotNull__Z" + ); + } + private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception { Object ret = null; try { diff -r a5cd79ee1d96 -r 80a388c8c27b vm/src/test/resources/org/apidesign/vm4brwsr/ko.js --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/resources/org/apidesign/vm4brwsr/ko.js Mon Jan 21 13:39:23 2013 +0100 @@ -0,0 +1,21 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ + +this.ko = {}; + +