Skip to content

Commit

Permalink
Fix rb_warn and add location and "warning: " prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Nov 13, 2024
1 parent 1571454 commit 95e39c8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/cext/ABI_check.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2
3
6 changes: 6 additions & 0 deletions lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2319,6 +2319,12 @@ def rb_eval_cmd_kw(cmd, args, kw_splat)
end
end

def rb_tr_warn(message)
location = caller_locations(1, 1)[0]
message_with_prefix = location.label + ': warning: ' + message
Warning.warn(message_with_prefix)
end

def rb_warning_category_enabled_p(category)
Warning[category]
end
Expand Down
22 changes: 6 additions & 16 deletions spec/ruby/optional/capi/kernel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,16 @@ class CApiKernelSpecs::Exc < StandardError
end

describe "rb_warn" do
before :each do
@stderr, $stderr = $stderr, IOStub.new
@verbose = $VERBOSE
end

after :each do
$stderr = @stderr
$VERBOSE = @verbose
end

it "prints a message to $stderr if $VERBOSE evaluates to true" do
$VERBOSE = true
@s.rb_warn("This is a warning")
$stderr.should =~ /This is a warning/
-> {
@s.rb_warn("This is a warning")
}.should complain(/warning: This is a warning/, verbose: true)
end

it "prints a message to $stderr if $VERBOSE evaluates to false" do
$VERBOSE = false
@s.rb_warn("This is a warning")
$stderr.should =~ /This is a warning/
-> {
@s.rb_warn("This is a warning")
}.should complain(/warning: This is a warning/, verbose: false)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/main/c/cext/truffleruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int rb_tr_obj_equal(VALUE first, VALUE second) {
}

void rb_tr_warn_va_list(const char *fmt, va_list args) {
RUBY_INVOKE(rb_mKernel, "warn", rb_vsprintf(fmt, args));
RUBY_CEXT_INVOKE("rb_tr_warn", rb_vsprintf(fmt, args));
}

VALUE rb_tr_zlib_crc_table(void) {
Expand Down

0 comments on commit 95e39c8

Please sign in to comment.