summaryrefslogtreecommitdiff
path: root/Runtime/Lua/LuaBind/LuaBindState.cpp
blob: 384cba2831f00007caf7f9f5576cbba048975f0b (plain)
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
#include "LuaBindEnum.h"
#include "LuaBindState.h"
#include "LuaBindVM.h"
#include "LuaBindClass.hpp"
#include "LuaBindInternal.h"

#include <string.h>
#include <string>

namespace LuaBind
{
    OnRegisterClassHandler onRegisterNativeClass = NULL;
	ErrorHandler onErrorOccured = NULL;

    std::string g_NameSpace = "";

	State::State(lua_State* state)
		: mState(state) 
	{
		assert(state);
	}

	State::State(const State& state)
		: mState(state.mState)
	{
		assert(state.mState);
	}

	State::~State()
	{
	}

	void State::OpenLibs()
	{
		luaL_openlibs(mState);
	}

	global_State* State::GetGlobalState()
	{
		return G(mState);
	}

	VM* State::GetVM()
	{
		return VM::TryGetVM(G(mState));
	}

	void State::PushGlobalNamespace()
	{
        g_NameSpace.clear();

#if false
		int top = GetTop();

		lua_newtable(mState); // pseudo namespace table 
		int pnt = GetTop();

		lua_newtable(mState); // metatable 
		int mt = GetTop();

		// __index = _G
		// __newindex = _G
		lua_pushvalue(mState, LUA_GLOBALSINDEX);
		lua_pushvalue(mState, LUA_GLOBALSINDEX);
		lua_setfield(mState, mt, "__index");
		lua_setfield(mState, mt, "__newindex");

		lua_setmetatable(mState, pnt);

		// stack: 
		// -1 pseudo global namespace
#else 
		lua_pushvalue(mState, LUA_GLOBALSINDEX);
#endif
	}

	void State::PushNamespace(cc8* name)
	{
		assert(IsNamespace(-1));

        g_NameSpace += (g_NameSpace.length() > 0 ? "." : "") + (std::string)name;

		int top = GetTop();

		lua_getfield(mState, -1, name);
		if (lua_isnil(mState, -1))
		{
			lua_pop(mState, 1);

			lua_newtable(mState);
			lua_pushvalue(mState, -1);
			lua_setfield(mState, top, name);
		}

		// stack:
		// -1 namespace
	}

	void State::PopNamespace()
	{
		assert(lua_istable(mState, -1));
		lua_pop(mState, 1);
	}

	bool State::IsNamespace(int idx)
	{
		return lua_istable(mState, idx);
	}

	void State::DoString(const std::string& code)
	{
		luaL_dostring(mState, code.c_str());
	}

	void State::DoFile(const std::string & path, ErrorHandler handler)
	{
		//luaL_dofile(mState, path.c_str());
		LoadFile(path);
		if (handler == NULL)
		{
			handler = onErrorOccured;
		}
		Call(0, 0, handler);
	}

	void State::LoadFile(const std::string& file)
	{
		luaL_loadfile(mState, file.c_str());
	}

	int State::AbsIndex(int idx)
	{
/*
#define abs_index(mState, i)		((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
					lua_gettop(mState) + (i) + 1)
*/
		if (idx < 0) {
			//return lua_gettop(mState) + idx + 1;
			return 	((idx) > 0 || (idx) <= LUA_REGISTRYINDEX ? (idx) : \
				lua_gettop(mState) + (idx)+1);
		}
		return idx;
	}

	static int TraceBack(lua_State* L)
	{
		int msgIdx = lua_gettop(L);
		if (!lua_isstring(L, -1))
			return 1;
		lua_getfield(L, LUA_GLOBALSINDEX, "debug");
		if (!lua_istable(L, -1))
		{
			lua_pop(L, 1);
			return 1;
		}
		lua_getfield(L, -1, "traceback");
		if (!lua_isfunction(L, -1))
		{
			lua_pop(L, 2);
			return 1;
		}
		lua_pushvalue(L, msgIdx);
		lua_pushinteger(L, 2);
		lua_call(L, 2, 1);
		return 1;
	}

