Skip to content

PHPC-730: Javascript::getCode() and Javascript::getScope() #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2016
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
49 changes: 49 additions & 0 deletions src/BSON/Javascript.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,53 @@ PHP_METHOD(Javascript, __wakeup)
}
/* }}} */

/* {{{ proto string Javascript::getCode()
*/
PHP_METHOD(Javascript, getCode)
{
php_phongo_javascript_t *intern;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

intern = Z_JAVASCRIPT_OBJ_P(getThis());

PHONGO_RETURN_STRINGL(intern->code, intern->code_len);
}
/* }}} */

/* {{{ proto object|null Javascript::getScope()
*/
PHP_METHOD(Javascript, getScope)
{
php_phongo_javascript_t *intern;

if (zend_parse_parameters_none() == FAILURE) {
return;
}

intern = Z_JAVASCRIPT_OBJ_P(getThis());

if (!intern->scope) {
RETURN_NULL();
}

if (intern->scope->len) {
php_phongo_bson_state state = PHONGO_BSON_STATE_INITIALIZER;

phongo_bson_to_zval_ex(bson_get_data(intern->scope), intern->scope->len, &state);
#if PHP_VERSION_ID >= 70000
RETURN_ZVAL(&state.zchild, 0, 1);
#else
RETURN_ZVAL(state.zchild, 0, 1);
#endif
} else {
RETURN_NULL();
}
}
/* }}} */

/* {{{ BSON\Javascript */

ZEND_BEGIN_ARG_INFO_EX(ai_Javascript___construct, 0, 0, 1)
Expand All @@ -199,6 +246,8 @@ static zend_function_entry php_phongo_javascript_me[] = {
PHP_ME(Javascript, __set_state, ai_Javascript___set_state, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(Javascript, __toString, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Javascript, __wakeup, ai_Javascript_void, ZEND_ACC_PUBLIC)
PHP_ME(Javascript, getCode, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(Javascript, getScope, ai_Javascript_void, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_FE_END
};

Expand Down
28 changes: 28 additions & 0 deletions tests/bson/bson-javascript-getCode-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
MongoDB\BSON\Javascript::getCode()
--FILE--
<?php

$tests = [
['function foo(bar) { return bar; }', null],
['function foo(bar) { return bar; }', []],
['function foo() { return foo; }', ['foo' => 42]],
['function foo() { return id; }', ['id' => new MongoDB\BSON\ObjectId('53e2a1c40640fd72175d4603')]],
];

foreach ($tests as $test) {
list($code, $scope) = $test;

$js = new MongoDB\BSON\Javascript($code, $scope);
var_dump($js->getCode());
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
string(33) "function foo(bar) { return bar; }"
string(33) "function foo(bar) { return bar; }"
string(30) "function foo() { return foo; }"
string(29) "function foo() { return id; }"
===DONE===
38 changes: 38 additions & 0 deletions tests/bson/bson-javascript-getScope-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
MongoDB\BSON\Javascript::getScope()
--FILE--
<?php

$tests = [
['function foo(bar) { return bar; }', null],
['function foo(bar) { return bar; }', []],
['function foo() { return foo; }', ['foo' => 42]],
['function foo() { return id; }', ['id' => new MongoDB\BSON\ObjectId('53e2a1c40640fd72175d4603')]],
];

foreach ($tests as $test) {
list($code, $scope) = $test;

$js = new MongoDB\BSON\Javascript($code, $scope);
var_dump($js->getScope());
}

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
NULL
object(stdClass)#%d (%d) {
}
object(stdClass)#%d (%d) {
["foo"]=>
int(42)
}
object(stdClass)#%d (%d) {
["id"]=>
object(MongoDB\BSON\ObjectID)#%d (%d) {
["oid"]=>
string(24) "53e2a1c40640fd72175d4603"
}
}
===DONE===