v8js_methods.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. else if (var->IsRegExp())
  83. {
  84. php_printf("RegExp(%s)\n", valstr);
  85. }
  86. else if (var->IsArray())
  87. {
  88. v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(var);
  89. uint32_t length = array->Length();
  90. php_printf("array(%d) {\n", length);
  91. for (unsigned i = 0; i < length; i++) {
  92. php_printf("%*c[%d] =>\n", level * 2, ' ', i);
  93. _php_v8js_dumper(array->Get(i), level + 1 TSRMLS_CC);
  94. }
  95. if (level > 1) {
  96. php_printf("%*c", (level - 1) * 2, ' ');
  97. }
  98. ZEND_PUTS("}\n");
  99. }
  100. else if (var->IsObject())
  101. {
  102. v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(var);
  103. V8JS_GET_CLASS_NAME(cname, object);
  104. if (var->IsFunction())
  105. {
  106. v8::String::Utf8Value csource(object->ToString());
  107. php_printf("object(%s)#%d {\n%*c%s\n", ToCString(cname), object->GetIdentityHash(), level * 2 + 2, ' ', ToCString(csource));
  108. }
  109. else
  110. {
  111. v8::Local<v8::Array> keys = object->GetPropertyNames();
  112. uint32_t length = keys->Length();
  113. php_printf("object(%s)#%d (%d) {\n", ToCString(cname), object->GetIdentityHash(), length);
  114. for (unsigned i = 0; i < length; i++) {
  115. v8::Local<v8::String> key = keys->Get(i)->ToString();
  116. v8::String::Utf8Value kname(key);
  117. php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(kname));
  118. _php_v8js_dumper(object->Get(key), level + 1 TSRMLS_CC);
  119. }
  120. }
  121. if (level > 1) {
  122. php_printf("%*c", (level - 1) * 2, ' ');
  123. }
  124. ZEND_PUTS("}\n");
  125. }
  126. else /* null, undefined, etc. */
  127. {
  128. php_printf("<%s>\n", valstr);
  129. }
  130. }
  131. /* }}} */
  132. /* global.var_dump - Dump JS values */
  133. V8JS_METHOD(var_dump) /* {{{ */
  134. {
  135. int i;
  136. TSRMLS_FETCH();
  137. for (int i = 0; i < args.Length(); i++) {
  138. _php_v8js_dumper(args[i], 1 TSRMLS_CC);
  139. }
  140. return V8JS_NULL;
  141. }
  142. /* }}} */
  143. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate> global) /* {{{ */
  144. {
  145. global->Set(V8JS_SYM("exit"), v8::FunctionTemplate::New(V8JS_MN(exit)), v8::ReadOnly);
  146. global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(V8JS_MN(sleep)), v8::ReadOnly);
  147. global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);
  148. global->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(V8JS_MN(var_dump)), v8::ReadOnly);
  149. }
  150. /* }}} */
  151. /*
  152. * Local variables:
  153. * tab-width: 4
  154. * c-basic-offset: 4
  155. * End:
  156. * vim600: noet sw=4 ts=4 fdm=marker
  157. * vim<600: noet sw=4 ts=4
  158. */