array_access_002.phpt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. echo 'count() = ', count($this->data), "\n";
  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. count() = 3
  44. set[3] = 23
  45. count() = 4
  46. int(4)
  47. int(23)
  48. count() = 4
  49. string(16) "one,two,three,23"
  50. ===EOF===