Feature #7424

an embedded Ruby interpreter doesn't get the full Ruby environment unless it calls ruby_process_options() (which is not documented)

Added by Graham Menhennitt over 1 year ago. Updated over 1 year ago.

[ruby-core:49890]
Status:Assigned
Priority:Normal
Assignee:Koichi Sasada
Category:-
Target version:next minor

Description

The documented way to embed a Ruby interpreter is to call:

RUBY_INIT_STACK;
ruby_init();
ruby_init_loadpath();

However, this leaves the Ruby environment incomplete. As an example, the following program:

#include

int
main(int argc, char *argv[])
{
RUBYINITSTACK;
rubyinit();
ruby
init_loadpath();

rb_eval_string("p Mutex.new.methods");

return 0;

}

prints:

[:locked?, :trylock, :lock, :unlock, :sleep, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singletonclass, :clone, :dup, :initializedup, :initializeclone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :tos, :inspect, :methods, :singletonmethods, :protectedmethods, :privatemethods, :publicmethods, :instancevariables, :instancevariableget, :instancevariableset, :instancevariabledefined?, :instanceof?, :kindof?, :isa?, :tap, :send, :publicsend, :respondto?, :respondtomissing?, :extend, :display, :method, :publicmethod, :definesingletonmethod, :objectid, :toenum, :enumfor,
:==, :equal?, :!, :!=, :instance
eval, :instanceexec, :send, :id_]

Whereas running "ruby -e 'p Mutex.new.methods'" produces:

[:locked?, :trylock, :lock, :unlock, :sleep, :synchronize, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singletonclass, :clone, :dup, :initializedup, :initializeclone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :tos, :inspect, :methods, :singletonmethods, :protectedmethods, :privatemethods, :publicmethods, :instancevariables, :instancevariableget, :instancevariableset, :instancevariabledefined?, :instanceof?, :kindof?, :isa?, :tap, :send, :publicsend, :respondto?, :respondtomissing?, :extend, :display, :method, :publicmethod, :definesingletonmethod, :objectid, :toenum, :enumfor, :==, :equal?, :!, :!=, :instanceeval, :instanceexec, :send, :id_]

Note that ":synchronize" is missing from the former. This is because rubyinitprelude() has not been called - that's what adds synchronize() to Mutex.

A workwaround is to call rubyprocessoptions() as in the following:

#include

int
main(int argc, char argv[])
{
RUBYINITSTACK;
rubyinit();
ruby
init_loadpath();
static char
args[] = { "ruby", "/dev/null" };
rubyprocessoptions(2, args);

rb_eval_string("p Mutex.new.methods");

return 0;

}

This seems very clumsy!

Some possible solutions are:
- call rubyinitprelude() from rubyinit()
- change the linkage of ruby
init_prelude() to be non-static and have the Ruby embedded program call it explicitly (requires a documentation change).


Related issues

Related to ruby-trunk - Feature #3731: Easier Embedding API for Ruby Assigned 08/22/2010

History

#1 Updated by Yusuke Endoh over 1 year ago

  • Tracker changed from Bug to Feature
  • Status changed from Open to Assigned
  • Assignee set to Koichi Sasada

I think that this is a feature request, not a bug. Moving the feature tracker.
Related to #3731.

Yusuke Endoh mame@tsg.ne.jp

#2 Updated by Yusuke Endoh over 1 year ago

  • Target version set to next minor

Also available in: Atom PDF