Skip to content

Commit 1879477

Browse files
committed
Move exception format logic to Ruby.
1 parent 2134cb1 commit 1879477

File tree

3 files changed

+18
-25
lines changed

3 files changed

+18
-25
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,10 @@ def rb_check_arity(arg_count, min, max)
11421142
Truffle::Type.check_arity arg_count, min, max
11431143
end
11441144

1145+
def rb_arity_error_string(arg_count, min, max)
1146+
Truffle::Type.arity_error_string(arg_count, min, max)
1147+
end
1148+
11451149
def rb_raise(object, name)
11461150
raise 'not implemented'
11471151
end

src/main/c/cext/ruby.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3468,23 +3468,9 @@ VALUE rb_enum_values_pack(int argc, const VALUE *argv) {
34683468
rb_tr_error("rb_enum_values_pack not implemented");
34693469
}
34703470

3471-
static inline VALUE rb_arity_error_new(int argc, int min, int max) {
3472-
VALUE err_mess = 0;
3473-
if (min == max) {
3474-
err_mess = rb_sprintf("wrong number of arguments (given %d, expected %d)", argc, min);
3475-
}
3476-
else if (max == UNLIMITED_ARGUMENTS) {
3477-
err_mess = rb_sprintf("wrong number of arguments (given %d, expected %d+)", argc, min);
3478-
}
3479-
else {
3480-
err_mess = rb_sprintf("wrong number of arguments (given %d, expected %d..%d)", argc, min, max);
3481-
}
3482-
return rb_exc_new3(rb_eArgError, err_mess);
3483-
}
34843471

3485-
NORETURN(void rb_error_arity(int argc, int min, int max))
3486-
{
3487-
rb_exc_raise(rb_arity_error_new(argc, min, max));
3472+
NORETURN(void rb_error_arity(int argc, int min, int max)) {
3473+
rb_exc_raise(rb_exc_new3(rb_eArgError, rb_tr_wrap(polyglot_invoke(RUBY_CEXT, "rb_arity_error_string", argc, min, max))));
34883474
}
34893475

34903476
void rb_error_untrusted(VALUE obj) {

src/main/ruby/core/type.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,18 @@ def self.object_respond_to_marshal_load?(obj)
611611

612612
def self.check_arity(arg_count, min, max)
613613
if arg_count < min || (max != -1 && arg_count > max)
614-
error_message = case
615-
when min == max
616-
'wrong number of arguments (given %d, expected %d)' % [arg_count, min]
617-
when max == -1
618-
'wrong number of arguments (given %d, expected %d+)' % [arg_count, min]
619-
else
620-
'wrong number of arguments (given %d, expected %d..%d)' % [arg_count, min, max]
621-
end
622-
raise ArgumentError, error_message
614+
raise ArgumentError, arity_error_string(arg_count, min, max)
615+
end
616+
end
617+
618+
def self.arity_error_string(arg_count, min, max)
619+
case
620+
when min == max
621+
'wrong number of arguments (given %d, expected %d)' % [arg_count, min]
622+
when max == -1
623+
'wrong number of arguments (given %d, expected %d+)' % [arg_count, min]
624+
else
625+
'wrong number of arguments (given %d, expected %d..%d)' % [arg_count, min, max]
623626
end
624627
end
625628

0 commit comments

Comments
 (0)