# sqstate.h

    00001 /*  see copyright notice in squirrel.h */
    00002 #ifndef _SQSTATE_H_
    00003 #define _SQSTATE_H_
    00004
    00005 #include "squtils.h"
    00006 #include "sqobject.h"
    00007 struct SQString;
    00008 struct SQTable;
    00009 //max number of character for a printed number
    00010 #define NUMBER_MAX_CHAR 50
    00011
    00012 struct StringTable
    00013 {
    00014   StringTable();
    00015   ~StringTable();
    00016   SQString *Add(const SQChar *,SQInteger len);
    00017   void Remove(SQString *);
    00018 private:
    00019   void Resize(SQInteger size);
    00020   void AllocNodes(SQInteger size);
    00021   SQString **_strings;
    00022   SQUnsignedInteger _numofslots;
    00023   SQUnsignedInteger _slotused;
    00024 };
    00025
    00026 struct RefTable {
    00027   struct RefNode {
    00028     SQObjectPtr obj;
    00029     SQUnsignedInteger refs;
    00030     struct RefNode *next;
    00031   };
    00032   RefTable();
    00033   ~RefTable();
    00034   void AddRef(SQObject &obj);
    00035   SQBool Release(SQObject &obj);
    00036 #ifndef NO_GARBAGE_COLLECTOR
    00037   void Mark(SQCollectable **chain);
    00038 #endif
    00039   void Finalize();
    00040 private:
    00041   RefNode *Get(SQObject &obj,SQHash &mainpos,RefNode **prev,bool add);
    00042   RefNode *Add(SQHash mainpos,SQObject &obj);
    00043   void Resize(SQUnsignedInteger size);
    00044   void AllocNodes(SQUnsignedInteger size);
    00045   SQUnsignedInteger _numofslots;
    00046   SQUnsignedInteger _slotused;
    00047   RefNode *_nodes;
    00048   RefNode *_freelist;
    00049   RefNode **_buckets;
    00050 };
    00051
    00052 #define ADD_STRING(ss,str,len) ss->_stringtable->Add(str,len)
    00053 #define REMOVE_STRING(ss,bstr) ss->_stringtable->Remove(bstr)
    00054
    00055 struct SQObjectPtr;
    00056
    00057 struct SQSharedState
    00058 {
    00059   SQSharedState();
    00060   ~SQSharedState();
    00061   void Init();
    00062 public:
    00063   SQChar* GetScratchPad(SQInteger size);
    00064   SQInteger GetMetaMethodIdxByName(const SQObjectPtr &name);
    00065 #ifndef NO_GARBAGE_COLLECTOR
    00066   SQInteger CollectGarbage(SQVM *vm);
    00067   static void MarkObject(SQObjectPtr &o,SQCollectable **chain);
    00068 #endif
    00069   SQObjectPtrVec *_metamethods;
    00070   SQObjectPtr _metamethodsmap;
    00071   SQObjectPtrVec *_systemstrings;
    00072   SQObjectPtrVec *_types;
    00073   StringTable *_stringtable;
    00074   RefTable _refs_table;
    00075   SQObjectPtr _registry;
    00076   SQObjectPtr _consts;
    00077   SQObjectPtr _constructoridx;
    00078 #ifndef NO_GARBAGE_COLLECTOR
    00079   SQCollectable *_gc_chain;
    00080 #endif
    00081   SQObjectPtr _root_vm;
    00082   SQObjectPtr _table_default_delegate;
    00083   static SQRegFunction _table_default_delegate_funcz[];
    00084   SQObjectPtr _array_default_delegate;
    00085   static SQRegFunction _array_default_delegate_funcz[];
    00086   SQObjectPtr _string_default_delegate;
    00087   static SQRegFunction _string_default_delegate_funcz[];
    00088   SQObjectPtr _number_default_delegate;
    00089   static SQRegFunction _number_default_delegate_funcz[];
    00090   SQObjectPtr _generator_default_delegate;
    00091   static SQRegFunction _generator_default_delegate_funcz[];
    00092   SQObjectPtr _closure_default_delegate;
    00093   static SQRegFunction _closure_default_delegate_funcz[];
    00094   SQObjectPtr _thread_default_delegate;
    00095   static SQRegFunction _thread_default_delegate_funcz[];
    00096   SQObjectPtr _class_default_delegate;
    00097   static SQRegFunction _class_default_delegate_funcz[];
    00098   SQObjectPtr _instance_default_delegate;
    00099   static SQRegFunction _instance_default_delegate_funcz[];
    00100   SQObjectPtr _weakref_default_delegate;
    00101   static SQRegFunction _weakref_default_delegate_funcz[];
    00102
    00103   SQCOMPILERERROR _compilererrorhandler;
    00104   SQPRINTFUNCTION _printfunc;
    00105   bool _debuginfo;
    00106   bool _notifyallexceptions;
    00107 private:
    00108   SQChar *_scratchpad;
    00109   SQInteger _scratchpadsize;
    00110 };
    00111
    00112 #define _sp(s) (_sharedstate->GetScratchPad(s))
    00113 #define _spval (_sharedstate->GetScratchPad(-1))
    00114
    00115 #define _table_ddel   _table(_sharedstate->_table_default_delegate)
    00116 #define _array_ddel   _table(_sharedstate->_array_default_delegate)
    00117 #define _string_ddel  _table(_sharedstate->_string_default_delegate)
    00118 #define _number_ddel  _table(_sharedstate->_number_default_delegate)
    00119 #define _generator_ddel _table(_sharedstate->_generator_default_delegate)
    00120 #define _closure_ddel _table(_sharedstate->_closure_default_delegate)
    00121 #define _thread_ddel  _table(_sharedstate->_thread_default_delegate)
    00122 #define _class_ddel   _table(_sharedstate->_class_default_delegate)
    00123 #define _instance_ddel  _table(_sharedstate->_instance_default_delegate)
    00124 #define _weakref_ddel _table(_sharedstate->_weakref_default_delegate)
    00125
    00126 #ifdef SQUNICODE //rsl REAL STRING LEN
    00127 #define rsl(l) ((l)<<1)
    00128 #else
    00129 #define rsl(l) (l)
    00130 #endif
    00131
    00132 extern SQObjectPtr _null_;
    00133 extern SQObjectPtr _true_;
    00134 extern SQObjectPtr _false_;
    00135 extern SQObjectPtr _one_;
    00136 extern SQObjectPtr _minusone_;
    00137
    00138 bool CompileTypemask(SQIntVec &res,const SQChar *typemask);
    00139
    00140 void *sq_vm_malloc(SQUnsignedInteger size);
    00141 void *sq_vm_realloc(void *p,SQUnsignedInteger oldsize,SQUnsignedInteger size);
    00142 void sq_vm_free(void *p,SQUnsignedInteger size);
    00143 #endif //_SQSTATE_H_