v8js_array_access.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2013 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Stefan Siegl <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #endif
  15. extern "C" {
  16. #include "php.h"
  17. #include "ext/date/php_date.h"
  18. #include "ext/standard/php_string.h"
  19. #include "zend_interfaces.h"
  20. #include "zend_closures.h"
  21. }
  22. #include "php_v8js_macros.h"
  23. #include "v8js_array_access.h"
  24. static void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  25. {
  26. v8::Isolate *isolate = info.GetIsolate();
  27. v8::Local<v8::Object> self = info.Holder();
  28. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(0));
  29. zend_class_entry *ce = Z_OBJCE_P(object);
  30. /* Okay, let's call offsetGet. */
  31. zend_fcall_info fci;
  32. zval *php_value;
  33. zval fmember;
  34. INIT_ZVAL(fmember);
  35. ZVAL_STRING(&fmember, "offsetGet", 0);
  36. zval zindex;
  37. INIT_ZVAL(zindex);
  38. ZVAL_LONG(&zindex, index);
  39. fci.size = sizeof(fci);
  40. fci.function_table = &ce->function_table;
  41. fci.function_name = &fmember;
  42. fci.symbol_table = NULL;
  43. fci.retval_ptr_ptr = &php_value;
  44. zval *zindex_ptr = &zindex;
  45. zval **zindex_ptr_ptr = &zindex_ptr;
  46. fci.param_count = 1;
  47. fci.params = &zindex_ptr_ptr;
  48. fci.object_ptr = object;
  49. fci.no_separation = 0;
  50. zend_call_function(&fci, NULL TSRMLS_CC);
  51. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  52. zval_ptr_dtor(&php_value);
  53. info.GetReturnValue().Set(ret_value);
  54. }
  55. /* }}} */
  56. static void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  57. {
  58. v8::Isolate *isolate = info.GetIsolate();
  59. v8::Local<v8::Object> self = info.Holder();
  60. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(0));
  61. zend_class_entry *ce = Z_OBJCE_P(object);
  62. zend_fcall_info fci;
  63. zval *php_value;
  64. zval fmember;
  65. INIT_ZVAL(fmember);
  66. ZVAL_STRING(&fmember, "count", 0);
  67. fci.size = sizeof(fci);
  68. fci.function_table = &ce->function_table;
  69. fci.function_name = &fmember;
  70. fci.symbol_table = NULL;
  71. fci.retval_ptr_ptr = &php_value;
  72. fci.param_count = 0;
  73. fci.params = NULL;
  74. fci.object_ptr = object;
  75. fci.no_separation = 0;
  76. zend_call_function(&fci, NULL TSRMLS_CC);
  77. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  78. zval_ptr_dtor(&php_value);
  79. info.GetReturnValue().Set(ret_value);
  80. }
  81. /* }}} */
  82. v8::Handle<v8::Value> php_v8js_array_access_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  83. {
  84. v8::Local<v8::ObjectTemplate> inst_tpl = v8::ObjectTemplate::New(isolate);
  85. inst_tpl->SetIndexedPropertyHandler(php_v8js_array_access_getter);
  86. inst_tpl->SetAccessor(V8JS_STR("length"), php_v8js_array_access_length);
  87. inst_tpl->SetInternalFieldCount(1);
  88. v8::Handle<v8::Object> newobj = inst_tpl->NewInstance();
  89. newobj->SetAlignedPointerInInternalField(0, value);
  90. /* Change prototype of `newobj' to that of Array */
  91. v8::Local<v8::Array> arr = v8::Array::New(isolate);
  92. newobj->SetPrototype(arr->GetPrototype());
  93. return newobj;
  94. }
  95. /* }}} */