How to Load Script from a C Extension?

Hi,

Is it possible to load (and execute) a Ruby script from within a C
extension? From my Ruby code, I can just write

.....
load 'conf.rb'
.....

and my configuration parameters are loaded at that point. However, from a
C extension, when I wrote

.....
rb_load (rb_str_new2 ("conf.rb"), 0);
.....

the program crashed. The strange thins is, if I moved the code several
lines up, the program is OK, but it is not its correct place as the
configuration parameters will be rewritten by the defaults in the
extension itself.

Regards,

Bill

Hi,

My current temporary solution is to open the Ruby script file in C, get it
line by line with “fgets”, and invoke “rb_eval_string” for each line. The
only problem is that it does not work with multi-line Ruby statements. Is
there a better (and easier) solution?

Regards,

Bill

···

==============================================================================
William Djaja Tjokroaminata billtj@z.glue.umd.edu wrote:

Hi,

Is it possible to load (and execute) a Ruby script from within a C
extension? From my Ruby code, I can just write

.....
load 'conf.rb'
.....

Hi,

···

At Sat, 3 Aug 2002 01:14:34 +0900, William Djaja Tjokroaminata wrote:

.....
rb_load (rb_str_new2 ("conf.rb"), 0);
.....

the program crashed. The strange thins is, if I moved the code several
lines up, the program is OK, but it is not its correct place as the
configuration parameters will be rewritten by the defaults in the
extension itself.

Perhaps, the code in that several lines is something wrong.


Nobu Nakada

William Djaja Tjokroaminata billtj@z.glue.umd.edu wrote in message news:aieb3t$o97$1@grapevine.wam.umd.edu

Hi,

Is it possible to load (and execute) a Ruby script from within a C
extension? From my Ruby code, I can just write

.....
load 'conf.rb'
.....

and my configuration parameters are loaded at that point. However, from a
C extension, when I wrote

.....
rb_load (rb_str_new2 ("conf.rb"), 0);
.....

rb_require(“conf.rb”);

works for me

Tomek ‘Alva’ Wroblewski

Hi,

I checked the code and it appears that all those lines are just global
variable definitions. Also, when I open the file manually (in C) and just
run “rb_eval_string” on each line, the program is fine.

(At least, “rb_load” is really the correct function that I need?)

Regards,

Bill

···

===========================================================================
nobu.nokada@softhome.net wrote:

.....
rb_load (rb_str_new2 ("conf.rb"), 0);
.....

the program crashed. The strange thins is, if I moved the code several
lines up, the program is OK, but it is not its correct place as the
configuration parameters will be rewritten by the defaults in the
extension itself.

Perhaps, the code in that several lines is something wrong.


Nobu Nakada

Why call Ruby from the C side? It seems to me to be easier to call your C
code from Ruby. Let Ruby drive…
See my post: embed or swig? for more info…

(OK, I guess if you have to distribute an executable that would be a
reason to embed Ruby in your app instead of using Ruby to call your C
code)

Phil

In article aiegkr$qin$1@grapevine.wam.umd.edu,

···

William Djaja Tjokroaminata billtj@z.glue.umd.edu wrote:

Hi,

My current temporary solution is to open the Ruby script file in C, get it
line by line with “fgets”, and invoke “rb_eval_string” for each line. The
only problem is that it does not work with multi-line Ruby statements. Is
there a better (and easier) solution?

Regards,

Bill

William Djaja Tjokroaminata billtj@z.glue.umd.edu wrote:

Hi,

Is it possible to load (and execute) a Ruby script from within a C
extension? From my Ruby code, I can just write

.....
load 'conf.rb'
.....

Hi,

Yes, in my C extension Init_xxx function, I have a rb_require right after
the variable declarations and it works. Then, I have some other C codes
such as class definitions. When I have another rb_require, it resulted in
segmentation fault.

Is it an implied rule that in a C extension all the rb_require’s should be
put at the beginning of the Init function? If rb_require does not work,
should rb_load or rb_load_protect work in the middle of the Init
function? At least in my case using rb_eval_string it works, but it
cannot handle multi-line Ruby statements. Thanks.

Regards,

Bill

···

===========================================================================
Alva alv@poczta.onet.pl wrote:

rb_require(“conf.rb”);

works for me

Tomek ‘Alva’ Wroblewski

Hi,

I checked the code and it appears that all those lines are just global
variable definitions. Also, when I open the file manually (in C) and just
run “rb_eval_string” on each line, the program is fine.

Could you show me the code fragment and backtrace? And what’s
your environment; platform, ruby version and so on?

(At least, “rb_load” is really the correct function that I need?)

Yes.

···

At Sat, 3 Aug 2002 03:34:52 +0900, William Djaja Tjokroaminata wrote:


Nobu Nakada

(At least, "rb_load" is really the correct function that I need?)

It's best to use rb_load_protect(), this give you the possibility to catch
errors given by ruby.

Guy Decoux

Why call Ruby from the C side? It seems to me to be easier to call your C
code from Ruby. Let Ruby drive...
See my post: embed or swig? for more info...

Well, probably it exist different problems with different solutions :slight_smile:

For example, just try to use swig to write PL/Ruby :slight_smile:

Guy Decoux

Hi,

Yes, in my application, initially it is a Ruby script that calls my C
extension, as a user will run it as

ruby my_script.rb

and in “my_script.rb” the user has to put “require
’my_extension’”. However, the configuration file is written in Ruby,
which is invoked by my_extension. It is this part which is not working.

Is there another solution to this ruby->C->ruby file sequence problem?

Regards,

Bill

···

============================================================================
Phil Tomson ptkwt@shell1.aracnet.com wrote:

Why call Ruby from the C side? It seems to me to be easier to call your C
code from Ruby. Let Ruby drive…
See my post: embed or swig? for more info…

(OK, I guess if you have to distribute an executable that would be a
reason to embed Ruby in your app instead of using Ruby to call your C
code)

Phil

Yes, in my C extension Init_xxx function, I have a rb_require right after
the variable declarations and it works. Then, I have some other C codes
such as class definitions. When I have another rb_require, it resulted in
segmentation fault.

You have a bug. Post your source or nobody will be able to help you

Guy Decoux

Hi,

My environment is
ruby -v : ruby 1.6.7 (2002-03-01) [i686-linux]
uname -a: Linux 2.4.8-26mdk #1 Sun Sep 23 17:06:39 CEST
2001 i686 unknown

Before I try to put the code fragment, what is exactly the second argument
in “rb_load (value, int)”? Can I simply set the int to zero? Also, what
is the third argument in “rb_load_protect (value, int, int*)”?

For your information, in my case the sequence of execution is like
this. A script is run in Ruby, such as “ruby my_script.rb”. my_script_rb
has “require ‘my_extension’”. my_extension.c has “rb_load” inside its
"Init_xxx" function. Thanks.

Regards,

Bill

···

===========================================================================
nobu.nokada@softhome.net wrote:

Could you show me the code fragment and backtrace? And what’s
your environment; platform, ruby version and so on?

(At least, “rb_load” is really the correct function that I need?)

Yes.


Nobu Nakada

Why go ruby->C->ruby? Why not do

  1. parse the file
  2. require your extension
  3. send the parsed data to extension?

/Erik

···

On Mon, 2002-08-05 at 15:18, William Djaja Tjokroaminata wrote:

Hi,

Yes, in my application, initially it is a Ruby script that calls my C
extension, as a user will run it as

ruby my_script.rb

and in “my_script.rb” the user has to put “require
’my_extension’”. However, the configuration file is written in Ruby,
which is invoked by my_extension. It is this part which is not working.

Is there another solution to this ruby->C->ruby file sequence problem?


Erik Bågfors | erik@bagfors.nu
Supporter of free software | GSM +46 733 279 273
fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32

Hi Guy,

Thanks for the assertion. After reading the code again, it is the same
old problem: some of the global variables have not been taken care of by
either “rb_global_variable”, “rb_define_readonly_variable”, or
"rb_define_variable".

The only tricky part is, the code has been working for so long, and only
much later that the problem appears. Now I am wondering, what is the rule
to protect the global variables? Do ALL C VALUE global variables have to
be protected as above? Or, ALL, except:

