webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
protobuf.h
Go to the documentation of this file.
1 #ifndef __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
2 #define __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
3 
4 #include <php.h>
5 
6 #include "upb.h"
7 
8 #define PHP_PROTOBUF_EXTNAME "protobuf"
9 #define PHP_PROTOBUF_VERSION "0.01"
10 
11 // Forward decls.
12 struct DescriptorPool;
13 struct Descriptor;
14 struct FieldDescriptor;
15 struct EnumDescriptor;
16 struct MessageLayout;
17 struct MessageField;
18 struct MessageHeader;
20 struct EnumBuilderContext;
21 
23 typedef struct Descriptor Descriptor;
28 typedef struct MessageField MessageField;
33 
34 extern zend_class_entry* builder_type;
35 extern zend_class_entry* descriptor_type;
36 extern zend_class_entry* message_builder_context_type;
37 
38 extern DescriptorPool* generated_pool; // The actual generated pool
39 
40 ZEND_BEGIN_MODULE_GLOBALS(protobuf)
41  zval* generated_pool;
42  zend_object_handlers* message_handlers;
44 ZEND_END_MODULE_GLOBALS(protobuf)
45 
46 ZEND_DECLARE_MODULE_GLOBALS(protobuf)
47 
48 #ifdef ZTS
49 #define PROTOBUF_G(v) TSRMG(protobuf_globals_id, zend_protobuf_globals*, v)
50 #else
51 #define PROTOBUF_G(v) (protobuf_globals.v)
52 #endif
53 
54 // -----------------------------------------------------------------------------
55 // PHP functions and global variables.
56 // -----------------------------------------------------------------------------
57 
59 
60 // -----------------------------------------------------------------------------
61 // PHP class structure.
62 // -----------------------------------------------------------------------------
63 
65  zend_object std;
67  HashTable* pending_list;
68 };
69 
70 struct Descriptor {
71  zend_object std;
74  // zval* klass; // begins as NULL
75  // const upb_handlers* fill_handlers;
76  // const upb_pbdecodermethod* fill_method;
78  // const upb_handlers* json_serialize_handlers;
79  // Handlers hold type class references for sub-message fields directly in some
80  // cases. We need to keep these rooted because they might otherwise be
81  // collected.
82  // zval_array typeclass_references;
83 };
84 
86  zend_object std;
88 };
89 
91  zend_object std;
93 };
94 
96  zend_object std;
98  // zval* module; // begins as NULL
99 };
100 
101 // -----------------------------------------------------------------------------
102 // Native slot storage abstraction.
103 // -----------------------------------------------------------------------------
104 
105 #define NATIVE_SLOT_MAX_SIZE sizeof(uint64_t)
106 
108 
109 #define MAP_KEY_FIELD 1
110 #define MAP_VALUE_FIELD 2
111 
112 // Oneof case slot value to indicate that no oneof case is set. The value `0` is
113 // safe because field numbers are used as case identifiers, and no field can
114 // have a number of 0.
115 #define ONEOF_CASE_NONE 0
116 
117 // These operate on a map field (i.e., a repeated field of submessages whose
118 // submessage type is a map-entry msgdef).
119 bool is_map_field(const upb_fielddef* field);
122 
123 // These operate on a map-entry msgdef.
124 const upb_fielddef* map_entry_key(const upb_msgdef* msgdef);
125 const upb_fielddef* map_entry_value(const upb_msgdef* msgdef);
126 
127 // -----------------------------------------------------------------------------
128 // Message layout / storage.
129 // -----------------------------------------------------------------------------
130 
131 #define MESSAGE_FIELD_NO_CASE ((size_t)-1)
132 
133 struct MessageField {
134  size_t offset;
135  size_t case_offset; // for oneofs, a uint32. Else, MESSAGE_FIELD_NO_CASE.
136 };
137 
141  size_t size;
142 };
143 
144 void layout_init(MessageLayout* layout, void* storage);
145 zval* layout_get(MessageLayout* layout, const void* storage,
146  const upb_fielddef* field TSRMLS_DC);
147 MessageLayout* create_layout(const upb_msgdef* msgdef);
148 void free_layout(MessageLayout* layout);
149 zval* native_slot_get(upb_fieldtype_t type, /*VALUE type_class,*/
150  const void* memory TSRMLS_DC);
151 
152 // -----------------------------------------------------------------------------
153 // Message class creation.
154 // -----------------------------------------------------------------------------
155 
157  zend_object std;
158  Descriptor* descriptor; // kept alive by self.class.descriptor reference.
159  // Data comes after this.
160 };
161 
163  zend_object std;
164  zval* descriptor;
165  zval* pool;
166 };
167 
169  zend_object std;
170  // VALUE descriptor;
171  // VALUE builder;
172 };
173 
175  zend_object std;
176  // VALUE enumdesc;
177 };
178 
179 // Forward-declare all of the PHP method implementations.
180 
182 zend_object_value descriptor_pool_create(zend_class_entry *ce TSRMLS_DC);
183 void descriptor_pool_free_c(DescriptorPool* object TSRMLS_DC);
184 void descriptor_pool_free(void* object TSRMLS_DC);
185 void descriptor_pool_init_c_instance(DescriptorPool* pool TSRMLS_DC);
186 PHP_METHOD(DescriptorPool, addMessage);
187 PHP_METHOD(DescriptorPool, finalize);
188 
189 Descriptor* php_to_descriptor(zval* value TSRMLS_DC);
190 zend_object_value descriptor_create(zend_class_entry *ce TSRMLS_DC);
191 void descriptor_init_c_instance(Descriptor* intern TSRMLS_DC);
192 void descriptor_free_c(Descriptor* object TSRMLS_DC);
193 void descriptor_free(void* object TSRMLS_DC);
194 void descriptor_name_set(Descriptor *desc, const char *name);
195 
197 zend_object_value message_builder_context_create(
198  zend_class_entry* ce TSRMLS_DC);
200  MessageBuilderContext* intern TSRMLS_DC);
202 void message_builder_context_free(void* object TSRMLS_DC);
204 PHP_METHOD(MessageBuilderContext, finalizeToPool);
205 
207 const zend_class_entry* build_class_from_descriptor(
208  zval* php_descriptor TSRMLS_DC);
209 
210 PHP_FUNCTION(get_generated_pool);
211 
212 // -----------------------------------------------------------------------------
213 // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
214 // instances.
215 // ----------------------------------------------------------------------------
216 
217 void add_def_obj(const void* def, zval* value);
218 zval* get_def_obj(const void* def);
219 
220 // -----------------------------------------------------------------------------
221 // Utilities.
222 // -----------------------------------------------------------------------------
223 
224 // PHP Array utils.
225 #define Z_ARRVAL_SIZE_P(zval_p) zend_hash_num_elements(Z_ARRVAL_P(zval_p))
226 #define Z_ARRVAL_BEGIN_P(zval_p) Z_ARRVAL_P(zval_p)->pListHead
227 #define Z_BUCKET_NEXT_PP(bucket_pp) *bucket_pp = (*bucket_pp)->pListNext
228 
229 #define DEFINE_PHP_OBJECT(class_name, class_name_lower, name) \
230  do { \
231  zval* name; \
232  MAKE_STD_ZVAL(name); \
233  object_init_ex(name, class_name_lower##_type); \
234  } while (0)
235 
236 #define DEFINE_PHP_WRAPPER(class_name, class_name_lower, name, intern) \
237  zval* name; \
238  MAKE_STD_ZVAL(name); \
239  object_init_ex(name, class_name_lower##_type); \
240  Z_OBJVAL_P(name) \
241  .handle = zend_objects_store_put( \
242  intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, \
243  class_name_lower##_free, NULL TSRMLS_CC);
244 
245 #define DEFINE_PHP_ZVAL(name) \
246  do { \
247  zval* name; \
248  MAKE_STD_ZVAL(name); \
249  } while (0)
250 
251 #define DEFINE_PHP_STRING(name, value) \
252  do { \
253  zval* name; \
254  MAKE_STD_ZVAL(name); \
255  ZVAL_STRING(name, value, 1); \
256  } while (0)
257 
258 // Upb Utilities
259 
260 void check_upb_status(const upb_status* status, const char* msg);
261 
262 #define CHECK_UPB(code, msg) \
263  do { \
264  upb_status status = UPB_STATUS_INIT; \
265  code; \
266  check_upb_status(&status, msg); \
267  } while (0)
268 
269 // Memory management
270 
271 #define ALLOC(class_name) (class_name*) emalloc(sizeof(class_name))
272 #define ALLOC_N(class_name, n) (class_name*) emalloc(sizeof(class_name) * n)
273 #define FREE(object) efree(object)
274 
275 // Type Checking
276 #define CHECK_TYPE(field, type) \
277  if (Z_TYPE_P(field) != type) { \
278  zend_error(E_ERROR, "Unexpected type"); \
279  }
280 
281 #endif // __GOOGLE_PROTOBUF_PHP_PROTOBUF_H__
upb_fieldtype_t
Definition: upb.h:1435
MessageField * fields
Definition: protobuf.h:140
const upb_handlers * pb_serialize_handlers
Definition: protobuf.h:77
DescriptorPool * generated_pool
Definition: protobuf.h:41
short encode(IlbcEncoder *iLBCenc_inst, int16_t *encoded_data, int16_t *data)
Definition: iLBC_testprogram.c:42
const upb_msgdef * msgdef
Definition: protobuf.h:139
MessageLayout * layout
Definition: protobuf.h:73
zval * native_slot_get(upb_fieldtype_t type, const void *memory TSRMLS_DC)
Definition: storage.c:63
const FieldDescriptor * field
Definition: parser_unittest.cc:2279
void free_layout(MessageLayout *layout)
Definition: storage.c:386
zval * pool
Definition: protobuf.h:165
void descriptor_name_set(Descriptor *desc, const char *name)
DescriptorPool * php_to_descriptor_pool(zval *value TSRMLS_DC)
Definition: protobuf.h:90
zend_object_value message_builder_context_create(zend_class_entry *ce TSRMLS_DC)
const upb_fielddef * map_field_key(const upb_fielddef *field)
Definition: storage.c:369
zend_object_value descriptor_create(zend_class_entry *ce TSRMLS_DC)
zend_object_value descriptor_pool_create(zend_class_entry *ce TSRMLS_DC)
zval * descriptor
Definition: protobuf.h:164
zend_class_entry * builder_type
zend_object_handlers * message_handlers
Definition: protobuf.h:42
Definition: protobuf.h:174
const upb_enumdef * enumdef
Definition: protobuf.h:97
Message
Definition: peerconnection_unittest.cc:105
Definition: upb.h:3092
void layout_init(MessageLayout *layout, void *storage)
Definition: storage.c:457
Definition: status.py:1
void message_builder_context_init_c_instance(MessageBuilderContext *intern TSRMLS_DC)
zend_class_entry * descriptor_type
upb_symtab * symtab
Definition: protobuf.h:66
Definition: upb.h:3140
zend_object std
Definition: protobuf.h:96
#define desc
Definition: extension_set.h:320
MessageLayout * create_layout(const upb_msgdef *msgdef)
Definition: storage.c:293
void descriptor_free_c(Descriptor *object TSRMLS_DC)
Definition: upb.h:3044
size_t case_offset
Definition: protobuf.h:135
Definition: protobuf.h:85
size_t offset
Definition: protobuf.h:134
void descriptor_pool_init_c_instance(DescriptorPool *pool TSRMLS_DC)
Definition: def.c:112
zend_object std
Definition: protobuf.h:163
EGLAttrib * value
Definition: eglext.h:120
zend_object std
Definition: protobuf.h:65
zend_object std
Definition: protobuf.h:169
Definition: protobuf.h:64
EGLImageKHR EGLint * name
Definition: eglext.h:851
Definition: upb.h:3126
zend_object std
Definition: protobuf.h:86
Definition: __init__.py:1
PHP_MINIT_FUNCTION(protobuf)
Definition: protobuf.c:85
zval * get_def_obj(const void *def)
Definition: protobuf.c:23
const upb_fielddef * map_field_value(const upb_fielddef *field)
Definition: storage.c:374
Definition: protobuf.h:133
const zend_class_entry * build_class_from_descriptor(zval *php_descriptor TSRMLS_DC)
Definition: message.c:240
PHP_FUNCTION(get_generated_pool)
void descriptor_pool_free_c(DescriptorPool *object TSRMLS_DC)
EGLenum type
Definition: eglext.h:63
void descriptor_free(void *object TSRMLS_DC)
void message_builder_context_free_c(MessageBuilderContext *object TSRMLS_DC)
PHP_METHOD(DescriptorPool, addMessage)
Definition: def.c:126
size_t size
Definition: protobuf.h:141
Definition: upb.h:504
void check_upb_status(const upb_status *status, const char *msg)
Definition: def.c:7
Descriptor * descriptor
Definition: protobuf.h:158
Definition: protobuf.h:156
Definition: protobuf.h:138
void add_def_obj(const void *def, zval *value)
Definition: protobuf.c:14
Definition: upb.h:3680
const upb_oneofdef * oneofdef
Definition: protobuf.h:92
Definition: protobuf.h:168
Descriptor * php_to_descriptor(zval *value TSRMLS_DC)
void descriptor_pool_free(void *object TSRMLS_DC)
MessageBuilderContext * php_to_message_builder_context(zval *value TSRMLS_DC)
zend_object std
Definition: protobuf.h:71
void message_builder_context_free(void *object TSRMLS_DC)
HashTable * pending_list
Definition: protobuf.h:67
HashTable upb_def_to_php_obj_map
Definition: protobuf.h:43
const upb_msgdef * msgdef
Definition: protobuf.h:72
bool is_map_field(const upb_fielddef *field)
Definition: storage.c:280
const upb_fielddef * map_entry_key(const upb_msgdef *msgdef)
Definition: storage.c:379
const upb_fielddef * fielddef
Definition: protobuf.h:87
zend_object std
Definition: protobuf.h:157
zend_object std
Definition: protobuf.h:91
size_t native_slot_size(upb_fieldtype_t type)
Definition: storage.c:19
const upb_fielddef * map_entry_value(const upb_msgdef *msgdef)
Definition: storage.c:385
Definition: EncryptedMediaExtensions.idl:16
zval * layout_get(MessageLayout *layout, const void *storage, const upb_fielddef *field TSRMLS_DC)
Definition: storage.c:515
zend_class_entry * message_builder_context_type
Definition: protobuf.h:70
void descriptor_init_c_instance(Descriptor *intern TSRMLS_DC)
Definition: protobuf.h:95
Definition: upb.h:3154
zend_object std
Definition: protobuf.h:175
Definition: protobuf.h:162