	void State::Call(int nArgs, int nResults, ErrorHandler handler)
	{
		if (handler == NULL)
		{
			handler = onErrorOccured;
		}
		int oldTop = GetTop() - nArgs - 1;
		int func = 0;
		if (handler != NULL)
		{
			func = oldTop + 1;
			lua_pushcfunction(mState, TraceBack);
			lua_insert(mState, oldTop + 1);
		}
		if (lua_pcall(mState, nArgs, nResults, func) == 0) 
		{
            if(func != 0)
			    lua_remove(mState, func);
		}
		else
		{
			if (handler != NULL)
			{
				lua_remove(mState, func);
				cc8* err = CheckValue<cc8*>(-1);
				handler(err);
				lua_pop(mState, 1);
			}
		}
	}

	void State::PushNil() 
	{
		lua_pushnil(mState);
	}

	void State::Push(bool value) 
	{
		lua_pushboolean(mState, value ? 1 : 0);
	}

	void State::Push(cc8* value) 
	{
		lua_pushstring(mState, value);
	}

	void State::Push(double value) 
	{
		lua_pushnumber(mState, value);
	}

	void State::Push(float value) 
	{
		lua_pushnumber(mState, value);
	}

	void State::Push(int value) 
	{
		lua_pushnumber(mState, value);
	}

	void State::Push(u16 value) 
	{
		lua_pushnumber(mState, value);
	}

	void State::Push(u32 value) 
	{
		lua_pushnumber(mState, value);
	}

	void State::Push(u64 value) 
	{
		lua_pushnumber(mState, (double)value);
	}

	void State::Push(s64 value)
	{
		lua_pushinteger(mState, value);
	}

	void State::Push(uintptr value) 
	{
		lua_pushlightuserdata(mState, (void*)value);
	}

	void State::Push(lua_CFunction value) 
	{
		lua_pushcfunction(mState, value);
	}

	void State::Push(void* data, size_t size)
	{
		lua_pushlstring(mState, (cc8*)data, size);
	}

	void State::Push(const void* value)
	{
		lua_pushlightuserdata(mState, (void*)value);
	}

	void State::Push(std::string value)
	{
		Push(value.c_str());
	}

	void State::PushValues(int idx, int n)
	{
		idx = AbsIndex(idx);
		for (int i = idx; i < idx + n; ++i)
			lua_pushvalue(mState, i);
	}

	void State::Pop(int n /* = 1 */)
	{
		lua_pop(mState, n);
	}

	bool State::IsNil(int idx) 
	{
		return lua_isnil(mState, idx);
	}

	bool State::IsNilOrNone(int idx) 
	{
		int t = lua_type(mState, idx);
		return ((t == LUA_TNONE) || (t == LUA_TNIL));
	}

	bool State::IsTable(int idx)
	{
		int check = lua_type(mState, idx);
		return check == LUA_TTABLE;
	}

	bool State::IsTableOrUserdata(int idx) 
	{
		int check = lua_type(mState, idx);
		return ((check == LUA_TTABLE) || (check == LUA_TUSERDATA));
	}

	bool State::IsTrueOrNotNil(int idx) 
	{
		if (lua_isboolean(mState, idx)) {
			return lua_toboolean(mState, idx) ? true : false;
		}
		return !lua_isnil(mState, idx);
	}

	bool State::IsType(int idx, int type) 
	{
		return (lua_type(mState, idx) == type);
	}

	bool State::IsType(int idx, cc8* name, int type) 
	{
		return this->HasField(idx, name, type);
	}

	bool State::IsValid() 
	{
		return (mState != 0);
	}

	void State::Settop(int idx)
	{
		lua_settop(mState, idx);
	}

	int State::GetTop()
	{
		return lua_gettop(mState);
	}

    void State::SetTop(int top)
    {
        lua_settop(mState, top);
    }

	bool State::HasField(int idx, cc8* name) {

		lua_getfield(mState, idx, name);
		bool hasField = (lua_isnil(mState, -1) == false);
		lua_pop(mState, 1);

		return hasField;
	}

	bool State::HasField(int idx, int key) {

		this->GetField(idx, key);
		bool hasField = (lua_isnil(mState, -1) == false);
		lua_pop(mState, 1);

		return hasField;
	}

	bool State::HasField(int idx, cc8* name, int type) {

		lua_getfield(mState, idx, name);
		bool hasField = (lua_type(mState, -1) == type);
		lua_pop(mState, 1);

		return hasField;
	}

