From 1bc4acdb89b9422161cc8baae61542adcd1938d5 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Tue, 14 Jan 2014 07:15:26 +0000 Subject: [PATCH] Add sample applet build file. --- build-applet.xml | 116 ++++++++++++++++++++++ examples/jse/SampleApplet.java | 37 ++++++- src/core/org/luaj/vm2/lib/PackageLib.java | 7 +- 3 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 build-applet.xml diff --git a/build-applet.xml b/build-applet.xml new file mode 100644 index 00000000..14cf984a --- /dev/null +++ b/build-applet.xml @@ -0,0 +1,116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -injars ${build.dir}/${script.name}-unobfuscated.jar + -outjars ${build.dir}/${script.name}.jar + -libraryjars ${java.home}/lib/rt.jar + -overloadaggressively + -repackageclasses '' + -allowaccessmodification + -printmapping ${build.dir}/mapping.txt + + -keep public class * extends java.applet.Applet + + -target 1.4 + + + + + + + + + + + Luaj Sample Applet + +

Luaj Sample Applet

+ Requires browser that supports applets. + ${script.name} + + + + + + +
+
+ + + + + + + + + + + + +
diff --git a/examples/jse/SampleApplet.java b/examples/jse/SampleApplet.java index fca4914e..b2ec9310 100644 --- a/examples/jse/SampleApplet.java +++ b/examples/jse/SampleApplet.java @@ -1,8 +1,11 @@ import java.applet.Applet; import java.awt.Graphics; +import java.io.InputStream; +import java.net.URL; import org.luaj.vm2.Globals; import org.luaj.vm2.LuaValue; +import org.luaj.vm2.lib.ResourceFinder; import org.luaj.vm2.lib.jse.CoerceJavaToLua; import org.luaj.vm2.lib.jse.JsePlatform; @@ -35,7 +38,7 @@ import org.luaj.vm2.lib.jse.JsePlatform; * @see LuaValue * @see Applet */ -public class SampleApplet extends Applet { +public class SampleApplet extends Applet implements ResourceFinder { private static final long serialVersionUID = 1L; Globals globals; @@ -57,7 +60,12 @@ public class SampleApplet extends Applet { System.out.println("Loading " + script); // Construct globals to use. - globals = JsePlatform.debugGlobals(); + globals = JsePlatform.standardGlobals(); + + // Use custom resource finder. + globals.FINDER = this; + + // Look up and save the handy pcall method. pcall = globals.get("pcall"); // Load and execute the script, supplying this Applet as the only @@ -94,4 +102,29 @@ public class SampleApplet extends Applet { coerced == g ? graphics : (graphics = CoerceJavaToLua .coerce(coerced = g))); } + + // ResourceFinder implementation. + public InputStream findResource(String filename) { + InputStream stream = findAsResource(filename); + if (stream != null) return stream; + stream = findAsDocument(filename); + if (stream != null) return stream; + System.err.println("Failed to find resource " + filename); + return null; + } + + private InputStream findAsResource(String filename) { + System.out.println("Looking for jar resource /"+filename); + return getClass().getResourceAsStream("/"+filename); + } + + private InputStream findAsDocument(String filename) { + try { + final URL base = getDocumentBase(); + System.out.println("Looking in document base "+base); + return new URL(base, filename).openStream(); + } catch (Exception e) { + return null; + } + } } diff --git a/src/core/org/luaj/vm2/lib/PackageLib.java b/src/core/org/luaj/vm2/lib/PackageLib.java index 666c2e4d..8a003dc5 100644 --- a/src/core/org/luaj/vm2/lib/PackageLib.java +++ b/src/core/org/luaj/vm2/lib/PackageLib.java @@ -81,8 +81,13 @@ public class PackageLib extends TwoArgFunction { /** The default value to use for package.path. This can be set with the system property * "luaj.package.path", and is "?.lua" by default. */ - public static String DEFAULT_LUA_PATH = System.getProperty("luaj.package.path"); + public static String DEFAULT_LUA_PATH; static { + try { + DEFAULT_LUA_PATH = System.getProperty("luaj.package.path"); + } catch (Exception e) { + System.out.println(e.toString()); + } if (DEFAULT_LUA_PATH == null) DEFAULT_LUA_PATH = "?.lua"; }