Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: third_party/protobuf/php/ext/google/protobuf/storage.c

Issue 2590803003: Revert "third_party/protobuf: Update to HEAD (83d681ee2c)" (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include <stdint.h> 1 #include <stdint.h>
32 #include <protobuf.h> 2 #include <protobuf.h>
33 #include <Zend/zend.h>
34
35 #include "utf8.h"
36 3
37 // ----------------------------------------------------------------------------- 4 // -----------------------------------------------------------------------------
38 // Native slot storage. 5 // PHP <-> native slot management.
39 // ----------------------------------------------------------------------------- 6 // -----------------------------------------------------------------------------
40 7
8 static zval* int32_to_zval(int32_t value) {
9 zval* tmp;
10 MAKE_STD_ZVAL(tmp);
11 ZVAL_LONG(tmp, value);
12 php_printf("int32 to zval\n");
13 // ZVAL_LONG(tmp, 1);
14 return tmp;
15 }
16
41 #define DEREF(memory, type) *(type*)(memory) 17 #define DEREF(memory, type) *(type*)(memory)
42 18
43 size_t native_slot_size(upb_fieldtype_t type) { 19 size_t native_slot_size(upb_fieldtype_t type) {
44 switch (type) { 20 switch (type) {
45 case UPB_TYPE_FLOAT: return 4; 21 case UPB_TYPE_FLOAT: return 4;
46 case UPB_TYPE_DOUBLE: return 8; 22 case UPB_TYPE_DOUBLE: return 8;
47 case UPB_TYPE_BOOL: return 1; 23 case UPB_TYPE_BOOL: return 1;
48 case UPB_TYPE_STRING: return sizeof(void*); 24 case UPB_TYPE_STRING: return sizeof(zval*);
49 case UPB_TYPE_BYTES: return sizeof(void*); 25 case UPB_TYPE_BYTES: return sizeof(zval*);
50 case UPB_TYPE_MESSAGE: return sizeof(void*); 26 case UPB_TYPE_MESSAGE: return sizeof(zval*);
51 case UPB_TYPE_ENUM: return 4; 27 case UPB_TYPE_ENUM: return 4;
52 case UPB_TYPE_INT32: return 4; 28 case UPB_TYPE_INT32: return 4;
53 case UPB_TYPE_INT64: return 8; 29 case UPB_TYPE_INT64: return 8;
54 case UPB_TYPE_UINT32: return 4; 30 case UPB_TYPE_UINT32: return 4;
55 case UPB_TYPE_UINT64: return 8; 31 case UPB_TYPE_UINT64: return 8;
56 default: return 0; 32 default: return 0;
57 } 33 }
58 } 34 }
59 35
60 bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass, 36 static bool is_php_num(zval* value) {
61 void* memory, zval* value TSRMLS_DC) { 37 // Is numerial string also valid?
62 switch (type) { 38 return (Z_TYPE_P(value) == IS_LONG ||
63 case UPB_TYPE_STRING: 39 Z_TYPE_P(value) == IS_DOUBLE);
64 case UPB_TYPE_BYTES: {
65 if (!protobuf_convert_to_string(value)) {
66 return false;
67 }
68 if (type == UPB_TYPE_STRING &&
69 !is_structurally_valid_utf8(Z_STRVAL_P(value), Z_STRLEN_P(value))) {
70 zend_error(E_USER_ERROR, "Given string is not UTF8 encoded.");
71 return false;
72 }
73 if (*(zval**)memory != NULL) {
74 REPLACE_ZVAL_VALUE((zval**)memory, value, 1);
75 } else {
76 // Handles repeated/map string field. Memory provided by
77 // RepeatedField/Map is not initialized.
78 MAKE_STD_ZVAL(DEREF(memory, zval*));
79 ZVAL_STRINGL(DEREF(memory, zval*), Z_STRVAL_P(value), Z_STRLEN_P(value),
80 1);
81 }
82 break;
83 }
84 case UPB_TYPE_MESSAGE: {
85 if (Z_TYPE_P(value) != IS_OBJECT && Z_TYPE_P(value) != IS_NULL) {
86 zend_error(E_USER_ERROR, "Given value is not message.");
87 return false;
88 }
89 if (Z_TYPE_P(value) == IS_OBJECT && klass != Z_OBJCE_P(value)) {
90 zend_error(E_USER_ERROR, "Given message does not have correct class.");
91 return false;
92 }
93 if (EXPECTED(DEREF(memory, zval*) != value)) {
94 if (DEREF(memory, zval*) != NULL) {
95 zval_ptr_dtor((zval**)memory);
96 }
97 DEREF(memory, zval*) = value;
98 Z_ADDREF_P(value);
99 }
100 break;
101 }
102
103 #define CASE_TYPE(upb_type, type, c_type, php_type) \
104 case UPB_TYPE_##upb_type: { \
105 c_type type##_value; \
106 if (protobuf_convert_to_##type(value, &type##_value)) { \
107 DEREF(memory, c_type) = type##_value; \
108 } \
109 break; \
110 }
111 CASE_TYPE(INT32, int32, int32_t, LONG)
112 CASE_TYPE(UINT32, uint32, uint32_t, LONG)
113 CASE_TYPE(ENUM, int32, int32_t, LONG)
114 CASE_TYPE(INT64, int64, int64_t, LONG)
115 CASE_TYPE(UINT64, uint64, uint64_t, LONG)
116 CASE_TYPE(FLOAT, float, float, DOUBLE)
117 CASE_TYPE(DOUBLE, double, double, DOUBLE)
118 CASE_TYPE(BOOL, bool, int8_t, BOOL)
119
120 #undef CASE_TYPE
121
122 default:
123 break;
124 }
125
126 return true;
127 } 40 }
128 41
129 void native_slot_init(upb_fieldtype_t type, void* memory, zval** cache) { 42 void native_slot_check_int_range_precision(upb_fieldtype_t type, zval* val) {
130 zval* tmp = NULL; 43 // TODO(teboring): Add it back.
44 // if (!is_php_num(val)) {
45 // zend_error(E_ERROR, "Expected number type for integral field.");
46 // }
47
48 // if (Z_TYPE_P(val) == IS_DOUBLE) {
49 // double dbl_val = NUM2DBL(val);
50 // if (floor(dbl_val) != dbl_val) {
51 // zend_error(E_ERROR,
52 // "Non-integral floating point value assigned to integer field." );
53 // }
54 // }
55 // if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
56 // if (NUM2DBL(val) < 0) {
57 // zend_error(E_ERROR,
58 // "Assigning negative value to unsigned integer field.");
59 // }
60 // }
61 }
62
63 zval* native_slot_get(upb_fieldtype_t type, /*VALUE type_class,*/
64 const void* memory TSRMLS_DC) {
65 zval* retval = NULL;
66 switch (type) {
67 // TODO(teboring): Add it back.
68 // case UPB_TYPE_FLOAT:
69 // return DBL2NUM(DEREF(memory, float));
70 // case UPB_TYPE_DOUBLE:
71 // return DBL2NUM(DEREF(memory, double));
72 // case UPB_TYPE_BOOL:
73 // return DEREF(memory, int8_t) ? Qtrue : Qfalse;
74 // case UPB_TYPE_STRING:
75 // case UPB_TYPE_BYTES:
76 // case UPB_TYPE_MESSAGE:
77 // return DEREF(memory, VALUE);
78 // case UPB_TYPE_ENUM: {
79 // int32_t val = DEREF(memory, int32_t);
80 // VALUE symbol = enum_lookup(type_class, INT2NUM(val));
81 // if (symbol == Qnil) {
82 // return INT2NUM(val);
83 // } else {
84 // return symbol;
85 // }
86 // }
87 case UPB_TYPE_INT32:
88 return int32_to_zval(DEREF(memory, int32_t));
89 // TODO(teboring): Add it back.
90 // case UPB_TYPE_INT64:
91 // return LL2NUM(DEREF(memory, int64_t));
92 // case UPB_TYPE_UINT32:
93 // return UINT2NUM(DEREF(memory, uint32_t));
94 // case UPB_TYPE_UINT64:
95 // return ULL2NUM(DEREF(memory, uint64_t));
96 default:
97 return EG(uninitialized_zval_ptr);
98 }
99 }
100
101 void native_slot_init(upb_fieldtype_t type, void* memory) {
131 switch (type) { 102 switch (type) {
132 case UPB_TYPE_FLOAT: 103 case UPB_TYPE_FLOAT:
133 DEREF(memory, float) = 0.0; 104 DEREF(memory, float) = 0.0;
134 break; 105 break;
135 case UPB_TYPE_DOUBLE: 106 case UPB_TYPE_DOUBLE:
136 DEREF(memory, double) = 0.0; 107 DEREF(memory, double) = 0.0;
137 break; 108 break;
138 case UPB_TYPE_BOOL: 109 case UPB_TYPE_BOOL:
139 DEREF(memory, int8_t) = 0; 110 DEREF(memory, int8_t) = 0;
140 break; 111 break;
141 case UPB_TYPE_STRING: 112 // TODO(teboring): Add it back.
142 case UPB_TYPE_BYTES: 113 // case UPB_TYPE_STRING:
143 case UPB_TYPE_MESSAGE: 114 // case UPB_TYPE_BYTES:
144 DEREF(memory, zval**) = cache; 115 // DEREF(memory, VALUE) = php_str_new2("");
145 break; 116 // php_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES)
117 // ? kRubyString8bitEncoding
118 // : kRubyStringUtf8Encoding);
119 // break;
120 // case UPB_TYPE_MESSAGE:
121 // DEREF(memory, VALUE) = Qnil;
122 // break;
146 case UPB_TYPE_ENUM: 123 case UPB_TYPE_ENUM:
147 case UPB_TYPE_INT32: 124 case UPB_TYPE_INT32:
148 DEREF(memory, int32_t) = 0; 125 DEREF(memory, int32_t) = 0;
149 break; 126 break;
150 case UPB_TYPE_INT64: 127 case UPB_TYPE_INT64:
151 DEREF(memory, int64_t) = 0; 128 DEREF(memory, int64_t) = 0;
152 break; 129 break;
153 case UPB_TYPE_UINT32: 130 case UPB_TYPE_UINT32:
154 DEREF(memory, uint32_t) = 0; 131 DEREF(memory, uint32_t) = 0;
155 break; 132 break;
156 case UPB_TYPE_UINT64: 133 case UPB_TYPE_UINT64:
157 DEREF(memory, uint64_t) = 0; 134 DEREF(memory, uint64_t) = 0;
158 break; 135 break;
159 default: 136 default:
160 break; 137 break;
161 } 138 }
162 } 139 }
163 140
164 void native_slot_get(upb_fieldtype_t type, const void* memory, 141 void native_slot_set(upb_fieldtype_t type, /*VALUE type_class,*/ void* memory,
165 zval** cache TSRMLS_DC) { 142 zval* value) {
143 native_slot_set_value_and_case(type, /*type_class,*/ memory, value, NULL, 0);
144 }
145
146 void native_slot_set_value_and_case(upb_fieldtype_t type, /*VALUE type_class,*/
147 void* memory, zval* value,
148 uint32_t* case_memory,
149 uint32_t case_number) {
166 switch (type) { 150 switch (type) {
167 #define CASE(upb_type, php_type, c_type) \ 151 case UPB_TYPE_FLOAT:
168 case UPB_TYPE_##upb_type: \ 152 if (!Z_TYPE_P(value) == IS_LONG) {
169 SEPARATE_ZVAL_IF_NOT_REF(cache); \ 153 zend_error(E_ERROR, "Expected number type for float field.");
170 ZVAL_##php_type(*cache, DEREF(memory, c_type)); \ 154 }
171 return; 155 DEREF(memory, float) = Z_DVAL_P(value);
172 156 break;
173 CASE(FLOAT, DOUBLE, float) 157 case UPB_TYPE_DOUBLE:
174 CASE(DOUBLE, DOUBLE, double) 158 // TODO(teboring): Add it back.
175 CASE(BOOL, BOOL, int8_t) 159 // if (!is_php_num(value)) {
176 CASE(INT32, LONG, int32_t) 160 // zend_error(E_ERROR, "Expected number type for double field.");
177 CASE(ENUM, LONG, uint32_t) 161 // }
178 162 // DEREF(memory, double) = Z_DVAL_P(value);
179 #undef CASE 163 break;
180 164 case UPB_TYPE_BOOL: {
181 #if SIZEOF_LONG == 4 165 int8_t val = -1;
182 #define CASE(upb_type, c_type) \ 166 if (zval_is_true(value)) {
183 case UPB_TYPE_##upb_type: { \ 167 val = 1;
184 SEPARATE_ZVAL_IF_NOT_REF(cache); \ 168 } else {
185 char buffer[MAX_LENGTH_OF_INT64]; \ 169 val = 0;
186 sprintf(buffer, "%lld", DEREF(memory, c_type)); \ 170 }
187 ZVAL_STRING(*cache, buffer, 1); \ 171 // TODO(teboring): Add it back.
188 return; \ 172 // else if (value == Qfalse) {
173 // val = 0;
174 // }
175 // else {
176 // php_raise(php_eTypeError, "Invalid argument for boolean field.");
177 // }
178 DEREF(memory, int8_t) = val;
179 break;
189 } 180 }
190 #else
191 #define CASE(upb_type, c_type) \
192 case UPB_TYPE_##upb_type: { \
193 SEPARATE_ZVAL_IF_NOT_REF(cache); \
194 ZVAL_LONG(*cache, DEREF(memory, c_type)); \
195 return; \
196 }
197 #endif
198 CASE(UINT64, uint64_t)
199 CASE(INT64, int64_t)
200 #undef CASE
201
202 case UPB_TYPE_UINT32: {
203 // Prepend bit-1 for negative numbers, so that uint32 value will be
204 // consistent on both 32-bit and 64-bit architectures.
205 SEPARATE_ZVAL_IF_NOT_REF(cache);
206 int value = DEREF(memory, int32_t);
207 if (sizeof(int) == 8) {
208 value |= (-((value >> 31) & 0x1) & 0xFFFFFFFF00000000);
209 }
210 ZVAL_LONG(*cache, value);
211 return;
212 }
213
214 case UPB_TYPE_STRING: 181 case UPB_TYPE_STRING:
215 case UPB_TYPE_BYTES: { 182 case UPB_TYPE_BYTES: {
216 // For optional string/bytes fields, the cache is owned by the containing 183 // TODO(teboring): Add it back.
217 // message and should have been updated during setting/decoding. However, 184 // if (Z_TYPE_P(value) != IS_STRING) {
218 // for repeated string/bytes fields, the cache is provided by zend engine 185 // zend_error(E_ERROR, "Invalid argument for string field.");
219 // and has not been updated. 186 // }
220 zval* value = DEREF(memory, zval*); 187 // native_slot_validate_string_encoding(type, value);
221 if (*cache != value) { 188 // DEREF(memory, zval*) = value;
222 ZVAL_STRINGL(*cache, Z_STRVAL_P(value), Z_STRLEN_P(value), 1);
223 }
224 break; 189 break;
225 } 190 }
226 case UPB_TYPE_MESSAGE: { 191 case UPB_TYPE_MESSAGE: {
227 // Same as above for string/bytes fields. 192 // TODO(teboring): Add it back.
228 zval* value = DEREF(memory, zval*); 193 // if (CLASS_OF(value) == CLASS_OF(Qnil)) {
229 if (*cache != value) { 194 // value = Qnil;
230 ZVAL_ZVAL(*cache, value, 1, 0); 195 // } else if (CLASS_OF(value) != type_class) {
196 // php_raise(php_eTypeError,
197 // "Invalid type %s to assign to submessage field.",
198 // php_class2name(CLASS_OF(value)));
199 // }
200 // DEREF(memory, VALUE) = value;
201 break;
202 }
203 case UPB_TYPE_ENUM: {
204 // TODO(teboring): Add it back.
205 // int32_t int_val = 0;
206 // if (!is_php_num(value) && TYPE(value) != T_SYMBOL) {
207 // php_raise(php_eTypeError,
208 // "Expected number or symbol type for enum field.");
209 // }
210 // if (TYPE(value) == T_SYMBOL) {
211 // // Ensure that the given symbol exists in the enum module.
212 // VALUE lookup = php_funcall(type_class, php_intern("resolve"), 1, valu e);
213 // if (lookup == Qnil) {
214 // php_raise(php_eRangeError, "Unknown symbol value for enum field.");
215 // } else {
216 // int_val = NUM2INT(lookup);
217 // }
218 // } else {
219 // native_slot_check_int_range_precision(UPB_TYPE_INT32, value);
220 // int_val = NUM2INT(value);
221 // }
222 // DEREF(memory, int32_t) = int_val;
223 // break;
224 }
225 case UPB_TYPE_INT32:
226 case UPB_TYPE_INT64:
227 case UPB_TYPE_UINT32:
228 case UPB_TYPE_UINT64:
229 native_slot_check_int_range_precision(type, value);
230 switch (type) {
231 case UPB_TYPE_INT32:
232 php_printf("Setting INT32 field\n");
233 DEREF(memory, int32_t) = Z_LVAL_P(value);
234 break;
235 case UPB_TYPE_INT64:
236 // TODO(teboring): Add it back.
237 // DEREF(memory, int64_t) = NUM2LL(value);
238 break;
239 case UPB_TYPE_UINT32:
240 // TODO(teboring): Add it back.
241 // DEREF(memory, uint32_t) = NUM2UINT(value);
242 break;
243 case UPB_TYPE_UINT64:
244 // TODO(teboring): Add it back.
245 // DEREF(memory, uint64_t) = NUM2ULL(value);
246 break;
247 default:
248 break;
231 } 249 }
232 return; 250 break;
233 }
234 default: 251 default:
235 return; 252 break;
253 }
254
255 if (case_memory != NULL) {
256 *case_memory = case_number;
236 } 257 }
237 } 258 }
238 259
239 void native_slot_get_default(upb_fieldtype_t type, zval** cache TSRMLS_DC) {
240 switch (type) {
241 #define CASE(upb_type, php_type) \
242 case UPB_TYPE_##upb_type: \
243 SEPARATE_ZVAL_IF_NOT_REF(cache); \
244 ZVAL_##php_type(*cache, 0); \
245 return;
246
247 CASE(FLOAT, DOUBLE)
248 CASE(DOUBLE, DOUBLE)
249 CASE(BOOL, BOOL)
250 CASE(INT32, LONG)
251 CASE(INT64, LONG)
252 CASE(UINT32, LONG)
253 CASE(UINT64, LONG)
254 CASE(ENUM, LONG)
255
256 #undef CASE
257
258 case UPB_TYPE_STRING:
259 case UPB_TYPE_BYTES: {
260 SEPARATE_ZVAL_IF_NOT_REF(cache);
261 ZVAL_STRINGL(*cache, "", 0, 1);
262 break;
263 }
264 case UPB_TYPE_MESSAGE: {
265 SEPARATE_ZVAL_IF_NOT_REF(cache);
266 ZVAL_NULL(*cache);
267 return;
268 }
269 default:
270 return;
271 }
272 }
273
274 // ----------------------------------------------------------------------------- 260 // -----------------------------------------------------------------------------
275 // Map field utilities. 261 // Map field utilities.
276 // ---------------------------------------------------------------------------- 262 // ----------------------------------------------------------------------------
277 263
278 const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) { 264 const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) {
279 const upb_msgdef* subdef; 265 const upb_msgdef* subdef;
280 if (upb_fielddef_label(field) != UPB_LABEL_REPEATED || 266 if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
281 upb_fielddef_type(field) != UPB_TYPE_MESSAGE) { 267 upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
282 return NULL; 268 return NULL;
283 } 269 }
284 subdef = upb_fielddef_msgsubdef(field); 270 subdef = upb_fielddef_msgsubdef(field);
285 return upb_msgdef_mapentry(subdef) ? subdef : NULL; 271 return upb_msgdef_mapentry(subdef) ? subdef : NULL;
286 } 272 }
287 273
288 const upb_msgdef* map_entry_msgdef(const upb_fielddef* field) { 274 const upb_msgdef* map_entry_msgdef(const upb_fielddef* field) {
289 const upb_msgdef* subdef = tryget_map_entry_msgdef(field); 275 const upb_msgdef* subdef = tryget_map_entry_msgdef(field);
290 assert(subdef); 276 assert(subdef);
291 return subdef; 277 return subdef;
292 } 278 }
293 279
294 bool is_map_field(const upb_fielddef* field) { 280 bool is_map_field(const upb_fielddef* field) {
295 return tryget_map_entry_msgdef(field) != NULL; 281 return tryget_map_entry_msgdef(field) != NULL;
296 } 282 }
297 283
298 const upb_fielddef* map_field_key(const upb_fielddef* field) {
299 const upb_msgdef* subdef = map_entry_msgdef(field);
300 return map_entry_key(subdef);
301 }
302
303 const upb_fielddef* map_field_value(const upb_fielddef* field) {
304 const upb_msgdef* subdef = map_entry_msgdef(field);
305 return map_entry_value(subdef);
306 }
307
308 const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
309 const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
310 assert(key_field != NULL);
311 return key_field;
312 }
313
314 const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
315 const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
316 assert(value_field != NULL);
317 return value_field;
318 }
319
320 const zend_class_entry* field_type_class(const upb_fielddef* field TSRMLS_DC) {
321 if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
322 zval* desc_php = get_def_obj(upb_fielddef_subdef(field));
323 Descriptor* desc = zend_object_store_get_object(desc_php TSRMLS_CC);
324 return desc->klass;
325 } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
326 zval* desc_php = get_def_obj(upb_fielddef_subdef(field));
327 EnumDescriptor* desc = zend_object_store_get_object(desc_php TSRMLS_CC);
328 return desc->klass;
329 }
330 return NULL;
331 }
332
333 // ----------------------------------------------------------------------------- 284 // -----------------------------------------------------------------------------
334 // Memory layout management. 285 // Memory layout management.
335 // ----------------------------------------------------------------------------- 286 // -----------------------------------------------------------------------------
336 287
337 static size_t align_up_to(size_t offset, size_t granularity) { 288 static size_t align_up_to(size_t offset, size_t granularity) {
338 // Granularity must be a power of two. 289 // Granularity must be a power of two.
339 return (offset + granularity - 1) & ~(granularity - 1); 290 return (offset + granularity - 1) & ~(granularity - 1);
340 } 291 }
341 292
342 static void* slot_memory(MessageLayout* layout, const void* storage,
343 const upb_fielddef* field) {
344 return ((uint8_t*)storage) + layout->fields[upb_fielddef_index(field)].offset;
345 }
346
347 static uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage,
348 const upb_fielddef* field) {
349 return (uint32_t*)(((uint8_t*)storage) +
350 layout->fields[upb_fielddef_index(field)].case_offset);
351 }
352
353 static int slot_property_cache(MessageLayout* layout, const void* storage,
354 const upb_fielddef* field) {
355 return layout->fields[upb_fielddef_index(field)].cache_index;
356 }
357
358 MessageLayout* create_layout(const upb_msgdef* msgdef) { 293 MessageLayout* create_layout(const upb_msgdef* msgdef) {
359 MessageLayout* layout = ALLOC(MessageLayout); 294 MessageLayout* layout = ALLOC(MessageLayout);
360 int nfields = upb_msgdef_numfields(msgdef); 295 int nfields = upb_msgdef_numfields(msgdef);
361 upb_msg_field_iter it; 296 upb_msg_field_iter it;
362 upb_msg_oneof_iter oit; 297 upb_msg_oneof_iter oit;
363 size_t off = 0; 298 size_t off = 0;
364 int i = 0;
365 299
366 layout->fields = ALLOC_N(MessageField, nfields); 300 layout->fields = ALLOC_N(MessageField, nfields);
367 301
368 for (upb_msg_field_begin(&it, msgdef); !upb_msg_field_done(&it); 302 for (upb_msg_field_begin(&it, msgdef); !upb_msg_field_done(&it);
369 upb_msg_field_next(&it)) { 303 upb_msg_field_next(&it)) {
370 const upb_fielddef* field = upb_msg_iter_field(&it); 304 const upb_fielddef* field = upb_msg_iter_field(&it);
371 size_t field_size; 305 size_t field_size;
372 306
373 if (upb_fielddef_containingoneof(field)) { 307 if (upb_fielddef_containingoneof(field)) {
374 // Oneofs are handled separately below. 308 // Oneofs are handled separately below.
375 continue; 309 continue;
376 } 310 }
377 311
378 // Allocate |field_size| bytes for this field in the layout. 312 // Allocate |field_size| bytes for this field in the layout.
379 field_size = 0; 313 field_size = 0;
380 if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { 314 if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
381 field_size = sizeof(zval*); 315 field_size = sizeof(zval*);
382 } else { 316 } else {
383 field_size = native_slot_size(upb_fielddef_type(field)); 317 field_size = native_slot_size(upb_fielddef_type(field));
384 } 318 }
385 319
386 // Align current offset up to | size | granularity. 320 // Align current offset up to | size | granularity.
387 off = align_up_to(off, field_size); 321 off = align_up_to(off, field_size);
388 layout->fields[upb_fielddef_index(field)].offset = off; 322 layout->fields[upb_fielddef_index(field)].offset = off;
389 layout->fields[upb_fielddef_index(field)].case_offset = 323 layout->fields[upb_fielddef_index(field)].case_offset =
390 MESSAGE_FIELD_NO_CASE; 324 MESSAGE_FIELD_NO_CASE;
391 layout->fields[upb_fielddef_index(field)].cache_index = i++;
392 off += field_size; 325 off += field_size;
393 } 326 }
394 327
395 // Handle oneofs now -- we iterate over oneofs specifically and allocate only 328 // Handle oneofs now -- we iterate over oneofs specifically and allocate only
396 // one slot per oneof. 329 // one slot per oneof.
397 // 330 //
398 // We assign all value slots first, then pack the 'case' fields at the end, 331 // We assign all value slots first, then pack the 'case' fields at the end,
399 // since in the common case (modern 64-bit platform) these are 8 bytes and 4 332 // since in the common case (modern 64-bit platform) these are 8 bytes and 4
400 // bytes respectively and we want to avoid alignment overhead. 333 // bytes respectively and we want to avoid alignment overhead.
401 // 334 //
(...skipping 11 matching lines...) Expand all
413 // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between 346 // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between
414 // all fields. 347 // all fields.
415 size_t field_size = NATIVE_SLOT_MAX_SIZE; 348 size_t field_size = NATIVE_SLOT_MAX_SIZE;
416 // Align the offset . 349 // Align the offset .
417 off = align_up_to( off, field_size); 350 off = align_up_to( off, field_size);
418 // Assign all fields in the oneof this same offset. 351 // Assign all fields in the oneof this same offset.
419 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); 352 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit);
420 upb_oneof_next(&fit)) { 353 upb_oneof_next(&fit)) {
421 const upb_fielddef* field = upb_oneof_iter_field(&fit); 354 const upb_fielddef* field = upb_oneof_iter_field(&fit);
422 layout->fields[upb_fielddef_index(field)].offset = off; 355 layout->fields[upb_fielddef_index(field)].offset = off;
423 layout->fields[upb_fielddef_index(field)].cache_index = i;
424 } 356 }
425 i++;
426 off += field_size; 357 off += field_size;
427 } 358 }
428 359
429 // Now the case offset. 360 // Now the case fields.
430 for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit); 361 for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit);
431 upb_msg_oneof_next(&oit)) { 362 upb_msg_oneof_next(&oit)) {
432 const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit); 363 const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit);
433 upb_oneof_iter fit; 364 upb_oneof_iter fit;
434 365
435 size_t field_size = sizeof(uint32_t); 366 size_t field_size = sizeof(uint32_t);
436 // Align the offset . 367 // Align the offset .
437 off = (off + field_size - 1) & ~(field_size - 1); 368 off = (off + field_size - 1) & ~(field_size - 1);
438 // Assign all fields in the oneof this same offset. 369 // Assign all fields in the oneof this same offset.
439 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); 370 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit);
(...skipping 11 matching lines...) Expand all
451 382
452 return layout; 383 return layout;
453 } 384 }
454 385
455 void free_layout(MessageLayout* layout) { 386 void free_layout(MessageLayout* layout) {
456 FREE(layout->fields); 387 FREE(layout->fields);
457 upb_msgdef_unref(layout->msgdef, &layout->msgdef); 388 upb_msgdef_unref(layout->msgdef, &layout->msgdef);
458 FREE(layout); 389 FREE(layout);
459 } 390 }
460 391
461 void layout_init(MessageLayout* layout, void* storage, 392 // TODO(teboring): Add it back.
462 zval** properties_table TSRMLS_DC) { 393 // VALUE field_type_class(const upb_fielddef* field) {
463 int i; 394 // VALUE type_class = Qnil;
395 // if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
396 // VALUE submsgdesc = get_def_obj(upb_fielddef_subdef(field));
397 // type_class = Descriptor_msgclass(submsgdesc);
398 // } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
399 // VALUE subenumdesc = get_def_obj(upb_fielddef_subdef(field));
400 // type_class = EnumDescriptor_enummodule(subenumdesc);
401 // }
402 // return type_class;
403 // }
404
405 static void* slot_memory(MessageLayout* layout, const void* storage,
406 const upb_fielddef* field) {
407 return ((uint8_t*)storage) + layout->fields[upb_fielddef_index(field)].offset;
408 }
409
410 static uint32_t* slot_oneof_case(MessageLayout* layout, const void* storage,
411 const upb_fielddef* field) {
412 return (uint32_t*)(((uint8_t*)storage) +
413 layout->fields[upb_fielddef_index(field)].case_offset);
414 }
415
416 void layout_set(MessageLayout* layout, void* storage, const upb_fielddef* field,
417 zval* val) {
418 void* memory = slot_memory(layout, storage, field);
419 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
420
421 if (upb_fielddef_containingoneof(field)) {
422 if (Z_TYPE_P(val) == IS_NULL) {
423 // Assigning nil to a oneof field clears the oneof completely.
424 *oneof_case = ONEOF_CASE_NONE;
425 memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
426 } else {
427 // The transition between field types for a single oneof (union) slot is
428 // somewhat complex because we need to ensure that a GC triggered at any
429 // point by a call into the Ruby VM sees a valid state for this field and
430 // does not either go off into the weeds (following what it thinks is a
431 // VALUE but is actually a different field type) or miss an object (seeing
432 // what it thinks is a primitive field but is actually a VALUE for the new
433 // field type).
434 //
435 // In order for the transition to be safe, the oneof case slot must be in
436 // sync with the value slot whenever the Ruby VM has been called. Thus, we
437 // use native_slot_set_value_and_case(), which ensures that both the value
438 // and case number are altered atomically (w.r.t. the Ruby VM).
439 native_slot_set_value_and_case(upb_fielddef_type(field),
440 /*field_type_class(field),*/ memory, val,
441 oneof_case, upb_fielddef_number(field));
442 }
443 } else if (is_map_field(field)) {
444 // TODO(teboring): Add it back.
445 // check_map_field_type(val, field);
446 // DEREF(memory, zval*) = val;
447 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
448 // TODO(teboring): Add it back.
449 // check_repeated_field_type(val, field);
450 // DEREF(memory, zval*) = val;
451 } else {
452 native_slot_set(upb_fielddef_type(field), /*field_type_class(field),*/ memor y,
453 val);
454 }
455 }
456
457 void layout_init(MessageLayout* layout, void* storage) {
464 upb_msg_field_iter it; 458 upb_msg_field_iter it;
465 for (upb_msg_field_begin(&it, layout->msgdef), i = 0; !upb_msg_field_done(&it) ; 459 for (upb_msg_field_begin(&it, layout->msgdef); !upb_msg_field_done(&it);
466 upb_msg_field_next(&it), i++) { 460 upb_msg_field_next(&it)) {
467 const upb_fielddef* field = upb_msg_iter_field(&it); 461 const upb_fielddef* field = upb_msg_iter_field(&it);
468 void* memory = slot_memory(layout, storage, field); 462 void* memory = slot_memory(layout, storage, field);
469 uint32_t* oneof_case = slot_oneof_case(layout, storage, field); 463 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
470 int cache_index = slot_property_cache(layout, storage, field);
471 zval** property_ptr = &properties_table[cache_index];
472 464
473 if (upb_fielddef_containingoneof(field)) { 465 if (upb_fielddef_containingoneof(field)) {
474 memset(memory, 0, NATIVE_SLOT_MAX_SIZE); 466 // TODO(teboring): Add it back.
475 *oneof_case = ONEOF_CASE_NONE; 467 // memset(memory, 0, NATIVE_SLOT_MAX_SIZE);
468 // *oneof_case = ONEOF_CASE_NONE;
476 } else if (is_map_field(field)) { 469 } else if (is_map_field(field)) {
477 zval_ptr_dtor(property_ptr); 470 // TODO(teboring): Add it back.
478 map_field_create_with_type(map_field_type, field, property_ptr TSRMLS_CC); 471 // VALUE map = Qnil;
479 DEREF(memory, zval**) = property_ptr; 472
473 // const upb_fielddef* key_field = map_field_key(field);
474 // const upb_fielddef* value_field = map_field_value(field);
475 // VALUE type_class = field_type_class(value_field);
476
477 // if (type_class != Qnil) {
478 // VALUE args[3] = {
479 // fieldtype_to_php(upb_fielddef_type(key_field)),
480 // fieldtype_to_php(upb_fielddef_type(value_field)), type_class,
481 // };
482 // map = php_class_new_instance(3, args, cMap);
483 // } else {
484 // VALUE args[2] = {
485 // fieldtype_to_php(upb_fielddef_type(key_field)),
486 // fieldtype_to_php(upb_fielddef_type(value_field)),
487 // };
488 // map = php_class_new_instance(2, args, cMap);
489 // }
490
491 // DEREF(memory, VALUE) = map;
480 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { 492 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
481 zval_ptr_dtor(property_ptr); 493 // TODO(teboring): Add it back.
482 repeated_field_create_with_type(repeated_field_type, field, 494 // VALUE ary = Qnil;
483 property_ptr TSRMLS_CC); 495
484 DEREF(memory, zval**) = property_ptr; 496 // VALUE type_class = field_type_class(field);
497
498 // if (type_class != Qnil) {
499 // VALUE args[2] = {
500 // fieldtype_to_php(upb_fielddef_type(field)), type_class,
501 // };
502 // ary = php_class_new_instance(2, args, cRepeatedField);
503 // } else {
504 // VALUE args[1] = {fieldtype_to_php(upb_fielddef_type(field))};
505 // ary = php_class_new_instance(1, args, cRepeatedField);
506 // }
507
508 // DEREF(memory, VALUE) = ary;
485 } else { 509 } else {
486 native_slot_init(upb_fielddef_type(field), memory, property_ptr); 510 native_slot_init(upb_fielddef_type(field), memory);
487 } 511 }
488 } 512 }
489 } 513 }
490 514
491 // For non-singular fields, the related memory needs to point to the actual
492 // zval in properties table first.
493 static void* value_memory(const upb_fielddef* field, void* memory) {
494 switch (upb_fielddef_type(field)) {
495 case UPB_TYPE_STRING:
496 case UPB_TYPE_BYTES:
497 case UPB_TYPE_MESSAGE:
498 memory = DEREF(memory, zval**);
499 break;
500 default:
501 // No operation
502 break;
503 }
504 return memory;
505 }
506
507 zval* layout_get(MessageLayout* layout, const void* storage, 515 zval* layout_get(MessageLayout* layout, const void* storage,
508 const upb_fielddef* field, zval** cache TSRMLS_DC) { 516 const upb_fielddef* field TSRMLS_DC) {
509 void* memory = slot_memory(layout, storage, field); 517 void* memory = slot_memory(layout, storage, field);
510 uint32_t* oneof_case = slot_oneof_case(layout, storage, field); 518 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
511 519
512 if (upb_fielddef_containingoneof(field)) { 520 if (upb_fielddef_containingoneof(field)) {
513 if (*oneof_case != upb_fielddef_number(field)) { 521 if (*oneof_case != upb_fielddef_number(field)) {
514 native_slot_get_default(upb_fielddef_type(field), cache TSRMLS_CC); 522 return NULL;
515 } else { 523 // TODO(teboring): Add it back.
516 native_slot_get(upb_fielddef_type(field), value_memory(field, memory), 524 // return Qnil;
517 cache TSRMLS_CC);
518 } 525 }
519 return *cache; 526 return NULL;
527 // TODO(teboring): Add it back.
528 // return native_slot_get(upb_fielddef_type(field), field_type_class(field),
529 // memory);
520 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { 530 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
521 return *cache; 531 return NULL;
532 // TODO(teboring): Add it back.
533 // return *((VALUE*)memory);
522 } else { 534 } else {
523 native_slot_get(upb_fielddef_type(field), value_memory(field, memory), 535 return native_slot_get(
524 cache TSRMLS_CC); 536 upb_fielddef_type(field), /*field_type_class(field), */
525 return *cache; 537 memory TSRMLS_CC);
526 } 538 }
527 } 539 }
528
529 void layout_set(MessageLayout* layout, MessageHeader* header,
530 const upb_fielddef* field, zval* val TSRMLS_DC) {
531 void* storage = message_data(header);
532 void* memory = slot_memory(layout, storage, field);
533 uint32_t* oneof_case = slot_oneof_case(layout, storage, field);
534
535 if (upb_fielddef_containingoneof(field)) {
536 upb_fieldtype_t type = upb_fielddef_type(field);
537 zend_class_entry *ce = NULL;
538
539 // For non-singular fields, the related memory needs to point to the actual
540 // zval in properties table first.
541 switch (type) {
542 case UPB_TYPE_MESSAGE: {
543 const upb_msgdef* msg = upb_fielddef_msgsubdef(field);
544 zval* desc_php = get_def_obj(msg);
545 Descriptor* desc = zend_object_store_get_object(desc_php TSRMLS_CC);
546 ce = desc->klass;
547 // Intentionally fall through.
548 }
549 case UPB_TYPE_STRING:
550 case UPB_TYPE_BYTES: {
551 int property_cache_index =
552 header->descriptor->layout->fields[upb_fielddef_index(field)]
553 .cache_index;
554 DEREF(memory, zval**) =
555 &(header->std.properties_table)[property_cache_index];
556 memory = DEREF(memory, zval**);
557 break;
558 }
559 default:
560 break;
561 }
562
563 native_slot_set(type, ce, memory, val TSRMLS_CC);
564 *oneof_case = upb_fielddef_number(field);
565 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
566 // Works for both repeated and map fields
567 memory = DEREF(memory, zval**);
568 if (EXPECTED(DEREF(memory, zval*) != val)) {
569 zval_ptr_dtor(memory);
570 DEREF(memory, zval*) = val;
571 Z_ADDREF_P(val);
572 }
573 } else {
574 upb_fieldtype_t type = upb_fielddef_type(field);
575 zend_class_entry *ce = NULL;
576 if (type == UPB_TYPE_MESSAGE) {
577 const upb_msgdef* msg = upb_fielddef_msgsubdef(field);
578 zval* desc_php = get_def_obj(msg);
579 Descriptor* desc = zend_object_store_get_object(desc_php TSRMLS_CC);
580 ce = desc->klass;
581 }
582 native_slot_set(type, ce, value_memory(field, memory), val TSRMLS_CC);
583 }
584 }
OLDNEW
« no previous file with comments | « third_party/protobuf/php/ext/google/protobuf/protobuf.c ('k') | third_party/protobuf/php/ext/google/protobuf/test.php » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698