v8js_array_access.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. static void php_v8js_array_access_getter(uint32_t index, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  25. {
  26. v8::Isolate *isolate = info.GetIsolate();
  27. v8::Local<v8::Object> self = info.Holder();
  28. V8JS_TSRMLS_FETCH();
  29. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  30. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  31. zend_class_entry *ce = Z_OBJCE_P(object);
  32. /* Okay, let's call offsetGet. */
  33. zend_fcall_info fci;
  34. zval *php_value;
  35. zval fmember;
  36. INIT_ZVAL(fmember);
  37. ZVAL_STRING(&fmember, "offsetGet", 0);
  38. zval zindex;
  39. INIT_ZVAL(zindex);
  40. ZVAL_LONG(&zindex, index);
  41. fci.size = sizeof(fci);
  42. fci.function_table = &ce->function_table;
  43. fci.function_name = &fmember;
  44. fci.symbol_table = NULL;
  45. fci.retval_ptr_ptr = &php_value;
  46. zval *zindex_ptr = &zindex;
  47. zval **zindex_ptr_ptr = &zindex_ptr;
  48. fci.param_count = 1;
  49. fci.params = &zindex_ptr_ptr;
  50. fci.object_ptr = object;
  51. fci.no_separation = 0;
  52. zend_call_function(&fci, NULL TSRMLS_CC);
  53. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  54. zval_ptr_dtor(&php_value);
  55. info.GetReturnValue().Set(ret_value);
  56. }
  57. /* }}} */
  58. static void php_v8js_array_access_setter(uint32_t index, v8::Local<v8::Value> value,
  59. const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  60. {
  61. v8::Isolate *isolate = info.GetIsolate();
  62. v8::Local<v8::Object> self = info.Holder();
  63. V8JS_TSRMLS_FETCH();
  64. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  65. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  66. zend_class_entry *ce = Z_OBJCE_P(object);
  67. /* Okay, let's call offsetSet. */
  68. zend_fcall_info fci;
  69. zval *php_value;
  70. zval fmember;
  71. INIT_ZVAL(fmember);
  72. ZVAL_STRING(&fmember, "offsetSet", 0);
  73. zval zindex;
  74. INIT_ZVAL(zindex);
  75. ZVAL_LONG(&zindex, index);
  76. zval *zvalue_ptr;
  77. MAKE_STD_ZVAL(zvalue_ptr);
  78. if (v8js_to_zval(value, zvalue_ptr, 0, isolate TSRMLS_CC) != SUCCESS) {
  79. info.GetReturnValue().Set(v8::Handle<v8::Value>());
  80. return;
  81. }
  82. fci.size = sizeof(fci);
  83. fci.function_table = &ce->function_table;
  84. fci.function_name = &fmember;
  85. fci.symbol_table = NULL;
  86. fci.retval_ptr_ptr = &php_value;
  87. zval *zindex_ptr = &zindex;
  88. zval **params[2] = { &zindex_ptr, &zvalue_ptr };
  89. fci.param_count = 2;
  90. fci.params = params;
  91. fci.object_ptr = object;
  92. fci.no_separation = 0;
  93. zend_call_function(&fci, NULL TSRMLS_CC);
  94. zval_ptr_dtor(&php_value);
  95. /* simply pass back the value to tell we intercepted the call
  96. * as the offsetSet function returns void. */
  97. info.GetReturnValue().Set(value);
  98. /* if PHP wanted to hold on to this value, zend_call_function would
  99. * have bumped the refcount. */
  100. zval_ptr_dtor(&zvalue_ptr);
  101. }
  102. /* }}} */
  103. static void php_v8js_array_access_length(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) /* {{{ */
  104. {
  105. v8::Isolate *isolate = info.GetIsolate();
  106. v8::Local<v8::Object> self = info.Holder();
  107. V8JS_TSRMLS_FETCH();
  108. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  109. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  110. zend_class_entry *ce = Z_OBJCE_P(object);
  111. zend_fcall_info fci;
  112. zval *php_value;
  113. zval fmember;
  114. INIT_ZVAL(fmember);
  115. ZVAL_STRING(&fmember, "count", 0);
  116. fci.size = sizeof(fci);
  117. fci.function_table = &ce->function_table;
  118. fci.function_name = &fmember;
  119. fci.symbol_table = NULL;
  120. fci.retval_ptr_ptr = &php_value;
  121. fci.param_count = 0;
  122. fci.params = NULL;
  123. fci.object_ptr = object;
  124. fci.no_separation = 0;
  125. zend_call_function(&fci, NULL TSRMLS_CC);
  126. v8::Local<v8::Value> ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  127. zval_ptr_dtor(&php_value);
  128. info.GetReturnValue().Set(ret_value);
  129. }
  130. /* }}} */
  131. v8::Handle<v8::Value> php_v8js_array_access_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  132. {
  133. v8::Local<v8::ObjectTemplate> inst_tpl = v8::ObjectTemplate::New(isolate);
  134. inst_tpl->SetIndexedPropertyHandler(php_v8js_array_access_getter,
  135. php_v8js_array_access_setter);
  136. inst_tpl->SetAccessor(V8JS_STR("length"), php_v8js_array_access_length);
  137. v8::Handle<v8::Object> newobj = inst_tpl->NewInstance();
  138. newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), v8::External::New(isolate, value));
  139. /* Change prototype of `newobj' to that of Array */
  140. v8::Local<v8::Array> arr = v8::Array::New(isolate);
  141. newobj->SetPrototype(arr->GetPrototype());
  142. return newobj;
  143. }
  144. /* }}} */
  145. /*
  146. * Local variables:
  147. * tab-width: 4
  148. * c-basic-offset: 4
  149. * End:
  150. * vim600: noet sw=4 ts=4 fdm=marker
  151. * vim<600: noet sw=4 ts=4
  152. */