0001: /*
0002: * $Id: LuaState.java,v 1.9 2006/12/22 14:06:40 thiago Exp $
0003: * Copyright (C) 2003-2007 Kepler Project.
0004: *
0005: * Permission is hereby granted, free of charge, to any person obtaining
0006: * a copy of this software and associated documentation files (the
0007: * "Software"), to deal in the Software without restriction, including
0008: * without limitation the rights to use, copy, modify, merge, publish,
0009: * distribute, sublicense, and/or sell copies of the Software, and to
0010: * permit persons to whom the Software is furnished to do so, subject to
0011: * the following conditions:
0012: *
0013: * The above copyright notice and this permission notice shall be
0014: * included in all copies or substantial portions of the Software.
0015: *
0016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0021: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0022: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023: */
0024:
0025: package org.keplerproject.luajava;
0026:
0027: /**
0028: * LuaState if the main class of LuaJava for the Java developer.
0029: * LuaState is a mapping of most of Lua's C API functions.
0030: * LuaState also provides many other functions that will be used to manipulate
0031: * objects between Lua and Java.
0032: * @author Thiago Ponte
0033: */
0034: public class LuaState {
0035: private final static String LUAJAVA_LIB = "luajava-1.1";
0036:
0037: final public static Integer LUA_GLOBALSINDEX = new Integer(-10002);
0038: final public static Integer LUA_REGISTRYINDEX = new Integer(-10000);
0039:
0040: final public static Integer LUA_TNONE = new Integer(-1);
0041: final public static Integer LUA_TNIL = new Integer(0);
0042: final public static Integer LUA_TBOOLEAN = new Integer(1);
0043: final public static Integer LUA_TLIGHTUSERDATA = new Integer(2);
0044: final public static Integer LUA_TNUMBER = new Integer(3);
0045: final public static Integer LUA_TSTRING = new Integer(4);
0046: final public static Integer LUA_TTABLE = new Integer(5);
0047: final public static Integer LUA_TFUNCTION = new Integer(6);
0048: final public static Integer LUA_TUSERDATA = new Integer(7);
0049: final public static Integer LUA_TTHREAD = new Integer(8);
0050:
0051: /**
0052: * Specifies that an unspecified (multiple) number of return arguments
0053: * will be returned by a call.
0054: */
0055: final public static Integer LUA_MULTRET = new Integer(-1);
0056:
0057: /*
0058: * error codes for `lua_load' and `lua_pcall'
0059: */
0060: /**
0061: * a runtime error.
0062: */
0063: final public static Integer LUA_ERRRUN = new Integer(1);
0064:
0065: /**
0066: *
0067: */
0068: final public static Integer LUA_YIELD = new Integer(2);
0069:
0070: /**
0071: * syntax error during pre-compilation.
0072: */
0073: final public static Integer LUA_ERRSYNTAX = new Integer(3);
0074:
0075: /**
0076: * memory allocation error. For such errors, Lua does not call
0077: * the error handler function.
0078: */
0079: final public static Integer LUA_ERRMEM = new Integer(4);
0080:
0081: /**
0082: * error while running the error handler function.
0083: */
0084: final public static Integer LUA_ERRERR = new Integer(5);
0085:
0086: /**
0087: * Opens the library containing the luajava API
0088: */
0089: static {
0090: System.loadLibrary(LUAJAVA_LIB);
0091: }
0092:
0093: private CPtr luaState;
0094:
0095: private int stateId;
0096:
0097: /**
0098: * Constructor to instance a new LuaState and initialize it with LuaJava's functions
0099: * @param stateId
0100: */
0101: protected LuaState(int stateId) {
0102: luaState = _open();
0103: luajava_open(luaState, stateId);
0104: this .stateId = stateId;
0105: }
0106:
0107: /**
0108: * Receives a existing state and initializes it
0109: * @param luaState
0110: */
0111: protected LuaState(CPtr luaState) {
0112: this .luaState = luaState;
0113: this .stateId = LuaStateFactory.insertLuaState(this );
0114: luajava_open(luaState, stateId);
0115: }
0116:
0117: /**
0118: * Closes state and removes the object from the LuaStateFactory
0119: */
0120: public synchronized void close() {
0121: LuaStateFactory.removeLuaState(stateId);
0122: _close(luaState);
0123: this .luaState = null;
0124: }
0125:
0126: /**
0127: * Returns <code>true</code> if state is closed.
0128: */
0129: public synchronized boolean isClosed() {
0130: return luaState == null;
0131: }
0132:
0133: /**
0134: * Return the long representing the LuaState pointer
0135: * @return long
0136: */
0137: public long getCPtrPeer() {
0138: return (luaState != null) ? luaState.getPeer() : 0;
0139: }
0140:
0141: /********************* Lua Native Interface *************************/
0142:
0143: private synchronized native CPtr _open();
0144:
0145: private synchronized native void _close(CPtr ptr);
0146:
0147: private synchronized native CPtr _newthread(CPtr ptr);
0148:
0149: // Stack manipulation
0150: private synchronized native int _getTop(CPtr ptr);
0151:
0152: private synchronized native void _setTop(CPtr ptr, int idx);
0153:
0154: private synchronized native void _pushValue(CPtr ptr, int idx);
0155:
0156: private synchronized native void _remove(CPtr ptr, int idx);
0157:
0158: private synchronized native void _insert(CPtr ptr, int idx);
0159:
0160: private synchronized native void _replace(CPtr ptr, int idx);
0161:
0162: private synchronized native int _checkStack(CPtr ptr, int sz);
0163:
0164: private synchronized native void _xmove(CPtr from, CPtr to, int n);
0165:
0166: // Access functions
0167: private synchronized native int _isNumber(CPtr ptr, int idx);
0168:
0169: private synchronized native int _isString(CPtr ptr, int idx);
0170:
0171: private synchronized native int _isCFunction(CPtr ptr, int idx);
0172:
0173: private synchronized native int _isUserdata(CPtr ptr, int idx);
0174:
0175: private synchronized native int _type(CPtr ptr, int idx);
0176:
0177: private synchronized native String _typeName(CPtr ptr, int tp);
0178:
0179: private synchronized native int _equal(CPtr ptr, int idx1, int idx2);
0180:
0181: private synchronized native int _rawequal(CPtr ptr, int idx1,
0182: int idx2);
0183:
0184: private synchronized native int _lessthan(CPtr ptr, int idx1,
0185: int idx2);
0186:
0187: private synchronized native double _toNumber(CPtr ptr, int idx);
0188:
0189: private synchronized native int _toInteger(CPtr ptr, int idx);
0190:
0191: private synchronized native int _toBoolean(CPtr ptr, int idx);
0192:
0193: private synchronized native String _toString(CPtr ptr, int idx);
0194:
0195: private synchronized native int _objlen(CPtr ptr, int idx);
0196:
0197: private synchronized native CPtr _toThread(CPtr ptr, int idx);
0198:
0199: // Push functions
0200: private synchronized native void _pushNil(CPtr ptr);
0201:
0202: private synchronized native void _pushNumber(CPtr ptr, double number);
0203:
0204: private synchronized native void _pushInteger(CPtr ptr, int integer);
0205:
0206: private synchronized native void _pushString(CPtr ptr, String str);
0207:
0208: private synchronized native void _pushString(CPtr ptr,
0209: byte[] bytes, int n);
0210:
0211: private synchronized native void _pushBoolean(CPtr ptr, int bool);
0212:
0213: // Get functions
0214: private synchronized native void _getTable(CPtr ptr, int idx);
0215:
0216: private synchronized native void _getField(CPtr ptr, int idx,
0217: String k);
0218:
0219: private synchronized native void _rawGet(CPtr ptr, int idx);
0220:
0221: private synchronized native void _rawGetI(CPtr ptr, int idx, int n);
0222:
0223: private synchronized native void _createTable(CPtr ptr, int narr,
0224: int nrec);
0225:
0226: private synchronized native int _getMetaTable(CPtr ptr, int idx);
0227:
0228: private synchronized native void _getFEnv(CPtr ptr, int idx);
0229:
0230: // Set functions
0231: private synchronized native void _setTable(CPtr ptr, int idx);
0232:
0233: private synchronized native void _setField(CPtr ptr, int idx,
0234: String k);
0235:
0236: private synchronized native void _rawSet(CPtr ptr, int idx);
0237:
0238: private synchronized native void _rawSetI(CPtr ptr, int idx, int n);
0239:
0240: private synchronized native int _setMetaTable(CPtr ptr, int idx);
0241:
0242: private synchronized native int _setFEnv(CPtr ptr, int idx);
0243:
0244: private synchronized native void _call(CPtr ptr, int nArgs,
0245: int nResults);
0246:
0247: private synchronized native int _pcall(CPtr ptr, int nArgs,
0248: int Results, int errFunc);
0249:
0250: // Coroutine Functions
0251: private synchronized native int _yield(CPtr ptr, int nResults);
0252:
0253: private synchronized native int _resume(CPtr ptr, int nargs);
0254:
0255: private synchronized native int _status(CPtr ptr);
0256:
0257: // Gargabe Collection Functions
0258: final public static Integer LUA_GCSTOP = new Integer(0);
0259: final public static Integer LUA_GCRESTART = new Integer(1);
0260: final public static Integer LUA_GCCOLLECT = new Integer(2);
0261: final public static Integer LUA_GCCOUNT = new Integer(3);
0262: final public static Integer LUA_GCCOUNTB = new Integer(4);
0263: final public static Integer LUA_GCSTEP = new Integer(5);
0264: final public static Integer LUA_GCSETPAUSE = new Integer(6);
0265: final public static Integer LUA_GCSETSTEPMUL = new Integer(7);
0266:
0267: private synchronized native int _gc(CPtr ptr, int what, int data);
0268:
0269: // Miscellaneous Functions
0270: private synchronized native int _error(CPtr ptr);
0271:
0272: private synchronized native int _next(CPtr ptr, int idx);
0273:
0274: private synchronized native void _concat(CPtr ptr, int n);
0275:
0276: // Some macros
0277: private synchronized native void _pop(CPtr ptr, int n);
0278:
0279: private synchronized native void _newTable(CPtr ptr);
0280:
0281: private synchronized native int _strlen(CPtr ptr, int idx);
0282:
0283: private synchronized native int _isFunction(CPtr ptr, int idx);
0284:
0285: private synchronized native int _isTable(CPtr ptr, int idx);
0286:
0287: private synchronized native int _isNil(CPtr ptr, int idx);
0288:
0289: private synchronized native int _isBoolean(CPtr ptr, int idx);
0290:
0291: private synchronized native int _isThread(CPtr ptr, int idx);
0292:
0293: private synchronized native int _isNone(CPtr ptr, int idx);
0294:
0295: private synchronized native int _isNoneOrNil(CPtr ptr, int idx);
0296:
0297: private synchronized native void _setGlobal(CPtr ptr, String name);
0298:
0299: private synchronized native void _getGlobal(CPtr ptr, String name);
0300:
0301: private synchronized native int _getGcCount(CPtr ptr);
0302:
0303: // LuaLibAux
0304: private synchronized native int _LdoFile(CPtr ptr, String fileName);
0305:
0306: private synchronized native int _LdoString(CPtr ptr, String string);
0307:
0308: //private synchronized native int _doBuffer(CPtr ptr, byte[] buff, long sz, String n);
0309:
0310: private synchronized native int _LgetMetaField(CPtr ptr, int obj,
0311: String e);
0312:
0313: private synchronized native int _LcallMeta(CPtr ptr, int obj,
0314: String e);
0315:
0316: private synchronized native int _Ltyperror(CPtr ptr, int nArg,
0317: String tName);
0318:
0319: private synchronized native int _LargError(CPtr ptr, int numArg,
0320: String extraMsg);
0321:
0322: private synchronized native String _LcheckString(CPtr ptr,
0323: int numArg);
0324:
0325: private synchronized native String _LoptString(CPtr ptr,
0326: int numArg, String def);
0327:
0328: private synchronized native double _LcheckNumber(CPtr ptr,
0329: int numArg);
0330:
0331: private synchronized native double _LoptNumber(CPtr ptr,
0332: int numArg, double def);
0333:
0334: private synchronized native int _LcheckInteger(CPtr ptr, int numArg);
0335:
0336: private synchronized native int _LoptInteger(CPtr ptr, int numArg,
0337: int def);
0338:
0339: private synchronized native void _LcheckStack(CPtr ptr, int sz,
0340: String msg);
0341:
0342: private synchronized native void _LcheckType(CPtr ptr, int nArg,
0343: int t);
0344:
0345: private synchronized native void _LcheckAny(CPtr ptr, int nArg);
0346:
0347: private synchronized native int _LnewMetatable(CPtr ptr,
0348: String tName);
0349:
0350: private synchronized native void _LgetMetatable(CPtr ptr,
0351: String tName);
0352:
0353: private synchronized native void _Lwhere(CPtr ptr, int lvl);
0354:
0355: private synchronized native int _Lref(CPtr ptr, int t);
0356:
0357: private synchronized native void _LunRef(CPtr ptr, int t, int ref);
0358:
0359: private synchronized native int _LgetN(CPtr ptr, int t);
0360:
0361: private synchronized native void _LsetN(CPtr ptr, int t, int n);
0362:
0363: private synchronized native int _LloadFile(CPtr ptr, String fileName);
0364:
0365: private synchronized native int _LloadBuffer(CPtr ptr, byte[] buff,
0366: long sz, String name);
0367:
0368: private synchronized native int _LloadString(CPtr ptr, String s);
0369:
0370: private synchronized native String _Lgsub(CPtr ptr, String s,
0371: String p, String r);
0372:
0373: private synchronized native String _LfindTable(CPtr ptr, int idx,
0374: String fname, int szhint);
0375:
0376: private synchronized native void _openBase(CPtr ptr);
0377:
0378: private synchronized native void _openTable(CPtr ptr);
0379:
0380: private synchronized native void _openIo(CPtr ptr);
0381:
0382: private synchronized native void _openOs(CPtr ptr);
0383:
0384: private synchronized native void _openString(CPtr ptr);
0385:
0386: private synchronized native void _openMath(CPtr ptr);
0387:
0388: private synchronized native void _openDebug(CPtr ptr);
0389:
0390: private synchronized native void _openPackage(CPtr ptr);
0391:
0392: private synchronized native void _openLibs(CPtr ptr);
0393:
0394: // Java Interface -----------------------------------------------------
0395:
0396: public LuaState newThread() {
0397: LuaState l = new LuaState(_newthread(luaState));
0398: LuaStateFactory.insertLuaState(l);
0399: return l;
0400: }
0401:
0402: // STACK MANIPULATION
0403:
0404: public int getTop() {
0405: return _getTop(luaState);
0406: }
0407:
0408: public void setTop(int idx) {
0409: _setTop(luaState, idx);
0410: }
0411:
0412: public void pushValue(int idx) {
0413: _pushValue(luaState, idx);
0414: }
0415:
0416: public void remove(int idx) {
0417: _remove(luaState, idx);
0418: }
0419:
0420: public void insert(int idx) {
0421: _insert(luaState, idx);
0422: }
0423:
0424: public void replace(int idx) {
0425: _replace(luaState, idx);
0426: }
0427:
0428: public int checkStack(int sz) {
0429: return _checkStack(luaState, sz);
0430: }
0431:
0432: public void xmove(LuaState to, int n) {
0433: _xmove(luaState, to.luaState, n);
0434: }
0435:
0436: // ACCESS FUNCTION
0437:
0438: public boolean isNumber(int idx) {
0439: return (_isNumber(luaState, idx) != 0);
0440: }
0441:
0442: public boolean isString(int idx) {
0443: return (_isString(luaState, idx) != 0);
0444: }
0445:
0446: public boolean isFunction(int idx) {
0447: return (_isFunction(luaState, idx) != 0);
0448: }
0449:
0450: public boolean isCFunction(int idx) {
0451: return (_isCFunction(luaState, idx) != 0);
0452: }
0453:
0454: public boolean isUserdata(int idx) {
0455: return (_isUserdata(luaState, idx) != 0);
0456: }
0457:
0458: public boolean isTable(int idx) {
0459: return (_isTable(luaState, idx) != 0);
0460: }
0461:
0462: public boolean isBoolean(int idx) {
0463: return (_isBoolean(luaState, idx) != 0);
0464: }
0465:
0466: public boolean isNil(int idx) {
0467: return (_isNil(luaState, idx) != 0);
0468: }
0469:
0470: public boolean isThread(int idx) {
0471: return (_isThread(luaState, idx) != 0);
0472: }
0473:
0474: public boolean isNone(int idx) {
0475: return (_isNone(luaState, idx) != 0);
0476: }
0477:
0478: public boolean isNoneOrNil(int idx) {
0479: return (_isNoneOrNil(luaState, idx) != 0);
0480: }
0481:
0482: public int type(int idx) {
0483: return _type(luaState, idx);
0484: }
0485:
0486: public String typeName(int tp) {
0487: return _typeName(luaState, tp);
0488: }
0489:
0490: public int equal(int idx1, int idx2) {
0491: return _equal(luaState, idx1, idx2);
0492: }
0493:
0494: public int rawequal(int idx1, int idx2) {
0495: return _rawequal(luaState, idx1, idx2);
0496: }
0497:
0498: public int lessthan(int idx1, int idx2) {
0499: return _lessthan(luaState, idx1, idx2);
0500: }
0501:
0502: public double toNumber(int idx) {
0503: return _toNumber(luaState, idx);
0504: }
0505:
0506: public int toInteger(int idx) {
0507: return _toInteger(luaState, idx);
0508: }
0509:
0510: public boolean toBoolean(int idx) {
0511: return (_toBoolean(luaState, idx) != 0);
0512: }
0513:
0514: public String toString(int idx) {
0515: return _toString(luaState, idx);
0516: }
0517:
0518: public int strLen(int idx) {
0519: return _strlen(luaState, idx);
0520: }
0521:
0522: public int objLen(int idx) {
0523: return _objlen(luaState, idx);
0524: }
0525:
0526: public LuaState toThread(int idx) {
0527: return new LuaState(_toThread(luaState, idx));
0528: }
0529:
0530: //PUSH FUNCTIONS
0531:
0532: public void pushNil() {
0533: _pushNil(luaState);
0534: }
0535:
0536: public void pushNumber(double db) {
0537: _pushNumber(luaState, db);
0538: }
0539:
0540: public void pushInteger(int integer) {
0541: _pushInteger(luaState, integer);
0542: }
0543:
0544: public void pushString(String str) {
0545: if (str == null)
0546: _pushNil(luaState);
0547: else
0548: _pushString(luaState, str);
0549: }
0550:
0551: public void pushString(byte[] bytes) {
0552: if (bytes == null)
0553: _pushNil(luaState);
0554: else
0555: _pushString(luaState, bytes, bytes.length);
0556: }
0557:
0558: public void pushBoolean(boolean bool) {
0559: _pushBoolean(luaState, bool ? 1 : 0);
0560: }
0561:
0562: // GET FUNCTIONS
0563:
0564: public void getTable(int idx) {
0565: _getTable(luaState, idx);
0566: }
0567:
0568: public void getField(int idx, String k) {
0569: _getField(luaState, idx, k);
0570: }
0571:
0572: public void rawGet(int idx) {
0573: _rawGet(luaState, idx);
0574: }
0575:
0576: public void rawGetI(int idx, int n) {
0577: _rawGetI(luaState, idx, n);
0578: }
0579:
0580: public void createTable(int narr, int nrec) {
0581: _createTable(luaState, narr, nrec);
0582: }
0583:
0584: public void newTable() {
0585: _newTable(luaState);
0586: }
0587:
0588: // if returns 0, there is no metatable
0589: public int getMetaTable(int idx) {
0590: return _getMetaTable(luaState, idx);
0591: }
0592:
0593: public void getFEnv(int idx) {
0594: _getFEnv(luaState, idx);
0595: }
0596:
0597: // SET FUNCTIONS
0598:
0599: public void setTable(int idx) {
0600: _setTable(luaState, idx);
0601: }
0602:
0603: public void setField(int idx, String k) {
0604: _setField(luaState, idx, k);
0605: }
0606:
0607: public void rawSet(int idx) {
0608: _rawSet(luaState, idx);
0609: }
0610:
0611: public void rawSetI(int idx, int n) {
0612: _rawSetI(luaState, idx, n);
0613: }
0614:
0615: // if returns 0, cannot set the metatable to the given object
0616: public int setMetaTable(int idx) {
0617: return _setMetaTable(luaState, idx);
0618: }
0619:
0620: // if object is not a function returns 0
0621: public int setFEnv(int idx) {
0622: return _setFEnv(luaState, idx);
0623: }
0624:
0625: public void call(int nArgs, int nResults) {
0626: _call(luaState, nArgs, nResults);
0627: }
0628:
0629: // returns 0 if ok of one of the error codes defined
0630: public int pcall(int nArgs, int nResults, int errFunc) {
0631: return _pcall(luaState, nArgs, nResults, errFunc);
0632: }
0633:
0634: public int yield(int nResults) {
0635: return _yield(luaState, nResults);
0636: }
0637:
0638: public int resume(int nArgs) {
0639: return _resume(luaState, nArgs);
0640: }
0641:
0642: public int status() {
0643: return _status(luaState);
0644: }
0645:
0646: public int gc(int what, int data) {
0647: return _gc(luaState, what, data);
0648: }
0649:
0650: public int getGcCount() {
0651: return _getGcCount(luaState);
0652: }
0653:
0654: public int next(int idx) {
0655: return _next(luaState, idx);
0656: }
0657:
0658: public int error() {
0659: return _error(luaState);
0660: }
0661:
0662: public void concat(int n) {
0663: _concat(luaState, n);
0664: }
0665:
0666: // FUNCTION FROM lauxlib
0667: // returns 0 if ok
0668: public int LdoFile(String fileName) {
0669: return _LdoFile(luaState, fileName);
0670: }
0671:
0672: // returns 0 if ok
0673: public int LdoString(String str) {
0674: return _LdoString(luaState, str);
0675: }
0676:
0677: public int LgetMetaField(int obj, String e) {
0678: return _LgetMetaField(luaState, obj, e);
0679: }
0680:
0681: public int LcallMeta(int obj, String e) {
0682: return _LcallMeta(luaState, obj, e);
0683: }
0684:
0685: public int Ltyperror(int nArg, String tName) {
0686: return _Ltyperror(luaState, nArg, tName);
0687: }
0688:
0689: public int LargError(int numArg, String extraMsg) {
0690: return _LargError(luaState, numArg, extraMsg);
0691: }
0692:
0693: public String LcheckString(int numArg) {
0694: return _LcheckString(luaState, numArg);
0695: }
0696:
0697: public String LoptString(int numArg, String def) {
0698: return _LoptString(luaState, numArg, def);
0699: }
0700:
0701: public double LcheckNumber(int numArg) {
0702: return _LcheckNumber(luaState, numArg);
0703: }
0704:
0705: public double LoptNumber(int numArg, double def) {
0706: return _LoptNumber(luaState, numArg, def);
0707: }
0708:
0709: public int LcheckInteger(int numArg) {
0710: return _LcheckInteger(luaState, numArg);
0711: }
0712:
0713: public int LoptInteger(int numArg, int def) {
0714: return _LoptInteger(luaState, numArg, def);
0715: }
0716:
0717: public void LcheckStack(int sz, String msg) {
0718: _LcheckStack(luaState, sz, msg);
0719: }
0720:
0721: public void LcheckType(int nArg, int t) {
0722: _LcheckType(luaState, nArg, t);
0723: }
0724:
0725: public void LcheckAny(int nArg) {
0726: _LcheckAny(luaState, nArg);
0727: }
0728:
0729: public int LnewMetatable(String tName) {
0730: return _LnewMetatable(luaState, tName);
0731: }
0732:
0733: public void LgetMetatable(String tName) {
0734: _LgetMetatable(luaState, tName);
0735: }
0736:
0737: public void Lwhere(int lvl) {
0738: _Lwhere(luaState, lvl);
0739: }
0740:
0741: public int Lref(int t) {
0742: return _Lref(luaState, t);
0743: }
0744:
0745: public void LunRef(int t, int ref) {
0746: _LunRef(luaState, t, ref);
0747: }
0748:
0749: public int LgetN(int t) {
0750: return _LgetN(luaState, t);
0751: }
0752:
0753: public void LsetN(int t, int n) {
0754: _LsetN(luaState, t, n);
0755: }
0756:
0757: public int LloadFile(String fileName) {
0758: return _LloadFile(luaState, fileName);
0759: }
0760:
0761: public int LloadString(String s) {
0762: return _LloadString(luaState, s);
0763: }
0764:
0765: public int LloadBuffer(byte[] buff, String name) {
0766: return _LloadBuffer(luaState, buff, buff.length, name);
0767: }
0768:
0769: public String Lgsub(String s, String p, String r) {
0770: return _Lgsub(luaState, s, p, r);
0771: }
0772:
0773: public String LfindTable(int idx, String fname, int szhint) {
0774: return _LfindTable(luaState, idx, fname, szhint);
0775: }
0776:
0777: //IMPLEMENTED C MACROS
0778:
0779: public void pop(int n) {
0780: //setTop(- (n) - 1);
0781: _pop(luaState, n);
0782: }
0783:
0784: public synchronized void getGlobal(String global) {
0785: // pushString(global);
0786: // getTable(LUA_GLOBALSINDEX.intValue());
0787: _getGlobal(luaState, global);
0788: }
0789:
0790: public synchronized void setGlobal(String name) {
0791: //pushString(name);
0792: //insert(-2);
0793: //setTable(LUA_GLOBALSINDEX.intValue());
0794: _setGlobal(luaState, name);
0795: }
0796:
0797: // Functions to open lua libraries
0798: public void openBase() {
0799: _openBase(luaState);
0800: }
0801:
0802: public void openTable() {
0803: _openTable(luaState);
0804: }
0805:
0806: public void openIo() {
0807: _openIo(luaState);
0808: }
0809:
0810: public void openOs() {
0811: _openOs(luaState);
0812: }
0813:
0814: public void openString() {
0815: _openString(luaState);
0816: }
0817:
0818: public void openMath() {
0819: _openMath(luaState);
0820: }
0821:
0822: public void openDebug() {
0823: _openDebug(luaState);
0824: }
0825:
0826: public void openPackage() {
0827: _openPackage(luaState);
0828: }
0829:
0830: public void openLibs() {
0831: _openLibs(luaState);
0832: }
0833:
0834: /********************** Luajava API Library **********************/
0835:
0836: /**
0837: * Initializes lua State to be used by luajava
0838: * @param cptr
0839: * @param stateId
0840: */
0841: private synchronized native void luajava_open(CPtr cptr, int stateId);
0842:
0843: /**
0844: * Gets a Object from a userdata
0845: * @param L
0846: * @param idx index of the lua stack
0847: * @return Object
0848: */
0849: private synchronized native Object _getObjectFromUserdata(CPtr L,
0850: int idx) throws LuaException;
0851:
0852: /**
0853: * Returns whether a userdata contains a Java Object
0854: * @param L
0855: * @param idx index of the lua stack
0856: * @return boolean
0857: */
0858: private synchronized native boolean _isObject(CPtr L, int idx);
0859:
0860: /**
0861: * Pushes a Java Object into the state stack
0862: * @param L
0863: * @param obj
0864: */
0865: private synchronized native void _pushJavaObject(CPtr L, Object obj);
0866:
0867: /**
0868: * Pushes a JavaFunction into the state stack
0869: * @param L
0870: * @param func
0871: */
0872: private synchronized native void _pushJavaFunction(CPtr L,
0873: JavaFunction func) throws LuaException;
0874:
0875: /**
0876: * Returns whether a userdata contains a Java Function
0877: * @param L
0878: * @param idx index of the lua stack
0879: * @return boolean
0880: */
0881: private synchronized native boolean _isJavaFunction(CPtr L, int idx);
0882:
0883: /**
0884: * Gets a Object from Lua
0885: * @param idx index of the lua stack
0886: * @return Object
0887: * @throws LuaException if the lua object does not represent a java object.
0888: */
0889: public Object getObjectFromUserdata(int idx) throws LuaException {
0890: return _getObjectFromUserdata(luaState, idx);
0891: }
0892:
0893: /**
0894: * Tells whether a lua index contains a java Object
0895: * @param idx index of the lua stack
0896: * @return boolean
0897: */
0898: public boolean isObject(int idx) {
0899: return _isObject(luaState, idx);
0900: }
0901:
0902: /**
0903: * Pushes a Java Object into the lua stack.<br>
0904: * This function does not check if the object is from a class that could
0905: * be represented by a lua type. Eg: java.lang.String could be a lua string.
0906: * @param obj Object to be pushed into lua
0907: */
0908: public void pushJavaObject(Object obj) {
0909: _pushJavaObject(luaState, obj);
0910: }
0911:
0912: /**
0913: * Pushes a JavaFunction into the state stack
0914: * @param func
0915: */
0916: public void pushJavaFunction(JavaFunction func) throws LuaException {
0917: _pushJavaFunction(luaState, func);
0918: }
0919:
0920: /**
0921: * Returns whether a userdata contains a Java Function
0922: * @param idx index of the lua stack
0923: * @return boolean
0924: */
0925: public boolean isJavaFunction(int idx) {
0926: return _isJavaFunction(luaState, idx);
0927: }
0928:
0929: /**
0930: * Pushes into the stack any object value.<br>
0931: * This function checks if the object could be pushed as a lua type, if not
0932: * pushes the java object.
0933: * @param obj
0934: */
0935: public void pushObjectValue(Object obj) throws LuaException {
0936: if (obj == null) {
0937: pushNil();
0938: } else if (obj instanceof Boolean) {
0939: Boolean bool = (Boolean) obj;
0940: pushBoolean(bool.booleanValue());
0941: } else if (obj instanceof Number) {
0942: pushNumber(((Number) obj).doubleValue());
0943: } else if (obj instanceof String) {
0944: pushString((String) obj);
0945: } else if (obj instanceof JavaFunction) {
0946: JavaFunction func = (JavaFunction) obj;
0947: pushJavaFunction(func);
0948: } else if (obj instanceof LuaObject) {
0949: LuaObject ref = (LuaObject) obj;
0950: ref.push();
0951: } else if (obj instanceof byte[]) {
0952: pushString((byte[]) obj);
0953: } else {
0954: pushJavaObject(obj);
0955: }
0956: }
0957:
0958: /**
0959: * Function that returns a Java Object equivalent to the one in the given
0960: * position of the Lua Stack.
0961: * @param idx Index in the Lua Stack
0962: * @return Java object equivalent to the Lua one
0963: */
0964: public synchronized Object toJavaObject(int idx)
0965: throws LuaException {
0966: Object obj = null;
0967:
0968: if (isBoolean(idx)) {
0969: obj = new Boolean(toBoolean(idx));
0970: } else if (type(idx) == LuaState.LUA_TSTRING.intValue()) {
0971: obj = toString(idx);
0972: } else if (isFunction(idx)) {
0973: obj = getLuaObject(idx);
0974: } else if (isTable(idx)) {
0975: obj = getLuaObject(idx);
0976: } else if (type(idx) == LuaState.LUA_TNUMBER.intValue()) {
0977: obj = new Double(toNumber(idx));
0978: } else if (isUserdata(idx)) {
0979: if (isObject(idx)) {
0980: obj = getObjectFromUserdata(idx);
0981: } else {
0982: obj = getLuaObject(idx);
0983: }
0984: } else if (isNil(idx)) {
0985: obj = null;
0986: }
0987:
0988: return obj;
0989: }
0990:
0991: /**
0992: * Creates a reference to an object in the variable globalName
0993: * @param globalName
0994: * @return LuaObject
0995: */
0996: public LuaObject getLuaObject(String globalName) {
0997: return new LuaObject(this , globalName);
0998: }
0999:
1000: /**
1001: * Creates a reference to an object inside another object
1002: * @param parent The Lua Table or Userdata that contains the Field.
1003: * @param name The name that index the field
1004: * @return LuaObject
1005: * @throws LuaException if parent is not a table or userdata
1006: */
1007: public LuaObject getLuaObject(LuaObject parent, String name)
1008: throws LuaException {
1009: if (parent.L.getCPtrPeer() != luaState.getPeer())
1010: throw new LuaException(
1011: "Object must have the same LuaState as the parent!");
1012:
1013: return new LuaObject(parent, name);
1014: }
1015:
1016: /**
1017: * This constructor creates a LuaObject from a table that is indexed by a number.
1018: * @param parent The Lua Table or Userdata that contains the Field.
1019: * @param name The name (number) that index the field
1020: * @return LuaObject
1021: * @throws LuaException When the parent object isn't a Table or Userdata
1022: */
1023: public LuaObject getLuaObject(LuaObject parent, Number name)
1024: throws LuaException {
1025: if (parent.L.getCPtrPeer() != luaState.getPeer())
1026: throw new LuaException(
1027: "Object must have the same LuaState as the parent!");
1028:
1029: return new LuaObject(parent, name);
1030: }
1031:
1032: /**
1033: * This constructor creates a LuaObject from a table that is indexed by any LuaObject.
1034: * @param parent The Lua Table or Userdata that contains the Field.
1035: * @param name The name (LuaObject) that index the field
1036: * @return LuaObject
1037: * @throws LuaException When the parent object isn't a Table or Userdata
1038: */
1039: public LuaObject getLuaObject(LuaObject parent, LuaObject name)
1040: throws LuaException {
1041: if (parent.getLuaState().getCPtrPeer() != luaState.getPeer()
1042: || parent.getLuaState().getCPtrPeer() != name
1043: .getLuaState().getCPtrPeer())
1044: throw new LuaException(
1045: "Object must have the same LuaState as the parent!");
1046:
1047: return new LuaObject(parent, name);
1048: }
1049:
1050: /**
1051: * Creates a reference to an object in the <code>index</code> position
1052: * of the stack
1053: * @param index position on the stack
1054: * @return LuaObject
1055: */
1056: public LuaObject getLuaObject(int index) {
1057: return new LuaObject(this , index);
1058: }
1059:
1060: /**
1061: * When you call a function in lua, it may return a number, and the
1062: * number will be interpreted as a <code>Double</code>.<br>
1063: * This function converts the number into a type specified by
1064: * <code>retType</code>
1065: * @param db lua number to be converted
1066: * @param retType type to convert to
1067: * @return The converted number
1068: */
1069: public static Number convertLuaNumber(Double db, Class retType) {
1070: // checks if retType is a primitive type
1071: if (retType.isPrimitive()) {
1072: if (retType == Integer.TYPE) {
1073: return new Integer(db.intValue());
1074: } else if (retType == Long.TYPE) {
1075: return new Long(db.longValue());
1076: } else if (retType == Float.TYPE) {
1077: return new Float(db.floatValue());
1078: } else if (retType == Double.TYPE) {
1079: return db;
1080: } else if (retType == Byte.TYPE) {
1081: return new Byte(db.byteValue());
1082: } else if (retType == Short.TYPE) {
1083: return new Short(db.shortValue());
1084: }
1085: } else if (retType.isAssignableFrom(Number.class)) {
1086: // Checks all possibilities of number types
1087: if (retType.isAssignableFrom(Integer.class)) {
1088: return new Integer(db.intValue());
1089: } else if (retType.isAssignableFrom(Long.class)) {
1090: return new Long(db.longValue());
1091: } else if (retType.isAssignableFrom(Float.class)) {
1092: return new Float(db.floatValue());
1093: } else if (retType.isAssignableFrom(Double.class)) {
1094: return db;
1095: } else if (retType.isAssignableFrom(Byte.class)) {
1096: return new Byte(db.byteValue());
1097: } else if (retType.isAssignableFrom(Short.class)) {
1098: return new Short(db.shortValue());
1099: }
1100: }
1101:
1102: // if all checks fail, return null
1103: return null;
1104: }
1105: }
|