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