Skip to content

Instantly share code, notes, and snippets.

@okuramasafumi
Created September 1, 2019 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okuramasafumi/ac90bbf04a1c13b7d67954c9c5e62553 to your computer and use it in GitHub Desktop.
Save okuramasafumi/ac90bbf04a1c13b7d67954c9c5e62553 to your computer and use it in GitHub Desktop.
diff --git a/proc.c b/proc.c
index 19ce7a1d19..3eb8c9445d 100644
--- a/proc.c
+++ b/proc.c
@@ -1196,6 +1196,24 @@ iseq_location(const rb_iseq_t *iseq)
return rb_ary_new4(2, loc);
}
+static VALUE
+iseq_code_location(const rb_iseq_t *iseq)
+{
+ VALUE loc[5];
+ rb_code_location_t code_location;
+
+ if (!iseq) return Qnil;
+ rb_iseq_check(iseq);
+ loc[0] = rb_iseq_path(iseq);
+ code_location = iseq->body->location.code_location;
+ loc[1] = INT2FIX(code_location.beg_pos.lineno);
+ loc[2] = INT2FIX(code_location.beg_pos.column);
+ loc[3] = INT2FIX(code_location.end_pos.lineno);
+ loc[4] = INT2FIX(code_location.end_pos.column);
+
+ return rb_ary_new4(5, loc);
+}
+
MJIT_FUNC_EXPORTED VALUE
rb_iseq_location(const rb_iseq_t *iseq)
{
@@ -1216,6 +1234,20 @@ rb_proc_location(VALUE self)
return iseq_location(rb_proc_get_iseq(self, 0));
}
+/*
+ * call-seq:
+ * prc.code_location -> [String, Integer, Integer, Integer, Integer]
+ *
+ * Returns the Ruby source filename and code location
+ * or +nil+ if this proc was not defined in Ruby (i.e. native).
+ */
+
+VALUE
+rb_proc_code_location(VALUE self)
+{
+ return iseq_code_location(rb_proc_get_iseq(self, 0));
+}
+
VALUE
rb_unnamed_parameters(int arity)
{
@@ -3670,6 +3702,7 @@ Init_Proc(void)
rb_define_method(rb_cProc, "<<", proc_compose_to_left, 1);
rb_define_method(rb_cProc, ">>", proc_compose_to_right, 1);
rb_define_method(rb_cProc, "source_location", rb_proc_location, 0);
+ rb_define_method(rb_cProc, "code_location", rb_proc_code_location, 0);
rb_define_method(rb_cProc, "parameters", rb_proc_parameters, 0);
/* Exceptions */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment