summaryrefslogtreecommitdiff
path: root/ext/rpc/rpc.h
blob: 727dbf782a1434ab7c29c79f3c5a789ee357403c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
   +----------------------------------------------------------------------+
   | PHP Version 4                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2003 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.0 of the PHP license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available through the world-wide-web at the following url:           |
   | http://www.php.net/license/3_0.txt.                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: Harald Radi <h.radi@nme.at>                                  |
   +----------------------------------------------------------------------+
 */

#ifndef RPC_H
#define RPC_H

#define RPC_HT(intern) (*((intern)->handlers))
#define RPC_CLASS(intern) ((intern)->hash)

#define GET_INTERNAL(intern)	rpc_internal *intern;								\
								if (GET_INTERNAL_EX(intern, object) != SUCCESS) {	\
									/* TODO: exception */							\
								}

#define GET_INTERNAL_EX(intern, object)	(((intern = zend_object_store_get_object(object TSRMLS_CC)) == NULL) ? FAILURE : SUCCESS)

#define GET_CLASS(ce)	char *key;																				\
						int key_len;																			\
						zend_class_entry **ce;																	\
																												\
						/* the name of the rpc layer is prepended to '_load' so lets strip everything after		\
						 * the first '_' away from the function name											\
						 */																						\
						key = estrdup(get_active_function_name(TSRMLS_C));										\
						key_len = strchr(key, '_') - key;														\
						key[key_len] = '\0';																	\
																												\
						/* get the class entry for the requested rpc layer */									\
						if (zend_hash_find(CG(class_table), key, key_len + 1, (void **) &ce) != SUCCESS) {		\
							efree(key);																			\
							/* TODO: exception here */															\
						} else {																				\
							efree(key);																			\
						}

#define GET_ARGS_EX(num_args, args, args_free, strip)										\
						GET_ARGS(num_args, args)											\
																							\
						args_free = args;													\
																							\
						/* strip away the first parameters */								\
						num_args -= strip;													\
						args = (num_args > 0) ? &args[strip] : NULL;

#define GET_ARGS(num_args, args)															\
						args = (zval ***) emalloc(sizeof(zval **) * num_args);				\
																							\
						if (zend_get_parameters_array_ex(num_args, args) != SUCCESS) {		\
							efree(args);													\
							/* TODO: exception */											\
						}

#define GET_METHOD_SIGNATURE(intern, method, hash_val, num_args, arg_types)											\
			GET_SIGNATURE(intern, method->str, method->len, hash_val, num_args, arg_types)

#define GET_SIGNATURE(intern, name, name_len, hash_val, num_args, arg_types)										\
						hash_val.len = name_len;																	\
																													\
						if ((*intern->handlers)->hash_type & HASH_WITH_SIGNATURE) {								\
							zend_uint _signature_counter;															\
																													\
							arg_types = (char *) emalloc(sizeof(char) * (num_args + 1));							\
							hash_val.len += num_args + 1;															\
																													\
							for (_signature_counter = 0; _signature_counter < num_args; _signature_counter++) {		\
								switch (Z_TYPE_PP(args[_signature_counter])) {										\
									case IS_NULL:																	\
										arg_types[_signature_counter] = 'n';										\
										break;																		\
									case IS_LONG:																	\
										arg_types[_signature_counter] = 'l';										\
										break;																		\
									case IS_DOUBLE:																	\
										arg_types[_signature_counter] = 'd';										\
										break;																		\
									case IS_STRING:																	\
										arg_types[_signature_counter] = 's';										\
										break;																		\
									case IS_ARRAY:																	\
										arg_types[_signature_counter] = 'a';										\
										break;																		\
									case IS_OBJECT:																	\
										arg_types[_signature_counter] = 'o';										\
										break;																		\
									case IS_BOOL:																	\
										arg_types[_signature_counter] = 'b';										\
										break;																		\
									case IS_RESOURCE:																\
										arg_types[_signature_counter] = 'r';										\
										break;																		\
									default:																		\
										arg_types[_signature_counter] = 'u';										\
								}																					\
							}																						\
																													\
							arg_types[_signature_counter] = '\0';													\
						} else {																					\
							arg_types = (char *) emalloc(sizeof(char));												\
							arg_types[0] = '\0';																	\
						}																							\
																													\
						hash_val.str = (char *) emalloc(sizeof(char) * (hash_val.len + 2));							\
						memcpy(hash_val.str, arg_types, num_args + 1);												\
						memcpy(&hash_val.str[hash_val.len - name_len],												\
							   name, name_len + 1);

#define FREE_SIGNATURE(hash_val, arg_types)																			\
						efree(arg_types);																			\
						efree(hash_val.str);

#define ALLOC_CLASS_HASH(_class_hash, _handlers) \
		if (_class_hash = pemalloc(sizeof(rpc_class_hash), TRUE)) { \
			/* set up the cache */ \
			zend_ts_hash_init(&(_class_hash->methods), 0, NULL, rpc_string_dtor, TRUE); \
			zend_ts_hash_init(&(_class_hash->properties), 0, NULL, rpc_string_dtor, TRUE); \
			_class_hash->singleton = FALSE; \
			_class_hash->poolable = FALSE; \
			_class_hash->data = NULL; \
			_class_hash->handlers = _handlers; \
		}

#define INIT_RPC_OBJECT(__intern, __clh) \
			(__intern)->ce = (__clh)->ce; \
			(__intern)->function_table.hash = (__intern)->ce->function_table;

#define OVERLOAD_RPC_CLASS(__name, __intern, __clh) { \
			zend_class_entry overloaded_class_entry; \
			INIT_CLASS_ENTRY(overloaded_class_entry, NULL, NULL); \
			overloaded_class_entry.name = __name.str; \
			overloaded_class_entry.name_length = (__name.str != NULL) ? __name.len : 0; \
			(__clh)->ce = zend_register_internal_class_ex(&overloaded_class_entry, (__intern)->ce, NULL TSRMLS_CC); \
			INIT_RPC_OBJECT(__intern, __clh); \
		}

#define  SEPARATE_RPC_CLASS(__intern) \
			(__intern)->free_function_table = 1; \
			zend_ts_hash_init(&((__intern)->function_table), 0, NULL, NULL, TRUE); \
			zend_hash_copy(&((__intern)->function_table.hash), &((__intern)->ce->function_table), NULL, NULL, 0);

#define REGISTER_RPC_CLASS(__name, __class_hash) { \
			rpc_class_hash **_tmp; \
			if ((__name).str != NULL) { \
				zend_ts_hash_add(&classes, (__name).str, (__name).len + 1, &(__class_hash), sizeof(rpc_class_hash *), (void **) &_tmp); \
			} \
            \
			tsrm_mutex_lock(classes.mx_writer); \
			zend_llist_add_element(&classes_list, _tmp); \
			tsrm_mutex_unlock(classes.mx_writer); \
			\
			if ((__class_hash)->name.str) { \
				zend_ts_hash_add(&classes, (__class_hash)->name.str, (__class_hash)->name.len + 1, &(__class_hash), sizeof(rpc_class_hash *), NULL); \
			} else { \
				zend_ts_hash_index_update(&classes, class_hash->name.len, &class_hash, sizeof(rpc_class_hash *), NULL); \
			} \
		}


#endif