v8js_methods.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2010 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. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. extern "C" {
  23. #include "php.h"
  24. }
  25. #include "php_v8js_macros.h"
  26. #include <v8.h>
  27. /* global.exit - terminate execution */
  28. V8JS_METHOD(exit) /* {{{ */
  29. {
  30. v8::V8::TerminateExecution();
  31. return v8::Undefined();
  32. }
  33. /* }}} */
  34. /* global.sleep - sleep for passed seconds */
  35. V8JS_METHOD(sleep) /* {{{ */
  36. {
  37. php_sleep(args[0]->Int32Value());
  38. return v8::Undefined();
  39. }
  40. /* }}} */
  41. /* global.print - php print() */
  42. V8JS_METHOD(print) /* {{{ */
  43. {
  44. int ret = 0;
  45. TSRMLS_FETCH();
  46. for (int i = 0; i < args.Length(); i++) {
  47. v8::String::Utf8Value str(args[i]);
  48. const char *cstr = ToCString(str);
  49. ret = PHPWRITE(cstr, strlen(cstr));
  50. }
  51. return V8JS_INT(ret);
  52. }
  53. /* }}} */
  54. static void _php_v8js_dumper(v8::Local<v8::Value> var, int level TSRMLS_DC) /* {{{ */
  55. {
  56. v8::String::Utf8Value str(var->ToDetailString());
  57. const char *valstr = ToCString(str);
  58. size_t valstr_len = (valstr) ? strlen(valstr) : 0;
  59. if (level > 1) {
  60. php_printf("%*c", (level - 1) * 2, ' ');
  61. }
  62. if (var->IsString())
  63. {
  64. php_printf("string(%d) \"%s\"\n", valstr_len, valstr);
  65. }
  66. else if (var->IsBoolean())
  67. {
  68. php_printf("bool(%s)\n", valstr);
  69. }
  70. else if (var->IsInt32() || var->IsUint32())
  71. {
  72. php_printf("int(%s)\n", valstr);
  73. }
  74. else if (var->IsNumber())
  75. {
  76. php_printf("float(%s)\n", valstr);
  77. }
  78. else if (var->IsDate())
  79. {
  80. php_printf("Date(%s)\n", valstr);
  81. }
  82. #if PHP_V8_API_VERSION >= 2003007
  83. else if (var->IsRegExp())
  84. {
  85. php_printf("RegExp(%s)\n", valstr);
  86. }
  87. #endif
  88. else if (var->IsArray())
  89. {
  90. v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(var);
  91. uint32_t length = array->Length();
  92. php_printf("array(%d) {\n", length);
  93. for (unsigned i = 0; i < length; i++) {
  94. php_printf("%*c[%d] =>\n", level * 2, ' ', i);
  95. _php_v8js_dumper(array->Get(i), level + 1 TSRMLS_CC);
  96. }
  97. if (level > 1) {
  98. php_printf("%*c", (level - 1) * 2, ' ');
  99. }
  100. ZEND_PUTS("}\n");
  101. }
  102. else if (var->IsObject())
  103. {
  104. v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(var);
  105. V8JS_GET_CLASS_NAME(cname, object);
  106. if (var->IsFunction())
  107. {
  108. v8::String::Utf8Value csource(object->ToString());
  109. php_printf("object(%s)#%d {\n%*c%s\n", ToCString(cname), object->GetIdentityHash(), level * 2 + 2, ' ', ToCString(csource));
  110. }
  111. else
  112. {
  113. v8::Local<v8::Array> keys = object->GetPropertyNames();
  114. uint32_t length = keys->Length();
  115. php_printf("object(%s)#%d (%d) {\n", ToCString(cname), object->GetIdentityHash(), length);
  116. for (unsigned i = 0; i < length; i++) {
  117. v8::Local<v8::String> key = keys->Get(i)->ToString();
  118. v8::String::Utf8Value kname(key);
  119. php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(kname));
  120. _php_v8js_dumper(object->Get(key), level + 1 TSRMLS_CC);
  121. }
  122. }
  123. if (level > 1) {
  124. php_printf("%*c", (level - 1) * 2, ' ');
  125. }
  126. ZEND_PUTS("}\n");
  127. }
  128. else /* null, undefined, etc. */
  129. {
  130. php_printf("<%s>\n", valstr);
  131. }
  132. }
  133. /* }}} */
  134. /* global.var_dump - Dump JS values */
  135. V8JS_METHOD(var_dump) /* {{{ */
  136. {
  137. int i;
  138. TSRMLS_FETCH();
  139. for (int i = 0; i < args.Length(); i++) {
  140. _php_v8js_dumper(args[i], 1 TSRMLS_CC);
  141. }
  142. return V8JS_NULL;
  143. }
  144. /* }}} */
  145. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate> global) /* {{{ */
  146. {
  147. global->Set(V8JS_SYM("exit"), v8::FunctionTemplate::New(V8JS_MN(exit)), v8::ReadOnly);
  148. global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(V8JS_MN(sleep)), v8::ReadOnly);
  149. global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);
  150. global->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(V8JS_MN(var_dump)), v8::ReadOnly);
  151. }
  152. /* }}} */
  153. /*
  154. * Local variables:
  155. * tab-width: 4
  156. * c-basic-offset: 4
  157. * End:
  158. * vim600: noet sw=4 ts=4 fdm=marker
  159. * vim<600: noet sw=4 ts=4
  160. */