array_access.phpt 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. --TEST--
  2. Test V8::executeString() : Check ArrayAccess interface wrapping
  3. --SKIPIF--
  4. <?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
  5. --INI--
  6. v8js.use_array_access = 1
  7. --FILE--
  8. <?php
  9. if (PHP_VERSION_ID < 80000) {
  10. class MyArray implements ArrayAccess, Countable {
  11. public function offsetExists($offset) {
  12. return $offset >= 0 && $offset <= 20;
  13. }
  14. public function offsetGet($offset) {
  15. return 19 - $offset;
  16. }
  17. public function offsetSet($offset, $value) {
  18. throw new Exception('Not implemented');
  19. }
  20. public function offsetUnset($offset) {
  21. throw new Exception('Not implemented');
  22. }
  23. public function count() {
  24. return 20;
  25. }
  26. }
  27. } else {
  28. class MyArray implements ArrayAccess, Countable {
  29. public function offsetExists($offset): bool {
  30. return $offset >= 0 && $offset <= 20;
  31. }
  32. public function offsetGet(mixed $offset): mixed {
  33. return 19 - $offset;
  34. }
  35. public function offsetSet(mixed $offset, mixed $value): void {
  36. throw new Exception('Not implemented');
  37. }
  38. public function offsetUnset(mixed $offset): void {
  39. throw new Exception('Not implemented');
  40. }
  41. public function count(): int {
  42. return 20;
  43. }
  44. }
  45. }
  46. $myarr = new MyArray();
  47. var_dump(count($myarr));
  48. var_dump($myarr[5]);
  49. $js = <<<EOJS
  50. var_dump(PHP.myarr.constructor.name);
  51. var_dump(PHP.myarr.length);
  52. var_dump(PHP.myarr[5]);
  53. var_dump(PHP.myarr.join(', '));
  54. var_dump(PHP.myarr.slice(5, 10).join(', '));
  55. EOJS;
  56. $v8 = new V8Js();
  57. $v8->myarr = $myarr;
  58. $v8->executeString($js);
  59. ?>
  60. ===EOF===
  61. --EXPECT--
  62. int(20)
  63. int(14)
  64. string(5) "Array"
  65. int(20)
  66. int(14)
  67. string(68) "19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0"
  68. string(18) "14, 13, 12, 11, 10"
  69. ===EOF===