Permalink
Browse files

Fixed Module#name.

OMG Module#name. We have moved a lot of critical functionality into
Rubinius::Type to avoid issues with people undef'ing methods on core
library classes. We need to create a proper Mirrors implementation.
So some of this commit removes helper methods and under, under protective
methods for Module#name. Further, we need the name at various points in
the VM/C-API, so the 1.8/1.9 logic for "" vs nil for an anonymous module's
name is now in the primitive.
  • Loading branch information...
brixen committed Feb 24, 2012
1 parent 199ec8f commit 63ae26da4df5661139a1373e10289801db1af52b
Showing with 220 additions and 204 deletions.
  1. +31 −0 kernel/bootstrap/type.rb
  2. +0 −18 kernel/common/class.rb
  3. +0 −1 kernel/common/class18.rb
  4. +0 −1 kernel/common/class19.rb
  5. +6 −2 kernel/common/marshal.rb
  6. +10 −36 kernel/common/module.rb
  7. +3 −3 kernel/common/module18.rb
  8. +3 −8 kernel/common/module19.rb
  9. +10 −0 kernel/common/type.rb
  10. +0 −2 kernel/delta/rubinius.rb
  11. +1 −1 lib/compiler/runtime.rb
  12. +1 −2 vm/builtin/access_variable.cpp
  13. +0 −1 vm/builtin/atomic.cpp
  14. +0 −1 vm/builtin/block_environment.cpp
  15. +0 −1 vm/builtin/bytearray.cpp
  16. +0 −1 vm/builtin/channel.cpp
  17. +4 −3 vm/builtin/class.cpp
  18. +0 −1 vm/builtin/compactlookuptable.cpp
  19. +1 −2 vm/builtin/compiledmethod.cpp
  20. +0 −2 vm/builtin/encoding.cpp
  21. +0 −1 vm/builtin/executable.cpp
  22. +0 −1 vm/builtin/list.cpp
  23. +0 −1 vm/builtin/location.cpp
  24. +57 −27 vm/builtin/module.cpp
  25. +7 −8 vm/builtin/module.hpp
  26. +8 −6 vm/builtin/object.cpp
  27. +0 −1 vm/builtin/staticscope.cpp
  28. +17 −9 vm/builtin/system.cpp
  29. +5 −0 vm/builtin/system.hpp
  30. +1 −2 vm/builtin/variable_scope.cpp
  31. +15 −16 vm/call_frame.cpp
  32. +1 −1 vm/capi/class.cpp
  33. +1 −1 vm/capi/module.cpp
  34. +1 −1 vm/environment.cpp
  35. +4 −2 vm/exception.cpp
  36. +1 −1 vm/gc/marksweep.cpp
  37. +4 −9 vm/helpers.cpp
  38. +3 −3 vm/inline_cache.cpp
  39. +9 −2 vm/instruments/rbxti.cpp
  40. +8 −8 vm/llvm/inline.cpp
  41. +1 −1 vm/llvm/jit_operations.hpp
  42. +1 −1 vm/llvm/state.cpp
  43. +3 −13 vm/ontology.cpp
  44. +1 −1 vm/test/test_access_variable.hpp
  45. +1 −1 vm/test/test_class.hpp
  46. +1 −1 vm/test/test_module.hpp
