generate-cpp-class-wrapper.nut 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. function generateCppClassWrapper(klass, klass_name="klass")
  2. {
  3. local dbg = function(...)
  4. {
  5. return;
  6. foreach(k, v in vargv)
  7. {
  8. if(k) stdout.write("\t");
  9. stdout.write(v);
  10. }
  11. stdout.write("\n");
  12. }
  13. local rename_code = function(code)
  14. {
  15. return code.gsub("KLASS", klass_name.toupper()).gsub("klass", klass_name);
  16. }
  17. local print_code = function(code)
  18. {
  19. print(rename_code(code));
  20. }
  21. local get_var_type = function(var_name, to_declare=false)
  22. {
  23. if(var_name.startswith("int_"))
  24. {
  25. return to_declare ? "i" : "INTEGER";
  26. }
  27. if(var_name.startswith("float_"))
  28. {
  29. return to_declare ? "f" : "FLOAT";
  30. }
  31. if(var_name.startswith("str_"))
  32. {
  33. return to_declare ? "s" : "STRING";
  34. }
  35. return to_declare ? "." : "STRING";
  36. }
  37. local members = [];
  38. foreach(k,v in klass)
  39. {
  40. members.push(k);
  41. }
  42. members.sort();
  43. print_code([==[
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. #ifdef USE_KLASS
  48. #include "squirrel.h"
  49. #include <string.h>
  50. #include <stdio.h>
  51. #include <stdlib.h> /* for malloc */
  52. #include <assert.h> /* for a few sanity tests */
  53. #include "klass.h"
  54. static const SQChar SQ_LIBNAME[] = _SC("klass");
  55. SQ_OPT_STRING_STRLEN();
  56. static const SQChar SSL_CTX_Tag[] = _SC("sq_klass_ctx");
  57. #define GET_klass_INSTANCE() SQ_GET_INSTANCE(v, 1, KLASS, KLASS_Tag) \
  58. if(self == NULL) return sq_throwerror(v, _SC("klass object already closed"));
  59. static SQRESULT klass_release_hook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  60. {
  61. KLASS *self = (KLASS*)p;
  62. if(self) klass_free(self);
  63. return 0;
  64. }
  65. static SQRESULT klass_free(HSQUIRRELVM v)
  66. {
  67. SQ_FUNC_VARS_NO_TOP(v);
  68. GET_ssl_INSTANCE();
  69. klass_release_hook(self, 0, v);
  70. sq_setinstanceup(v, 1, 0);
  71. return 0;
  72. }
  73. static SQRESULT klass_constructor(HSQUIRRELVM v, KLASS *klass, int free_on_gc)
  74. {
  75. if(!klass)
  76. return sq_throwerror(v, _SC("Could'nt create an klass object."));
  77. sq_pushstring(v, SQ_LIBNAME, -1);
  78. if(sq_getonroottable(v) == SQ_OK){
  79. sq_pushstring(v, klass_NAME, -1);
  80. if(sq_get(v, -2) == SQ_OK){
  81. if(sq_createinstance(v, -1) == SQ_OK){
  82. sq_setinstanceup(v, -1, ssl);
  83. if(free_on_gc) sq_setreleasehook(v,-1, klass_release_hook);
  84. return 1;
  85. }
  86. }
  87. }
  88. return SQ_ERROR;
  89. }
  90. ]==]);
  91. foreach(k,v in members)
  92. {
  93. dbg(k,v);
  94. v = klass[v];
  95. local v_type = type(v);
  96. if(v_type == "function")
  97. {
  98. local info = v.getinfos();
  99. local ndefparams = info.defparams.len();
  100. print_code(format("static SQRESULT sq_klass_%s(HSQUIRRELVM v){", info.name));
  101. if(ndefparams) print("\tSQ_FUNC_VARS(v);");
  102. else print("\tSQ_FUNC_VARS_NO_TOP(v);");
  103. print_code("\tGET_klass_INSTANCE();\n");
  104. local nparams = info.parameters.len();
  105. if(nparams > 1)
  106. {
  107. local firstDefParam = nparams - ndefparams;
  108. for(local i=1; i < nparams; ++i)
  109. {
  110. local hasDefParam = ndefparams && (i >= firstDefParam);
  111. local vtype = get_var_type(info.parameters[i]);
  112. if(hasDefParam) print(format("\tSQ_OPT_%s(v, %d, %s, %q);", vtype, i+1, info.parameters[i], info.defparams[i - firstDefParam].tostring()));
  113. else print(format("\tSQ_GET_%s(v, %d, %s);", vtype, i+1, info.parameters[i]));
  114. }
  115. }
  116. foreach(k2,v2 in info)
  117. {
  118. dbg("", k2, v2);
  119. if(type(v2) == "array")
  120. {
  121. foreach(k3,v3 in v2)
  122. {
  123. dbg("","", k3, v3);
  124. }
  125. }
  126. }
  127. //local return_type =
  128. local attr = klass.getattributes(info.name);
  129. if(attr && attr.rawin("cfunc")) print("//", attr.rawget("cfunc"));
  130. print("\n\treturn 0;\n}\n");
  131. }
  132. }
  133. print_code( [==[
  134. #define _DECL_KLASS_FUNC(name,nparams,pmask) {_SC(#name),sq_klass_##name,nparams,pmask}
  135. static SQRegFunction klass_obj_funcs[]={
  136. ]==]);
  137. foreach(k,v in members)
  138. {
  139. v = klass[v];
  140. if(type(v) == "function")
  141. {
  142. local info = v.getinfos();
  143. local ndefparams = info.defparams.len();
  144. local nparams = info.parameters.len();
  145. local nreqparams = nparams - ndefparams;
  146. if(ndefparams) nreqparams *= -1;
  147. stdout.write(rename_code(format("\t_DECL_KLASS_FUNC(%s, %d, _SC(\".", info.name, nreqparams)));
  148. for(local i=1; i < nparams; ++i)
  149. {
  150. stdout.write(get_var_type(info.parameters[i], true));
  151. }
  152. stdout.write("\")),\n");
  153. }
  154. }
  155. print_code([==[
  156. {0,0}
  157. };
  158. #undef _DECL_KLASS_FUNC
  159. typedef struct {
  160. const SQChar *Str;
  161. SQInteger Val;
  162. } KeyIntType, * KeyIntPtrType;
  163. static KeyIntType klass_constants[] = {
  164. #define MK_CONST(c) {_SC(#c), c}
  165. //MK_CONST(SSL_SESSION_ID_SIZE),
  166. ]==]);
  167. foreach(k,v in klass)
  168. {
  169. //print(k, v);
  170. local v_type = type(v);
  171. if(v_type == "integer")
  172. {
  173. if(k.startswith("const_"))
  174. {
  175. local const_name = k.replace("const_", "");
  176. print(format("\tMK_CONST(v, %s);", const_name));
  177. }
  178. }
  179. }
  180. print_code([==[
  181. {0,0}
  182. };
  183. /* This defines a function that opens up your library. */
  184. SQRESULT sqext_register_klass (HSQUIRRELVM v) {
  185. //add a namespace klass
  186. sq_pushstring(v, SQ_LIBNAME, -1);
  187. sq_newtable(v);
  188. sq_insert_reg_funcs(v, klass_obj_funcs);
  189. //add constants
  190. KeyIntPtrType KeyIntPtr;
  191. for (KeyIntPtr = klass_constants; KeyIntPtr->Str; KeyIntPtr++) {
  192. sq_pushstring(v, KeyIntPtr->Str, -1); //first the key
  193. sq_pushinteger(v, KeyIntPtr->Val); //then the value
  194. sq_newslot(v, -3, SQFalse); //store then
  195. }
  196. sq_newslot(v,-3,SQFalse); //add klass table to the root table
  197. return SQ_OK;
  198. }
  199. #ifdef __cplusplus
  200. }
  201. #endif //USE_KLASS
  202. #endif
  203. ]==]);
  204. }