Skip to content

Commit b3298a0

Browse files
committed
PHPC-739: Rename "javascript" to "code" in Javascript BSON class
This changes behavior of var_dump(), var_export(), and serialization. Additionally, this renames the internal struct fields to be consistent with the publicized property names.
1 parent e6058a9 commit b3298a0

File tree

7 files changed

+69
-69
lines changed

7 files changed

+69
-69
lines changed

php_phongo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,9 +2070,9 @@ void php_phongo_new_javascript_from_javascript_and_scope(int init, zval *object,
20702070
}
20712071

20722072
intern = Z_JAVASCRIPT_OBJ_P(object);
2073-
intern->javascript = estrndup(code, code_len);
2074-
intern->javascript_len = code_len;
2075-
intern->document = scope ? bson_copy(scope) : NULL;
2073+
intern->code = estrndup(code, code_len);
2074+
intern->code_len = code_len;
2075+
intern->scope = scope ? bson_copy(scope) : NULL;
20762076
} /* }}} */
20772077
void php_phongo_new_binary_from_binary_and_type(zval *object, const char *data, size_t data_len, bson_subtype_t type TSRMLS_DC) /* {{{ */
20782078
{

php_phongo_structs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ typedef struct {
168168

169169
typedef struct {
170170
PHONGO_ZEND_OBJECT_PRE
171-
char *javascript;
172-
size_t javascript_len;
173-
bson_t *document;
171+
char *code;
172+
size_t code_len;
173+
bson_t *scope;
174174
PHONGO_ZEND_OBJECT_POST
175175
} php_phongo_javascript_t;
176176

src/BSON/Javascript.c

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ PHONGO_API zend_class_entry *php_phongo_javascript_ce;
4747
zend_object_handlers php_phongo_handler_javascript;
4848

4949
/* Initialize the object from a string and return whether it was successful. */
50-
static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const char *javascript, phongo_zpp_char_len javascript_len, zval *scope TSRMLS_DC)
50+
static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const char *code, phongo_zpp_char_len code_len, zval *scope TSRMLS_DC)
5151
{
5252
if (scope && Z_TYPE_P(scope) != IS_OBJECT && Z_TYPE_P(scope) != IS_ARRAY && Z_TYPE_P(scope) != IS_NULL) {
5353
return false;
5454
}
5555

56-
intern->javascript = estrndup(javascript, javascript_len);
57-
intern->javascript_len = javascript_len;
56+
intern->code = estrndup(code, code_len);
57+
intern->code_len = code_len;
5858

5959
if (scope && (Z_TYPE_P(scope) == IS_OBJECT || Z_TYPE_P(scope) == IS_ARRAY)) {
60-
intern->document = bson_new();
61-
phongo_zval_to_bson(scope, PHONGO_BSON_NONE, intern->document, NULL TSRMLS_CC);
60+
intern->scope = bson_new();
61+
phongo_zval_to_bson(scope, PHONGO_BSON_NONE, intern->scope, NULL TSRMLS_CC);
6262
} else {
63-
intern->document = NULL;
63+
intern->scope = NULL;
6464
}
6565

6666
return true;
@@ -70,47 +70,47 @@ static bool php_phongo_javascript_init(php_phongo_javascript_t *intern, const ch
7070
static bool php_phongo_javascript_init_from_hash(php_phongo_javascript_t *intern, HashTable *props TSRMLS_DC)
7171
{
7272
#if PHP_VERSION_ID >= 70000
73-
zval *javascript, *scope;
73+
zval *code, *scope;
7474

75-
if ((javascript = zend_hash_str_find(props, "javascript", sizeof("javascript")-1)) && Z_TYPE_P(javascript) == IS_STRING) {
75+
if ((code = zend_hash_str_find(props, "code", sizeof("code")-1)) && Z_TYPE_P(code) == IS_STRING) {
7676
scope = zend_hash_str_find(props, "scope", sizeof("scope")-1);
7777

78-
return php_phongo_javascript_init(intern, Z_STRVAL_P(javascript), Z_STRLEN_P(javascript), scope TSRMLS_CC);
78+
return php_phongo_javascript_init(intern, Z_STRVAL_P(code), Z_STRLEN_P(code), scope TSRMLS_CC);
7979
}
8080
#else
81-
zval **javascript, **scope;
81+
zval **code, **scope;
8282

83-
if (zend_hash_find(props, "javascript", sizeof("javascript"), (void**) &javascript) == SUCCESS && Z_TYPE_PP(javascript) == IS_STRING) {
83+
if (zend_hash_find(props, "code", sizeof("code"), (void**) &code) == SUCCESS && Z_TYPE_PP(code) == IS_STRING) {
8484
zval *tmp = zend_hash_find(props, "scope", sizeof("scope"), (void**) &scope) == SUCCESS ? *scope : NULL;
8585

86-
return php_phongo_javascript_init(intern, Z_STRVAL_PP(javascript), Z_STRLEN_PP(javascript), tmp TSRMLS_CC);
86+
return php_phongo_javascript_init(intern, Z_STRVAL_PP(code), Z_STRLEN_PP(code), tmp TSRMLS_CC);
8787
}
8888
#endif
8989
return false;
9090
}
9191

92-
/* {{{ proto BSON\Javascript Javascript::__construct(string $javascript[, array|object $document])
92+
/* {{{ proto BSON\Javascript Javascript::__construct(string $code[, array|object $scope])
9393
* The string is JavaScript code. The document is a mapping from identifiers to values, representing the scope in which the string should be evaluated
9494
* NOTE: eJSON does not support this type :( */
9595
PHP_METHOD(Javascript, __construct)
9696
{
9797
php_phongo_javascript_t *intern;
9898
zend_error_handling error_handling;
99-
char *javascript;
100-
phongo_zpp_char_len javascript_len;
101-
zval *document = NULL;
99+
char *code;
100+
phongo_zpp_char_len code_len;
101+
zval *scope = NULL;
102102

103103

104104
zend_replace_error_handling(EH_THROW, phongo_exception_from_phongo_domain(PHONGO_ERROR_INVALID_ARGUMENT), &error_handling TSRMLS_CC);
105105
intern = Z_JAVASCRIPT_OBJ_P(getThis());
106106

107-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|A!", &javascript, &javascript_len, &document) == FAILURE) {
107+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|A!", &code, &code_len, &scope) == FAILURE) {
108108
zend_restore_error_handling(&error_handling TSRMLS_CC);
109109
return;
110110
}
111111
zend_restore_error_handling(&error_handling TSRMLS_CC);
112112

113-
php_phongo_javascript_init(intern, javascript, javascript_len, document TSRMLS_CC);
113+
php_phongo_javascript_init(intern, code, code_len, scope TSRMLS_CC);
114114
}
115115
/* }}} */
116116

@@ -189,12 +189,12 @@ static void php_phongo_javascript_free_object(phongo_free_object_arg *object TSR
189189

190190
zend_object_std_dtor(&intern->std TSRMLS_CC);
191191

192-
if (intern->javascript) {
193-
efree(intern->javascript);
192+
if (intern->code) {
193+
efree(intern->code);
194194
}
195-
if (intern->document) {
196-
bson_destroy(intern->document);
197-
intern->document = NULL;
195+
if (intern->scope) {
196+
bson_destroy(intern->scope);
197+
intern->scope = NULL;
198198
}
199199

200200
#if PHP_VERSION_ID < 70000
@@ -233,21 +233,21 @@ HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ *
233233
intern = Z_JAVASCRIPT_OBJ_P(object);
234234
props = zend_std_get_properties(object TSRMLS_CC);
235235

236-
if (!intern->javascript) {
236+
if (!intern->code) {
237237
return props;
238238
}
239239

240240
#if PHP_VERSION_ID >= 70000
241241
{
242-
zval javascript;
242+
zval code;
243243

244-
ZVAL_STRING(&javascript, intern->javascript);
245-
zend_hash_str_update(props, "javascript", sizeof("javascript")-1, &javascript);
244+
ZVAL_STRING(&code, intern->code);
245+
zend_hash_str_update(props, "code", sizeof("code")-1, &code);
246246

247-
if (intern->document) {
247+
if (intern->scope) {
248248
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
249249

250-
if (phongo_bson_to_zval_ex(bson_get_data(intern->document), intern->document->len, &state)) {
250+
if (phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
251251
Z_ADDREF(state.zchild);
252252
zend_hash_str_update(props, "scope", sizeof("scope")-1, &state.zchild);
253253
} else {
@@ -262,16 +262,16 @@ HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ *
262262
}
263263
#else
264264
{
265-
zval *javascript;
265+
zval *code;
266266

267-
MAKE_STD_ZVAL(javascript);
268-
ZVAL_STRING(javascript, intern->javascript, 1);
269-
zend_hash_update(props, "javascript", sizeof("javascript"), &javascript, sizeof(javascript), NULL);
267+
MAKE_STD_ZVAL(code);
268+
ZVAL_STRING(code, intern->code, 1);
269+
zend_hash_update(props, "code", sizeof("code"), &code, sizeof(code), NULL);
270270

271-
if (intern->document) {
271+
if (intern->scope) {
272272
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
273273

274-
if (phongo_bson_to_zval_ex(bson_get_data(intern->document), intern->document->len, &state)) {
274+
if (phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state)) {
275275
Z_ADDREF_P(state.zchild);
276276
zend_hash_update(props, "scope", sizeof("scope"), &state.zchild, sizeof(state.zchild), NULL);
277277
} else {

src/bson.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,23 @@ bool php_phongo_javascript_has_scope(zval *object TSRMLS_DC)
115115

116116
intern = Z_JAVASCRIPT_OBJ_P(object);
117117

118-
return !!intern->document;
118+
return !!intern->scope;
119119
}
120-
char *php_phongo_javascript_get_javascript(zval *object TSRMLS_DC)
120+
char *php_phongo_javascript_get_code(zval *object TSRMLS_DC)
121121
{
122122
php_phongo_javascript_t *intern;
123123

124124
intern = Z_JAVASCRIPT_OBJ_P(object);
125125

126-
return intern->javascript;
126+
return intern->code;
127127
}
128128
bson_t *php_phongo_javascript_get_scope(zval *object TSRMLS_DC)
129129
{
130130
php_phongo_javascript_t *intern;
131131

132132
intern = Z_JAVASCRIPT_OBJ_P(object);
133133

134-
return intern->document;
134+
return intern->scope;
135135
}
136136
int php_phongo_binary_get_data(zval *object, char **data TSRMLS_DC)
137137
{
@@ -983,10 +983,10 @@ void object_to_bson(zval *object, php_phongo_bson_flags_t flags, const char *key
983983
if (instanceof_function(Z_OBJCE_P(object), php_phongo_javascript_ce TSRMLS_CC)) {
984984
if (php_phongo_javascript_has_scope(object TSRMLS_CC)) {
985985
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript with scope");
986-
bson_append_code(bson, key, key_len, php_phongo_javascript_get_javascript(object TSRMLS_CC));
986+
bson_append_code(bson, key, key_len, php_phongo_javascript_get_code(object TSRMLS_CC));
987987
} else {
988988
mongoc_log(MONGOC_LOG_LEVEL_TRACE, MONGOC_LOG_DOMAIN, "encoding Javascript without scope");
989-
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));
989+
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));
990990
}
991991
return;
992992
}

tests/bson/bson-javascript-002.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ foreach ($tests as $test) {
3636
<?php exit(0); ?>
3737
--EXPECTF--
3838
object(%SBSON\Javascript)#%d (%d) {
39-
["javascript"]=>
39+
["code"]=>
4040
string(33) "function foo(bar) { return bar; }"
4141
["scope"]=>
4242
object(stdClass)#%d (%d) {
4343
}
4444
}
4545
object(%SBSON\Javascript)#%d (%d) {
46-
["javascript"]=>
46+
["code"]=>
4747
string(30) "function foo() { return foo; }"
4848
["scope"]=>
4949
object(stdClass)#%d (%d) {
@@ -52,7 +52,7 @@ object(%SBSON\Javascript)#%d (%d) {
5252
}
5353
}
5454
object(%SBSON\Javascript)#%d (%d) {
55-
["javascript"]=>
55+
["code"]=>
5656
string(29) "function foo() { return id; }"
5757
["scope"]=>
5858
object(stdClass)#%d (%d) {

tests/bson/bson-javascript-003.phpt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,43 @@ foreach ($tests as $test) {
2727
<?php exit(0); ?>
2828
--EXPECTF--
2929
object(MongoDB\BSON\Javascript)#%d (%d) {
30-
["javascript"]=>
30+
["code"]=>
3131
string(33) "function foo(bar) { return bar; }"
3232
}
33-
string(94) "O:23:"MongoDB\BSON\Javascript":1:{s:10:"javascript";s:33:"function foo(bar) { return bar; }";}"
33+
string(87) "O:23:"MongoDB\BSON\Javascript":1:{s:4:"code";s:33:"function foo(bar) { return bar; }";}"
3434
object(MongoDB\BSON\Javascript)#%d (%d) {
35-
["javascript"]=>
35+
["code"]=>
3636
string(33) "function foo(bar) { return bar; }"
3737
}
3838

3939
object(MongoDB\BSON\Javascript)#%d (%d) {
40-
["javascript"]=>
40+
["code"]=>
4141
string(33) "function foo(bar) { return bar; }"
4242
["scope"]=>
4343
object(stdClass)#%d (%d) {
4444
}
4545
}
46-
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:{}}"
46+
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:{}}"
4747
object(MongoDB\BSON\Javascript)#%d (%d) {
48-
["javascript"]=>
48+
["code"]=>
4949
string(33) "function foo(bar) { return bar; }"
5050
["scope"]=>
5151
object(stdClass)#%d (%d) {
5252
}
5353
}
5454

5555
object(MongoDB\BSON\Javascript)#%d (%d) {
56-
["javascript"]=>
56+
["code"]=>
5757
string(30) "function foo() { return foo; }"
5858
["scope"]=>
5959
object(stdClass)#%d (%d) {
6060
["foo"]=>
6161
int(42)
6262
}
6363
}
64-
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;}}"
64+
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;}}"
6565
object(MongoDB\BSON\Javascript)#%d (%d) {
66-
["javascript"]=>
66+
["code"]=>
6767
string(30) "function foo() { return foo; }"
6868
["scope"]=>
6969
object(stdClass)#%d (%d) {
@@ -73,7 +73,7 @@ object(MongoDB\BSON\Javascript)#%d (%d) {
7373
}
7474

