Skip to content

Real C extension gems: install, load and match CRuby - #250

Merged
Largo merged 3 commits into
masterfrom
capi-gems
Nov 20, 2026
Merged

Largo merged 3 commits into
masterfrom
capi-gems

Conversation

@Largo

@Largo Largo commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Installing real C extension gems with gem install --platform ruby and running a script exercising each against CRuby 4.0 turned up a few gaps. With this branch these build, load and give CRuby's results (Linux x64):

  • fast_blank, redcarpet (a Ruby renderer subclass included), jaro_winkler, escape_utils, racc (cparse, with a generated grammar), digest-murmurhash (digest plugin API) and unf_ext (C++)
  • with IRONRUBY_NATIVE_GEMS: bigdecimal, msgpack (ext types included), bcrypt and oj (compat and object mode)

Three commits:

  1. C API: ractor.h and the functions real gems reach for first. rb_ext_ractor_safe was a stub that raised, and most current extensions call it first in Init_. The rest of ruby/ractor.h comes along. Also added: rb_obj_freeze_inline, rb_hash_size_num, rb_opts_exception_p, rb_must_asciicompat and rb_memhash.
  2. Allow undefining initialize, as CRuby does. IronRuby raised a TypeError, and racc's cparse undefines it in its Init. undef and undef_method now warn as CRuby does, and new then ends in NoMethodError through method_missing. Scenario_RubyInitializers6 now checks CRuby's behaviour.
  3. RubyGems: native gems' extensions win over IronRuby's own.
    • Specification#build_extensions is a prepended module now. Before, its super reached BasicSpecification, so gem install oj failed.
    • IronRuby's shims named like an extension (msgpack/msgpack.rb, oj/oj.rb, bcrypt_ext.rb) load the gem's .so when IRONRUBY_NATIVE_GEMS selects that gem. Without that, a .rb on the load path always wins over a .so.

Checked: IronRuby.Tests and spec/optional/capi (1660 examples) all pass, and spec/language, core/class, core/module and core/basicobject show only the known into/to errors. Without IRONRUBY_NATIVE_GEMS, the built-in msgpack, oj and bcrypt load and behave as before.

Installing and running real C extension gems turned up functions the spec suite never
calls:
- rb_ext_ractor_safe (RB_EXT_RACTOR_SAFE), which most current extensions call
  first thing in Init_ (jaro_winkler, msgpack, oj...). It was a stub that raised,
  so the extension failed to load. The rest of ruby/ractor.h comes along:
  - The Ractor's stdin, stdout and stderr are $stdin & co.
  - rb_ractor_make_shareable(_copy) and the rb_ractor_shareable_p fallback go to
    Ractor.
  - Ractor-local storage keys hold one value each, since an extension only runs
    in the main Ractor here.
- rb_obj_freeze_inline (RB_OBJ_FREEZE) and rb_must_asciicompat: bigdecimal, oj.
- rb_hash_size_num (RHASH_SIZE): msgpack.
- rb_opts_exception_p, object.c's handling of the exception: keyword, which
  bigdecimal looks for with have_func: bigdecimal, oj.
- rb_memhash: oj.

With these, and the two commits that follow, these gems build with `gem install`,
load and give the same results as on CRuby 4.0 for a script exercising each:
- fast_blank, redcarpet, jaro_winkler, escape_utils, racc (cparse),
  digest-murmurhash and unf_ext (C++).
- With IRONRUBY_NATIVE_GEMS: bigdecimal, msgpack, bcrypt and oj.

spec/optional/capi still passes in full.
IronRuby raised TypeError "Cannot undefine 'initialize' method". CRuby 4.0 allows
it. `undef`/undef_method warn "undefining 'initialize' may cause serious problems",
as they do for object_id and __send__, and C's rb_undef_method says nothing. racc's
C parser does it in its Init (rb_undef_method(CparseParams, "initialize")), so
racc/cparse failed to load.

new on such a class allocates the object and calls #initialize anyway, which ends
in method_missing: NoMethodError "undefined method 'initialize' for an instance of
D" unless the class defines method_missing, as in CRuby. Object construction had
asserted that initialize always resolves; it now builds that call when it does
not.

Scenario_RubyInitializers6 asserted the old TypeError and now checks CRuby's
behaviour and its warning. IronRuby.Tests, spec/language, spec/core/class,
spec/core/module and spec/core/basicobject pass, apart from the known into/to
wording errors.
Two problems with installing real C extension gems:

- Gem::Specification#build_extensions was redefined in the class body, with a
  `super` meant for RubyGems' own method. That method was what it replaced, so
  `super` reached BasicSpecification, which has none. `gem install oj` failed with
  NoMethodError as soon as it got to oj's installed bigdecimal dependency. The
  override is now a prepended module, as the installer's already was.

- With IRONRUBY_NATIVE_GEMS naming msgpack (or oj, or bcrypt), the gem was
  activated ahead of IronRuby's built-in on $LOAD_PATH, and its compiled
  extension still never loaded: `require "msgpack/msgpack"` takes a .rb from
  anywhere on the load path before a .so, as CRuby's does, and
  Src/StdLib/ironruby/msgpack/msgpack.rb is IronRuby's implementation under the
  extension's name. The three built-in files that sit under an extension's name
  (msgpack/msgpack.rb, oj/oj.rb, bcrypt_ext.rb) now ask
  Gem.ironruby_native_extension_for first, and load the native gem's .so instead
  when there is one. Without IRONRUBY_NATIVE_GEMS the lookup returns at once and
  nothing changes.

Checked by installing msgpack, oj and bcrypt with IRONRUBY_NATIVE_GEMS='*' and
comparing a script that exercises them with CRuby 4.0, and by running the same
script without the variable (the built-ins load as before). The C API README
lists the gems tried.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@Largo
Largo merged commit 1938e51 into master Nov 20, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant