class.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <assert.h>
  6. /**
  7. * PHP includes
  8. */
  9. #include <php.h>
  10. #include <zend_exceptions.h>
  11. #include <zend_interfaces.h>
  12. #include <zend_ini.h>
  13. #include <SAPI.h>
  14. #include <zend_hash.h>
  15. #include <zend_API.h>
  16. #ifdef ZTS
  17. #include "TSRM.h"
  18. #endif
  19. #include "_cgo_export.h"
  20. // #include "sztypes.h"
  21. #include "../zend/goapi.h"
  22. #include "../zend/clog.h"
  23. #include "uthash.h"
  24. #include "objectmap.h"
  25. #include "class.h"
  26. struct _phpgo_class_entry {
  27. zend_class_entry *ce;
  28. // phpgo_function_entry **fes;
  29. const char *class_name;
  30. phpgo_object_map *fmap;
  31. zend_function_entry *fes;
  32. };
  33. phpgo_class_entry* phpgo_class_new(const char *class_name) {
  34. phpgo_class_entry* pce = (phpgo_class_entry*)calloc(1, sizeof(phpgo_class_entry));
  35. memset(pce, 0, sizeof(phpgo_class_entry));
  36. pce->ce = (zend_class_entry*)calloc(1, sizeof(zend_class_entry));
  37. pce->class_name = class_name;
  38. pce->fmap = NULL;
  39. return pce;
  40. }
  41. void phpgo_class_method_add(phpgo_class_entry* pce, const char *func_name) {
  42. phpgo_function_entry* pfe = phpgo_function_new(func_name);
  43. phpgo_object_map_add(&pce->fmap, func_name, pfe);
  44. int count = phpgo_object_map_count(pce->fmap);
  45. if (count == 1) {
  46. pce->fes = (zend_function_entry*)calloc(count + 1, sizeof(zend_function_entry));
  47. } else {
  48. pce->fes = (zend_function_entry*)realloc(pce->fes, (count + 1) * sizeof(zend_function_entry));
  49. }
  50. memset(&pce->fes[count], 0, sizeof(zend_function_entry));
  51. memcpy(&pce->fes[count-1], phpgo_function_get(pfe), sizeof(zend_function_entry));
  52. pce->ce->info.internal.builtin_functions = pce->fes;
  53. }
  54. zend_class_entry* phpgo_class_get(phpgo_class_entry* pce) {
  55. return pce->ce;
  56. }
  57. zend_function_entry* phpgo_class_get_funcs(phpgo_class_entry* pce) {
  58. return (zend_function_entry*)pce->ce->info.internal.builtin_functions;
  59. }
  60. phpgo_function_entry* phpgo_class_method_get(phpgo_class_entry* pce, const char *func_name) {
  61. phpgo_function_entry* pfe = (phpgo_function_entry*)phpgo_object_map_get(pce->fmap, func_name);
  62. if (pfe != NULL) {
  63. return pfe;
  64. }
  65. return NULL;
  66. }
  67. int phpgo_class_method_count(phpgo_class_entry* pce) {
  68. return phpgo_object_map_count(pce->fmap);
  69. }
  70. // function operations
  71. struct _phpgo_function_entry {
  72. const char *func_name;
  73. zend_function_entry *fe;
  74. };
  75. #ifdef ZEND_ENGINE_3
  76. extern void phpgo_function_handler(zend_execute_data *execute_data, zval *return_value);
  77. #else
  78. extern void phpgo_function_handler(int ht, zval *return_value, zval **return_value_ptr,
  79. zval *this_ptr, int return_value_used TSRMLS_DC);
  80. #endif
  81. phpgo_function_entry *phpgo_function_new(const char *func_name) {
  82. phpgo_function_entry *pce = (phpgo_function_entry*)calloc(1, sizeof(phpgo_function_entry));
  83. pce->func_name = func_name;
  84. pce->fe = (zend_function_entry*)calloc(1, sizeof(zend_function_entry));
  85. zend_function_entry *fe = pce->fe;
  86. zend_function_entry e = {strdup(func_name), phpgo_function_handler, NULL, 0, 0};
  87. memcpy(fe, &e, sizeof(e));
  88. return pce;
  89. }
  90. int phpgo_function_delete(phpgo_function_entry *pfe) {
  91. free((void*)(pfe->fe->fname));
  92. free(pfe->fe);
  93. free(pfe);
  94. return 0;
  95. }
  96. zend_function_entry* phpgo_function_get(phpgo_function_entry* pfe) {
  97. return pfe->fe;
  98. }
  99. //
  100. #define MAX_ARG_NUM 10
  101. struct _phpgo_callback_info {
  102. char arg_types[MAX_ARG_NUM];
  103. int ret_type;
  104. int varidict;
  105. };
  106. phpgo_callback_info* phpgo_callback_info_new(char *arg_types, int ret_type) {
  107. phpgo_callback_info* cbi = (phpgo_callback_info*)calloc(1, sizeof(phpgo_callback_info));
  108. strncpy(cbi->arg_types, arg_types, sizeof(cbi->arg_types));
  109. cbi->ret_type = ret_type;
  110. return cbi;
  111. }
  112. char* phpgo_callback_info_get_arg_types(phpgo_callback_info* cbi) {
  113. return cbi->arg_types;
  114. }
  115. int phpgo_callback_info_get_ret_type(phpgo_callback_info* cbi) {
  116. return cbi->ret_type;
  117. }