7575
object(MongoDB\BSON\Javascript)#%d (%d) {
76-
["javascript"]=>
76+
["code"]=>
7777
string(29) "function foo() { return id; }"
7878
["scope"]=>
7979
object(stdClass)#%d (%d) {
@@ -84,9 +84,9 @@ object(MongoDB\BSON\Javascript)#%d (%d) {
8484
}
8585
}
8686
}
87-
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";}}}"
87+
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";}}}"
8888
object(MongoDB\BSON\Javascript)#%d (%d) {
89-
["javascript"]=>
89+
["code"]=>
9090
string(29) "function foo() { return id; }"
9191
["scope"]=>
9292
object(stdClass)#%d (%d) {

tests/bson/bson-javascript-set_state-001.phpt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ foreach ($tests as $test) {
1717
list($code, $scope) = $test;
1818

1919
var_export(MongoDB\BSON\Javascript::__set_state([
20-
'javascript' => $code,
20+
'code' => $code,
2121
'scope' => $scope,
2222
]));
2323
echo "\n\n";
2424
}
2525

2626
// Test with missing scope field
2727
var_export(MongoDB\BSON\Javascript::__set_state([
28-
'javascript' => 'function foo(bar) { return bar; }',
28+
'code' => 'function foo(bar) { return bar; }',
2929
]));
3030
echo "\n\n";
3131

