The Squirrel interpreter. See http://www.squirrel-lang.org/

Dependents:   Squirrel

Committer:
jhnwkmn
Date:
Tue Dec 16 10:20:34 2014 +0000
Revision:
0:97a4f8cc534c
Initial import of Squirrel.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhnwkmn 0:97a4f8cc534c 1 /* see copyright notice in squirrel.h */
jhnwkmn 0:97a4f8cc534c 2 #ifndef _SQCLASS_H_
jhnwkmn 0:97a4f8cc534c 3 #define _SQCLASS_H_
jhnwkmn 0:97a4f8cc534c 4
jhnwkmn 0:97a4f8cc534c 5 struct SQInstance;
jhnwkmn 0:97a4f8cc534c 6
jhnwkmn 0:97a4f8cc534c 7 struct SQClassMember {
jhnwkmn 0:97a4f8cc534c 8 SQObjectPtr val;
jhnwkmn 0:97a4f8cc534c 9 SQObjectPtr attrs;
jhnwkmn 0:97a4f8cc534c 10 void Null() {
jhnwkmn 0:97a4f8cc534c 11 val.Null();
jhnwkmn 0:97a4f8cc534c 12 attrs.Null();
jhnwkmn 0:97a4f8cc534c 13 }
jhnwkmn 0:97a4f8cc534c 14 };
jhnwkmn 0:97a4f8cc534c 15
jhnwkmn 0:97a4f8cc534c 16 typedef sqvector<SQClassMember> SQClassMemberVec;
jhnwkmn 0:97a4f8cc534c 17
jhnwkmn 0:97a4f8cc534c 18 #define MEMBER_TYPE_METHOD 0x01000000
jhnwkmn 0:97a4f8cc534c 19 #define MEMBER_TYPE_FIELD 0x02000000
jhnwkmn 0:97a4f8cc534c 20
jhnwkmn 0:97a4f8cc534c 21 #define _ismethod(o) (_integer(o)&MEMBER_TYPE_METHOD)
jhnwkmn 0:97a4f8cc534c 22 #define _isfield(o) (_integer(o)&MEMBER_TYPE_FIELD)
jhnwkmn 0:97a4f8cc534c 23 #define _make_method_idx(i) ((SQInteger)(MEMBER_TYPE_METHOD|i))
jhnwkmn 0:97a4f8cc534c 24 #define _make_field_idx(i) ((SQInteger)(MEMBER_TYPE_FIELD|i))
jhnwkmn 0:97a4f8cc534c 25 #define _member_type(o) (_integer(o)&0xFF000000)
jhnwkmn 0:97a4f8cc534c 26 #define _member_idx(o) (_integer(o)&0x00FFFFFF)
jhnwkmn 0:97a4f8cc534c 27
jhnwkmn 0:97a4f8cc534c 28 struct SQClass : public CHAINABLE_OBJ
jhnwkmn 0:97a4f8cc534c 29 {
jhnwkmn 0:97a4f8cc534c 30 SQClass(SQSharedState *ss,SQClass *base);
jhnwkmn 0:97a4f8cc534c 31 public:
jhnwkmn 0:97a4f8cc534c 32 static SQClass* Create(SQSharedState *ss,SQClass *base) {
jhnwkmn 0:97a4f8cc534c 33 SQClass *newclass = (SQClass *)SQ_MALLOC(sizeof(SQClass));
jhnwkmn 0:97a4f8cc534c 34 new (newclass) SQClass(ss, base);
jhnwkmn 0:97a4f8cc534c 35 return newclass;
jhnwkmn 0:97a4f8cc534c 36 }
jhnwkmn 0:97a4f8cc534c 37 ~SQClass();
jhnwkmn 0:97a4f8cc534c 38 bool NewSlot(SQSharedState *ss, const SQObjectPtr &key,const SQObjectPtr &val,bool bstatic);
jhnwkmn 0:97a4f8cc534c 39 bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
jhnwkmn 0:97a4f8cc534c 40 if(_members->Get(key,val)) {
jhnwkmn 0:97a4f8cc534c 41 if(_isfield(val)) {
jhnwkmn 0:97a4f8cc534c 42 SQObjectPtr &o = _defaultvalues[_member_idx(val)].val;
jhnwkmn 0:97a4f8cc534c 43 val = _realval(o);
jhnwkmn 0:97a4f8cc534c 44 }
jhnwkmn 0:97a4f8cc534c 45 else {
jhnwkmn 0:97a4f8cc534c 46 val = _methods[_member_idx(val)].val;
jhnwkmn 0:97a4f8cc534c 47 }
jhnwkmn 0:97a4f8cc534c 48 return true;
jhnwkmn 0:97a4f8cc534c 49 }
jhnwkmn 0:97a4f8cc534c 50 return false;
jhnwkmn 0:97a4f8cc534c 51 }
jhnwkmn 0:97a4f8cc534c 52 bool GetConstructor(SQObjectPtr &ctor)
jhnwkmn 0:97a4f8cc534c 53 {
jhnwkmn 0:97a4f8cc534c 54 if(_constructoridx != -1) {
jhnwkmn 0:97a4f8cc534c 55 ctor = _methods[_constructoridx].val;
jhnwkmn 0:97a4f8cc534c 56 return true;
jhnwkmn 0:97a4f8cc534c 57 }
jhnwkmn 0:97a4f8cc534c 58 return false;
jhnwkmn 0:97a4f8cc534c 59 }
jhnwkmn 0:97a4f8cc534c 60 bool SetAttributes(const SQObjectPtr &key,const SQObjectPtr &val);
jhnwkmn 0:97a4f8cc534c 61 bool GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval);
jhnwkmn 0:97a4f8cc534c 62 void Lock() { _locked = true; if(_base) _base->Lock(); }
jhnwkmn 0:97a4f8cc534c 63 void Release() {
jhnwkmn 0:97a4f8cc534c 64 if (_hook) { _hook(_typetag,0);}
jhnwkmn 0:97a4f8cc534c 65 sq_delete(this, SQClass);
jhnwkmn 0:97a4f8cc534c 66 }
jhnwkmn 0:97a4f8cc534c 67 void Finalize();
jhnwkmn 0:97a4f8cc534c 68 #ifndef NO_GARBAGE_COLLECTOR
jhnwkmn 0:97a4f8cc534c 69 void Mark(SQCollectable ** );
jhnwkmn 0:97a4f8cc534c 70 SQObjectType GetType() {return OT_CLASS;}
jhnwkmn 0:97a4f8cc534c 71 #endif
jhnwkmn 0:97a4f8cc534c 72 SQInteger Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
jhnwkmn 0:97a4f8cc534c 73 SQInstance *CreateInstance();
jhnwkmn 0:97a4f8cc534c 74 SQTable *_members;
jhnwkmn 0:97a4f8cc534c 75 SQClass *_base;
jhnwkmn 0:97a4f8cc534c 76 SQClassMemberVec _defaultvalues;
jhnwkmn 0:97a4f8cc534c 77 SQClassMemberVec _methods;
jhnwkmn 0:97a4f8cc534c 78 SQObjectPtr _metamethods[MT_LAST];
jhnwkmn 0:97a4f8cc534c 79 SQObjectPtr _attributes;
jhnwkmn 0:97a4f8cc534c 80 SQUserPointer _typetag;
jhnwkmn 0:97a4f8cc534c 81 SQRELEASEHOOK _hook;
jhnwkmn 0:97a4f8cc534c 82 bool _locked;
jhnwkmn 0:97a4f8cc534c 83 SQInteger _constructoridx;
jhnwkmn 0:97a4f8cc534c 84 SQInteger _udsize;
jhnwkmn 0:97a4f8cc534c 85 };
jhnwkmn 0:97a4f8cc534c 86
jhnwkmn 0:97a4f8cc534c 87 #define calcinstancesize(_theclass_) \
jhnwkmn 0:97a4f8cc534c 88 (_theclass_->_udsize + sq_aligning(sizeof(SQInstance) + (sizeof(SQObjectPtr)*(_theclass_->_defaultvalues.size()>0?_theclass_->_defaultvalues.size()-1:0))))
jhnwkmn 0:97a4f8cc534c 89
jhnwkmn 0:97a4f8cc534c 90 struct SQInstance : public SQDelegable
jhnwkmn 0:97a4f8cc534c 91 {
jhnwkmn 0:97a4f8cc534c 92 void Init(SQSharedState *ss);
jhnwkmn 0:97a4f8cc534c 93 SQInstance(SQSharedState *ss, SQClass *c, SQInteger memsize);
jhnwkmn 0:97a4f8cc534c 94 SQInstance(SQSharedState *ss, SQInstance *c, SQInteger memsize);
jhnwkmn 0:97a4f8cc534c 95 public:
jhnwkmn 0:97a4f8cc534c 96 static SQInstance* Create(SQSharedState *ss,SQClass *theclass) {
jhnwkmn 0:97a4f8cc534c 97
jhnwkmn 0:97a4f8cc534c 98 SQInteger size = calcinstancesize(theclass);
jhnwkmn 0:97a4f8cc534c 99 SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
jhnwkmn 0:97a4f8cc534c 100 new (newinst) SQInstance(ss, theclass,size);
jhnwkmn 0:97a4f8cc534c 101 if(theclass->_udsize) {
jhnwkmn 0:97a4f8cc534c 102 newinst->_userpointer = ((unsigned char *)newinst) + (size - theclass->_udsize);
jhnwkmn 0:97a4f8cc534c 103 }
jhnwkmn 0:97a4f8cc534c 104 return newinst;
jhnwkmn 0:97a4f8cc534c 105 }
jhnwkmn 0:97a4f8cc534c 106 SQInstance *Clone(SQSharedState *ss)
jhnwkmn 0:97a4f8cc534c 107 {
jhnwkmn 0:97a4f8cc534c 108 SQInteger size = calcinstancesize(_class);
jhnwkmn 0:97a4f8cc534c 109 SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
jhnwkmn 0:97a4f8cc534c 110 new (newinst) SQInstance(ss, this,size);
jhnwkmn 0:97a4f8cc534c 111 if(_class->_udsize) {
jhnwkmn 0:97a4f8cc534c 112 newinst->_userpointer = ((unsigned char *)newinst) + (size - _class->_udsize);
jhnwkmn 0:97a4f8cc534c 113 }
jhnwkmn 0:97a4f8cc534c 114 return newinst;
jhnwkmn 0:97a4f8cc534c 115 }
jhnwkmn 0:97a4f8cc534c 116 ~SQInstance();
jhnwkmn 0:97a4f8cc534c 117 bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
jhnwkmn 0:97a4f8cc534c 118 if(_class->_members->Get(key,val)) {
jhnwkmn 0:97a4f8cc534c 119 if(_isfield(val)) {
jhnwkmn 0:97a4f8cc534c 120 SQObjectPtr &o = _values[_member_idx(val)];
jhnwkmn 0:97a4f8cc534c 121 val = _realval(o);
jhnwkmn 0:97a4f8cc534c 122 }
jhnwkmn 0:97a4f8cc534c 123 else {
jhnwkmn 0:97a4f8cc534c 124 val = _class->_methods[_member_idx(val)].val;
jhnwkmn 0:97a4f8cc534c 125 }
jhnwkmn 0:97a4f8cc534c 126 return true;
jhnwkmn 0:97a4f8cc534c 127 }
jhnwkmn 0:97a4f8cc534c 128 return false;
jhnwkmn 0:97a4f8cc534c 129 }
jhnwkmn 0:97a4f8cc534c 130 bool Set(const SQObjectPtr &key,const SQObjectPtr &val) {
jhnwkmn 0:97a4f8cc534c 131 SQObjectPtr idx;
jhnwkmn 0:97a4f8cc534c 132 if(_class->_members->Get(key,idx) && _isfield(idx)) {
jhnwkmn 0:97a4f8cc534c 133 _values[_member_idx(idx)] = val;
jhnwkmn 0:97a4f8cc534c 134 return true;
jhnwkmn 0:97a4f8cc534c 135 }
jhnwkmn 0:97a4f8cc534c 136 return false;
jhnwkmn 0:97a4f8cc534c 137 }
jhnwkmn 0:97a4f8cc534c 138 void Release() {
jhnwkmn 0:97a4f8cc534c 139 _uiRef++;
jhnwkmn 0:97a4f8cc534c 140 if (_hook) { _hook(_userpointer,0);}
jhnwkmn 0:97a4f8cc534c 141 _uiRef--;
jhnwkmn 0:97a4f8cc534c 142 if(_uiRef > 0) return;
jhnwkmn 0:97a4f8cc534c 143 SQInteger size = _memsize;
jhnwkmn 0:97a4f8cc534c 144 this->~SQInstance();
jhnwkmn 0:97a4f8cc534c 145 SQ_FREE(this, size);
jhnwkmn 0:97a4f8cc534c 146 }
jhnwkmn 0:97a4f8cc534c 147 void Finalize();
jhnwkmn 0:97a4f8cc534c 148 #ifndef NO_GARBAGE_COLLECTOR
jhnwkmn 0:97a4f8cc534c 149 void Mark(SQCollectable ** );
jhnwkmn 0:97a4f8cc534c 150 SQObjectType GetType() {return OT_INSTANCE;}
jhnwkmn 0:97a4f8cc534c 151 #endif
jhnwkmn 0:97a4f8cc534c 152 bool InstanceOf(SQClass *trg);
jhnwkmn 0:97a4f8cc534c 153 bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
jhnwkmn 0:97a4f8cc534c 154
jhnwkmn 0:97a4f8cc534c 155 SQClass *_class;
jhnwkmn 0:97a4f8cc534c 156 SQUserPointer _userpointer;
jhnwkmn 0:97a4f8cc534c 157 SQRELEASEHOOK _hook;
jhnwkmn 0:97a4f8cc534c 158 SQInteger _memsize;
jhnwkmn 0:97a4f8cc534c 159 SQObjectPtr _values[1];
jhnwkmn 0:97a4f8cc534c 160 };
jhnwkmn 0:97a4f8cc534c 161
jhnwkmn 0:97a4f8cc534c 162 #endif //_SQCLASS_H_