Skip to content

Commit

Permalink
Do not cache the struct layout in the metaclass
Browse files Browse the repository at this point in the history
This caching breaks code that attempts to layout the same struct
class more than once, as in struct_spec.rb's layout specs that
reopen PairLayout in two successive specs.

I do not think this behavior should be supported, and laying out
the same struct twice should probably be an error. Doing it this
way requires at least a guard on the layout value and at most
repeated lookups of that layout via instance variables.
  • Loading branch information
headius committed Jan 8, 2020
1 parent c647799 commit 21a8a8c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/src/main/java/org/jruby/ext/ffi/Struct.java
Expand Up @@ -112,8 +112,14 @@ static final StructLayout getStructLayout(Ruby runtime, IRubyObject structClass)
throw runtime.newRuntimeError("no valid struct layout for " + ((RubyClass) structClass).getName());
}

// Cache the layout on the Struct metaclass for faster retrieval next time
((RubyClass) structClass).setFFIHandle(layout);
// FIXME: This cache breaks repeat layouts against the same class, since the first layout
// gets cached. It broke struct_spec.rb where it reopens PairLayout in two successive specs,
// but has never been reported to JRuby as a bug. This may be a case worth preventing with
// a hard error, to avoid the need to constantly re-retrieve the layout.

// // Cache the layout on the Struct metaclass for faster retrieval next time
// ((RubyClass) structClass).setFFIHandle(layout);

return (StructLayout) layout;

} catch (ClassCastException ex) {
Expand Down

0 comments on commit 21a8a8c

Please sign in to comment.