v8js_convert.cc 19 KB

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