Skip to content

Commit

Permalink
[GR-52676] Handle the Panama Truffle NFI backend not being available …
Browse files Browse the repository at this point in the history
…when embedded

* We cannot just add it as a dependency of TruffleRuby because
  the Panama backend is 22+ but Truffle 24.2 will still support JDK 21.
  We can add it as a dependency once Truffle drops supports for JDK 21.
  • Loading branch information
eregon committed Nov 10, 2024
1 parent 28d3457 commit 3a37f06
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/truffle/truffle/cext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,20 @@ def self.init_libtrufflerubytrampoline(libtrampoline)

init_functions = libtrampoline[:rb_tr_trampoline_init_functions]
init_functions = Primitive.interop_eval_nfi('(env,(string):pointer):void').bind(init_functions)
if Truffle::Boot.get_option 'cexts-panama' and Primitive.vm_java_version >= 22 and !TruffleRuby.native?

panama = Truffle::Boot.get_option('cexts-panama') && Primitive.vm_java_version >= 22 && !TruffleRuby.native?
if panama
# Check if the Panama backend is available, it might not be when embedding TruffleRuby
rb_tr_invoke = LIBTRUFFLERUBY['rb_tr_invoke']
begin
keep_alive << rb_tr_invoke.createNativeClosure('panama')
rescue Polyglot::ForeignException => e
warn "warning: the Panama Truffle NFI backend for running C extensions faster was not available (#{e.message}). Add 'org.graalvm.truffle:truffle-nfi-panama' to Maven dependencies to resolve that."
panama = false
end
end

if panama
init_functions.call(-> name {
closure = LIBTRUFFLERUBY[name].createNativeClosure('panama')
keep_alive << closure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public void testRequireGem() {
}
}

@Test
public void testRequireCExtension() {
try (Context context = Context.newBuilder("ruby").allowIO(IOAccess.ALL).allowNativeAccess(true).build()) {
Assert.assertEquals("Etc", context.eval("ruby", "require 'etc'; Etc.to_s").asString());
}
}

@Test
public void testThreadsNoNative() throws Throwable {
// The ruby.single_threaded option needs to be set because --single-threaded defaults to --embedded.
Expand Down

0 comments on commit 3a37f06

Please sign in to comment.