array_access_002.phpt 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #[AllowDynamicProperties]
  10. class MyArray implements ArrayAccess, Countable {
  11. private $data = Array('one', 'two', 'three');
  12. public function offsetExists($offset): bool {
  13. return isset($this->data[$offset]);
  14. }
  15. public function offsetGet(mixed $offset): mixed {
  16. return $this->data[$offset];
  17. }
  18. public function offsetSet(mixed $offset, mixed $value): void {
  19. echo "set[$offset] = $value\n";
  20. $this->data[$offset] = $value;
  21. }
  22. public function offsetUnset(mixed $offset): void {
  23. throw new Exception('Not implemented');
  24. }
  25. public function count(): int {
  26. return count($this->data);
  27. }
  28. }
  29. $v8 = new V8Js();
  30. $v8->myarr = new MyArray();
  31. /* Call native JavaScript push method, this should cause a count
  32. * offsetSet call. */
  33. $v8->executeString('PHP.myarr.push(23);');
  34. /* Access array from PHP side, should work if JavaScript called
  35. * the write accessor functions. */
  36. var_dump(count($v8->myarr));
  37. var_dump($v8->myarr[3]);
  38. /* And JS side of course should see it too. */
  39. $v8->executeString('var_dump(PHP.myarr.join(","));');
  40. ?>
  41. ===EOF===
  42. --EXPECT--
  43. set[3] = 23
  44. int(4)
  45. int(23)
  46. string(16) "one,two,three,23"
  47. ===EOF===