Add "Java API" like "C API" for use with JavaFunctions.
This commit is contained in:
@@ -78,9 +78,9 @@ final class Builtin extends LFunction {
|
||||
break;
|
||||
case PCALL: {
|
||||
int n = vm.getArgCount();
|
||||
int s = vm.lua_pcall( n-1, Lua.LUA_MULTRET );
|
||||
int s = vm.pcall( n-1, Lua.LUA_MULTRET, 0 );
|
||||
if ( s != 0 ) {
|
||||
LValue v = vm.lua_tolvalue(-1);
|
||||
LValue v = vm.topointer(-1);
|
||||
vm.setResult( LBoolean.FALSE );
|
||||
vm.push( v );
|
||||
} else {
|
||||
|
||||
74
src/main/java/lua/JavaFunction.java
Normal file
74
src/main/java/lua/JavaFunction.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package lua;
|
||||
|
||||
import lua.value.LFunction;
|
||||
|
||||
|
||||
/**
|
||||
Type for Java functions.
|
||||
|
||||
|
||||
<p>
|
||||
In order to communicate properly with Lua,
|
||||
a Java function must use the following protocol,
|
||||
which defines the way parameters and results are passed:
|
||||
a Java function receives its arguments from Lua in its stack
|
||||
in direct order (the first argument is pushed first).
|
||||
So, when the function starts,
|
||||
<code>lua_gettop(L)</code> returns the number of arguments received by the function.
|
||||
The first argument (if any) is at index 1
|
||||
and its last argument is at index <code>lua_gettop(L)</code>.
|
||||
To return values to Lua, a Java function just pushes them onto the stack,
|
||||
in direct order (the first result is pushed first),
|
||||
and returns the number of results.
|
||||
Any other value in the stack below the results will be properly
|
||||
discarded by Lua.
|
||||
Like a Lua function, a Java function called by Lua can also return
|
||||
many results.
|
||||
|
||||
|
||||
<p>
|
||||
As an example, the following function receives a variable number
|
||||
of numerical arguments and returns their average and sum:
|
||||
|
||||
<pre><code>
|
||||
int foo (VM lua) {
|
||||
int n = lua.gettop(); // number of arguments
|
||||
double sum = 0;
|
||||
int i;
|
||||
for (i = 1; i <= n; i++) {
|
||||
if (!lua.isnumber(L, i)) {
|
||||
lua.pushstring(L, "incorrect argument");
|
||||
lua.error(L);
|
||||
}
|
||||
sum += lua.tonumber(L, i);
|
||||
}
|
||||
lua.pushnumber(L, sum/n); // first result
|
||||
lua.pushnumber(L, sum); // second result
|
||||
return 2; // number of results
|
||||
}
|
||||
</code></pre>
|
||||
*/
|
||||
|
||||
abstract public class JavaFunction extends LFunction {
|
||||
|
||||
/**
|
||||
* Called to invoke a JavaFunction.
|
||||
*
|
||||
* The implementation should manipulate the stack
|
||||
* via the VM Java API in the same way that lua_CFunctions
|
||||
* do so in standard lua.
|
||||
*
|
||||
* @param lua the LuaState calling this function.
|
||||
* @return number of results pushed onto the stack.
|
||||
*/
|
||||
abstract public int invoke( VM lua );
|
||||
|
||||
/**
|
||||
* Set up a Java invocation, and fix up the results
|
||||
* when it returns.
|
||||
*/
|
||||
public boolean luaStackCall(VM vm) {
|
||||
vm.invokeJavaFunction( this );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -89,8 +89,8 @@ public class DebugStackState extends StackState implements DebugRequestListener
|
||||
|
||||
|
||||
// override and fill in line number info
|
||||
public void lua_error(String message) {
|
||||
super.lua_error( getFileLine(cc)+": "+message );
|
||||
public void error(String message) {
|
||||
super.error( getFileLine(cc)+": "+message );
|
||||
}
|
||||
|
||||
private void printLuaTrace() {
|
||||
|
||||
@@ -15,14 +15,14 @@ public class LFunction extends LValue {
|
||||
vm.push( table );
|
||||
vm.push( key );
|
||||
vm.push( val );
|
||||
vm.lua_call( 3, 0 );
|
||||
vm.call( 3, 0 );
|
||||
}
|
||||
|
||||
public void luaGetTable(VM vm, LValue table, LValue key) {
|
||||
vm.push( this );
|
||||
vm.push( table );
|
||||
vm.push( key );
|
||||
vm.lua_call( 2, 1 );
|
||||
vm.call( 2, 1 );
|
||||
}
|
||||
|
||||
public int luaGetType() {
|
||||
|
||||
@@ -33,7 +33,7 @@ public class LValue {
|
||||
// perform a lua call, return true if the call is to a lua function, false
|
||||
// if it ran to completion.
|
||||
public boolean luaStackCall(VM vm) {
|
||||
vm.lua_error("attempt to call "+this);
|
||||
vm.error("attempt to call "+this);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user