/*
* $Id: LuaState.java,v 1.9 2006/12/22 14:06:40 thiago Exp $
* Copyright (C) 2003-2007 Kepler Project.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.keplerproject.luajava;
/**
* LuaState if the main class of LuaJava for the Java developer.
* LuaState is a mapping of most of Lua's C API functions.
* LuaState also provides many other functions that will be used to manipulate
* objects between Lua and Java.
* @author Thiago Ponte
*/
public class LuaState
{
private final static String LUAJAVA_LIB = "luajava";
final public static Integer LUA_GLOBALSINDEX = new Integer(-10002);
final public static Integer LUA_REGISTRYINDEX = new Integer(-10000);
final public static Integer LUA_TNONE = new Integer(-1);
final public static Integer LUA_TNIL = new Integer(0);
final public static Integer LUA_TBOOLEAN = new Integer(1);
final public static Integer LUA_TLIGHTUSERDATA = new Integer(2);
final public static Integer LUA_TNUMBER = new Integer(3);
final public static Integer LUA_TSTRING = new Integer(4);
final public static Integer LUA_TTABLE = new Integer(5);
final public static Integer LUA_TFUNCTION = new Integer(6);
final public static Integer LUA_TUSERDATA = new Integer(7);
final public static Integer LUA_TTHREAD = new Integer(8);
/**
* Specifies that an unspecified (multiple) number of return arguments
* will be returned by a call.
*/
final public static Integer LUA_MULTRET = new Integer(-1);
/*
* error codes for `lua_load' and `lua_pcall'
*/
/**
* a runtime error.
*/
final public static Integer LUA_ERRRUN = new Integer(1);
/**
*
*/
final public static Integer LUA_YIELD = new Integer(2);
/**
* syntax error during pre-compilation.
*/
final public static Integer LUA_ERRSYNTAX = new Integer(3);
/**
* memory allocation error. For such errors, Lua does not call
* the error handler function.
*/
final public static Integer LUA_ERRMEM = new Integer(4);
/**
* error while running the error handler function.
*/
final public static Integer LUA_ERRERR = new Integer(5);
/**
* Opens the library containing the luajava API
*/
static
{
System.loadLibrary(LUAJAVA_LIB);
}
private CPtr luaState;
private int stateId;
/**
* Constructor to instance a new LuaState and initialize it with LuaJava's functions
* @param stateId
*/
protected LuaState(int stateId)
{
luaState = _open();
luajava_open(luaState, stateId);
this.stateId = stateId;
}
/**
* Receives a existing state and initializes it
* @param luaState
*/
protected LuaState(CPtr luaState)
{
this.luaState = luaState;
this.stateId = LuaStateFactory.insertLuaState(this);
luajava_open(luaState, stateId);
}
/**
* Closes state and removes the object from the LuaStateFactory
*/
public synchronized void close()
{
LuaStateFactory.removeLuaState(stateId);
_close(luaState);
this.luaState = null;
}
/**
* Returns <code>true</code> if state is closed.
*/
public synchronized boolean isClosed()
{
return luaState == null;
}
/**
* Return the long representing the LuaState pointer
* @return long
*/
public long getCPtrPeer()
{
return (luaState != null)? luaState.getPeer() : 0;
}
/********************* Lua Native Interface *************************/
private synchronized native CPtr _open();
private synchronized native void _close(CPtr ptr);
private synchronized native CPtr _newthread(CPtr ptr);
// Stack manipulation
private synchronized native int _getTop(CPtr ptr);
private synchronized native void _setTop(CPtr ptr, int idx);
private synchronized native void _pushValue(CPtr ptr, int idx);
private synchronized native void _remove(CPtr ptr, int idx);
private synchronized native void _insert(CPtr ptr, int idx);
private synchronized native void _replace(CPtr ptr, int idx);
private synchronized native int _checkStack(CPtr ptr, int sz);
private synchronized native void _xmove(CPtr from, CPtr to, int n);
// Access functions
private synchronized native int _isNumber(CPtr ptr, int idx);
private synchronized native int _isString(CPtr ptr, int idx);
private synchronized native int _isCFunction(CPtr ptr, int idx);
private synchronized native int _isUserdata(CPtr ptr, int idx);
private synchronized native int _type(CPtr ptr, int idx);
private synchronized native String _typeName(CPtr ptr, int tp);
private synchronized native int _equal(CPtr ptr, int idx1, int idx2);
private synchronized native int _rawequal(CPtr ptr, int idx1, int idx2);
private synchronized native int _lessthan(CPtr ptr, int idx1, int idx2);
private synchronized native double _toNumber(CPtr ptr, int idx);
private synchronized native int _toInteger(CPtr ptr, int idx);
private synchronized native int _toBoolean(CPtr ptr, int idx);
private synchronized native String _toString(CPtr ptr, int idx);
private synchronized native int _objlen(CPtr ptr, int idx);
private synchronized native CPtr _toThread(CPtr ptr, int idx);
// Push functions
private synchronized native void _pushNil(CPtr ptr);
private synchronized native void _pushNumber(CPtr ptr, double number);
private synchronized native void _pushInteger(CPtr ptr, int integer);
private synchronized native void _pushString(CPtr ptr, String str);
private synchronized native void _pushString(CPtr ptr, byte[] bytes, int n);
private synchronized native void _pushBoolean(CPtr ptr, int bool);
// Get functions
private synchronized native void _getTable(CPtr ptr, int idx);
private synchronized native void _getField(CPtr ptr, int idx, String k);
private synchronized native void _rawGet(CPtr ptr, int idx);
private synchronized native void _rawGetI(CPtr ptr, int idx, int n);
private synchronized native void _createTable(CPtr ptr, int narr, int nrec);
private synchronized native int _getMetaTable(CPtr ptr, int idx);
private synchronized native void _getFEnv(CPtr ptr, int idx);
// Set functions
private synchronized native void _setTable(CPtr ptr, int idx);
private synchronized native void _setField(CPtr ptr, int idx, String k);
private synchronized native void _rawSet(CPtr ptr, int idx);
private synchronized native void _rawSetI(CPtr ptr, int idx, int n);
private synchronized native int _setMetaTable(CPtr ptr, int idx);
private synchronized native int _setFEnv(CPtr ptr, int idx);
private synchronized native void _call(CPtr ptr, int nArgs, int nResults);
private synchronized native int _pcall(CPtr ptr, int nArgs, int Results, int errFunc);
// Coroutine Functions
priv