-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsgs_cppbc.h
1212 lines (1106 loc) · 42.6 KB
/
sgs_cppbc.h
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
#ifndef __SGS_CPPBC_H__
#define __SGS_CPPBC_H__
#ifndef HEADER_SGSCRIPT_H
# define HEADER_SGSCRIPT_H <sgscript.h>
#endif
#include HEADER_SGSCRIPT_H
#include <new>
#include <assert.h>
#ifdef SGS_CPPBC_WITH_STD_STRING
# include <string>
#endif
#ifdef SGS_CPPBC_WITH_STD_VECTOR
# include <vector>
#endif
struct sgsLiteObjectBase {};
struct sgsObjectBase : sgsLiteObjectBase
{
sgsObjectBase() : m_sgsObject(NULL), C(NULL){}
sgs_VarObj* m_sgsObject;
SGS_CTX;
};
#ifndef SGS_CPPBC_PROCESS
struct _sgsInterface
{
_sgsInterface( const sgs_ObjInterface& src, sgs_CFunc ifn )
: iface(src), iface_func(ifn)
{
}
void init( SGS_CTX, sgs_Variable* out ) const
{
if( iface_func )
{
sgs_PushInterface( C, iface_func );
sgs_ObjSetMetaObj( C,
out ? sgs_GetObjectStructP( out ) : sgs_GetObjectStruct( C, -2 ),
sgs_GetObjectStruct( C, -1 ) );
sgs_Pop( C, 1 );
}
}
operator sgs_ObjInterface* (){ return &iface; }
sgs_ObjInterface* operator -> (){ return &iface; }
sgs_ObjInterface iface;
sgs_CFunc iface_func;
};
#ifdef SGS_CPPBC_PP
# define SGS_CPPBC_IGNORE(x)
#else
# define SGS_CPPBC_IGNORE(x) x
# define SGS_OBJECT_LITE \
int _sgs_destruct( SGS_CTX, sgs_VarObj* obj ); \
int _sgs_gcmark( SGS_CTX, sgs_VarObj* obj ); \
int _sgs_getindex( SGS_CTX, sgs_VarObj* obj ); \
int _sgs_setindex( SGS_CTX, sgs_VarObj* obj ); \
int _sgs_convert( SGS_CTX, sgs_VarObj* obj, int type ); \
int _sgs_serialize( SGS_CTX, sgs_VarObj* obj ); \
int _sgs_dump( SGS_CTX, sgs_VarObj* obj, int depth ); \
int _sgs_getnext( SGS_CTX, sgs_VarObj* obj, int flags ); \
int _sgs_call( SGS_CTX, sgs_VarObj* obj ); \
int _sgs_expr( SGS_CTX, sgs_VarObj* obj ); \
static int _sgslocal_destruct( SGS_CTX, sgs_VarObj* obj ); \
static int _sgslocal_gcmark( SGS_CTX, sgs_VarObj* obj ); \
static int _sgslocal_getindex( SGS_CTX, sgs_VarObj* obj ); \
static int _sgslocal_setindex( SGS_CTX, sgs_VarObj* obj ); \
static int _sgslocal_convert( SGS_CTX, sgs_VarObj* obj, int type ); \
static int _sgslocal_serialize( SGS_CTX, sgs_VarObj* obj ); \
static int _sgslocal_dump( SGS_CTX, sgs_VarObj* obj, int depth ); \
static int _sgslocal_getnext( SGS_CTX, sgs_VarObj* obj, int flags ); \
static int _sgslocal_call( SGS_CTX, sgs_VarObj* obj ); \
static int _sgslocal_expr( SGS_CTX, sgs_VarObj* obj ); \
int _sgsimpl_destruct( SGS_CTX, sgs_VarObj* obj ); \
int _sgsimpl_gcmark( SGS_CTX, sgs_VarObj* obj ); \
int _sgsimpl_getindex( SGS_CTX, sgs_VarObj* obj ); \
int _sgsimpl_setindex( SGS_CTX, sgs_VarObj* obj ); \
int _sgsimpl_serialize( SGS_CTX, sgs_VarObj* obj ); \
int _sgsimpl_dump( SGS_CTX, sgs_VarObj* obj, int depth ); \
static _sgsInterface _sgs_interface;
# define SGS_OBJECT SGS_OBJECT_LITE
# define SGS_OBJECT_INHERIT( name ) SGS_OBJECT_LITE
# define SGS_NO_DESTRUCT
# define SGS_NO_SERIALIZE
# define SGS_NO_UNSERIALIZE
# define SGS_METHOD
# define SGS_METHOD_NAMED( name )
# define SGS_STATICMETHOD static
# define SGS_STATICMETHOD_NAMED( name ) static
# define SGS_MULTRET int
# define SGS_PROPERTY
# define SGS_PROPERTY_FUNC( funcs )
# define SGS_PROPFN( funcs )
# define SGS_GCREF( mbname )
# define SGS_DUMP( what )
# define SGS_NODUMP( what )
# define SGS_BACKING_STORE( what )
# define SGS_IFUNC( type )
# define SGS_ALIAS( func )
# define SGS_UNSERIALIZE_FUNC( func )
# define SGS_CPPBC_INHERIT_BEGIN
# define SGS_CPPBC_INHERIT_END
#endif
#define SGS_DEFAULT_LITE_OBJECT_INTERFACE( name ) \
template<> inline void sgs_PushVar<name>( SGS_CTX, const name& v ){ sgs_CreateLiteClassFrom( C, NULL, &v ); } \
template<> struct sgs_GetVar<name> { name operator () ( SGS_CTX, sgs_StkIdx item ){ \
if( sgs_IsObject( C, item, name::_sgs_interface ) ) return *(name*)sgs_GetObjectData( C, item ); return name(); }};
#endif
#define TOKENPASTE_(x, y) x ## y
#define TOKENPASTE(x, y) TOKENPASTE_(x, y)
template< class T > class _sgsTmpChanger
{
T& dst;
T src;
T bk;
public:
_sgsTmpChanger( T& d, const T s ) : dst(d), src(s), bk(d){ d = src; }
~_sgsTmpChanger(){ dst = bk; }
};
class sgsScope
{
public:
sgsScope( sgs_Context* c ) : C(c), m_stackSize( sgs_StackSize( c ) ){}
~sgsScope()
{
assert( m_stackSize <= sgs_StackSize( C ) );
sgs_SetStackSize( C, m_stackSize );
}
bool is_restored(){ return sgs_StackSize( C ) == m_stackSize; }
SGS_CTX;
sgs_StkIdx m_stackSize;
};
#define SGS_CSCOPE( C ) sgsScope TOKENPASTE( _scp, __LINE__ ) ( C )
#define SGS_SCOPE SGS_CSCOPE( C )
enum sgsEMaybeNot { sgsMaybeNot };
enum sgsEPickAndPop { sgsPickAndPop };
enum sgsEMayNotExist { sgsMayNotExist };
template< class T >
class sgsMaybe /* nullable PODs */
{
public:
sgsMaybe() : isset(false) {};
sgsMaybe( sgsEMaybeNot ) : isset(false) {};
sgsMaybe( const T& val ) : data(val), isset(true) {}
void set( const T& val ){ data = val; isset = true; }
void unset(){ isset = false; }
bool operator == ( const sgsMaybe<T>& o ) const
{
if( isset != o.isset ) return false;
else if( !isset ) return true;
else return data == o.data;
}
bool operator != ( const sgsMaybe<T>& o ) const { return !( *this == o ); }
T data;
bool isset;
};
template< class OwningClass >
class sgsArrayIterator
{
public:
sgsArrayIterator( OwningClass* owner ) : m_owner(owner), m_origsize(owner->size()), m_offset(-1){ sgs_ObjAcquire( m_owner->C, m_owner->m_sgsObject ); }
sgsArrayIterator( const sgsArrayIterator& other ) : m_owner(other.m_owner), m_origsize(other.m_origsize), m_offset(other.m_offset){ sgs_ObjAcquire( m_owner->C, m_owner->m_sgsObject ); }
const sgsArrayIterator& operator = ( const sgsArrayIterator& other )
{
sgs_ObjRelease( m_owner->C, m_owner->m_sgsObject );
m_owner = other.m_owner; m_origsize = other.m_origsize; m_offset = other.m_offset;
sgs_ObjAcquire( m_owner->C, m_owner->m_sgsObject );
}
~sgsArrayIterator(){ sgs_ObjRelease( m_owner->C, m_owner->m_sgsObject ); }
void gcmark(){ sgs_ObjGCMark( m_owner->C, m_owner->m_sgsObject ); }
int convert( SGS_CTX, sgs_VarObj* obj, int to )
{
if( to == SGS_CONVOP_TOITER ){ sgs_PushObjectPtr( C, obj ); return SGS_SUCCESS; }
if( to == SGS_CONVOP_CLONE ){ sgs_PushLiteClassFrom( C, this ); return SGS_SUCCESS; }
return SGS_ENOTSUP;
}
int getnext( SGS_CTX, int mode )
{
if( m_origsize != m_owner->size() )
return SGS_EINPROC;
if( !mode ) // incr
{
m_offset++;
return m_offset >= 0 && m_offset < m_origsize;
}
if( mode & SGS_GETNEXT_KEY ) sgs_PushInt( C, m_offset );
if( mode & SGS_GETNEXT_VALUE ) sgs_PushVar( C, (*m_owner)[ m_offset ] );
return SGS_SUCCESS;
}
static int _sgs_destruct( SGS_CTX, sgs_VarObj* obj ){ ((sgsArrayIterator*)obj->data)->~sgsArrayIterator(); return SGS_SUCCESS; }
static int _sgs_gcmark( SGS_CTX, sgs_VarObj* obj ){ ((sgsArrayIterator*)obj->data)->gcmark(); return SGS_SUCCESS; }
static int _sgs_convert( SGS_CTX, sgs_VarObj* obj, int type ){ return ((sgsArrayIterator*)obj->data)->convert( C, obj, type ); }
static int _sgs_getnext( SGS_CTX, sgs_VarObj* obj, int type ){ return ((sgsArrayIterator*)obj->data)->getnext( C, type ); }
static sgs_ObjInterface _sgs_interface[1];
OwningClass* m_owner;
sgs_SizeVal m_origsize, m_offset;
};
template< class OwningClass >
sgs_ObjInterface sgsArrayIterator<OwningClass>::_sgs_interface[1] =
{
OwningClass::IteratorTypeName,
sgsArrayIterator::_sgs_destruct, sgsArrayIterator::_sgs_gcmark,
NULL, NULL,
sgsArrayIterator::_sgs_convert, NULL, NULL, sgsArrayIterator::_sgs_getnext,
NULL, NULL
};
// traverse the hierarchy to find the child class in it
// can't do it in reverse for now
// (no way to indicate that sgs_ObjInterface can be downcasted to _sgsInterface)
inline bool _sgsIsChild( const sgs_ObjInterface* child, const sgs_ObjInterface* parent )
{
while( child )
{
if( child == parent )
return true;
child = child->parent;
}
return false;
}
template< class T >
class sgsHandle
{
public:
sgsHandle() : object(NULL), C(NULL) {};
sgsHandle( const sgsHandle& h ) : object(h.object), C(sgs_RootContext(h.C))
{
if( object )
_acquire();
}
sgsHandle( sgs_Context* c, sgs_VarObj* obj, bool cast = true )
: object(NULL), C(sgs_RootContext(c))
{
_init( obj, cast );
}
sgsHandle( sgs_Context* c, sgs_StkIdx item, bool cast = true )
: object(NULL), C(sgs_RootContext(c))
{
if( sgs_ItemType( c, item ) == SGS_VT_OBJECT )
_init( sgs_GetObjectStruct( c, item ), cast );
}
sgsHandle( sgs_Context* c, sgs_Variable* var, bool cast = true )
: object(NULL), C(sgs_RootContext(c))
{
if( var->type == SGS_VT_OBJECT )
_init( var->data.O, cast );
}
explicit sgsHandle( T* obj ) : object(NULL), C(NULL)
{
if( obj )
{
object = obj->m_sgsObject;
C = obj->C;
_acquire();
}
}
~sgsHandle(){ _release(); }
void _init( sgs_VarObj* obj, bool cast )
{
if( !obj )
return;
if( obj->iface == T::_sgs_interface ||
( cast && _sgsIsChild( obj->iface, T::_sgs_interface ) ) )
{
object = obj;
_acquire();
}
}
const sgsHandle& operator = ( const sgsHandle& h )
{
if( object != h.object )
{
_release();
if( h.object )
{
object = h.object;
C = h.C;
_acquire();
}
}
return *this;
}
const sgsHandle& operator = ( T* obj )
{
if( object != obj->m_sgsObject )
{
_release();
if( obj->m_sgsObject )
{
object = obj->m_sgsObject;
C = obj->C;
_acquire();
}
}
return *this;
}
operator T*(){ return get(); }
operator const T*() const { return get(); }
T* operator -> (){ return get(); }
const T* operator -> () const { return get(); }
bool operator < ( const sgsHandle& h ) const { return object < h.object; }
bool operator == ( const sgsHandle& h ) const { return object == h.object; }
bool operator != ( const sgsHandle& h ) const { return object != h.object; }
sgsLiteObjectBase* get_liteobj(){ return object ? static_cast<sgsLiteObjectBase*>( object->data ) : NULL; }
const sgsLiteObjectBase* get_liteobj() const { return object ? static_cast<sgsLiteObjectBase*>( object->data ) : NULL; }
T* get(){ return object ? static_cast<T*>( get_liteobj() ) : NULL; }
const T* get() const { return object ? static_cast<const T*>( get_liteobj() ) : NULL; }
class sgsString serialize( int mode = 2 );
void gcmark() const { if( object ) sgs_ObjGCMark( C, object ); }
void push( sgs_Context* c ) const { assert( c ); sgs_Variable v;
v.type = object ? SGS_VT_OBJECT : SGS_VT_NULL; v.data.O = object; sgs_PushVariable( c, v ); }
bool is_null() const { return !object; }
bool not_null() const { return !!object; }
class sgsVariable get_variable();
sgs_VarObj* object;
void _acquire(){ if( object ) sgs_ObjAcquire( C, object ); }
void _release(){ if( object ){ sgs_ObjRelease( C, object ); object = NULL; } }
sgs_Context* get_ctx() const { return C; }
void set_ctx( sgs_Context* c ){ C = sgs_RootContext( c ); }
protected: /* prevent mysterious bugs from setting a non-root context */
SGS_CTX;
};
class sgsString
{
public:
sgsString() : str(NULL), C(NULL) {};
sgsString( const sgsString& h ) : str(h.str), C(sgs_RootContext(h.C)) { _acquire(); }
sgsString( sgs_Context* c, sgs_iStr* s ) : str(s), C(sgs_RootContext(c)) { _acquire(); }
sgsString( sgs_Context* c, sgs_StkIdx item ) : str(NULL), C(sgs_RootContext(c))
{
if( sgs_ParseString( c, item, NULL, NULL ) )
{
sgs_Variable v;
sgs_GetStackItem( c, item, &v );
str = v.data.S;
}
}
sgsString( sgs_Context* c, sgsEPickAndPop ) : str(NULL), C(sgs_RootContext(c))
{
if( sgs_ParseString( c, -1, NULL, NULL ) )
{
sgs_Variable v;
sgs_GetStackItem( c, -1, &v );
str = v.data.S;
}
sgs_Pop( C, 1 );
}
sgsString( sgs_Context* c, sgs_Variable* var ) : str(NULL), C(sgs_RootContext(c))
{
if( sgs_ParseStringP( c, var, NULL, NULL ) )
{
str = var->data.S;
_acquire();
}
}
sgsString( sgs_Context* c, const char* s, size_t sz ) : str(NULL), C(sgs_RootContext(c))
{
assert( sz <= 0x7fffffff );
sgs_Variable v;
sgs_InitStringBuf( c, &v, s, (sgs_SizeVal) sz );
str = v.data.S;
}
sgsString( sgs_Context* c, const char* s ) : str(NULL), C(sgs_RootContext(c))
{
sgs_Variable v;
sgs_InitString( c, &v, s );
str = v.data.S;
}
~sgsString(){ _release(); }
const sgsString& operator = ( const sgsString& s )
{
if( !same_as( s ) )
{
_release();
if( s.str )
{
str = s.str;
C = s.get_ctx();
_acquire();
}
}
return *this;
}
const char* c_str() const { return str ? sgs_str_c_cstr( str ) : ""; }
size_t size() const { return str ? (size_t) str->size : 0; }
#ifdef SGS_CPPBC_WITH_STD_STRING
bool get_string( std::string& out ){ if( str ){ out = std::string( sgs_str_c_cstr( str ), str->size ); return true; } return false; }
#endif
int compare( const sgsString& s ) const
{
if( same_as( s ) )
return 0;
int null1 = str == NULL;
int null2 = s.str == NULL;
if( null1 || null2 )
return null2 - null1;
int cmp = memcmp( sgs_str_c_cstr( str ), sgs_str_c_cstr( s.str ), str->size < s.str->size ? str->size : s.str->size );
if( cmp )
return cmp;
return str->size == s.str->size ? 0 : ( str->size < s.str->size ? -1 : 1 );
}
bool operator < ( const sgsString& s ) const { return compare( s ) < 0; }
bool operator == ( const sgsString& s ) const { return same_as( s ); }
bool operator != ( const sgsString& s ) const { return !same_as( s ); }
bool same_as( const sgsString& s ) const { return str == s.str; }
bool equals( const char* s ) const { if( !str && !*s ) return true;
return str && strcmp( sgs_str_c_cstr( str ), s ) == 0; }
bool equals( const char* s, size_t sz ) const { if( !str && !sz ) return true;
return str && sz == str->size && memcmp( sgs_str_c_cstr( str ), s, sz ) == 0; }
void push( sgs_Context* c ) const { assert( c ); sgs_Variable v;
v.type = str ? SGS_VT_STRING : SGS_VT_NULL; v.data.S = str; sgs_PushVariable( c, v ); }
bool is_null() const { return !str; }
bool not_null() const { return !!str; }
class sgsVariable get_variable();
class sgsVariable unserialize( int mode = 2, bool* result = NULL );
sgs_iStr* str;
void _acquire(){ if( str ){ sgs_Variable v; v.type = SGS_VT_STRING; v.data.S = str; sgs_Acquire( C, &v ); } }
void _release(){ if( str ){ sgs_Variable v; v.type = SGS_VT_STRING; v.data.S = str; sgs_Release( C, &v ); str = NULL; } }
sgs_Context* get_ctx() const { return C; }
void set_ctx( sgs_Context* c ){ C = sgs_RootContext( c ); }
protected: /* prevent mysterious bugs from setting a non-root context */
SGS_CTX;
};
struct sgsCall_KeepOnStack {};
class sgsVariable
{
public:
sgsVariable() : C(NULL) { var.type = SGS_VT_NULL; };
sgsVariable( const sgsVariable& h ) : var(h.var), C(h.C)
{
if( h.var.type != SGS_VT_NULL )
_acquire();
}
sgsVariable( sgs_Context* c ) : C(sgs_RootContext(c)) { var.type = SGS_VT_NULL; }
sgsVariable( sgs_Context* c, sgs_StkIdx item ) : C(sgs_RootContext(c))
{
var = sgs_StackItem( c, item );
_acquire();
}
sgsVariable( sgs_Context* c, sgs_StkIdx item, sgsEMayNotExist ) : C(sgs_RootContext(c))
{
var = sgs_OptStackItem( c, item );
_acquire();
}
sgsVariable( sgs_Context* c, sgsEPickAndPop ) : C(sgs_RootContext(c))
{
var = sgs_StackItem( c, -1 );
_acquire();
sgs_Pop( c, 1 );
}
sgsVariable( sgs_Context* c, sgs_Variable v ) : C(sgs_RootContext(c))
{
var = v;
_acquire();
}
sgsVariable( sgs_Context* c, sgs_Variable* v, bool transfer = false ) : C(sgs_RootContext(c))
{
var.type = SGS_VT_NULL;
if( v && v->type != SGS_VT_NULL )
{
var = *v;
if( !transfer )
_acquire();
}
}
sgsVariable( sgs_Context* c, sgs_VarObj* o ) : C(sgs_RootContext(c))
{
var.type = SGS_VT_NULL;
if( o )
{
sgs_InitObjectPtr( &var, o );
// acquired by ^this
}
}
sgsVariable( const sgsString& s ) : C(sgs_RootContext(s.get_ctx()))
{
if( s.str != NULL )
{
var.type = SGS_VT_STRING;
var.data.S = s.str;
_acquire();
}
else
var.type = SGS_VT_NULL;
}
template< class T > sgsVariable( const sgsHandle<T>& h ) : C(h.get_ctx())
{
if( h.object != NULL )
{
var.type = SGS_VT_OBJECT;
var.data.O = h.object;
_acquire();
}
else
var.type = SGS_VT_NULL;
}
~sgsVariable(){ _release(); }
const sgsVariable& operator = ( const sgsVariable& h )
{
if( *this != h )
{
_release();
if( h.var.type != SGS_VT_NULL )
{
var = h.var;
C = h.get_ctx();
_acquire();
}
}
return *this;
}
bool operator < ( const sgsVariable& h ) const
{
if( var.type != h.var.type )
return var.type < h.var.type;
switch( var.type )
{
case SGS_VT_BOOL: return var.data.B < h.var.data.B;
case SGS_VT_INT: return var.data.I < h.var.data.I;
case SGS_VT_REAL: return var.data.R < h.var.data.R;
case SGS_VT_STRING: { uint32_t minsize =
var.data.S->size < h.var.data.S->size ? var.data.S->size : h.var.data.S->size;
int diff = memcmp( sgs_var_cstr( &var ), sgs_var_cstr( &h.var ), minsize );
if( diff ) return diff < 0;
return var.data.S->size < h.var.data.S->size; }
case SGS_VT_FUNC: return var.data.F < h.var.data.F;
case SGS_VT_CFUNC: return var.data.C < h.var.data.C;
case SGS_VT_DFUNC: return var.data.D < h.var.data.D;
case SGS_VT_OBJECT: return var.data.O < h.var.data.O;
case SGS_VT_PTR: return var.data.P < h.var.data.P;
}
return false;
}
bool operator == ( const sgsVariable& h ) const
{
if( var.type != h.var.type )
return false;
switch( var.type )
{
case SGS_VT_BOOL: return var.data.B == h.var.data.B;
case SGS_VT_INT: return var.data.I == h.var.data.I;
case SGS_VT_REAL: return var.data.R == h.var.data.R;
case SGS_VT_STRING:
#if SGS_STRINGTABLE_MAXLEN >= 0x7fffffff
return var.data.S == h.var.data.S;
#else
if( var.data.S == h.var.data.S ) return true;
return var.data.S->size == h.var.data.S->size &&
memcmp( sgs_var_cstr( &var ), sgs_var_cstr( &h.var ), var.data.S->size ) == 0;
#endif
case SGS_VT_FUNC: return var.data.F == h.var.data.F;
case SGS_VT_CFUNC: return var.data.C == h.var.data.C;
case SGS_VT_DFUNC: return var.data.D == h.var.data.D;
case SGS_VT_OBJECT: return var.data.O == h.var.data.O;
case SGS_VT_PTR: return var.data.P == h.var.data.P;
}
return true;
}
bool operator != ( const sgsVariable& h ) const { return !( *this == h ); }
void push( sgs_Context* c ) const { assert( c ); sgs_PushVariable( c, var ); }
void gcmark() { if( C ) sgs_GCMark( C, &var ); }
bool is_null() const { return var.type == SGS_VT_NULL; }
bool not_null() const { return var.type != SGS_VT_NULL; }
bool is_object( sgs_ObjInterface* iface ){ return !!sgs_IsObjectP( &var, iface ); }
template< class T > bool is_object(){ return sgs_IsObjectP( &var, T::_sgs_interface ); }
sgs_VarObj* get_object_struct() const { return var.type == SGS_VT_OBJECT ? var.data.O : NULL; }
template< class T > T* get_raw_object_data(){ return (T*) sgs_GetObjectDataP( &var ); }
template< class T > T* get_object_data(){ return static_cast<T*>((sgsLiteObjectBase*)sgs_GetObjectDataP( &var )); }
template< class T > sgsHandle<T> get_handle(){ return sgsHandle<T>( C, &var ); }
template< class T > sgsHandle<T> downcast(){ return sgsHandle<T>( C, &var, true ); }
int type_id() const { return (int) var.type; }
bool is_string() const { return var.type == SGS_VT_STRING; }
sgsString get_string(){ return is_string() ? sgsString( C, var.data.S ) : sgsString(); }
bool is_array() const { return sgs_IsArray( var ) != 0; }
bool is_dict() const { return sgs_IsDict( var ) != 0; }
bool is_map() const { return sgs_IsMap( var ) != 0; }
void set_meta_obj( sgsVariable metaobj )
{
sgs_ObjSetMetaObj( C, get_object_struct(), metaobj.get_object_struct() );
}
sgsVariable get_meta_obj()
{
return sgsVariable( C, sgs_ObjGetMetaObj( get_object_struct() ) );
}
bool get_metamethods_enabled(){ return sgs_ObjGetMetaMethodEnable( get_object_struct() ) != 0; }
void enable_metamethods( bool e ){ sgs_ObjSetMetaMethodEnable( get_object_struct(), e ); }
sgsString serialize( int mode = 2 )
{
if( !C )
return sgsString();
sgs_SerializeExt( C, var, mode );
return sgsString( C, sgsPickAndPop );
}
void create_array( sgs_Context* c, int n = 0 )
{
if( !c && !n ) c = C;
_release();
sgs_CreateArray( c, &var, n );
C = sgs_RootContext( c );
}
void create_dict( sgs_Context* c, int n = 0 )
{
if( !c && !n ) c = C;
_release();
sgs_CreateDict( c, &var, n );
C = sgs_RootContext( c );
}
void create_map( sgs_Context* c, int n = 0 )
{
if( !c && !n ) c = C;
_release();
sgs_CreateMap( c, &var, n );
C = sgs_RootContext( c );
}
/* indexing */
sgsVariable getsubitem( sgsVariable key, bool prop )
{
sgsVariable out(C);
if( not_null() )
sgs_GetIndex( C, var, key.var, &out.var, prop );
return out;
}
sgsVariable getsubitem( const char* key, bool prop )
{
if( !not_null() )
{
return sgsVariable( C );
}
return getsubitem( sgsString( C, key ).get_variable(), prop );
}
sgsVariable getprop( sgsVariable key ){ return getsubitem( key, true ); }
sgsVariable getindex( sgsVariable key ){ return getsubitem( key, false ); }
sgsVariable getprop( const char* key ){ return getsubitem( key, true ); }
sgsVariable getindex( const char* key ){ return getsubitem( key, false ); }
sgsVariable operator [] ( sgsVariable key ){ return getsubitem( key, false ); }
sgsVariable operator [] ( const char* key ){ return getsubitem( key, false ); }
bool setsubitem( sgsVariable key, sgsVariable val, bool prop )
{
return sgs_SetIndex( C, var, key.var, val.var, prop ) != 0;
}
bool setsubitem( const char* key, sgsVariable val, bool prop )
{
if( !not_null() )
{
return false;
}
return setsubitem( sgsString( C, key ).get_variable(), val, prop );
}
bool setprop( sgsVariable key, sgsVariable val ){ return setsubitem( key, val, true ); }
bool setindex( sgsVariable key, sgsVariable val ){ return setsubitem( key, val, false ); }
bool setprop( const char* key, sgsVariable val ){ return setsubitem( key, val, true ); }
bool setindex( const char* key, sgsVariable val ){ return setsubitem( key, val, false ); }
template< class T > T get();
template< class T > T getdef( const T& def ){ if( not_null() ) return get<T>(); else return def; }
sgsVariable& set_null(){ _release(); var = sgs_MakeNull(); return *this; }
sgsVariable& set_bool( bool v ){ _release(); var = sgs_MakeBool( v ); return *this; }
sgsVariable& set_int( sgs_Int v ){ _release(); var = sgs_MakeInt( v ); return *this; }
sgsVariable& set_real( sgs_Real v ){ _release(); var = sgs_MakeReal( v ); return *this; }
sgsVariable& set_ptr( void* v ){ _release(); var = sgs_MakePtr( v ); return *this; }
sgsVariable& set( sgsString v ){ _release(); if( v.not_null() ){ C = v.get_ctx(); var.type = SGS_VT_STRING; var.data.S = v.str; _acquire(); } return *this; }
sgsVariable& set( sgs_CFunc v ){ _release(); var = sgs_MakeCFunc( v ); return *this; }
sgsVariable& set( sgs_DFunc* v ){ _release(); var = sgs_MakeDFunc( v ); return *this; }
template< class T > sgsVariable& set( sgsHandle< T > v ){ _release(); C = v.object->C; sgs_InitObjectPtr( C, &var, v.object ); return *this; }
template< class T > sgsVariable& set( T* v ){ _release(); C = v->C; sgs_InitObjectPtr( C, &var, v->m_sgsObject ); return *this; }
void call( sgs_Context* c, int args = 0, int ret = 0 )
{
sgs_InsertVariable( c, -args - 1, var );
sgs_Call( c, args, ret );
}
void thiscall( sgs_Context* c, sgsVariable func, int args = 0, int ret = 0 )
{
sgs_InsertVariable( c, -args - 1, func.var );
sgs_InsertVariable( c, -args - 1, var );
sgs_ThisCall( c, args, ret );
}
void thiscall( sgs_Context* c, const char* key, int args = 0, int ret = 0 )
{
thiscall( c, getprop( key ), args, ret );
}
template< class RT > RT tcallu( sgs_Context* c, int args );
template< class RT >
RT tcall( sgs_Context* c ){ return tcallu<RT>( c, 0 ); }
template< class RT, class A0 >
RT tcall( sgs_Context* c, A0 const& a0 ){ sgs_PushVar( c, a0 ); return tcallu<RT>( c, 1 ); }
template< class RT, class A0, class A1 >
RT tcall( sgs_Context* c, A0 const& a0, A1 const& a1 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); return tcallu<RT>( c, 2 ); }
template< class RT, class A0, class A1, class A2 >
RT tcall( sgs_Context* c, A0 const& a0, A1 const& a1, A2 const& a2 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 ); return tcallu<RT>( c, 3 ); }
template< class RT, class A0, class A1, class A2, class A3 >
RT tcall( sgs_Context* c, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 );
sgs_PushVar( c, a3 ); return tcallu<RT>( c, 4 ); }
template< class RT, class A0, class A1, class A2, class A3, class A4 >
RT tcall( sgs_Context* c, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 );
sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); return tcallu<RT>( c, 5 ); }
template< class RT, class A0, class A1, class A2, class A3, class A4, class A5 >
RT tcall( sgs_Context* c, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 );
sgs_PushVar( c, a2 ); sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); sgs_PushVar( c, a5 ); return tcallu<RT>( c, 6 ); }
template< class RT, class A0, class A1, class A2, class A3, class A4, class A5, class A6 >
RT tcall( sgs_Context* c, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5, A6 const& a6 ){ sgs_PushVar( c, a0 );
sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 ); sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); sgs_PushVar( c, a5 ); sgs_PushVar( c, a6 ); return tcallu<RT>( c, 7 ); }
template< class RT, class A0, class A1, class A2, class A3, class A4, class A5, class A6, class A7 >
RT tcall( sgs_Context* c, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5, A6 const& a6, A7 const& a7 ){ sgs_PushVar( c, a0 );
sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 ); sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); sgs_PushVar( c, a5 ); sgs_PushVar( c, a6 ); sgs_PushVar( c, a7 ); return tcallu<RT>( c, 8 ); }
template< class RT > RT tthiscallu( sgs_Context* c, sgsVariable func, int args );
template< class RT > RT tthiscallu( sgs_Context* c, const char* key, int args ){ return tthiscallu<RT>( c, getprop( key ), args ); }
template< class RT, class FT >
RT tthiscall( sgs_Context* c, const FT& fn ){ return tthiscallu<RT>( c, fn, 0 ); }
template< class RT, class FT, class A0 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0 ){ sgs_PushVar( c, a0 ); return tthiscallu<RT>( c, fn, 1 ); }
template< class RT, class FT, class A0, class A1 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0, A1 const& a1 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); return tthiscallu<RT>( c, fn, 2 ); }
template< class RT, class FT, class A0, class A1, class A2 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0, A1 const& a1, A2 const& a2 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 ); return tthiscallu<RT>( c, fn, 3 ); }
template< class RT, class FT, class A0, class A1, class A2, class A3 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 );
sgs_PushVar( c, a3 ); return tthiscallu<RT>( c, fn, 4 ); }
template< class RT, class FT, class A0, class A1, class A2, class A3, class A4 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 );
sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); return tthiscallu<RT>( c, fn, 5 ); }
template< class RT, class FT, class A0, class A1, class A2, class A3, class A4, class A5 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5 ){ sgs_PushVar( c, a0 ); sgs_PushVar( c, a1 );
sgs_PushVar( c, a2 ); sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); sgs_PushVar( c, a5 ); return tthiscallu<RT>( c, fn, 6 ); }
template< class RT, class FT, class A0, class A1, class A2, class A3, class A4, class A5, class A6 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5, A6 const& a6 ){ sgs_PushVar( c, a0 );
sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 ); sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); sgs_PushVar( c, a5 ); sgs_PushVar( c, a6 ); return tthiscallu<RT>( c, fn, 7 ); }
template< class RT, class FT, class A0, class A1, class A2, class A3, class A4, class A5, class A6, class A7 >
RT tthiscall( sgs_Context* c, const FT& fn, A0 const& a0, A1 const& a1, A2 const& a2, A3 const& a3, A4 const& a4, A5 const& a5, A6 const& a6, A7 const& a7 ){ sgs_PushVar( c, a0 );
sgs_PushVar( c, a1 ); sgs_PushVar( c, a2 ); sgs_PushVar( c, a3 ); sgs_PushVar( c, a4 ); sgs_PushVar( c, a5 ); sgs_PushVar( c, a6 ); sgs_PushVar( c, a7 ); return tthiscallu<RT>( c, fn, 8 ); }
sgs_Variable var;
void _acquire(){ if( C ){ sgs_Acquire( C, &var ); } }
void _release(){ if( C ){ sgs_Release( C, &var ); var.type = SGS_VT_NULL; } }
sgs_Context* get_ctx() const { return C; }
void set_ctx( sgs_Context* c ){ C = sgs_RootContext( c ); }
protected: /* prevent mysterious bugs from setting a non-root context */
SGS_CTX;
};
template<class T>
inline sgsVariable sgsHandle<T>::get_variable()
{
sgs_Variable v;
v.type = object ? SGS_VT_OBJECT : SGS_VT_NULL;
v.data.O = object;
return sgsVariable( C, &v );
}
template< class T >
sgsString sgsHandle<T>::serialize( int mode )
{
return get_variable().serialize( mode );
}
inline sgsVariable sgsString::get_variable()
{
sgs_Variable v;
v.type = str ? SGS_VT_STRING : SGS_VT_NULL;
v.data.S = str;
return sgsVariable( C, &v );
}
inline sgsVariable sgsString::unserialize( int mode, bool* result )
{
if( !C ) return sgsVariable();
if( !str ) return sgsVariable( C );
sgs_Variable v;
v.type = SGS_VT_STRING;
v.data.S = str;
bool r = sgs_UnserializeExt( C, v, mode ) != 0;
if( result )
*result = r;
return sgsVariable( C, sgsPickAndPop );
}
struct sgsVarIterator
{
sgsVarIterator( SGS_CTX, sgs_Variable* var ){ _Init( C, var ); }
sgsVarIterator( sgsVariable var ){ _Init( var.get_ctx(), &var.var ); }
void _Init( SGS_CTX, sgs_Variable* var )
{
if( !C )
return;
sgs_Variable it;
if( SGS_SUCCEEDED( sgs_CreateIterator( C, &it, *var ) ) )
{
m_iter = sgsVariable( C, &it, /* transfer ownership = */ true );
}
}
sgsVariable GetKey()
{
sgsVariable out( m_iter.get_ctx() );
if( m_iter.get_ctx() )
sgs_IterGetData( m_iter.get_ctx(), m_iter.var, &out.var, NULL );
return out;
}
sgsVariable GetValue()
{
sgsVariable out( m_iter.get_ctx() );
if( m_iter.get_ctx() )
sgs_IterGetData( m_iter.get_ctx(), m_iter.var, NULL, &out.var );
return out;
}
bool Advance()
{
return m_iter.get_ctx() && sgs_IterAdvance( m_iter.get_ctx(), m_iter.var ) > 0;
}
sgsVariable m_iter;
};
inline void sgsAssignProperties( sgsVariable to, sgsVariable from, const char* exclprefix )
{
if( !( from.not_null() && to.not_null() ) )
return;
size_t eplen = exclprefix ? strlen( exclprefix ) : 0;
sgsVarIterator it( from );
while( it.Advance() )
{
sgsVariable key = it.GetKey();
if( exclprefix && key.is_string() )
{
sgsString str = key.get_string();
if( str.size() >= eplen &&
memcmp( str.c_str(), exclprefix, eplen ) == 0 )
continue;
}
to.setindex( key, it.GetValue() );
}
}
/* GCMark<T> */
template< class T > void sgs_GCMarkVar( SGS_CTX, T& var ){}
template<> inline void sgs_GCMarkVar<sgs_Variable>( SGS_CTX, sgs_Variable& v ){ sgs_GCMark( C, &v ); }
template<> inline void sgs_GCMarkVar<sgsVariable>( SGS_CTX, sgsVariable& v ){ v.gcmark(); }
template< class T > inline void sgs_GCMarkVar( SGS_CTX, sgsHandle<T>& v ){ v.gcmark(); }
#ifdef SGS_CPPBC_WITH_STD_VECTOR
template< class T > inline void sgs_GCMarkVar( SGS_CTX, std::vector<T>& v ){
for( size_t i = 0; i < v.size(); ++i ) sgs_GCMarkVar( C, v[i] ); }
#endif
/* Dump<T> */
template< class T > sgsString sgs_DumpData( SGS_CTX, const T& var, int depth ){ return sgsString( C, "<unknown>" ); }
template< class T > sgsString sgs_DumpData( SGS_CTX, const T* var, int depth )
{
if( !var )
return sgsString( C, "null" );
return sgs_DumpData( C, *var, depth );
}
template< class T > inline sgsString sgs_DumpDataPushVar( SGS_CTX, const T& var, int depth )
{
SGS_SCOPE;
sgs_PushVar( C, var );
sgs_DumpVar( C, sgs_StackItem( C, -1 ), depth );
return sgsString( C, -1 );
}
template< class T > inline sgsString sgs_DumpData( SGS_CTX, const sgsMaybe<T>& var, int depth )
{
SGS_SCOPE;
if( var.isset )
{
sgs_PushString( C, "[set] " );
sgs_DumpData( C, var.data, depth ).push( C );
sgs_StringConcat( C, 2 );
return sgsString( C, -1 );
}
return sgsString( C, "[unset]" );
}
template<> inline sgsString sgs_DumpData<sgs_Variable>( SGS_CTX, const sgs_Variable& v, int depth )
{
SGS_SCOPE;
sgs_DumpVar( C, v, depth );
return sgsString( C, -1 );
}
template<> inline sgsString sgs_DumpData<sgsVariable>( SGS_CTX, const sgsVariable& v, int depth ){ return sgs_DumpData( C, v.var, depth ); }
template< class T > inline sgsString sgs_DumpData( SGS_CTX, const sgsHandle<T>& v, int depth ){ return sgs_DumpDataPushVar( C, v, depth ); }
template<> inline sgsString sgs_DumpData<bool>( SGS_CTX, const bool& v, int ){ return sgsString( C, v ? "true" : "false" ); }
#define SGS_DECL_DUMPDATA_INT( type ) \
template<> inline sgsString sgs_DumpData<type>( SGS_CTX, const type& v, int depth ){ return sgs_DumpDataPushVar( C, v, depth ); }
SGS_DECL_DUMPDATA_INT( signed char );
SGS_DECL_DUMPDATA_INT( unsigned char );
SGS_DECL_DUMPDATA_INT( signed short );
SGS_DECL_DUMPDATA_INT( unsigned short );
SGS_DECL_DUMPDATA_INT( signed int );
SGS_DECL_DUMPDATA_INT( unsigned int );
SGS_DECL_DUMPDATA_INT( signed long );
SGS_DECL_DUMPDATA_INT( unsigned long );
SGS_DECL_DUMPDATA_INT( signed long long );
SGS_DECL_DUMPDATA_INT( unsigned long long );
SGS_DECL_DUMPDATA_INT( float );
SGS_DECL_DUMPDATA_INT( double );
SGS_DECL_DUMPDATA_INT( sgsString );
#ifdef SGS_CPPBC_WITH_STD_STRING
SGS_DECL_DUMPDATA_INT( std::string );
#endif
#ifdef SGS_CPPBC_WITH_STD_VECTOR
template< class T > inline sgsString sgs_DumpData( SGS_CTX, const std::vector<T>& v, int depth )
{
SGS_SCOPE;
char bfr[ 32 ];
bool expand = depth-- > 0;
sprintf( bfr, "array(std) [%d]%s", (int) v.size(),
expand ? ( v.size() ? "\n{" : " {}" ) : " ..." );
sgs_PushString( C, bfr );
if( expand && v.size() )
{
for( size_t i = 0; i < v.size(); ++i )
{
sgs_PushString( C, "\n" );
sgs_DumpData( C, v[i], depth ).push( C );
}
sgs_StringConcat( C, (sgs_StkIdx) v.size() * 2 );
sgs_PadString( C );
sgs_PushString( C, "\n}" );
sgs_StringConcat( C, 3 );
}
return sgsString( C, -1 );
}
#endif
/* PushVar [stack] */
template< class T > void sgs_PushVar( SGS_CTX, const T& );
template< class T > inline void sgs_PushVar( SGS_CTX, T* v ){ sgs_CreateClass( C, NULL, v ); }
template< class T > inline void sgs_PushVar( SGS_CTX, sgsMaybe<T> v ){ if( v.isset ) sgs_PushVar( C, v.data ); else sgs_PushNull( C ); }
template< class T > inline void sgs_PushVar( SGS_CTX, sgsHandle<T> v ){ v.push( C ); }
template<> inline void sgs_PushVar<sgsVariable>( SGS_CTX, const sgsVariable& v ){ v.push( C ); }
template<> inline void sgs_PushVar( SGS_CTX, void* v ){ sgs_PushPtr( C, v ); }