v8js_convert.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2012 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | [email protected] so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Jani Taskinen <[email protected]> |
  16. | Author: Patrick Reilly <[email protected]> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. extern "C" {
  24. #include "php.h"
  25. #include "ext/date/php_date.h"
  26. #include "zend_interfaces.h"
  27. #include "zend_closures.h"
  28. }
  29. #include "php_v8js_macros.h"
  30. #include <v8.h>
  31. /* Callback for PHP methods and functions */
  32. static void php_v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
  33. {
  34. v8::Handle<v8::Value> return_value;
  35. zval *value = reinterpret_cast<zval *>(info.This()->GetAlignedPointerFromInternalField(0));
  36. v8::Isolate *isolate = reinterpret_cast<v8::Isolate *>(info.This()->GetAlignedPointerFromInternalField(1));
  37. zend_function *method_ptr;
  38. zend_fcall_info fci;
  39. zend_fcall_info_cache fcc;
  40. zval fname, *retval_ptr = NULL, **argv = NULL;
  41. TSRMLS_FETCH();
  42. zend_class_entry *ce = Z_OBJCE_P(value);
  43. zend_uint argc = info.Length(), min_num_args = 0, max_num_args = 0;
  44. char *error;
  45. int error_len, i, flags = V8JS_FLAG_NONE;
  46. /* Set method_ptr from v8::External or fetch the closure invoker */
  47. if (!info.Data().IsEmpty() && info.Data()->IsExternal()) {
  48. method_ptr = static_cast<zend_function *>(v8::External::Cast(*info.Data())->Value());
  49. } else {
  50. method_ptr = zend_get_closure_invoke_method(value TSRMLS_CC);
  51. }
  52. /* Set parameter limits */
  53. min_num_args = method_ptr->common.required_num_args;
  54. max_num_args = method_ptr->common.num_args;
  55. /* Function name to call */
  56. ZVAL_STRING(&fname, method_ptr->common.function_name, 0);
  57. /* zend_fcall_info */
  58. fci.size = sizeof(fci);
  59. fci.function_table = &ce->function_table;
  60. fci.function_name = &fname;
  61. fci.symbol_table = NULL;
  62. fci.object_ptr = value;
  63. fci.retval_ptr_ptr = &retval_ptr;
  64. fci.param_count = 0;
  65. /* Check for passed vs required number of arguments */
  66. if (argc < min_num_args)
  67. {
  68. error_len = spprintf(&error, 0,
  69. "%s::%s() expects %s %d parameter%s, %d given",
  70. ce->name,
  71. method_ptr->common.function_name,
  72. min_num_args == max_num_args ? "exactly" : argc < min_num_args ? "at least" : "at most",
  73. argc < min_num_args ? min_num_args : max_num_args,
  74. (argc < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  75. argc);
  76. return_value = V8JS_THROW(TypeError, error, error_len);
  77. if (ce == zend_ce_closure) {
  78. efree(const_cast<char*>(method_ptr->internal_function.function_name));
  79. efree(method_ptr);
  80. }
  81. efree(error);
  82. info.GetReturnValue().Set(return_value);
  83. return;
  84. }
  85. /* Convert parameters passed from V8 */
  86. if (argc) {
  87. flags = V8JS_GLOBAL_GET_FLAGS();
  88. fci.params = (zval ***) safe_emalloc(argc, sizeof(zval **), 0);
  89. argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0);
  90. for (i = 0; i < argc; i++) {
  91. MAKE_STD_ZVAL(argv[i]);
  92. if (v8js_to_zval(info[i], argv[i], flags, isolate TSRMLS_CC) == FAILURE) {
  93. fci.param_count++;
  94. error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, method_ptr->common.function_name);
  95. return_value = V8JS_THROW(Error, error, error_len);
  96. efree(error);
  97. goto failure;
  98. }
  99. fci.params[fci.param_count++] = &argv[i];
  100. }
  101. } else {
  102. fci.params = NULL;
  103. }
  104. fci.no_separation = 1;
  105. /* zend_fcall_info_cache */
  106. fcc.initialized = 1;
  107. fcc.function_handler = method_ptr;
  108. fcc.calling_scope = ce;
  109. fcc.called_scope = ce;
  110. fcc.object_ptr = value;
  111. /* Call the method */
  112. zend_call_function(&fci, &fcc TSRMLS_CC);
  113. failure:
  114. /* Cleanup */
  115. if (argc) {
  116. for (i = 0; i < fci.param_count; i++) {
  117. zval_ptr_dtor(&argv[i]);
  118. }
  119. efree(argv);
  120. efree(fci.params);
  121. }
  122. if (retval_ptr != NULL) {
  123. return_value = zval_to_v8js(retval_ptr, isolate TSRMLS_CC);
  124. zval_ptr_dtor(&retval_ptr);
  125. } else {
  126. return_value = V8JS_NULL;
  127. }
  128. info.GetReturnValue().Set(return_value);
  129. }
  130. /* }}} */
  131. static int _php_v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
  132. {
  133. int i;
  134. char *key;
  135. ulong index, idx = 0;
  136. uint key_len;
  137. HashPosition pos;
  138. zend_hash_internal_pointer_reset_ex(myht, &pos);
  139. for (;; zend_hash_move_forward_ex(myht, &pos)) {
  140. i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
  141. if (i == HASH_KEY_NON_EXISTANT)
  142. break;
  143. if (i == HASH_KEY_IS_STRING || index != idx) {
  144. return 1;
  145. }
  146. idx++;
  147. }
  148. return 0;
  149. }
  150. /* }}} */
  151. static void php_v8js_property_caller(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
  152. {
  153. v8::Local<v8::Object> self = info.Holder();
  154. v8::Local<v8::String> cname = info.Callee()->GetName()->ToString();
  155. v8::Local<v8::Value> value;
  156. v8::Local<v8::String> cb_func = v8::Local<v8::String>::Cast(info.Data());
  157. value = self->GetHiddenValue(cb_func);
  158. if (!value.IsEmpty() && value->IsFunction())
  159. {
  160. int argc = info.Length(), i = 0;
  161. v8::Local<v8::Value> *argv = new v8::Local<v8::Value>[argc];
  162. v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(value);
  163. if (cb_func->Equals(V8JS_SYM(ZEND_INVOKE_FUNC_NAME))) {
  164. for (; i < argc; ++i) {
  165. argv[i] = info[i];
  166. }
  167. value = cb->Call(self, argc, argv);
  168. }
  169. else /* __call() */
  170. {
  171. v8::Local<v8::Array> argsarr = v8::Array::New(argc);
  172. for (; i < argc; ++i) {
  173. argsarr->Set(i, info[i]);
  174. }
  175. v8::Local<v8::Value> argsv[2] = { cname, argsarr };
  176. value = cb->Call(self, 2, argsv);
  177. }
  178. }
  179. if (info.IsConstructCall()) {
  180. if (!value.IsEmpty() && !value->IsNull()) {
  181. info.GetReturnValue().Set(value);
  182. return;
  183. }
  184. info.GetReturnValue().Set(self);
  185. return;
  186. }
  187. info.GetReturnValue().Set(value);
  188. }
  189. /* }}} */
  190. static void php_v8js_property_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
  191. {
  192. v8::Local<v8::Object> self = info.Holder();
  193. v8::Local<v8::Value> value;
  194. v8::Local<v8::Function> cb;
  195. /* Check first if JS object has the named property */
  196. value = self->GetRealNamedProperty(property);
  197. if (!value.IsEmpty()) {
  198. info.GetReturnValue().Set(value);
  199. return;
  200. }
  201. /* If __get() is set for PHP object, call it */
  202. value = self->GetHiddenValue(V8JS_SYM(ZEND_GET_FUNC_NAME));
  203. if (!value.IsEmpty() && value->IsFunction()) {
  204. cb = v8::Local<v8::Function>::Cast(value);
  205. v8::Local<v8::Value> argv[1] = {property};
  206. value = cb->Call(self, 1, argv);
  207. }
  208. /* If __get() does not exist or returns NULL, create new function with callback for __call() */
  209. if ((value.IsEmpty() || value->IsNull()) && info.Data()->IsTrue()) {
  210. v8::Local<v8::FunctionTemplate> cb_t = v8::FunctionTemplate::New(php_v8js_property_caller, V8JS_SYM(ZEND_CALL_FUNC_NAME));
  211. cb = cb_t->GetFunction();
  212. cb->SetName(property);
  213. info.GetReturnValue().Set(cb);
  214. return;
  215. }
  216. info.GetReturnValue().Set(value);
  217. }
  218. /* }}} */
  219. static void php_v8js_property_query(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
  220. {
  221. v8::Local<v8::Object> self = info.Holder();
  222. v8::Local<v8::Value> value;
  223. /* Return early if property is set in JS object */
  224. if (self->HasRealNamedProperty(property)) {
  225. info.GetReturnValue().Set(V8JS_INT(v8::ReadOnly));
  226. return;
  227. }
  228. value = self->GetHiddenValue(V8JS_SYM(ZEND_ISSET_FUNC_NAME));
  229. if (!value.IsEmpty() && value->IsFunction()) {
  230. v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(value);
  231. v8::Local<v8::Value> argv[1] = {property};
  232. value = cb->Call(self, 1, argv);
  233. }
  234. info.GetReturnValue().Set((!value.IsEmpty() && value->IsTrue()) ? V8JS_INT(v8::ReadOnly) : v8::Local<v8::Integer>());
  235. }
  236. /* }}} */
  237. /* These are not defined by Zend */
  238. #define ZEND_WAKEUP_FUNC_NAME "__wakeup"
  239. #define ZEND_SLEEP_FUNC_NAME "__sleep"
  240. #define ZEND_SET_STATE_FUNC_NAME "__set_state"
  241. #define IS_MAGIC_FUNC(mname) \
  242. ((key_len == sizeof(mname)) && \
  243. !strncasecmp(key, mname, key_len - 1))
  244. #define PHP_V8JS_CALLBACK(mptr) \
  245. v8::FunctionTemplate::New(php_v8js_php_callback, v8::External::New(mptr))->GetFunction()
  246. static v8::Handle<v8::Value> php_v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  247. {
  248. v8::Local<v8::FunctionTemplate> new_tpl = v8::FunctionTemplate::New();
  249. v8::Local<v8::Object> newobj;
  250. int i;
  251. char *key = NULL;
  252. ulong index;
  253. uint key_len;
  254. HashTable *myht;
  255. HashPosition pos;
  256. zend_class_entry *ce = NULL;
  257. zend_function *method_ptr, *call_ptr = NULL, *get_ptr = NULL, *invoke_ptr = NULL, *isset_ptr = NULL;
  258. if (Z_TYPE_P(value) == IS_ARRAY) {
  259. myht = HASH_OF(value);
  260. } else {
  261. myht = Z_OBJPROP_P(value);
  262. ce = Z_OBJCE_P(value);
  263. }
  264. /* Prevent recursion */
  265. if (myht && myht->nApplyCount > 1) {
  266. return V8JS_NULL;
  267. }
  268. /* Object methods */
  269. if (ce) {
  270. new_tpl->SetClassName(V8JS_STRL(ce->name, ce->name_length));
  271. new_tpl->InstanceTemplate()->SetInternalFieldCount(2);
  272. if (ce == zend_ce_closure) {
  273. new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_php_callback);
  274. newobj = new_tpl->InstanceTemplate()->NewInstance();
  275. } else {
  276. zend_hash_internal_pointer_reset_ex(&ce->function_table, &pos);
  277. for (;; zend_hash_move_forward_ex(&ce->function_table, &pos)) {
  278. if (zend_hash_get_current_key_ex(&ce->function_table, &key, &key_len, &index, 0, &pos) != HASH_KEY_IS_STRING ||
  279. zend_hash_get_current_data_ex(&ce->function_table, (void **) &method_ptr, &pos) == FAILURE
  280. ) {
  281. break;
  282. }
  283. if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) && /* Allow only public methods */
  284. (method_ptr->common.fn_flags & ZEND_ACC_CTOR) == 0 && /* ..and no __construct() */
  285. (method_ptr->common.fn_flags & ZEND_ACC_DTOR) == 0 && /* ..or __destruct() */
  286. (method_ptr->common.fn_flags & ZEND_ACC_CLONE) == 0 /* ..or __clone() functions */
  287. ) {
  288. /* Override native toString() with __tostring() if it is set in passed object */
  289. if (IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME)) {
  290. new_tpl->InstanceTemplate()->Set(V8JS_SYM("toString"), PHP_V8JS_CALLBACK(method_ptr));
  291. /* TODO: __set(), __unset() disabled as JS is not allowed to modify the passed PHP object yet.
  292. * __sleep(), __wakeup(), __set_state() are always ignored */
  293. } else if (
  294. IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME)|| /* TODO */
  295. IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
  296. IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
  297. IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
  298. IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
  299. IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME)
  300. ) {
  301. /* Register all magic function as hidden with lowercase name */
  302. } else if (IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME)) {
  303. get_ptr = method_ptr;
  304. } else if (IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME)) {
  305. call_ptr = method_ptr;
  306. } else if (IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME)) {
  307. invoke_ptr = method_ptr;
  308. } else if (IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
  309. isset_ptr = method_ptr;
  310. } else {
  311. new_tpl->InstanceTemplate()->Set(V8JS_STR(method_ptr->common.function_name), PHP_V8JS_CALLBACK(method_ptr), v8::ReadOnly);
  312. }
  313. }
  314. }
  315. /* Only register getter, etc. when they're set in PHP side */
  316. if (call_ptr || get_ptr || isset_ptr)
  317. {
  318. /* Set __get() handler which acts also as __call() proxy */
  319. new_tpl->InstanceTemplate()->SetNamedPropertyHandler(
  320. php_v8js_property_getter, /* getter */
  321. 0, /* setter */
  322. isset_ptr ? php_v8js_property_query : 0, /* query */
  323. 0, /* deleter */
  324. 0, /* enumerator */
  325. V8JS_BOOL(call_ptr ? true : false)
  326. );
  327. }
  328. /* __invoke() handler */
  329. if (invoke_ptr) {
  330. new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_property_caller, V8JS_SYM(ZEND_INVOKE_FUNC_NAME));
  331. }
  332. newobj = new_tpl->InstanceTemplate()->NewInstance();
  333. if (call_ptr) {
  334. newobj->SetHiddenValue(V8JS_SYM(ZEND_CALL_FUNC_NAME), PHP_V8JS_CALLBACK(call_ptr));
  335. }
  336. if (get_ptr) {
  337. newobj->SetHiddenValue(V8JS_SYM(ZEND_GET_FUNC_NAME), PHP_V8JS_CALLBACK(get_ptr));
  338. }
  339. if (invoke_ptr) {
  340. newobj->SetHiddenValue(V8JS_SYM(ZEND_INVOKE_FUNC_NAME), PHP_V8JS_CALLBACK(invoke_ptr));
  341. }
  342. if (isset_ptr) {
  343. newobj->SetHiddenValue(V8JS_SYM(ZEND_ISSET_FUNC_NAME), PHP_V8JS_CALLBACK(isset_ptr));
  344. }
  345. }
  346. // Increase the reference count of this value because we're storing it internally for use later
  347. // See https://github.com/preillyme/v8js/issues/6
  348. Z_ADDREF_P(value);
  349. newobj->SetAlignedPointerInInternalField(0, (void *) value);
  350. newobj->SetAlignedPointerInInternalField(1, (void *) isolate);
  351. } else {
  352. new_tpl->SetClassName(V8JS_SYM("Array"));
  353. newobj = new_tpl->InstanceTemplate()->NewInstance();
  354. }
  355. /* Object properties */
  356. i = myht ? zend_hash_num_elements(myht) : 0;
  357. if (i > 0)
  358. {
  359. zval **data;
  360. HashTable *tmp_ht;
  361. zend_hash_internal_pointer_reset_ex(myht, &pos);
  362. for (;; zend_hash_move_forward_ex(myht, &pos)) {
  363. i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
  364. if (i == HASH_KEY_NON_EXISTANT)
  365. break;
  366. if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS)
  367. {
  368. tmp_ht = HASH_OF(*data);
  369. if (tmp_ht) {
  370. tmp_ht->nApplyCount++;
  371. }
  372. if (i == HASH_KEY_IS_STRING)
  373. {
  374. if (key[0] == '\0' && Z_TYPE_P(value) == IS_OBJECT) {
  375. /* Skip protected and private members. */
  376. if (tmp_ht) {
  377. tmp_ht->nApplyCount--;
  378. }
  379. continue;
  380. }
  381. newobj->Set(V8JS_STRL(key, key_len - 1), zval_to_v8js(*data, isolate TSRMLS_CC), v8::ReadOnly);
  382. } else {
  383. newobj->Set(index, zval_to_v8js(*data, isolate TSRMLS_CC));
  384. }
  385. if (tmp_ht) {
  386. tmp_ht->nApplyCount--;
  387. }
  388. }
  389. }
  390. }
  391. return newobj;
  392. }
  393. /* }}} */
  394. static v8::Handle<v8::Value> php_v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  395. {
  396. HashTable *myht = HASH_OF(value);
  397. int i = myht ? zend_hash_num_elements(myht) : 0;
  398. /* Return object if dealing with assoc array */
  399. if (i > 0 && _php_v8js_is_assoc_array(myht TSRMLS_CC)) {
  400. return php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  401. }
  402. v8::Local<v8::Array> newarr;
  403. /* Prevent recursion */
  404. if (myht && myht->nApplyCount > 1) {
  405. return V8JS_NULL;
  406. }
  407. newarr = v8::Array::New(i);
  408. if (i > 0)
  409. {
  410. zval **data;
  411. ulong index = 0;
  412. HashTable *tmp_ht;
  413. HashPosition pos;
  414. for (zend_hash_internal_pointer_reset_ex(myht, &pos);
  415. SUCCESS == zend_hash_get_current_data_ex(myht, (void **) &data, &pos);
  416. zend_hash_move_forward_ex(myht, &pos)
  417. ) {
  418. tmp_ht = HASH_OF(*data);
  419. if (tmp_ht) {
  420. tmp_ht->nApplyCount++;
  421. }
  422. newarr->Set(index++, zval_to_v8js(*data, isolate TSRMLS_CC));
  423. if (tmp_ht) {
  424. tmp_ht->nApplyCount--;
  425. }
  426. }
  427. }
  428. return newarr;
  429. }
  430. /* }}} */
  431. v8::Handle<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  432. {
  433. v8::Handle<v8::Value> jsValue;
  434. switch (Z_TYPE_P(value))
  435. {
  436. case IS_ARRAY:
  437. jsValue = php_v8js_hash_to_jsarr(value, isolate TSRMLS_CC);
  438. break;
  439. case IS_OBJECT:
  440. jsValue = php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  441. break;
  442. case IS_STRING:
  443. jsValue = V8JS_STRL(Z_STRVAL_P(value), Z_STRLEN_P(value));
  444. break;
  445. case IS_LONG:
  446. jsValue = V8JS_INT(Z_LVAL_P(value));
  447. break;
  448. case IS_DOUBLE:
  449. jsValue = V8JS_FLOAT(Z_DVAL_P(value));
  450. break;
  451. case IS_BOOL:
  452. jsValue = V8JS_BOOL(Z_BVAL_P(value));
  453. break;
  454. default:
  455. case IS_NULL:
  456. jsValue = V8JS_NULL;
  457. break;
  458. }
  459. return jsValue;
  460. }
  461. /* }}} */
  462. int v8js_to_zval(v8::Handle<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  463. {
  464. if (jsValue->IsString())
  465. {
  466. v8::String::Utf8Value str(jsValue);
  467. const char *cstr = ToCString(str);
  468. RETVAL_STRING(cstr, 1);
  469. }
  470. else if (jsValue->IsBoolean())
  471. {
  472. RETVAL_BOOL(jsValue->Uint32Value());
  473. }
  474. else if (jsValue->IsInt32() || jsValue->IsUint32())
  475. {
  476. RETVAL_LONG((long) jsValue->IntegerValue());
  477. }
  478. else if (jsValue->IsNumber())
  479. {
  480. RETVAL_DOUBLE(jsValue->NumberValue());
  481. }
  482. else if (jsValue->IsDate()) /* Return as a PHP DateTime object */
  483. {
  484. v8::String::Utf8Value str(jsValue);
  485. const char *cstr = ToCString(str);
  486. zend_class_entry *ce = php_date_get_date_ce();
  487. #if PHP_VERSION_ID < 50304
  488. zval *param;
  489. MAKE_STD_ZVAL(param);
  490. ZVAL_STRING(param, cstr, 1);
  491. object_init_ex(return_value, ce TSRMLS_CC);
  492. zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, param);
  493. zval_ptr_dtor(&param);
  494. if (EG(exception)) {
  495. return FAILURE;
  496. }
  497. #else
  498. php_date_instantiate(ce, return_value TSRMLS_CC);
  499. if (!php_date_initialize((php_date_obj *) zend_object_store_get_object(return_value TSRMLS_CC), (char *) cstr, strlen(cstr), NULL, NULL, 0 TSRMLS_CC)) {
  500. return FAILURE;
  501. }
  502. #endif
  503. }
  504. else if (jsValue->IsObject())
  505. {
  506. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  507. array_init(return_value);
  508. return php_v8js_v8_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate TSRMLS_CC);
  509. } else {
  510. php_v8js_create_v8(return_value, jsValue, flags, isolate TSRMLS_CC);
  511. return SUCCESS;
  512. }
  513. }
  514. else /* types External, RegExp, Undefined and Null are considered NULL */
  515. {
  516. RETVAL_NULL();
  517. }
  518. return SUCCESS;
  519. }
  520. /* }}} */
  521. /*
  522. * Local variables:
  523. * tab-width: 4
  524. * c-basic-offset: 4
  525. * End:
  526. * vim600: noet sw=4 ts=4 fdm=marker
  527. * vim<600: noet sw=4 ts=4
  528. */