v8js_array_access.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_setter(uint32_t index, v8::Local<v8::Value> value,
  57. const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  58. {
  59. v8::Isolate *isolate = info.GetIsolate();
  60. v8::Local<v8::Object> self = info.Holder();
  61. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(0));
  62. zend_class_entry *ce = Z_OBJCE_P(object);
  63. /* Okay, let's call offsetSet. */
  64. zend_fcall_info fci;
  65. zval *php_value;
  66. zval fmember;
  67. INIT_ZVAL(fmember);
  68. ZVAL_STRING(&fmember, "offsetSet", 0);
  69. zval zindex;
  70. INIT_ZVAL(zindex);
  71. ZVAL_LONG(&zindex, index);
  72. zval *zvalue_ptr;
  73. MAKE_STD_ZVAL(zvalue_ptr);
  74. if (v8js_to_zval(value, zvalue_ptr, 0, isolate TSRMLS_CC) != SUCCESS) {
  75. info.GetReturnValue().Set(v8::Handle<v8::Value>());
  76. return;
  77. }
  78. fci.size = sizeof(fci);
  79. fci.function_table = &ce->function_table;
  80. fci.function_name = &fmember;
  81. fci.symbol_table = NULL;
  82. fci.retval_ptr_ptr = &php_value;
  83. zval *zindex_ptr = &zindex;
  84. zval **params[2] = { &zindex_ptr, &zvalue_ptr };
  85. fci.param_count = 2;
  86. fci.params = params;
  87. fci.object_ptr = object;
  88. fci.no_separation = 0;
  89. zend_call_function(&fci, NULL TSRMLS_CC);
  90. zval_ptr_dtor(&php_value);
  91. /* simply pass back the value to tell we intercepted the call
  92. * as the offsetSet function returns void. */
  93. info.GetReturnValue().Set(value);
  94. }
  95. /* }}} */
  96. static void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  97. {
  98. v8::Isolate *isolate = info.GetIsolate();
  99. v8::Local<v8::Object> self = info.Holder();
  100. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(0));
  101. zend_class_entry *ce = Z_OBJCE_P(object);
  102. zend_fcall_info fci;
  103. zval *php_value;
  104. zval fmember;
  105. INIT_ZVAL(fmember);
  106. ZVAL_STRING(&fmember, "count", 0);
  107. fci.size = sizeof(fci);
  108. fci.function_table = &ce->function_table;
  109. fci.function_name = &fmember;
  110. fci.symbol_table = NULL;
  111. fci.retval_ptr_ptr = &php_value;
  112. fci.param_count = 0;
  113. fci.params = NULL;
  114. fci.object_ptr = object;
  115. fci.no_separation = 0;
  116. zend_call_function(&fci, NULL TSRMLS_CC);
  117. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  118. zval_ptr_dtor(&php_value);
  119. info.GetReturnValue().Set(ret_value);
  120. }
  121. /* }}} */
  122. v8::Handle<v8::Value> php_v8js_array_access_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  123. {
  124. v8::Local<v8::ObjectTemplate> inst_tpl = v8::ObjectTemplate::New(isolate);
  125. inst_tpl->SetIndexedPropertyHandler(php_v8js_array_access_getter,
  126. php_v8js_array_access_setter);
  127. inst_tpl->SetAccessor(V8JS_STR("length"), php_v8js_array_access_length);
  128. inst_tpl->SetInternalFieldCount(1);
  129. v8::Handle<v8::Object> newobj = inst_tpl->NewInstance();
  130. newobj->SetAlignedPointerInInternalField(0, value);
  131. /* Change prototype of `newobj' to that of Array */
  132. v8::Local<v8::Array> arr = v8::Array::New(isolate);
  133. newobj->SetPrototype(arr->GetPrototype());
  134. return newobj;
  135. }
  136. /* }}} */
  137. /*
  138. * Local variables:
  139. * tab-width: 4
  140. * c-basic-offset: 4
  141. * End:
  142. * vim600: noet sw=4 ts=4 fdm=marker
  143. * vim<600: noet sw=4 ts=4
  144. */