v8js_convert.cc 21 KB

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