Skip to content

Commit 27a140c

Browse files
committed
Bug fixes for JavaScript var_dump implementation (make it match PHP).
1 parent c725a80 commit 27a140c

File tree

2 files changed

+359
-18
lines changed

2 files changed

+359
-18
lines changed

tests/var_dump.phpt

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
--TEST--
2+
Test V8::executeString() : var_dump
3+
--SKIPIF--
4+
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
5+
--INI--
6+
date.timezone=UTC
7+
--FILE--
8+
<?php
9+
# Test var_dump of various types
10+
11+
$JS = <<< EOT
12+
13+
print("--- JS var_dump of PHP object ----\\n");
14+
var_dump(PHP.phptypes);
15+
16+
print("--- JS var_dump of JS object ----\\n");
17+
var types = {
18+
undefined: undefined,
19+
null: null,
20+
bool: true,
21+
string: "string",
22+
uint: 1,
23+
int: -1,
24+
number: 3.141592654,
25+
// XXX this gets parsed with local timezone,
26+
// which is bad for test repeatability.
27+
//date: new Date('September 27, 1976 09:00:00 GMT'),
28+
regexp: /regexp/,
29+
array: [1,2,3],
30+
object: { field: "foo" },
31+
function: function id(x) { return x; },
32+
phpobject: PHP.obj
33+
};
34+
35+
var_dump(types);
36+
print("--- PHP var_dump of JS object ----\\n");
37+
types;
38+
EOT;
39+
40+
class Foo {
41+
var $field = "php";
42+
}
43+
44+
$v8 = new V8Js();
45+
$v8->obj = new Foo;
46+
47+
$phptypes = $v8->phptypes = array(
48+
"null" => NULL,
49+
"bool" => true,
50+
"string" => "string",
51+
"uint" => 1,
52+
"int" => -1,
53+
"number" => 3.141592654,
54+
"date" => new DateTime('September 27, 1976 09:00:00 UTC', new DateTimeZone('UTC')),
55+
//"regexp" => new Regexp('/regexp/'), /* no native PHP regex type */
56+
"array" => array(1,2,3),
57+
"object" => array( "field" => "foo" ),
58+
"function" => (function ($x) { return $x; }),
59+
"phpobject" => new Foo
60+
);
61+
62+
echo "---- PHP var_dump of PHP object ----\n";
63+
var_dump($phptypes);
64+
65+
try {
66+
var_dump($v8->executeString($JS, 'var_dump.js'));
67+
} catch (V8JsScriptException $e) {
68+
echo "Error!\n";
69+
var_dump($e);
70+
}
71+
?>
72+
===EOF===
73+
--EXPECTF--
74+
---- PHP var_dump of PHP object ----
75+
array(11) {
76+
["null"]=>
77+
NULL
78+
["bool"]=>
79+
bool(true)
80+
["string"]=>
81+
string(6) "string"
82+
["uint"]=>
83+
int(1)
84+
["int"]=>
85+
int(-1)
86+
["number"]=>
87+
float(3.141592654)
88+
["date"]=>
89+
object(DateTime)#%d (3) {
90+
["date"]=>
91+
string(19) "1976-09-27 09:00:00"
92+
["timezone_type"]=>
93+
int(3)
94+
["timezone"]=>
95+
string(3) "UTC"
96+
}
97+
["array"]=>
98+
array(3) {
99+
[0]=>
100+
int(1)
101+
[1]=>
102+
int(2)
103+
[2]=>
104+
int(3)
105+
}
106+
["object"]=>
107+
array(1) {
108+
["field"]=>
109+
string(3) "foo"
110+
}
111+
["function"]=>
112+
object(Closure)#%d (1) {
113+
["parameter"]=>
114+
array(1) {
115+
["$x"]=>
116+
string(10) "<required>"
117+
}
118+
}
119+
["phpobject"]=>
120+
object(Foo)#%d (1) {
121+
["field"]=>
122+
string(3) "php"
123+
}
124+
}
125+
--- JS var_dump of PHP object ----
126+
array (11) {
127+
["null"] =>
128+
NULL
129+
["bool"] =>
130+
bool(true)
131+
["string"] =>
132+
string(6) "string"
133+
["uint"] =>
134+
int(1)
135+
["int"] =>
136+
int(-1)
137+
["number"] =>
138+
float(3.141593)
139+
["date"] =>
140+
object(DateTime)#%d (18) {
141+
["createFromFormat"] =>
142+
object(Closure)#%d {
143+
function () { [native code] }
144+
}
145+
["getLastErrors"] =>
146+
object(Closure)#%d {
147+
function () { [native code] }
148+
}
149+
["format"] =>
150+
object(Closure)#%d {
151+
function () { [native code] }
152+
}
153+
["modify"] =>
154+
object(Closure)#%d {
155+
function () { [native code] }
156+
}
157+
["add"] =>
158+
object(Closure)#%d {
159+
function () { [native code] }
160+
}
161+
["sub"] =>
162+
object(Closure)#%d {
163+
function () { [native code] }
164+
}
165+
["getTimezone"] =>
166+
object(Closure)#%d {
167+
function () { [native code] }
168+
}
169+
["setTimezone"] =>
170+
object(Closure)#%d {
171+
function () { [native code] }
172+
}
173+
["getOffset"] =>
174+
object(Closure)#%d {
175+
function () { [native code] }
176+
}
177+
["setTime"] =>
178+
object(Closure)#%d {
179+
function () { [native code] }
180+
}
181+
["setDate"] =>
182+
object(Closure)#%d {
183+
function () { [native code] }
184+
}
185+
["setISODate"] =>
186+
object(Closure)#%d {
187+
function () { [native code] }
188+
}
189+
["setTimestamp"] =>
190+
object(Closure)#%d {
191+
function () { [native code] }
192+
}
193+
["getTimestamp"] =>
194+
object(Closure)#%d {
195+
function () { [native code] }
196+
}
197+
["diff"] =>
198+
object(Closure)#%d {
199+
function () { [native code] }
200+
}
201+
["date"] =>
202+
string(19) "1976-09-27 09:00:00"
203+
["timezone_type"] =>
204+
int(3)
205+
["timezone"] =>
206+
string(3) "UTC"
207+
}
208+
["array"] =>
209+
array(3) {
210+
[0] =>
211+
int(1)
212+
[1] =>
213+
int(2)
214+
[2] =>
215+
int(3)
216+
}
217+
["object"] =>
218+
array (1) {
219+
["field"] =>
220+
string(3) "foo"
221+
}
222+
["function"] =>
223+
object(Closure)#%d (0) {
224+
}
225+
["phpobject"] =>
226+
object(Foo)#%d (1) {
227+
["field"] =>
228+
string(3) "php"
229+
}
230+
}
231+
--- JS var_dump of JS object ----
232+
object(Object)#%d (12) {
233+
["undefined"] =>
234+
NULL
235+
["null"] =>
236+
NULL
237+
["bool"] =>
238+
bool(true)
239+
["string"] =>
240+
string(6) "string"
241+
["uint"] =>
242+
int(1)
243+
["int"] =>
244+
int(-1)
245+
["number"] =>
246+
float(3.141593)
247+
["regexp"] =>
248+
regexp(/regexp/)
249+
["array"] =>
250+
array(3) {
251+
[0] =>
252+
int(1)
253+
[1] =>
254+
int(2)
255+
[2] =>
256+
int(3)
257+
}
258+
["object"] =>
259+
object(Object)#%d (1) {
260+
["field"] =>
261+
string(3) "foo"
262+
}
263+
["function"] =>
264+
object(Closure)#%d {
265+
function id(x) { return x; }
266+
}
267+
["phpobject"] =>
268+
object(Foo)#%d (1) {
269+
["field"] =>
270+
string(3) "php"
271+
}
272+
}
273+
--- PHP var_dump of JS object ----
274+
object(V8Object)#%d (12) {
275+
["undefined"]=>
276+
NULL
277+
["null"]=>
278+
NULL
279+
["bool"]=>
280+
bool(true)
281+
["string"]=>
282+
string(6) "string"
283+
["uint"]=>
284+
int(1)
285+
["int"]=>
286+
int(-1)
287+
["number"]=>
288+
float(3.141592654)
289+
["regexp"]=>
290+
object(V8Object)#%d (0) {
291+
}
292+
["array"]=>
293+
array(3) {
294+
[0]=>
295+
int(1)
296+
[1]=>
297+
int(2)
298+
[2]=>
299+
int(3)
300+
}
301+
["object"]=>
302+
object(V8Object)#%d (1) {
303+
["field"]=>
304+
string(3) "foo"
305+
}
306+
["function"]=>
307+
object(V8Function)#%d (0) {
308+
}
309+
["phpobject"]=>
310+
object(Foo)#%d (1) {
311+
["field"]=>
312+
string(3) "php"
313+
}
314+
}
315+
===EOF===

0 commit comments

Comments
 (0)