v8js_array_access.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include "v8js_object_export.h"
  25. void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  26. {
  27. v8::Isolate *isolate = info.GetIsolate();
  28. v8::Local<v8::Object> self = info.Holder();
  29. V8JS_TSRMLS_FETCH();
  30. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  31. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  32. zend_class_entry *ce = Z_OBJCE_P(object);
  33. /* Okay, let's call offsetGet. */
  34. zend_fcall_info fci;
  35. zval *php_value;
  36. zval fmember;
  37. INIT_ZVAL(fmember);
  38. ZVAL_STRING(&fmember, "offsetGet", 0);
  39. zval zindex;
  40. INIT_ZVAL(zindex);
  41. ZVAL_LONG(&zindex, index);
  42. fci.size = sizeof(fci);
  43. fci.function_table = &ce->function_table;
  44. fci.function_name = &fmember;
  45. fci.symbol_table = NULL;
  46. fci.retval_ptr_ptr = &php_value;
  47. zval *zindex_ptr = &zindex;
  48. zval **zindex_ptr_ptr = &zindex_ptr;
  49. fci.param_count = 1;
  50. fci.params = &zindex_ptr_ptr;
  51. fci.object_ptr = object;
  52. fci.no_separation = 0;
  53. zend_call_function(&fci, NULL TSRMLS_CC);
  54. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  55. zval_ptr_dtor(&php_value);
  56. info.GetReturnValue().Set(ret_value);
  57. }
  58. /* }}} */
  59. void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
  60. const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  61. {
  62. v8::Isolate *isolate = info.GetIsolate();
  63. v8::Local<v8::Object> self = info.Holder();
  64. V8JS_TSRMLS_FETCH();
  65. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  66. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  67. zend_class_entry *ce = Z_OBJCE_P(object);
  68. /* Okay, let's call offsetSet. */
  69. zend_fcall_info fci;
  70. zval *php_value;
  71. zval fmember;
  72. INIT_ZVAL(fmember);
  73. ZVAL_STRING(&fmember, "offsetSet", 0);
  74. zval zindex;
  75. INIT_ZVAL(zindex);
  76. ZVAL_LONG(&zindex, index);
  77. zval *zvalue_ptr;
  78. MAKE_STD_ZVAL(zvalue_ptr);
  79. if (v8js_to_zval(value, zvalue_ptr, 0, isolate TSRMLS_CC) != SUCCESS) {
  80. info.GetReturnValue().Set(v8::Handle<v8::Value>());
  81. return;
  82. }
  83. fci.size = sizeof(fci);
  84. fci.function_table = &ce->function_table;
  85. fci.function_name = &fmember;
  86. fci.symbol_table = NULL;
  87. fci.retval_ptr_ptr = &php_value;
  88. zval *zindex_ptr = &zindex;
  89. zval **params[2] = { &zindex_ptr, &zvalue_ptr };
  90. fci.param_count = 2;
  91. fci.params = params;
  92. fci.object_ptr = object;
  93. fci.no_separation = 0;
  94. zend_call_function(&fci, NULL TSRMLS_CC);
  95. zval_ptr_dtor(&php_value);
  96. /* simply pass back the value to tell we intercepted the call
  97. * as the offsetSet function returns void. */
  98. info.GetReturnValue().Set(value);
  99. /* if PHP wanted to hold on to this value, zend_call_function would
  100. * have bumped the refcount. */
  101. zval_ptr_dtor(&zvalue_ptr);
  102. }
  103. /* }}} */
  104. static int php_v8js_array_access_get_count_result(zval *object TSRMLS_DC) /* {{{ */
  105. {
  106. zend_class_entry *ce = Z_OBJCE_P(object);
  107. zend_fcall_info fci;
  108. zval *php_value;
  109. zval fmember;
  110. INIT_ZVAL(fmember);
  111. ZVAL_STRING(&fmember, "count", 0);
  112. fci.size = sizeof(fci);
  113. fci.function_table = &ce->function_table;
  114. fci.function_name = &fmember;
  115. fci.symbol_table = NULL;
  116. fci.retval_ptr_ptr = &php_value;
  117. fci.param_count = 0;
  118. fci.params = NULL;
  119. fci.object_ptr = object;
  120. fci.no_separation = 0;
  121. zend_call_function(&fci, NULL TSRMLS_CC);
  122. if(Z_TYPE_P(php_value) != IS_LONG) {
  123. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-numeric return value from count() method");
  124. return 0;
  125. }
  126. int result = Z_LVAL_P(php_value);
  127. zval_ptr_dtor(&php_value);
  128. return result;
  129. }
  130. /* }}} */
  131. static bool php_v8js_array_access_isset_p(zval *object, int index TSRMLS_DC) /* {{{ */
  132. {
  133. zend_class_entry *ce = Z_OBJCE_P(object);
  134. /* Okay, let's call offsetExists. */
  135. zend_fcall_info fci;
  136. zval *php_value;
  137. zval fmember;
  138. INIT_ZVAL(fmember);
  139. ZVAL_STRING(&fmember, "offsetExists", 0);
  140. zval zindex;
  141. INIT_ZVAL(zindex);
  142. ZVAL_LONG(&zindex, index);
  143. fci.size = sizeof(fci);
  144. fci.function_table = &ce->function_table;
  145. fci.function_name = &fmember;
  146. fci.symbol_table = NULL;
  147. fci.retval_ptr_ptr = &php_value;
  148. zval *zindex_ptr = &zindex;
  149. zval **zindex_ptr_ptr = &zindex_ptr;
  150. fci.param_count = 1;
  151. fci.params = &zindex_ptr_ptr;
  152. fci.object_ptr = object;
  153. fci.no_separation = 0;
  154. zend_call_function(&fci, NULL TSRMLS_CC);
  155. if(Z_TYPE_P(php_value) != IS_BOOL) {
  156. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-boolean return value from offsetExists() method");
  157. return false;
  158. }
  159. bool result = Z_LVAL_P(php_value);
  160. zval_ptr_dtor(&php_value);
  161. return result;
  162. }
  163. /* }}} */
  164. void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  165. {
  166. v8::Isolate *isolate = info.GetIsolate();
  167. v8::Local<v8::Object> self = info.Holder();
  168. V8JS_TSRMLS_FETCH();
  169. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  170. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  171. int length = php_v8js_array_access_get_count_result(object TSRMLS_CC);
  172. info.GetReturnValue().Set(V8JS_INT(length));
  173. }
  174. /* }}} */
  175. void php_v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) /* {{{ */
  176. {
  177. v8::Isolate *isolate = info.GetIsolate();
  178. v8::Local<v8::Object> self = info.Holder();
  179. V8JS_TSRMLS_FETCH();
  180. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  181. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  182. int length = php_v8js_array_access_get_count_result(object TSRMLS_CC);
  183. v8::Local<v8::Array> result = v8::Array::New(isolate, length);
  184. int i = 0;
  185. for(int j = 0; j < length; j ++) {
  186. if(php_v8js_array_access_isset_p(object, j TSRMLS_CC)) {
  187. result->Set(i ++, V8JS_INT(j));
  188. }
  189. }
  190. result->Set(V8JS_STR("length"), V8JS_INT(i));
  191. info.GetReturnValue().Set(result);
  192. }
  193. /* }}} */
  194. void php_v8js_array_access_named_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
  195. {
  196. v8::String::Utf8Value cstr(property);
  197. const char *name = ToCString(cstr);
  198. if(strcmp(name, "length") == 0) {
  199. php_v8js_array_access_length(property, info);
  200. return;
  201. }
  202. v8::Local<v8::Value> ret_value = php_v8js_named_property_callback(property, info, V8JS_PROP_GETTER);
  203. if(ret_value.IsEmpty()) {
  204. v8::Isolate *isolate = info.GetIsolate();
  205. v8::Local<v8::Array> arr = v8::Array::New(isolate);
  206. v8::Local<v8::Value> prototype = arr->GetPrototype();
  207. if(!prototype->IsObject()) {
  208. /* ehh? Array.prototype not an object? strange, stop. */
  209. info.GetReturnValue().Set(ret_value);
  210. }
  211. ret_value = prototype->ToObject()->Get(property);
  212. }
  213. info.GetReturnValue().Set(ret_value);
  214. }
  215. /* }}} */
  216. /*
  217. * Local variables:
  218. * tab-width: 4
  219. * c-basic-offset: 4
  220. * End:
  221. * vim600: noet sw=4 ts=4 fdm=marker
  222. * vim<600: noet sw=4 ts=4
  223. */