MRI-Binding: Adjust 'Kernel#caller' output to match RMXP

This commit is contained in:
Jonas Kulla 2014-05-06 12:13:12 +02:00
parent c6a3c5aa59
commit 1bdcfa7b04
1 changed files with 37 additions and 0 deletions

View File

@ -70,6 +70,8 @@ RB_METHOD(mriP);
RB_METHOD(mriDataDirectory); RB_METHOD(mriDataDirectory);
RB_METHOD(mkxpPuts); RB_METHOD(mkxpPuts);
RB_METHOD(_kernelCaller);
static void mriBindingInit() static void mriBindingInit()
{ {
tableBindingInit(); tableBindingInit();
@ -102,6 +104,9 @@ static void mriBindingInit()
_rb_define_module_function(mod, "data_directory", mriDataDirectory); _rb_define_module_function(mod, "data_directory", mriDataDirectory);
_rb_define_module_function(mod, "puts", mkxpPuts); _rb_define_module_function(mod, "puts", mkxpPuts);
rb_define_alias(rb_singleton_class(rb_mKernel), "_mkxp_kernel_caller_alias", "caller");
_rb_define_module_function(rb_mKernel, "caller", _kernelCaller);
rb_define_global_const("MKXP", Qtrue); rb_define_global_const("MKXP", Qtrue);
} }
@ -176,6 +181,38 @@ RB_METHOD(mriDataDirectory)
return pathStr; return pathStr;
} }
RB_METHOD(_kernelCaller)
{
RB_UNUSED_PARAM;
VALUE trace = rb_funcall2(rb_mKernel, rb_intern("_mkxp_kernel_caller_alias"), 0, 0);
if (rb_type(trace) != RUBY_T_ARRAY)
return trace;
long len = RARRAY_LEN(trace);
if (len < 2)
return trace;
/* Remove useless "ruby:1:in 'eval'" */
rb_ary_pop(trace);
/* Also remove trace of this helper function */
rb_ary_shift(trace);
len -= 2;
if (len == 0)
return trace;
/* RMXP does this, not sure if specific or 1.8 related */
VALUE args[] = { rb_str_new_cstr(":in `<main>'"), rb_str_new_cstr("") };
rb_funcallv(rb_ary_entry(trace, len-1), rb_intern("gsub!"), 2, args);
return trace;
}
static VALUE newStringUTF8(const char *string, long length) static VALUE newStringUTF8(const char *string, long length)
{ {
return rb_enc_str_new(string, length, rb_utf8_encoding()); return rb_enc_str_new(string, length, rb_utf8_encoding());