studlyCaps patch

From: Date: Fri, 12 Sep 2003 11:05:34 +0000
Subject: studlyCaps patch
Groups: php.internals 
Request: Send a blank email to internals+get-4454@lists.php.net to get a copy of this message
Hello internals, after last discussion on this list regarding PHP's naming convention for OOCode i took a deeper look at fixing my main concerns. That is error messages, backtraces and reflection show lowercased method names. However this is easily changeable; with a minor performance decrease at compiletime when handling methods (not functions). The attached patch now changes two files for two different purposes: 1) Zend/zend_compile.c, These changes allow to show the original function/method names in errors, backtraces and reflection. This works for internal as well as for user functions/methods. 2) Zend/zend_default_classes.c, changes the method names of the internal exception class to studlyCaps naming convention. The patches were already tested by some other developers and we found out thefollowing things: 1) Some few error messsages show the new method name but should show the old method. For example when trying to override a final method the new name is shown instead of the old one which the message states. 2) Errors for non existing functions are still shown lowercased. This could be changed too. But the performance decrease is higher so i wouldn't recommend that. I think decision whether to incorporate this patch is up to Zeev and Andi. -- Best regards, Marcus mailto:helly@php.net

Index: Zend/zend_compile.c =================================================================== RCS file: /repository/ZendEngine2/zend_compile.c,v retrieving revision 1.482 diff -u -p -d -r1.482 zend_compile.c --- Zend/zend_compile.c 8 Sep 2003 23:25:57 -0000 1.482 +++ Zend/zend_compile.c 12 Sep 2003 10:42:24 -0000 @@ -963,6 +963,7 @@ void zend_do_begin_function_declaration( int name_len = function_name->u.constant.value.str.len; int function_begin_line = function_token->u.opline_num; zend_uint fn_flags; + char *lcname; if (is_method) { if (CG(active_class_entry)->ce_flags & ZEND_ACC_INTERFACE) { @@ -977,7 +978,7 @@ void zend_do_begin_function_declaration( } function_token->u.op_array = CG(active_op_array); - zend_str_tolower(name, name_len); + lcname = zend_str_tolower_dup(name, name_len); init_op_array(&op_array, ZEND_USER_FUNCTION, INITIAL_OP_ARRAY_SIZE TSRMLS_CC); @@ -1002,7 +1003,7 @@ void zend_do_begin_function_declaration( short_class_name_length = strlen(short_class_name); } } - if (zend_hash_add(&CG(active_class_entry)->function_table, name, name_len+1, &op_array, sizeof(zend_op_array), (void **) &CG(active_op_array)) == FAILURE) { + if (zend_hash_add(&CG(active_class_entry)->function_table, lcname, name_len+1, &op_array, sizeof(zend_op_array), (void **) &CG(active_op_array)) == FAILURE) { zend_op_array *child_op_array, *parent_op_array; if (CG(active_class_entry)->parent && (zend_hash_find(&CG(active_class_entry)->function_table, name, name_len+1, (void **) &child_op_array) == SUCCESS) @@ -1010,6 +1011,7 @@ void zend_do_begin_function_declaration( && (child_op_array == parent_op_array)) { zend_hash_update(&CG(active_class_entry)->function_table, name, name_len+1, &op_array, sizeof(zend_op_array), (void **) &CG(active_op_array)); } else { + efree(lcname); zend_error(E_COMPILE_ERROR, "Cannot redeclare %s::%s()", CG(active_class_entry)->name, name); } } @@ -1022,30 +1024,32 @@ void zend_do_begin_function_declaration( fn_flags |= ZEND_ACC_PUBLIC; } - if ((short_class_name_length == name_len) && (!memcmp(short_class_name, name, name_len)) && !CG(active_class_entry)->constructor) { + if ((short_class_name_length == name_len) && (!memcmp(short_class_name, lcname, name_len)) && !CG(active_class_entry)->constructor) { CG(active_class_entry)->constructor = (zend_function *) CG(active_op_array); - } else if ((function_name->u.constant.value.str.len == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) && (!memcmp(function_name->u.constant.value.str.val, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)))) { + } else if ((name_len == sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CONSTRUCTOR_FUNC_NAME, sizeof(ZEND_CONSTRUCTOR_FUNC_NAME)))) { CG(active_class_entry)->constructor = (zend_function *) CG(active_op_array); - } else if ((function_name->u.constant.value.str.len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME)-1) && (!memcmp(function_name->u.constant.value.str.val, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME)))) { + } else if ((name_len == sizeof(ZEND_DESTRUCTOR_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_DESTRUCTOR_FUNC_NAME, sizeof(ZEND_DESTRUCTOR_FUNC_NAME)))) { CG(active_class_entry)->destructor = (zend_function *) CG(active_op_array); - } else if ((function_name->u.constant.value.str.len == sizeof(ZEND_CLONE_FUNC_NAME)-1) && (!memcmp(function_name->u.constant.value.str.val, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME)))) { + } else if ((name_len == sizeof(ZEND_CLONE_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CLONE_FUNC_NAME, sizeof(ZEND_CLONE_FUNC_NAME)))) { CG(active_class_entry)->clone = (zend_function *) CG(active_op_array); - } else if ((function_name->u.constant.value.str.len == sizeof(ZEND_CALL_FUNC_NAME)-1) && (!memcmp(function_name->u.constant.value.str.val, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME)))) { + } else if ((name_len == sizeof(ZEND_CALL_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_CALL_FUNC_NAME, sizeof(ZEND_CALL_FUNC_NAME)))) { CG(active_class_entry)->__call = (zend_function *) CG(active_op_array); - } else if ((function_name->u.constant.value.str.len == sizeof(ZEND_GET_FUNC_NAME)-1) && (!memcmp(function_name->u.constant.value.str.val, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME)))) { + } else if ((name_len == sizeof(ZEND_GET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_GET_FUNC_NAME, sizeof(ZEND_GET_FUNC_NAME)))) { CG(active_class_entry)->__get = (zend_function *) CG(active_op_array); - } else if ((function_name->u.constant.value.str.len == sizeof(ZEND_SET_FUNC_NAME)-1) && (!memcmp(function_name->u.constant.value.str.val, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME)))) { + } else if ((name_len == sizeof(ZEND_SET_FUNC_NAME)-1) && (!memcmp(lcname, ZEND_SET_FUNC_NAME, sizeof(ZEND_SET_FUNC_NAME)))) { CG(active_class_entry)->__set = (zend_function *) CG(active_op_array); } + + efree(lcname); } else { zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); opline->opcode = ZEND_DECLARE_FUNCTION; opline->op1.op_type = IS_CONST; - build_runtime_defined_function_key(&opline->op1.u.constant, function_name->u.constant.value.str.val, function_name->u.constant.value.str.len, opline TSRMLS_CC); + build_runtime_defined_function_key(&opline->op1.u.constant, lcname, name_len, opline TSRMLS_CC); opline->op2.op_type = IS_CONST; opline->op2.u.constant.type = IS_STRING; - opline->op2.u.constant.value.str.val = estrndup(name, name_len); + opline->op2.u.constant.value.str.val = lcname; opline->op2.u.constant.value.str.len = name_len; opline->op2.u.constant.refcount = 1; opline->extended_value = ZEND_DECLARE_FUNCTION; Index: Zend/zend_default_classes.c =================================================================== RCS file: /repository/ZendEngine2/zend_default_classes.c,v retrieving revision 1.38 diff -u -p -d -r1.38 zend_default_classes.c --- Zend/zend_default_classes.c 11 Sep 2003 10:26:47 -0000 1.38 +++ Zend/zend_default_classes.c 12 Sep 2003 10:42:25 -0000 @@ -89,35 +89,35 @@ static void _default_exception_get_entry zval_copy_ctor(return_value); } -ZEND_METHOD(exception, getfile) +ZEND_METHOD(exception, getFile) { DEFAULT_0_PARAMS; _default_exception_get_entry(getThis(), "file", sizeof("file")-1, return_value TSRMLS_CC); } -ZEND_METHOD(exception, getline) +ZEND_METHOD(exception, getLine) { DEFAULT_0_PARAMS; _default_exception_get_entry(getThis(), "line", sizeof("line")-1, return_value TSRMLS_CC); } -ZEND_METHOD(exception, getmessage) +ZEND_METHOD(exception, getMessage) { DEFAULT_0_PARAMS; _default_exception_get_entry(getThis(), "message", sizeof("message")-1, return_value TSRMLS_CC); } -ZEND_METHOD(exception, getcode) +ZEND_METHOD(exception, getCode) { DEFAULT_0_PARAMS; _default_exception_get_entry(getThis(), "code", sizeof("code")-1, return_value TSRMLS_CC); } -ZEND_METHOD(exception, gettrace) +ZEND_METHOD(exception, getTrace) { DEFAULT_0_PARAMS; @@ -276,7 +276,7 @@ static int _build_trace_string(zval **fr return ZEND_HASH_APPLY_KEEP; } -ZEND_METHOD(exception, gettraceasstring) +ZEND_METHOD(exception, getTraceAsString) { zval *trace; char *res = estrdup(""), **str = &res, *s_tmp; @@ -295,7 +295,7 @@ ZEND_METHOD(exception, gettraceasstring) } /* }}} */ -ZEND_METHOD(exception, tostring) +ZEND_METHOD(exception, toString) { zval *message, *file, *line, *trace; char *str; @@ -348,13 +348,13 @@ ZEND_METHOD(exception, tostring) */ static zend_function_entry default_exception_functions[] = { ZEND_ME(exception, __construct, NULL, 0) - ZEND_ME(exception, getmessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) - ZEND_ME(exception, getcode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) - ZEND_ME(exception, getfile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) - ZEND_ME(exception, getline, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) - ZEND_ME(exception, gettrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) - ZEND_ME(exception, gettraceasstring, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) - ZEND_ME(exception, tostring, NULL, 0) + ZEND_ME(exception, getMessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + ZEND_ME(exception, getCode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + ZEND_ME(exception, getFile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + ZEND_ME(exception, getLine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + ZEND_ME(exception, getTraceAsString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) + ZEND_ME(exception, toString, NULL, 0) {NULL, NULL, NULL} };
« previous php.internals (#4454) next »