v8js_array_access.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  105. {
  106. v8::Isolate *isolate = info.GetIsolate();
  107. v8::Local<v8::Object> self = info.Holder();
  108. V8JS_TSRMLS_FETCH();
  109. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  110. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  111. zend_class_entry *ce = Z_OBJCE_P(object);
  112. zend_fcall_info fci;
  113. zval *php_value;
  114. zval fmember;
  115. INIT_ZVAL(fmember);
  116. ZVAL_STRING(&fmember, "count", 0);
  117. fci.size = sizeof(fci);
  118. fci.function_table = &ce->function_table;
  119. fci.function_name = &fmember;
  120. fci.symbol_table = NULL;
  121. fci.retval_ptr_ptr = &php_value;
  122. fci.param_count = 0;
  123. fci.params = NULL;
  124. fci.object_ptr = object;
  125. fci.no_separation = 0;
  126. zend_call_function(&fci, NULL TSRMLS_CC);
  127. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  128. zval_ptr_dtor(&php_value);
  129. info.GetReturnValue().Set(ret_value);
  130. }
  131. /* }}} */
  132. void php_v8js_array_access_named_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
  133. {
  134. v8::String::Utf8Value cstr(property);
  135. const char *name = ToCString(cstr);
  136. if(strcmp(name, "length") == 0) {
  137. php_v8js_array_access_length(property, info);
  138. return;
  139. }
  140. v8::Local<v8::Value> ret_value = php_v8js_named_property_callback(property, info, V8JS_PROP_GETTER);
  141. if(ret_value.IsEmpty()) {
  142. v8::Isolate *isolate = info.GetIsolate();
  143. v8::Local<v8::Array> arr = v8::Array::New(isolate);
  144. v8::Local<v8::Value> prototype = arr->GetPrototype();
  145. if(!prototype->IsObject()) {
  146. /* ehh? Array.prototype not an object? strange, stop. */
  147. info.GetReturnValue().Set(ret_value);
  148. }
  149. ret_value = prototype->ToObject()->Get(property);
  150. }
  151. info.GetReturnValue().Set(ret_value);
  152. }
  153. /* }}} */
  154. /*
  155. * Local variables:
  156. * tab-width: 4
  157. * c-basic-offset: 4
  158. * End:
  159. * vim600: noet sw=4 ts=4 fdm=marker
  160. * vim<600: noet sw=4 ts=4
  161. */