forked from Android500/AndroidLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaState.java
More file actions
1184 lines (1007 loc) · 29.7 KB
/
Copy pathLuaState.java
File metadata and controls
1184 lines (1007 loc) · 29.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $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
private synchronized native int _yield(CPtr ptr, int nResults);
private synchronized native int _resume(CPtr ptr, int nargs);
private synchronized native int _status(CPtr ptr);
// Gargabe Collection Functions
final public static Integer LUA_GCSTOP = new Integer(0);
final public static Integer LUA_GCRESTART = new Integer(1);
final public static Integer LUA_GCCOLLECT = new Integer(2);
final public static Integer LUA_GCCOUNT = new Integer(3);
final public static Integer LUA_GCCOUNTB = new Integer(4);
final public static Integer LUA_GCSTEP = new Integer(5);
final public static Integer LUA_GCSETPAUSE = new Integer(6);
final public static Integer LUA_GCSETSTEPMUL = new Integer(7);
private synchronized native int _gc(CPtr ptr, int what, int data);
// Miscellaneous Functions
private synchronized native int _error(CPtr ptr);
private synchronized native int _next(CPtr ptr, int idx);
private synchronized native void _concat(CPtr ptr, int n);
// Some macros
private synchronized native void _pop(CPtr ptr, int n);
private synchronized native void _newTable(CPtr ptr);
private synchronized native int _strlen(CPtr ptr, int idx);
private synchronized native int _isFunction(CPtr ptr, int idx);
private synchronized native int _isTable(CPtr ptr, int idx);
private synchronized native int _isNil(CPtr ptr, int idx);
private synchronized native int _isBoolean(CPtr ptr, int idx);
private synchronized native int _isThread(CPtr ptr, int idx);
private synchronized native int _isNone(CPtr ptr, int idx);
private synchronized native int _isNoneOrNil(CPtr ptr, int idx);
private synchronized native void _setGlobal(CPtr ptr, String name);
private synchronized native void _getGlobal(CPtr ptr, String name);
private synchronized native int _getGcCount(CPtr ptr);
// LuaLibAux
private synchronized native int _LdoFile(CPtr ptr, String fileName);
private synchronized native int _LdoString(CPtr ptr, String string);
//private synchronized native int _doBuffer(CPtr ptr, byte[] buff, long sz, String n);
private synchronized native int _LgetMetaField(CPtr ptr, int obj, String e);
private synchronized native int _LcallMeta(CPtr ptr, int obj, String e);
private synchronized native int _Ltyperror(CPtr ptr, int nArg, String tName);
private synchronized native int _LargError(CPtr ptr, int numArg, String extraMsg);
private synchronized native String _LcheckString(CPtr ptr, int numArg);
private synchronized native String _LoptString(CPtr ptr, int numArg, String def);
private synchronized native double _LcheckNumber(CPtr ptr, int numArg);
private synchronized native double _LoptNumber(CPtr ptr, int numArg, double def);
private synchronized native int _LcheckInteger(CPtr ptr, int numArg);
private synchronized native int _LoptInteger(CPtr ptr, int numArg, int def);
private synchronized native void _LcheckStack(CPtr ptr, int sz, String msg);
private synchronized native void _LcheckType(CPtr ptr, int nArg, int t);
private synchronized native void _LcheckAny(CPtr ptr, int nArg);
private synchronized native int _LnewMetatable(CPtr ptr, String tName);
private synchronized native void _LgetMetatable(CPtr ptr, String tName);
private synchronized native void _Lwhere(CPtr ptr, int lvl);
private synchronized native int _Lref(CPtr ptr, int t);
private synchronized native void _LunRef(CPtr ptr, int t, int ref);
private synchronized native int _LgetN(CPtr ptr, int t);
private synchronized native void _LsetN(CPtr ptr, int t, int n);
private synchronized native int _LloadFile(CPtr ptr, String fileName);
private synchronized native int _LloadBuffer(CPtr ptr, byte[] buff, long sz, String name);
private synchronized native int _LloadString(CPtr ptr, String s);
private synchronized native String _Lgsub(CPtr ptr, String s, String p, String r);
private synchronized native String _LfindTable(CPtr ptr, int idx, String fname, int szhint);
private synchronized native void _openBase(CPtr ptr);
private synchronized native void _openTable(CPtr ptr);
private synchronized native void _openIo(CPtr ptr);
private synchronized native void _openOs(CPtr ptr);
private synchronized native void _openString(CPtr ptr);
private synchronized native void _openMath(CPtr ptr);
private synchronized native void _openDebug(CPtr ptr);
private synchronized native void _openPackage(CPtr ptr);
private synchronized native void _openLibs(CPtr ptr);
// Java Interface -----------------------------------------------------
public LuaState newThread()
{
LuaState l = new LuaState(_newthread(luaState));
LuaStateFactory.insertLuaState(l);
return l;
}
// STACK MANIPULATION
public int getTop()
{
return _getTop(luaState);
}
public void setTop(int idx)
{
_setTop(luaState, idx);
}
public void pushValue(int idx)
{
_pushValue(luaState, idx);
}
public void remove(int idx)
{
_remove(luaState, idx);
}
public void insert(int idx)
{
_insert(luaState, idx);
}
public void replace(int idx)
{
_replace(luaState, idx);
}
public int checkStack(int sz)
{
return _checkStack(luaState, sz);
}
public void xmove(LuaState to, int n)
{
_xmove(luaState, to.luaState, n);
}
// ACCESS FUNCTION
public boolean isNumber(int idx)
{
return (_isNumber(luaState, idx)!=0);
}
public boolean isString(int idx)
{
return (_isString(luaState, idx)!=0);
}
public boolean isFunction(int idx)
{
return (_isFunction(luaState, idx)!=0);
}
public boolean isCFunction(int idx)
{
return (_isCFunction(luaState, idx)!=0);
}
public boolean isUserdata(int idx)
{
return (_isUserdata(luaState, idx)!=0);
}
public boolean isTable(int idx)
{
return (_isTable(luaState, idx)!=0);
}
public boolean isBoolean(int idx)
{
return (_isBoolean(luaState, idx)!=0);
}
public boolean isNil(int idx)
{
return (_isNil(luaState, idx)!=0);
}
public boolean isThread(int idx)
{
return (_isThread(luaState, idx)!=0);
}
public boolean isNone(int idx)
{
return (_isNone(luaState, idx)!=0);
}
public boolean isNoneOrNil(int idx)
{
return (_isNoneOrNil(luaState, idx)!=0);
}
public int type(int idx)
{
return _type(luaState, idx);
}
public String typeName(int tp)
{
return _typeName(luaState, tp);
}
public int equal(int idx1, int idx2)
{
return _equal(luaState, idx1, idx2);
}
public int rawequal(int idx1, int idx2)
{
return _rawequal(luaState, idx1, idx2);
}
public int lessthan(int idx1, int idx2)
{
return _lessthan(luaState, idx1, idx2);
}
public double toNumber(int idx)
{
return _toNumber(luaState, idx);
}
public int toInteger(int idx)
{
return _toInteger(luaState, idx);
}
public boolean toBoolean(int idx)
{
return (_toBoolean(luaState, idx)!=0);
}
public String toString(int idx)
{
return _toString(luaState, idx);
}
public int strLen(int idx)
{
return _strlen(luaState, idx);
}
public int objLen(int idx)
{
return _objlen(luaState, idx);
}
public LuaState toThread(int idx)
{
return new LuaState(_toThread(luaState, idx));
}
//PUSH FUNCTIONS
public void pushNil()
{
_pushNil(luaState);
}
public void pushNumber(double db)
{
_pushNumber(luaState, db);
}
public void pushInteger(int integer)
{
_pushInteger(luaState, integer);
}
public void pushString(String str)
{
if (str == null)
_pushNil(luaState);
else
_pushString(luaState, str);
}
public void pushString(byte[] bytes)
{
if (bytes == null)
_pushNil(luaState);
else
_pushString(luaState, bytes, bytes.length);
}
public void pushBoolean(boolean bool)
{
_pushBoolean(luaState, bool ? 1 : 0);
}
// GET FUNCTIONS
public void getTable(int idx)
{
_getTable(luaState, idx);
}
public void getField(int idx, String k)
{
_getField(luaState, idx, k);
}
public void rawGet(int idx)
{
_rawGet(luaState, idx);
}
public void rawGetI(int idx, int n)
{
_rawGetI(luaState, idx, n);
}
public void createTable(int narr, int nrec)
{
_createTable(luaState, narr, nrec);
}
public void newTable()
{
_newTable(luaState);
}
// if returns 0, there is no metatable
public int getMetaTable(int idx)
{
return _getMetaTable(luaState, idx);
}
public void getFEnv(int idx)
{
_getFEnv(luaState, idx);
}
// SET FUNCTIONS
public void setTable(int idx)
{
_setTable(luaState, idx);
}
public void setField(int idx, String k)
{
_setField(luaState, idx, k);
}
public void rawSet(int idx)
{
_rawSet(luaState, idx);
}
public void rawSetI(int idx, int n)
{
_rawSetI(luaState, idx, n);
}
// if returns 0, cannot set the metatable to the given object
public int setMetaTable(int idx)
{
return _setMetaTable(luaState, idx);
}
// if object is not a function returns 0
public int setFEnv(int idx)
{
return _setFEnv(luaState, idx);
}
public void call(int nArgs, int nResults)
{
_call(luaState, nArgs, nResults);
}
// returns 0 if ok of one of the error codes defined
public int pcall(int nArgs, int nResults, int errFunc)
{
return _pcall(luaState, nArgs, nResults, errFunc);
}
public int yield(int nResults)
{
return _yield(luaState, nResults);
}
public int resume(int nArgs)
{
return _resume(luaState, nArgs);
}
public int status()
{
return _status(luaState);
}
public int gc(int what, int data)
{
return _gc(luaState, what, data);
}
public int getGcCount()
{
return _getGcCount(luaState);
}
public int next(int idx)
{
return _next(luaState, idx);
}
public int error()
{
return _error(luaState);
}
public void concat(int n)
{
_concat(luaState, n);
}
// FUNCTION FROM lauxlib
// returns 0 if ok
public int LdoFile(String fileName)
{
return _LdoFile(luaState, fileName);
}
// returns 0 if ok
public int LdoString(String str)
{
return _LdoString(luaState, str);
}
public int LgetMetaField(int obj, String e)
{
return _LgetMetaField(luaState, obj, e);
}
public int LcallMeta(int obj, String e)
{
return _LcallMeta(luaState, obj, e);
}
public int Ltyperror(int nArg, String tName)
{
return _Ltyperror(luaState, nArg, tName);
}
public int LargError(int numArg, String extraMsg)
{
return _LargError(luaState, numArg, extraMsg);
}
public String LcheckString(int numArg)
{
return _LcheckString(luaState, numArg);
}
public String LoptString(int numArg, String def)
{
return _LoptString(luaState, numArg, def);
}
public double LcheckNumber(int numArg)
{
return _LcheckNumber(luaState, numArg);
}
public double LoptNumber(int numArg, double def)
{
return _LoptNumber(luaState, numArg, def);
}
public int LcheckInteger(int numArg)
{
return _LcheckInteger(luaState, numArg);
}
public int LoptInteger(int numArg, int def)
{
return _LoptInteger(luaState, numArg, def);
}
public void LcheckStack(int sz, String msg)
{
_LcheckStack(luaState, sz, msg);
}
public void LcheckType(int nArg, int t)
{
_LcheckType(luaState, nArg, t);
}
public void LcheckAny(int nArg)
{
_LcheckAny(luaState, nArg);
}
public int LnewMetatable(String tName)
{
return _LnewMetatable(luaState, tName);
}
public void LgetMetatable(String tName)
{
_LgetMetatable(luaState, tName);
}
public void Lwhere(int lvl)
{
_Lwhere(luaState, lvl);
}
public int Lref(int t)
{
return _Lref(luaState, t);
}
public void LunRef(int t, int ref)
{
_LunRef(luaState, t, ref);
}
public int LgetN(int t)
{
return _LgetN(luaState, t);
}
public void LsetN(int t, int n)
{
_LsetN(luaState, t, n);
}
public int LloadFile(String fileName)
{
return _LloadFile(luaState, fileName);
}
public int LloadString(String s)
{
return _LloadString(luaState, s);
}
public int LloadBuffer(byte[] buff, String name)
{
return _LloadBuffer(luaState, buff, buff.length, name);
}
public String Lgsub(String s, String p, String r)
{
return _Lgsub(luaState, s, p, r);
}
public String LfindTable(int idx, String fname, int szhint)
{
return _LfindTable(luaState, idx, fname, szhint);
}
//IMPLEMENTED C MACROS
public void pop(int n)
{
//setTop(- (n) - 1);
_pop(luaState, n);
}
public synchronized void getGlobal(String global)
{
// pushString(global);
// getTable(LUA_GLOBALSINDEX.intValue());
_getGlobal(luaState, global);
}
public synchronized void setGlobal(String name)
{
//pushString(name);
//insert(-2);
//setTable(LUA_GLOBALSINDEX.intValue());
_setGlobal(luaState, name);
}
// Functions to open lua libraries
public void openBase()
{
_openBase(luaState);
}
public void openTable()
{
_openTable(luaState);
}
public void openIo()
{
_openIo(luaState);
}
public void openOs()
{
_openOs(luaState);
}
public void openString()
{
_openString(luaState);
}
public void openMath()
{
_openMath(luaState);
}
public void openDebug()
{
_openDebug(luaState);
}
public void openPackage()
{
_openPackage(luaState);
}
public void openLibs()
{
_openLibs(luaState);
}
/********************** Luajava API Library **********************/
/**
* Initializes lua State to be used by luajava
* @param cptr
* @param stateId
*/
private synchronized native void luajava_open(CPtr cptr, int stateId);
/**
* Gets a Object from a userdata
* @param L
* @param idx index of the lua stack
* @return Object
*/
private synchronized native Object _getObjectFromUserdata(CPtr L, int idx) throws LuaException;
/**
* Returns whether a userdata contains a Java Object
* @param L
* @param idx index of the lua stack
* @return boolean
*/
private synchronized native boolean _isObject(CPtr L, int idx);
/**
* Pushes a Java Object into the state stack
* @param L
* @param obj
*/
private synchronized native void _pushJavaObject(CPtr L, Object obj);
/**
* Pushes a JavaFunction into the state stack
* @param L
* @param func
*/
private synchronized native void _pushJavaFunction(CPtr L, JavaFunction func) throws LuaException;
/**
* Returns whether a userdata contains a Java Function
* @param L
* @param idx index of the lua stack
* @return boolean
*/
private synchronized native boolean _isJavaFunction(CPtr L, int idx);
/**
* Gets a Object from Lua
* @param idx index of the lua stack
* @return Object
* @throws LuaException if the lua object does not represent a java object.
*/
public Object getObjectFromUserdata(int idx) throws LuaException
{
return _getObjectFromUserdata(luaState, idx);
}
/**
* Tells whether a lua index contains a java Object
* @param idx index of the lua stack
* @return boolean
*/
public boolean isObject(int idx)
{
return _isObject(luaState, idx);
}
/**
* Pushes a Java Object into the lua stack.<br>
* This function does not check if the object is from a class that could
* be represented by a lua type. Eg: java.lang.String could be a lua string.
* @param obj Object to be pushed into lua
*/
public void pushJavaObject(Object obj)
{
_pushJavaObject(luaState, obj);
}
/**
* Pushes a JavaFunction into the state stack
* @param func
*/
public void pushJavaFunction(JavaFunction func) throws LuaException
{
_pushJavaFunction(luaState, func);
}
/**
* Returns whether a userdata contains a Java Function
* @param idx index of the lua stack
* @return boolean
*/
public boolean isJavaFunction(int idx)
{
return _isJavaFunction(luaState, idx);
}
/**
* Pushes into the stack any object value.<br>
* This function checks if the object could be pushed as a lua type, if not
* pushes the java object.
* @param obj
*/
public void pushObjectValue(Object obj) throws LuaException
{
if (obj == null)
{
pushNil();
}
else if (obj instanceof Boolean)
{
Boolean bool = (Boolean) obj;
pushBoolean(bool.booleanValue());
}
else if (obj instanceof Number)
{
pushNumber(((Number) obj).doubleValue());
}
else if (obj instanceof String)
{
pushString((String) obj);
}
else if (obj instanceof JavaFunction)
{
JavaFunction func = (JavaFunction) obj;
pushJavaFunction(func);
}
else if (obj instanceof LuaObject)
{
LuaObject ref = (LuaObject) obj;
ref.push();
}
else if (obj instanceof byte[])
{
pushString((byte[]) obj);
}
else
{
pushJavaObject(obj);
}
}
/**
* Function that returns a Java Object equivalent to the one in the given
* position of the Lua Stack.
* @param idx Index in the Lua Stack
* @return Java object equivalent to the Lua one
*/
public synchronized Object toJavaObject( int idx ) throws LuaException
{
Object obj = null;
if (isBoolean(idx))
{
obj = new Boolean(toBoolean(idx));
}
else if (type(idx) == LuaState.LUA_TSTRING.intValue())
{
obj = toString(idx);
}
else if (isFunction(idx))
{
obj = getLuaObject(idx);
}