ruby-changes:53750
From: nobu <ko1@a...>
Date: Sun, 25 Nov 2018 13:05:23 +0900 (JST)
Subject: [ruby-changes:53750] nobu:r65967 (trunk): No new entry when getting undefined global variable
nobu 2018-11-25 13:05:09 +0900 (Sun, 25 Nov 2018) New Revision: 65967 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65967 Log: No new entry when getting undefined global variable Modified files: trunk/variable.c Index: variable.c =================================================================== --- variable.c (revision 65966) +++ variable.c (revision 65967) @@ -483,13 +483,25 @@ struct rb_global_variable { https://github.com/ruby/ruby/blob/trunk/variable.c#L483 struct trace_var *trace; }; -MJIT_FUNC_EXPORTED struct rb_global_entry* -rb_global_entry(ID id) +static struct rb_global_entry* +rb_find_global_entry(ID id) { struct rb_global_entry *entry; VALUE data; if (!rb_id_table_lookup(rb_global_tbl, id, &data)) { + return NULL; + } + entry = (struct rb_global_entry *)data; + ASSUME(entry != NULL); + return entry; +} + +MJIT_FUNC_EXPORTED struct rb_global_entry* +rb_global_entry(ID id) +{ + struct rb_global_entry *entry = rb_find_global_entry(id); + if (!entry) { struct rb_global_variable *var; entry = ALLOC(struct rb_global_entry); var = ALLOC(struct rb_global_variable); @@ -505,9 +517,6 @@ rb_global_entry(ID id) https://github.com/ruby/ruby/blob/trunk/variable.c#L517 var->trace = 0; rb_id_table_insert(rb_global_tbl, id, (VALUE)entry); } - else { - entry = (struct rb_global_entry *)data; - } return entry; } @@ -620,6 +629,27 @@ global_id(const char *name) https://github.com/ruby/ruby/blob/trunk/variable.c#L629 return id; } +static ID +find_global_id(const char *name) +{ + ID id; + size_t len = strlen(name); + + if (name[0] == '$') { + id = rb_check_id_cstr(name, len, NULL); + } + else { + VALUE vbuf = 0; + char *buf = ALLOCV_N(char, vbuf, len+1); + buf[0] = '$'; + memcpy(buf+1, name, len); + id = rb_check_id_cstr(buf, len+1, NULL); + ALLOCV_END(vbuf); + } + + return id; +} + void rb_define_hooked_variable( const char *name, @@ -858,8 +888,14 @@ VALUE https://github.com/ruby/ruby/blob/trunk/variable.c#L888 rb_gv_get(const char *name) { struct rb_global_entry *entry; + ID id = find_global_id(name); - entry = rb_global_entry(global_id(name)); + if (!id) { + rb_warning("global variable `%s' not initialized", name); + return Qnil; + } + + entry = rb_global_entry(id); return rb_gvar_get(entry); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/