generate-cpp-class-wrapper.nut 5.4 KB

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