v8js_exceptions.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #endif
  16. extern "C" {
  17. #include "php.h"
  18. #include "ext/date/php_date.h"
  19. #include "ext/standard/php_string.h"
  20. #include "zend_interfaces.h"
  21. #include "zend_closures.h"
  22. #include "ext/spl/spl_exceptions.h"
  23. #include "zend_exceptions.h"
  24. }
  25. #include "php_v8js_macros.h"
  26. /* {{{ Class Entries */
  27. zend_class_entry *php_ce_v8js_exception;
  28. zend_class_entry *php_ce_v8js_script_exception;
  29. zend_class_entry *php_ce_v8js_time_limit_exception;
  30. zend_class_entry *php_ce_v8js_memory_limit_exception;
  31. /* }}} */
  32. /* {{{ Class: V8JsScriptException */
  33. void v8js_create_script_exception(zval *return_value, v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  34. {
  35. v8::String::Utf8Value exception(try_catch->Exception());
  36. const char *exception_string = ToCString(exception);
  37. v8::Handle<v8::Message> tc_message = try_catch->Message();
  38. const char *filename_string, *sourceline_string;
  39. char *message_string;
  40. int linenum, start_col, end_col, message_len;
  41. object_init_ex(return_value, php_ce_v8js_script_exception);
  42. #define PHPV8_EXPROP(type, name, value) \
  43. zend_update_property##type(php_ce_v8js_script_exception, return_value, #name, sizeof(#name) - 1, value TSRMLS_CC);
  44. if (tc_message.IsEmpty()) {
  45. message_len = spprintf(&message_string, 0, "%s", exception_string);
  46. }
  47. else
  48. {
  49. v8::String::Utf8Value filename(tc_message->GetScriptResourceName());
  50. filename_string = ToCString(filename);
  51. PHPV8_EXPROP(_string, JsFileName, filename_string);
  52. v8::String::Utf8Value sourceline(tc_message->GetSourceLine());
  53. sourceline_string = ToCString(sourceline);
  54. PHPV8_EXPROP(_string, JsSourceLine, sourceline_string);
  55. linenum = tc_message->GetLineNumber();
  56. PHPV8_EXPROP(_long, JsLineNumber, linenum);
  57. start_col = tc_message->GetStartColumn();
  58. PHPV8_EXPROP(_long, JsStartColumn, start_col);
  59. end_col = tc_message->GetEndColumn();
  60. PHPV8_EXPROP(_long, JsEndColumn, end_col);
  61. message_len = spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
  62. v8::String::Utf8Value stacktrace(try_catch->StackTrace());
  63. if (stacktrace.length() > 0) {
  64. const char* stacktrace_string = ToCString(stacktrace);
  65. PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
  66. }
  67. }
  68. PHPV8_EXPROP(_string, message, message_string);
  69. efree(message_string);
  70. }
  71. /* }}} */
  72. void v8js_throw_script_exception(v8::TryCatch *try_catch TSRMLS_DC) /* {{{ */
  73. {
  74. v8::String::Utf8Value exception(try_catch->Exception());
  75. const char *exception_string = ToCString(exception);
  76. zval *zexception = NULL;
  77. if (try_catch->Message().IsEmpty()) {
  78. zend_throw_exception(php_ce_v8js_script_exception, (char *) exception_string, 0 TSRMLS_CC);
  79. } else {
  80. MAKE_STD_ZVAL(zexception);
  81. v8js_create_script_exception(zexception, try_catch TSRMLS_CC);
  82. zend_throw_exception_object(zexception TSRMLS_CC);
  83. }
  84. }
  85. /* }}} */
  86. #define V8JS_EXCEPTION_METHOD(property) \
  87. static PHP_METHOD(V8JsScriptException, get##property) \
  88. { \
  89. zval *value; \
  90. \
  91. if (zend_parse_parameters_none() == FAILURE) { \
  92. return; \
  93. } \
  94. value = zend_read_property(php_ce_v8js_script_exception, getThis(), #property, sizeof(#property) - 1, 0 TSRMLS_CC); \
  95. *return_value = *value; \
  96. zval_copy_ctor(return_value); \
  97. INIT_PZVAL(return_value); \
  98. }
  99. /* {{{ proto string V8JsEScriptxception::getJsFileName()
  100. */
  101. V8JS_EXCEPTION_METHOD(JsFileName);
  102. /* }}} */
  103. /* {{{ proto string V8JsScriptException::getJsLineNumber()
  104. */
  105. V8JS_EXCEPTION_METHOD(JsLineNumber);
  106. /* }}} */
  107. /* {{{ proto string V8JsScriptException::getJsStartColumn()
  108. */
  109. V8JS_EXCEPTION_METHOD(JsStartColumn);
  110. /* }}} */
  111. /* {{{ proto string V8JsScriptException::getJsEndColumn()
  112. */
  113. V8JS_EXCEPTION_METHOD(JsEndColumn);
  114. /* }}} */
  115. /* {{{ proto string V8JsScriptException::getJsSourceLine()
  116. */
  117. V8JS_EXCEPTION_METHOD(JsSourceLine);
  118. /* }}} */
  119. /* {{{ proto string V8JsScriptException::getJsTrace()
  120. */
  121. V8JS_EXCEPTION_METHOD(JsTrace);
  122. /* }}} */
  123. ZEND_BEGIN_ARG_INFO(arginfo_v8jsscriptexception_no_args, 0)
  124. ZEND_END_ARG_INFO()
  125. static const zend_function_entry v8js_script_exception_methods[] = { /* {{{ */
  126. PHP_ME(V8JsScriptException, getJsFileName, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  127. PHP_ME(V8JsScriptException, getJsLineNumber, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  128. PHP_ME(V8JsScriptException, getJsStartColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  129. PHP_ME(V8JsScriptException, getJsEndColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  130. PHP_ME(V8JsScriptException, getJsSourceLine, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  131. PHP_ME(V8JsScriptException, getJsTrace, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  132. {NULL, NULL, NULL}
  133. };
  134. /* }}} */
  135. /* }}} V8JsScriptException */
  136. /* {{{ Class: V8JsException */
  137. static const zend_function_entry v8js_exception_methods[] = { /* {{{ */
  138. {NULL, NULL, NULL}
  139. };
  140. /* }}} */
  141. /* }}} V8JsException */
  142. /* {{{ Class: V8JsTimeLimitException */
  143. static const zend_function_entry v8js_time_limit_exception_methods[] = { /* {{{ */
  144. {NULL, NULL, NULL}
  145. };
  146. /* }}} */
  147. /* }}} V8JsTimeLimitException */
  148. /* {{{ Class: V8JsMemoryLimitException */
  149. static const zend_function_entry v8js_memory_limit_exception_methods[] = { /* {{{ */
  150. {NULL, NULL, NULL}
  151. };
  152. /* }}} */
  153. /* }}} V8JsMemoryLimitException */
  154. PHP_MINIT_FUNCTION(v8js_exceptions) /* {{{ */
  155. {
  156. zend_class_entry ce;
  157. /* V8JsException Class */
  158. INIT_CLASS_ENTRY(ce, "V8JsException", v8js_exception_methods);
  159. php_ce_v8js_exception = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException, NULL TSRMLS_CC);
  160. /* V8JsScriptException Class */
  161. INIT_CLASS_ENTRY(ce, "V8JsScriptException", v8js_script_exception_methods);
  162. php_ce_v8js_script_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception, NULL TSRMLS_CC);
  163. php_ce_v8js_script_exception->ce_flags |= ZEND_ACC_FINAL;
  164. /* Add custom JS specific properties */
  165. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED TSRMLS_CC);
  166. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED TSRMLS_CC);
  167. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsStartColumn"), ZEND_ACC_PROTECTED TSRMLS_CC);
  168. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsEndColumn"), ZEND_ACC_PROTECTED TSRMLS_CC);
  169. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED TSRMLS_CC);
  170. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED TSRMLS_CC);
  171. /* V8JsTimeLimitException Class */
  172. INIT_CLASS_ENTRY(ce, "V8JsTimeLimitException", v8js_time_limit_exception_methods);
  173. php_ce_v8js_time_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception, NULL TSRMLS_CC);
  174. php_ce_v8js_time_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  175. /* V8JsMemoryLimitException Class */
  176. INIT_CLASS_ENTRY(ce, "V8JsMemoryLimitException", v8js_memory_limit_exception_methods);
  177. php_ce_v8js_memory_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception, NULL TSRMLS_CC);
  178. php_ce_v8js_memory_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  179. } /* }}} */
  180. /*
  181. * Local variables:
  182. * tab-width: 4
  183. * c-basic-offset: 4
  184. * End:
  185. * vim600: noet sw=4 ts=4 fdm=marker
  186. * vim<600: noet sw=4 ts=4
  187. */