object_method_call.phpt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. --TEST--
  2. Test V8::executeString() : Calling methods of object passed from PHP
  3. --SKIPIF--
  4. <?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
  5. --INI--
  6. date.timezone=UTC
  7. --FILE--
  8. <?php
  9. // Test class
  10. class Testing
  11. {
  12. public $foo = 'ORIGINAL';
  13. private $my_private = 'arf'; // Should not show in JS side
  14. protected $my_protected = 'argh'; // Should not show in JS side
  15. function mytest($a, $b, $c = NULL)
  16. {
  17. var_dump(func_get_args());
  18. }
  19. function mydatetest(DateTime $date, $b) {
  20. $date->setTimeZone(new DateTimeZone(ini_get('date.timezone')));
  21. echo $date->format(DateTime::RFC1123), "\n";
  22. var_dump($b);
  23. }
  24. }
  25. $a = new V8Js();
  26. $a->myobj = new Testing();
  27. $a->executeString("PHP.myobj.mytest('arg1', 'arg2');", "test1.js");
  28. $a->executeString("PHP.myobj.mytest(true, false, 1234567890);", "test2.js");
  29. $a->executeString("PHP.myobj.mytest(3.14, 42, null);", "test3.js");
  30. // Invalid parameters
  31. try {
  32. $a->executeString("PHP.myobj.mytest();", "test4.js");
  33. } catch (V8JsScriptException $e) {
  34. echo $e->getMessage(), "\n";
  35. }
  36. try {
  37. $a->executeString("PHP.myobj.mytest('arg1', 'arg2', 'arg3', 'extra_arg');", "test5.js");
  38. } catch (V8JsScriptException $e) {
  39. echo $e->getMessage(), "\n";
  40. }
  41. try {
  42. echo "\nTEST: Javascript Date -> PHP DateTime\n";
  43. echo "======================================\n";
  44. $a->executeString("date = new Date('September 8, 1975 09:00:00 GMT'); print(date.toUTCString() + '\\n'); PHP.myobj.mydatetest(date, 'foo');", "test6.js");
  45. } catch (V8JsScriptException $e) {
  46. echo $e->getMessage(), "\n";
  47. }
  48. // Array / Object
  49. try {
  50. $a->executeString("PHP.myobj.mytest(PHP.myobj, new Array(1,2,3), new Array('foo', 'bar', PHP.myobj));", "test7.js");
  51. } catch (V8JsScriptException $e) {
  52. var_dump($e);
  53. }
  54. // Type safety
  55. // this is illegal, but shouldn't crash!
  56. try {
  57. $a->executeString("PHP.myobj.mytest.call({})", "test8.js");
  58. } catch (V8JsScriptException $e) {
  59. echo "exception: ", $e->getMessage(), "\n";
  60. }
  61. ?>
  62. ===EOF===
  63. --EXPECTF--
  64. array(2) {
  65. [0]=>
  66. string(4) "arg1"
  67. [1]=>
  68. string(4) "arg2"
  69. }
  70. array(3) {
  71. [0]=>
  72. bool(true)
  73. [1]=>
  74. bool(false)
  75. [2]=>
  76. int(1234567890)
  77. }
  78. array(3) {
  79. [0]=>
  80. float(3.14)
  81. [1]=>
  82. int(42)
  83. [2]=>
  84. NULL
  85. }
  86. test4.js:1: TypeError: Testing::mytest() expects at least 2 parameters, 0 given
  87. array(4) {
  88. [0]=>
  89. string(4) "arg1"
  90. [1]=>
  91. string(4) "arg2"
  92. [2]=>
  93. string(4) "arg3"
  94. [3]=>
  95. string(9) "extra_arg"
  96. }
  97. TEST: Javascript Date -> PHP DateTime
  98. ======================================
  99. Mon, 08 Sep 1975 09:00:00 GMT
  100. Mon, 08 Sep 1975 09:00:00 +0000
  101. string(3) "foo"
  102. array(3) {
  103. [0]=>
  104. object(Testing)#%d (3) {
  105. ["foo"]=>
  106. string(8) "ORIGINAL"
  107. ["my_private":"Testing":private]=>
  108. string(3) "arf"
  109. ["my_protected":protected]=>
  110. string(4) "argh"
  111. }
  112. [1]=>
  113. array(3) {
  114. [0]=>
  115. int(1)
  116. [1]=>
  117. int(2)
  118. [2]=>
  119. int(3)
  120. }
  121. [2]=>
  122. array(3) {
  123. [0]=>
  124. string(3) "foo"
  125. [1]=>
  126. string(3) "bar"
  127. [2]=>
  128. object(Testing)#%d (3) {
  129. ["foo"]=>
  130. string(8) "ORIGINAL"
  131. ["my_private":"Testing":private]=>
  132. string(3) "arf"
  133. ["my_protected":protected]=>
  134. string(4) "argh"
  135. }
  136. }
  137. }
  138. exception: test8.js:1: TypeError: Illegal invocation
  139. ===EOF===