From 706d9ba47e11a64b78bf0f06e7dc87222fe6a231 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Mon, 21 Jan 2013 21:37:44 +0000 Subject: [PATCH] Supply environment as extra argument when loading functions. --- README.html | 130 +++++++++++++++++---- examples/jse/hyperbolic.java | 65 +++++++++++ examples/lua/hyperbolicapp.lua | 19 +++ src/core/org/luaj/vm2/lib/LibFunction.java | 75 ++++++------ src/core/org/luaj/vm2/lib/PackageLib.java | 55 ++++----- version.properties | 2 +- 6 files changed, 259 insertions(+), 87 deletions(-) create mode 100644 examples/jse/hyperbolic.java create mode 100644 examples/lua/hyperbolicapp.lua diff --git a/README.html b/README.html index 6b480d73..0e25e618 100644 --- a/README.html +++ b/README.html @@ -16,7 +16,7 @@ Getting Started with LuaJ -James Roseborough, Ian Farmer, Version 3.0-alpha1 +James Roseborough, Ian Farmer, Version 3.0-alpha2

Copyright © 2009-2012 Luaj.org. @@ -117,7 +117,7 @@ in comparison with the standard C distribution. 16.794 11.274 Java - java -cp luaj-jse-3.0-alpha1.jar;bcel-5.2.jar lua -b fannkuch.lua 10 + java -cp luaj-jse-3.0-alpha2.jar;bcel-5.2.jar lua -b fannkuch.lua 10 @@ -127,7 +127,7 @@ in comparison with the standard C distribution. 36.894 15.163 - java -cp luaj-jse-3.0-alpha1.jar lua -n fannkuch.lua 10 + java -cp luaj-jse-3.0-alpha2.jar lua -n fannkuch.lua 10 lua 5.1.4 @@ -183,7 +183,7 @@ It is also faster than Java-lua implementations Jill, Kahlua, and Mochalua for a From the main distribution directory line type:

-	java -cp lib/luaj-jse-3.0-alpha1.jar lua examples/lua/hello.lua
+	java -cp lib/luaj-jse-3.0-alpha2.jar lua examples/lua/hello.lua
 

@@ -195,7 +195,7 @@ You should see the following output: To see how luaj can be used to acccess most Java API's including swing, try:

-	java -cp lib/luaj-jse-3.0-alpha1.jar lua examples/lua/swingapp.lua
+	java -cp lib/luaj-jse-3.0-alpha2.jar lua examples/lua/swingapp.lua
 

Compile lua source to lua bytecode

@@ -204,8 +204,8 @@ To see how luaj can be used to acccess most Java API's including swing, try: From the main distribution directory line type:
-	java -cp lib/luaj-jse-3.0-alpha1.jar luac examples/lua/hello.lua
-	java -cp lib/luaj-jse-3.0-alpha1.jar lua luac.out
+	java -cp lib/luaj-jse-3.0-alpha2.jar luac examples/lua/hello.lua
+	java -cp lib/luaj-jse-3.0-alpha2.jar lua luac.out
 

@@ -219,8 +219,8 @@ Luaj can compile lua sources or binaries directly to java bytecode if the bcel l

 	ant bcel-lib
-	java -cp "lib/luaj-jse-3.0-alpha1.jar;lib/bcel-5.2.jar" luajc -s examples/lua -d . hello.lua
-	java -cp "lib/luaj-jse-3.0-alpha1.jar;." lua -l hello
+	java -cp "lib/luaj-jse-3.0-alpha2.jar;lib/bcel-5.2.jar" luajc -s examples/lua -d . hello.lua
+	java -cp "lib/luaj-jse-3.0-alpha2.jar;." lua -l hello
 

@@ -231,7 +231,7 @@ but the compiled classes must be in the class path at runtime, unless runtime ji

Lua scripts can also be run directly in this mode without precompiling using the lua command with the -b option and providing the bcel library in the class path:

-	java -cp "lib/luaj-jse-3.0-alpha1.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua
+	java -cp "lib/luaj-jse-3.0-alpha2.jar;lib/bcel-5.2.jar" lua -b examples/lua/hello.lua
 
@@ -256,7 +256,7 @@ A simple example may be found in

-You must include the library lib/luaj-jse-3.0-alpha1.jar in your class path. +You must include the library lib/luaj-jse-3.0-alpha2.jar in your class path.

Run a script in a MIDlet

@@ -283,7 +283,7 @@ A simple example may be found in

-You must include the library lib/luaj-jme-3.0-alpha1.jar in your midlet jar. +You must include the library lib/luaj-jme-3.0-alpha2.jar in your midlet jar.

