mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
variable.c: rb_search_class_path
* variable.c (rb_tmp_class_path): defer making temporary class path string. * variable.c (rb_search_class_path): search class path or return Qnil or Qfalse if unnamed, not creating a temporary path. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49779 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1ef1a0c602
commit
badb4de72a
3 changed files with 56 additions and 17 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Sat Feb 28 15:42:27 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* variable.c (rb_tmp_class_path): defer making temporary class
|
||||||
|
path string.
|
||||||
|
|
||||||
|
* variable.c (rb_search_class_path): search class path or return
|
||||||
|
Qnil or Qfalse if unnamed, not creating a temporary path.
|
||||||
|
|
||||||
Sat Feb 28 15:02:02 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Sat Feb 28 15:02:02 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* variable.c (rb_tmp_class_path): preserve name encoding of an
|
* variable.c (rb_tmp_class_path): preserve name encoding of an
|
||||||
|
|
|
@ -1029,6 +1029,7 @@ extern rb_encoding OnigEncodingUTF_8;
|
||||||
|
|
||||||
/* variable.c */
|
/* variable.c */
|
||||||
size_t rb_generic_ivar_memsize(VALUE);
|
size_t rb_generic_ivar_memsize(VALUE);
|
||||||
|
VALUE rb_search_class_path(VALUE);
|
||||||
|
|
||||||
/* version.c */
|
/* version.c */
|
||||||
extern VALUE ruby_engine_name;
|
extern VALUE ruby_engine_name;
|
||||||
|
|
64
variable.c
64
variable.c
|
@ -211,7 +211,26 @@ rb_mod_name(VALUE mod)
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef VALUE (*path_cache_func)(VALUE obj, ID id, VALUE val);
|
static VALUE
|
||||||
|
make_temporary_path(VALUE obj, VALUE klass)
|
||||||
|
{
|
||||||
|
VALUE path;
|
||||||
|
switch (klass) {
|
||||||
|
case Qnil:
|
||||||
|
path = rb_sprintf("#<Class:%p>", (void*)obj);
|
||||||
|
break;
|
||||||
|
case Qfalse:
|
||||||
|
path = rb_sprintf("#<Module:%p>", (void*)obj);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
path = rb_sprintf("#<%"PRIsVALUE":%p>", klass, (void*)obj);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
OBJ_FREEZE(path);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef VALUE (*path_cache_func)(VALUE obj, VALUE name);
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_tmp_class_path(VALUE klass, int *permanent, path_cache_func cache_path)
|
rb_tmp_class_path(VALUE klass, int *permanent, path_cache_func cache_path)
|
||||||
|
@ -230,39 +249,37 @@ rb_tmp_class_path(VALUE klass, int *permanent, path_cache_func cache_path)
|
||||||
else {
|
else {
|
||||||
if (RB_TYPE_P(klass, T_MODULE)) {
|
if (RB_TYPE_P(klass, T_MODULE)) {
|
||||||
if (rb_obj_class(klass) == rb_cModule) {
|
if (rb_obj_class(klass) == rb_cModule) {
|
||||||
path = rb_sprintf("#<Module:%p>", (void*)klass);
|
path = Qfalse;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int perm;
|
int perm;
|
||||||
path = rb_tmp_class_path(RBASIC(klass)->klass, &perm, cache_path);
|
path = rb_tmp_class_path(RBASIC(klass)->klass, &perm, cache_path);
|
||||||
path = rb_sprintf("#<%"PRIsVALUE":%p>", path, (void*)klass);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
path = rb_sprintf("#<Class:%p>", (void*)klass);
|
|
||||||
}
|
|
||||||
OBJ_FREEZE(path);
|
|
||||||
|
|
||||||
cache_path(klass, tmp_classpath, path);
|
|
||||||
*permanent = 0;
|
*permanent = 0;
|
||||||
|
return cache_path(klass, path);
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
ivar_cache(VALUE obj, VALUE name)
|
||||||
|
{
|
||||||
|
return rb_ivar_set(obj, tmp_classpath, make_temporary_path(obj, name));
|
||||||
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_class_path(VALUE klass)
|
rb_class_path(VALUE klass)
|
||||||
{
|
{
|
||||||
int permanent;
|
int permanent;
|
||||||
VALUE path = rb_tmp_class_path(klass, &permanent, rb_ivar_set);
|
VALUE path = rb_tmp_class_path(klass, &permanent, ivar_cache);
|
||||||
if (!NIL_P(path)) path = rb_str_dup(path);
|
if (!NIL_P(path)) path = rb_str_dup(path);
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
null_cache(VALUE obj, ID id, VALUE val)
|
null_cache(VALUE obj, VALUE name)
|
||||||
{
|
{
|
||||||
return Qnil;
|
return make_temporary_path(obj, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
|
@ -286,6 +303,19 @@ rb_class_path_cached(VALUE klass)
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
never_cache(VALUE obj, VALUE name)
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_search_class_path(VALUE klass)
|
||||||
|
{
|
||||||
|
int permanent;
|
||||||
|
return rb_tmp_class_path(klass, &permanent, never_cache);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
rb_set_class_path_string(VALUE klass, VALUE under, VALUE name)
|
rb_set_class_path_string(VALUE klass, VALUE under, VALUE name)
|
||||||
{
|
{
|
||||||
|
@ -297,7 +327,7 @@ rb_set_class_path_string(VALUE klass, VALUE under, VALUE name)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int permanent;
|
int permanent;
|
||||||
str = rb_str_dup(rb_tmp_class_path(under, &permanent, rb_ivar_set));
|
str = rb_str_dup(rb_tmp_class_path(under, &permanent, ivar_cache));
|
||||||
rb_str_cat2(str, "::");
|
rb_str_cat2(str, "::");
|
||||||
rb_str_append(str, name);
|
rb_str_append(str, name);
|
||||||
OBJ_FREEZE(str);
|
OBJ_FREEZE(str);
|
||||||
|
@ -320,7 +350,7 @@ rb_set_class_path(VALUE klass, VALUE under, const char *name)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int permanent;
|
int permanent;
|
||||||
str = rb_str_dup(rb_tmp_class_path(under, &permanent, rb_ivar_set));
|
str = rb_str_dup(rb_tmp_class_path(under, &permanent, ivar_cache));
|
||||||
rb_str_cat2(str, "::");
|
rb_str_cat2(str, "::");
|
||||||
rb_str_cat2(str, name);
|
rb_str_cat2(str, name);
|
||||||
if (!permanent) {
|
if (!permanent) {
|
||||||
|
@ -394,7 +424,7 @@ const char *
|
||||||
rb_class2name(VALUE klass)
|
rb_class2name(VALUE klass)
|
||||||
{
|
{
|
||||||
int permanent;
|
int permanent;
|
||||||
VALUE path = rb_tmp_class_path(rb_class_real(klass), &permanent, rb_ivar_set);
|
VALUE path = rb_tmp_class_path(rb_class_real(klass), &permanent, ivar_cache);
|
||||||
if (NIL_P(path)) return NULL;
|
if (NIL_P(path)) return NULL;
|
||||||
return RSTRING_PTR(path);
|
return RSTRING_PTR(path);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue