danielpclark / rutie

“The Tie Between Ruby and Rust.”
MIT License
939 stars 62 forks source link

Can't find `RUBY_DESCRIPTION` when using a native gem from rust #143

Open pwoolcoc opened 3 years ago

pwoolcoc commented 3 years ago

When trying to use ruby inside a rust application, I'm having some issues with requiring gems with C extensions, in this case Nokogiri. When I VM::require("nokogiri"), I get an error from the VM: #<NameError: uninitialized constant RUBY_DESCRIPTION>.

To reproduce:

# Gemfile

# ...

gem "nokogiri"
// src/main.rs

use rutie::{VM, Object, NilClass};

fn main() {
    VM::init();
    VM::init_loadpath();
    VM::require("rubygems"); 

    if let Err(e) = VM::protect(|| {
        VM::require("nokogiri");
        NilClass::new().into()
    }) {
        let output = VM::error_info().unwrap();
        eprintln!("{}", output);
    }
}

I run the program with:


$ RUBYLIB=$(bundle exec ruby -e 'puts $LOAD_PATH' | tr '\n' ':') DYLD_LIBRARY_PATH=$(bundle exec ruby -e "puts RbConfig::CONFIG['libdir']") cargo run
danielpclark commented 3 years ago

@pwoolcoc You might need to initialize it first:

https://github.com/ruby/ruby/blob/d92f09a5eea009fa28cd046e9d0eb698e3d94c5c/version.c#L100

which provides

void
Init_ruby_description(void)
{
    VALUE description;

    if (MJIT_OPTS_ON) {
        description = MKSTR(description_with_jit);
    }
    else {
        description = MKSTR(description);
    }

    /*
     * The full ruby version string, like <tt>ruby -v</tt> prints
     */
    rb_define_global_const("RUBY_DESCRIPTION", /* MKSTR(description) */ description);
}

On many of these kinds of errors you can simply search the Ruby source code.