array_access_002.phpt 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. --TEST--
  2. Test V8::executeString() : Use ArrayAccess with JavaScript native push method
  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. private $data = Array('one', 'two', 'three');
  12. public function offsetExists($offset) {
  13. return isset($this->data[$offset]);
  14. }
  15. public function offsetGet($offset) {
  16. return $this->data[$offset];
  17. }
  18. public function offsetSet($offset, $value) {
  19. echo "set[$offset] = $value\n";
  20. $this->data[$offset] = $value;
  21. }
  22. public function offsetUnset($offset) {
  23. throw new Exception('Not implemented');
  24. }
  25. public function count() {
  26. return count($this->data);
  27. }
  28. }
  29. } else {
  30. class MyArray implements ArrayAccess, Countable {
  31. private $data = Array('one', 'two', 'three');
  32. public function offsetExists($offset): bool {
  33. return isset($this->data[$offset]);
  34. }
  35. public function offsetGet(mixed $offset): mixed {
  36. return $this->data[$offset];
  37. }
  38. public function offsetSet(mixed $offset, mixed $value): void {
  39. echo "set[$offset] = $value\n";
  40. $this->data[$offset] = $value;
  41. }
  42. public function offsetUnset(mixed $offset): void {
  43. throw new Exception('Not implemented');
  44. }
  45. public function count(): int {
  46. return count($this->data);
  47. }
  48. }
  49. }
  50. $v8 = new V8Js();
  51. $v8->myarr = new MyArray();
  52. /* Call native JavaScript push method, this should cause a count
  53. * offsetSet call. */
  54. $v8->executeString('PHP.myarr.push(23);');
  55. /* Access array from PHP side, should work if JavaScript called
  56. * the write accessor functions. */
  57. var_dump(count($v8->myarr));
  58. var_dump($v8->myarr[3]);
  59. /* And JS side of course should see it too. */
  60. $v8->executeString('var_dump(PHP.myarr.join(","));');
  61. ?>
  62. ===EOF===
  63. --EXPECT--
  64. set[3] = 23
  65. int(4)
  66. int(23)
  67. string(16) "one,two,three,23"
  68. ===EOF===