v8js_convert.cc 6.9 KB

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