v8js_convert.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2017 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. static int v8js_is_assoc_array(HashTable *myht) /* {{{ */
  33. {
  34. zend_string *key;
  35. zend_ulong index, idx = 0;
  36. ZEND_HASH_FOREACH_KEY(myht, index, key) {
  37. if(key) {
  38. // HASH_KEY_IS_STRING
  39. return 1;
  40. }
  41. if(index != idx) {
  42. return 1;
  43. }
  44. idx ++;
  45. } ZEND_HASH_FOREACH_END();
  46. return 0;
  47. }
  48. /* }}} */
  49. static v8::Local<v8::Value> v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate) /* {{{ */
  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)) {
  55. return v8js_hash_to_jsobj(value, isolate);
  56. }
  57. v8::Local<v8::Array> newarr;
  58. /* Prevent recursion */
  59. #if PHP_VERSION_ID >= 70300
  60. if (myht && GC_IS_RECURSIVE(myht))
  61. #else
  62. if (myht && ZEND_HASH_GET_APPLY_COUNT(myht) > 0)
  63. #endif
  64. {
  65. return V8JS_NULL;
  66. }
  67. v8::Local<v8::Context> v8_context = isolate->GetEnteredOrMicrotaskContext();
  68. newarr = v8::Array::New(isolate, i);
  69. if (i > 0)
  70. {
  71. zval *data;
  72. zend_ulong index = 0;
  73. #if PHP_VERSION_ID >= 70300
  74. if (myht && !(GC_FLAGS(myht) & GC_IMMUTABLE))
  75. #else
  76. if (myht)
  77. #endif
  78. {
  79. GC_PROTECT_RECURSION(myht);
  80. }
  81. ZEND_HASH_FOREACH_VAL(myht, data) {
  82. newarr->Set(v8_context, index++, zval_to_v8js(data, isolate));
  83. } ZEND_HASH_FOREACH_END();
  84. #if PHP_VERSION_ID >= 70300
  85. if (myht && !(GC_FLAGS(myht) & GC_IMMUTABLE))
  86. #else
  87. if (myht)
  88. #endif
  89. {
  90. GC_UNPROTECT_RECURSION(myht);
  91. }
  92. }
  93. return newarr;
  94. }
  95. /* }}} */
  96. v8::Local<v8::Value> zend_long_to_v8js(zend_long v, v8::Isolate *isolate) /* {{{ */
  97. {
  98. if (v < std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
  99. return V8JS_FLOAT(static_cast<double>(v));
  100. } else {
  101. return V8JS_INT(static_cast<int32_t>(v));
  102. }
  103. }
  104. /* }}} */
  105. v8::Local<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate) /* {{{ */
  106. {
  107. v8::Local<v8::Value> jsValue;
  108. zend_string *value_str;
  109. zend_class_entry *ce;
  110. switch (Z_TYPE_P(value))
  111. {
  112. case IS_INDIRECT:
  113. jsValue = zval_to_v8js(Z_INDIRECT_P(value), isolate);
  114. break;
  115. case IS_REFERENCE:
  116. jsValue = zval_to_v8js(Z_REFVAL_P(value), isolate);
  117. break;
  118. case IS_ARRAY:
  119. jsValue = v8js_hash_to_jsarr(value, isolate);
  120. break;
  121. case IS_OBJECT:
  122. if (V8JSG(use_date)) {
  123. ce = php_date_get_date_ce();
  124. if (instanceof_function(Z_OBJCE_P(value), ce)) {
  125. zval dtval;
  126. zend_call_method_with_0_params(value, NULL, NULL, "getTimestamp", &dtval);
  127. v8::Date::New(isolate->GetEnteredOrMicrotaskContext(), ((double)Z_LVAL(dtval) * 1000.0)).ToLocal(&jsValue);
  128. zval_dtor(&dtval);
  129. } else
  130. jsValue = v8js_hash_to_jsobj(value, isolate);
  131. } else
  132. jsValue = v8js_hash_to_jsobj(value, isolate);
  133. break;
  134. case IS_STRING:
  135. value_str = Z_STR_P(value);
  136. if (ZSTR_LEN(value_str) > std::numeric_limits<int>::max()) {
  137. zend_throw_exception(php_ce_v8js_exception,
  138. "String exceeds maximum string length", 0);
  139. break;
  140. }
  141. jsValue = V8JS_ZSTR(value_str);
  142. break;
  143. case IS_LONG:
  144. jsValue = zend_long_to_v8js(Z_LVAL_P(value), isolate);
  145. break;
  146. case IS_DOUBLE:
  147. jsValue = V8JS_FLOAT(Z_DVAL_P(value));
  148. break;
  149. case IS_TRUE:
  150. jsValue = V8JS_TRUE();
  151. break;
  152. case IS_FALSE:
  153. jsValue = V8JS_FALSE();
  154. break;
  155. case IS_NULL:
  156. jsValue = V8JS_NULL;
  157. break;
  158. case IS_UNDEF:
  159. default:
  160. /* undefined -> return v8::Value left empty */
  161. jsValue = v8::Undefined(isolate);
  162. break;
  163. }
  164. return jsValue;
  165. }
  166. /* }}} */
  167. int v8js_to_zval(v8::Local<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate) /* {{{ */
  168. {
  169. v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
  170. v8::Local<v8::Context> v8_context = v8::Local<v8::Context>::New(isolate, ctx->context);
  171. if (jsValue->IsString())
  172. {
  173. v8::String::Utf8Value str(isolate, jsValue);
  174. const char *cstr = ToCString(str);
  175. RETVAL_STRINGL(cstr, str.length());
  176. }
  177. else if (jsValue->IsBoolean())
  178. {
  179. bool value = jsValue->BooleanValue(isolate);
  180. RETVAL_BOOL(value);
  181. }
  182. else if (jsValue->IsInt32() || jsValue->IsUint32())
  183. {
  184. v8::Maybe<int64_t> value = jsValue->IntegerValue(v8_context);
  185. if (value.IsNothing())
  186. {
  187. return FAILURE;
  188. }
  189. RETVAL_LONG((long) value.ToChecked());
  190. }
  191. else if (jsValue->IsNumber())
  192. {
  193. v8::Maybe<double> value = jsValue->NumberValue(v8_context);
  194. if (value.IsNothing())
  195. {
  196. return FAILURE;
  197. }
  198. RETVAL_DOUBLE(value.ToChecked());
  199. }
  200. else if (jsValue->IsDate()) /* Return as a PHP DateTime object */
  201. {
  202. v8::String::Utf8Value str(isolate, jsValue);
  203. const char *cstr = ToCString(str);
  204. /* cstr has two timezone specifications:
  205. *
  206. * example from Linux:
  207. * Mon Sep 08 1975 09:00:00 GMT+0000 (UTC)
  208. *
  209. * example from Windows:
  210. * Mon Sep 08 1975 11:00:00 GMT+0200 (W. Europe Daylight Time)
  211. *
  212. * ... problem is, that PHP can't parse the second timezone
  213. * specification as returned by v8 running on Windows. And as a
  214. * matter of that fails due to inconsistent second timezone spec
  215. */
  216. char *date_str = estrdup(cstr);
  217. char *paren_ptr = strchr(date_str, '(');
  218. if (paren_ptr != NULL) {
  219. *paren_ptr = 0;
  220. }
  221. zend_class_entry *ce = php_date_get_date_ce();
  222. php_date_instantiate(ce, return_value);
  223. if (!php_date_initialize(Z_PHPDATE_P(return_value), date_str, strlen(date_str), NULL, NULL, 0)) {
  224. efree(date_str);
  225. return FAILURE;
  226. }
  227. efree(date_str);
  228. }
  229. else if (jsValue->IsObject())
  230. {
  231. v8::Local<v8::Object> self;
  232. if (!jsValue->ToObject(v8_context).ToLocal(&self))
  233. {
  234. return FAILURE;
  235. }
  236. // if this is a wrapped PHP object, then just unwrap it.
  237. if (self->InternalFieldCount() == 2) {
  238. zend_object *object = reinterpret_cast<zend_object *>(self->GetAlignedPointerFromInternalField(1));
  239. zval zval_object;
  240. ZVAL_OBJ(&zval_object, object);
  241. RETVAL_ZVAL(&zval_object, 1, 0);
  242. return SUCCESS;
  243. }
  244. if ((flags & V8JS_FLAG_FORCE_ARRAY && !jsValue->IsFunction()) || jsValue->IsArray()) {
  245. array_init(return_value);
  246. return v8js_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate);
  247. } else {
  248. v8js_v8object_create(return_value, jsValue, flags, isolate);
  249. return SUCCESS;
  250. }
  251. }
  252. else /* types External, RegExp, Undefined and Null are considered NULL */
  253. {
  254. RETVAL_NULL();
  255. }
  256. return SUCCESS;
  257. }
  258. /* }}} */
  259. /*
  260. * Local variables:
  261. * tab-width: 4
  262. * c-basic-offset: 4
  263. * indent-tabs-mode: t
  264. * End:
  265. * vim600: noet sw=4 ts=4 fdm=marker
  266. * vim<600: noet sw=4 ts=4
  267. */