Skip to content

Commit

Permalink
added __line__ and __file__ methods for Proc and Method (from MBARI6)
Browse files Browse the repository at this point in the history
  • Loading branch information
brentr authored and FooBarWidget committed Sep 2, 2009
1 parent 9fd1bea commit 0f575c3
Showing 1 changed file with 126 additions and 1 deletion.
127 changes: 126 additions & 1 deletion eval.c
Expand Up @@ -271,6 +271,12 @@ ruby_set_current_source()
}
}

#ifdef MBARI_API
#define SET_METHOD_SOURCE() ruby_set_current_source()
#else
#define SET_METHOD_SOURCE() (void)0
#endif


int ruby_safe_level = 0;
/* safe-level:
Expand Down Expand Up @@ -755,6 +761,7 @@ rb_attr(klass, id, read, write, ex)
if (!name) {
rb_raise(rb_eArgError, "argument needs to be symbol or string");
}
SET_METHOD_SOURCE();
len = strlen(name)+2;
buf = ALLOCA_N(char,len);
snprintf(buf, len, "@%s", name);
Expand Down Expand Up @@ -2283,7 +2290,10 @@ rb_copy_node_scope(node, rval)
NODE *node;
NODE *rval;
{
NODE *copy = NEW_NODE(NODE_SCOPE,0,rval,node->nd_next);
NODE *copy;

SET_METHOD_SOURCE();
copy=NEW_NODE(NODE_SCOPE,0,rval,node->nd_next);

if (node->nd_tbl) {
copy->nd_tbl = ALLOC_N(ID, node->nd_tbl[0]+1);
Expand Down Expand Up @@ -10093,6 +10103,7 @@ rb_mod_define_method(argc, argv, mod)
else {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
}
SET_METHOD_SOURCE();
if (RDATA(body)->dmark == (RUBY_DATA_FUNC)bm_mark) {
node = NEW_DMETHOD(method_unbind(body));
}
Expand Down Expand Up @@ -10125,6 +10136,111 @@ rb_mod_define_method(argc, argv, mod)
}


#ifdef MBARI_API
/*
* call-seq:
* meth.__file__ => String
*
* returns the filename containing this method's definition
*
* raises ArgumentError if method has no associated ruby source code
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/

static VALUE
method_source_file_name(VALUE method)
{
struct METHOD *data;
NODE *node;

Data_Get_Struct(method, struct METHOD, data);
if (node = data->body) {
const char *filename = node->nd_file;
if (filename)
return rb_str_new2(filename);
}
rb_raise(rb_eArgError, "native Method");
}

/*
* call-seq:
* meth.__line__ => Fixnum
*
* returns the starting line number of this method
*
* raises ArgumentError if method has no associated ruby source code
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/

static VALUE
method_source_line(VALUE method)
{
struct METHOD *data;
NODE *node;

Data_Get_Struct(method, struct METHOD, data);
if (node = data->body) {
int lineno = nd_line(node);
if (lineno)
return INT2FIX(nd_line(node));
}
rb_raise(rb_eArgError, "native Method");
}


/*
* call-seq:
* prc.__file__ => String
*
* returns the filename where this proc is defined
*
* raises ArgumentError if proc has no associated ruby source
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/

static VALUE
proc_source_file_name(VALUE block)
{
struct BLOCK *data;
const char *filename;
NODE *node;

Data_Get_Struct(block, struct BLOCK, data);
if ((node = data->frame.node) || (node = data->body))
return rb_str_new2(node->nd_file);
rb_raise(rb_eArgError, "native Proc");
}


/*
* call-seq:
* prc.__line__ => Fixnum
*
* returns the starting line number of this proc
*
* raises ArgumentError if proc has no associated ruby source code
*
* <i>Only available when MBARI_API extentions are enabled at build time</i>
*/

static VALUE
proc_source_line(VALUE block)
{
struct BLOCK *data;
NODE *node;

Data_Get_Struct(block, struct BLOCK, data);
if ((node = data->frame.node) || (node = data->body))
return INT2FIX( nd_line(node) );
rb_raise(rb_eArgError, "native Proc");
}

#endif /* MBARI_API */


/*
* <code>Proc</code> objects are blocks of code that have been bound to
* a set of local variables. Once bound, the code may be called in
Expand Down Expand Up @@ -10209,6 +10325,15 @@ Init_Proc()
rb_define_method(rb_cUnboundMethod, "owner", method_owner, 0);
rb_define_method(rb_cUnboundMethod, "bind", umethod_bind, 1);
rb_define_method(rb_cModule, "instance_method", rb_mod_method, 1);

#ifdef MBARI_API
rb_define_method(rb_cUnboundMethod, "__file__", method_source_file_name, 0);
rb_define_method(rb_cUnboundMethod, "__line__", method_source_line, 0);
rb_define_method(rb_cProc, "__file__", proc_source_file_name, 0);
rb_define_method(rb_cProc, "__line__", proc_source_line, 0);
rb_define_method(rb_cMethod, "__file__", method_source_file_name, 0);
rb_define_method(rb_cMethod, "__line__", method_source_line, 0);
#endif
}

/*
Expand Down

0 comments on commit 0f575c3

Please sign in to comment.