v8js_exceptions.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 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. | Author: Stefan Siegl <[email protected]> |
  12. +----------------------------------------------------------------------+
  13. */
  14. #ifdef HAVE_CONFIG_H
  15. #include "config.h"
  16. #endif
  17. #include "php_v8js_macros.h"
  18. extern "C" {
  19. #include "ext/date/php_date.h"
  20. #include "ext/standard/php_string.h"
  21. #include "zend_interfaces.h"
  22. #include "zend_closures.h"
  23. #include "ext/spl/spl_exceptions.h"
  24. #include "zend_exceptions.h"
  25. }
  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::Isolate *isolate, v8::TryCatch *try_catch) /* {{{ */
  34. {
  35. v8::String::Utf8Value exception(try_catch->Exception());
  36. const char *exception_string = ToCString(exception);
  37. v8::Local<v8::Message> tc_message = try_catch->Message();
  38. const char *filename_string, *sourceline_string;
  39. char *message_string;
  40. int linenum, start_col;
  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);
  44. if (tc_message.IsEmpty()) {
  45. 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. v8::Maybe<int> end_col = tc_message->GetEndColumn(isolate->GetEnteredContext());
  60. if (end_col.IsJust()) {
  61. PHPV8_EXPROP(_long, JsEndColumn, end_col.FromJust());
  62. }
  63. spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum, exception_string);
  64. v8::String::Utf8Value stacktrace(try_catch->StackTrace());
  65. if (stacktrace.length() > 0) {
  66. const char* stacktrace_string = ToCString(stacktrace);
  67. PHPV8_EXPROP(_string, JsTrace, stacktrace_string);
  68. }
  69. if(try_catch->Exception()->IsObject() && try_catch->Exception()->ToObject()->InternalFieldCount() == 2) {
  70. zend_object *php_exception = reinterpret_cast<zend_object *>(try_catch->Exception()->ToObject()->GetAlignedPointerFromInternalField(1));
  71. zend_class_entry *exception_ce = zend_exception_get_default();
  72. if (instanceof_function(php_exception->ce, exception_ce)) {
  73. ++GC_REFCOUNT(php_exception);
  74. zend_exception_set_previous(Z_OBJ_P(return_value), php_exception);
  75. }
  76. }
  77. }
  78. PHPV8_EXPROP(_string, message, message_string);
  79. efree(message_string);
  80. }
  81. /* }}} */
  82. void v8js_throw_script_exception(v8::Isolate *isolate, v8::TryCatch *try_catch) /* {{{ */
  83. {
  84. v8::String::Utf8Value exception(try_catch->Exception());
  85. const char *exception_string = ToCString(exception);
  86. zval zexception;
  87. if (try_catch->Message().IsEmpty()) {
  88. zend_throw_exception(php_ce_v8js_script_exception, (char *) exception_string, 0);
  89. } else {
  90. v8js_create_script_exception(&zexception, isolate, try_catch);
  91. zend_throw_exception_object(&zexception);
  92. }
  93. }
  94. /* }}} */
  95. #define V8JS_EXCEPTION_METHOD(property) \
  96. static PHP_METHOD(V8JsScriptException, get##property) \
  97. { \
  98. zval *value, rv; \
  99. \
  100. if (zend_parse_parameters_none() == FAILURE) { \
  101. return; \
  102. } \
  103. value = zend_read_property(php_ce_v8js_script_exception, getThis(), #property, sizeof(#property) - 1, 0, &rv); \
  104. RETURN_ZVAL(value, 1, 0); \
  105. }
  106. /* {{{ proto string V8JsEScriptxception::getJsFileName()
  107. */
  108. V8JS_EXCEPTION_METHOD(JsFileName);
  109. /* }}} */
  110. /* {{{ proto string V8JsScriptException::getJsLineNumber()
  111. */
  112. V8JS_EXCEPTION_METHOD(JsLineNumber);
  113. /* }}} */
  114. /* {{{ proto string V8JsScriptException::getJsStartColumn()
  115. */
  116. V8JS_EXCEPTION_METHOD(JsStartColumn);
  117. /* }}} */
  118. /* {{{ proto string V8JsScriptException::getJsEndColumn()
  119. */
  120. V8JS_EXCEPTION_METHOD(JsEndColumn);
  121. /* }}} */
  122. /* {{{ proto string V8JsScriptException::getJsSourceLine()
  123. */
  124. V8JS_EXCEPTION_METHOD(JsSourceLine);
  125. /* }}} */
  126. /* {{{ proto string V8JsScriptException::getJsTrace()
  127. */
  128. V8JS_EXCEPTION_METHOD(JsTrace);
  129. /* }}} */
  130. ZEND_BEGIN_ARG_INFO(arginfo_v8jsscriptexception_no_args, 0)
  131. ZEND_END_ARG_INFO()
  132. static const zend_function_entry v8js_script_exception_methods[] = { /* {{{ */
  133. PHP_ME(V8JsScriptException, getJsFileName, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  134. PHP_ME(V8JsScriptException, getJsLineNumber, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  135. PHP_ME(V8JsScriptException, getJsStartColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  136. PHP_ME(V8JsScriptException, getJsEndColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  137. PHP_ME(V8JsScriptException, getJsSourceLine, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  138. PHP_ME(V8JsScriptException, getJsTrace, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  139. {NULL, NULL, NULL}
  140. };
  141. /* }}} */
  142. /* }}} V8JsScriptException */
  143. /* {{{ Class: V8JsException */
  144. static const zend_function_entry v8js_exception_methods[] = { /* {{{ */
  145. {NULL, NULL, NULL}
  146. };
  147. /* }}} */
  148. /* }}} V8JsException */
  149. /* {{{ Class: V8JsTimeLimitException */
  150. static const zend_function_entry v8js_time_limit_exception_methods[] = { /* {{{ */
  151. {NULL, NULL, NULL}
  152. };
  153. /* }}} */
  154. /* }}} V8JsTimeLimitException */
  155. /* {{{ Class: V8JsMemoryLimitException */
  156. static const zend_function_entry v8js_memory_limit_exception_methods[] = { /* {{{ */
  157. {NULL, NULL, NULL}
  158. };
  159. /* }}} */
  160. /* }}} V8JsMemoryLimitException */
  161. PHP_MINIT_FUNCTION(v8js_exceptions) /* {{{ */
  162. {
  163. zend_class_entry ce;
  164. /* V8JsException Class */
  165. INIT_CLASS_ENTRY(ce, "V8JsException", v8js_exception_methods);
  166. php_ce_v8js_exception = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException);
  167. /* V8JsScriptException Class */
  168. INIT_CLASS_ENTRY(ce, "V8JsScriptException", v8js_script_exception_methods);
  169. php_ce_v8js_script_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  170. php_ce_v8js_script_exception->ce_flags |= ZEND_ACC_FINAL;
  171. /* Add custom JS specific properties */
  172. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED);
  173. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED);
  174. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsStartColumn"), ZEND_ACC_PROTECTED);
  175. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsEndColumn"), ZEND_ACC_PROTECTED);
  176. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED);
  177. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED);
  178. /* V8JsTimeLimitException Class */
  179. INIT_CLASS_ENTRY(ce, "V8JsTimeLimitException", v8js_time_limit_exception_methods);
  180. php_ce_v8js_time_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  181. php_ce_v8js_time_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  182. /* V8JsMemoryLimitException Class */
  183. INIT_CLASS_ENTRY(ce, "V8JsMemoryLimitException", v8js_memory_limit_exception_methods);
  184. php_ce_v8js_memory_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  185. php_ce_v8js_memory_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  186. return SUCCESS;
  187. } /* }}} */
  188. /*
  189. * Local variables:
  190. * tab-width: 4
  191. * c-basic-offset: 4
  192. * indent-tabs-mode: t
  193. * End:
  194. * vim600: noet sw=4 ts=4 fdm=marker
  195. * vim<600: noet sw=4 ts=4
  196. */