Skip to content

Commit 6df3f8a

Browse files
committed
PHPC-337: BSON\Javascript debug handler
1 parent 02cb37f commit 6df3f8a

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/BSON/Javascript.c

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
PHONGO_API zend_class_entry *php_phongo_javascript_ce;
4646

47+
zend_object_handlers php_phongo_handler_javascript;
4748

4849
/* {{{ proto BSON\Javascript Javascript::__construct(string $javascript[, array|object $document])
4950
* The string is JavaScript code. The document is a mapping from identifiers to values, representing the scope in which the string should be evaluated
@@ -119,12 +120,43 @@ zend_object_value php_phongo_javascript_create_object(zend_class_entry *class_ty
119120
object_properties_init(&intern->std, class_type);
120121

121122
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, php_phongo_javascript_free_object, NULL TSRMLS_CC);
122-
retval.handlers = phongo_get_std_object_handlers();
123+
retval.handlers = &php_phongo_handler_javascript;
123124

124125
intern->document = NULL;
125126

126127
return retval;
127128
} /* }}} */
129+
130+
HashTable *php_phongo_javascript_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
131+
{
132+
php_phongo_javascript_t *intern;
133+
zval retval = zval_used_for_init;
134+
135+
136+
*is_temp = 1;
137+
intern = (php_phongo_javascript_t *)zend_object_store_get_object(object TSRMLS_CC);
138+
139+
array_init(&retval);
140+
141+
add_assoc_stringl_ex(&retval, ZEND_STRS("javascript"), intern->javascript, intern->javascript_len, 1);
142+
143+
if (intern->document) {
144+
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;
145+
146+
MAKE_STD_ZVAL(state.zchild);
147+
148+
if (bson_to_zval(bson_get_data(intern->document), intern->document->len, &state)) {
149+
Z_ADDREF_P(state.zchild);
150+
add_assoc_zval_ex(&retval, ZEND_STRS("scope"), state.zchild);
151+
} else {
152+
add_assoc_null_ex(&retval, ZEND_STRS("scope"));
153+
}
154+
155+
zval_ptr_dtor(&state.zchild);
156+
}
157+
158+
return Z_ARRVAL(retval);
159+
} /* }}} */
128160
/* }}} */
129161

130162
/* {{{ PHP_MINIT_FUNCTION */
@@ -140,6 +172,8 @@ PHP_MINIT_FUNCTION(Javascript)
140172

141173
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, php_phongo_type_ce);
142174

175+
memcpy(&php_phongo_handler_javascript, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
176+
php_phongo_handler_javascript.get_debug_info = php_phongo_javascript_get_debug_info;
143177

144178
return SUCCESS;
145179
}

tests/bson/bson-javascript-002.phpt

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
--TEST--
2+
BSON BSON\Javascript debug handler
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$jsClassname = BSON_NAMESPACE . '\Javascript';
10+
$oidClassname = BSON_NAMESPACE . '\ObjectId';
11+
12+
$tests = array(
13+
array(
14+
'function foo(bar) { return bar; }',
15+
array(),
16+
),
17+
array(
18+
'function foo() { return foo; }',
19+
array('foo' => 42),
20+
),
21+
array(
22+
'function foo() { return id; }',
23+
array('id' => new $oidClassname('53e2a1c40640fd72175d4603')),
24+
),
25+
);
26+
27+
foreach ($tests as $test) {
28+
list($code, $scope) = $test;
29+
30+
$js = new $jsClassname($code, $scope);
31+
var_dump($js);
32+
}
33+
34+
?>
35+
===DONE===
36+
<?php exit(0); ?>
37+
--EXPECTF--
38+
object(%SBSON\Javascript)#%d (%d) {
39+
["javascript"]=>
40+
string(33) "function foo(bar) { return bar; }"
41+
["scope"]=>
42+
object(stdClass)#%d (%d) {
43+
}
44+
}
45+
object(%SBSON\Javascript)#%d (%d) {
46+
["javascript"]=>
47+
string(30) "function foo() { return foo; }"
48+
["scope"]=>
49+
object(stdClass)#%d (%d) {
50+
["foo"]=>
51+
int(42)
52+
}
53+
}
54+
object(%SBSON\Javascript)#%d (%d) {
55+
["javascript"]=>
56+
string(29) "function foo() { return id; }"
57+
["scope"]=>
58+
object(stdClass)#%d (%d) {
59+
["id"]=>
60+
object(MongoDB\BSON\ObjectID)#%d (%d) {
61+
["oid"]=>
62+
string(24) "53e2a1c40640fd72175d4603"
63+
}
64+
}
65+
}
66+
===DONE===

0 commit comments

Comments
 (0)