v8js_array_access.cc 7.6 KB

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