v8js_convert.cc 36 KB

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