v8js_convert.cc 21 KB

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