An ant script to build and run the midlet is in @@ -311,7 +311,7 @@ The standard use of JSR-223 scripting engines may be used: All standard aspects of script engines including compiled statements should be supported.

-You must include the library lib/luaj-jse-3.0-alpha1.jar in your class path. +You must include the library lib/luaj-jse-3.0-alpha2.jar in your class path.

A working example may be found in @@ -323,7 +323,7 @@ To compile and run it using Java 1.6 or higher:

 	javac examples/jse/ScriptEngineSample.java
-	java -cp "lib/luaj-jse-3.0-alpha1.jar;examples/jse" ScriptEngineSample
+	java -cp "lib/luaj-jse-3.0-alpha2.jar;examples/jse" ScriptEngineSample
 

Excluding the lua bytecode compiler

@@ -487,7 +487,7 @@ The following lua script will open a swing frame on Java SE: See a longer sample in examples/lua/swingapp.lua for details, including a simple animation loop, rendering graphics, mouse and key handling, and image loading. Or try running it using:
-	java -cp lib/luaj-jse-3.0-alpha1.jar lua examples/lua/swingapp.lua
+	java -cp lib/luaj-jse-3.0-alpha2.jar lua examples/lua/swingapp.lua
 

@@ -574,11 +574,11 @@ Each of these functions has an abstract method that must be implemented, and argument fixup is done automatically by the classes as each Java function is invoked.

-For example, to implement a "hello, world" function, we could supply: +An example of a function with no arguments but a useful return value might be:

-	pubic class hello extends ZeroArgFunction {
+	pubic class hostname extends ZeroArgFunction {
 		public LuaValue call() {
-			env.get("print").call(valueOf("hello, world"));
+			return valueOf(java.net.InetAddress.getLocalHost().getHostName());
 		}
 	}
 
@@ -589,20 +589,90 @@ by the instantiating object whenever default loading is used.

Calling this function from lua could be done by:

 
-	require( 'hello' )()
+	local hostname = require( 'hostname' )
 
while calling this function from Java would look like:
 
-	new hello().call();
+	new hostname().call();
 
Note that in both the lua and Java case, extra arguments will be ignored, and the function will be called. Also, no virtual machine instance is necessary to call the function. To allow for arguments, or return multiple values, extend one of the other base classes. + +

Libraries of Java Functions

+When require() is called, it will first attempt to load the module as a Java class that implements LuaFunction. +To succeed, the following requirements must be met: + + +

+If luaj can find a class that meets these critera, it will instantiate it, cast it to LuaFunction +then call() the instance with two arguments: +the modname used in the call to require(), and the environment for that function. +The Java may use these values however it wishes. A typical case is to create named functions +in the environment that can be called from lua. + +

+A complete example of Java code for a simple toy library is in examples/jse/hyperbolic.java +

+import org.luaj.vm2.LuaValue;
+import org.luaj.vm2.lib.*;
+
+public class hyperbolic extends TwoArgFunction {
+
+	public hyperbolic() {}
+
+	public LuaValue call(LuaValue modname, LuaValue env) {
+		LuaValue library = tableOf();
+		library.set( "sinh", new sinh() );
+		library.set( "cosh", new cosh() );
+		env.set( "hyperbolic", library );
+		return library;
+	}
+
+	static class sinh extends OneArgFunction {
+		public LuaValue call(LuaValue x) {
+			return LuaValue.valueOf(Math.sinh(x.checkdouble()));
+		}
+	}
+	
+	static class cosh extends OneArgFunction {
+		public LuaValue call(LuaValue x) {
+			return LuaValue.valueOf(Math.cosh(x.checkdouble()));
+		}
+	}
+}
+
+ +In this case the call to require invokes the library itself to initialize it. The library implementation +puts entries into a table, and stores this table in the environment. + +

+The lua script used to load and test it is in examples/lua/hyperbolicapp.lua +

+	require 'hyperbolic'
+
+	print('hyperbolic', hyperbolic)
+	print('hyperbolic.sinh', hyperbolic.sinh)
+	print('hyperbolic.cosh', hyperbolic.cosh)
+
+	print('sinh(0.5)', hyperbolic.sinh(0.5))
+	print('cosh(0.5)', hyperbolic.cosh(0.5))
+
+ +For this example to work the code in hyperbolic.java must be compiled and put on the class path.

Closures

-Closures still exist in this framework, but are optional, and are only used to implement lua bytecode execution. +Closures still exist in this framework, but are optional, and are only used to implement lua bytecode execution, +and is generally not directly manipulated by the user of luaj. +

+See the org.luaj.vm2.LuaClosure +javadoc for details on using that class directly.

6 - Parser

@@ -712,11 +782,13 @@ and LuaForge:   2.0 +   2.0.1 +   2.0.2