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