v8js_array_access.cc 7.3 KB

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