v8js_convert.cc 37 KB

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