- variables that have been used in "rb_define_module..."
- variables that have been used in "rb_define_class..."
- (others?)

On the other hand, if we protect such variables, will it result in
segmentation fault instead? Or is it OK to “overprotect” them? (Is it
similar to the C’s problem of is it OK to free a variable that has been
freed?)

In the pick-axe book such variables are not protected. Currently I don’t
protect them either. However, because of these two experiences, I don’t
know whether I have done the right thing or not, because the problem may
appear in the future, when I just add another function call, which will
lead me to believe that the problem occurs because of the newly added
code. Thanks.

Regards,

Bill

···

============================================================================
ts decoux@moulon.inra.fr wrote:

You have a bug. Post your source or nobody will be able to help you

Guy Decoux

Hi,

I am not too clear regarding the meaning of “send the parsed data to
extension”.

In my case, the extension has the default values while the file has the
user-definable parameters. For user’s ease-of-use, he needs to do only
"require ‘my_extension’". my_extension defines several global
(configuration) parameters with their default values. The user, however,
can also change the parameters in a separate file called conf.rb, which
can simply contain statements such as

$param1 = myValue

Therefore, in my_extension, first the global variables are defined with
their default values, and then, it may be overridden by the Ruby
statements in conf.rb.

Also, I remember that in Tcl, there is a “source” statement which is
pretty similar to C’s #include directive, which has the net effect of
inserting the content of the file at that point. Is Ruby’s
"rb_require" or “rb_load” the same as Tcl’s “source”?

Regards,

Bill

···

========================================================================
Erik Bagfors erik@bagfors.nu wrote:

Why go ruby->C->ruby? Why not do

  1. parse the file
  2. require your extension
  3. send the parsed data to extension?

/Erik

The only tricky part is, the code has been working for so long, and only
much later that the problem appears. Now I am wondering, what is the rule
to protect the global variables? Do ALL C VALUE global variables have to
be protected as above? Or, ALL, except:

You really don't want to post your source :slight_smile:

Just the part between 'Init_xxx()' and the line where it segfault, or send
me the source in private email

Guy Decoux

Hi,

Thanks for the assertion. After reading the code again, it is the same
old problem: some of the global variables have not been taken care of by
either “rb_global_variable”, “rb_define_readonly_variable”, or
"rb_define_variable".

The only tricky part is, the code has been working for so long, and only
much later that the problem appears. Now I am wondering, what is the rule
to protect the global variables? Do ALL C VALUE global variables have to
be protected as above? Or, ALL, except:

- variables that have been used in "rb_define_module..."
- variables that have been used in "rb_define_class..."
- (others?)

All of your list protect the variables. Particularly, all what
rb_global_variable() does is it. And other Ruby global
variables and classes/modules are also protected of course.

On the other hand, if we protect such variables, will it result in
segmentation fault instead? Or is it OK to “overprotect” them? (Is it
similar to the C’s problem of is it OK to free a variable that has been
freed?)

Only speed and space penalties.

···

At Tue, 6 Aug 2002 00:39:10 +0900, William Djaja Tjokroaminata wrote:


Nobu Nakada

Hi Guy,

When I protect the global variables, the problem has been solved. One
problem with the source is it is rather long (currently 2362 lines); I
have tried to cut it, but then depending where I cut it, segmentation
fault either appeared in different places or not at all.

Although currently the problem has been solved, I don’t know whether there
are other “time bombs” still lurking in my code. That’s why I am asking
the rule of when it is necessary to protect the global variables. Is it
that such a rule does not exist that you need to see the actual source?

Regards,

Bill

···

============================================================================
ts decoux@moulon.inra.fr wrote:

You really don’t want to post your source :slight_smile:

Just the part between ‘Init_xxx()’ and the line where it segfault, or send
me the source in private email

Guy Decoux

Although currently the problem has been solved, I don't know whether there
are other "time bombs" still lurking in my code. That's why I am asking
the rule of when it is necessary to protect the global variables. Is it
that such a rule does not exist that you need to see the actual source?

No, these rules exist but I don't want to formalize them because I don't
speak english. When you ask for help, it's best to give the maximum of
informations, otherwise you'll have only useless responses.

Guy Decoux