Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions php_phongo.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ zend_bool phongo_writeconcernerror_init(zval *return_value, bson_t *bson TSRMLS_
ce->unserialize = zend_class_unserialize_deny; \
} while(0);

#define PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, size) do { \
if (is_debug) { \
ALLOC_HASHTABLE(props); \
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
} else if ((intern)->properties) { \
zend_hash_clean((intern)->properties); \
(props) = (intern)->properties; \
} else { \
ALLOC_HASHTABLE(props); \
zend_hash_init((props), (size), NULL, ZVAL_PTR_DTOR, 0); \
(intern)->properties = (props); \
} \
} while(0);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derickr: Refactored to use a macro. A function wasn't feasible since the type of intern varies with each class.

#endif /* PHONGO_H */


Expand Down
45 changes: 26 additions & 19 deletions php_phongo_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,27 @@ typedef struct {

typedef struct {
PHONGO_ZEND_OBJECT_PRE
char *data;
int data_len;
uint8_t type;
char *data;
int data_len;
uint8_t type;
HashTable *properties;
PHONGO_ZEND_OBJECT_POST
} php_phongo_binary_t;

typedef struct {
PHONGO_ZEND_OBJECT_PRE
bool initialized;
bson_decimal128_t decimal;
bool initialized;
bson_decimal128_t decimal;
HashTable *properties;
PHONGO_ZEND_OBJECT_POST
} php_phongo_decimal128_t;

typedef struct {
PHONGO_ZEND_OBJECT_PRE
char *code;
size_t code_len;
bson_t *scope;
char *code;
size_t code_len;
bson_t *scope;
HashTable *properties;
PHONGO_ZEND_OBJECT_POST
} php_phongo_javascript_t;

Expand All @@ -172,32 +175,36 @@ typedef struct {

typedef struct {
PHONGO_ZEND_OBJECT_PRE
bool initialized;
char oid[25];
bool initialized;
char oid[25];
HashTable *properties;
PHONGO_ZEND_OBJECT_POST
} php_phongo_objectid_t;

typedef struct {
PHONGO_ZEND_OBJECT_PRE
char *pattern;
int pattern_len;
char *flags;
int flags_len;
char *pattern;
int pattern_len;
char *flags;
int flags_len;
HashTable *properties;
PHONGO_ZEND_OBJECT_POST
} php_phongo_regex_t;

typedef struct {
PHONGO_ZEND_OBJECT_PRE
bool initialized;
uint32_t increment;
uint32_t timestamp;
bool initialized;
uint32_t increment;
uint32_t timestamp;
HashTable *properties;
PHONGO_ZEND_OBJECT_POST
} php_phongo_timestamp_t;

typedef struct {
PHONGO_ZEND_OBJECT_PRE
bool initialized;
int64_t milliseconds;
bool initialized;
int64_t milliseconds;
HashTable *properties;
PHONGO_ZEND_OBJECT_POST
} php_phongo_utcdatetime_t;

Expand Down
24 changes: 21 additions & 3 deletions src/BSON/Binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ static void php_phongo_binary_free_object(phongo_free_object_arg *object TSRMLS_
efree(intern->data);
}

if (intern->properties) {
zend_hash_destroy(intern->properties);
FREE_HASHTABLE(intern->properties);
}

#if PHP_VERSION_ID < 70000
efree(intern);
#endif
Expand Down Expand Up @@ -390,16 +395,17 @@ static HashTable *php_phongo_binary_get_gc(zval *object, phongo_get_gc_table tab
*table = NULL;
*n = 0;

return zend_std_get_properties(object TSRMLS_CC);
return Z_BINARY_OBJ_P(object)->properties;
} /* }}} */

static HashTable *php_phongo_binary_get_properties(zval *object TSRMLS_DC) /* {{{ */
static HashTable *php_phongo_binary_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
{
php_phongo_binary_t *intern;
HashTable *props;

intern = Z_BINARY_OBJ_P(object);
props = zend_std_get_properties(object TSRMLS_CC);

PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2);

if (!intern->data) {
return props;
Expand Down Expand Up @@ -431,6 +437,17 @@ static HashTable *php_phongo_binary_get_properties(zval *object TSRMLS_DC) /* {{

return props;
} /* }}} */

static HashTable *php_phongo_binary_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
{
*is_temp = 1;
return php_phongo_binary_get_properties_hash(object, true TSRMLS_CC);
} /* }}} */

static HashTable *php_phongo_binary_get_properties(zval *object TSRMLS_DC) /* {{{ */
{
return php_phongo_binary_get_properties_hash(object, false TSRMLS_CC);
} /* }}} */
/* }}} */

void php_phongo_binary_init_ce(INIT_FUNC_ARGS) /* {{{ */
Expand All @@ -448,6 +465,7 @@ void php_phongo_binary_init_ce(INIT_FUNC_ARGS) /* {{{ */

memcpy(&php_phongo_handler_binary, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
php_phongo_handler_binary.compare_objects = php_phongo_binary_compare_objects;
php_phongo_handler_binary.get_debug_info = php_phongo_binary_get_debug_info;
php_phongo_handler_binary.get_gc = php_phongo_binary_get_gc;
php_phongo_handler_binary.get_properties = php_phongo_binary_get_properties;
#if PHP_VERSION_ID >= 70000
Expand Down
24 changes: 21 additions & 3 deletions src/BSON/Decimal128.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ static void php_phongo_decimal128_free_object(phongo_free_object_arg *object TSR

zend_object_std_dtor(&intern->std TSRMLS_CC);

if (intern->properties) {
zend_hash_destroy(intern->properties);
FREE_HASHTABLE(intern->properties);
}

#if PHP_VERSION_ID < 70000
efree(intern);
#endif
Expand Down Expand Up @@ -303,17 +308,18 @@ static HashTable *php_phongo_decimal128_get_gc(zval *object, phongo_get_gc_table
*table = NULL;
*n = 0;

return zend_std_get_properties(object TSRMLS_CC);
return Z_DECIMAL128_OBJ_P(object)->properties;
} /* }}} */

static HashTable *php_phongo_decimal128_get_properties(zval *object TSRMLS_DC) /* {{{ */
static HashTable *php_phongo_decimal128_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
{
php_phongo_decimal128_t *intern;
HashTable *props;
char outbuf[BSON_DECIMAL128_STRING] = "";

intern = Z_DECIMAL128_OBJ_P(object);
props = zend_std_get_properties(object TSRMLS_CC);

PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 1);

if (!intern->initialized) {
return props;
Expand All @@ -340,6 +346,17 @@ static HashTable *php_phongo_decimal128_get_properties(zval *object TSRMLS_DC) /

return props;
} /* }}} */

static HashTable *php_phongo_decimal128_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
{
*is_temp = 1;
return php_phongo_decimal128_get_properties_hash(object, true TSRMLS_CC);
} /* }}} */

static HashTable *php_phongo_decimal128_get_properties(zval *object TSRMLS_DC) /* {{{ */
{
return php_phongo_decimal128_get_properties_hash(object, false TSRMLS_CC);
} /* }}} */
/* }}} */

void php_phongo_decimal128_init_ce(INIT_FUNC_ARGS) /* {{{ */
Expand All @@ -356,6 +373,7 @@ void php_phongo_decimal128_init_ce(INIT_FUNC_ARGS) /* {{{ */
zend_class_implements(php_phongo_decimal128_ce TSRMLS_CC, 1, zend_ce_serializable);

memcpy(&php_phongo_handler_decimal128, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
php_phongo_handler_decimal128.get_debug_info = php_phongo_decimal128_get_debug_info;
php_phongo_handler_decimal128.get_gc = php_phongo_decimal128_get_gc;
php_phongo_handler_decimal128.get_properties = php_phongo_decimal128_get_properties;
#if PHP_VERSION_ID >= 70000
Expand Down
24 changes: 21 additions & 3 deletions src/BSON/Javascript.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ static void php_phongo_javascript_free_object(phongo_free_object_arg *object TSR
intern->scope = NULL;
}

if (intern->properties) {
zend_hash_destroy(intern->properties);
FREE_HASHTABLE(intern->properties);
}

#if PHP_VERSION_ID < 70000
efree(intern);
#endif
Expand Down Expand Up @@ -426,16 +431,17 @@ static HashTable *php_phongo_javascript_get_gc(zval *object, phongo_get_gc_table
*table = NULL;
*n = 0;

return zend_std_get_properties(object TSRMLS_CC);
return Z_JAVASCRIPT_OBJ_P(object)->properties;
} /* }}} */

HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ */
HashTable *php_phongo_javascript_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
{
php_phongo_javascript_t *intern;
HashTable *props;

intern = Z_JAVASCRIPT_OBJ_P(object);
props = zend_std_get_properties(object TSRMLS_CC);

PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 2);

if (!intern->code) {
return props;
Expand Down Expand Up @@ -504,6 +510,17 @@ HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ *

return props;
} /* }}} */

static HashTable *php_phongo_javascript_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
{
*is_temp = 1;
return php_phongo_javascript_get_properties_hash(object, true TSRMLS_CC);
} /* }}} */

static HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ */
{
return php_phongo_javascript_get_properties_hash(object, false TSRMLS_CC);
} /* }}} */
/* }}} */

void php_phongo_javascript_init_ce(INIT_FUNC_ARGS) /* {{{ */
Expand All @@ -521,6 +538,7 @@ void php_phongo_javascript_init_ce(INIT_FUNC_ARGS) /* {{{ */

memcpy(&php_phongo_handler_javascript, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
php_phongo_handler_javascript.compare_objects = php_phongo_javascript_compare_objects;
php_phongo_handler_javascript.get_debug_info = php_phongo_javascript_get_debug_info;
php_phongo_handler_javascript.get_gc = php_phongo_javascript_get_gc;
php_phongo_handler_javascript.get_properties = php_phongo_javascript_get_properties;
#if PHP_VERSION_ID >= 70000
Expand Down
24 changes: 21 additions & 3 deletions src/BSON/ObjectID.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ static void php_phongo_objectid_free_object(phongo_free_object_arg *object TSRML

zend_object_std_dtor(&intern->std TSRMLS_CC);

if (intern->properties) {
zend_hash_destroy(intern->properties);
FREE_HASHTABLE(intern->properties);
}

#if PHP_VERSION_ID < 70000
efree(intern);
#endif
Expand Down Expand Up @@ -352,16 +357,17 @@ static HashTable *php_phongo_objectid_get_gc(zval *object, phongo_get_gc_table t
*table = NULL;
*n = 0;

return zend_std_get_properties(object TSRMLS_CC);
return Z_OBJECTID_OBJ_P(object)->properties;
} /* }}} */

static HashTable *php_phongo_objectid_get_properties(zval *object TSRMLS_DC) /* {{{ */
static HashTable *php_phongo_objectid_get_properties_hash(zval *object, bool is_debug TSRMLS_DC) /* {{{ */
{
php_phongo_objectid_t *intern;
HashTable *props;

intern = Z_OBJECTID_OBJ_P(object);
props = zend_std_get_properties(object TSRMLS_CC);

PHONGO_GET_PROPERTY_HASH_INIT_PROPS(is_debug, intern, props, 1);

if (!intern->oid) {
return props;
Expand All @@ -386,6 +392,17 @@ static HashTable *php_phongo_objectid_get_properties(zval *object TSRMLS_DC) /*

return props;
} /* }}} */

static HashTable *php_phongo_objectid_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
{
*is_temp = 1;
return php_phongo_objectid_get_properties_hash(object, true TSRMLS_CC);
} /* }}} */

static HashTable *php_phongo_objectid_get_properties(zval *object TSRMLS_DC) /* {{{ */
{
return php_phongo_objectid_get_properties_hash(object, false TSRMLS_CC);
} /* }}} */
/* }}} */

void php_phongo_objectid_init_ce(INIT_FUNC_ARGS) /* {{{ */
Expand All @@ -403,6 +420,7 @@ void php_phongo_objectid_init_ce(INIT_FUNC_ARGS) /* {{{ */

memcpy(&php_phongo_handler_objectid, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
php_phongo_handler_objectid.compare_objects = php_phongo_objectid_compare_objects;
php_phongo_handler_objectid.get_debug_info = php_phongo_objectid_get_debug_info;
php_phongo_handler_objectid.get_gc = php_phongo_objectid_get_gc;
php_phongo_handler_objectid.get_properties = php_phongo_objectid_get_properties;
#if PHP_VERSION_ID >= 70000
Expand Down
Loading