public
Created

Scalar type hint/casting patch 2

  • Download Gist
gistfile1.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
Index: Zend/zend.h
===================================================================
--- Zend/zend.h (revision 323850)
+++ Zend/zend.h (working copy)
@@ -575,6 +575,15 @@
#define IS_CONSTANT_ARRAY 9
#define IS_CALLABLE 10
+/* Data type casts, hacked to be different from data type constants */
+#define IS_NULL_CAST 0x010
+#define IS_LONG_CAST 0x011
+#define IS_DOUBLE_CAST 0x012
+#define IS_BOOL_CAST 0x013
+#define IS_ARRAY_CAST 0x014
+#define IS_OBJECT_CAST 0x015
+#define IS_STRING_CAST 0x016
+
/* Ugly hack to support constants as static array indices */
#define IS_CONSTANT_TYPE_MASK 0x00f
#define IS_CONSTANT_UNQUALIFIED 0x010
Index: Zend/zend_execute.c
===================================================================
--- Zend/zend_execute.c (revision 323850)
+++ Zend/zend_execute.c (working copy)
@@ -597,12 +597,29 @@
return 0;
}
-static inline int zend_verify_arg_type(zend_function *zf, zend_uint arg_num, zval *arg, ulong fetch_type TSRMLS_DC)
+/* We need an inline function here to wrap the convert_to_string MACRO to be usable in place of all the other convert_to_* functions */
+static inline void zend_convert_to_string_cast(zval *op) {
+ convert_to_string(op);
+}
+
+static inline int zend_verify_arg_type_cast(zend_function *zf, zend_uint arg_num, zend_arg_info *cur_arg_info, zval **arg, int type, char *msg, void (*convert_func)(zval *)) {
+ if (!*arg) {
+ return zend_verify_arg_error(E_WARNING, zf, arg_num, msg, "", "none", "" TSRMLS_CC);
+ } else if (type != Z_TYPE_PP(arg) && !(Z_TYPE_PP(arg) == IS_NULL && cur_arg_info->allow_null)) {
+ if (Z_ISREF_PP(arg)) {
+ return zend_verify_arg_error(E_CORE_ERROR, zf, arg_num, "cannot cast referenced parameter", "", zend_zval_type_name(*arg), "" TSRMLS_CC);
+ }
+ SEPARATE_ZVAL(arg);
+ (*convert_func)(*arg);
+ }
+ return SUCCESS;
+}
+
+static inline int zend_verify_arg_type(zend_function *zf, zend_uint arg_num, zval **arg, ulong fetch_type TSRMLS_DC)
{
zend_arg_info *cur_arg_info;
char *need_msg;
zend_class_entry *ce;
-
if (!zf->common.arg_info
|| arg_num>zf->common.num_args) {
return 1;
@@ -613,40 +630,50 @@
if (cur_arg_info->class_name) {
const char *class_name;
- if (!arg) {
+ if (!*arg) {
need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "none", "" TSRMLS_CC);
}
- if (Z_TYPE_P(arg) == IS_OBJECT) {
+ if (Z_TYPE_PP(arg) == IS_OBJECT) {
need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
- if (!ce || !instanceof_function(Z_OBJCE_P(arg), ce TSRMLS_CC)) {
- return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "instance of ", Z_OBJCE_P(arg)->name TSRMLS_CC);
+ if (!ce || !instanceof_function(Z_OBJCE_PP(arg), ce TSRMLS_CC)) {
+ return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, "instance of ", Z_OBJCE_PP(arg)->name TSRMLS_CC);
}
- } else if (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null) {
+ } else if (Z_TYPE_PP(arg) != IS_NULL || !cur_arg_info->allow_null) {
need_msg = zend_verify_arg_class_kind(cur_arg_info, fetch_type, &class_name, &ce TSRMLS_CC);
- return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, zend_zval_type_name(arg), "" TSRMLS_CC);
+ return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, need_msg, class_name, zend_zval_type_name(*arg), "" TSRMLS_CC);
}
} else if (cur_arg_info->type_hint) {
switch(cur_arg_info->type_hint) {
case IS_ARRAY:
- if (!arg) {
+ if (!*arg) {
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", "none", "" TSRMLS_CC);
}
- if (Z_TYPE_P(arg) != IS_ARRAY && (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null)) {
- return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", zend_zval_type_name(arg), "" TSRMLS_CC);
+ if (Z_TYPE_PP(arg) != IS_ARRAY && (Z_TYPE_PP(arg) != IS_NULL || !cur_arg_info->allow_null)) {
+ return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be of the type array", "", zend_zval_type_name(*arg), "" TSRMLS_CC);
}
break;
-
case IS_CALLABLE:
- if (!arg) {
+ if (!*arg) {
return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be callable", "", "none", "" TSRMLS_CC);
}
- if (!zend_is_callable(arg, IS_CALLABLE_CHECK_SILENT, NULL TSRMLS_CC) && (Z_TYPE_P(arg) != IS_NULL || !cur_arg_info->allow_null)) {
- return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be callable", "", zend_zval_type_name(arg), "" TSRMLS_CC);
+ if (!zend_is_callable(*arg, IS_CALLABLE_CHECK_SILENT, NULL TSRMLS_CC) && (Z_TYPE_PP(arg) != IS_NULL || !cur_arg_info->allow_null)) {
+ return zend_verify_arg_error(E_RECOVERABLE_ERROR, zf, arg_num, "be callable", "", zend_zval_type_name(*arg), "" TSRMLS_CC);
}
break;
-
+ case IS_DOUBLE_CAST:
+ return zend_verify_arg_type_cast(zf, arg_num, cur_arg_info, arg, IS_DOUBLE, "be of the type float", &convert_to_double);
+ case IS_BOOL_CAST:
+ return zend_verify_arg_type_cast(zf, arg_num, cur_arg_info, arg, IS_BOOL, "be of the type boolean", &convert_to_boolean);
+ case IS_STRING_CAST:
+ return zend_verify_arg_type_cast(zf, arg_num, cur_arg_info, arg, IS_STRING, "be of the type string", &zend_convert_to_string_cast);
+ case IS_LONG_CAST:
+ return zend_verify_arg_type_cast(zf, arg_num, cur_arg_info, arg, IS_LONG, "be of the type integer", &convert_to_long);
+ case IS_ARRAY_CAST:
+ return zend_verify_arg_type_cast(zf, arg_num, cur_arg_info, arg, IS_ARRAY, "be of the type array", &convert_to_array);
+ case IS_OBJECT_CAST:
+ return zend_verify_arg_type_cast(zf, arg_num, cur_arg_info, arg, IS_OBJECT, "be of the type object", &convert_to_object);
default:
zend_error(E_ERROR, "Unknown typehint");
}
Index: Zend/zend_execute.h
===================================================================
--- Zend/zend_execute.h (revision 323850)
+++ Zend/zend_execute.h (working copy)
@@ -180,6 +180,7 @@
} \
} while (0)
+
static zend_always_inline zend_vm_stack zend_vm_stack_new_page(int count) {
zend_vm_stack page = (zend_vm_stack)emalloc(ZEND_MM_ALIGNED_SIZE(sizeof(*page)) + sizeof(void*) * count);
Index: Zend/zend_compile.c
===================================================================
--- Zend/zend_compile.c (revision 323850)
+++ Zend/zend_compile.c (working copy)
@@ -1822,6 +1822,20 @@
}
/* }}} */
+static inline void zend_init_type_casted_arg(zend_uchar op, int type_cast, zend_arg_info *cur_arg_info, const znode *initialization, zend_uchar pass_by_reference, char *default_error, char *reference_error) {
+ cur_arg_info->type_hint = type_cast;
+ if (op == ZEND_RECV_INIT) {
+ if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
+ cur_arg_info->allow_null = 1;
+ } else if (Z_TYPE(initialization->u.constant) != (type_cast & 0x0F)) {
+ zend_error(E_COMPILE_ERROR, default_error);
+ }
+ }
+ if (pass_by_reference) {
+ zend_error(E_COMPILE_ERROR, reference_error);
+ }
+}
+
void zend_do_receive_arg(zend_uchar op, znode *varname, const znode *offset, const znode *initialization, znode *class_type, zend_uchar pass_by_reference TSRMLS_DC) /* {{{ */
{
zend_op *opline;
@@ -1880,8 +1894,8 @@
cur_arg_info->allow_null = 0;
if (class_type->u.constant.type != IS_NULL) {
- if (class_type->u.constant.type == IS_ARRAY) {
- cur_arg_info->type_hint = IS_ARRAY;
+ if (class_type->u.constant.type == IS_ARRAY || class_type->u.constant.type == IS_ARRAY_CAST) {
+ cur_arg_info->type_hint = class_type->u.constant.type;
if (op == ZEND_RECV_INIT) {
if (Z_TYPE(initialization->u.constant) == IS_NULL || (Z_TYPE(initialization->u.constant) == IS_CONSTANT && !strcasecmp(Z_STRVAL(initialization->u.constant), "NULL"))) {
cur_arg_info->allow_null = 1;
@@ -1889,6 +1903,59 @@
zend_error(E_COMPILE_ERROR, "Default value for parameters with array type hint can only be an array or NULL");
}
}
+ if (pass_by_reference && class_type->u.constant.type == IS_ARRAY_CAST) {
+ zend_error(E_COMPILE_ERROR, "Parameters with array cast type hint cannot be passed by reference");
+ }
+ } else if (class_type->u.constant.type == IS_LONG_CAST) {
+ zend_init_type_casted_arg(
+ op,
+ IS_LONG_CAST,
+ cur_arg_info,
+ initialization,
+ pass_by_reference,
+ "Default value for parameters with int type hint can only be INT or NULL",
+ "Parameters with integer cast hint cannot be passed by reference"
+ );
+ } else if (class_type->u.constant.type == IS_DOUBLE_CAST) {
+ zend_init_type_casted_arg(
+ op,
+ IS_DOUBLE_CAST,
+ cur_arg_info,
+ initialization,
+ pass_by_reference,
+ "Default value for parameters with float type hint can only be FLOAT or NULL",
+ "Parameters with float cast hint cannot be passed by reference"
+ );
+ } else if (class_type->u.constant.type == IS_BOOL_CAST) {
+ zend_init_type_casted_arg(
+ op,
+ IS_BOOL_CAST,
+ cur_arg_info,
+ initialization,
+ pass_by_reference,
+ "Default value for parameters with boolean type hint can only be BOOL or NULL",
+ "Parameters with boolean cast hint cannot be passed by reference"
+ );
+ } else if (class_type->u.constant.type == IS_STRING_CAST) {
+ zend_init_type_casted_arg(
+ op,
+ IS_STRING_CAST,
+ cur_arg_info,
+ initialization,
+ pass_by_reference,
+ "Default value for parameters with string type hint can only be STRING or NULL",
+ "Parameters with string cast hint cannot be passed by reference"
+ );
+ } else if (class_type->u.constant.type == IS_OBJECT_CAST) {
+ zend_init_type_casted_arg(
+ op,
+ IS_OBJECT_CAST,
+ cur_arg_info,
+ initialization,
+ pass_by_reference,
+ "Default value for parameters with object type hint can only be NULL",
+ "Parameters with object cast hint cannot be passed by reference"
+ );
} else if (class_type->u.constant.type == IS_CALLABLE) {
cur_arg_info->type_hint = IS_CALLABLE;
if (op == ZEND_RECV_INIT) {
Index: Zend/zend_language_parser.y
===================================================================
--- Zend/zend_language_parser.y (revision 323850)
+++ Zend/zend_language_parser.y (working copy)
@@ -508,7 +508,6 @@
| /* empty */
;
-
non_empty_parameter_list:
optional_class_type T_VARIABLE { $$.op_type = IS_UNUSED; $$.u.op.num=1; zend_do_receive_arg(ZEND_RECV, &$2, &$$, NULL, &$1, 0 TSRMLS_CC); }
| optional_class_type '&' T_VARIABLE { $$.op_type = IS_UNUSED; $$.u.op.num=1; zend_do_receive_arg(ZEND_RECV, &$3, &$$, NULL, &$1, 1 TSRMLS_CC); }
@@ -525,10 +524,15 @@
/* empty */ { $$.op_type = IS_UNUSED; }
| T_ARRAY { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_ARRAY; }
| T_CALLABLE { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_CALLABLE; }
+ | T_ARRAY_CAST { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_ARRAY_CAST; }
+ | T_BOOL_CAST { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_BOOL_CAST; }
+ | T_INT_CAST { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_LONG_CAST; }
+ | T_DOUBLE_CAST { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_DOUBLE_CAST; }
+ | T_STRING_CAST { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_STRING_CAST; }
+ | T_OBJECT_CAST { $$.op_type = IS_CONST; Z_TYPE($$.u.constant)=IS_OBJECT_CAST; }
| fully_qualified_class_name { $$ = $1; }
;
-
function_call_parameter_list:
non_empty_function_call_parameter_list { $$ = $1; }
| /* empty */ { Z_LVAL($$.u.constant) = 0; }
Index: Zend/zend_vm_def.h
===================================================================
--- Zend/zend_vm_def.h (revision 323850)
+++ Zend/zend_vm_def.h (working copy)
@@ -2676,7 +2676,7 @@
ulong arg_count = opline->extended_value;
while (arg_count>0) {
- zend_verify_arg_type(fbc, ++i, *(p-arg_count), 0 TSRMLS_CC);
+ zend_verify_arg_type(fbc, ++i, (p-arg_count), 0 TSRMLS_CC);
arg_count--;
}
}
@@ -3202,7 +3202,7 @@
} else {
zval **var_ptr;
- zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, *param, opline->extended_value TSRMLS_CC);
+ zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, param, opline->extended_value TSRMLS_CC);
var_ptr = _get_zval_ptr_ptr_cv_BP_VAR_W(EX_CVs(), opline->result.var TSRMLS_CC);
Z_DELREF_PP(var_ptr);
*var_ptr = *param;
@@ -3238,7 +3238,7 @@
Z_ADDREF_P(assignment_value);
}
- zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, assignment_value, opline->extended_value TSRMLS_CC);
+ zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, &assignment_value, opline->extended_value TSRMLS_CC);
var_ptr = _get_zval_ptr_ptr_cv_BP_VAR_W(EX_CVs(), opline->result.var TSRMLS_CC);
Z_DELREF_PP(var_ptr);
*var_ptr = assignment_value;
Index: Zend/zend_vm_execute.h
===================================================================
--- Zend/zend_vm_execute.h (revision 323850)
+++ Zend/zend_vm_execute.h (working copy)
@@ -632,7 +632,7 @@
ulong arg_count = opline->extended_value;
while (arg_count>0) {
- zend_verify_arg_type(fbc, ++i, *(p-arg_count), 0 TSRMLS_CC);
+ zend_verify_arg_type(fbc, ++i, (p-arg_count), 0 TSRMLS_CC);
arg_count--;
}
}
@@ -782,7 +782,7 @@
} else {
zval **var_ptr;
- zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, *param, opline->extended_value TSRMLS_CC);
+ zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, param, opline->extended_value TSRMLS_CC);
var_ptr = _get_zval_ptr_ptr_cv_BP_VAR_W(EX_CVs(), opline->result.var TSRMLS_CC);
Z_DELREF_PP(var_ptr);
*var_ptr = *param;
@@ -1366,7 +1366,7 @@
Z_ADDREF_P(assignment_value);
}
- zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, assignment_value, opline->extended_value TSRMLS_CC);
+ zend_verify_arg_type((zend_function *) EG(active_op_array), arg_num, &assignment_value, opline->extended_value TSRMLS_CC);
var_ptr = _get_zval_ptr_ptr_cv_BP_VAR_W(EX_CVs(), opline->result.var TSRMLS_CC);
Z_DELREF_PP(var_ptr);
*var_ptr = assignment_value;
Index: Zend/zend_API.c
===================================================================
--- Zend/zend_API.c (revision 323850)
+++ Zend/zend_API.c (working copy)
@@ -205,22 +205,36 @@
switch(type) {
case IS_BOOL:
return "boolean";
+ case IS_BOOL_CAST:
+ return "(bool)";
case IS_LONG:
return "integer";
+ case IS_LONG_CAST:
+ return "(int)";
case IS_DOUBLE:
return "double";
+ case IS_DOUBLE_CAST:
+ return "(float)";
case IS_STRING:
return "string";
+ case IS_STRING_CAST:
+ return "(string)";
case IS_OBJECT:
return "object";
+ case IS_OBJECT_CAST:
+ return "(object)";
case IS_RESOURCE:
return "resource";
case IS_NULL:
return "null";
+ case IS_NULL_CAST:
+ return "(unset)";
case IS_CALLABLE:
return "callable";
case IS_ARRAY:
return "array";
+ case IS_ARRAY_CAST:
+ return "(array)";
default:
return "unknown";
}

Note: updated patch to support null default parameters without casting to the destination type.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.