Skip to content

Commit 3cafa7f

Browse files
committed
Assign (un)serialize_func during compilation
This avoids writing this cache at runtime, which is illegal if preloading is used. Not every serialize/unserialize function actually belongs to the Serializable interface, but I think it's not a problem to assign these anyway -- whether they are used ultimately depends on whether Serializable is implemented. Alternatively it might make sense to just drop these entirely. I don't think this is performance critical functionality.
1 parent ffc7e95 commit 3cafa7f

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

Zend/zend_API.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_functio
20932093
int count=0, unload=0;
20942094
HashTable *target_function_table = function_table;
20952095
int error_type;
2096-
zend_function *ctor = NULL, *dtor = NULL, *clone = NULL, *__get = NULL, *__set = NULL, *__unset = NULL, *__isset = NULL, *__call = NULL, *__callstatic = NULL, *__tostring = NULL, *__debugInfo = NULL;
2096+
zend_function *ctor = NULL, *dtor = NULL, *clone = NULL, *__get = NULL, *__set = NULL, *__unset = NULL, *__isset = NULL, *__call = NULL, *__callstatic = NULL, *__tostring = NULL, *__debugInfo = NULL, *serialize_func = NULL, *unserialize_func = NULL;
20972097
zend_string *lowercase_name;
20982098
size_t fname_len;
20992099
const char *lc_class_name = NULL;
@@ -2268,6 +2268,10 @@ ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_functio
22682268
*/
22692269
if ((fname_len == class_name_len) && !ctor && !memcmp(ZSTR_VAL(lowercase_name), lc_class_name, class_name_len+1)) {
22702270
ctor = reg_function;
2271+
} else if (zend_string_equals_literal(lowercase_name, "serialize")) {
2272+
serialize_func = reg_function;
2273+
} else if (zend_string_equals_literal(lowercase_name, "unserialize")) {
2274+
unserialize_func = reg_function;
22712275
} else if (ZSTR_VAL(lowercase_name)[0] != '_' || ZSTR_VAL(lowercase_name)[1] != '_') {
22722276
reg_function = NULL;
22732277
} else if (zend_string_equals_literal(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
@@ -2339,6 +2343,8 @@ ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_functio
23392343
scope->__unset = __unset;
23402344
scope->__isset = __isset;
23412345
scope->__debugInfo = __debugInfo;
2346+
scope->serialize_func = serialize_func;
2347+
scope->unserialize_func = unserialize_func;
23422348
if (ctor) {
23432349
ctor->common.fn_flags |= ZEND_ACC_CTOR;
23442350
if (ctor->common.fn_flags & ZEND_ACC_STATIC) {

Zend/zend_compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5637,6 +5637,10 @@ void zend_begin_method_decl(zend_op_array *op_array, zend_string *name, zend_boo
56375637
if (!ce->constructor) {
56385638
ce->constructor = (zend_function *) op_array;
56395639
}
5640+
} else if (zend_string_equals_literal(lcname, "serialize")) {
5641+
ce->serialize_func = (zend_function *) op_array;
5642+
} else if (zend_string_equals_literal(lcname, "unserialize")) {
5643+
ce->unserialize_func = (zend_function *) op_array;
56405644
} else if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
56415645
if (!is_static) {
56425646
op_array->fn_flags |= ZEND_ACC_ALLOW_STATIC;

Zend/zend_inheritance.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,15 @@ static void do_inherit_parent_constructor(zend_class_entry *ce) /* {{{ */
124124
if (EXPECTED(!ce->clone)) {
125125
ce->clone = parent->clone;
126126
}
127+
if (EXPECTED(!ce->serialize_func)) {
128+
ce->serialize_func = parent->serialize_func;
129+
}
127130
if (EXPECTED(!ce->serialize)) {
128131
ce->serialize = parent->serialize;
129132
}
133+
if (EXPECTED(!ce->unserialize_func)) {
134+
ce->unserialize_func = parent->unserialize_func;
135+
}
130136
if (EXPECTED(!ce->unserialize)) {
131137
ce->unserialize = parent->unserialize;
132138
}
@@ -1287,7 +1293,11 @@ static void zend_do_implement_interfaces(zend_class_entry *ce) /* {{{ */
12871293

12881294
static void zend_add_magic_methods(zend_class_entry* ce, zend_string* mname, zend_function* fe) /* {{{ */
12891295
{
1290-
if (ZSTR_LEN(ce->name) != ZSTR_LEN(mname) && (ZSTR_VAL(mname)[0] != '_' || ZSTR_VAL(mname)[1] != '_')) {
1296+
if (zend_string_equals_literal(mname, "serialize")) {
1297+
ce->serialize_func = fe;
1298+
} else if (zend_string_equals_literal(mname, "unserialize")) {
1299+
ce->unserialize_func = fe;
1300+
} else if (ZSTR_LEN(ce->name) != ZSTR_LEN(mname) && (ZSTR_VAL(mname)[0] != '_' || ZSTR_VAL(mname)[1] != '_')) {
12911301
/* pass */
12921302
} else if (zend_string_equals_literal(mname, ZEND_CLONE_FUNC_NAME)) {
12931303
ce->clone = fe;

0 commit comments

Comments
 (0)