Skip to content

Commit 8b173d9

Browse files
committed
PHPC-654: Javascript comparison handler
1 parent ae987ca commit 8b173d9

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/BSON/Javascript.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,17 @@ phongo_create_object_retval php_phongo_javascript_create_object(zend_class_entry
439439
#endif
440440
} /* }}} */
441441

442+
static int php_phongo_javascript_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
443+
{
444+
php_phongo_javascript_t *intern1, *intern2;
445+
446+
intern1 = Z_JAVASCRIPT_OBJ_P(o1);
447+
intern2 = Z_JAVASCRIPT_OBJ_P(o2);
448+
449+
/* Do not consider the scope document for comparisons */
450+
return strcmp(intern1->code, intern2->code);
451+
} /* }}} */
452+
442453
HashTable *php_phongo_javascript_get_properties(zval *object TSRMLS_DC) /* {{{ */
443454
{
444455
php_phongo_javascript_t *intern;
@@ -533,6 +544,7 @@ PHP_MINIT_FUNCTION(Javascript)
533544
zend_class_implements(php_phongo_javascript_ce TSRMLS_CC, 1, zend_ce_serializable);
534545

535546
memcpy(&php_phongo_handler_javascript, phongo_get_std_object_handlers(), sizeof(zend_object_handlers));
547+
php_phongo_handler_javascript.compare_objects = php_phongo_javascript_compare_objects;
536548
php_phongo_handler_javascript.get_properties = php_phongo_javascript_get_properties;
537549
#if PHP_VERSION_ID >= 70000
538550
php_phongo_handler_javascript.free_obj = php_phongo_javascript_free_object;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
MongoDB\BSON\Javascript comparisons (without scope)
3+
--FILE--
4+
<?php
5+
6+
var_dump(new MongoDB\BSON\Javascript('function() { return 1; }') == new MongoDB\BSON\Javascript('function() { return 1; }'));
7+
var_dump(new MongoDB\BSON\Javascript('function() { return 1; }') < new MongoDB\BSON\Javascript('function() { return 2; }'));
8+
var_dump(new MongoDB\BSON\Javascript('function() { return 1; }') > new MongoDB\BSON\Javascript('function() { return 0; }'));
9+
10+
?>
11+
===DONE===
12+
<?php exit(0); ?>
13+
--EXPECTF--
14+
bool(true)
15+
bool(true)
16+
bool(true)
17+
===DONE===
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
MongoDB\BSON\Javascript comparisons (with scope)
3+
--FILE--
4+
<?php
5+
6+
// Comparison does not consider scope document
7+
var_dump(new MongoDB\BSON\Javascript('function() { return 1; }', ['x' => 1]) == new MongoDB\BSON\Javascript('function() { return 1; }', ['x' => 1]));
8+
var_dump(new MongoDB\BSON\Javascript('function() { return 1; }', ['x' => 1]) == new MongoDB\BSON\Javascript('function() { return 1; }', ['x' => 0]));
9+
var_dump(new MongoDB\BSON\Javascript('function() { return 1; }', ['x' => 1]) == new MongoDB\BSON\Javascript('function() { return 1; }', ['x' => 2]));
10+
11+
?>
12+
===DONE===
13+
<?php exit(0); ?>
14+
--EXPECTF--
15+
bool(true)
16+
bool(true)
17+
bool(true)
18+
===DONE===

0 commit comments

Comments
 (0)