v8js_array_access.cc 7.6 KB

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