test.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. $v8 = new V8Js;
  4. $v8->func = function ($a) { return var_export(func_get_args(), TRUE); };
  5. $v8->executeString("PHP.func();", "arg_test1.js");
  6. exit;
  7. */
  8. var_dump(V8Js::registerExtension('myparser.js', 'function foo() { print("foobar!\n"}', array('jstparser.js', 'json-template.js'), false));
  9. var_dump(V8Js::registerExtension('myparser.js', 'function foo() { print("foobar!\n"}', array('jstparser.js', 'json-template.js'), false));
  10. var_dump(V8Js::registerExtension('jstparser.js', file_get_contents('js/jstparser.js'), array(), false));
  11. //V8Js::registerExtension('json-template.js', file_get_contents('js/json-template.js'), array(), false);
  12. var_dump(V8JS::getExtensions());
  13. $a = new V8Js('myobj', array(), array('jstparser.js'));
  14. $jstparser = <<< 'EOT'
  15. var template = 'Gold &amp; Hot Rod Red, as seen in the new <a href="http://blog.markturansky.com/archives/51">Iron Man trailer</a>!' + "\n" +
  16. '<table cellspacing="0" cellpadding="4">' + "\n" +
  17. ' <% for(var i = 0; i < 10; i++){ %> ' + "\n" +
  18. ' <tr>' + "\n" +
  19. ' <td style="background-color: <%= i % 2 == 0 ? \'red\' : \'gold\' %>">' + "\n" +
  20. ' Hi, <%=name%>! i is <%= i %>' + "\n" +
  21. ' </td>' + "\n" +
  22. ' </tr>' + "\n" +
  23. ' <% } %>' + "\n" +
  24. '</table>' + "\n" +
  25. 'Note that name is HTML escaped by default. Here it is without escaping:'+
  26. '<%+ name %>';
  27. Jst.evaluateSingleShot(template, {"name":"foobar"});
  28. EOT;
  29. echo($a->executeString($jstparser, "ext_test1.js")), "\n";
  30. $a->_SERVER = $_SERVER;
  31. $a->func = function ($a) { echo "Closure..\n"; };
  32. $a->executeString("print(myobj._SERVER['HOSTNAME']);", "test1.js");
  33. $a->executeString("print(myobj.func); myobj.func(1);", "closure_test.js");
  34. $JS = <<<'EOT'
  35. function dump(a)
  36. {
  37. for (var i in a) {
  38. var val = a[i];
  39. print(i + ' => ' + val + "\n");
  40. }
  41. }
  42. function foo()
  43. {
  44. var bar = 'bar';
  45. var foo = 'foo';
  46. return foo + bar;
  47. }
  48. function test()
  49. {
  50. var a = 'PHP version: ' + PHP.phpver;
  51. phpver = 'changed in JS!';
  52. return a;
  53. }
  54. function loop()
  55. {
  56. var foo = 'foo';
  57. while(true)
  58. {
  59. foo + 'bar';
  60. }
  61. }
  62. function output()
  63. {
  64. while(true)
  65. {
  66. print("output:foo\n");
  67. sleep(5);
  68. exit();
  69. }
  70. };
  71. function simplearray()
  72. {
  73. print(myarray.a + "\n");
  74. print(myarray.b + "\n");
  75. print(myarray.c + "\n");
  76. print(myarray.d + "\n");
  77. }
  78. function bigarray()
  79. {
  80. print(PHP.$_SERVER['HOSTNAME'] + "\n");
  81. print(PHP.$_SERVER.argv + "\n");
  82. }
  83. EOT;
  84. $jsontemplate = <<< EOT
  85. var t = jsontemplate.Template("{# This is a comment and will be removed from the output.}{.section songs}<h2>Songs in '{playlist-name}'</h2><table width=\"100%\">{.repeated section @}<tr><td><a href=\"{url-base|htmltag}{url|htmltag}\">Play</a><td><i>{title}</i></td><td>{artist}</td></tr>{.end}</table>{.or}<p><em>(No page content matches)</em></p>{.end}");
  86. t.expand({
  87. "url-base": "http://example.com/music/",
  88. "playlist-name": "Epic Playlist",
  89. "songs": [
  90. {
  91. "url": "1.mp3",
  92. "artist": "Grayceon",
  93. "title": "Sounds Like Thunder"
  94. },
  95. {
  96. "url": "2.mp3",
  97. "artist": "Thou",
  98. "title": "Their Hooves Carve Craters in the Earth"
  99. }]});
  100. EOT;
  101. class tester
  102. {
  103. public $foo = 'bar';
  104. private $my_private = 'arf';
  105. protected $my_protected = 'argh';
  106. function mytest() { echo 'Here be monsters..', "\n"; }
  107. }
  108. $a = new V8Js();
  109. $a->obj = new tester();
  110. $a->phpver = phpversion();
  111. $a->argv = $_SERVER['argv'];
  112. $a->integer = 1;
  113. $a->float = 3.14;
  114. $a->{'$'._SERVER} = $_SERVER;
  115. $a->GLOBALS = $GLOBALS;
  116. $a->myarray = array(
  117. 'a' => 'Array value for key A',
  118. 'b' => 'Array value for key B',
  119. 'c' => 'Array value for key C',
  120. 'd' => 'Array value for key D',
  121. );
  122. $a->arr = array("first", "second", "third");
  123. $a->executeString($JS, "test1.js");
  124. $a->executeString("bigarray()", "test1.js");
  125. try {
  126. echo($a->executeString($jstparser, "test2.js")), "\n";
  127. var_dump($a->executeString($jsontemplate, "test1.js"));
  128. } catch (V8JsException $e) {
  129. echo $e->getMessage();
  130. }
  131. // Test for context handling
  132. $a->executeString($JS, "test1.js");
  133. $a->executeString("bigarray();");
  134. echo '$a->obj: ', "\n"; $a->executeString("dump(PHP.obj);");
  135. echo '$a->arr: ', "\n"; $a->executeString("dump(PHP.arr);");
  136. echo '$a->argv: ', "\n"; $a->executeString("dump(PHP.argv);");
  137. var_dump($a->argv);
  138. var_dump($a->executeString("test();"));
  139. var_dump($a->executeString("test();"));
  140. $b = new V8Js();
  141. var_dump($a->phpver, $a->executeString("test();"));
  142. $b->executeString($JS, "test2.js");
  143. var_dump($b->executeString("test();"));
  144. var_dump($b->executeString("print('foobar\\n');"));
  145. // Exception methods
  146. try {
  147. $b->executeString("foobar; foo();", "extest.js");
  148. } catch (V8JsException $e) {
  149. var_dump($e, $e->getJsFileName(), $e->getJsLineNumber(), $e->getJsSourceLine(), $e->getJsTrace());
  150. }