v8js_convert.cc 17 KB

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