Code::Blocks  SVN r11506
SquirrelBindingsUtils.cpp
Go to the documentation of this file.
1 #include "sqplus.h"
2 
3 
4 
5 BOOL_T CreateStaticNamespace(HSQUIRRELVM v,ScriptNamespaceDecl *sn)
6 {
7  SQInteger n = 0;
9  sq_pushstring(v,sn->name,-1);
10  sq_newtable(v);
11  const ScriptClassMemberDecl *members = sn->members;
12  const ScriptClassMemberDecl *m = NULL;
13  while(members[n].name) {
14  m = &members[n];
15  sq_pushstring(v,m->name,-1);
16  sq_newclosure(v,m->func,0);
17  sq_setparamscheck(v,m->params,m->typemask);
18  sq_setnativeclosurename(v,-1,m->name);
19  sq_createslot(v,-3);
20  n++;
21  }
22  const ScriptConstantDecl *consts = sn->constants;
23  const ScriptConstantDecl *c = NULL;
24  n = 0;
25  while(consts[n].name) {
26  c = &consts[n];
27  sq_pushstring(v,c->name,-1);
28  switch(c->type) {
29  case OT_STRING: sq_pushstring(v,c->val.s,-1);break;
30  case OT_INTEGER: sq_pushinteger(v,c->val.i);break;
31  case OT_FLOAT: sq_pushfloat(v,c->val.f);break;
32  // C::B patch: Handle all switch cases
33  default: break;
34  }
35  sq_createslot(v,-3);
36  n++;
37  }
38  if(sn->delegate) {
39  // C::B patch: Fix shadowed variable warning
40  members = sn->delegate;
41  m = NULL;
42  sq_newtable(v);
43  while(members[n].name) {
44  m = &members[n];
45  sq_pushstring(v,m->name,-1);
46  sq_newclosure(v,m->func,0);
47  sq_setparamscheck(v,m->params,m->typemask);
48  sq_setnativeclosurename(v,-1,m->name);
49  sq_createslot(v,-3);
50  n++;
51  }
52  sq_setdelegate(v,-2);
53  }
54  sq_createslot(v,-3);
55  sq_pop(v,1);
56 
57  return TRUE;
58 }
59 
60 BOOL_T CreateClass(HSQUIRRELVM v,SquirrelClassDecl *cd)
61 {
62  SQInteger n = 0;
63  SQInteger oldtop = sq_gettop(v);
65  sq_pushstring(v,cd->name,-1);
66  if(cd->base) {
67  sq_pushstring(v,cd->base,-1);
68  if(SQ_FAILED(sq_get(v,-3))) { // Make sure the base exists if specified by cd->base name.
69  sq_settop(v,oldtop);
70  return FALSE;
71  }
72  }
73  if(SQ_FAILED(sq_newclass(v,cd->base?1:0))) { // Will inherit from base class on stack from sq_get() above.
74  sq_settop(v,oldtop);
75  return FALSE;
76  }
77 // sq_settypetag(v,-1,(unsigned int)cd);
78 // C::B patch: Use pragmas only for MS compilers
79 #if defined(_WIN32) && defined(_MSC_VER)
80 #pragma warning(disable : 4311)
81 #endif
82  sq_settypetag(v,-1,reinterpret_cast<SQUserPointer>(cd));
83  const ScriptClassMemberDecl *members = cd->members;
84  const ScriptClassMemberDecl *m = NULL;
85  if (members) {
86  while(members[n].name) {
87  m = &members[n];
88  sq_pushstring(v,m->name,-1);
89  sq_newclosure(v,m->func,0);
90  sq_setparamscheck(v,m->params,m->typemask);
91  sq_setnativeclosurename(v,-1,m->name);
92  sq_createslot(v,-3);
93  n++;
94  }
95  } // if
96  sq_createslot(v,-3);
97  sq_pop(v,1);
98  return TRUE;
99 }
100 
101 BOOL_T CreateNativeClassInstance(HSQUIRRELVM v,const SQChar *classname,SQUserPointer ud,SQRELEASEHOOK hook)
102 {
103  SQInteger oldtop = sq_gettop(v);
104  sq_pushroottable(v);
105  sq_pushstring(v,classname,-1);
106  if(SQ_FAILED(sq_rawget(v,-2))){ // Get the class (created with sq_newclass()).
107  sq_settop(v,oldtop);
108  return FALSE;
109  }
110  //sq_pushroottable(v);
111  if(SQ_FAILED(sq_createinstance(v,-1))) {
112  sq_settop(v,oldtop);
113  return FALSE;
114  }
115  sq_remove(v,-3); //removes the root table
116  sq_remove(v,-2); //removes the class
117  if(SQ_FAILED(sq_setinstanceup(v,-1,ud))) {
118  sq_settop(v,oldtop);
119  return FALSE;
120  }
121  sq_setreleasehook(v,-1,hook);
122  return TRUE;
123 }
SQRESULT sq_get(HSQUIRRELVM v, SQInteger idx)
Definition: sqapi.cpp:840
SQRESULT sq_newclass(HSQUIRRELVM v, SQBool hasbase)
Definition: sqapi.cpp:239
SQRESULT sq_rawget(HSQUIRRELVM v, SQInteger idx)
Definition: sqapi.cpp:849
BOOL_T CreateNativeClassInstance(HSQUIRRELVM v, const SQChar *classname, SQUserPointer ud, SQRELEASEHOOK hook)
void sq_pop(HSQUIRRELVM v, SQInteger nelemstopop)
Definition: sqapi.cpp:678
void sq_newtable(HSQUIRRELVM v)
Definition: sqapi.cpp:229
void sq_pushroottable(HSQUIRRELVM v)
Definition: sqapi.cpp:437
void sq_pushfloat(HSQUIRRELVM v, SQFloat n)
Definition: sqapi.cpp:212
SQInteger sq_gettop(HSQUIRRELVM v)
Definition: sqapi.cpp:664
void sq_pushinteger(HSQUIRRELVM v, SQInteger n)
Definition: sqapi.cpp:202
void sq_newclosure(HSQUIRRELVM v, SQFUNCTION func, SQUnsignedInteger nfreevars)
Definition: sqapi.cpp:338
SQRESULT sq_setnativeclosurename(HSQUIRRELVM v, SQInteger idx, const SQChar *name)
Definition: sqapi.cpp:362
void sq_settop(HSQUIRRELVM v, SQInteger newtop)
Definition: sqapi.cpp:669
BOOL_T CreateStaticNamespace(HSQUIRRELVM v, ScriptNamespaceDecl *sn)
void sq_remove(HSQUIRRELVM v, SQInteger idx)
Definition: sqapi.cpp:691
SQRESULT sq_setparamscheck(HSQUIRRELVM v, SQInteger nparamscheck, const SQChar *typemask)
Definition: sqapi.cpp:373
void sq_pushstring(HSQUIRRELVM v, const SQChar *s, SQInteger len)
Definition: sqapi.cpp:195
SQRESULT sq_setinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer p)
Definition: sqapi.cpp:629
char SQChar
SQRESULT sq_settypetag(HSQUIRRELVM v, SQInteger idx, SQUserPointer typetag)
Definition: sqapi.cpp:591
BOOL_T CreateClass(HSQUIRRELVM v, SquirrelClassDecl *cd)
SQRESULT sq_setdelegate(HSQUIRRELVM v, SQInteger idx)
Definition: sqapi.cpp:776
void sq_setreleasehook(HSQUIRRELVM v, SQInteger idx, SQRELEASEHOOK hook)
Definition: sqapi.cpp:999
#define NULL
Definition: prefix.cpp:59
SQRESULT sq_createinstance(HSQUIRRELVM v, SQInteger idx)
Definition: sqapi.cpp:1158