2 see copyright notice in squirrel.h
\r
4 #include "sqpcheader.h"
\r
5 #include "sqopcodes.h"
\r
7 #include "sqfuncproto.h"
\r
8 #include "sqclosure.h"
\r
9 #include "sqstring.h"
\r
10 #include "sqtable.h"
\r
11 #include "sqarray.h"
\r
12 #include "squserdata.h"
\r
13 #include "sqclass.h"
\r
16 SQObjectPtr _true_(true);
\r
17 SQObjectPtr _false_(false);
\r
18 SQObjectPtr _one_(1);
\r
19 SQObjectPtr _minusone_(-1);
\r
21 SQSharedState::SQSharedState()
\r
23 _compilererrorhandler = NULL;
\r
28 #define newsysstring(s) { \
\r
29 _systemstrings->push_back(SQString::Create(this,s)); \
\r
32 #define newmetamethod(s) { \
\r
33 _metamethods->push_back(SQString::Create(this,s)); \
\r
34 _table(_metamethodsmap)->NewSlot(_metamethods->back(),(SQInteger)(_metamethods->size()-1)); \
\r
37 bool CompileTypemask(SQIntVec &res,const SQChar *typemask)
\r
42 while(typemask[i] != 0) {
\r
44 switch(typemask[i]){
\r
45 case 'i': mask |= _RT_INTEGER; break;
\r
46 case 'f': mask |= _RT_FLOAT; break;
\r
47 case 'n': mask |= (_RT_FLOAT | _RT_INTEGER); break;
\r
48 case 's': mask |= _RT_STRING; break;
\r
49 case 't': mask |= _RT_TABLE; break;
\r
50 case 'a': mask |= _RT_ARRAY; break;
\r
51 case 'u': mask |= _RT_USERDATA; break;
\r
52 case 'c': mask |= (_RT_CLOSURE | _RT_NATIVECLOSURE); break;
\r
53 case 'b': mask |= _RT_BOOL; break;
\r
54 case 'g': mask |= _RT_GENERATOR; break;
\r
55 case 'p': mask |= _RT_USERPOINTER; break;
\r
56 case 'v': mask |= _RT_THREAD; break;
\r
57 case 'x': mask |= _RT_INSTANCE; break;
\r
58 case 'y': mask |= _RT_CLASS; break;
\r
59 case '.': mask = -1; res.push_back(mask); i++; mask = 0; continue;
\r
60 case ' ': i++; continue; //ignores spaces
\r
65 if(typemask[i] == '|') {
\r
67 if(typemask[i] == 0)
\r
71 res.push_back(mask);
\r
78 SQTable *CreateDefaultDelegate(SQSharedState *ss,SQRegFunction *funcz)
\r
81 SQTable *t=SQTable::Create(ss,0);
\r
82 while(funcz[i].name!=0){
\r
83 SQNativeClosure *nc = SQNativeClosure::Create(ss,funcz[i].f);
\r
84 nc->_nparamscheck = funcz[i].nparamscheck;
\r
85 nc->_name = SQString::Create(ss,funcz[i].name);
\r
86 if(funcz[i].typemask && !CompileTypemask(nc->_typecheck,funcz[i].typemask))
\r
88 t->NewSlot(SQString::Create(ss,funcz[i].name),nc);
\r
94 void SQSharedState::Init()
\r
98 #ifndef NO_GARBAGE_COLLECTOR
\r
101 sq_new(_stringtable,StringTable);
\r
102 sq_new(_metamethods,SQObjectPtrVec);
\r
103 sq_new(_systemstrings,SQObjectPtrVec);
\r
104 sq_new(_types,SQObjectPtrVec);
\r
105 _metamethodsmap = SQTable::Create(this,MT_LAST-1);
\r
106 //adding type strings to avoid memory trashing
\r
108 newsysstring(_SC("null"));
\r
109 newsysstring(_SC("table"));
\r
110 newsysstring(_SC("array"));
\r
111 newsysstring(_SC("closure"));
\r
112 newsysstring(_SC("string"));
\r
113 newsysstring(_SC("userdata"));
\r
114 newsysstring(_SC("integer"));
\r
115 newsysstring(_SC("float"));
\r
116 newsysstring(_SC("userpointer"));
\r
117 newsysstring(_SC("function"));
\r
118 newsysstring(_SC("generator"));
\r
119 newsysstring(_SC("thread"));
\r
120 newsysstring(_SC("class"));
\r
121 newsysstring(_SC("instance"));
\r
122 newsysstring(_SC("bool"));
\r
124 newmetamethod(MM_ADD);
\r
125 newmetamethod(MM_SUB);
\r
126 newmetamethod(MM_MUL);
\r
127 newmetamethod(MM_DIV);
\r
128 newmetamethod(MM_UNM);
\r
129 newmetamethod(MM_MODULO);
\r
130 newmetamethod(MM_SET);
\r
131 newmetamethod(MM_GET);
\r
132 newmetamethod(MM_TYPEOF);
\r
133 newmetamethod(MM_NEXTI);
\r
134 newmetamethod(MM_CMP);
\r
135 newmetamethod(MM_CALL);
\r
136 newmetamethod(MM_CLONED);
\r
137 newmetamethod(MM_NEWSLOT);
\r
138 newmetamethod(MM_DELSLOT);
\r
140 _constructoridx = SQString::Create(this,_SC("constructor"));
\r
141 _refs_table = SQTable::Create(this,0);
\r
142 _registry = SQTable::Create(this,0);
\r
143 _table_default_delegate=CreateDefaultDelegate(this,_table_default_delegate_funcz);
\r
144 _array_default_delegate=CreateDefaultDelegate(this,_array_default_delegate_funcz);
\r
145 _string_default_delegate=CreateDefaultDelegate(this,_string_default_delegate_funcz);
\r
146 _number_default_delegate=CreateDefaultDelegate(this,_number_default_delegate_funcz);
\r
147 _closure_default_delegate=CreateDefaultDelegate(this,_closure_default_delegate_funcz);
\r
148 _generator_default_delegate=CreateDefaultDelegate(this,_generator_default_delegate_funcz);
\r
149 _thread_default_delegate=CreateDefaultDelegate(this,_thread_default_delegate_funcz);
\r
150 _class_default_delegate=CreateDefaultDelegate(this,_class_default_delegate_funcz);
\r
151 _instance_default_delegate=CreateDefaultDelegate(this,_instance_default_delegate_funcz);
\r
155 SQSharedState::~SQSharedState()
\r
157 _constructoridx = _null_;
\r
158 _table(_refs_table)->Finalize();
\r
159 _table(_registry)->Finalize();
\r
160 _table(_metamethodsmap)->Finalize();
\r
161 _refs_table = _null_;
\r
162 _registry = _null_;
\r
163 _metamethodsmap = _null_;
\r
164 while(!_systemstrings->empty()){
\r
165 _systemstrings->back()=_null_;
\r
166 _systemstrings->pop_back();
\r
168 _thread(_root_vm)->Finalize();
\r
170 _table_default_delegate=_null_;
\r
171 _array_default_delegate=_null_;
\r
172 _string_default_delegate=_null_;
\r
173 _number_default_delegate=_null_;
\r
174 _closure_default_delegate=_null_;
\r
175 _generator_default_delegate=_null_;
\r
176 _thread_default_delegate=_null_;
\r
177 _class_default_delegate=_null_;
\r
178 _instance_default_delegate=_null_;
\r
180 #ifndef NO_GARBAGE_COLLECTOR
\r
183 SQCollectable *t=_gc_chain;
\r
184 SQCollectable *nx=NULL;
\r
193 assert(_gc_chain==NULL); //just to proove a theory
\r
195 _gc_chain->_uiRef++;
\r
196 _gc_chain->Release();
\r
199 sq_delete(_types,SQObjectPtrVec);
\r
200 sq_delete(_systemstrings,SQObjectPtrVec);
\r
201 sq_delete(_metamethods,SQObjectPtrVec);
\r
202 sq_delete(_stringtable,StringTable);
\r
203 if(_scratchpad)SQ_FREE(_scratchpad,_scratchpadsize);
\r
207 SQInteger SQSharedState::GetMetaMethodIdxByName(const SQObjectPtr &name)
\r
209 if(type(name) != OT_STRING)
\r
212 if(_table(_metamethodsmap)->Get(name,ret)) {
\r
213 return _integer(ret);
\r
218 #ifndef NO_GARBAGE_COLLECTOR
\r
220 void SQSharedState::MarkObject(SQObjectPtr &o,SQCollectable **chain)
\r
223 case OT_TABLE:_table(o)->Mark(chain);break;
\r
224 case OT_ARRAY:_array(o)->Mark(chain);break;
\r
225 case OT_USERDATA:_userdata(o)->Mark(chain);break;
\r
226 case OT_CLOSURE:_closure(o)->Mark(chain);break;
\r
227 case OT_NATIVECLOSURE:_nativeclosure(o)->Mark(chain);break;
\r
228 case OT_GENERATOR:_generator(o)->Mark(chain);break;
\r
229 case OT_THREAD:_thread(o)->Mark(chain);break;
\r
230 case OT_CLASS:_class(o)->Mark(chain);break;
\r
231 case OT_INSTANCE:_instance(o)->Mark(chain);break;
\r
236 int SQSharedState::CollectGarbage(SQVM *vm)
\r
239 SQCollectable *tchain=NULL;
\r
240 SQVM *vms=_thread(_root_vm);
\r
242 vms->Mark(&tchain);
\r
243 int x = _table(_thread(_root_vm)->_roottable)->CountUsed();
\r
244 MarkObject(_refs_table,&tchain);
\r
245 MarkObject(_registry,&tchain);
\r
246 MarkObject(_metamethodsmap,&tchain);
\r
247 MarkObject(_table_default_delegate,&tchain);
\r
248 MarkObject(_array_default_delegate,&tchain);
\r
249 MarkObject(_string_default_delegate,&tchain);
\r
250 MarkObject(_number_default_delegate,&tchain);
\r
251 MarkObject(_generator_default_delegate,&tchain);
\r
252 MarkObject(_thread_default_delegate,&tchain);
\r
253 MarkObject(_closure_default_delegate,&tchain);
\r
254 MarkObject(_class_default_delegate,&tchain);
\r
255 MarkObject(_instance_default_delegate,&tchain);
\r
257 SQCollectable *t=_gc_chain;
\r
258 SQCollectable *nx=NULL;
\r
275 int z = _table(_thread(_root_vm)->_roottable)->CountUsed();
\r
281 #ifndef NO_GARBAGE_COLLECTOR
\r
282 void SQCollectable::AddToChain(SQCollectable **chain,SQCollectable *c)
\r
286 if(*chain) (*chain)->_prev=c;
\r
290 void SQCollectable::RemoveFromChain(SQCollectable **chain,SQCollectable *c)
\r
292 if(c->_prev) c->_prev->_next=c->_next;
\r
293 else *chain=c->_next;
\r
295 c->_next->_prev=c->_prev;
\r
301 SQChar* SQSharedState::GetScratchPad(int size)
\r
305 if(_scratchpadsize<size){
\r
306 newsize=size+(size>>1);
\r
307 _scratchpad=(SQChar *)SQ_REALLOC(_scratchpad,_scratchpadsize,newsize);
\r
308 _scratchpadsize=newsize;
\r
310 }else if(_scratchpadsize>=(size<<5)){
\r
311 newsize=_scratchpadsize>>1;
\r
312 _scratchpad=(SQChar *)SQ_REALLOC(_scratchpad,_scratchpadsize,newsize);
\r
313 _scratchpadsize=newsize;
\r
316 return _scratchpad;
\r
319 //////////////////////////////////////////////////////////////////////////
\r
322 * The following code is based on Lua 4.0 (Copyright 1994-2002 Tecgraf, PUC-Rio.)
\r
323 * http://www.lua.org/copyright.html#4
\r
324 * http://www.lua.org/source/4.0.1/src_lstring.c.html
\r
327 int SQString::Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
\r
329 int idx = (int)TranslateIndex(refpos);
\r
331 outkey = (SQInteger)idx;
\r
332 outval = SQInteger(_val[idx]);
\r
333 //return idx for the next iteration
\r
336 //nothing to iterate anymore
\r
340 StringTable::StringTable()
\r
346 StringTable::~StringTable()
\r
348 SQ_FREE(_strings,sizeof(SQString*)*_numofslots);
\r
352 void StringTable::AllocNodes(int size)
\r
356 _strings=(SQString**)SQ_MALLOC(sizeof(SQString*)*_numofslots);
\r
357 memset(_strings,0,sizeof(SQString*)*_numofslots);
\r
360 SQString *StringTable::Add(const SQChar *news,int len)
\r
363 len=scstrlen(news);
\r
364 unsigned int h=::_hashstr(news,len)&(_numofslots-1);
\r
366 for (s = _strings[h]; s; s = s->_next){
\r
367 if(s->_len == len && (!memcmp(news,s->_val,rsl(len))))
\r
371 SQString *t=(SQString *)SQ_MALLOC(rsl(len)+sizeof(SQString));
\r
373 memcpy(t->_val,news,rsl(len));
\r
374 t->_val[len]=_SC('\0');
\r
376 t->_hash=::_hashstr(news,len);
\r
377 t->_next=_strings[h];
\r
381 if (_slotused > _numofslots) /* too crowded? */
\r
382 Resize(_numofslots*2);
\r
386 void StringTable::Resize(int size)
\r
388 int oldsize=_numofslots;
\r
389 SQString **oldtable=_strings;
\r
391 for (int i=0; i<oldsize; i++){
\r
392 SQString *p = oldtable[i];
\r
394 SQString *next = p->_next;
\r
395 unsigned int h=p->_hash&(_numofslots-1);
\r
396 p->_next=_strings[h];
\r
401 SQ_FREE(oldtable,oldsize*sizeof(SQString*));
\r
404 void StringTable::Remove(SQString *bs)
\r
407 SQString *prev=NULL;
\r
408 unsigned int h=bs->_hash&(_numofslots-1);
\r
410 for (s = _strings[h]; s; ){
\r
413 prev->_next = s->_next;
\r
415 _strings[h] = s->_next;
\r
419 SQ_FREE(s,sizeof(SQString)+rsl(slen));
\r
425 assert(0);//if this fail something is wrong
\r