Skip to content

Commit

Permalink
Use rb_eArgError directly.
Browse files Browse the repository at this point in the history
It's part of the public API, so little point keeping another
private reference to it.

Additionally when storing an object in a global variable you MUST
register that variable to the gc with `rb_global_variable(&cArgumentError)`
and that *before* you store anything in it, so that the GC knows to
update your global variable if it moves that object.

In practice `ArgumentError` is pinned for now, so it would have been
OK, but it might not be pinned forever.
  • Loading branch information
byroot committed Sep 9, 2024
1 parent 7b9d039 commit 2a76936
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions ext/iou/ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

VALUE mIOU;
VALUE cRing;
VALUE cArgumentError;

VALUE SYM_accept;
VALUE SYM_block;
Expand Down Expand Up @@ -151,15 +150,15 @@ static inline struct io_uring_sqe *get_sqe(IOU_t *iou) {

static inline void get_required_kwargs(VALUE spec, VALUE *values, int argc, ...) {
if (TYPE(spec) != T_HASH)
rb_raise(cArgumentError, "Expected keyword arguments");
rb_raise(rb_eArgError, "Expected keyword arguments");

va_list ptr;
va_start(ptr, argc);
for (int i = 0; i < argc; i++) {
VALUE k = va_arg(ptr, VALUE);
VALUE v = rb_hash_aref(spec, k);
if (NIL_P(v))
rb_raise(cArgumentError, "Missing %"PRIsVALUE" value", k);
rb_raise(rb_eArgError, "Missing %"PRIsVALUE" value", k);
values[i] = v;
}
va_end(ptr);
Expand Down Expand Up @@ -294,13 +293,13 @@ VALUE IOU_prep_cancel(VALUE self, VALUE spec) {
return prep_cancel_id(iou, NUM2UINT(spec));

if (TYPE(spec) != T_HASH)
rb_raise(cArgumentError, "Expected operation id or keyword arguments");
rb_raise(rb_eArgError, "Expected operation id or keyword arguments");

VALUE id = rb_hash_aref(spec, SYM_id);
if (!NIL_P(id))
return prep_cancel_id(iou, NUM2UINT(id));

rb_raise(cArgumentError, "Missing operation id");
rb_raise(rb_eArgError, "Missing operation id");
}

VALUE IOU_prep_close(VALUE self, VALUE spec) {
Expand Down Expand Up @@ -719,8 +718,6 @@ void Init_IOU(void) {
rb_define_method(cRing, "process_completions", IOU_process_completions, -1);
rb_define_method(cRing, "process_completions_loop", IOU_process_completions_loop, 0);

cArgumentError = rb_const_get(rb_cObject, rb_intern("ArgumentError"));

SYM_accept = MAKE_SYM("accept");
SYM_block = MAKE_SYM("block");
SYM_buffer = MAKE_SYM("buffer");
Expand Down

0 comments on commit 2a76936

Please sign in to comment.