	bool State::HasField(int idx, int key, int type) {

		this->GetField(idx, key);
		bool hasField = (lua_type(mState, -1) == type);
		lua_pop(mState, 1);

		return hasField;
	}

	bool State::HasKeys(int idx) {

		idx = this->AbsIndex(idx);

		lua_pushnil(mState);  /* first key */
		if (lua_next(mState, idx) != 0) {
			lua_pop(mState, 2);
			return true;
		}
		return false;
	}

	void State::GetField(int idx, cc8* name) 
	{
		lua_getfield(mState, idx, name);
	}

	void State::GetField(int idx, int key) 
	{
		idx = this->AbsIndex(idx);

		lua_pushinteger(mState, key);
		lua_gettable(mState, idx);
	}

	std::string State::GetField(int idx, cc8* key, cc8* default_value) 
	{
		std::string str;
		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
			str = lua_tostring(mState, -1);
			lua_pop(mState, 1);
		}
		else {
			str = default_value;
		}
		return str;
	}

	std::string State::GetField(int idx, int key, cc8* default_value)
	{
		std::string str;
		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
			str = lua_tostring(mState, -1);
			lua_pop(mState, 1);
		}
		else {
			str = default_value;
		}
		return str;
	}

	std::string State::GetField(int idx, cc8* key, const std::string& value)
	{
		std::string str;
		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
			str = lua_tostring(mState, -1);
			lua_pop(mState, 1);
		}
		else {
			str = value;
		}
		return str;
	}

	std::string State::GetField(int idx, int key, const std::string& value) 
	{
		std::string str;
		if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
			str = lua_tostring(mState, -1);
			lua_pop(mState, 1);
		}
		else {
			str = value;
		}
		return str;
	}

	bool State::GetFieldWithType(int idx, cc8* name, int type) 
	{
		lua_getfield(mState, idx, name);
		if (lua_type(mState, -1) != type) {
			lua_pop(mState, 1);
			return false;
		}
		return true;
	}

	bool State::GetFieldWithType(int idx, int key, int type) 
	{
		this->GetField(idx, key);
		if (lua_type(mState, -1) != type) {
			lua_pop(mState, 1);
			return false;
		}
		return true;
	}

	void State::SetField(int idx, cc8* key)
	{
		if (IsTableOrUserdata(idx))
		{
			idx = AbsIndex(idx);
			lua_setfield(mState, idx, key);
		}
	}

	cc8* State::GetLuaTypeName(int type) 
	{
		switch (type) {
			case LUA_TNONE:				return "none";
			case LUA_TNIL:				return "nil";
			case LUA_TBOOLEAN:			return "boolean";
			case LUA_TLIGHTUSERDATA:	return "lightuserdata";
			case LUA_TNUMBER:			return "number";
			case LUA_TSTRING:			return "string";
			case LUA_TTABLE:			return "table";
			case LUA_TFUNCTION:			return "function";
			case LUA_TUSERDATA:			return "userdata";
			case LUA_TTHREAD:			return "thread";
		}
		return "unknown";
	}

	
	bool State::GetSubfieldWithType(int idx, cc8* format, int type, ...) 
	{
		va_list args;
		va_start(args, type);

		idx = this->AbsIndex(idx);
		lua_pushvalue(this->mState, idx);

		for (cc8* c = format; *c; ++c) {
			switch (*c) {
				// number
			case 'N':
				lua_pushnumber(this->mState, va_arg(args, int));
				lua_gettable(this->mState, -1);
				break;

				// string
			case 'S':
				lua_getfield(this->mState, -1, va_arg(args, char*));
				break;

			default:
				lua_pushnil(this->mState);
			}

			if (lua_isnil(this->mState, -1)) break;
			lua_replace(this->mState, -2);
		}
		va_end(args);
		if (lua_type(this->mState, -1) != type) {
			lua_pop(this->mState, 1);
			return false;
		}
		return true;
	}

    bool State::CheckParams(int idx, cc8* format, int len)
    {
        idx = AbsIndex(idx);

        for (int i = 0; i < len; ++i) {
            int pos = idx + i;
            int type = LUA_TNIL;
            bool expected = true;
            if (pos <= GetTop()) {
                type = lua_type(mState, pos);
            }

            switch (format[i]) {

                    // boolean
                case 'B':
                    if (type != LUA_TBOOLEAN) expected = false;
                    break;

                    // coroutine
                case 'C':
                    if (type != LUA_TTHREAD) expected = false;
                    break;

                    // function
                case 'F':
                    if (type != LUA_TFUNCTION) expected = false;
                    break;

                    // light userdata
                case 'L':
                    if (type != LUA_TLIGHTUSERDATA) expected = false;
                    break;

                    // number
                case 'N':
                    if (type != LUA_TNUMBER) expected = false;
                    break;

                    // string
                case 'S':
                    if (type != LUA_TSTRING) expected = false;
                    break;

                    // table
                case 'T':
                    if (type != LUA_TTABLE) expected = false;
                    break;

                    // userdata
                case 'U':
                    if (type != LUA_TUSERDATA) expected = false;
                    break;

                    // any type
                case '*':
                    break;

                    // not nil
                case '+':
                    if (type == LUA_TNIL) expected = false;
                    break;

                    // nil
                case '!':
                    if (type != LUA_TNIL) expected = false;
                    break;

            }

            if (!expected) {
                return false;
            }
        }

        return true;
    }

	bool State::CheckParams(int idx, cc8* fmt)
	{
        cc8* blank = fmt + strlen(fmt);
        while (*fmt) {
            int len = blank - fmt;
            const char* end = strchr(fmt, '|');
            if (end != NULL){
                len = end - fmt;
            }
            if (CheckParams(idx, fmt, len)){
                return true;
            }
            fmt = fmt + len + (end != NULL ? 1 : 0);
        }
        return false;
    }
	
	template <>
	bool State::GetValue < bool >(int idx, const bool value) {

		if (this->IsType(idx, LUA_TBOOLEAN)) {
			return (lua_toboolean(this->mState, idx) != 0);
		}
		return value;
	}

	static std::string s_str;
	template <>
	cc8* State::GetValue < cc8* >(int idx, const cc8* value) {

		if (this->IsType(idx, LUA_TSTRING)) {
			return lua_tostring(this->mState, idx);
		}

		if (this->IsType(idx, LUA_TNUMBER)) {
			if (luax_isinteger(mState, -1)) {
				int num = lua_tointeger(this->mState, idx);
				s_str = std::to_string(num);
				cc8* str = s_str.c_str();
				return str;
			}
			else {
				float num = lua_tonumber(this->mState, idx);
				s_str = std::to_string(num);
				cc8* str = s_str.c_str();
				return str;
			}
		}

		if (this->IsType(idx, LUA_TBOOLEAN)) {
			bool b = lua_toboolean(this->mState, idx);
			return b ? "true" : "false";
		}

		if (this->IsType(idx, LUA_TNIL)) {
			return "NIL";
		}

		return value;
	}

	template <>
	std::string State::GetValue<std::string>(int idx, const std::string value) 
	{
		std::string str;
		if (lua_type(this->mState, idx) == LUA_TSTRING) {
			str = lua_tostring(this->mState, idx);
		}
		else {
			str = value;
		}
		return str;
	}
	
	template <>
	double State::GetValue < double >(int idx, const double value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return lua_tonumber(this->mState, idx);
		}
		return value;
	}
	
	template <>
	float State::GetValue < float >(int idx, const float value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (float)lua_tonumber(this->mState, idx);
		}
		return value;
	}
	
	template <>
	s8 State::GetValue < s8 >(int idx, const s8 value)
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (s8)lua_tonumber(this->mState, idx);
		}
		return value;
	}

	
	template <>
	s16 State::GetValue < s16 >(int idx, const s16 value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (s16)lua_tonumber(this->mState, idx);
		}
		return value;
	}

	
	template <>
	s32 State::GetValue < s32 >(int idx, const s32 value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (s32)lua_tonumber(this->mState, idx);
		}
		return value;
	}
	
	template <>
	s64 State::GetValue < s64 >(int idx, const s64 value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (s64)lua_tonumber(this->mState, idx);
		}
		return value;
	}

	template <>
	u8 State::GetValue < u8 >(int idx, const u8 value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (u8)lua_tonumber(this->mState, idx);
		}
		return value;
	}

	template <>
	u16 State::GetValue < u16 >(int idx, const u16 value)
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (u16)lua_tonumber(this->mState, idx);
		}
		return value;
	}
	
	template <>
	u32 State::GetValue < u32 >(int idx, const u32 value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (u32)lua_tonumber(this->mState, idx);
		}
		return value;
	}
	
	template <>
	u64 State::GetValue < u64 >(int idx, const u64 value) 
	{
		if (this->IsType(idx, LUA_TNUMBER)) {
			return (u64)lua_tonumber(this->mState, idx);
		}
		return value;
	}
	
	template <>
	const void* State::GetValue < const void* >(int idx, const void* value)
	{
		if (this->IsType(idx, LUA_TLIGHTUSERDATA)) {
			return (void*)lua_touserdata(this->mState, idx);
		}
		return value;
	}

	void State::PushPtrUserdata(void* ptr) 
	{
		void** handle = (void**)lua_newuserdata(this->mState, sizeof(void*));
		assert(handle);
		(*handle) = ptr;
	}

	void State::RegisterEnum(cc8* name, Enum* en)
	{
		assert(name);
		assert(en);

		// short name
		lua_State* L = mState;

		int top = GetTop();

		lua_newtable(L); // enum table 

		int et = GetTop();

		lua_newtable(L); // matatable

		// 所有枚举都存在metatable下,修改时触发__newindex报错
		for (; en->name; ++en)
		{
			lua_pushinteger(L, en->value);
			lua_setfield(L, -2, en->name);
		}

		// __index 
		//lua_pushvalue(L, -1); // metatable
		//lua_pushcclosure(L, _rmt__index, 1);
		lua_pushvalue(L, -1);
		lua_setfield(L, -2, "__index");

		// __newinedx
		lua_pushstring(L, name); // enum name
		lua_pushcclosure(L, _rmt__newindex, 1);
		lua_setfield(L, -2, "__newindex");

		lua_setmetatable(L, et);

		lua_setfield(L, top, name);
	}


	void State::RegisterMethods(const luaL_Reg *l)
	{
		assert(lua_istable(mState, -1));
		// luaL_register第二个参数为空,则向-1位置注册luaL_Reg中这些函数
		luaL_register(mState, 0, l);
	}

	void State::RegisterMethod(cc8* fname, lua_CFunction func)
	{
		assert(lua_istable(mState, -1));
		lua_pushcfunction(mState, func);
		lua_setfield(mState, -2, fname);
	}

	void State::RegisterPreloader(cc8* libname, lua_CFunction preloader)
	{
		lua_getglobal(mState, "package");
		lua_getfield(mState, -1, "preload");
		lua_pushcfunction(mState, preloader);
		lua_setfield(mState, -2, libname);
		lua_pop(mState, 2);
	}

	void State::RegisterLib(cc8* libname, const luaL_Reg* l)
	{
		luaL_register(mState, libname, l);
	}

	int State::ErrorType(int idx, cc8* hint)
	{
		return luaL_typerror(mState, idx, hint);
	}

	template <> 
	bool State::CheckValue < bool >(int idx)
	{
		bool b = false;
		if (lua_type(mState, idx) == LUA_TBOOLEAN)
		{
			b = lua_toboolean(mState, idx);
		}
		else
		{
			luaL_typerror(mState, idx, lua_typename(mState, LUA_TBOOLEAN));
		}
		return b;
	}

	template <> 
	cc8* State::CheckValue < cc8* >(int idx)
	{
		return luaL_checkstring(mState, idx);
	}

	template <> 
	double State::CheckValue < double >(int idx)
	{
		return luaL_checknumber(mState, idx);
	}

	template <> 
	float State::CheckValue < float >(int idx)
	{
		return luaL_checknumber(mState, idx);
	}

	template <> 
	s8 State::CheckValue < s8 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	s16 State::CheckValue < s16 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	s32 State::CheckValue < s32 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	s64 State::CheckValue < s64 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	u8 State::CheckValue < u8 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	u16 State::CheckValue < u16 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	u32 State::CheckValue < u32 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	u64	 State::CheckValue < u64 >(int idx)
	{
		return luaL_checkinteger(mState, idx);
	}

	template <> 
	std::string State::CheckValue < std::string >(int idx)
	{
		return luaL_checkstring(mState, idx);
	}

	// check light userdata
	template <>
	const void* State::CheckValue < const void* >(int idx)
	{
		if (IsType(idx, LUA_TLIGHTUSERDATA))
		{
			return GetValue<const void*>(idx, nullptr);
		}
		else
		{
			luaL_typerror(mState, idx, "light userdata");
			return nullptr;
		}
	}

}