From 938c0fbc198423812317d1575e5e3210d94e91c6 Mon Sep 17 00:00:00 2001 From: Zeev Suraski Date: Mon, 7 Jul 2003 09:00:36 +0000 Subject: [PATCH] Initial support for overloading of array syntax for objects (very initial) --- Zend/zend_compile.c | 9 ++++++++ Zend/zend_compile.h | 2 ++ Zend/zend_execute.c | 39 ++++++++++++++++++++++++++++++---- Zend/zend_object_handlers.c | 20 ++++++++++++++++++ Zend/zend_object_handlers.h | 42 +++++++++++++++++++++++-------------- 5 files changed, 92 insertions(+), 20 deletions(-) diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 51a0bd22395..131397912dd 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -447,6 +447,15 @@ void zend_do_assign(znode *result, znode *variable, znode *value TSRMLS_DC) zend_do_op_data(opline, value TSRMLS_CC); SET_UNUSED(opline->result); *result = last_op->result; + } else if (last_op->opcode == ZEND_FETCH_DIM_W) { + last_op->opcode = ZEND_ASSIGN_DIM; + + zend_do_op_data(opline, value TSRMLS_CC); + opline->op2.u.var = get_temporary_variable(CG(active_op_array)); + opline->op2.u.EA.type = 0; + opline->op2.op_type = IS_VAR; + SET_UNUSED(opline->result); + *result = last_op->result; } else { opline->opcode = ZEND_ASSIGN; opline->op1 = *variable; diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index 5f8f4890306..d4ed288c193 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -676,6 +676,8 @@ int zendlex(znode *zendlval TSRMLS_DC); #define ZEND_VERIFY_INSTANCEOF 145 #define ZEND_VERIFY_ABSTRACT_CLASS 146 +#define ZEND_ASSIGN_DIM 147 + /* end of block */ /* END: OPCODES */ diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index 771bf98051a..769f1ba37f6 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -281,9 +281,8 @@ static inline zval *get_obj_zval_ptr(znode *op, temp_variable *Ts, zval **freeop return get_zval_ptr(op, Ts, freeop, type); } -static inline void zend_assign_to_object(znode *result, znode *op1, znode *op2, znode *value_op, temp_variable *Ts TSRMLS_DC) +static inline void zend_assign_to_object(znode *result, zval **object_ptr, znode *op2, znode *value_op, temp_variable *Ts, int opcode TSRMLS_DC) { - zval **object_ptr = get_obj_zval_ptr_ptr(op1, Ts, BP_VAR_W TSRMLS_CC); zval *object; zval *property = get_zval_ptr(op2, Ts, &EG(free_op2), BP_VAR_R); zval *free_value; @@ -336,7 +335,11 @@ static inline void zend_assign_to_object(znode *result, znode *op1, znode *op2, value->is_ref = 0; value->refcount = 0; } - Z_OBJ_HT_P(object)->write_property(object, property, value TSRMLS_CC); + if (opcode == ZEND_ASSIGN_OBJ) { + Z_OBJ_HT_P(object)->write_property(object, property, value TSRMLS_CC); + } else { + Z_OBJ_HT_P(object)->write_dimension(object, property, value TSRMLS_CC); + } if (property == &tmp) { zval_dtor(property); } @@ -2006,13 +2009,39 @@ int zend_make_var_handler(ZEND_OPCODE_HANDLER_ARGS) int zend_assign_obj_handler(ZEND_OPCODE_HANDLER_ARGS) { zend_op *op_data = EX(opline)+1; - zend_assign_to_object(&EX(opline)->result, &EX(opline)->op1, &EX(opline)->op2, &op_data->op1, EX(Ts) TSRMLS_CC); + zval **object_ptr = get_obj_zval_ptr_ptr(&EX(opline)->op1, EX(Ts), BP_VAR_W TSRMLS_CC); + + zend_assign_to_object(&EX(opline)->result, object_ptr, &EX(opline)->op2, &op_data->op1, EX(Ts), ZEND_ASSIGN_OBJ TSRMLS_CC); /* assign_obj has two opcodes! */ EX(opline)++; NEXT_OPCODE(); } +int zend_assign_dim_handler(ZEND_OPCODE_HANDLER_ARGS) + { + zend_op *op_data = EX(opline)+1; + zval **object_ptr = get_obj_zval_ptr_ptr(&EX(opline)->op1, EX(Ts), BP_VAR_W TSRMLS_CC); + + if ((*object_ptr)->type == IS_OBJECT) { + zend_assign_to_object(&EX(opline)->result, object_ptr, &EX(opline)->op2, &op_data->op1, EX(Ts), ZEND_ASSIGN_DIM TSRMLS_CC); + /* assign_dim has two opcodes! */ + } else { + zval *value; + zend_op *data_opline = EX(opline)+1; + + (*object_ptr)->refcount++; /* undo the effect of get_obj_zval_ptr_ptr() */ + zend_fetch_dimension_address(&data_opline->op2, &EX(opline)->op1, &EX(opline)->op2, EX(Ts), BP_VAR_W TSRMLS_CC); + + /* opline points to the data op at this point */ + value = get_zval_ptr(&data_opline->op1, EX(Ts), &EG(free_op1), BP_VAR_R); + zend_assign_to_variable(&EX(opline)->result, &data_opline->op2, &EX(opline)->op2, value, (EG(free_op1)?IS_TMP_VAR:EX(opline)->op1.op_type), EX(Ts) TSRMLS_CC); + } + EX(opline)++; + NEXT_OPCODE(); +} + + int zend_assign_handler(ZEND_OPCODE_HANDLER_ARGS) { zval *value; @@ -4234,6 +4263,8 @@ void zend_init_opcodes_handlers() zend_opcode_handlers[ZEND_ADD_INTERFACE] = zend_add_interface_handler; zend_opcode_handlers[ZEND_VERIFY_INSTANCEOF] = zend_verify_instanceof_handler; zend_opcode_handlers[ZEND_VERIFY_ABSTRACT_CLASS] = zend_verify_abstract_class; + + zend_opcode_handlers[ZEND_ASSIGN_DIM] = zend_assign_dim_handler; } /* diff --git a/Zend/zend_object_handlers.c b/Zend/zend_object_handlers.c index 20f0450b062..9ae70b28573 100644 --- a/Zend/zend_object_handlers.c +++ b/Zend/zend_object_handlers.c @@ -335,6 +335,25 @@ static void zend_std_write_property(zval *object, zval *member, zval *value TSRM } } + +static void zend_std_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC) +{ + zend_error("Cannot use object as array"); +#if 0 + zend_printf("Assigning to object: "); + zend_print_zval(object, 0); + + zend_printf("\n with offset: "); + zend_print_zval(offset, 0); + + zend_printf("\n the value: "); + zend_print_zval(value, 0); +#endif + + zend_printf("\n"); +} + + static zval **zend_std_get_property_ptr(zval *object, zval *member TSRMLS_DC) { zend_object *zobj; @@ -816,6 +835,7 @@ zend_object_handlers std_object_handlers = { zend_std_read_property, /* read_property */ zend_std_write_property, /* write_property */ + zend_std_write_dimension, /* write_dimension */ zend_std_get_property_ptr, /* get_property_ptr */ zend_std_get_property_ptr, /* get_property_zval_ptr */ NULL, /* get */ diff --git a/Zend/zend_object_handlers.h b/Zend/zend_object_handlers.h index 7cd01bf4013..efdff447ce0 100644 --- a/Zend/zend_object_handlers.h +++ b/Zend/zend_object_handlers.h @@ -24,39 +24,48 @@ union _zend_function; -typedef zval *(*zend_object_read_property_t)(zval *object, zval *member TSRMLS_DC); /* Used to fetch property from the object, read-only */ -typedef void (*zend_object_write_property_t)(zval *object, zval *member, zval *value TSRMLS_DC); +typedef zval *(*zend_object_read_property_t)(zval *object, zval *member TSRMLS_DC); + /* Used to set property of the object */ +typedef void (*zend_object_write_property_t)(zval *object, zval *member, zval *value TSRMLS_DC); + +/* Used to set dimension of the object */ +typedef void (*zend_object_write_dimension_t)(zval *object, zval *offset, zval *value TSRMLS_DC); -typedef zval **(*zend_object_get_property_ptr_t)(zval *object, zval *member TSRMLS_DC); /* Used to create pointer to the property of the object, for future r/w access via get/set */ -typedef zval **(*zend_object_get_property_zval_ptr_t)(zval *object, zval *member TSRMLS_DC); +typedef zval **(*zend_object_get_property_ptr_t)(zval *object, zval *member TSRMLS_DC); + /* Used to create pointer to the property of the object, for future direct r/w access */ -typedef void (*zend_object_set_t)(zval **property, zval *value TSRMLS_DC); +typedef zval **(*zend_object_get_property_zval_ptr_t)(zval *object, zval *member TSRMLS_DC); + /* Used to set object value (most probably used in combination with -typedef the result of the get_property_ptr) */ -typedef zval* (*zend_object_get_t)(zval *property TSRMLS_DC); + * typedef the result of the get_property_ptr) + */ +typedef void (*zend_object_set_t)(zval **property, zval *value TSRMLS_DC); + /* Used to get object value (most probably used in combination with -the result of the get_property_ptr or when converting object value to -one of the basic types) */ + * the result of the get_property_ptr or when converting object value to + * one of the basic types) + */ +typedef zval* (*zend_object_get_t)(zval *property TSRMLS_DC); -typedef int (*zend_object_has_property_t)(zval *object, zval *member, int check_empty TSRMLS_DC); /* Used to check if a property of the object exists */ -typedef void (*zend_object_unset_property_t)(zval *object, zval *member TSRMLS_DC); +typedef int (*zend_object_has_property_t)(zval *object, zval *member, int check_empty TSRMLS_DC); + /* Used to remove a property of the object */ +typedef void (*zend_object_unset_property_t)(zval *object, zval *member TSRMLS_DC); -typedef HashTable *(*zend_object_get_properties_t)(zval *object TSRMLS_DC); /* Used to get hash of the properties of the object, as hash of zval's */ +typedef HashTable *(*zend_object_get_properties_t)(zval *object TSRMLS_DC); -typedef int (*zend_object_call_method_t)(char *method, INTERNAL_FUNCTION_PARAMETERS); /* Used to call methods */ /* args on stack! */ -/* Andi - EX(fbc) (function being called) needs to be initialized already in the INIT fcall opcode so that the parameters can be parsed the right way. We need to add another callback fror this. -*/ +/* Andi - EX(fbc) (function being called) needs to be initialized already in the INIT fcall opcode so that the parameters can be parsed the right way. We need to add another callback for this. + */ +typedef int (*zend_object_call_method_t)(char *method, INTERNAL_FUNCTION_PARAMETERS); typedef union _zend_function *(*zend_object_get_method_t)(zval *object, char *method, int method_len TSRMLS_DC); typedef union _zend_function *(*zend_object_get_constructor_t)(zval *object TSRMLS_DC); -/* Get method parameter mask - by value/by reference, etc. */ /* Object maintenance/destruction */ typedef void (*zend_object_add_ref_t)(zval *object TSRMLS_DC); @@ -79,6 +88,7 @@ typedef struct _zend_object_handlers { /* individual object functions */ zend_object_read_property_t read_property; zend_object_write_property_t write_property; + zend_object_write_dimension_t write_dimension; zend_object_get_property_ptr_t get_property_ptr; zend_object_get_property_zval_ptr_t get_property_zval_ptr; zend_object_get_t get;