v8js_convert.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #include <stdexcept>
  18. #include <limits>
  19. #include "php_v8js_macros.h"
  20. #include "v8js_object_export.h"
  21. #include "v8js_v8object_class.h"
  22. #include "v8js_v8.h"
  23. extern "C" {
  24. #include "php.h"
  25. #include "ext/date/php_date.h"
  26. #include "ext/standard/php_string.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_closures.h"
  29. }
  30. static int v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
  31. {
  32. zend_string *key;
  33. ulong index, idx = 0;
  34. ZEND_HASH_FOREACH_KEY(myht, index, key) {
  35. if(key) {
  36. // HASH_KEY_IS_STRING
  37. return 1;
  38. }
  39. if(index != idx) {
  40. return 1;
  41. }
  42. idx ++;
  43. } ZEND_HASH_FOREACH_END();
  44. return 0;
  45. }
  46. /* }}} */
  47. static v8::Handle<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  48. {
  49. HashTable *myht = HASH_OF(value);
  50. int i = myht ? zend_hash_num_elements(myht) : 0;
  51. /* Return object if dealing with assoc array */
  52. if (i > 0 && v8js_is_assoc_array(myht TSRMLS_CC)) {
  53. return v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  54. }
  55. v8::Local<v8::Array> newarr;
  56. /* Prevent recursion */
  57. if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 1) {
  58. return V8JS_NULL;
  59. }
  60. newarr = v8::Array::New(isolate, i);
  61. if (i > 0)
  62. {
  63. zval *data;
  64. ulong index = 0;
  65. HashTable *tmp_ht;
  66. ZEND_HASH_FOREACH_VAL(myht, data) {
  67. tmp_ht = HASH_OF(data);
  68. if (tmp_ht) {
  69. ZEND_HASH_INC_APPLY_COUNT(myht);
  70. }
  71. newarr->Set(index++, zval_to_v8js(data, isolate TSRMLS_CC));
  72. if (tmp_ht) {
  73. ZEND_HASH_DEC_APPLY_COUNT(myht);
  74. }
  75. } ZEND_HASH_FOREACH_END();
  76. }
  77. return newarr;
  78. }
  79. /* }}} */
  80. v8::Handle<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  81. {
  82. v8::Handle<v8::Value> jsValue;
  83. long v;
  84. zend_class_entry *ce;
  85. switch (Z_TYPE_P(value))
  86. {
  87. case IS_INDIRECT:
  88. jsValue = zval_to_v8js(Z_INDIRECT_P(value), isolate);
  89. break;
  90. case IS_REFERENCE:
  91. jsValue = zval_to_v8js(Z_REFVAL_P(value), isolate);
  92. break;
  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. jsValue = V8JS_DATE(((double)Z_LVAL(dtval) * 1000.0));
  103. zval_dtor(&dtval);
  104. } else
  105. jsValue = v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  106. } else
  107. jsValue = v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  108. break;
  109. case IS_STRING:
  110. jsValue = V8JS_ZSTR(Z_STR_P(value));
  111. break;
  112. case IS_LONG:
  113. v = Z_LVAL_P(value);
  114. /* On Windows there are max and min macros, which would clobber the
  115. * method names of std::numeric_limits< > otherwise. */
  116. #undef max
  117. #undef min
  118. if (v < - std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
  119. jsValue = V8JS_FLOAT((double)v);
  120. } else {
  121. jsValue = V8JS_INT(v);
  122. }
  123. break;
  124. case IS_DOUBLE:
  125. jsValue = V8JS_FLOAT(Z_DVAL_P(value));
  126. break;
  127. case IS_TRUE:
  128. jsValue = V8JS_TRUE();
  129. break;
  130. case IS_FALSE:
  131. jsValue = V8JS_FALSE();
  132. break;
  133. case IS_NULL:
  134. jsValue = V8JS_NULL;
  135. break;
  136. case IS_UNDEF:
  137. default:
  138. /* undefined -> return v8::Value left empty */
  139. jsValue = v8::Undefined(isolate);
  140. break;
  141. }
  142. return jsValue;
  143. }
  144. /* }}} */
  145. int v8js_to_zval(v8::Handle<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  146. {
  147. if (jsValue->IsString())
  148. {
  149. v8::String::Utf8Value str(jsValue);
  150. const char *cstr = ToCString(str);
  151. RETVAL_STRINGL(cstr, jsValue->ToString()->Utf8Length());
  152. }
  153. else if (jsValue->IsBoolean())
  154. {
  155. RETVAL_BOOL(jsValue->Uint32Value());
  156. }
  157. else if (jsValue->IsInt32() || jsValue->IsUint32())
  158. {
  159. RETVAL_LONG((long) jsValue->IntegerValue());
  160. }
  161. else if (jsValue->IsNumber())
  162. {
  163. RETVAL_DOUBLE(jsValue->NumberValue());
  164. }
  165. else if (jsValue->IsDate()) /* Return as a PHP DateTime object */
  166. {
  167. v8::String::Utf8Value str(jsValue);
  168. const char *cstr = ToCString(str);
  169. /* cstr has two timezone specifications:
  170. *
  171. * example from Linux:
  172. * Mon Sep 08 1975 09:00:00 GMT+0000 (UTC)
  173. *
  174. * example from Windows:
  175. * Mon Sep 08 1975 11:00:00 GMT+0200 (W. Europe Daylight Time)
  176. *
  177. * ... problem is, that PHP can't parse the second timezone
  178. * specification as returned by v8 running on Windows. And as a
  179. * matter of that fails due to inconsistent second timezone spec
  180. */
  181. char *date_str = estrdup(cstr);
  182. char *paren_ptr = strchr(date_str, '(');
  183. if (paren_ptr != NULL) {
  184. *paren_ptr = 0;
  185. }
  186. zend_class_entry *ce = php_date_get_date_ce();
  187. php_date_instantiate(ce, return_value TSRMLS_CC);
  188. if (!php_date_initialize(Z_PHPDATE_P(return_value), date_str, strlen(date_str), NULL, NULL, 0 TSRMLS_CC)) {
  189. efree(date_str);
  190. return FAILURE;
  191. }
  192. efree(date_str);
  193. }
  194. else if (jsValue->IsObject())
  195. {
  196. v8::Local<v8::Object> self = jsValue->ToObject();
  197. // if this is a wrapped PHP object, then just unwrap it.
  198. if (self->InternalFieldCount() == 2) {
  199. zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
  200. zval zval_object;
  201. ZVAL_OBJ(&zval_object, object);
  202. RETVAL_ZVAL(&zval_object, 1, 0);
  203. return SUCCESS;
  204. }
  205. if ((flags & V8JS_FLAG_FORCE_ARRAY && !jsValue->IsFunction()) || jsValue->IsArray()) {
  206. array_init(return_value);
  207. return v8js_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate TSRMLS_CC);
  208. } else {
  209. v8js_v8object_create(return_value, jsValue, flags, isolate TSRMLS_CC);
  210. return SUCCESS;
  211. }
  212. }
  213. else /* types External, RegExp, Undefined and Null are considered NULL */
  214. {
  215. RETVAL_NULL();
  216. }
  217. return SUCCESS;
  218. }
  219. /* }}} */
  220. /*
  221. * Local variables:
  222. * tab-width: 4
  223. * c-basic-offset: 4
  224. * indent-tabs-mode: t
  225. * End:
  226. * vim600: noet sw=4 ts=4 fdm=marker
  227. * vim<600: noet sw=4 ts=4
  228. */