-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLuaObject.cpp
More file actions
2398 lines (1950 loc) · 49.3 KB
/
Copy pathLuaObject.cpp
File metadata and controls
2398 lines (1950 loc) · 49.3 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
///////////////////////////////////////////////////////////////////////////////
// This source file is part of the LuaPlus source distribution and is Copyright
// 2001-2010 by Joshua C. Jensen (jjensen@workspacewhiz.com).
//
// The latest version may be obtained from http://luaplus.org/.
//
// The code presented in this file may be used in any environment it is
// acceptable to use Lua.
///////////////////////////////////////////////////////////////////////////////
#ifndef BUILDING_LUAPLUS
#define BUILDING_LUAPLUS
#endif
#include "LuaLink.h"
LUA_EXTERN_C_BEGIN
#include "src/lfunc.h"
#include "src/lobject.h"
#include "src/lstate.h"
#include "src/lstring.h"
#include "src/ltable.h"
#include "src/lvm.h"
LUA_EXTERN_C_END
#include "LuaPlus.h"
#include <string.h>
#if defined(_MSC_VER)
#include <malloc.h>
#endif
#if LUAPLUS_EXTENSIONS
NAMESPACE_LUA_BEGIN
LUA_EXTERN_C TValue *index2adr (lua_State *L, int idx);
LUA_EXTERN_C Table *getcurrenv (lua_State *L);
NAMESPACE_LUA_END
USING_NAMESPACE_LUA
// From the Loki reference implementation
template<int> struct CompileTimeError;
template<> struct CompileTimeError<true> {};
////////////////////////////////////////////////////////////////////////////////
// macro STATIC_CHECK
// Invocation: STATIC_CHECK(expr, id)
// where:
// expr is a compile-time integral or pointer expression
// id is a C++ identifier that does not need to be defined
// If expr is zero, id will appear in a compile-time error message.
////////////////////////////////////////////////////////////////////////////////
#define STATIC_CHECK(expr, msg) \
{ CompileTimeError<((expr) != 0)> ERROR_##msg; (void)ERROR_##msg; }
///////////////////////////////////////////////////////////////////////////////
// namespace LuaPlus
///////////////////////////////////////////////////////////////////////////////
namespace LuaPlus
{
#undef api_check
#define api_check(L, o) luaplus_assert(o)
#if !LUA_REFCOUNT
#define setnilvalue2n(L,obj) setnilvalue((obj))
#define setbvalue2n(obj,x) setbvalue((obj),(x))
#define setclvalue2n(L,obj,x) setclvalue((L),(obj),(x))
#define sethvalue2n(L,obj,x) sethvalue((L),(obj),(x))
#define setnvalue2n(obj,x) setnvalue((obj),(x))
#define setpvalue2n(obj,x) setpvalue((obj),(x))
#define setuvalue2n(L,obj,x) setuvalue((L),(obj),(x))
#endif /* !LUA_REFCOUNT */
LuaObject::LuaObject() :
m_next(NULL),
m_prev(NULL),
L(NULL)
{
setnilvalue2n(NULL, &m_object);
}
LuaObject::LuaObject(lua_State* L) throw()
{
AddToUsedList(L);
setnilvalue2n(L, &m_object);
}
LuaObject::LuaObject(LuaState* state) throw()
{
lua_State* L = LuaState_to_lua_State(state);
AddToUsedList(L);
setnilvalue2n(L, &m_object);
}
LuaObject::LuaObject(lua_State* L, int stackIndex) throw()
{
setnilvalue2n(L, &m_object);
AddToUsedList(L, *index2adr(L, stackIndex));
}
LuaObject::LuaObject(LuaState* state, int stackIndex) throw()
{
lua_State* L = LuaState_to_lua_State(state);
setnilvalue2n(L, &m_object);
AddToUsedList(L, *index2adr(L, stackIndex));
}
LuaObject::LuaObject(lua_State* L, const TValue* obj)
{
luaplus_assert(obj);
setnilvalue2n(L, &m_object);
AddToUsedList(L, *obj);
}
LuaObject::LuaObject(LuaState* state, const TValue* obj)
{
lua_State* L = LuaState_to_lua_State(state);
luaplus_assert(obj);
setnilvalue2n(L, &m_object);
AddToUsedList(L, *obj);
}
LuaObject::LuaObject(const LuaObject& src) throw()
{
setnilvalue2n(src.L, &m_object);
if (src.L)
AddToUsedList(src.L, src.m_object);
else
{
L = NULL;
m_next = m_prev = NULL;
}
}
LuaObject::LuaObject(const LuaStackObject& src) throw()
{
setnilvalue2n(src.L, &m_object);
if (src.L)
AddToUsedList(src.L, *index2adr(src.L, src.m_stackIndex));
else
{
L = NULL;
m_next = m_prev = NULL;
}
}
LuaObject& LuaObject::operator=(const LuaObject& src) throw()
{
RemoveFromUsedList();
if (src.L)
AddToUsedList(src.L, src.m_object);
else
{
L = NULL;
m_next = m_prev = NULL;
}
return *this;
}
LuaObject& LuaObject::operator=(const LuaStackObject& src) throw()
{
RemoveFromUsedList();
if (src.L)
AddToUsedList(src.L, *index2adr(src.L, src.m_stackIndex));
else
{
L = NULL;
m_next = m_prev = NULL;
}
return *this;
}
LuaObject::~LuaObject()
{
RemoveFromUsedList();
}
/**
Resets the LuaObject by removing itself from the used GC list and setting the state to NULL.
**/
void LuaObject::Reset()
{
RemoveFromUsedList();
L = NULL;
m_next = m_prev = NULL;
}
// Mirrors lua_typename().
const char* LuaObject::TypeName() const
{
int t = Type();
return (t == LUA_TNONE) ? "no value" : luaT_typenames[t];
}
// Mirrors lua_type().
int LuaObject::Type() const
{
return ttype(&m_object);
}
// Mirrors lua_isnil().
bool LuaObject::IsNil() const
{
return ttype(&m_object) == LUA_TNIL;
}
// Mirrors lua_istable().
bool LuaObject::IsTable() const
{
return ttype(&m_object) == LUA_TTABLE;
}
// Mirrors lua_isuserdata().
bool LuaObject::IsUserData() const
{
return ttisuserdata(&m_object) || ttislightuserdata(&m_object);
}
// Mirrors lua_iscfunction().
bool LuaObject::IsCFunction() const
{
return iscfunction(&m_object);
}
// Behaves differently than lua_isinteger(). This function only tests for a value that is
// a real integer, not something that can be converted to a integer.
bool LuaObject::IsInteger() const
{
return ttype(&m_object) == LUA_TNUMBER;
}
// Behaves differently than lua_isnumber(). This function only tests for a value that is
// a real number, not something that can be converted to a number.
bool LuaObject::IsNumber() const
{
return ttype(&m_object) == LUA_TNUMBER;
}
// Behaves differently than lua_isstring(). This function only tests for a value that is
// a real string, not something that can be converted to a string.
bool LuaObject::IsString() const
{
return ttype(&m_object) == LUA_TSTRING;
}
#if LUA_WIDESTRING
bool LuaObject::IsWString() const
{
return ttype(&m_object) == LUA_TWSTRING;
}
#endif /* LUA_WIDESTRING */
// Mirrors lua_isinteger().
bool LuaObject::IsConvertibleToInteger() const
{
luaplus_assert(L);
const TValue* o = &m_object;
TValue n;
setnilvalue2n(L, &n);
lua_lock(L);
bool ret = tonumber(o, &n);
lua_unlock(L);
return ret;
}
// Mirrors lua_isnumber().
bool LuaObject::IsConvertibleToNumber() const
{
luaplus_assert(L);
const TValue* o = &m_object;
TValue n;
setnilvalue2n(L, &n);
lua_lock(L);
bool ret = tonumber(o, &n);
lua_unlock(L);
return ret;
}
// Mirrors lua_isstring().
bool LuaObject::IsConvertibleToString() const
{
int t = Type();
return (t == LUA_TSTRING || t == LUA_TNUMBER);
}
// Mirrors lua_iswstring().
#if LUA_WIDESTRING
bool LuaObject::IsConvertibleToWString() const
{
int t = Type();
return (t == LUA_TNUMBER || t == LUA_TWSTRING);
}
#endif /* LUA_WIDESTRING */
// Mirrors lua_isfunction().
bool LuaObject::IsFunction() const
{
return ttype(&m_object) == LUA_TFUNCTION;
}
// Mirrors lua_isnone().
bool LuaObject::IsNone() const
{
return ttype(&m_object) == LUA_TNONE;
}
// Mirrors lua_islightuserdata().
bool LuaObject::IsLightUserData() const
{
return ttype(&m_object) == LUA_TLIGHTUSERDATA;
}
// Mirrors lua_isboolean().
bool LuaObject::IsBoolean() const
{
return ttype(&m_object) == LUA_TBOOLEAN;
}
// Mirrors lua_tointeger()
int LuaObject::ToInteger()
{
const TValue* o = &m_object;
TValue n;
lua_lock(L);
bool ret = tonumber(o, &n);
lua_unlock(L);
if (ret)
return (int)nvalue(&m_object);
else
return 0;
}
// Mirrors lua_tonumber()
lua_Number LuaObject::ToNumber()
{
const TValue* o = &m_object;
TValue n;
lua_lock(L);
bool ret = tonumber(o, &n);
lua_unlock(L);
if (ret)
return nvalue(&m_object);
else
return 0;
}
// Mirrors lua_tostring().
const char* LuaObject::ToString()
{
if (ttisstring(&m_object))
return svalue(&m_object);
else
{
const char *s;
lua_lock(L); /* `luaV_tostring' may create a new string */
s = (luaV_tostring(L, &m_object) ? svalue(&m_object) : NULL);
lua_unlock(L);
return s;
}
}
// Mirrors lua_towstring().
#if LUA_WIDESTRING
const lua_WChar* LuaObject::ToWString()
{
if (ttiswstring(&m_object))
return wsvalue(&m_object);
else
{
const lua_WChar *s;
lua_lock(L); /* `luaV_tostring' may create a new string */
s = (luaV_towstring(L, &m_object) ? wsvalue(&m_object) : NULL);
lua_unlock(L);
return s;
}
}
#endif /* LUA_WIDESTRING */
size_t LuaObject::ToStrLen()
{
#if LUA_WIDESTRING
if (ttisstring(&m_object) || ttiswstring(&m_object))
#else
if (ttisstring(&m_object))
#endif /* LUA_WIDESTRING */
return tsvalue(&m_object)->len;
else
{
size_t l;
lua_lock(L); /* `luaV_tostring' may create a new string */
l = (luaV_tostring(L, &m_object) ? tsvalue(&m_object)->len : 0);
lua_unlock(L);
return l;
}
}
int LuaObject::GetInteger() const
{
luaplus_assert(L && IsInteger());
return (int)(unsigned int)nvalue(&m_object);
}
float LuaObject::GetFloat() const
{
luaplus_assert(L && IsNumber());
return (float)nvalue(&m_object);
}
double LuaObject::GetDouble() const
{
luaplus_assert(L && IsNumber());
return (double)nvalue(&m_object);
}
lua_Number LuaObject::GetNumber() const
{
luaplus_assert(L && IsNumber());
return (lua_Number)nvalue(&m_object);
}
const char* LuaObject::GetString() const
{
luaplus_assert(L && IsString());
return svalue(&m_object);
}
#if LUA_WIDESTRING
const lua_WChar* LuaObject::GetWString()const
{
luaplus_assert(L && IsWString());
return wsvalue(&m_object);
}
#endif /* LUA_WIDESTRING */
size_t LuaObject::StrLen()
{
luaplus_assert(L);
#if LUA_WIDESTRING
if (IsString() || IsWString())
#else
if (IsString())
#endif /* LUA_WIDESTRING */
{
return tsvalue(&m_object)->len;
}
else if (IsUserData())
{
return uvalue(&m_object)->len;
}
else
{
luaplus_assert(0);
return 0;
}
}
NAMESPACE_LUA_PREFIX lua_CFunction LuaObject::GetCFunction() const
{
luaplus_assert(L && IsCFunction());
return (!iscfunction(&m_object)) ? NULL : clvalue(&m_object)->c.f;
}
// Mirrors lua_touserdata().
void* LuaObject::GetUserData()
{
luaplus_assert(L && IsUserData());
StkId o = &m_object;
switch (ttype(o))
{
case LUA_TUSERDATA: return (rawuvalue(o) + 1);
case LUA_TLIGHTUSERDATA: return pvalue(o);
default: return NULL;
}
}
// Mirrors lua_topointer.
const void* LuaObject::GetLuaPointer()
{
luaplus_assert(L);
StkId o = &m_object;
switch (ttype(o))
{
case LUA_TTABLE: return hvalue(o);
case LUA_TFUNCTION: return clvalue(o);
case LUA_TUSERDATA:
case LUA_TLIGHTUSERDATA:
return GetUserData();
default: return NULL;
}
}
// No equivalent.
void* LuaObject::GetLightUserData() const
{
luaplus_assert(L && IsLightUserData());
return pvalue(&m_object);
}
// Mirrors lua_toboolean().
bool LuaObject::GetBoolean() const
{
luaplus_assert(L && IsBoolean() || IsNil());
return !l_isfalse(&m_object);
}
/**
**/
LuaObject LuaObject::Clone() const
{
lua_lock(L);
if (IsTable())
{
LuaObject tableObj(L);
sethvalue(L, &tableObj.m_object, luaH_new(L, hvalue(&m_object)->sizearray, hvalue(&m_object)->lsizenode));
tableObj.SetMetaTable(GetMetaTable());
for (LuaTableIterator it(*this); it; ++it)
{
if (it.GetValue().IsTable())
{
LuaObject clonedChildTableObj = it.GetValue().Clone();
tableObj.SetObject(it.GetKey(), clonedChildTableObj);
}
else
tableObj.SetObject(it.GetKey(), it.GetValue());
}
return tableObj;
}
lua_unlock(L);
return LuaObject(L, &m_object);
}
void LuaObject::DeepClone(LuaObject& outObj) const
{
if (IsTable())
{
outObj.AssignNewTable(outObj.GetState());
for (LuaTableIterator it(*this); it; ++it)
{
LuaObject keyObj;
switch (it.GetKey().Type())
{
case LUA_TBOOLEAN: keyObj.AssignBoolean(outObj.GetState(), it.GetKey().GetBoolean()); break;
case LUA_TNUMBER: keyObj.AssignNumber(outObj.GetState(), it.GetKey().GetNumber()); break;
case LUA_TSTRING: keyObj.AssignString(outObj.GetState(), it.GetKey().GetString()); break;
#if LUA_WIDESTRING
case LUA_TWSTRING: keyObj.AssignWString(outObj.GetState(), it.GetKey().GetWString()); break;
#endif /* LUA_WIDESTRING */
}
switch (it.GetValue().Type())
{
case LUA_TBOOLEAN: outObj.SetBoolean(keyObj, it.GetValue().GetBoolean()); break;
case LUA_TNUMBER: outObj.SetNumber(keyObj, it.GetValue().GetNumber()); break;
case LUA_TSTRING: outObj.SetString(keyObj, it.GetValue().GetString()); break;
#if LUA_WIDESTRING
case LUA_TWSTRING: outObj.SetWString(keyObj, it.GetValue().GetWString()); break;
#endif /* LUA_WIDESTRING */
case LUA_TTABLE:
{
LuaObject newTableObj(outObj.GetState());
it.GetValue().DeepClone(newTableObj);
outObj.SetObject(keyObj, newTableObj);
break;
}
}
}
}
else
{
switch (Type())
{
case LUA_TBOOLEAN: outObj.AssignBoolean(outObj.GetState(), this->GetBoolean()); break;
case LUA_TNUMBER: outObj.AssignNumber(outObj.GetState(), this->GetNumber()); break;
case LUA_TSTRING: outObj.AssignString(outObj.GetState(), this->GetString()); break;
#if LUA_WIDESTRING
case LUA_TWSTRING: outObj.AssignWString(outObj.GetState(), this->GetWString()); break;
#endif /* LUA_WIDESTRING */
case LUA_TTABLE: DeepClone(outObj); break;
}
}
}
static inline int InternalGetTop(lua_State* L)
{
return (int)(L->top - L->base);
}
#undef api_incr_top
#define api_incr_top(L) \
{if (L->top >= L->ci->top) lua_checkstack(L, 1); L->top++;}
static inline void InternalPushTObject(lua_State *L, const void* tobject)
{
TValue* tobj = (TValue*)tobject;
lua_lock(L);
setobj2s(L, L->top, tobj);
api_incr_top(L);
lua_unlock(L);
}
LuaStackObject LuaObject::Push() const
{
luaplus_assert(L);
InternalPushTObject(L, &m_object);
return LuaStackObject(L, InternalGetTop(L));
}
LuaObject LuaObject::GetMetaTable() const
{
luaplus_assert(L);
lua_lock(L);
Table *mt = NULL;
// int res;
switch (ttype(&m_object))
{
case LUA_TTABLE:
mt = hvalue(&m_object)->metatable;
break;
case LUA_TUSERDATA:
mt = uvalue(&m_object)->metatable;
break;
default:
mt = G(L)->mt[ttype(&m_object)];
break;
}
LuaObject ret(L);
if (mt)
{
sethvalue(L, &ret.m_object, mt);
}
lua_unlock(L);
return ret;
}
void LuaObject::SetMetaTable(const LuaObject& valueObj)
{
luaplus_assert(L);
lua_lock(L);
TValue* obj = &m_object;
Table* mt = valueObj.IsTable() ? hvalue(&valueObj.m_object) : NULL;
#if LUA_REFCOUNT
if (mt)
luarc_addreftable(mt);
#endif
switch (ttype(obj))
{
case LUA_TTABLE:
{
#if LUA_REFCOUNT
if (hvalue(obj)->metatable)
luarc_releasetable(L, hvalue(obj)->metatable);
#endif
hvalue(obj)->metatable = mt;
if (mt)
luaC_objbarriert(L, hvalue(obj), mt);
break;
}
case LUA_TUSERDATA:
{
#if LUA_REFCOUNT
if (uvalue(obj)->metatable)
luarc_releasetable(L, uvalue(obj)->metatable);
#endif
uvalue(obj)->metatable = mt;
if (mt)
luaC_objbarrier(L, rawuvalue(obj), mt);
break;
}
default:
{
#if LUA_REFCOUNT
if (G(L)->mt[ttype(obj)])
luarc_releasetable(L, G(L)->mt[ttype(obj)]);
#endif
G(L)->mt[ttype(obj)] = mt;
break;
}
}
//jj luaT_setmetatable(L, &m_object, hvalue(&valueObj.m_object));
lua_unlock(L);
}
#if defined(LUA_COMPAT_GETN)
int LuaObject::GetN()
{
luaplus_assert(L);
LuaAutoBlock autoBlock(L);
Push();
return luaL_getn(L, -1);
}
void LuaObject::SetN(int n)
{
luaplus_assert(L);
LuaAutoBlock autoBlock(L);
Push();
luaL_setn(LuaState_to_lua_State(L), -1, n);
}
#endif /* LUA_COMPAT_GETN */
void LuaObject::Insert(LuaObject& obj)
{
luaplus_assert(L);
luaplus_assert(L == obj.L);
LuaAutoBlock autoBlock(L);
LuaState* state = lua_State_To_LuaState(L);
LuaObject tableObj = state->GetGlobal("table");
LuaObject funcObj = tableObj["insert"];
luaplus_assert(funcObj.IsFunction());
LuaCall callObj(funcObj);
callObj << *this << obj << LuaRun();
}
void LuaObject::Insert(int index, LuaObject& obj)
{
luaplus_assert(L);
luaplus_assert(L == obj.L);
LuaAutoBlock autoBlock(L);
LuaState* state = lua_State_To_LuaState(L);
LuaObject tableObj = state->GetGlobal("table");
LuaObject funcObj = tableObj["insert"];
luaplus_assert(funcObj.IsFunction());
LuaCall callObj(funcObj);
callObj << *this << index << obj << LuaRun();
}
void LuaObject::Remove(int index)
{
luaplus_assert(L);
LuaAutoBlock autoBlock(L);
LuaState* state = lua_State_To_LuaState(L);
LuaObject tableObj = state->GetGlobal("table");
LuaObject funcObj = tableObj["remove"];
luaplus_assert(funcObj.IsFunction());
LuaCall callObj(funcObj);
callObj << *this << index << LuaRun();
}
void LuaObject::Sort()
{
luaplus_assert(L);
LuaAutoBlock autoBlock(L);
LuaState* state = lua_State_To_LuaState(L);
LuaObject tableObj = state->GetGlobal("table");
LuaObject funcObj = tableObj["sort"];
luaplus_assert(funcObj.IsFunction());
LuaCall callObj(funcObj);
callObj << *this << LuaRun();
}
size_t LuaObject::GetCount() const
{
luaplus_assert(L);
Push();
size_t count = lua_objlen(L, -1);
lua_pop(L, 1);
return count;
}
size_t LuaObject::GetTableCount() const
{
size_t count = 0;
for (LuaTableIterator it(*this); it; ++it)
{
count++;
}
return count;
}
/**
Creates a table called [name] within the current LuaObject.
@param key The name of the table to create.
@param size The size of the table.
@return Returns the object representing the newly created table.
**/
LuaObject LuaObject::CreateTable(const char* key, int narray, int lnhash)
{
luaplus_assert(L);
LuaObject ret(L);
sethvalue2n(L, &ret.m_object, luaH_new(L, narray, lnhash));
SetTableHelper(key, &ret.m_object);
return ret;
}
/**
Creates a table called [key] within the current LuaStackObject.
@param index The index of the table to create.
@param size The size of the table.
@return Returns the object representing the newly created table.
**/
LuaObject LuaObject::CreateTable(int key, int narray, int lnhash)
{
luaplus_assert(L);
LuaObject ret(L);
sethvalue2n(L, &ret.m_object, luaH_new(LuaState_to_lua_State(L), narray, lnhash));
SetTableHelper(key, &ret.m_object);
return ret;
}
/**
Creates a table called [key] within the current LuaStackObject.
@param index The index of the table to create.
@param size The size of the table.
@return Returns the object representing the newly created table.
**/
LuaObject LuaObject::CreateTable(LuaObject& key, int narray, int lnhash)
{
luaplus_assert(L);
LuaObject ret(L);
sethvalue2n(L, &ret.m_object, luaH_new(L, narray, lnhash));
SetTableHelper(key, &ret.m_object);
return ret;
}
LuaObject LuaObject::Get(const char* key) const
{
luaplus_assert(L);
api_check(L, ttistable(&m_object));
TValue str;
setsvalue(L, &str, luaS_newlstr(L, key, strlen(key)));
TValue v;
luaV_gettable(L, &m_object, &str, &v);
setnilvalue(&str);
return LuaObject(L, &v);
}
LuaObject LuaObject::Get(int key) const
{
luaplus_assert(L);
api_check(L, ttistable(&m_object));
TValue obj;
setnvalue2n(&obj, key);
TValue v;
luaV_gettable(L, &m_object, &obj, &v);
return LuaObject(L, &v);
}
LuaObject LuaObject::Get(const LuaObject& key) const
{
luaplus_assert(L);
api_check(L, ttistable(&m_object));
TValue v;
luaV_gettable(L, &m_object, (TValue*)&key.m_object, &v);
return LuaObject(L, &v);
}
LuaObject LuaObject::Get(const LuaStackObject& key) const
{
luaplus_assert(L);
api_check(L, ttistable(&m_object));
TValue v;
luaV_gettable(L, &m_object, index2adr(L, key.m_stackIndex), &v);
return LuaObject(L, &v);
}
LuaObject LuaObject::GetByName(const char* key) const
{
return Get(key);
}
LuaObject LuaObject::GetByIndex(int key) const
{
return Get(key);
}
LuaObject LuaObject::GetByObject(const LuaObject& key) const
{
luaplus_assert(L);
api_check(L, ttistable(&m_object));
TValue v;
luaV_gettable(L, &m_object, (TValue*)&key.m_object, &v);
return LuaObject(L, &v);
}
LuaObject LuaObject::GetByObject(const LuaStackObject& key) const
{
luaplus_assert(L);
api_check(L, ttistable(&m_object));
TValue v;
luaV_gettable(L, &m_object, index2adr(L, key.m_stackIndex), &v);
return LuaObject(L, &v);
}
LuaObject LuaObject::RawGet(const char* key) const
{
luaplus_assert(L);
api_check(L, ttistable(&m_object));
TValue str;
setnilvalue2n(L, &str);
// It's safe to assume that if name is not in the hash table, this function can return nil.
size_t l = strlen(key);
GCObject *o;
unsigned int h = (unsigned int)l; /* seed */
size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
size_t l1;
for (l1=l; l1>=step; l1-=step) /* compute hash */
h = h ^ ((h<<5)+(h>>2)+(unsigned char)(key[l1-1]));
for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
o != NULL;
o = o->gch.next)
{
TString *ts = rawgco2ts(o);
if (ts->tsv.tt == LUA_TSTRING && ts->tsv.len == l && (memcmp(key, getstr(ts), l) == 0))
{