View
@@ -37,5 +37,36 @@ def self.object_equal(a, b)
Rubinius.primitive :vm_object_equal
raise PrimitiveFailure, "Rubinius::Type.object_equal failed"
end
+
+ def self.module_name(mod)
+ Rubinius.primitive :vm_get_module_name
+ raise PrimitiveFailure, "Rubinius::Type.module_name failed"
+ end
+
+ def self.module_inspect(mod)
+ sc = singleton_class_object mod
+
+ if sc
+ case sc
+ when Class, Module
+ name = "#<Class:#{module_inspect(sc)}>"
+ else
+ cls = object_class sc
+ name = "#<Class:#<#{module_name(cls)}:0x#{sc.object_id.to_s(16)}>>"
+ end
+ else
+ name = module_name mod
+ if !name or name == ""
+ name = "#<#{object_class(mod)}:0x#{mod.object_id.to_s(16)}>"
+ end
+ end
+
+ name
+ end
+
+ def self.set_module_name(mod, name, under)
+ Rubinius.primitive :vm_set_module_name
+ raise PrimitiveFailure, "Rubinius::Type.set_module_name failed"
+ end
end
end
View
@@ -68,22 +68,4 @@ def inherited(name)
end
private :inherited
- def to_s
- if obj = Rubinius::Type.singleton_class_object(self)
- case obj
- when Class
- "#<Class:#{obj.__describe__}>"
- when Module
- "#<Class:#{obj}>"
- else
- "#<Class: #<#{obj.class.__name__}:#{obj.object_id}>>"
- end
- else
- @module_name ? @module_name.to_s : super
- end
- end
-
- alias_method :inspect, :to_s
- alias_method :__describe__, :to_s
-
end
View
@@ -9,7 +9,6 @@ def initialize(sclass=Object, name=nil, under=nil)
# Things (rails) depend on the fact that a normal class is in the constant
# table and have a name BEFORE inherited is run.
- set_name_if_necessary name, under if name and under
under.const_set name, self if under
super()
View
@@ -9,7 +9,6 @@ def initialize(sclass=Object, name=nil, under=nil)
# Things (rails) depend on the fact that a normal class is in the constant
# table and have a name BEFORE inherited is run.
- set_name_if_necessary name, under if name and under
under.const_set name, self if under
if sclass
View
@@ -4,7 +4,9 @@ class Object
def __marshal__(ms, strip_ivars = false)
out = ms.serialize_extended_object self
out << "o"
- out << ms.serialize(self.__class__.__name__.to_sym)
+ cls = Rubinius::Type.object_class self
+ name = Rubinius::Type.module_inspect cls
+ out << ms.serialize(name.to_sym)
out << ms.serialize_instance_variables_suffix(self, true, strip_ivars)
end
end
@@ -847,7 +849,9 @@ def serialize_user_marshal(obj)
add_object val
- Rubinius.binary_string("U#{serialize(obj.class.__name__.to_sym)}#{val.__marshal__(self)}")
+ cls = Rubinius::Type.object_class obj
+ name = Rubinius::Type.module_inspect cls
+ Rubinius.binary_string("U#{serialize(name.to_sym)}#{val.__marshal__(self)}")
end
def set_instance_variables(obj)
View
@@ -85,18 +85,11 @@ def class_variables
end
def name
- @module_name ? @module_name.to_s : ""
- end
-
- alias_method :__name__, :name
-
- def __path__
- return @module_name if @module_name
- inspect
+ Rubinius::Type.module_name self
end
def to_s
- @module_name ? @module_name.to_s : super
+ Rubinius::Type.module_inspect self
end
alias_method :inspect, :to_s
@@ -449,11 +442,12 @@ def module_exec(*args, &prc)
alias_method :class_exec, :module_exec
def const_set(name, value)
- if Rubinius::Type.object_kind_of?(value, Module)
- value.set_name_if_necessary(name, self)
+ name = Rubinius::Type.coerce_to_constant_name name
+
+ if Rubinius::Type.object_kind_of? value, Module
+ Rubinius::Type.set_module_name value, name, self
end
- name = normalize_const_name(name)
@constant_table[name] = value
Rubinius.inc_global_serial
@@ -469,7 +463,8 @@ def const_set(name, value)
# with the name.
def const_missing(name)
- raise NameError, "Missing or uninitialized constant: #{self.__name__}::#{name}"
+ mod_name = Rubinius::Type.module_name self
+ raise NameError, "Missing or uninitialized constant: #{mod_name}::#{name}"
end
def <(other)
@@ -527,16 +522,6 @@ def ===(inst)
raise PrimitiveFailure, "Module#=== primitive failed"
end
- def set_name_if_necessary(name, mod)
- return if @module_name
-
- if mod == Object
- @module_name = name.to_sym
- else
- @module_name = "#{mod.__path__}::#{name}".to_sym
- end
- end
-
# Is an autoload trigger defined for the given path?
def autoload?(name)
name = name.to_sym
@@ -557,7 +542,8 @@ def remove_const(name)
sym = name.to_sym
unless constant_table.has_key?(sym)
- raise NameError, "Missing or uninitialized constant: #{self.__name__}::#{name}"
+ mod_name = Rubinius::Type.module_name self
+ raise NameError, "Missing or uninitialized constant: #{mod_name}::#{name}"
end
val = constant_table.delete(sym)
@@ -580,18 +566,6 @@ def method_added(name)
private :method_added
- def normalize_const_name(name)
- name = Rubinius::Type.coerce_to_symbol(name)
-
- unless name.is_constant?
- raise NameError, "wrong constant name #{name}"
- end
-
- name
- end
-
- private :normalize_const_name
-
def initialize_copy(other)
@method_table = other.method_table.dup
@@ -3,7 +3,7 @@
class Module
def const_get(name)
- name = normalize_const_name(name)
+ name = Rubinius::Type.coerce_to_constant_name name
current, constant = self, undefined
@@ -29,7 +29,7 @@ def const_get(name)
end
def const_defined?(name)
- name = normalize_const_name(name)
+ name = Rubinius::Type.coerce_to_constant_name name
return true if @constant_table.has_key? name
# a silly special case
@@ -68,7 +68,7 @@ def autoload(name, path)
return if Rubinius::CodeLoader.feature_provided?(path)
- name = normalize_const_name(name)
+ name = Rubinius::Type.coerce_to_constant_name name
if existing = @constant_table[name]
if existing.kind_of? Autoload
View
@@ -2,9 +2,8 @@
class Module
-
def const_get(name, inherit = true)
- name = normalize_const_name(name)
+ name = Rubinius::Type.coerce_to_constant_name name
current, constant = self, undefined
@@ -34,7 +33,7 @@ def const_get(name, inherit = true)
end
def const_defined?(name, search_parents=true)
- name = normalize_const_name(name)
+ name = Rubinius::Type.coerce_to_constant_name name
return true if @constant_table.has_key? name
# a silly special case
@@ -57,10 +56,6 @@ def const_defined?(name, search_parents=true)
return false
end
- def name
- @module_name ? @module_name.to_s : nil
- end
-
def attr(*attributes)
vis = Rubinius::VariableScope.of_sender.method_visibility
@@ -97,7 +92,7 @@ def autoload(name, path)
return if Rubinius::CodeLoader.feature_provided?(path)
- name = normalize_const_name(name)
+ name = Rubinius::Type.coerce_to_constant_name name
if existing = @constant_table[name]
if existing.kind_of? Autoload
View
@@ -102,6 +102,16 @@ def self.each_ancestor(mod)
end
end
+ def self.coerce_to_constant_name(name)
+ name = Rubinius::Type.coerce_to_symbol(name)
+
+ unless name.is_constant?
+ raise NameError, "wrong constant name #{name}"
+ end
+
+ name
+ end
+
# Taint host if source is tainted.
def self.infect(host, source)
Rubinius.primitive :object_infect
View
@@ -50,7 +50,6 @@ def self.open_module_under(name, mod)
if !found
# Create the module
obj = Module.new
- obj.set_name_if_necessary name, mod
mod.const_set name, obj
else
obj = tbl[name]
@@ -60,7 +59,6 @@ def self.open_module_under(name, mod)
# See comment above about autoload returning nil
unless obj
obj = Module.new
- obj.set_name_if_necessary name, mod
mod.const_set name, obj
end
end
View
@@ -14,7 +14,7 @@ def unwrap_block_arg(arg)
module_function :unwrap_block_arg
def find_constant_for_op_asign_or(name, scope)
- name = normalize_const_name(name)
+ name = Rubinius::Type.coerce_to_constant_name name
current, constant = scope.module, undefined
@@ -18,10 +18,9 @@ namespace rubinius {
void AccessVariable::init(STATE) {
// HACK test superclass of AccessVariable
- GO(access_variable).set(ontology::new_class(state,
+ GO(access_variable).set(ontology::new_class(state,
"AccessVariable", G(executable), G(rubinius)));
G(access_variable)->set_object_type(state, AccessVariableType);
- G(access_variable)->name(state, state->symbol("Rubinius::AccessVariable"));
}
AccessVariable* AccessVariable::allocate(STATE) {
View
@@ -9,7 +9,6 @@ namespace rubinius {
"AtomicReference", G(object), G(rubinius)));
G(atomic_ref)->set_object_type(state, AtomicReferenceType);
- G(atomic_ref)->name(state, state->symbol("Rubinius::AtomicReference"));
}
AtomicReference* AtomicReference::allocate(STATE) {
@@ -45,7 +45,6 @@ namespace rubinius {
GO(blokenv).set(ontology::new_class(state, "BlockEnvironment", G(object),
G(rubinius)));
G(blokenv)->set_object_type(state, BlockEnvironmentType);
- G(blokenv)->name(state, state->symbol("Rubinius::BlockEnvironment"));
}
BlockEnvironment* BlockEnvironment::allocate(STATE) {
View
@@ -22,7 +22,6 @@ namespace rubinius {
GO(bytearray).set(ontology::new_class_under(state,
"ByteArray", G(rubinius)));
G(bytearray)->set_object_type(state, ByteArrayType);
- G(bytearray)->name(state, state->symbol("Rubinius::ByteArray"));
}
ByteArray* ByteArray::create(STATE, native_int bytes) {
View
@@ -29,7 +29,6 @@ namespace rubinius {
GO(channel).set(ontology::new_class(state, "Channel",
G(object), G(rubinius)));
G(channel)->set_object_type(state, Channel::type);
- G(channel)->name(state, state->symbol("Rubinius::Channel"));
}
Channel* Channel::create(STATE) {
View
@@ -30,7 +30,7 @@ namespace rubinius {
cls->init(state->shared().inc_class_count(state));
- cls->name(state, nil<Symbol>());
+ cls->module_name(state, nil<Symbol>());
cls->instance_type(state, super->instance_type());
if(super->type_info()->type == PackedObject::type) {
@@ -347,12 +347,13 @@ namespace rubinius {
std::string name;
if(mod) {
- name = mod->name()->nil_p() ? "<anonymous>" : mod->name()->debug_str(state);
+ name = mod->debug_str(state);
} else {
name = "<some object>";
}
- std::cout << "#<SingletonClass:" << self->class_object(state)->name()->debug_str(state) <<
+ std::cout << "#<SingletonClass:" <<
+ self->class_object(state)->debug_str(state) <<
" " << name << ":" << (void*)self << ">" << std::endl;
}
}
@@ -17,7 +17,6 @@ namespace rubinius {
GO(compactlookuptable).set(ontology::new_class(state, "CompactLookupTable",
G(tuple), G(rubinius)));
G(compactlookuptable)->set_object_type(state, CompactLookupTableType);
- G(compactlookuptable)->name(state, state->symbol("Rubinius::CompactLookupTable"));
}
CompactLookupTable* CompactLookupTable::create(STATE) {
@@ -40,10 +40,9 @@
namespace rubinius {
void CompiledMethod::init(STATE) {
- GO(cmethod).set(ontology::new_class(state,
+ GO(cmethod).set(ontology::new_class(state,
"CompiledMethod", G(executable), G(rubinius)));
G(cmethod)->set_object_type(state, CompiledMethodType);
- G(cmethod)->name(state, state->symbol("Rubinius::CompiledMethod"));
}
CompiledMethod* CompiledMethod::create(STATE) {
Oops, something went wrong.

0 comments on commit 63ae26d

Please sign in to comment.