v8js_methods.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include <map>
  29. /* global.exit - terminate execution */
  30. V8JS_METHOD(exit) /* {{{ */
  31. {
  32. v8::V8::TerminateExecution();
  33. return v8::Undefined();
  34. }
  35. /* }}} */
  36. /* global.sleep - sleep for passed seconds */
  37. V8JS_METHOD(sleep) /* {{{ */
  38. {
  39. php_sleep(args[0]->Int32Value());
  40. return v8::Undefined();
  41. }
  42. /* }}} */
  43. /* global.print - php print() */
  44. V8JS_METHOD(print) /* {{{ */
  45. {
  46. int ret = 0;
  47. TSRMLS_FETCH();
  48. for (int i = 0; i < args.Length(); i++) {
  49. v8::String::Utf8Value str(args[i]);
  50. const char *cstr = ToCString(str);
  51. ret = PHPWRITE(cstr, strlen(cstr));
  52. }
  53. return V8JS_INT(ret);
  54. }
  55. /* }}} */
  56. static void _php_v8js_dumper(v8::Local<v8::Value> var, int level TSRMLS_DC) /* {{{ */
  57. {
  58. v8::String::Utf8Value str(var->ToDetailString());
  59. const char *valstr = ToCString(str);
  60. size_t valstr_len = (valstr) ? strlen(valstr) : 0;
  61. if (level > 1) {
  62. php_printf("%*c", (level - 1) * 2, ' ');
  63. }
  64. if (var->IsString())
  65. {
  66. php_printf("string(%d) \"%s\"\n", valstr_len, valstr);
  67. }
  68. else if (var->IsBoolean())
  69. {
  70. php_printf("bool(%s)\n", valstr);
  71. }
  72. else if (var->IsInt32() || var->IsUint32())
  73. {
  74. php_printf("int(%s)\n", valstr);
  75. }
  76. else if (var->IsNumber())
  77. {
  78. php_printf("float(%s)\n", valstr);
  79. }
  80. else if (var->IsDate())
  81. {
  82. php_printf("Date(%s)\n", valstr);
  83. }
  84. #if PHP_V8_API_VERSION >= 2003007
  85. else if (var->IsRegExp())
  86. {
  87. php_printf("RegExp(%s)\n", valstr);
  88. }
  89. #endif
  90. else if (var->IsArray())
  91. {
  92. v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(var);
  93. uint32_t length = array->Length();
  94. php_printf("array(%d) {\n", length);
  95. for (unsigned i = 0; i < length; i++) {
  96. php_printf("%*c[%d] =>\n", level * 2, ' ', i);
  97. _php_v8js_dumper(array->Get(i), level + 1 TSRMLS_CC);
  98. }
  99. if (level > 1) {
  100. php_printf("%*c", (level - 1) * 2, ' ');
  101. }
  102. ZEND_PUTS("}\n");
  103. }
  104. else if (var->IsObject())
  105. {
  106. v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(var);
  107. V8JS_GET_CLASS_NAME(cname, object);
  108. if (var->IsFunction())
  109. {
  110. v8::String::Utf8Value csource(object->ToString());
  111. php_printf("object(%s)#%d {\n%*c%s\n", ToCString(cname), object->GetIdentityHash(), level * 2 + 2, ' ', ToCString(csource));
  112. }
  113. else
  114. {
  115. v8::Local<v8::Array> keys = object->GetPropertyNames();
  116. uint32_t length = keys->Length();
  117. php_printf("object(%s)#%d (%d) {\n", ToCString(cname), object->GetIdentityHash(), length);
  118. for (unsigned i = 0; i < length; i++) {
  119. v8::Local<v8::String> key = keys->Get(i)->ToString();
  120. v8::String::Utf8Value kname(key);
  121. php_printf("%*c[\"%s\"] =>\n", level * 2, ' ', ToCString(kname));
  122. _php_v8js_dumper(object->Get(key), level + 1 TSRMLS_CC);
  123. }
  124. }
  125. if (level > 1) {
  126. php_printf("%*c", (level - 1) * 2, ' ');
  127. }
  128. ZEND_PUTS("}\n");
  129. }
  130. else /* null, undefined, etc. */
  131. {
  132. php_printf("<%s>\n", valstr);
  133. }
  134. }
  135. /* }}} */
  136. /* global.var_dump - Dump JS values */
  137. V8JS_METHOD(var_dump) /* {{{ */
  138. {
  139. int i;
  140. TSRMLS_FETCH();
  141. for (int i = 0; i < args.Length(); i++) {
  142. _php_v8js_dumper(args[i], 1 TSRMLS_CC);
  143. }
  144. return V8JS_NULL;
  145. }
  146. /* }}} */
  147. // TODO: Put this in php_v8js_context
  148. std::map<char *, v8::Handle<v8::Object> > modules_loaded;
  149. V8JS_METHOD(require)
  150. {
  151. //v8::Persistent<v8::Value> module_name_value_v8 = v8::Persistent<v8::Value>::New(args[0]->ToObject());
  152. v8::String::Utf8Value module_name_v8(args[0]);
  153. // Make sure to duplicate the string to ensure it is not freed by V8's garbage collector
  154. char *module_name = strdup(ToCString(module_name_v8));
  155. if (modules_loaded.count(module_name) > 0) {
  156. printf("Using cached module\n");
  157. return modules_loaded[module_name];
  158. }
  159. zval module_code;
  160. zval *module_name_zend;
  161. MAKE_STD_ZVAL(module_name_zend);
  162. ZVAL_STRING(module_name_zend, module_name, 1);
  163. zval* params[] = { module_name_zend };
  164. // Get the module loader from extension context
  165. v8::Handle<v8::External> data = v8::Handle<v8::External>::Cast(args.Data());
  166. php_v8js_ctx *c = static_cast<php_v8js_ctx*>(data->Value());
  167. if (c->module_loader == NULL) {
  168. printf("Module loader not defined!\n");
  169. exit(-1);
  170. }
  171. if (SUCCESS == call_user_function(EG(function_table), NULL, c->module_loader, &module_code, 1, params TSRMLS_CC)) {
  172. printf("CODE = %s\n", Z_STRVAL(module_code));
  173. } else {
  174. printf("Module loader callback failed\n");
  175. exit(-1);
  176. }
  177. // Create a template for the global object and set the built-in global functions
  178. v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
  179. global->Set(v8::String::New("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);
  180. global->Set(v8::String::New("require"), v8::FunctionTemplate::New(V8JS_MN(require), v8::External::New(c)), v8::ReadOnly);
  181. // Add the exports object
  182. v8::Handle<v8::ObjectTemplate> exports_template = v8::ObjectTemplate::New();
  183. v8::Handle<v8::Object> exports = exports_template->NewInstance();
  184. global->Set(v8::String::New("exports"), exports);
  185. // Each module gets its own context so different modules do not affect each other
  186. v8::Persistent<v8::Context> context = v8::Context::New(NULL, global);
  187. // Catch JS exceptions
  188. v8::TryCatch try_catch;
  189. // Enter the module context
  190. v8::Context::Scope scope(context);
  191. v8::HandleScope handle_scope;
  192. // Set script identifier
  193. v8::Local<v8::String> sname = V8JS_SYM("require");
  194. // TODO: Load module code here
  195. v8::Local<v8::String> source = v8::String::New(Z_STRVAL(module_code));
  196. // Create and compile script
  197. v8::Local<v8::Script> script = v8::Script::New(source, sname);
  198. // The script will be empty if there are compile errors
  199. if (script.IsEmpty()) {
  200. //php_v8js_throw_exception(&try_catch TSRMLS_CC);
  201. printf("Compile error\n");
  202. exit(-1);
  203. return V8JS_NULL;
  204. }
  205. // Run script
  206. //c->in_execution++;
  207. v8::Local<v8::Value> result = script->Run();
  208. //c->in_execution--;
  209. // Script possibly terminated, return immediately
  210. if (!try_catch.CanContinue()) {
  211. // TODO: Throw PHP exception here?
  212. return V8JS_NULL;
  213. }
  214. // Handle runtime JS exceptions
  215. if (try_catch.HasCaught()) {
  216. // Rethrow the exception back to JS
  217. try_catch.ReThrow();
  218. return V8JS_NULL;
  219. }
  220. // Cache the module
  221. modules_loaded[module_name] = handle_scope.Close(exports);
  222. return modules_loaded[module_name];
  223. }
  224. void php_v8js_register_methods(v8::Handle<v8::ObjectTemplate> global, php_v8js_ctx *c) /* {{{ */
  225. {
  226. global->Set(V8JS_SYM("exit"), v8::FunctionTemplate::New(V8JS_MN(exit)), v8::ReadOnly);
  227. global->Set(V8JS_SYM("sleep"), v8::FunctionTemplate::New(V8JS_MN(sleep)), v8::ReadOnly);
  228. global->Set(V8JS_SYM("print"), v8::FunctionTemplate::New(V8JS_MN(print)), v8::ReadOnly);
  229. global->Set(V8JS_SYM("var_dump"), v8::FunctionTemplate::New(V8JS_MN(var_dump)), v8::ReadOnly);
  230. global->Set(V8JS_SYM("require"), v8::FunctionTemplate::New(V8JS_MN(require), v8::External::New(c)), v8::ReadOnly);
  231. }
  232. /* }}} */
  233. /*
  234. * Local variables:
  235. * tab-width: 4
  236. * c-basic-offset: 4
  237. * End:
  238. * vim600: noet sw=4 ts=4 fdm=marker
  239. * vim<600: noet sw=4 ts=4
  240. */