Using MetaClass to compile class methods into

still #24, still wip
This commit is contained in:
Torsten Ruger 2019-02-17 14:37:50 +02:00
parent 3db7707614
commit e430701645
4 changed files with 25 additions and 34 deletions

View File

@ -41,11 +41,6 @@ module Parfait
"MetaClass(#{clazz.name})" "MetaClass(#{clazz.name})"
end end
# no superclass, return nil to signal
def super_class_name
nil
end
def add_method_for(name , type , frame , body ) def add_method_for(name , type , frame , body )
method = Parfait::VoolMethod.new(name , type , frame , body ) method = Parfait::VoolMethod.new(name , type , frame , body )
add_method( method ) add_method( method )
@ -73,35 +68,15 @@ module Parfait
self.instance_type = type self.instance_type = type
end end
# return the super class, but raise exception if either the super class name # Nil name means no superclass, and so nil returned
# or the super classs is nil.
# Use only for non Object base class
def super_class!
raise "No super_class for class #{name}" unless super_class_name
s = super_class
raise "superclass not found for class #{name} (#{super_class_name})" unless s
s
end
# return the super class
# we only store the name, and so have to resolve.
# Nil name means no superclass, and so nil is a valid return value
def super_class def super_class
return nil unless super_class_name return nil
Parfait.object_space.get_class_by_name(super_class_name)
end end
# ruby 2.1 list (just for reference, keep at bottom) # no superclass, return nil to signal
#:allocate, :new, :superclass def super_class_name
nil
# + modules end
# :<, :<=, :>, :>=, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods,
# :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?,
# :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set,
# :class_variable_defined?, :public_constant, :private_constant, :singleton_class?, :include, :prepend,
# :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?,
# :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload,
# :autoload?, :instance_method, :public_instance_method
end end
end end

View File

@ -8,6 +8,7 @@ module Vool
end end
def to_mom(clazz) def to_mom(clazz)
raise "not meta" unless clazz.class == Parfait::MetaClass
raise( "no class in #{self}") unless clazz raise( "no class in #{self}") unless clazz
method = clazz.add_method_for(name , make_arg_type , make_frame , body ) method = clazz.add_method_for(name , make_arg_type , make_frame , body )
compiler = method.compiler_for(clazz.instance_type) compiler = method.compiler_for(clazz.instance_type)

View File

@ -20,10 +20,14 @@ module Vool
def to_mom( _ ) def to_mom( _ )
create_class_object create_class_object
method_compilers = body.statements.collect do |node| method_compilers = body.statements.collect do |node|
unless node.is_a?(MethodStatement) or node.is_a?(ClassMethodStatement) case node
when MethodStatement
node.to_mom(@clazz)
when ClassMethodStatement
node.to_mom(@clazz.meta_class)
else
raise "Only methods for now #{node.class}:#{node}" raise "Only methods for now #{node.class}:#{node}"
end end
node.to_mom(@clazz)
end end
Mom::MomCompiler.new(method_compilers) Mom::MomCompiler.new(method_compilers)
end end

View File

@ -18,11 +18,17 @@ module Parfait
assert @space.false_object , "No lies" assert @space.false_object , "No lies"
assert @space.nil_object , "No nothing" assert @space.nil_object , "No nothing"
end end
def space_class
Parfait.object_space.get_class_by_name(:Space)
end
def test_global_space def test_global_space
assert_equal Parfait::Space , Parfait.object_space.class assert_equal Parfait::Space , Parfait.object_space.class
end end
def test_get_class_by_name def test_get_class_by_name
assert_equal Parfait::Class , Parfait.object_space.get_class_by_name(:Space).class assert_equal Parfait::Class , space_class.class
end
def test_get_meta_class
assert_equal Parfait::MetaClass , space_class.meta_class.class
end end
def test_get_type_by_class_name def test_get_type_by_class_name
assert_equal Parfait::Type , Parfait.object_space.get_type_by_class_name(:Space).class assert_equal Parfait::Type , Parfait.object_space.get_type_by_class_name(:Space).class
@ -88,6 +94,11 @@ module Parfait
end end
end end
end end
def test_all_meta
@space.classes.each do |name , clazz|
assert clazz.meta_class , clazz.name
end
end
def test_has_factory def test_has_factory
assert_equal Dictionary , @space.factories.class assert_equal Dictionary , @space.factories.class
end end