Ruby  3.4.0dev (2024-12-06 revision 892c46283a5ea4179500d951c9d4866c0051f27b)
error.h
Go to the documentation of this file.
1 #ifndef RBIMPL_INTERN_ERROR_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_ERROR_H
26 #include "ruby/internal/value.h"
27 #include "ruby/internal/fl_type.h"
28 #include "ruby/backward/2/assume.h"
29 
35 #define UNLIMITED_ARGUMENTS (-1)
36 
37 #define rb_exc_new2 rb_exc_new_cstr
38 #define rb_exc_new3 rb_exc_new_str
41 #define rb_check_arity rb_check_arity
45 
46 /* error.c */
47 
48 
66 VALUE rb_exc_new(VALUE etype, const char *ptr, long len);
67 
78 VALUE rb_exc_new_cstr(VALUE etype, const char *str);
79 
89 VALUE rb_exc_new_str(VALUE etype, VALUE str);
90 
93 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2)
105 void rb_loaderror(const char *fmt, ...);
106 
109 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
120 void rb_loaderror_with_path(VALUE path, const char *fmt, ...);
121 
124 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
134 void rb_name_error(ID name, const char *fmt, ...);
135 
138 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
147 void rb_name_error_str(VALUE name, const char *fmt, ...);
148 
151 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
166 void rb_frozen_error_raise(VALUE recv, const char *fmt, ...);
167 
179 void rb_invalid_str(const char *str, const char *type);
180 
191 void rb_error_frozen(const char *what);
192 
202 void rb_error_frozen_object(VALUE what);
203 
211 void rb_check_frozen(VALUE obj);
212 
223 void rb_check_copyable(VALUE obj, VALUE orig);
224 
238 void rb_error_arity(int argc, int min, int max);
239 
240 void rb_str_modify(VALUE str);
241 
243 
249 #define rb_check_frozen_internal rb_check_frozen
250 
252 static inline void
254 {
255  if (RB_UNLIKELY(RB_OBJ_FROZEN(obj))) {
257  }
258 
259  /* ref: internal CHILLED_STRING_P()
260  This is an implementation detail subject to change. */
261  if (RB_UNLIKELY(RB_TYPE_P(obj, T_STRING) && FL_TEST_RAW(obj, RUBY_FL_USER2 | RUBY_FL_USER3))) { // STR_CHILLED
262  rb_str_modify(obj);
263  }
264 }
265 
266 /* rb_check_frozen() is available as a symbol, but have
267  * the inline version take priority for native consumers. */
268 #define rb_check_frozen rb_check_frozen_inline
269 
283 static inline int
284 rb_check_arity(int argc, int min, int max)
285 {
286  if ((argc < min) || (max != UNLIMITED_ARGUMENTS && argc > max))
287  rb_error_arity(argc, min, max);
288  return argc;
289 }
290 
291 #endif /* RBIMPL_INTERN_ERROR_H */
Defines ASSUME / RB_LIKELY / UNREACHABLE.
#define RB_UNLIKELY(x)
Asserts that the given Boolean expression likely doesn't hold.
Definition: assume.h:50
Tweaking visibility of C variables/functions.
#define RBIMPL_SYMBOL_EXPORT_END()
Counterpart of RBIMPL_SYMBOL_EXPORT_BEGIN.
Definition: dllexport.h:74
#define RBIMPL_SYMBOL_EXPORT_BEGIN()
Shortcut macro equivalent to RUBY_SYMBOL_EXPORT_BEGIN extern "C" {.
Definition: dllexport.h:65
Defines enum ruby_fl_type.
static bool RB_OBJ_FROZEN(VALUE obj)
Checks if an object is frozen.
Definition: fl_type.h:898
@ RUBY_FL_USER3
User-defined flag.
Definition: fl_type.h:331
@ RUBY_FL_USER2
User-defined flag.
Definition: fl_type.h:330
Defines RBIMPL_ATTR_FORMAT.
#define RBIMPL_ATTR_FORMAT(x, y, z)
Wraps (or simulates) __attribute__((format))
Definition: format.h:27
#define T_STRING
Old name of RUBY_T_STRING.
Definition: value_type.h:78
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition: fl_type.h:132
void rb_check_frozen(VALUE obj)
Queries if the passed object is frozen.
Definition: error.c:4062
void rb_name_error(ID name, const char *fmt,...)
Raises an instance of rb_eNameError.
Definition: error.c:2215
void rb_check_copyable(VALUE obj, VALUE orig)
Ensures that the passed object can be initialize_copy relationship.
Definition: error.c:4068
void rb_error_frozen(const char *what)
Identical to rb_frozen_error_raise(), except its raising exception has a message like "can't modify f...
Definition: error.c:3954
void rb_name_error_str(VALUE name, const char *fmt,...)
Identical to rb_name_error(), except it takes a VALUE instead of ID.
Definition: error.c:2230
void rb_frozen_error_raise(VALUE recv, const char *fmt,...)
Raises an instance of rb_eFrozenError.
Definition: error.c:3960
void rb_invalid_str(const char *str, const char *type)
Honestly I don't understand the name, but it raises an instance of rb_eArgError.
Definition: error.c:2654
VALUE rb_exc_new_cstr(VALUE etype, const char *str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
Definition: error.c:1453
VALUE rb_exc_new(VALUE etype, const char *ptr, long len)
Creates an instance of the passed exception class.
Definition: error.c:1446
void rb_error_frozen_object(VALUE what)
Identical to rb_error_frozen(), except it takes arbitrary Ruby object instead of C's string.
Definition: error.c:4000
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition: error.c:1459
void rb_loaderror(const char *fmt,...)
Raises an instance of rb_eLoadError.
Definition: error.c:3655
void rb_loaderror_with_path(VALUE path, const char *fmt,...)
Identical to rb_loaderror(), except it additionally takes which file is unable to load.
Definition: error.c:3667
#define UNLIMITED_ARGUMENTS
This macro is used in conjunction with rb_check_arity().
Definition: error.h:35
static void rb_check_frozen_inline(VALUE obj)
Just another name of rb_check_frozen.
Definition: error.h:253
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition: error.h:284
void rb_str_modify(VALUE str)
Declares that the string is about to be modified.
Definition: string.c:2640
RBIMPL_ATTR_NORETURN() void rb_eof_error(void)
Utility function to raise rb_eEOFError.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
int len
Length of the buffer.
Definition: io.h:8
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
#define RBIMPL_ATTR_NONNULL(list)
Wraps (or simulates) __attribute__((nonnull))
Definition: nonnull.h:27
Defines RBIMPL_ATTR_NORETURN.
Defines VALUE and ID.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:376