Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit 827d3fa

Browse files
committed
HHVM-266: Javascript should emit $code/$scope for JSON
1 parent 7dd7ccf commit 827d3fa

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

ext_mongodb.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,14 @@ public function serialize() : string
13271327

13281328
public function jsonSerialize() : mixed
13291329
{
1330-
return $this->code;
1330+
$json = [ '$code' => $this->code ];
1331+
1332+
if ( isset( $this->scope ) )
1333+
{
1334+
$json['$scope'] = $this->scope;
1335+
}
1336+
1337+
return $json;
13311338
}
13321339

13331340
public function unserialize(mixed $serialized) : void
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
JsonSerializable: Javascript (with scope)
3+
--FILE--
4+
<?php
5+
$doc = [
6+
'foo' => new MongoDB\BSON\Javascript( "function foo(bar) { return bar; }", [ 'foo' => 42 ] )
7+
];
8+
9+
echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
10+
$d = json_encode( $doc );
11+
echo $d, "\n";
12+
13+
var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
14+
?>
15+
--EXPECTF--
16+
{ "foo" : "function foo(bar) { return bar; }" }
17+
{"foo":{"$code":"function foo(bar) { return bar; }","$scope":{"foo":42}}}
18+
object(stdClass)#%d (%d) {
19+
["foo"]=>
20+
object(stdClass)#%d (%d) {
21+
["$code"]=>
22+
string(33) "function foo(bar) { return bar; }"
23+
["$scope"]=>
24+
object(stdClass)#%d (%d) {
25+
["foo"]=>
26+
int(42)
27+
}
28+
}
29+
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
--TEST--
2-
JsonSerializable: Javascript
2+
JsonSerializable: Javascript (without scope)
33
--FILE--
44
<?php
55
$doc = [
6-
'foo' => new MongoDB\BSON\Javascript( "function foo(bar) { return bar; }", [ 'foo' => 42 ] )
6+
'foo' => new MongoDB\BSON\Javascript( "function foo(bar) { return bar; }")
77
];
88

99
echo MongoDB\BSON\toJSON( \MongoDB\BSON\fromPHP( $doc ) ), "\n";
@@ -14,8 +14,11 @@ var_dump( \MongoDB\BSON\toPHP( \MongoDB\BSON\fromJSON( $d ) ) );
1414
?>
1515
--EXPECTF--
1616
{ "foo" : "function foo(bar) { return bar; }" }
17-
{"foo":"function foo(bar) { return bar; }"}
17+
{"foo":{"$code":"function foo(bar) { return bar; }"}}
1818
object(stdClass)#%d (%d) {
1919
["foo"]=>
20-
string(33) "function foo(bar) { return bar; }"
20+
object(stdClass)#%d (%d) {
21+
["$code"]=>
22+
string(33) "function foo(bar) { return bar; }"
23+
}
2124
}

0 commit comments

Comments
 (0)