#ifndef ERRORS_H #define ERRORS_H #include "types.h" #include #include typedef struct sv_func sv_func_t; #define ERR_FMT "\x1b[31m%.*s\x1b[0m: \x1b[1m%.*s\x1b[0m" #define ERR_NAME_ONLY "\x1b[31m%.*s\x1b[0m" typedef enum { JS_ERR_GENERIC = 0, JS_ERR_TYPE, JS_ERR_SYNTAX, JS_ERR_REFERENCE, JS_ERR_RANGE, JS_ERR_EVAL, JS_ERR_URI, JS_ERR_INTERNAL, JS_ERR_AGGREGATE, JS_ERR_TYPE_COUNT } js_err_type_t; static constexpr int JS_ERR_NO_STACK = 1 << 8; static constexpr int JS_ERR_NODE_CODE = 1 << 9; static constexpr int JS_ERR_FLAG_MASK = JS_ERR_NO_STACK | JS_ERR_NODE_CODE; typedef struct { const char *src; sv_func_t *func; const char *filename; ant_offset_t src_len; ant_offset_t off; ant_offset_t span_len; uint32_t line; uint32_t col; bool valid; } js_error_site_t; js_err_type_t get_error_type(ant_t *js); const char *js_err_type_name(js_err_type_t err_type); ant_value_t js_mkerr_code(ant_t *js, js_err_type_t type, const char *code, const char *message); void js_describe_received(ant_t *js, ant_value_t value, char *out, size_t out_len); ant_value_t Ant_Error_CreateStackOverflow(ant_t *js); ant_value_t Ant_Error_CreateInvalidArgType(ant_t *js, const char *name, const char *expected, ant_value_t received); ant_value_t Ant_Error_CreateInvalidArgValue(ant_t *js, const char *name, const char *expected, ant_value_t received); ant_value_t Ant_Error_CheckNoNullBytes(ant_t *js, const char *name, ant_value_t value); ant_value_t Ant_Error_CreateOutOfRange(ant_t *js, const char *name, const char *range, ant_value_t received); bool print_uncaught_throw(ant_t *js); bool print_unhandled_promise_rejection(ant_t *js, ant_value_t value); void print_error_value(ant_t *js, ant_value_t value, ant_value_t fallback_stack, const char *prefix); char *js_error_render_pretty(ant_t *js, ant_value_t err, ant_value_t stack, size_t *out_len, size_t *header_at); bool js_mark_errorlike_no_stack(ant_t *js, ant_value_t value); void js_print_stack_trace_vm(ant_t *js, FILE *stream); void js_capture_stack(ant_t *js, ant_value_t err_obj); void js_error_define_stack(ant_t *js, ant_value_t obj, ant_value_t record, ant_value_t text); ant_value_t js_error_header_text(ant_t *js, ant_value_t obj); ant_value_t js_error_display_name(ant_t *js, ant_value_t obj, ant_value_t name); ant_value_t js_error_stack_value(ant_t *js, ant_value_t obj); ant_value_t js_error_header_parts(ant_t *js, ant_value_t obj, ant_value_t *name, ant_value_t *message); js_error_site_t js_error_site_from_bc(sv_func_t *func, int bc_offset); js_error_site_t js_error_site_from_frame(sv_func_t *func, int bc_offset); js_error_site_t js_error_site_from_vm_top(ant_t *js); void js_get_call_location( ant_t *js, const char **out_filename, int *out_line, int *out_col ); js_error_site_t js_error_site_from_source( const char *src, ant_offset_t src_len, const char *filename, ant_offset_t off, ant_offset_t span_len ); __attribute__((format(printf, 5, 6))) ant_value_t js_create_error_at( ant_t *js, const js_error_site_t *site, js_err_type_t err_type, ant_value_t props, const char *fmt, ... ); __attribute__((format(printf, 4, 5))) ant_value_t js_create_error(ant_t *js, js_err_type_t err_type, ant_value_t props, const char *fmt, ...); ant_value_t Ant_Error_Create(ant_t *js, js_err_type_t err_type, const char *message); ant_value_t Ant_Error_CreateWithProps(ant_t *js, js_err_type_t err_type, ant_value_t props, const char *message); ant_value_t Ant_Error_CreateConstructor(ant_t *js, const char *name, ant_value_t *out_proto); ant_value_t Ant_Error_CreateConstructorEx(ant_t *js, const char *name, ant_value_t cfunc, ant_value_t *out_proto); __attribute__((format(printf, 3, 4))) ant_value_t Ant_Error_CreateFormatted(ant_t *js, js_err_type_t err_type, const char *fmt, ...); ant_value_t js_capture_raw_stack(ant_t *js); ant_value_t js_build_callsite_array(ant_t *js); ant_value_t js_throw(ant_t *js, ant_value_t value); ant_value_t js_take_thrown(ant_t *js, ant_value_t fallback); bool Ant_Exception_Pending(ant_t *js); ant_value_t Ant_Exception_Peek(ant_t *js); ant_value_t Ant_Exception_Current(ant_t *js); void Ant_Exception_Set(ant_t *js, ant_value_t completion); void Ant_Exception_Clear(ant_t *js); ant_value_t Ant_Exception_Value(ant_t *js, ant_value_t completion); ant_value_t Ant_Exception_Stack(ant_t *js, ant_value_t completion); ant_value_t Ant_Exception_Raise(ant_t *js, ant_value_t value, ant_value_t stack); ant_value_t Ant_Exception_CreateRecord(ant_t *js, ant_value_t value, ant_value_t stack); ant_value_t Ant_Error_ConsumeMarker(ant_t *js, ant_value_t value); ant_value_t Ant_Error_CallCallback( ant_t *js, ant_value_t callback, ant_value_t this_value, ant_value_t *args, int nargs ); #define js_mkerr(js, ...) js_create_error(js, JS_ERR_TYPE, js_mkundef(), __VA_ARGS__) #define js_mkerr_typed(js, err_type, ...) js_create_error(js, err_type, js_mkundef(), __VA_ARGS__) #define js_mkerr_props(js, err_type, props, ...) js_create_error(js, err_type, props, __VA_ARGS__) #define js_mkerr_at(site, js, ...) js_create_error_at(js, site, JS_ERR_TYPE, js_mkundef(), __VA_ARGS__) #define js_mkerr_typed_at(site, js, err_type, ...) js_create_error_at(js, site, err_type, js_mkundef(), __VA_ARGS__) #endif