Merge pull request #345

This commit is contained in:
Jeremy Mikola
2016-07-13 11:40:35 -04:00
7 changed files with 69 additions and 71 deletions
+3 -3
View File
@@ -2070,9 +2070,9 @@ void php_phongo_new_javascript_from_javascript_and_scope(int init, zval *object,
}
intern = Z_JAVASCRIPT_OBJ_P(object);
intern->javascript = estrndup(code, code_len);
intern->javascript_len = code_len;
intern->document = scope ? bson_copy(scope) : NULL;
intern->code = estrndup(code, code_len);
intern->code_len = code_len;
intern->scope = scope ? bson_copy(scope) : NULL;
} /* }}} */
void php_phongo_new_binary_from_binary_and_type(zval *object, const char *data, size_t data_len, bson_subtype_t type TSRMLS_DC) /* {{{ */
{
+3 -3
View File
@@ -168,9 +168,9 @@ typedef struct {
typedef struct {
PHONGO_ZEND_OBJECT_PRE
char *javascript;
size_t javascript_len;
bson_t *document;
char *code;
size_t code_len;
bson_t *scope;
PHONGO_ZEND_OBJECT_POST
} php_phongo_javascript_t;
+35 -37
View File
@@ -47,20 +47,20 @@ PHONGO_API zend_class_entry *php_phongo_javascript_ce;
zend_object_handlers php_phongo_handler_javascript;
/* Initialize the object from a string and return whether it was successful. */
static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const char *javascript, phongo_zpp_char_len javascript_len, zval *scope TSRMLS_DC)
static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const char *code, phongo_zpp_char_len code_len, zval *scope TSRMLS_DC)
{
if (scope && Z_TYPE_P(scope) != IS_OBJECT && Z_TYPE_P(scope) != IS_ARRAY && Z_TYPE_P(scope) != IS_NULL) {
return false;
}
intern->javascript = estrndup(javascript, javascript_len);
intern->javascript_len = javascript_len;
intern->code = estrndup(code, code_len);
intern->code_len = code_len;
if (scope && (Z_TYPE_P(scope) == IS_OBJECT || Z_TYPE_P(scope) == IS_ARRAY)) {
intern->document = bson_new();
phongo_zval_to_bson(scope, PHONGO_BSON_NONE, intern->document, NULL TSRMLS_CC);
intern->scope = bson_new();
phongo_zval_to_bson(scope, PHONGO_BSON_NONE, intern->scope, NULL TSRMLS_CC);
} else {
intern->document = NULL;
intern->scope = NULL;
}
return true;
@@ -70,47 +70,47 @@ static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const ch
static bool php_phongo_javascript_init_from_hash(php_phongo_javascript_t *intern, HashTable *props TSRMLS_DC)
{
#if PHP_VERSION_ID >= 70000
zval *javascript, *scope;
zval *code, *scope;
if ((javascript = zend_hash_str_find(props, "javascript", sizeof("javascript")-1)) && Z_TYPE_P(javascript) == IS_STRING) {
if ((code = zend_hash_str_find(props, "code", sizeof("code")-1)) && Z_TYPE_P(code) == IS_STRING) {
scope = zend_hash_str_find(props, "scope", sizeof("scope")-1);
return php_phongo_javascript_init(intern, Z_STRVAL_P(javascript), Z_STRLEN_P(javascript), scope TSRMLS_CC);
return php_phongo_javascript_init(intern, Z_STRVAL_P(code), Z_STRLEN_P(code), scope TSRMLS_CC);
}
#else
zval **javascript, **scope;
zval **code, **scope;
if (zend_hash_find(props, "javascript", sizeof("javascript"), (void**) &javascript) == SUCCESS && Z_TYPE_PP(javascript) == IS_STRING) {
if (zend_hash_find(props, "code", sizeof("code"), (void**) &code) == SUCCESS && Z_TYPE_PP(code) == IS_STRING) {
zval *tmp = zend_hash_find(props, "scope", sizeof("scope"), (void**) &scope) == SUCCESS ? *scope : NULL;
return php_phongo_javascript_init(intern, Z_STRVAL_PP(javascript), Z_STRLEN_PP(javascript), tmp TSRMLS_CC);
return php_phongo_javascript_init(intern, Z_STRVAL_PP(code), Z_STRLEN_PP(code), tmp TSRMLS_CC);
}
#endif
return false;
}
/* {{{ proto BSON\Javascript Javascript::__construct(string $javascript[, array|object $document])
/* {{{ proto BSON\Javascript Javascript::__construct(string $code[, array|object $scope])
* The string is JavaScript code. The document is a mapping from identifiers to values, representing the scope in which the string should be evaluated
* NOTE: eJSON does not support this type :( */
PHP_METHOD(Javascript, __construct)
{
php_phongo_javascript_t *intern;
zend_error_handling error_handling;
char *javascript;
phongo_zpp_char_len javascript_len;
zval *document = NULL;
char *code;
phongo_zpp_char_len code_len;
zval *scope = NULL;
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
intern = Z_JAVASCRIPT_OBJ_P(getThis());
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|A!", &javascript, &javascript_len, &document) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|A!", &code, &code_len, &scope) == FAILURE) {
zend_restore_error_handling(&error_handling TSRMLS_CC);
return;
}
zend_restore_error_handling(&error_handling TSRMLS_CC);
php_phongo_javascript_init(intern, javascript, javascript_len, document TSRMLS_CC);
php_phongo_javascript_init(intern, code, code_len, scope TSRMLS_CC);
}
/* }}} */
@@ -189,12 +189,12 @@ static void php_phongo_javascript_free_object(phongo_free_object_arg *object TSR
zend_object_std_dtor(&intern->std TSRMLS_CC);
if (intern->javascript) {
efree(intern->javascript);
if (intern->code) {
efree(intern->code);
}
if (intern->document) {
bson_destroy(intern->document);
intern->document = NULL;
if (intern->scope) {
bson_destroy(intern->scope);
intern->scope = NULL;
}
#if PHP_VERSION_ID < 70000
@@ -210,8 +210,6 @@ phongo_create_object_retval php_phongo_javascript_create_object(zend_class_entry
zend_object_std_init(&intern->std, class_type TSRMLS_CC);
object_properties_init(&intern->std, class_type);
intern->document = NULL;
#if PHP_VERSION_ID >= 70000
intern->std.handlers = &php_phongo_handler_javascript;
@@ -235,21 +233,21 @@ HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ *
intern = Z_JAVASCRIPT_OBJ_P(object);
props = zend_std_get_properties(object TSRMLS_CC);
if (!intern->javascript) {
if (!intern->code) {
return props;
}
#if PHP_VERSION_ID >= 70000
{
zval javascript;
zval code;
ZVAL_STRING(&javascript, intern->javascript);
zend_hash_str_update(props, "javascript", sizeof("javascript")-1, &javascript);
ZVAL_STRING(&code, intern->code);
zend_hash_str_update(props, "code", sizeof("code")-1, &code);
if (intern->document) {
if (intern->scope) {
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
if (phongo_bson_to_zval_ex(bson_get_data(intern->document), intern->document->len, &state)) {
if (phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
Z_ADDREF(state.zchild);
zend_hash_str_update(props, "scope", sizeof("scope")-1, &state.zchild);
} else {
@@ -264,16 +262,16 @@ HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ *
}
#else
{
zval *javascript;
zval *code;
MAKE_STD_ZVAL(javascript);
ZVAL_STRING(javascript, intern->javascript, 1);
zend_hash_update(props, "javascript", sizeof("javascript"), &javascript, sizeof(javascript), NULL);
MAKE_STD_ZVAL(code);
ZVAL_STRING(code, intern->code, 1);
zend_hash_update(props, "code", sizeof("code"), &code, sizeof(code), NULL);
if (intern->document) {
if (intern->scope) {
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
if (phongo_bson_to_zval_ex(bson_get_data(intern->document), intern->document->len, &state)) {
if (phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
Z_ADDREF_P(state.zchild);
zend_hash_update(props, "scope", sizeof("scope"), &state.zchild, sizeof(state.zchild), NULL);
} else {
+6 -6
View File
@@ -115,15 +115,15 @@ bool php_phongo_javascript_has_scope(zval *object TSRMLS_DC)
intern = Z_JAVASCRIPT_OBJ_P(object);
return !!intern->document;
return !!intern->scope;
}
char *php_phongo_javascript_get_javascript(zval *object TSRMLS_DC)
char *php_phongo_javascript_get_code(zval *object TSRMLS_DC)
{
php_phongo_javascript_t *intern;
intern = Z_JAVASCRIPT_OBJ_P(object);
return intern->javascript;
return intern->code;
}
bson_t *php_phongo_javascript_get_scope(zval *object TSRMLS_DC)
{
@@ -131,7 +131,7 @@ bson_t *php_phongo_javascript_get_scope(zval *object TSRMLS_DC)
intern = Z_JAVASCRIPT_OBJ_P(object);
return intern->document;
return intern->scope;
}
int php_phongo_binary_get_data(zval *object, char **data TSRMLS_DC)
{
@@ -983,10 +983,10 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
if (instanceof_function(Z_OBJCE_P(object), php_phongo_javascript_ce TSRMLS_CC)) {
if (php_phongo_javascript_has_scope(object TSRMLS_CC)) {
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript with scope");
bson_append_code(bson, key, key_len, php_phongo_javascript_get_javascript(object TSRMLS_CC));
bson_append_code(bson, key, key_len, php_phongo_javascript_get_code(object TSRMLS_CC));
} else {
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript without scope");
bson_append_code_with_scope(bson, key, key_len, php_phongo_javascript_get_javascript(object TSRMLS_CC), php_phongo_javascript_get_scope(object TSRMLS_CC));
bson_append_code_with_scope(bson, key, key_len, php_phongo_javascript_get_code(object TSRMLS_CC), php_phongo_javascript_get_scope(object TSRMLS_CC));
}
return;
}
+3 -3
View File
@@ -36,14 +36,14 @@ foreach ($tests as $test) {
<?php exit(0); ?>
--EXPECTF--
object(%SBSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(33) "function foo(bar) { return bar; }"
["scope"]=>
object(stdClass)#%d (%d) {
}
}
object(%SBSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(30) "function foo() { return foo; }"
["scope"]=>
object(stdClass)#%d (%d) {
@@ -52,7 +52,7 @@ object(%SBSON\Javascript)#%d (%d) {
}
}
object(%SBSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(29) "function foo() { return id; }"
["scope"]=>
object(stdClass)#%d (%d) {
+12 -12
View File
@@ -27,25 +27,25 @@ foreach ($tests as $test) {
<?php exit(0); ?>
--EXPECTF--
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(33) "function foo(bar) { return bar; }"
}
string(94) "O:23:"MongoDB\BSON\Javascript":1:{s:10:"javascript";s:33:"function foo(bar) { return bar; }";}"
string(87) "O:23:"MongoDB\BSON\Javascript":1:{s:4:"code";s:33:"function foo(bar) { return bar; }";}"
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(33) "function foo(bar) { return bar; }"
}
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(33) "function foo(bar) { return bar; }"
["scope"]=>
object(stdClass)#%d (%d) {
}
}
string(125) "O:23:"MongoDB\BSON\Javascript":2:{s:10:"javascript";s:33:"function foo(bar) { return bar; }";s:5:"scope";O:8:"stdClass":0:{}}"
string(118) "O:23:"MongoDB\BSON\Javascript":2:{s:4:"code";s:33:"function foo(bar) { return bar; }";s:5:"scope";O:8:"stdClass":0:{}}"
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(33) "function foo(bar) { return bar; }"
["scope"]=>
object(stdClass)#%d (%d) {
@@ -53,7 +53,7 @@ object(MongoDB\BSON\Javascript)#%d (%d) {
}
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(30) "function foo() { return foo; }"
["scope"]=>
object(stdClass)#%d (%d) {
@@ -61,9 +61,9 @@ object(MongoDB\BSON\Javascript)#%d (%d) {
int(42)
}
}
string(137) "O:23:"MongoDB\BSON\Javascript":2:{s:10:"javascript";s:30:"function foo() { return foo; }";s:5:"scope";O:8:"stdClass":1:{s:3:"foo";i:42;}}"
string(130) "O:23:"MongoDB\BSON\Javascript":2:{s:4:"code";s:30:"function foo() { return foo; }";s:5:"scope";O:8:"stdClass":1:{s:3:"foo";i:42;}}"
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(30) "function foo() { return foo; }"
["scope"]=>
object(stdClass)#%d (%d) {
@@ -73,7 +73,7 @@ object(MongoDB\BSON\Javascript)#%d (%d) {
}
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(29) "function foo() { return id; }"
["scope"]=>
object(stdClass)#%d (%d) {
@@ -84,9 +84,9 @@ object(MongoDB\BSON\Javascript)#%d (%d) {
}
}
}
string(205) "O:23:"MongoDB\BSON\Javascript":2:{s:10:"javascript";s:29:"function foo() { return id; }";s:5:"scope";O:8:"stdClass":1:{s:2:"id";O:21:"MongoDB\BSON\ObjectID":1:{s:3:"oid";s:24:"53e2a1c40640fd72175d4603";}}}"
string(198) "O:23:"MongoDB\BSON\Javascript":2:{s:4:"code";s:29:"function foo() { return id; }";s:5:"scope";O:8:"stdClass":1:{s:2:"id";O:21:"MongoDB\BSON\ObjectID":1:{s:3:"oid";s:24:"53e2a1c40640fd72175d4603";}}}"
object(MongoDB\BSON\Javascript)#%d (%d) {
["javascript"]=>
["code"]=>
string(29) "function foo() { return id; }"
["scope"]=>
object(stdClass)#%d (%d) {
@@ -17,7 +17,7 @@ foreach ($tests as $test) {
list($code, $scope) = $test;
var_export(MongoDB\BSON\Javascript::__set_state([
'javascript' => $code,
'code' => $code,
'scope' => $scope,
]));
echo "\n\n";
@@ -25,7 +25,7 @@ foreach ($tests as $test) {
// Test with missing scope field
var_export(MongoDB\BSON\Javascript::__set_state([
'javascript' => 'function foo(bar) { return bar; }',
'code' => 'function foo(bar) { return bar; }',
]));
echo "\n\n";
@@ -34,18 +34,18 @@ echo "\n\n";
<?php exit(0); ?>
--EXPECTF--
MongoDB\BSON\Javascript::__set_state(array(
'javascript' => 'function foo(bar) { return bar; }',
'code' => 'function foo(bar) { return bar; }',
))
MongoDB\BSON\Javascript::__set_state(array(
'javascript' => 'function foo(bar) { return bar; }',
'code' => 'function foo(bar) { return bar; }',
'scope' =>
stdClass::__set_state(array(
)),
))
MongoDB\BSON\Javascript::__set_state(array(
'javascript' => 'function foo() { return foo; }',
'code' => 'function foo() { return foo; }',
'scope' =>
stdClass::__set_state(array(
'foo' => 42,
@@ -53,7 +53,7 @@ MongoDB\BSON\Javascript::__set_state(array(
))
MongoDB\BSON\Javascript::__set_state(array(
'javascript' => 'function foo() { return id; }',
'code' => 'function foo() { return id; }',
'scope' =>
stdClass::__set_state(array(
'id' =>
@@ -64,7 +64,7 @@ MongoDB\BSON\Javascript::__set_state(array(
))
MongoDB\BSON\Javascript::__set_state(array(
'javascript' => 'function foo(bar) { return bar; }',
'code' => 'function foo(bar) { return bar; }',
))
===DONE===