v8js_convert.cc 7.0 KB

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