v8js_exceptions.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
  36. v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, ctx->context);
  37. v8::String::Utf8Value exception(isolate, try_catch->Exception());
  38. const char *exception_string = ToCString(exception);
  39. v8::Local<v8::Message> tc_message = try_catch->Message();
  40. const char *filename_string, *sourceline_string;
  41. char *message_string;
  42. object_init_ex(return_value, php_ce_v8js_script_exception);
  43. #define PHPV8_EXPROP(type, name, value) \
  44. zend_update_property##type(php_ce_v8js_script_exception, return_value, #name, sizeof(#name) - 1, value);
  45. if (tc_message.IsEmpty()) {
  46. spprintf(&message_string, 0, "%s", exception_string);
  47. }
  48. else
  49. {
  50. v8::String::Utf8Value filename(isolate, tc_message->GetScriptResourceName());
  51. filename_string = ToCString(filename);
  52. PHPV8_EXPROP(_string, JsFileName, filename_string);
  53. v8::MaybeLocal<v8::String> maybe_sourceline = tc_message->GetSourceLine(context);
  54. if (!maybe_sourceline.IsEmpty()) {
  55. v8::String::Utf8Value sourceline(isolate, maybe_sourceline.ToLocalChecked());
  56. sourceline_string = ToCString(sourceline);
  57. PHPV8_EXPROP(_string, JsSourceLine, sourceline_string);
  58. }
  59. v8::Maybe<int> linenum = tc_message->GetLineNumber(context);
  60. if (linenum.IsJust()) {
  61. PHPV8_EXPROP(_long, JsLineNumber, linenum.FromJust());
  62. }
  63. v8::Maybe<int> start_col = tc_message->GetStartColumn(context);
  64. if (start_col.IsJust()) {
  65. PHPV8_EXPROP(_long, JsStartColumn, start_col.FromJust());
  66. }
  67. v8::Maybe<int> end_col = tc_message->GetEndColumn(context);
  68. if (end_col.IsJust()) {
  69. PHPV8_EXPROP(_long, JsEndColumn, end_col.FromJust());
  70. }
  71. spprintf(&message_string, 0, "%s:%d: %s", filename_string, linenum.FromMaybe(0), exception_string);
  72. v8::MaybeLocal<v8::Value> maybe_stacktrace = try_catch->StackTrace(context);
  73. if (!maybe_stacktrace.IsEmpty()) {
  74. v8::String::Utf8Value stacktrace(isolate, maybe_stacktrace.ToLocalChecked());
  75. PHPV8_EXPROP(_string, JsTrace, ToCString(stacktrace));
  76. }
  77. if(try_catch->Exception()->IsObject() && try_catch->Exception()->ToObject(isolate)->InternalFieldCount() == 2) {
  78. zend_object *php_exception = reinterpret_cast<zend_object *>(try_catch->Exception()->ToObject(isolate)->GetAlignedPointerFromInternalField(1));
  79. zend_class_entry *exception_ce = zend_exception_get_default();
  80. if (instanceof_function(php_exception->ce, exception_ce)) {
  81. #ifdef GC_ADDREF
  82. GC_ADDREF(php_exception);
  83. #else
  84. ++GC_REFCOUNT(php_exception);
  85. #endif
  86. zend_exception_set_previous(Z_OBJ_P(return_value), php_exception);
  87. }
  88. }
  89. }
  90. PHPV8_EXPROP(_string, message, message_string);
  91. efree(message_string);
  92. }
  93. /* }}} */
  94. void v8js_throw_script_exception(v8::Isolate *isolate, v8::TryCatch *try_catch) /* {{{ */
  95. {
  96. v8::String::Utf8Value exception(isolate, try_catch->Exception());
  97. const char *exception_string = ToCString(exception);
  98. zval zexception;
  99. if (try_catch->Message().IsEmpty()) {
  100. zend_throw_exception(php_ce_v8js_script_exception, (char *) exception_string, 0);
  101. } else {
  102. v8js_create_script_exception(&zexception, isolate, try_catch);
  103. zend_throw_exception_object(&zexception);
  104. }
  105. }
  106. /* }}} */
  107. #define V8JS_EXCEPTION_METHOD(property) \
  108. static PHP_METHOD(V8JsScriptException, get##property) \
  109. { \
  110. zval *value, rv; \
  111. \
  112. if (zend_parse_parameters_none() == FAILURE) { \
  113. return; \
  114. } \
  115. value = zend_read_property(php_ce_v8js_script_exception, getThis(), #property, sizeof(#property) - 1, 0, &rv); \
  116. RETURN_ZVAL(value, 1, 0); \
  117. }
  118. /* {{{ proto string V8JsEScriptxception::getJsFileName()
  119. */
  120. V8JS_EXCEPTION_METHOD(JsFileName);
  121. /* }}} */
  122. /* {{{ proto string V8JsScriptException::getJsLineNumber()
  123. */
  124. V8JS_EXCEPTION_METHOD(JsLineNumber);
  125. /* }}} */
  126. /* {{{ proto string V8JsScriptException::getJsStartColumn()
  127. */
  128. V8JS_EXCEPTION_METHOD(JsStartColumn);
  129. /* }}} */
  130. /* {{{ proto string V8JsScriptException::getJsEndColumn()
  131. */
  132. V8JS_EXCEPTION_METHOD(JsEndColumn);
  133. /* }}} */
  134. /* {{{ proto string V8JsScriptException::getJsSourceLine()
  135. */
  136. V8JS_EXCEPTION_METHOD(JsSourceLine);
  137. /* }}} */
  138. /* {{{ proto string V8JsScriptException::getJsTrace()
  139. */
  140. V8JS_EXCEPTION_METHOD(JsTrace);
  141. /* }}} */
  142. ZEND_BEGIN_ARG_INFO(arginfo_v8jsscriptexception_no_args, 0)
  143. ZEND_END_ARG_INFO()
  144. static const zend_function_entry v8js_script_exception_methods[] = { /* {{{ */
  145. PHP_ME(V8JsScriptException, getJsFileName, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  146. PHP_ME(V8JsScriptException, getJsLineNumber, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  147. PHP_ME(V8JsScriptException, getJsStartColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  148. PHP_ME(V8JsScriptException, getJsEndColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  149. PHP_ME(V8JsScriptException, getJsSourceLine, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  150. PHP_ME(V8JsScriptException, getJsTrace, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  151. {NULL, NULL, NULL}
  152. };
  153. /* }}} */
  154. /* }}} V8JsScriptException */
  155. /* {{{ Class: V8JsException */
  156. static const zend_function_entry v8js_exception_methods[] = { /* {{{ */
  157. {NULL, NULL, NULL}
  158. };
  159. /* }}} */
  160. /* }}} V8JsException */
  161. /* {{{ Class: V8JsTimeLimitException */
  162. static const zend_function_entry v8js_time_limit_exception_methods[] = { /* {{{ */
  163. {NULL, NULL, NULL}
  164. };
  165. /* }}} */
  166. /* }}} V8JsTimeLimitException */
  167. /* {{{ Class: V8JsMemoryLimitException */
  168. static const zend_function_entry v8js_memory_limit_exception_methods[] = { /* {{{ */
  169. {NULL, NULL, NULL}
  170. };
  171. /* }}} */
  172. /* }}} V8JsMemoryLimitException */
  173. PHP_MINIT_FUNCTION(v8js_exceptions) /* {{{ */
  174. {
  175. zend_class_entry ce;
  176. /* V8JsException Class */
  177. INIT_CLASS_ENTRY(ce, "V8JsException", v8js_exception_methods);
  178. php_ce_v8js_exception = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException);
  179. /* V8JsScriptException Class */
  180. INIT_CLASS_ENTRY(ce, "V8JsScriptException", v8js_script_exception_methods);
  181. php_ce_v8js_script_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  182. php_ce_v8js_script_exception->ce_flags |= ZEND_ACC_FINAL;
  183. /* Add custom JS specific properties */
  184. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED);
  185. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED);
  186. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsStartColumn"), ZEND_ACC_PROTECTED);
  187. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsEndColumn"), ZEND_ACC_PROTECTED);
  188. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED);
  189. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED);
  190. /* V8JsTimeLimitException Class */
  191. INIT_CLASS_ENTRY(ce, "V8JsTimeLimitException", v8js_time_limit_exception_methods);
  192. php_ce_v8js_time_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  193. php_ce_v8js_time_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  194. /* V8JsMemoryLimitException Class */
  195. INIT_CLASS_ENTRY(ce, "V8JsMemoryLimitException", v8js_memory_limit_exception_methods);
  196. php_ce_v8js_memory_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  197. php_ce_v8js_memory_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  198. return SUCCESS;
  199. } /* }}} */
  200. /*
  201. * Local variables:
  202. * tab-width: 4
  203. * c-basic-offset: 4
  204. * indent-tabs-mode: t
  205. * End:
  206. * vim600: noet sw=4 ts=4 fdm=marker
  207. * vim<600: noet sw=4 ts=4
  208. */