OLD | NEW |
| 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 |
1 #include <stdint.h> | 31 #include <stdint.h> |
2 #include <protobuf.h> | 32 #include <protobuf.h> |
| 33 #include <Zend/zend.h> |
| 34 |
| 35 #include "utf8.h" |
3 | 36 |
4 // ----------------------------------------------------------------------------- | 37 // ----------------------------------------------------------------------------- |
5 // PHP <-> native slot management. | 38 // Native slot storage. |
6 // ----------------------------------------------------------------------------- | 39 // ----------------------------------------------------------------------------- |
7 | 40 |
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 | |
17 #define DEREF(memory, type) *(type*)(memory) | 41 #define DEREF(memory, type) *(type*)(memory) |
18 | 42 |
19 size_t native_slot_size(upb_fieldtype_t type) { | 43 size_t native_slot_size(upb_fieldtype_t type) { |
20 switch (type) { | 44 switch (type) { |
21 case UPB_TYPE_FLOAT: return 4; | 45 case UPB_TYPE_FLOAT: return 4; |
22 case UPB_TYPE_DOUBLE: return 8; | 46 case UPB_TYPE_DOUBLE: return 8; |
23 case UPB_TYPE_BOOL: return 1; | 47 case UPB_TYPE_BOOL: return 1; |
24 case UPB_TYPE_STRING: return sizeof(zval*); | 48 case UPB_TYPE_STRING: return sizeof(void*); |
25 case UPB_TYPE_BYTES: return sizeof(zval*); | 49 case UPB_TYPE_BYTES: return sizeof(void*); |
26 case UPB_TYPE_MESSAGE: return sizeof(zval*); | 50 case UPB_TYPE_MESSAGE: return sizeof(void*); |
27 case UPB_TYPE_ENUM: return 4; | 51 case UPB_TYPE_ENUM: return 4; |
28 case UPB_TYPE_INT32: return 4; | 52 case UPB_TYPE_INT32: return 4; |
29 case UPB_TYPE_INT64: return 8; | 53 case UPB_TYPE_INT64: return 8; |
30 case UPB_TYPE_UINT32: return 4; | 54 case UPB_TYPE_UINT32: return 4; |
31 case UPB_TYPE_UINT64: return 8; | 55 case UPB_TYPE_UINT64: return 8; |
32 default: return 0; | 56 default: return 0; |
33 } | 57 } |
34 } | 58 } |
35 | 59 |
36 static bool is_php_num(zval* value) { | 60 bool native_slot_set(upb_fieldtype_t type, const zend_class_entry* klass, |
37 // Is numerial string also valid? | 61 void* memory, zval* value TSRMLS_DC) { |
38 return (Z_TYPE_P(value) == IS_LONG || | 62 switch (type) { |
39 Z_TYPE_P(value) == IS_DOUBLE); | 63 case UPB_TYPE_STRING: |
| 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; |
40 } | 127 } |
41 | 128 |
42 void native_slot_check_int_range_precision(upb_fieldtype_t type, zval* val) { | 129 void native_slot_init(upb_fieldtype_t type, void* memory, zval** cache) { |
43 // TODO(teboring): Add it back. | 130 zval* tmp = NULL; |
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) { | |
102 switch (type) { | 131 switch (type) { |
103 case UPB_TYPE_FLOAT: | 132 case UPB_TYPE_FLOAT: |
104 DEREF(memory, float) = 0.0; | 133 DEREF(memory, float) = 0.0; |
105 break; | 134 break; |
106 case UPB_TYPE_DOUBLE: | 135 case UPB_TYPE_DOUBLE: |
107 DEREF(memory, double) = 0.0; | 136 DEREF(memory, double) = 0.0; |
108 break; | 137 break; |
109 case UPB_TYPE_BOOL: | 138 case UPB_TYPE_BOOL: |
110 DEREF(memory, int8_t) = 0; | 139 DEREF(memory, int8_t) = 0; |
111 break; | 140 break; |
112 // TODO(teboring): Add it back. | 141 case UPB_TYPE_STRING: |
113 // case UPB_TYPE_STRING: | 142 case UPB_TYPE_BYTES: |
114 // case UPB_TYPE_BYTES: | 143 case UPB_TYPE_MESSAGE: |
115 // DEREF(memory, VALUE) = php_str_new2(""); | 144 DEREF(memory, zval**) = cache; |
116 // php_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) | 145 break; |
117 // ? kRubyString8bitEncoding | |
118 // : kRubyStringUtf8Encoding); | |
119 // break; | |
120 // case UPB_TYPE_MESSAGE: | |
121 // DEREF(memory, VALUE) = Qnil; | |
122 // break; | |
123 case UPB_TYPE_ENUM: | 146 case UPB_TYPE_ENUM: |
124 case UPB_TYPE_INT32: | 147 case UPB_TYPE_INT32: |
125 DEREF(memory, int32_t) = 0; | 148 DEREF(memory, int32_t) = 0; |
126 break; | 149 break; |
127 case UPB_TYPE_INT64: | 150 case UPB_TYPE_INT64: |
128 DEREF(memory, int64_t) = 0; | 151 DEREF(memory, int64_t) = 0; |
129 break; | 152 break; |
130 case UPB_TYPE_UINT32: | 153 case UPB_TYPE_UINT32: |
131 DEREF(memory, uint32_t) = 0; | 154 DEREF(memory, uint32_t) = 0; |
132 break; | 155 break; |
133 case UPB_TYPE_UINT64: | 156 case UPB_TYPE_UINT64: |
134 DEREF(memory, uint64_t) = 0; | 157 DEREF(memory, uint64_t) = 0; |
135 break; | 158 break; |
136 default: | 159 default: |
137 break; | 160 break; |
138 } | 161 } |
139 } | 162 } |
140 | 163 |
141 void native_slot_set(upb_fieldtype_t type, /*VALUE type_class,*/ void* memory, | 164 void native_slot_get(upb_fieldtype_t type, const void* memory, |
142 zval* value) { | 165 zval** cache TSRMLS_DC) { |
143 native_slot_set_value_and_case(type, /*type_class,*/ memory, value, NULL, 0); | 166 switch (type) { |
144 } | 167 #define CASE(upb_type, php_type, c_type) \ |
| 168 case UPB_TYPE_##upb_type: \ |
| 169 SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 170 ZVAL_##php_type(*cache, DEREF(memory, c_type)); \ |
| 171 return; |
145 | 172 |
146 void native_slot_set_value_and_case(upb_fieldtype_t type, /*VALUE type_class,*/ | 173 CASE(FLOAT, DOUBLE, float) |
147 void* memory, zval* value, | 174 CASE(DOUBLE, DOUBLE, double) |
148 uint32_t* case_memory, | 175 CASE(BOOL, BOOL, int8_t) |
149 uint32_t case_number) { | 176 CASE(INT32, LONG, int32_t) |
150 switch (type) { | 177 CASE(ENUM, LONG, uint32_t) |
151 case UPB_TYPE_FLOAT: | 178 |
152 if (!Z_TYPE_P(value) == IS_LONG) { | 179 #undef CASE |
153 zend_error(E_ERROR, "Expected number type for float field."); | 180 |
| 181 #if SIZEOF_LONG == 4 |
| 182 #define CASE(upb_type, c_type) \ |
| 183 case UPB_TYPE_##upb_type: { \ |
| 184 SEPARATE_ZVAL_IF_NOT_REF(cache); \ |
| 185 char buffer[MAX_LENGTH_OF_INT64]; \ |
| 186 sprintf(buffer, "%lld", DEREF(memory, c_type)); \ |
| 187 ZVAL_STRING(*cache, buffer, 1); \ |
| 188 return; \ |
| 189 } |
| 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); |
154 } | 209 } |
155 DEREF(memory, float) = Z_DVAL_P(value); | 210 ZVAL_LONG(*cache, value); |
156 break; | 211 return; |
157 case UPB_TYPE_DOUBLE: | |
158 // TODO(teboring): Add it back. | |
159 // if (!is_php_num(value)) { | |
160 // zend_error(E_ERROR, "Expected number type for double field."); | |
161 // } | |
162 // DEREF(memory, double) = Z_DVAL_P(value); | |
163 break; | |
164 case UPB_TYPE_BOOL: { | |
165 int8_t val = -1; | |
166 if (zval_is_true(value)) { | |
167 val = 1; | |
168 } else { | |
169 val = 0; | |
170 } | |
171 // TODO(teboring): Add it back. | |
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; | |
180 } | 212 } |
| 213 |
181 case UPB_TYPE_STRING: | 214 case UPB_TYPE_STRING: |
182 case UPB_TYPE_BYTES: { | 215 case UPB_TYPE_BYTES: { |
183 // TODO(teboring): Add it back. | 216 // For optional string/bytes fields, the cache is owned by the containing |
184 // if (Z_TYPE_P(value) != IS_STRING) { | 217 // message and should have been updated during setting/decoding. However, |
185 // zend_error(E_ERROR, "Invalid argument for string field."); | 218 // for repeated string/bytes fields, the cache is provided by zend engine |
186 // } | 219 // and has not been updated. |
187 // native_slot_validate_string_encoding(type, value); | 220 zval* value = DEREF(memory, zval*); |
188 // DEREF(memory, zval*) = value; | 221 if (*cache != value) { |
| 222 ZVAL_STRINGL(*cache, Z_STRVAL_P(value), Z_STRLEN_P(value), 1); |
| 223 } |
189 break; | 224 break; |
190 } | 225 } |
191 case UPB_TYPE_MESSAGE: { | 226 case UPB_TYPE_MESSAGE: { |
192 // TODO(teboring): Add it back. | 227 // Same as above for string/bytes fields. |
193 // if (CLASS_OF(value) == CLASS_OF(Qnil)) { | 228 zval* value = DEREF(memory, zval*); |
194 // value = Qnil; | 229 if (*cache != value) { |
195 // } else if (CLASS_OF(value) != type_class) { | 230 ZVAL_ZVAL(*cache, value, 1, 0); |
196 // php_raise(php_eTypeError, | 231 } |
197 // "Invalid type %s to assign to submessage field.", | 232 return; |
198 // php_class2name(CLASS_OF(value))); | |
199 // } | |
200 // DEREF(memory, VALUE) = value; | |
201 break; | |
202 } | 233 } |
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; | |
249 } | |
250 break; | |
251 default: | 234 default: |
252 break; | 235 return; |
253 } | |
254 | |
255 if (case_memory != NULL) { | |
256 *case_memory = case_number; | |
257 } | 236 } |
258 } | 237 } |
259 | 238 |
| 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 |
260 // ----------------------------------------------------------------------------- | 274 // ----------------------------------------------------------------------------- |
261 // Map field utilities. | 275 // Map field utilities. |
262 // ---------------------------------------------------------------------------- | 276 // ---------------------------------------------------------------------------- |
263 | 277 |
264 const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) { | 278 const upb_msgdef* tryget_map_entry_msgdef(const upb_fielddef* field) { |
265 const upb_msgdef* subdef; | 279 const upb_msgdef* subdef; |
266 if (upb_fielddef_label(field) != UPB_LABEL_REPEATED || | 280 if (upb_fielddef_label(field) != UPB_LABEL_REPEATED || |
267 upb_fielddef_type(field) != UPB_TYPE_MESSAGE) { | 281 upb_fielddef_type(field) != UPB_TYPE_MESSAGE) { |
268 return NULL; | 282 return NULL; |
269 } | 283 } |
270 subdef = upb_fielddef_msgsubdef(field); | 284 subdef = upb_fielddef_msgsubdef(field); |
271 return upb_msgdef_mapentry(subdef) ? subdef : NULL; | 285 return upb_msgdef_mapentry(subdef) ? subdef : NULL; |
272 } | 286 } |
273 | 287 |
274 const upb_msgdef* map_entry_msgdef(const upb_fielddef* field) { | 288 const upb_msgdef* map_entry_msgdef(const upb_fielddef* field) { |
275 const upb_msgdef* subdef = tryget_map_entry_msgdef(field); | 289 const upb_msgdef* subdef = tryget_map_entry_msgdef(field); |
276 assert(subdef); | 290 assert(subdef); |
277 return subdef; | 291 return subdef; |
278 } | 292 } |
279 | 293 |
280 bool is_map_field(const upb_fielddef* field) { | 294 bool is_map_field(const upb_fielddef* field) { |
281 return tryget_map_entry_msgdef(field) != NULL; | 295 return tryget_map_entry_msgdef(field) != NULL; |
282 } | 296 } |
283 | 297 |
| 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 |
284 // ----------------------------------------------------------------------------- | 333 // ----------------------------------------------------------------------------- |
285 // Memory layout management. | 334 // Memory layout management. |
286 // ----------------------------------------------------------------------------- | 335 // ----------------------------------------------------------------------------- |
287 | 336 |
288 static size_t align_up_to(size_t offset, size_t granularity) { | 337 static size_t align_up_to(size_t offset, size_t granularity) { |
289 // Granularity must be a power of two. | 338 // Granularity must be a power of two. |
290 return (offset + granularity - 1) & ~(granularity - 1); | 339 return (offset + granularity - 1) & ~(granularity - 1); |
291 } | 340 } |
292 | 341 |
| 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 |
293 MessageLayout* create_layout(const upb_msgdef* msgdef) { | 358 MessageLayout* create_layout(const upb_msgdef* msgdef) { |
294 MessageLayout* layout = ALLOC(MessageLayout); | 359 MessageLayout* layout = ALLOC(MessageLayout); |
295 int nfields = upb_msgdef_numfields(msgdef); | 360 int nfields = upb_msgdef_numfields(msgdef); |
296 upb_msg_field_iter it; | 361 upb_msg_field_iter it; |
297 upb_msg_oneof_iter oit; | 362 upb_msg_oneof_iter oit; |
298 size_t off = 0; | 363 size_t off = 0; |
| 364 int i = 0; |
299 | 365 |
300 layout->fields = ALLOC_N(MessageField, nfields); | 366 layout->fields = ALLOC_N(MessageField, nfields); |
301 | 367 |
302 for (upb_msg_field_begin(&it, msgdef); !upb_msg_field_done(&it); | 368 for (upb_msg_field_begin(&it, msgdef); !upb_msg_field_done(&it); |
303 upb_msg_field_next(&it)) { | 369 upb_msg_field_next(&it)) { |
304 const upb_fielddef* field = upb_msg_iter_field(&it); | 370 const upb_fielddef* field = upb_msg_iter_field(&it); |
305 size_t field_size; | 371 size_t field_size; |
306 | 372 |
307 if (upb_fielddef_containingoneof(field)) { | 373 if (upb_fielddef_containingoneof(field)) { |
308 // Oneofs are handled separately below. | 374 // Oneofs are handled separately below. |
309 continue; | 375 continue; |
310 } | 376 } |
311 | 377 |
312 // Allocate |field_size| bytes for this field in the layout. | 378 // Allocate |field_size| bytes for this field in the layout. |
313 field_size = 0; | 379 field_size = 0; |
314 if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { | 380 if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
315 field_size = sizeof(zval*); | 381 field_size = sizeof(zval*); |
316 } else { | 382 } else { |
317 field_size = native_slot_size(upb_fielddef_type(field)); | 383 field_size = native_slot_size(upb_fielddef_type(field)); |
318 } | 384 } |
319 | 385 |
320 // Align current offset up to | size | granularity. | 386 // Align current offset up to | size | granularity. |
321 off = align_up_to(off, field_size); | 387 off = align_up_to(off, field_size); |
322 layout->fields[upb_fielddef_index(field)].offset = off; | 388 layout->fields[upb_fielddef_index(field)].offset = off; |
323 layout->fields[upb_fielddef_index(field)].case_offset = | 389 layout->fields[upb_fielddef_index(field)].case_offset = |
324 MESSAGE_FIELD_NO_CASE; | 390 MESSAGE_FIELD_NO_CASE; |
| 391 layout->fields[upb_fielddef_index(field)].cache_index = i++; |
325 off += field_size; | 392 off += field_size; |
326 } | 393 } |
327 | 394 |
328 // Handle oneofs now -- we iterate over oneofs specifically and allocate only | 395 // Handle oneofs now -- we iterate over oneofs specifically and allocate only |
329 // one slot per oneof. | 396 // one slot per oneof. |
330 // | 397 // |
331 // We assign all value slots first, then pack the 'case' fields at the end, | 398 // We assign all value slots first, then pack the 'case' fields at the end, |
332 // since in the common case (modern 64-bit platform) these are 8 bytes and 4 | 399 // since in the common case (modern 64-bit platform) these are 8 bytes and 4 |
333 // bytes respectively and we want to avoid alignment overhead. | 400 // bytes respectively and we want to avoid alignment overhead. |
334 // | 401 // |
(...skipping 11 matching lines...) Expand all Loading... |
346 // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between | 413 // Always allocate NATIVE_SLOT_MAX_SIZE bytes, but share the slot between |
347 // all fields. | 414 // all fields. |
348 size_t field_size = NATIVE_SLOT_MAX_SIZE; | 415 size_t field_size = NATIVE_SLOT_MAX_SIZE; |
349 // Align the offset . | 416 // Align the offset . |
350 off = align_up_to( off, field_size); | 417 off = align_up_to( off, field_size); |
351 // Assign all fields in the oneof this same offset. | 418 // Assign all fields in the oneof this same offset. |
352 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); | 419 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); |
353 upb_oneof_next(&fit)) { | 420 upb_oneof_next(&fit)) { |
354 const upb_fielddef* field = upb_oneof_iter_field(&fit); | 421 const upb_fielddef* field = upb_oneof_iter_field(&fit); |
355 layout->fields[upb_fielddef_index(field)].offset = off; | 422 layout->fields[upb_fielddef_index(field)].offset = off; |
| 423 layout->fields[upb_fielddef_index(field)].cache_index = i; |
356 } | 424 } |
| 425 i++; |
357 off += field_size; | 426 off += field_size; |
358 } | 427 } |
359 | 428 |
360 // Now the case fields. | 429 // Now the case offset. |
361 for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit); | 430 for (upb_msg_oneof_begin(&oit, msgdef); !upb_msg_oneof_done(&oit); |
362 upb_msg_oneof_next(&oit)) { | 431 upb_msg_oneof_next(&oit)) { |
363 const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit); | 432 const upb_oneofdef* oneof = upb_msg_iter_oneof(&oit); |
364 upb_oneof_iter fit; | 433 upb_oneof_iter fit; |
365 | 434 |
366 size_t field_size = sizeof(uint32_t); | 435 size_t field_size = sizeof(uint32_t); |
367 // Align the offset . | 436 // Align the offset . |
368 off = (off + field_size - 1) & ~(field_size - 1); | 437 off = (off + field_size - 1) & ~(field_size - 1); |
369 // Assign all fields in the oneof this same offset. | 438 // Assign all fields in the oneof this same offset. |
370 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); | 439 for (upb_oneof_begin(&fit, oneof); !upb_oneof_done(&fit); |
(...skipping 11 matching lines...) Expand all Loading... |
382 | 451 |
383 return layout; | 452 return layout; |
384 } | 453 } |
385 | 454 |
386 void free_layout(MessageLayout* layout) { | 455 void free_layout(MessageLayout* layout) { |
387 FREE(layout->fields); | 456 FREE(layout->fields); |
388 upb_msgdef_unref(layout->msgdef, &layout->msgdef); | 457 upb_msgdef_unref(layout->msgdef, &layout->msgdef); |
389 FREE(layout); | 458 FREE(layout); |
390 } | 459 } |
391 | 460 |
392 // TODO(teboring): Add it back. | 461 void layout_init(MessageLayout* layout, void* storage, |
393 // VALUE field_type_class(const upb_fielddef* field) { | 462 zval** properties_table TSRMLS_DC) { |
394 // VALUE type_class = Qnil; | 463 int i; |
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) { | |
458 upb_msg_field_iter it; | 464 upb_msg_field_iter it; |
459 for (upb_msg_field_begin(&it, layout->msgdef); !upb_msg_field_done(&it); | 465 for (upb_msg_field_begin(&it, layout->msgdef), i = 0; !upb_msg_field_done(&it)
; |
460 upb_msg_field_next(&it)) { | 466 upb_msg_field_next(&it), i++) { |
461 const upb_fielddef* field = upb_msg_iter_field(&it); | 467 const upb_fielddef* field = upb_msg_iter_field(&it); |
462 void* memory = slot_memory(layout, storage, field); | 468 void* memory = slot_memory(layout, storage, field); |
463 uint32_t* oneof_case = slot_oneof_case(layout, storage, field); | 469 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]; |
464 | 472 |
465 if (upb_fielddef_containingoneof(field)) { | 473 if (upb_fielddef_containingoneof(field)) { |
466 // TODO(teboring): Add it back. | 474 memset(memory, 0, NATIVE_SLOT_MAX_SIZE); |
467 // memset(memory, 0, NATIVE_SLOT_MAX_SIZE); | 475 *oneof_case = ONEOF_CASE_NONE; |
468 // *oneof_case = ONEOF_CASE_NONE; | |
469 } else if (is_map_field(field)) { | 476 } else if (is_map_field(field)) { |
470 // TODO(teboring): Add it back. | 477 zval_ptr_dtor(property_ptr); |
471 // VALUE map = Qnil; | 478 map_field_create_with_type(map_field_type, field, property_ptr TSRMLS_CC); |
472 | 479 DEREF(memory, zval**) = property_ptr; |
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; | |
492 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { | 480 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
493 // TODO(teboring): Add it back. | 481 zval_ptr_dtor(property_ptr); |
494 // VALUE ary = Qnil; | 482 repeated_field_create_with_type(repeated_field_type, field, |
495 | 483 property_ptr TSRMLS_CC); |
496 // VALUE type_class = field_type_class(field); | 484 DEREF(memory, zval**) = property_ptr; |
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; | |
509 } else { | 485 } else { |
510 native_slot_init(upb_fielddef_type(field), memory); | 486 native_slot_init(upb_fielddef_type(field), memory, property_ptr); |
511 } | 487 } |
512 } | 488 } |
513 } | 489 } |
514 | 490 |
| 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 |
515 zval* layout_get(MessageLayout* layout, const void* storage, | 507 zval* layout_get(MessageLayout* layout, const void* storage, |
516 const upb_fielddef* field TSRMLS_DC) { | 508 const upb_fielddef* field, zval** cache TSRMLS_DC) { |
517 void* memory = slot_memory(layout, storage, field); | 509 void* memory = slot_memory(layout, storage, field); |
518 uint32_t* oneof_case = slot_oneof_case(layout, storage, field); | 510 uint32_t* oneof_case = slot_oneof_case(layout, storage, field); |
519 | 511 |
520 if (upb_fielddef_containingoneof(field)) { | 512 if (upb_fielddef_containingoneof(field)) { |
521 if (*oneof_case != upb_fielddef_number(field)) { | 513 if (*oneof_case != upb_fielddef_number(field)) { |
522 return NULL; | 514 native_slot_get_default(upb_fielddef_type(field), cache TSRMLS_CC); |
523 // TODO(teboring): Add it back. | 515 } else { |
524 // return Qnil; | 516 native_slot_get(upb_fielddef_type(field), value_memory(field, memory), |
| 517 cache TSRMLS_CC); |
525 } | 518 } |
526 return NULL; | 519 return *cache; |
527 // TODO(teboring): Add it back. | |
528 // return native_slot_get(upb_fielddef_type(field), field_type_class(field), | |
529 // memory); | |
530 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { | 520 } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) { |
531 return NULL; | 521 return *cache; |
532 // TODO(teboring): Add it back. | |
533 // return *((VALUE*)memory); | |
534 } else { | 522 } else { |
535 return native_slot_get( | 523 native_slot_get(upb_fielddef_type(field), value_memory(field, memory), |
536 upb_fielddef_type(field), /*field_type_class(field), */ | 524 cache TSRMLS_CC); |
537 memory TSRMLS_CC); | 525 return *cache; |
538 } | 526 } |
539 } | 527 } |
| 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 } |
OLD | NEW |