@@ -34,26 +34,26 @@ echo "\n\n";
3434
<?php exit(0); ?>
3535
--EXPECTF--
3636
MongoDB\BSON\Javascript::__set_state(array(
37-
'javascript' => 'function foo(bar) { return bar; }',
37+
'code' => 'function foo(bar) { return bar; }',
3838
))
3939

4040
MongoDB\BSON\Javascript::__set_state(array(
41-
'javascript' => 'function foo(bar) { return bar; }',
41+
'code' => 'function foo(bar) { return bar; }',
4242
'scope' =>
4343
stdClass::__set_state(array(
4444
)),
4545
))
4646

4747
MongoDB\BSON\Javascript::__set_state(array(
48-
'javascript' => 'function foo() { return foo; }',
48+
'code' => 'function foo() { return foo; }',
4949
'scope' =>
5050
stdClass::__set_state(array(
5151
'foo' => 42,
5252
)),
5353
))
5454

5555
MongoDB\BSON\Javascript::__set_state(array(
56-
'javascript' => 'function foo() { return id; }',
56+
'code' => 'function foo() { return id; }',
5757
'scope' =>
5858
stdClass::__set_state(array(
5959
'id' =>
@@ -64,7 +64,7 @@ MongoDB\BSON\Javascript::__set_state(array(
6464
))
6565

6666
MongoDB\BSON\Javascript::__set_state(array(
67-
'javascript' => 'function foo(bar) { return bar; }',
67+
'code' => 'function foo(bar) { return bar; }',
6868
))
6969

7070
===DONE===

0 commit comments

Comments
 (0)