Skip to content

Commit

Permalink
use Check_Type for type-checking
Browse files Browse the repository at this point in the history
Check_Type is less code and used throughout the Ruby ecosystem and
raises TypeError on incorrect argument types.  This is a minor behavior
change as it replaces some ArgumentError exceptions with TypeError, but
is more consistent with other Ruby libraries.
  • Loading branch information
SamSaffron committed Feb 22, 2022
1 parent 493e1bc commit 8b4d03e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
30 changes: 8 additions & 22 deletions ext/mini_racer_extension/mini_racer_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,7 @@ static const rb_data_type_t isolate_type = {
static VALUE rb_platform_set_flag_as_str(VALUE _klass, VALUE flag_as_str) {
bool platform_already_initialized = false;

if(TYPE(flag_as_str) != T_STRING) {
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE" (should be a string)",
rb_obj_class(flag_as_str));
}
Check_Type(flag_as_str, T_STRING);

platform_lock.lock();

Expand Down Expand Up @@ -871,10 +868,7 @@ static VALUE rb_snapshot_load(VALUE self, VALUE str) {
SnapshotInfo* snapshot_info;
TypedData_Get_Struct(self, SnapshotInfo, &snapshot_type, snapshot_info);

if(TYPE(str) != T_STRING) {
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
rb_obj_class(str));
}
Check_Type(str, T_STRING);

init_v8();

Expand All @@ -901,10 +895,7 @@ static VALUE rb_snapshot_warmup_unsafe(VALUE self, VALUE str) {
SnapshotInfo* snapshot_info;
TypedData_Get_Struct(self, SnapshotInfo, &snapshot_type, snapshot_info);

if(TYPE(str) != T_STRING) {
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
rb_obj_class(str));
}
Check_Type(str, T_STRING);

init_v8();

Expand Down Expand Up @@ -1148,13 +1139,10 @@ static VALUE rb_context_eval_unsafe(VALUE self, VALUE str, VALUE filename) {
TypedData_Get_Struct(self, ContextInfo, &context_type, context_info);
Isolate* isolate = context_info->isolate_info->isolate;

if(TYPE(str) != T_STRING) {
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be a string)",
rb_obj_class(str));
}
if(filename != Qnil && TYPE(filename) != T_STRING) {
rb_raise(rb_eArgError, "wrong type argument %" PRIsVALUE " (should be nil or a string)",
rb_obj_class(filename));
Check_Type(str, T_STRING);

if (!NIL_P(filename)) {
Check_Type(filename, T_STRING);
}

{
Expand Down Expand Up @@ -1772,9 +1760,7 @@ static VALUE rb_context_call_unsafe(int argc, VALUE *argv, VALUE self) {
}

VALUE function_name = argv[0];
if (TYPE(function_name) != T_STRING) {
rb_raise(rb_eTypeError, "first argument should be a String");
}
Check_Type(function_name, T_STRING);

char *fname = RSTRING_PTR(function_name);
if (!fname) {
Expand Down
4 changes: 2 additions & 2 deletions test/mini_racer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_types

def test_compile_nil_context
context = MiniRacer::Context.new
assert_raises(ArgumentError) do
assert_raises(TypeError) do
assert_equal 2, context.eval(nil)
end
end
Expand Down Expand Up @@ -463,7 +463,7 @@ def test_invalid_warmup_sources_throw_an_exception
end

def test_invalid_warmup_sources_throw_an_exception_2
assert_raises(ArgumentError) do
assert_raises(TypeError) do
MiniRacer::Snapshot.new('function f() { return 1 }').warmup!([])
end
end
Expand Down

0 comments on commit 8b4d03e

Please sign in to comment.