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.
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
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.
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?)
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.
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?
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.
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:
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
For example, just try to use swig to write PL/Ruby
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)
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
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.
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?
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.
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:
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
Just the part between 'Init_xxx()' and the line where it segfault, or send
me the source in private email
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:
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?
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.