v8js_exceptions.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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, Z_OBJ_P(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. v8::Local<v8::Object> error_object;
  78. if(try_catch->Exception()->IsObject() && try_catch->Exception()->ToObject(context).ToLocal(&error_object) && error_object->InternalFieldCount() == 2) {
  79. zend_object *php_exception = reinterpret_cast<zend_object *>(error_object->GetAlignedPointerFromInternalField(1));
  80. zend_class_entry *exception_ce = zend_exception_get_default();
  81. if (instanceof_function(php_exception->ce, exception_ce)) {
  82. #ifdef GC_ADDREF
  83. GC_ADDREF(php_exception);
  84. #else
  85. ++GC_REFCOUNT(php_exception);
  86. #endif
  87. zend_exception_set_previous(Z_OBJ_P(return_value), php_exception);
  88. }
  89. }
  90. }
  91. PHPV8_EXPROP(_string, message, message_string);
  92. efree(message_string);
  93. }
  94. /* }}} */
  95. void v8js_throw_script_exception(v8::Isolate *isolate, v8::TryCatch *try_catch) /* {{{ */
  96. {
  97. v8::String::Utf8Value exception(isolate, try_catch->Exception());
  98. const char *exception_string = ToCString(exception);
  99. zval zexception;
  100. if (try_catch->Message().IsEmpty()) {
  101. zend_throw_exception(php_ce_v8js_script_exception, (char *) exception_string, 0);
  102. } else {
  103. v8js_create_script_exception(&zexception, isolate, try_catch);
  104. zend_throw_exception_object(&zexception);
  105. }
  106. }
  107. /* }}} */
  108. #define V8JS_EXCEPTION_METHOD(property) \
  109. static PHP_METHOD(V8JsScriptException, get##property) \
  110. { \
  111. zval *value, rv; \
  112. \
  113. if (zend_parse_parameters_none() == FAILURE) { \
  114. return; \
  115. } \
  116. value = zend_read_property(php_ce_v8js_script_exception, Z_OBJ_P(getThis()), #property, sizeof(#property) - 1, 0, &rv); \
  117. RETURN_ZVAL(value, 1, 0); \
  118. }
  119. /* {{{ proto string V8JsEScriptxception::getJsFileName()
  120. */
  121. V8JS_EXCEPTION_METHOD(JsFileName);
  122. /* }}} */
  123. /* {{{ proto string V8JsScriptException::getJsLineNumber()
  124. */
  125. V8JS_EXCEPTION_METHOD(JsLineNumber);
  126. /* }}} */
  127. /* {{{ proto string V8JsScriptException::getJsStartColumn()
  128. */
  129. V8JS_EXCEPTION_METHOD(JsStartColumn);
  130. /* }}} */
  131. /* {{{ proto string V8JsScriptException::getJsEndColumn()
  132. */
  133. V8JS_EXCEPTION_METHOD(JsEndColumn);
  134. /* }}} */
  135. /* {{{ proto string V8JsScriptException::getJsSourceLine()
  136. */
  137. V8JS_EXCEPTION_METHOD(JsSourceLine);
  138. /* }}} */
  139. /* {{{ proto string V8JsScriptException::getJsTrace()
  140. */
  141. V8JS_EXCEPTION_METHOD(JsTrace);
  142. /* }}} */
  143. ZEND_BEGIN_ARG_INFO(arginfo_v8jsscriptexception_no_args, 0)
  144. ZEND_END_ARG_INFO()
  145. static const zend_function_entry v8js_script_exception_methods[] = { /* {{{ */
  146. PHP_ME(V8JsScriptException, getJsFileName, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  147. PHP_ME(V8JsScriptException, getJsLineNumber, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  148. PHP_ME(V8JsScriptException, getJsStartColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  149. PHP_ME(V8JsScriptException, getJsEndColumn, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  150. PHP_ME(V8JsScriptException, getJsSourceLine, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  151. PHP_ME(V8JsScriptException, getJsTrace, arginfo_v8jsscriptexception_no_args, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
  152. {NULL, NULL, NULL}
  153. };
  154. /* }}} */
  155. /* }}} V8JsScriptException */
  156. /* {{{ Class: V8JsException */
  157. static const zend_function_entry v8js_exception_methods[] = { /* {{{ */
  158. {NULL, NULL, NULL}
  159. };
  160. /* }}} */
  161. /* }}} V8JsException */
  162. /* {{{ Class: V8JsTimeLimitException */
  163. static const zend_function_entry v8js_time_limit_exception_methods[] = { /* {{{ */
  164. {NULL, NULL, NULL}
  165. };
  166. /* }}} */
  167. /* }}} V8JsTimeLimitException */
  168. /* {{{ Class: V8JsMemoryLimitException */
  169. static const zend_function_entry v8js_memory_limit_exception_methods[] = { /* {{{ */
  170. {NULL, NULL, NULL}
  171. };
  172. /* }}} */
  173. /* }}} V8JsMemoryLimitException */
  174. PHP_MINIT_FUNCTION(v8js_exceptions) /* {{{ */
  175. {
  176. zend_class_entry ce;
  177. /* V8JsException Class */
  178. INIT_CLASS_ENTRY(ce, "V8JsException", v8js_exception_methods);
  179. php_ce_v8js_exception = zend_register_internal_class_ex(&ce, spl_ce_RuntimeException);
  180. /* V8JsScriptException Class */
  181. INIT_CLASS_ENTRY(ce, "V8JsScriptException", v8js_script_exception_methods);
  182. php_ce_v8js_script_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  183. php_ce_v8js_script_exception->ce_flags |= ZEND_ACC_FINAL;
  184. /* Add custom JS specific properties */
  185. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsFileName"), ZEND_ACC_PROTECTED);
  186. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsLineNumber"), ZEND_ACC_PROTECTED);
  187. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsStartColumn"), ZEND_ACC_PROTECTED);
  188. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsEndColumn"), ZEND_ACC_PROTECTED);
  189. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsSourceLine"), ZEND_ACC_PROTECTED);
  190. zend_declare_property_null(php_ce_v8js_script_exception, ZEND_STRL("JsTrace"), ZEND_ACC_PROTECTED);
  191. /* V8JsTimeLimitException Class */
  192. INIT_CLASS_ENTRY(ce, "V8JsTimeLimitException", v8js_time_limit_exception_methods);
  193. php_ce_v8js_time_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  194. php_ce_v8js_time_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  195. /* V8JsMemoryLimitException Class */
  196. INIT_CLASS_ENTRY(ce, "V8JsMemoryLimitException", v8js_memory_limit_exception_methods);
  197. php_ce_v8js_memory_limit_exception = zend_register_internal_class_ex(&ce, php_ce_v8js_exception);
  198. php_ce_v8js_memory_limit_exception->ce_flags |= ZEND_ACC_FINAL;
  199. return SUCCESS;
  200. } /* }}} */
  201. /*
  202. * Local variables:
  203. * tab-width: 4
  204. * c-basic-offset: 4
  205. * indent-tabs-mode: t
  206. * End:
  207. * vim600: noet sw=4 ts=4 fdm=marker
  208. * vim<600: noet sw=4 ts=4
  209. */