v8js_array_access.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. static zval *v8js_array_access_dispatch(zval *object, const char *method_name, int param_count,
  26. uint32_t index, zval *zvalue_ptr TSRMLS_DC) /* {{{ */
  27. {
  28. zend_class_entry *ce = Z_OBJCE_P(object);
  29. /* Okay, let's call offsetGet. */
  30. zend_fcall_info fci;
  31. zval *php_value;
  32. zval fmember;
  33. INIT_ZVAL(fmember);
  34. ZVAL_STRING(&fmember, method_name, 0);
  35. zval zindex;
  36. INIT_ZVAL(zindex);
  37. ZVAL_LONG(&zindex, index);
  38. fci.size = sizeof(fci);
  39. fci.function_table = &ce->function_table;
  40. fci.function_name = &fmember;
  41. fci.symbol_table = NULL;
  42. fci.retval_ptr_ptr = &php_value;
  43. zval *zindex_ptr = &zindex;
  44. zval **params[2] = { &zindex_ptr, &zvalue_ptr };
  45. fci.param_count = param_count;
  46. fci.params = params;
  47. fci.object_ptr = object;
  48. fci.no_separation = 0;
  49. zend_call_function(&fci, NULL TSRMLS_CC);
  50. return php_value;
  51. }
  52. /* }}} */
  53. void v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  54. {
  55. v8::Isolate *isolate = info.GetIsolate();
  56. v8::Local<v8::Object> self = info.Holder();
  57. V8JS_TSRMLS_FETCH();
  58. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
  59. zval *php_value = v8js_array_access_dispatch(object, "offsetGet", 1, index, NULL TSRMLS_CC);
  60. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  61. zval_ptr_dtor(&php_value);
  62. info.GetReturnValue().Set(ret_value);
  63. }
  64. /* }}} */
  65. void v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
  66. const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  67. {
  68. v8::Isolate *isolate = info.GetIsolate();
  69. v8::Local<v8::Object> self = info.Holder();
  70. V8JS_TSRMLS_FETCH();
  71. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
  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. zval *php_value = v8js_array_access_dispatch(object, "offsetSet", 2, index, zvalue_ptr TSRMLS_CC);
  79. zval_ptr_dtor(&php_value);
  80. /* simply pass back the value to tell we intercepted the call
  81. * as the offsetSet function returns void. */
  82. info.GetReturnValue().Set(value);
  83. /* if PHP wanted to hold on to this value, zend_call_function would
  84. * have bumped the refcount. */
  85. zval_ptr_dtor(&zvalue_ptr);
  86. }
  87. /* }}} */
  88. static int v8js_array_access_get_count_result(zval *object TSRMLS_DC) /* {{{ */
  89. {
  90. zval *php_value = v8js_array_access_dispatch(object, "count", 0, 0, NULL TSRMLS_CC);
  91. if(Z_TYPE_P(php_value) != IS_LONG) {
  92. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-numeric return value from count() method");
  93. return 0;
  94. }
  95. int result = Z_LVAL_P(php_value);
  96. zval_ptr_dtor(&php_value);
  97. return result;
  98. }
  99. /* }}} */
  100. static bool v8js_array_access_isset_p(zval *object, int index TSRMLS_DC) /* {{{ */
  101. {
  102. zval *php_value = v8js_array_access_dispatch(object, "offsetExists", 1, index, NULL TSRMLS_CC);
  103. if(Z_TYPE_P(php_value) != IS_BOOL) {
  104. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Non-boolean return value from offsetExists() method");
  105. return false;
  106. }
  107. bool result = Z_LVAL_P(php_value);
  108. zval_ptr_dtor(&php_value);
  109. return result;
  110. }
  111. /* }}} */
  112. static void v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  113. {
  114. v8::Isolate *isolate = info.GetIsolate();
  115. v8::Local<v8::Object> self = info.Holder();
  116. V8JS_TSRMLS_FETCH();
  117. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
  118. int length = v8js_array_access_get_count_result(object TSRMLS_CC);
  119. info.GetReturnValue().Set(V8JS_INT(length));
  120. }
  121. /* }}} */
  122. void v8js_array_access_deleter(uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean>& info) /* {{{ */
  123. {
  124. v8::Isolate *isolate = info.GetIsolate();
  125. v8::Local<v8::Object> self = info.Holder();
  126. V8JS_TSRMLS_FETCH();
  127. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
  128. zval *php_value = v8js_array_access_dispatch(object, "offsetUnset", 1, index, NULL TSRMLS_CC);
  129. zval_ptr_dtor(&php_value);
  130. info.GetReturnValue().Set(V8JS_BOOL(true));
  131. }
  132. /* }}} */
  133. void v8js_array_access_query(uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) /* {{{ */
  134. {
  135. v8::Isolate *isolate = info.GetIsolate();
  136. v8::Local<v8::Object> self = info.Holder();
  137. V8JS_TSRMLS_FETCH();
  138. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
  139. /* If index is set, then return an integer encoding a v8::PropertyAttribute;
  140. * otherwise we're expected to return an empty handle. */
  141. if(v8js_array_access_isset_p(object, index TSRMLS_CC)) {
  142. info.GetReturnValue().Set(V8JS_UINT(v8::PropertyAttribute::None));
  143. }
  144. }
  145. /* }}} */
  146. void v8js_array_access_enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) /* {{{ */
  147. {
  148. v8::Isolate *isolate = info.GetIsolate();
  149. v8::Local<v8::Object> self = info.Holder();
  150. V8JS_TSRMLS_FETCH();
  151. zval *object = reinterpret_cast<zval *>(self->GetAlignedPointerFromInternalField(1));
  152. int length = v8js_array_access_get_count_result(object TSRMLS_CC);
  153. v8::Local<v8::Array> result = v8::Array::New(isolate, length);
  154. int i = 0;
  155. for(int j = 0; j < length; j ++) {
  156. if(v8js_array_access_isset_p(object, j TSRMLS_CC)) {
  157. result->Set(i ++, V8JS_INT(j));
  158. }
  159. }
  160. result->Set(V8JS_STR("length"), V8JS_INT(i));
  161. info.GetReturnValue().Set(result);
  162. }
  163. /* }}} */
  164. void v8js_array_access_named_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
  165. {
  166. v8::String::Utf8Value cstr(property);
  167. const char *name = ToCString(cstr);
  168. if(strcmp(name, "length") == 0) {
  169. v8js_array_access_length(property, info);
  170. return;
  171. }
  172. v8::Local<v8::Value> ret_value = v8js_named_property_callback(property, info, V8JS_PROP_GETTER);
  173. if(ret_value.IsEmpty()) {
  174. v8::Isolate *isolate = info.GetIsolate();
  175. v8::Local<v8::Array> arr = v8::Array::New(isolate);
  176. v8::Local<v8::Value> prototype = arr->GetPrototype();
  177. if(!prototype->IsObject()) {
  178. /* ehh? Array.prototype not an object? strange, stop. */
  179. info.GetReturnValue().Set(ret_value);
  180. }
  181. ret_value = prototype->ToObject()->Get(property);
  182. }
  183. info.GetReturnValue().Set(ret_value);
  184. }
  185. /* }}} */
  186. /*
  187. * Local variables:
  188. * tab-width: 4
  189. * c-basic-offset: 4
  190. * End:
  191. * vim600: noet sw=4 ts=4 fdm=marker
  192. * vim<600: noet sw=4 ts=4
  193. */