v8js_methods.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2012 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | [email protected] so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Jani Taskinen <[email protected]> |
  16. | Author: Patrick Reilly <[email protected]> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. extern "C" {
  24. #include "php.h"
  25. }
  26. #include "php_v8js_macros.h"
  27. #include <v8.h>
  28. /* global.exit - terminate execution */
  29. V8JS_METHOD(exit) /* {{{ */
  30. {
  31. v8::V8::TerminateExecution();
  32. return v8::Undefined();
  33. }
  34. /* }}} */
  35. /* global.sleep - sleep for passed seconds */
  36. V8JS_METHOD(sleep) /* {{{ */
  37. {
  38. php_sleep(args[0]->Int32Value());
  39. return v8::Undefined();
  40. }
  41. /* }}} */
  42. /* global.print - php print() */
  43. V8JS_METHOD(print) /* {{{ */
  44. {
  45. int ret = 0;
  46. TSRMLS_FETCH();
  47. for (int i = 0; i < args.Length(); i++) {
  48. v8::String::Utf8Value str(args[i]);
  49. const char *cstr = ToCString(str);
  50. ret = PHPWRITE(cstr, strlen(cstr));
  51. }
  52. return V8JS_INT(ret);
  53. }
  54. /* }}} */
  55. static void _php_v8js_dumper(v8::Local<v8::Value> var, int level TSRMLS_DC) /* {{{ */
  56. {
  57. v8::String::Utf8Value str(var->ToDetailString());
  58. const char *valstr = ToCString(str);
  59. size_t valstr_len = (valstr) ? strlen(valstr) : 0;
  60. if (level > 1) {
  61. php_printf("%*c", (level - 1) * 2, ' ');
  62. }
  63. if (var->IsString())
  64. {
  65. php_printf("string(%d) \"%s\"\n", valstr_len, valstr);
  66. }
  67. else if (var->IsBoolean())
  68. {
  69. php_printf("bool(%s)\n", valstr);
  70. }
  71. else if (var->IsInt32() || var->IsUint32())
  72. {
  73. php_printf("int(%s)\n", valstr);
  74. }
  75. else if (var->IsNumber())
  76. {
  77. php_printf("float(%s)\n", valstr);
  78. }
  79. else if (var->IsDate())
  80. {
  81. php_printf("Date(%s)\n", valstr);
  82. }
  83. #if PHP_V8_API_VERSION >= 2003007
  84. else if (var->IsRegExp())
  85. {
  86. php_printf("RegExp(%s)\n", valstr);
  87. }
  88. #endif
  89. else if (var->IsArray())
  90. {
  91. v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(var);
  92. uint32_t length = array->Length();
  93. php_printf("array(%d) {\n", length);
  94. for (unsigned i = 0; i < length; i++) {
  95. php_printf("%*c[%d] =>\n", level * 2, ' ', i);
  96. _php_v8js_dumper(array->Get(i), level + 1 TSRMLS_CC);
  97. }
  98. if (level > 1) {
  99. php_printf("%*c", (level - 1) * 2, ' ');
  100. }
  101. ZEND_PUTS("}\n");
  102. }
  103. else if (var->IsObject())
  104. {
  105. v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(var);
  106. V8JS_GET_CLASS_NAME(cname, object);
  107. if (var->IsFunction())
  108. {
  109. v8::String::Utf8Value csource(object->ToString());
  110. php_printf("object(%s)#%d {\n%*c%s\n", ToCString(cname), object->GetIdentityHash(), level * 2 + 2, ' ', ToCString(csource));
  111. }
  112. else
  113. {
  114. v8::Local<v8::Array> keys = object->GetPropertyNames();
  115. uint32_t length = keys->Length();
  116. php_printf("object(%s)#%d (%d) {\n", ToCString(cname), object->GetIdentityHash(), length);
  117. for (unsigned i = 0; i < length; i++) {
  118. v8::Local<v8::String> key = keys->Get(i)->ToString();
  119. v8::String::Utf8Value kname(key);
  120. php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(kname));
  121. _php_v8js_dumper(object->Get(key), level + 1 TSRMLS_CC);
  122. }
  123. }
  124. if (level > 1) {
  125. php_printf("%*c", (level - 1) * 2, ' ');
  126. }
  127. ZEND_PUTS("}\n");
  128. }
  129. else /* null, undefined, etc. */
  130. {
  131. php_printf("<%s>\n", valstr);
  132. }
  133. }
  134. /* }}} */
  135. /* global.var_dump - Dump JS values */
  136. V8JS_METHOD(var_dump) /* {{{ */
  137. {
  138. int i;
  139. TSRMLS_FETCH();
  140. for (int i = 0; i < args.Length(); i++) {
  141. _php_v8js_dumper(args[i], 1 TSRMLS_CC);
  142. }
  143. return V8JS_NULL;
  144. }
  145. /* }}} */
  146. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate> global) /* {{{ */
  147. {
  148. global->Set(V8JS_SYM("exit"), v8::FunctionTemplate::New(V8JS_MN(exit)), v8::ReadOnly);
  149. global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(V8JS_MN(sleep)), v8::ReadOnly);
  150. global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);
  151. global->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(V8JS_MN(var_dump)), v8::ReadOnly);
  152. }
  153. /* }}} */
  154. /*
  155. * Local variables:
  156. * tab-width: 4
  157. * c-basic-offset: 4
  158. * End:
  159. * vim600: noet sw=4 ts=4 fdm=marker
  160. * vim<600: noet sw=4 ts=4
  161. */