v8js_convert.cc 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 "ext/standard/php_string.h"
  21. #include "zend_interfaces.h"
  22. #include "zend_closures.h"
  23. }
  24. #include "php_v8js_macros.h"
  25. #include <v8.h>
  26. #include <stdexcept>
  27. #include <limits>
  28. static void php_v8js_error_handler(int error_num, const char *error_filename, const uint error_lineno, const char *format, va_list args)
  29. {
  30. char *buffer;
  31. int buffer_len;
  32. buffer_len = vspprintf(&buffer, PG(log_errors_max_len), format, args);
  33. V8JSG(fatal_error_abort) = true;
  34. V8JSG(error_num) = error_num;
  35. V8JSG(error_message) = buffer;
  36. longjmp(*V8JSG(unwind_env), 1);
  37. }
  38. static void php_v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zval> &data);
  39. /* Callback for PHP methods and functions */
  40. 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 TSRMLS_DC) /* {{{ */
  41. {
  42. v8::Handle<v8::Value> return_value;
  43. zend_fcall_info fci;
  44. zend_fcall_info_cache fcc;
  45. zval fname, *retval_ptr = NULL, **argv = NULL;
  46. zend_uint argc = info.Length(), min_num_args = 0, max_num_args = 0;
  47. char *error;
  48. int error_len, i, flags = V8JS_FLAG_NONE;
  49. #if PHP_V8_API_VERSION <= 3023008
  50. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  51. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData();
  52. #else
  53. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
  54. #endif
  55. /* Set parameter limits */
  56. min_num_args = method_ptr->common.required_num_args;
  57. max_num_args = method_ptr->common.num_args;
  58. /* Function name to call */
  59. INIT_ZVAL(fname);
  60. ZVAL_STRING(&fname, method_ptr->common.function_name, 0);
  61. /* zend_fcall_info */
  62. fci.size = sizeof(fci);
  63. fci.function_table = &ce->function_table;
  64. fci.function_name = &fname;
  65. fci.symbol_table = NULL;
  66. fci.object_ptr = value;
  67. fci.retval_ptr_ptr = &retval_ptr;
  68. fci.param_count = 0;
  69. /* Check for passed vs required number of arguments */
  70. if (argc < min_num_args)
  71. {
  72. error_len = spprintf(&error, 0,
  73. "%s::%s() expects %s %d parameter%s, %d given",
  74. ce->name,
  75. method_ptr->common.function_name,
  76. min_num_args == max_num_args ? "exactly" : argc < min_num_args ? "at least" : "at most",
  77. argc < min_num_args ? min_num_args : max_num_args,
  78. (argc < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  79. argc);
  80. return_value = V8JS_THROW(isolate, TypeError, error, error_len);
  81. if (ce == zend_ce_closure) {
  82. efree(const_cast<char*>(method_ptr->internal_function.function_name));
  83. efree(method_ptr);
  84. }
  85. efree(error);
  86. info.GetReturnValue().Set(return_value);
  87. return;
  88. }
  89. /* Convert parameters passed from V8 */
  90. if (argc) {
  91. flags = V8JS_GLOBAL_GET_FLAGS(isolate);
  92. fci.params = (zval ***) safe_emalloc(argc, sizeof(zval **), 0);
  93. argv = (zval **) safe_emalloc(argc, sizeof(zval *), 0);
  94. for (i = 0; i < argc; i++) {
  95. v8::Local<v8::Value> php_object;
  96. if (info[i]->IsObject()) {
  97. php_object = v8::Local<v8::Object>::Cast(info[i])->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  98. }
  99. if (!php_object.IsEmpty()) {
  100. /* This is a PHP object, passed to JS and back. */
  101. argv[i] = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  102. Z_ADDREF_P(argv[i]);
  103. } else {
  104. MAKE_STD_ZVAL(argv[i]);
  105. if (v8js_to_zval(info[i], argv[i], flags, isolate TSRMLS_CC) == FAILURE) {
  106. fci.param_count++;
  107. error_len = spprintf(&error, 0, "converting parameter #%d passed to %s() failed", i + 1, method_ptr->common.function_name);
  108. return_value = V8JS_THROW(isolate, Error, error, error_len);
  109. efree(error);
  110. goto failure;
  111. }
  112. }
  113. fci.params[fci.param_count++] = &argv[i];
  114. }
  115. } else {
  116. fci.params = NULL;
  117. }
  118. fci.no_separation = 1;
  119. {
  120. isolate->Exit();
  121. v8::Unlocker unlocker(isolate);
  122. /* zend_fcall_info_cache */
  123. fcc.initialized = 1;
  124. fcc.function_handler = method_ptr;
  125. fcc.calling_scope = ce;
  126. fcc.called_scope = ce;
  127. fcc.object_ptr = value;
  128. jmp_buf env;
  129. int val;
  130. void (*old_error_handler)(int, const char *, const uint, const char*, va_list);
  131. old_error_handler = zend_error_cb;
  132. zend_error_cb = php_v8js_error_handler;
  133. val = setjmp (env);
  134. V8JSG(unwind_env) = &env;
  135. if (val) {
  136. zend_error_cb = old_error_handler;
  137. }
  138. else {
  139. /* Call the method */
  140. zend_call_function(&fci, &fcc TSRMLS_CC);
  141. }
  142. }
  143. isolate->Enter();
  144. if (V8JSG(fatal_error_abort)) {
  145. v8::V8::TerminateExecution(isolate);
  146. info.GetReturnValue().Set(V8JS_NULL);
  147. return;
  148. }
  149. failure:
  150. /* Cleanup */
  151. if (argc) {
  152. for (i = 0; i < fci.param_count; i++) {
  153. zval_ptr_dtor(&argv[i]);
  154. }
  155. efree(argv);
  156. efree(fci.params);
  157. }
  158. if (retval_ptr != NULL) {
  159. return_value = zval_to_v8js(retval_ptr, isolate TSRMLS_CC);
  160. zval_ptr_dtor(&retval_ptr);
  161. } else {
  162. return_value = V8JS_NULL;
  163. }
  164. info.GetReturnValue().Set(return_value);
  165. }
  166. /* }}} */
  167. /* Callback for PHP methods and functions */
  168. static void php_v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
  169. {
  170. v8::Isolate *isolate = info.GetIsolate();
  171. v8::Local<v8::Object> self = info.Holder();
  172. V8JS_TSRMLS_FETCH();
  173. zval *value = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
  174. zend_function *method_ptr;
  175. zend_class_entry *ce = Z_OBJCE_P(value);
  176. /* Set method_ptr from v8::External or fetch the closure invoker */
  177. if (!info.Data().IsEmpty() && info.Data()->IsExternal()) {
  178. method_ptr = static_cast<zend_function *>(v8::External::Cast(*info.Data())->Value());
  179. } else {
  180. method_ptr = zend_get_closure_invoke_method(value TSRMLS_CC);
  181. }
  182. return php_v8js_call_php_func(value, ce, method_ptr, isolate, info TSRMLS_CC);
  183. }
  184. /* Callback for PHP constructor calls */
  185. static void php_v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
  186. {
  187. v8::Isolate *isolate = info.GetIsolate();
  188. info.GetReturnValue().Set(V8JS_UNDEFINED);
  189. // @todo assert constructor call
  190. v8::Handle<v8::Object> newobj = info.This();
  191. v8::Local<v8::External> php_object;
  192. zval *value;
  193. if (!info.IsConstructCall()) {
  194. return;
  195. }
  196. v8::Local<v8::Array> cons_data = v8::Local<v8::Array>::Cast(info.Data());
  197. v8::Local<v8::External> ext_tmpl = v8::Local<v8::External>::Cast(cons_data->Get(0));
  198. v8::Local<v8::External> ext_ce = v8::Local<v8::External>::Cast(cons_data->Get(1));
  199. if (info[0]->IsExternal()) {
  200. // Object created by v8js in php_v8js_hash_to_jsobj, PHP object passed as v8::External.
  201. php_object = v8::Local<v8::External>::Cast(info[0]);
  202. value = reinterpret_cast<zval *>(php_object->Value());
  203. // Increase the reference count of this value because we're storing it internally for use later
  204. // See https://github.com/preillyme/v8js/issues/6
  205. Z_ADDREF_P(value);
  206. } else {
  207. // Object created from JavaScript context. Need to create PHP object first.
  208. V8JS_TSRMLS_FETCH();
  209. zend_class_entry *ce = static_cast<zend_class_entry *>(ext_ce->Value());
  210. zend_function *ctor_ptr = ce->constructor;
  211. // Check access on __construct function, if any
  212. if (ctor_ptr != NULL && (ctor_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
  213. info.GetReturnValue().Set(isolate->ThrowException(V8JS_SYM("Call to protected __construct() not allowed")));
  214. return;
  215. }
  216. MAKE_STD_ZVAL(value);
  217. object_init_ex(value, ce);
  218. // Call __construct function
  219. if (ctor_ptr != NULL) {
  220. php_v8js_call_php_func(value, ce, ctor_ptr, isolate, info TSRMLS_CC);
  221. }
  222. php_object = V8JS_NEW(v8::External, isolate, value);
  223. }
  224. newobj->SetAlignedPointerInInternalField(0, ext_tmpl->Value());
  225. newobj->SetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY), php_object);
  226. #if PHP_V8_API_VERSION <= 3023008
  227. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  228. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData();
  229. #else
  230. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
  231. #endif
  232. // Since we got to decrease the reference count again, in case v8 garbage collector
  233. // decides to dispose the JS object, we add a weak persistent handle and register
  234. // a callback function that removes the reference.
  235. ctx->weak_objects[value].Reset(isolate, newobj);
  236. ctx->weak_objects[value].SetWeak(value, php_v8js_weak_object_callback);
  237. // Just tell v8 that we're allocating some external memory
  238. // (for the moment we just always tell 1k instead of trying to find out actual values)
  239. isolate->AdjustAmountOfExternalAllocatedMemory(1024);
  240. }
  241. /* }}} */
  242. static int _php_v8js_is_assoc_array(HashTable *myht TSRMLS_DC) /* {{{ */
  243. {
  244. int i;
  245. char *key;
  246. ulong index, idx = 0;
  247. uint key_len;
  248. HashPosition pos;
  249. zend_hash_internal_pointer_reset_ex(myht, &pos);
  250. for (;; zend_hash_move_forward_ex(myht, &pos)) {
  251. i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
  252. if (i == HASH_KEY_NON_EXISTANT)
  253. break;
  254. if (i == HASH_KEY_IS_STRING || index != idx) {
  255. return 1;
  256. }
  257. idx++;
  258. }
  259. return 0;
  260. }
  261. /* }}} */
  262. static void php_v8js_weak_object_callback(const v8::WeakCallbackData<v8::Object, zval> &data) {
  263. v8::Isolate *isolate = data.GetIsolate();
  264. zval *value = data.GetParameter();
  265. zval_ptr_dtor(&value);
  266. #if PHP_V8_API_VERSION <= 3023008
  267. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  268. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData();
  269. #else
  270. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
  271. #endif
  272. ctx->weak_objects.at(value).Reset();
  273. ctx->weak_objects.erase(value);
  274. isolate->AdjustAmountOfExternalAllocatedMemory(-1024);
  275. }
  276. static void php_v8js_weak_closure_callback(const v8::WeakCallbackData<v8::Object, v8js_tmpl_t> &data) {
  277. v8::Isolate *isolate = data.GetIsolate();
  278. v8js_tmpl_t *persist_tpl_ = data.GetParameter();
  279. persist_tpl_->Reset();
  280. delete persist_tpl_;
  281. #if PHP_V8_API_VERSION <= 3023008
  282. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  283. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData();
  284. #else
  285. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
  286. #endif
  287. ctx->weak_closures.at(persist_tpl_).Reset();
  288. ctx->weak_closures.erase(persist_tpl_);
  289. };
  290. /* These are not defined by Zend */
  291. #define ZEND_WAKEUP_FUNC_NAME "__wakeup"
  292. #define ZEND_SLEEP_FUNC_NAME "__sleep"
  293. #define ZEND_SET_STATE_FUNC_NAME "__set_state"
  294. #define IS_MAGIC_FUNC(mname) \
  295. ((key_len == sizeof(mname)) && \
  296. !strncasecmp(key, mname, key_len - 1))
  297. #define PHP_V8JS_CALLBACK(isolate, mptr, tmpl) \
  298. V8JS_NEW(v8::FunctionTemplate, (isolate), php_v8js_php_callback, V8JS_NEW(v8::External, (isolate), mptr), V8JS_NEW(v8::Signature, (isolate), tmpl))->GetFunction()
  299. static void php_v8js_named_property_enumerator(const v8::PropertyCallbackInfo<v8::Array> &info) /* {{{ */
  300. {
  301. // note: 'special' properties like 'constructor' are not enumerated.
  302. v8::Isolate *isolate = info.GetIsolate();
  303. v8::Local<v8::Object> self = info.Holder();
  304. v8::Local<v8::Array> result = V8JS_NEW(v8::Array, isolate, 0);
  305. uint32_t result_len = 0;
  306. V8JS_TSRMLS_FETCH();
  307. zend_class_entry *ce;
  308. zend_function *method_ptr;
  309. HashTable *proptable;
  310. HashPosition pos;
  311. char *key = NULL;
  312. uint key_len;
  313. ulong index;
  314. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
  315. ce = Z_OBJCE_P(object);
  316. /* enumerate all methods */
  317. zend_hash_internal_pointer_reset_ex(&ce->function_table, &pos);
  318. for (;; zend_hash_move_forward_ex(&ce->function_table, &pos)) {
  319. if (zend_hash_get_current_key_ex(&ce->function_table, &key, &key_len, &index, 0, &pos) != HASH_KEY_IS_STRING ||
  320. zend_hash_get_current_data_ex(&ce->function_table, (void **) &method_ptr, &pos) == FAILURE
  321. ) {
  322. break;
  323. }
  324. if ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
  325. /* Allow only public methods */
  326. continue;
  327. }
  328. if ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
  329. /* no __construct, __destruct(), or __clone() functions */
  330. continue;
  331. }
  332. // hide (do not enumerate) other PHP magic functions
  333. if (IS_MAGIC_FUNC(ZEND_CALLSTATIC_FUNC_NAME) ||
  334. IS_MAGIC_FUNC(ZEND_SLEEP_FUNC_NAME) ||
  335. IS_MAGIC_FUNC(ZEND_WAKEUP_FUNC_NAME) ||
  336. IS_MAGIC_FUNC(ZEND_SET_STATE_FUNC_NAME) ||
  337. IS_MAGIC_FUNC(ZEND_GET_FUNC_NAME) ||
  338. IS_MAGIC_FUNC(ZEND_SET_FUNC_NAME) ||
  339. IS_MAGIC_FUNC(ZEND_UNSET_FUNC_NAME) ||
  340. IS_MAGIC_FUNC(ZEND_CALL_FUNC_NAME) ||
  341. IS_MAGIC_FUNC(ZEND_INVOKE_FUNC_NAME) ||
  342. IS_MAGIC_FUNC(ZEND_ISSET_FUNC_NAME)) {
  343. continue;
  344. }
  345. v8::Local<v8::String> method_name = V8JS_STR(method_ptr->common.function_name);
  346. // rename PHP special method names to JS equivalents.
  347. if (IS_MAGIC_FUNC(ZEND_TOSTRING_FUNC_NAME)) {
  348. method_name = V8JS_SYM("toString");
  349. }
  350. result->Set(result_len++, method_name);
  351. }
  352. /* enumerate all properties */
  353. /* Z_OBJPROP uses the get_properties handler */
  354. proptable = Z_OBJPROP_P(object);
  355. zend_hash_internal_pointer_reset_ex(proptable, &pos);
  356. for (;; zend_hash_move_forward_ex(proptable, &pos)) {
  357. int i = zend_hash_get_current_key_ex(proptable, &key, &key_len, &index, 0, &pos);
  358. if (i == HASH_KEY_NON_EXISTANT)
  359. break;
  360. // for consistency with the 'in' operator, skip properties whose
  361. // value IS_NULL (like isset does)
  362. zval **data;
  363. if (zend_hash_get_current_data_ex(proptable, (void **) &data, &pos) == SUCCESS &&
  364. ZVAL_IS_NULL(*data))
  365. continue;
  366. if (i == HASH_KEY_IS_STRING) {
  367. /* skip protected and private members */
  368. if (key[0] == '\0') {
  369. continue;
  370. }
  371. // prefix enumerated property names with '$' so they can be
  372. // dereferenced unambiguously (ie, don't conflict with method
  373. // names)
  374. char prefixed[key_len + 1];
  375. prefixed[0] = '$';
  376. strncpy(prefixed + 1, key, key_len);
  377. result->Set(result_len++, V8JS_STRL(prefixed, key_len));
  378. } else {
  379. // even numeric indices are enumerated as strings in JavaScript
  380. result->Set(result_len++, V8JS_FLOAT((double) index)->ToString());
  381. }
  382. }
  383. /* done */
  384. info.GetReturnValue().Set(result);
  385. }
  386. /* }}} */
  387. static void php_v8js_invoke_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
  388. {
  389. v8::Local<v8::Object> self = info.Holder();
  390. v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(info.Data());
  391. int argc = info.Length(), i;
  392. v8::Local<v8::Value> argv[argc];
  393. v8::Local<v8::Value> result;
  394. for (i=0; i<argc; i++) {
  395. argv[i] = info[i];
  396. }
  397. if (info.IsConstructCall() && self->GetConstructor()->IsFunction()) {
  398. // this is a 'new obj(...)' invocation. Handle this like PHP does;
  399. // that is, treat it as synonymous with 'new obj.constructor(...)'
  400. cb = v8::Local<v8::Function>::Cast(self->GetConstructor());
  401. result = cb->NewInstance(argc, argv);
  402. } else {
  403. result = cb->Call(self, argc, argv);
  404. }
  405. info.GetReturnValue().Set(result);
  406. }
  407. /* }}} */
  408. // this is a magic '__call' implementation for PHP classes which don't actually
  409. // have a '__call' magic function. This way we can always force a method
  410. // call (as opposed to a property get) from JavaScript using __call.
  411. static void php_v8js_fake_call_impl(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
  412. {
  413. v8::Isolate *isolate = info.GetIsolate();
  414. v8::Local<v8::Object> self = info.Holder();
  415. v8::Handle<v8::Value> return_value;
  416. char *error;
  417. int error_len;
  418. V8JS_TSRMLS_FETCH();
  419. zend_class_entry *ce;
  420. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
  421. ce = Z_OBJCE_P(object);
  422. // first arg is method name, second arg is array of args.
  423. if (info.Length() < 2) {
  424. error_len = spprintf(&error, 0,
  425. "%s::__call expects 2 parameters, %d given",
  426. ce->name, (int) info.Length());
  427. return_value = V8JS_THROW(isolate, TypeError, error, error_len);
  428. efree(error);
  429. info.GetReturnValue().Set(return_value);
  430. return;
  431. }
  432. if (!info[1]->IsArray()) {
  433. error_len = spprintf(&error, 0,
  434. "%s::__call expects 2nd parameter to be an array",
  435. ce->name);
  436. return_value = V8JS_THROW(isolate, TypeError, error, error_len);
  437. efree(error);
  438. info.GetReturnValue().Set(return_value);
  439. return;
  440. }
  441. v8::String::Utf8Value str(info[0]->ToString());
  442. const char *method_name = ToCString(str);
  443. uint method_name_len = strlen(method_name);
  444. v8::Local<v8::Array> args = v8::Local<v8::Array>::Cast(info[1]);
  445. if (args->Length() > 1000000) {
  446. // prevent overflow, since args->Length() is a uint32_t and args
  447. // in the Function->Call method below is a (signed) int.
  448. error_len = spprintf(&error, 0,
  449. "%s::__call expects fewer than a million arguments",
  450. ce->name);
  451. return_value = V8JS_THROW(isolate, TypeError, error, error_len);
  452. efree(error);
  453. info.GetReturnValue().Set(return_value);
  454. return;
  455. }
  456. // okay, look up the method name and manually invoke it.
  457. const zend_object_handlers *h = Z_OBJ_HT_P(object);
  458. zend_function *method_ptr =
  459. h->get_method(&object, (char*)method_name, method_name_len
  460. ZEND_HASH_KEY_NULL TSRMLS_CC);
  461. if (method_ptr == NULL ||
  462. (method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) == 0 ||
  463. (method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) != 0) {
  464. error_len = spprintf(&error, 0,
  465. "%s::__call to %s method %s", ce->name,
  466. (method_ptr == NULL) ? "undefined" : "non-public", method_name);
  467. return_value = V8JS_THROW(isolate, TypeError, error, error_len);
  468. efree(error);
  469. info.GetReturnValue().Set(return_value);
  470. return;
  471. }
  472. v8::Local<v8::FunctionTemplate> tmpl =
  473. v8::Local<v8::FunctionTemplate>::New
  474. (isolate, *reinterpret_cast<v8js_tmpl_t *>(self->GetAlignedPointerFromInternalField(0)));
  475. // use php_v8js_php_callback to actually execute the method
  476. v8::Local<v8::Function> cb = PHP_V8JS_CALLBACK(isolate, method_ptr, tmpl);
  477. uint32_t i, argc = args->Length();
  478. v8::Local<v8::Value> argv[argc];
  479. for (i=0; i<argc; i++) {
  480. argv[i] = args->Get(i);
  481. }
  482. return_value = cb->Call(info.This(), (int) argc, argv);
  483. info.GetReturnValue().Set(return_value);
  484. }
  485. /* }}} */
  486. typedef enum {
  487. V8JS_PROP_GETTER,
  488. V8JS_PROP_SETTER,
  489. V8JS_PROP_QUERY,
  490. V8JS_PROP_DELETER
  491. } property_op_t;
  492. /* This method handles named property and method get/set/query/delete. */
  493. template<typename T>
  494. static inline v8::Local<v8::Value> php_v8js_named_property_callback(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<T> &info, property_op_t callback_type, v8::Local<v8::Value> set_value = v8::Local<v8::Value>()) /* {{{ */
  495. {
  496. v8::Isolate *isolate = info.GetIsolate();
  497. v8::String::Utf8Value cstr(property);
  498. const char *name = ToCString(cstr);
  499. uint name_len = strlen(name);
  500. char *lower = estrndup(name, name_len);
  501. const char *method_name;
  502. uint method_name_len;
  503. v8::Local<v8::Object> self = info.Holder();
  504. v8::Local<v8::Value> ret_value;
  505. v8::Local<v8::Function> cb;
  506. V8JS_TSRMLS_FETCH();
  507. zend_class_entry *scope, *ce;
  508. zend_function *method_ptr = NULL;
  509. zval *php_value;
  510. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY)))->Value());
  511. v8::Local<v8::FunctionTemplate> tmpl =
  512. v8::Local<v8::FunctionTemplate>::New
  513. (isolate, *reinterpret_cast<v8js_tmpl_t *>(self->GetAlignedPointerFromInternalField(0)));
  514. ce = scope = Z_OBJCE_P(object);
  515. /* First, check the (case-insensitive) method table */
  516. php_strtolower(lower, name_len);
  517. method_name = lower;
  518. method_name_len = name_len;
  519. // toString() -> __tostring()
  520. if (name_len == 8 && strcmp(name, "toString") == 0) {
  521. method_name = ZEND_TOSTRING_FUNC_NAME;
  522. method_name_len = sizeof(ZEND_TOSTRING_FUNC_NAME) - 1;
  523. }
  524. bool is_constructor = (name_len == 11 && strcmp(name, "constructor") == 0);
  525. bool is_magic_call = (method_name_len == 6 && strcmp(method_name, "__call") == 0);
  526. if (is_constructor ||
  527. (name[0] != '$' /* leading '$' means property, not method */ &&
  528. zend_hash_find(&ce->function_table, method_name, method_name_len + 1, (void**)&method_ptr) == SUCCESS &&
  529. ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0) && /* Allow only public methods */
  530. ((method_ptr->common.fn_flags & (ZEND_ACC_CTOR|ZEND_ACC_DTOR|ZEND_ACC_CLONE)) == 0) /* no __construct, __destruct(), or __clone() functions */
  531. ) || (method_ptr=NULL, is_magic_call)
  532. ) {
  533. if (callback_type == V8JS_PROP_GETTER) {
  534. if (is_constructor) {
  535. ret_value = self->GetConstructor();
  536. } else {
  537. if (is_magic_call && method_ptr==NULL) {
  538. // Fake __call implementation
  539. // (only use this if method_ptr==NULL, which means
  540. // there is no actual PHP __call() implementation)
  541. v8::Local<v8::Function> cb =
  542. V8JS_NEW(v8::FunctionTemplate, isolate,
  543. php_v8js_fake_call_impl, V8JS_NULL,
  544. V8JS_NEW(v8::Signature, isolate, tmpl))->GetFunction();
  545. cb->SetName(property);
  546. ret_value = cb;
  547. } else {
  548. ret_value = PHP_V8JS_CALLBACK(isolate, method_ptr, tmpl);
  549. }
  550. }
  551. } else if (callback_type == V8JS_PROP_QUERY) {
  552. // methods are not enumerable
  553. ret_value = V8JS_UINT(v8::ReadOnly|v8::DontEnum|v8::DontDelete);
  554. } else if (callback_type == V8JS_PROP_SETTER) {
  555. ret_value = set_value; // lie. this field is read-only.
  556. } else if (callback_type == V8JS_PROP_DELETER) {
  557. ret_value = V8JS_BOOL(false);
  558. } else {
  559. /* shouldn't reach here! but bail safely */
  560. ret_value = v8::Handle<v8::Value>();
  561. }
  562. } else {
  563. if (name[0]=='$') {
  564. // this is a property (not a method)
  565. name++; name_len--;
  566. }
  567. if (callback_type == V8JS_PROP_GETTER) {
  568. /* Nope, not a method -- must be a (case-sensitive) property */
  569. zval zname;
  570. INIT_ZVAL(zname);
  571. ZVAL_STRINGL(&zname, name, name_len, 0);
  572. zend_property_info *property_info = zend_get_property_info(ce, &zname, 1 TSRMLS_CC);
  573. if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
  574. php_value = zend_read_property(NULL, object, V8JS_CONST name, name_len, true TSRMLS_CC);
  575. // special case uninitialized_zval_ptr and return an empty value
  576. // (indicating that we don't intercept this property) if the
  577. // property doesn't exist.
  578. if (php_value == EG(uninitialized_zval_ptr)) {
  579. ret_value = v8::Handle<v8::Value>();
  580. } else {
  581. // wrap it
  582. ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  583. /* We don't own the reference to php_value... unless the
  584. * returned refcount was 0, in which case the below code
  585. * will free it. */
  586. zval_add_ref(&php_value);
  587. zval_ptr_dtor(&php_value);
  588. }
  589. }
  590. else if (zend_hash_find(&ce->function_table, "__get", 6, (void**)&method_ptr) == SUCCESS
  591. /* Allow only public methods */
  592. && ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
  593. /* Okay, let's call __get. */
  594. zend_fcall_info fci;
  595. zval fmember;
  596. INIT_ZVAL(fmember);
  597. ZVAL_STRING(&fmember, "__get", 0);
  598. fci.size = sizeof(fci);
  599. fci.function_table = &ce->function_table;
  600. fci.function_name = &fmember;
  601. fci.symbol_table = NULL;
  602. fci.retval_ptr_ptr = &php_value;
  603. zval *zname_ptr = &zname;
  604. zval **zname_ptr_ptr = &zname_ptr;
  605. fci.param_count = 1;
  606. fci.params = &zname_ptr_ptr;
  607. fci.object_ptr = object;
  608. fci.no_separation = 0;
  609. zend_call_function(&fci, NULL TSRMLS_CC);
  610. ret_value = zval_to_v8js(php_value, isolate TSRMLS_CC);
  611. /* We don't own the reference to php_value... unless the
  612. * returned refcount was 0, in which case the below code
  613. * will free it. */
  614. zval_add_ref(&php_value);
  615. zval_ptr_dtor(&php_value);
  616. }
  617. } else if (callback_type == V8JS_PROP_SETTER) {
  618. MAKE_STD_ZVAL(php_value);
  619. if (v8js_to_zval(set_value, php_value, 0, isolate TSRMLS_CC) != SUCCESS) {
  620. ret_value = v8::Handle<v8::Value>();
  621. }
  622. else {
  623. zval zname;
  624. INIT_ZVAL(zname);
  625. ZVAL_STRINGL(&zname, name, name_len, 0);
  626. zend_property_info *property_info = zend_get_property_info(ce, &zname, 1 TSRMLS_CC);
  627. if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
  628. zend_update_property(scope, object, V8JS_CONST name, name_len, php_value TSRMLS_CC);
  629. ret_value = set_value;
  630. }
  631. else if (zend_hash_find(&ce->function_table, "__set", 6, (void**)&method_ptr) == SUCCESS
  632. /* Allow only public methods */
  633. && ((method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) != 0)) {
  634. /* Okay, let's call __set. */
  635. zend_fcall_info fci;
  636. zval fmember;
  637. INIT_ZVAL(fmember);
  638. ZVAL_STRING(&fmember, "__set", 0);
  639. zval *php_ret_value;
  640. fci.size = sizeof(fci);
  641. fci.function_table = &ce->function_table;
  642. fci.function_name = &fmember;
  643. fci.symbol_table = NULL;
  644. fci.retval_ptr_ptr = &php_ret_value;
  645. zval *zname_ptr = &zname;
  646. zval **params[2];
  647. fci.param_count = 2;
  648. fci.params = params;
  649. fci.params[0] = &zname_ptr;
  650. fci.params[1] = &php_value;
  651. fci.object_ptr = object;
  652. fci.no_separation = 1;
  653. zend_call_function(&fci, NULL TSRMLS_CC);
  654. ret_value = zval_to_v8js(php_ret_value, isolate TSRMLS_CC);
  655. /* We don't own the reference to php_ret_value... unless the
  656. * returned refcount was 0, in which case the below code
  657. * will free it. */
  658. zval_add_ref(&php_ret_value);
  659. zval_ptr_dtor(&php_ret_value);
  660. }
  661. }
  662. // if PHP wanted to hold on to this value, update_property would
  663. // have bumped the refcount
  664. zval_ptr_dtor(&php_value);
  665. } else if (callback_type == V8JS_PROP_QUERY ||
  666. callback_type == V8JS_PROP_DELETER) {
  667. const zend_object_handlers *h = Z_OBJ_HT_P(object);
  668. zval *prop;
  669. MAKE_STD_ZVAL(prop);
  670. ZVAL_STRINGL(prop, name, name_len, 1);
  671. if (callback_type == V8JS_PROP_QUERY) {
  672. if (h->has_property(object, prop, 0 ZEND_HASH_KEY_NULL TSRMLS_CC)) {
  673. ret_value = V8JS_UINT(v8::None);
  674. } else {
  675. ret_value = v8::Handle<v8::Value>(); // empty handle
  676. }
  677. } else {
  678. zend_property_info *property_info = zend_get_property_info(ce, prop, 1 TSRMLS_CC);
  679. if(property_info && property_info->flags & ZEND_ACC_PUBLIC) {
  680. h->unset_property(object, prop ZEND_HASH_KEY_NULL TSRMLS_CC);
  681. ret_value = V8JS_BOOL(true);
  682. }
  683. else {
  684. ret_value = v8::Handle<v8::Value>(); // empty handle
  685. }
  686. }
  687. zval_ptr_dtor(&prop);
  688. } else {
  689. /* shouldn't reach here! but bail safely */
  690. ret_value = v8::Handle<v8::Value>();
  691. }
  692. }
  693. efree(lower);
  694. return ret_value;
  695. }
  696. /* }}} */
  697. static void php_v8js_named_property_getter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
  698. {
  699. info.GetReturnValue().Set(php_v8js_named_property_callback(property, info, V8JS_PROP_GETTER));
  700. }
  701. /* }}} */
  702. static void php_v8js_named_property_setter(v8::Local<v8::String> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value> &info) /* {{{ */
  703. {
  704. info.GetReturnValue().Set(php_v8js_named_property_callback(property, info, V8JS_PROP_SETTER, value));
  705. }
  706. /* }}} */
  707. static void php_v8js_named_property_query(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Integer> &info) /* {{{ */
  708. {
  709. v8::Local<v8::Value> r = php_v8js_named_property_callback(property, info, V8JS_PROP_QUERY);
  710. if (!r.IsEmpty()) {
  711. info.GetReturnValue().Set(r->ToInteger());
  712. }
  713. }
  714. /* }}} */
  715. static void php_v8js_named_property_deleter(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Boolean> &info) /* {{{ */
  716. {
  717. v8::Local<v8::Value> r = php_v8js_named_property_callback(property, info, V8JS_PROP_DELETER);
  718. if (!r.IsEmpty()) {
  719. info.GetReturnValue().Set(r->ToBoolean());
  720. }
  721. }
  722. /* }}} */
  723. static v8::Handle<v8::Value> php_v8js_hash_to_jsobj(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  724. {
  725. v8::Handle<v8::Object> newobj;
  726. int i;
  727. char *key = NULL;
  728. ulong index;
  729. uint key_len;
  730. HashTable *myht;
  731. HashPosition pos;
  732. zend_class_entry *ce = NULL;
  733. if (Z_TYPE_P(value) == IS_ARRAY) {
  734. myht = HASH_OF(value);
  735. } else {
  736. myht = Z_OBJPROP_P(value);
  737. ce = Z_OBJCE_P(value);
  738. }
  739. /* Prevent recursion */
  740. if (myht && myht->nApplyCount > 1) {
  741. return V8JS_NULL;
  742. }
  743. /* Object methods */
  744. if (ce == php_ce_v8_function) {
  745. php_v8js_object *c = (php_v8js_object *) zend_object_store_get_object(value TSRMLS_CC);
  746. v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, c->v8obj);
  747. return v8obj;
  748. } else if (ce) {
  749. #if PHP_V8_API_VERSION <= 3023008
  750. /* Until V8 3.23.8 Isolate could only take one external pointer. */
  751. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData();
  752. #else
  753. php_v8js_ctx *ctx = (php_v8js_ctx *) isolate->GetData(0);
  754. #endif
  755. v8::Local<v8::FunctionTemplate> new_tpl;
  756. v8js_tmpl_t *persist_tpl_;
  757. try {
  758. new_tpl = v8::Local<v8::FunctionTemplate>::New
  759. (isolate, ctx->template_cache.at(ce->name));
  760. }
  761. catch (const std::out_of_range &) {
  762. /* No cached v8::FunctionTemplate available as of yet, create one. */
  763. new_tpl = V8JS_NEW(v8::FunctionTemplate, isolate, 0);
  764. new_tpl->SetClassName(V8JS_STRL(ce->name, ce->name_length));
  765. new_tpl->InstanceTemplate()->SetInternalFieldCount(1);
  766. if (ce == zend_ce_closure) {
  767. /* Got a closure, mustn't cache ... */
  768. persist_tpl_ = new v8js_tmpl_t(isolate, new_tpl);
  769. /* We'll free persist_tpl_ via php_v8js_weak_closure_callback, below */
  770. new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_php_callback);
  771. } else {
  772. /* Add new v8::FunctionTemplate to tpl_map, as long as it is not a closure. */
  773. persist_tpl_ = &ctx->template_cache[ce->name];
  774. persist_tpl_->Reset(isolate, new_tpl);
  775. /* We'll free persist_tpl_ when template_cache is destroyed */
  776. // Finish setup of new_tpl
  777. new_tpl->InstanceTemplate()->SetNamedPropertyHandler
  778. (php_v8js_named_property_getter, /* getter */
  779. php_v8js_named_property_setter, /* setter */
  780. php_v8js_named_property_query, /* query */
  781. php_v8js_named_property_deleter, /* deleter */
  782. php_v8js_named_property_enumerator, /* enumerator */
  783. V8JS_NULL /* data */
  784. );
  785. // add __invoke() handler
  786. zend_function *invoke_method_ptr;
  787. if (zend_hash_find(&ce->function_table, ZEND_INVOKE_FUNC_NAME,
  788. sizeof(ZEND_INVOKE_FUNC_NAME),
  789. (void**)&invoke_method_ptr) == SUCCESS &&
  790. invoke_method_ptr->common.fn_flags & ZEND_ACC_PUBLIC) {
  791. new_tpl->InstanceTemplate()->SetCallAsFunctionHandler(php_v8js_invoke_callback, PHP_V8JS_CALLBACK(isolate, invoke_method_ptr, new_tpl));
  792. }
  793. }
  794. v8::Local<v8::Array> call_handler_data = V8JS_NEW(v8::Array, isolate, 2);
  795. call_handler_data->Set(0, V8JS_NEW(v8::External, isolate, persist_tpl_));
  796. call_handler_data->Set(1, V8JS_NEW(v8::External, isolate, ce));
  797. new_tpl->SetCallHandler(php_v8js_construct_callback, call_handler_data);
  798. }
  799. // Create v8 wrapper object
  800. v8::Handle<v8::Value> external = V8JS_NEW(v8::External, isolate, value);
  801. newobj = new_tpl->GetFunction()->NewInstance(1, &external);
  802. if (ce == zend_ce_closure) {
  803. // free uncached function template when object is freed
  804. ctx->weak_closures[persist_tpl_].Reset(isolate, newobj);
  805. ctx->weak_closures[persist_tpl_].SetWeak(persist_tpl_, php_v8js_weak_closure_callback);
  806. }
  807. } else {
  808. // @todo re-use template likewise
  809. v8::Local<v8::FunctionTemplate> new_tpl = V8JS_NEW(v8::FunctionTemplate, isolate, 0);
  810. new_tpl->SetClassName(V8JS_SYM("Array"));
  811. newobj = new_tpl->InstanceTemplate()->NewInstance();
  812. }
  813. /* Object properties */
  814. i = myht ? zend_hash_num_elements(myht) : 0;
  815. if (i > 0 && !ce)
  816. {
  817. zval **data;
  818. HashTable *tmp_ht;
  819. zend_hash_internal_pointer_reset_ex(myht, &pos);
  820. for (;; zend_hash_move_forward_ex(myht, &pos)) {
  821. i = zend_hash_get_current_key_ex(myht, &key, &key_len, &index, 0, &pos);
  822. if (i == HASH_KEY_NON_EXISTANT)
  823. break;
  824. if (zend_hash_get_current_data_ex(myht, (void **) &data, &pos) == SUCCESS)
  825. {
  826. tmp_ht = HASH_OF(*data);
  827. if (tmp_ht) {
  828. tmp_ht->nApplyCount++;
  829. }
  830. if (i == HASH_KEY_IS_STRING)
  831. {
  832. if (key[0] == '\0' && Z_TYPE_P(value) == IS_OBJECT) {
  833. /* Skip protected and private members. */
  834. if (tmp_ht) {
  835. tmp_ht->nApplyCount--;
  836. }
  837. continue;
  838. }
  839. newobj->Set(V8JS_STRL(key, key_len - 1), zval_to_v8js(*data, isolate TSRMLS_CC), v8::ReadOnly);
  840. } else {
  841. newobj->Set(index, zval_to_v8js(*data, isolate TSRMLS_CC));
  842. }
  843. if (tmp_ht) {
  844. tmp_ht->nApplyCount--;
  845. }
  846. }
  847. }
  848. }
  849. return newobj;
  850. }
  851. /* }}} */
  852. static v8::Handle<v8::Value> php_v8js_hash_to_jsarr(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  853. {
  854. HashTable *myht = HASH_OF(value);
  855. int i = myht ? zend_hash_num_elements(myht) : 0;
  856. /* Return object if dealing with assoc array */
  857. if (i > 0 && _php_v8js_is_assoc_array(myht TSRMLS_CC)) {
  858. return php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  859. }
  860. v8::Local<v8::Array> newarr;
  861. /* Prevent recursion */
  862. if (myht && myht->nApplyCount > 1) {
  863. return V8JS_NULL;
  864. }
  865. newarr = V8JS_NEW(v8::Array, isolate, i);
  866. if (i > 0)
  867. {
  868. zval **data;
  869. ulong index = 0;
  870. HashTable *tmp_ht;
  871. HashPosition pos;
  872. for (zend_hash_internal_pointer_reset_ex(myht, &pos);
  873. SUCCESS == zend_hash_get_current_data_ex(myht, (void **) &data, &pos);
  874. zend_hash_move_forward_ex(myht, &pos)
  875. ) {
  876. tmp_ht = HASH_OF(*data);
  877. if (tmp_ht) {
  878. tmp_ht->nApplyCount++;
  879. }
  880. newarr->Set(index++, zval_to_v8js(*data, isolate TSRMLS_CC));
  881. if (tmp_ht) {
  882. tmp_ht->nApplyCount--;
  883. }
  884. }
  885. }
  886. return newarr;
  887. }
  888. /* }}} */
  889. v8::Handle<v8::Value> zval_to_v8js(zval *value, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  890. {
  891. v8::Handle<v8::Value> jsValue;
  892. long v;
  893. zend_class_entry *ce;
  894. switch (Z_TYPE_P(value))
  895. {
  896. case IS_ARRAY:
  897. jsValue = php_v8js_hash_to_jsarr(value, isolate TSRMLS_CC);
  898. break;
  899. case IS_OBJECT:
  900. if (V8JSG(use_date)) {
  901. ce = php_date_get_date_ce();
  902. if (instanceof_function(Z_OBJCE_P(value), ce TSRMLS_CC)) {
  903. zval *dtval;
  904. zend_call_method_with_0_params(&value, NULL, NULL, "getTimestamp", &dtval);
  905. if (dtval)
  906. jsValue = V8JS_DATE(((double)Z_LVAL_P(dtval) * 1000.0));
  907. else
  908. jsValue = V8JS_NULL;
  909. } else
  910. jsValue = php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  911. } else
  912. jsValue = php_v8js_hash_to_jsobj(value, isolate TSRMLS_CC);
  913. break;
  914. case IS_STRING:
  915. jsValue = V8JS_STRL(Z_STRVAL_P(value), Z_STRLEN_P(value));
  916. break;
  917. case IS_LONG:
  918. v = Z_LVAL_P(value);
  919. if (v < - std::numeric_limits<int32_t>::min() || v > std::numeric_limits<int32_t>::max()) {
  920. jsValue = V8JS_FLOAT((double)v);
  921. } else {
  922. jsValue = V8JS_INT(v);
  923. }
  924. break;
  925. case IS_DOUBLE:
  926. jsValue = V8JS_FLOAT(Z_DVAL_P(value));
  927. break;
  928. case IS_BOOL:
  929. jsValue = V8JS_BOOL(Z_BVAL_P(value));
  930. break;
  931. default:
  932. case IS_NULL:
  933. jsValue = V8JS_NULL;
  934. break;
  935. }
  936. return jsValue;
  937. }
  938. /* }}} */
  939. int v8js_to_zval(v8::Handle<v8::Value> jsValue, zval *return_value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
  940. {
  941. if (jsValue->IsString())
  942. {
  943. v8::String::Utf8Value str(jsValue);
  944. const char *cstr = ToCString(str);
  945. RETVAL_STRINGL(cstr, jsValue->ToString()->Utf8Length(), 1);
  946. // RETVAL_STRING(cstr, 1);
  947. }
  948. else if (jsValue->IsBoolean())
  949. {
  950. RETVAL_BOOL(jsValue->Uint32Value());
  951. }
  952. else if (jsValue->IsInt32() || jsValue->IsUint32())
  953. {
  954. RETVAL_LONG((long) jsValue->IntegerValue());
  955. }
  956. else if (jsValue->IsNumber())
  957. {
  958. RETVAL_DOUBLE(jsValue->NumberValue());
  959. }
  960. else if (jsValue->IsDate()) /* Return as a PHP DateTime object */
  961. {
  962. v8::String::Utf8Value str(jsValue);
  963. const char *cstr = ToCString(str);
  964. zend_class_entry *ce = php_date_get_date_ce();
  965. #if PHP_VERSION_ID < 50304
  966. zval *param;
  967. MAKE_STD_ZVAL(param);
  968. ZVAL_STRING(param, cstr, 1);
  969. object_init_ex(return_value, ce TSRMLS_CC);
  970. zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, param);
  971. zval_ptr_dtor(&param);
  972. if (EG(exception)) {
  973. return FAILURE;
  974. }
  975. #else
  976. php_date_instantiate(ce, return_value TSRMLS_CC);
  977. 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)) {
  978. return FAILURE;
  979. }
  980. #endif
  981. }
  982. else if (jsValue->IsObject())
  983. {
  984. v8::Handle<v8::Object> self = v8::Handle<v8::Object>::Cast(jsValue);
  985. // if this is a wrapped PHP object, then just unwrap it.
  986. v8::Local<v8::Value> php_object = self->GetHiddenValue(V8JS_SYM(PHPJS_OBJECT_KEY));
  987. if (!php_object.IsEmpty()) {
  988. zval *object = reinterpret_cast<zval *>(v8::External::Cast(*php_object)->Value());
  989. RETVAL_ZVAL(object, 1, 0);
  990. return SUCCESS;
  991. }
  992. if ((flags & V8JS_FLAG_FORCE_ARRAY) || jsValue->IsArray()) {
  993. array_init(return_value);
  994. return php_v8js_v8_get_properties_hash(jsValue, Z_ARRVAL_P(return_value), flags, isolate TSRMLS_CC);
  995. } else {
  996. php_v8js_create_v8(return_value, jsValue, flags, isolate TSRMLS_CC);
  997. return SUCCESS;
  998. }
  999. }
  1000. else /* types External, RegExp, Undefined and Null are considered NULL */
  1001. {
  1002. RETVAL_NULL();
  1003. }
  1004. return SUCCESS;
  1005. }
  1006. /* }}} */
  1007. /*
  1008. * Local variables:
  1009. * tab-width: 4
  1010. * c-basic-offset: 4
  1011. * End:
  1012. * vim600: noet sw=4 ts=4 fdm=marker
  1013. * vim<600: noet sw=4 ts=4
  1014. */