dad_utils.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "squirrel.h"
  2. #include <stdio.h>
  3. typedef struct {
  4. HSQOBJECT func_to_call;
  5. HSQOBJECT param;
  6. } gc_scope_alert_st;
  7. static const SQChar *gc_scope_alert_TAG = _SC("gc_scope_alert");
  8. static SQRESULT gc_scope_alert_releasehook(SQUserPointer p, SQInteger size, HSQUIRRELVM v)
  9. {
  10. gc_scope_alert_st *self = ((gc_scope_alert_st *)p);
  11. //printf("%p %p\n", p, v);
  12. if(self){
  13. if(v){
  14. SQInteger top = sq_gettop(v);
  15. sq_reservestack(v, 20);
  16. sq_pushobject(v, self->func_to_call);
  17. sq_pushroottable(v);
  18. sq_pushobject(v, self->param);
  19. sq_call(v, 2, SQFalse, SQTrue);
  20. sq_release(v, &self->func_to_call);
  21. sq_release(v, &self->param);
  22. sq_settop(v, top);
  23. }
  24. //else
  25. sq_free(self, sizeof(gc_scope_alert_st));
  26. }
  27. return 0;
  28. }
  29. static SQRESULT gc_scope_alert_constructor(HSQUIRRELVM v)
  30. {
  31. SQInteger rc;
  32. gc_scope_alert_st proto;
  33. sq_resetobject(&proto.func_to_call);
  34. sq_resetobject(&proto.param);
  35. if((rc = sq_getstackobj(v, 2, &proto.func_to_call)) < 0) return rc;
  36. if(sq_gettop(v) > 2){
  37. if((rc = sq_getstackobj(v, 3, &proto.param)) < 0) return rc;
  38. }
  39. gc_scope_alert_st *self = (gc_scope_alert_st*)sq_malloc(sizeof(gc_scope_alert_st));
  40. if(!self) return sq_throwerror(v, "Failed to create %s", gc_scope_alert_TAG);
  41. sq_addref(v, &proto.func_to_call);
  42. sq_addref(v, &proto.param);
  43. *self = proto;
  44. sq_setinstanceup(v, 1, self);
  45. sq_setreleasehook(v,1, gc_scope_alert_releasehook);
  46. return 1;
  47. }
  48. static SQRESULT spectralnorm_A(HSQUIRRELVM v)
  49. {
  50. SQ_FUNC_VARS_NO_TOP();
  51. SQ_GET_INTEGER(v, 2, i);
  52. SQ_GET_INTEGER(v, 3, j);
  53. SQInteger ij = j + i++;
  54. sq_pushfloat(v, 1.0/(ij * (ij+1)/2.0+i));
  55. return 1;
  56. }
  57. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), gc_scope_alert_##name,nparams,tycheck}
  58. static SQRegFunction gc_scope_alert_methods[] =
  59. {
  60. _DECL_FUNC(constructor, -2, _SC("xc.")),
  61. {0,0}
  62. };
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. SQRESULT sqext_register_dad_utils(HSQUIRRELVM v)
  67. {
  68. sq_insertfunc(v, _SC("spectralnorm_A"), spectralnorm_A, 3, _SC(".ii"), true);
  69. sq_pushstring(v,_SC("dad_utils"),-1);
  70. sq_newtable(v);
  71. sq_pushstring(v,gc_scope_alert_TAG,-1);
  72. sq_newclass(v,SQFalse);
  73. sq_settypetag(v,-1,(void*)gc_scope_alert_TAG);
  74. sq_insert_reg_funcs(v, gc_scope_alert_methods);
  75. sq_rawset(v,-3);
  76. sq_rawset(v,-3);//insert dad_utils
  77. return 1;
  78. }
  79. #ifdef __cplusplus
  80. }
  81. #endif