v8js_array_access.cc 5.2 KB

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