suggestion: rb_eval_string_location

  • Thread starter Basile STARYNKEVITCH
  • Start date
B

Basile STARYNKEVITCH

Dear All Ruby implementors,

Sometimes it is useful that if an error occur in an evaluated string,
the location of the error is displayed as something more meaningful
(and more specific) than "(eval)"

May I suggest to add the following functions to the Ruby C API - add
them into eval.c (FWIW I did want them & tested them in my Guis for
ruby - see http://starynkevitch.net/Basile/guisdoc_ruby_beta.html for
more)

I believe that a bit more stuff might need to be coded for this to
work really properly - but I really think that the C coder should be
offered the possibility to set the location information (displayed in
backtraces & error messages) of the evaluated string


################################################################
/***
evaluate a string with an explicit location (name of the "file"
displayed on error)
***/

VALUE
rb_eval_string_location(const char *str, const char* loc)
{
VALUE v;
NODE *oldsrc = ruby_current_node;
ruby_current_node = 0;
v = eval(ruby_top_self, rb_str_new2(str), Qnil, loc, 1);
ruby_current_node = oldsrc;
return v;
}

/*** evaluate with protection a string with explicit location ***/
VALUE
rb_eval_string_protect_location(const char *str, const char*loc, int *state)
{
VALUE result; /* OK */
int status;
char* oldfile = ruby_sourcefile;
int oldline = ruby_sourceline;
PUSH_TAG(PROT_NONE);
if ((status = EXEC_TAG()) == 0) {
ruby_sourcefile = loc;
ruby_sourceline = 1;
result = rb_eval_string_location(str, loc);
}
POP_TAG();
if (state) {
*state = status;
ruby_sourcefile = oldfile;
ruby_sourceline = oldline;
}
if (status != 0) {
return Qnil;
}
return result;
}
################################################################
--

Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top