Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

  1. #include "sqplus.h"
  2. #if defined(VSCRIPT_DLL_EXPORT) || defined(VSQUIRREL_TEST)
  3. #include "memdbgon.h"
  4. #endif
  5. BOOL CreateStaticNamespace(HSQUIRRELVM v,ScriptNamespaceDecl *sn)
  6. {
  7. int n = 0;
  8. sq_pushroottable(v);
  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. }
  33. sq_createslot(v,-3);
  34. n++;
  35. }
  36. if(sn->delegate) {
  37. const ScriptClassMemberDecl *members = sn->delegate;
  38. const ScriptClassMemberDecl *m = NULL;
  39. sq_newtable(v);
  40. while(members[n].name) {
  41. m = &members[n];
  42. sq_pushstring(v,m->name,-1);
  43. sq_newclosure(v,m->func,0);
  44. sq_setparamscheck(v,m->params,m->typemask);
  45. sq_setnativeclosurename(v,-1,m->name);
  46. sq_createslot(v,-3);
  47. n++;
  48. }
  49. sq_setdelegate(v,-2);
  50. }
  51. sq_createslot(v,-3);
  52. sq_pop(v,1);
  53. return TRUE;
  54. }
  55. BOOL CreateClass(HSQUIRRELVM v,SquirrelClassDecl *cd)
  56. {
  57. int n = 0;
  58. int oldtop = sq_gettop(v);
  59. sq_pushroottable(v);
  60. sq_pushstring(v,cd->name,-1);
  61. if(cd->base) {
  62. sq_pushstring(v,cd->base,-1);
  63. if(SQ_FAILED(sq_get(v,-3))) { // Make sure the base exists if specified by cd->base name.
  64. sq_settop(v,oldtop);
  65. return FALSE;
  66. }
  67. }
  68. if(SQ_FAILED(sq_newclass(v,cd->base?1:0))) { // Will inherit from base class on stack from sq_get() above.
  69. sq_settop(v,oldtop);
  70. return FALSE;
  71. }
  72. // sq_settypetag(v,-1,(unsigned int)cd);
  73. #ifdef _WIN32
  74. #pragma warning(disable : 4311)
  75. #endif
  76. sq_settypetag(v,-1,reinterpret_cast<SQUserPointer>(cd));
  77. const ScriptClassMemberDecl *members = cd->members;
  78. const ScriptClassMemberDecl *m = NULL;
  79. if (members) {
  80. while(members[n].name) {
  81. m = &members[n];
  82. sq_pushstring(v,m->name,-1);
  83. sq_newclosure(v,m->func,0);
  84. sq_setparamscheck(v,m->params,m->typemask);
  85. sq_setnativeclosurename(v,-1,m->name);
  86. sq_createslot(v,-3);
  87. n++;
  88. }
  89. } // if
  90. sq_createslot(v,-3);
  91. sq_pop(v,1);
  92. return TRUE;
  93. }
  94. BOOL CreateNativeClassInstance(HSQUIRRELVM v,const SQChar *classname,SQUserPointer ud,SQRELEASEHOOK hook)
  95. {
  96. int oldtop = sq_gettop(v);
  97. sq_pushroottable(v);
  98. sq_pushstring(v,classname,-1);
  99. if(SQ_FAILED(sq_rawget(v,-2))){ // Get the class (created with sq_newclass()).
  100. sq_settop(v,oldtop);
  101. return FALSE;
  102. }
  103. //sq_pushroottable(v);
  104. if(SQ_FAILED(sq_createinstance(v,-1))) {
  105. sq_settop(v,oldtop);
  106. return FALSE;
  107. }
  108. sq_remove(v,-3); //removes the root table
  109. sq_remove(v,-2); //removes the class
  110. if(SQ_FAILED(sq_setinstanceup(v,-1,ud))) {
  111. sq_settop(v,oldtop);
  112. return FALSE;
  113. }
  114. sq_setreleasehook(v,-1,hook);
  115. return TRUE;
  116. }