v8js_convert.cc 36 KB

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