Skip to content

Commit

Permalink
[GR-18163] Add spec and fix exception for Fiddle::Handle.new with a m…
Browse files Browse the repository at this point in the history
…issing library (#2714)

PullRequest: truffleruby/3477
  • Loading branch information
eregon committed Sep 2, 2022
2 parents 621db11 + 3fd4d3a commit 76dfee9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Compatibility:
* Fix `rb_class2name` to call `inspect` on anonymous classes like in CRuby (#2701, @aardvark179).
* Implement `rb_ivar_foreach` to iterate over instance and class variables like in CRuby (#2701, @aardvark179).
* Fix the absolute path of the main script after chdir (#2709, @eregon).
* Fix exception for `Fiddle::Handle.new` with a missing library (#2714, @eregon).

Performance:

Expand Down
7 changes: 4 additions & 3 deletions lib/truffle/truffle/fiddle_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,16 @@ def self.[](*args)
RTLD_GLOBAL = Truffle::Config['platform.dlopen.RTLD_GLOBAL']

def initialize(library = nil, flags = RTLD_LAZY | RTLD_GLOBAL)
raise DLError, 'unsupported dlopen flags' if flags != RTLD_LAZY | RTLD_GLOBAL
raise DLError, 'unsupported dlopen flags' if flags != (RTLD_LAZY | RTLD_GLOBAL)

if library == Truffle::FiddleBackend::RTLD_NEXT
@handle = :rtld_next
else
library = nil if library == Truffle::FiddleBackend::RTLD_DEFAULT
begin
@handle = Primitive.interop_eval_nfi(library ? "load '#{library}'" : 'default')
rescue RuntimeError
raise DLError, "#{library}: cannot open shared object file: No such file or directory"
rescue Polyglot::ForeignException => e
raise DLError, "#{e.message}"
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/ruby/library/fiddle/handle/initialize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require_relative '../../../spec_helper'
require 'fiddle'

describe "Fiddle::Handle#initialize" do
it "raises Fiddle::DLError if the library cannot be found" do
-> {
Fiddle::Handle.new("doesnotexist.doesnotexist")
}.should raise_error(Fiddle::DLError)
end
end
3 changes: 2 additions & 1 deletion spec/ruby/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def report_on_exception=(value)
end
end

ruby_version_is ""..."2.7" do
# Compare with SpecVersion directly here so it works even with --unguarded
if VersionGuard::FULL_RUBY_VERSION < SpecVersion.new('2.7')
abort "This version of ruby/spec requires Ruby 2.7+"
end

Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestFiddle.rb

This file was deleted.

0 comments on commit 76dfee9

Please sign in to comment.