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