v8js_convert.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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: Jani Taskinen <[email protected]> |
  10. | Author: Patrick Reilly <[email protected]> |
  11. +----------------------------------------------------------------------+
  12. */
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif
  16. extern "C" {
  17. #include "php.h"
  18. #include "ext/date/php_date.h"
  19. #include "ext/standard/php_string.h"
  20. #include "zend_interfaces.h"
  21. #include "zend_closures.h"
  22. }
  23. #include "php_v8js_macros.h"
  24. #include "v8js_object_export.h"
  25. #include "v8js_v8object_class.h"
  26. #include "v8js_v8.h"
  27. #include <stdexcept>
  28. #include <limits>
  29. static int v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
  30. {
  31. int i;
  32. char *key;
  33. ulong index, idx = 0;
  34. uint key_len;
  35. HashPosition pos;
  36. zend_hash_internal_pointer_reset_ex(myht, &pos);
  37. for (;; zend_hash_move_forward_ex(myht, &pos)) {
  38. i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
  39. if (i == HASH_KEY_NON_EXISTANT)
  40. break;
  41. if (i == HASH_KEY_IS_STRING || index != idx) {
  42. return 1;
  43. }
  44. idx++;
  45. }
  46. return 0;
  47. }
  48. /* }}} */
  49. static v8::Handle<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  50. {
  51. HashTable *myht = HASH_OF(value);
  52. int i = myht ? zend_hash_num_elements(myht) : 0;
  53. /* Return object if dealing with assoc array */
  54. if (i > 0 && v8js_is_assoc_array(myht TSRMLS_CC)) {
  55. return v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  56. }
  57. v8::Local<v8::Array> newarr;
  58. /* Prevent recursion */
  59. if (myht && myht->nApplyCount > 1) {
  60. return V8JS_NULL;
  61. }
  62. newarr = v8::Array::New(isolate, i);
  63. if (i > 0)
  64. {
  65. zval **data;
  66. ulong index = 0;
  67. HashTable *tmp_ht;
  68. HashPosition pos;
  69. for (zend_hash_internal_pointer_reset_ex(myht, &pos);
  70. SUCCESS == zend_hash_get_current_data_ex(myht, (void **) &data, &pos);
  71. zend_hash_move_forward_ex(myht, &pos)
  72. ) {
  73. tmp_ht = HASH_OF(*data);
  74. if (tmp_ht) {
  75. tmp_ht->nApplyCount++;
  76. }
  77. newarr->Set(index++, zval_to_v8js(*data, isolate TSRMLS_CC));
  78. if (tmp_ht) {
  79. tmp_ht->nApplyCount--;
  80. }
  81. }
  82. }
  83. return newarr;
  84. }
  85. /* }}} */
  86. v8::Handle<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  87. {
  88. v8::Handle<v8::Value> jsValue;
  89. long v;
  90. zend_class_entry *ce;
  91. switch (Z_TYPE_P(value))
  92. {
  93. case IS_ARRAY:
  94. jsValue = v8js_hash_to_jsarr(value, isolate TSRMLS_CC);
  95. break;
  96. case IS_OBJECT:
  97. if (V8JSG(use_date)) {
  98. ce = php_date_get_date_ce();
  99. if (instanceof_function(Z_OBJCE_P(value), ce TSRMLS_CC)) {
  100. zval *dtval;
  101. zend_call_method_with_0_params(&value, NULL, NULL, "getTimestamp", &dtval);
  102. if (dtval) {
  103. jsValue = V8JS_DATE(((double)Z_LVAL_P(dtval) * 1000.0));
  104. zval_ptr_dtor(&dtval);
  105. }
  106. else
  107. jsValue = V8JS_NULL;
  108. } else
  109. jsValue = v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  110. } else
  111. jsValue = v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  112. break;
  113. case IS_STRING:
  114. jsValue = V8JS_STRL(Z_STRVAL_P(value), Z_STRLEN_P(value));
  115. break;
  116. case IS_LONG:
  117. v = Z_LVAL_P(value);
  118. /* On Windows there are max and min macros, which would clobber the
  119. * method names of std::numeric_limits< > otherwise. */
  120. #undef max
  121. #undef min
  122. if (v < - std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
  123. jsValue = V8JS_FLOAT((double)v);
  124. } else {
  125. jsValue = V8JS_INT(v);
  126. }
  127. break;
  128. case IS_DOUBLE:
  129. jsValue = V8JS_FLOAT(Z_DVAL_P(value));
  130. break;
  131. case IS_BOOL:
  132. jsValue = V8JS_BOOL(Z_BVAL_P(value));
  133. break;
  134. default:
  135. case IS_NULL:
  136. jsValue = V8JS_NULL;
  137. break;
  138. }
  139. return jsValue;
  140. }
  141. /* }}} */
  142. int v8js_to_zval(v8::Handle<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  143. {
  144. if (jsValue->IsString())
  145. {
  146. v8::String::Utf8Value str(jsValue);
  147. const char *cstr = ToCString(str);
  148. RETVAL_STRINGL(cstr, jsValue->ToString()->Utf8Length(), 1);
  149. // RETVAL_STRING(cstr, 1);
  150. }
  151. else if (jsValue->IsBoolean())
  152. {
  153. RETVAL_BOOL(jsValue->Uint32Value());
  154. }
  155. else if (jsValue->IsInt32() || jsValue->IsUint32())
  156. {
  157. RETVAL_LONG((long) jsValue->IntegerValue());
  158. }
  159. else if (jsValue->IsNumber())
  160. {
  161. RETVAL_DOUBLE(jsValue->NumberValue());
  162. }
  163. else if (jsValue->IsDate()) /* Return as a PHP DateTime object */
  164. {
  165. v8::String::Utf8Value str(jsValue);
  166. const char *cstr = ToCString(str);
  167. /* cstr has two timezone specifications:
  168. *
  169. * example from Linux:
  170. * Mon Sep 08 1975 09:00:00 GMT+0000 (UTC)
  171. *
  172. * example from Windows:
  173. * Mon Sep 08 1975 11:00:00 GMT+0200 (W. Europe Daylight Time)
  174. *
  175. * ... problem is, that PHP can't parse the second timezone
  176. * specification as returned by v8 running on Windows. And as a
  177. * matter of that fails due to inconsistent second timezone spec
  178. */
  179. char *date_str = estrdup(cstr);
  180. char *paren_ptr = strchr(date_str, '(');
  181. if (paren_ptr != NULL) {
  182. *paren_ptr = 0;
  183. }
  184. zend_class_entry *ce = php_date_get_date_ce();
  185. #if PHP_VERSION_ID < 50304
  186. zval *param;
  187. MAKE_STD_ZVAL(param);
  188. ZVAL_STRING(param, date_str, 0);
  189. object_init_ex(return_value, ce TSRMLS_CC);
  190. zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, param);
  191. zval_ptr_dtor(&param);
  192. if (EG(exception)) {
  193. return FAILURE;
  194. }
  195. #else
  196. php_date_instantiate(ce, return_value TSRMLS_CC);
  197. if (!php_date_initialize((php_date_obj *) zend_object_store_get_object(return_value TSRMLS_CC), date_str, strlen(date_str), NULL, NULL, 0 TSRMLS_CC)) {
  198. efree(date_str);
  199. return FAILURE;
  200. }
  201. efree(date_str);
  202. #endif
  203. }
  204. else if (jsValue->IsObject())
  205. {
  206. v8::Handle<v8::Object> self = v8::Handle<v8::Object>::Cast(jsValue);
  207. // if this is a wrapped PHP object, then just unwrap it.
  208. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  209. if (!php_object.IsEmpty()) {
  210. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  211. RETVAL_ZVAL(object, 1, 0);
  212. return SUCCESS;
  213. }
  214. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  215. array_init(return_value);
  216. return v8js_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate TSRMLS_CC);
  217. } else {
  218. v8js_v8object_create(return_value, jsValue, flags, isolate TSRMLS_CC);
  219. return SUCCESS;
  220. }
  221. }
  222. else /* types External, RegExp, Undefined and Null are considered NULL */
  223. {
  224. RETVAL_NULL();
  225. }
  226. return SUCCESS;
  227. }
  228. /* }}} */
  229. /*
  230. * Local variables:
  231. * tab-width: 4
  232. * c-basic-offset: 4
  233. * End:
  234. * vim600: noet sw=4 ts=4 fdm=marker
  235. * vim<600: noet sw=4 ts=4
  236. */