rework type with separate arrays for names and types

Means no more indexed
also using ruby instances
small rename for names
This commit is contained in:
Torsten Ruger 2016-12-29 18:42:38 +02:00
parent 4c3007e6c0
commit f4b3c645e5

View File

@ -34,9 +34,10 @@
module Parfait module Parfait
class Type < Object class Type < Object
attributes [:object_class , :instance_methods] def self.attributes
include Indexed [:names , :types , :object_class , :instance_methods ]
self.offset(3) end
attr_reader :object_class , :names , :types , :instance_methods
def self.for_hash( object_class , hash) def self.for_hash( object_class , hash)
new_type = Type.new( object_class , hash) new_type = Type.new( object_class , hash)
@ -74,22 +75,28 @@ module Parfait
def initialize( object_class , hash = {}) def initialize( object_class , hash = {})
super() super()
set_object_class( object_class)
init_lists( hash )
end
# this part of the init is seperate because at boot time we can not use normal new
# new is overloaded to grab the type from space, and before boot, that is not set up
def init_lists(hash)
@instance_methods = List.new
@names = List.new
@types = List.new
private_add_instance_variable :type ,:Type private_add_instance_variable :type ,:Type
self.object_class = object_class
hash.each do |name , type| hash.each do |name , type|
private_add_instance_variable(name , type) unless name == :type private_add_instance_variable(name , type) unless name == :type
end end
self.instance_methods = List.new
end end
def to_s def to_s
"" "#{@object_class.name}:#{@names.inspect}"
end end
def methods def methods
m = self.instance_methods @instance_methods
return m if m
self.instance_methods = List.new
end end
def method_names def method_names
@ -104,7 +111,7 @@ module Parfait
raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol) raise "create_instance_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
#puts "Self: #{self.class} clazz: #{clazz.name}" #puts "Self: #{self.class} clazz: #{clazz.name}"
type = arguments type = arguments
type = Parfait::Type.for_hash( self.object_class , arguments) if arguments.is_a?(Hash) type = Parfait::Type.for_hash( @object_class , arguments) if arguments.is_a?(Hash)
add_instance_method TypedMethod.new( self , method_name , type ) add_instance_method TypedMethod.new( self , method_name , type )
end end
@ -159,54 +166,53 @@ module Parfait
if existing if existing
return existing return existing
else else
return Type.for_hash( object_class , hash) return Type.for_hash( @object_class , hash)
end end
end end
def instance_names def set_object_class(oc)
names = List.new raise "object class should be a class, not #{oc.class}" unless oc.is_a?(Class)
each_pair do |name , type| @object_class = oc
names.push(name)
end
names
end end
def instance_length def instance_length
(self.get_length / 2).to_i # to_i for opal @names.get_length()
end
alias :super_index :index_of
def index_of(name)
raise "Use variable_index instead"
end end
# index of the variable when using get_internal_word # index of the variable when using get_internal_word
# (get_internal_word is 1 based and 1 is always the type) # (get_internal_word is 1 based and 1 is always the type)
def variable_index( name ) def variable_index( name )
has = super_index(name) has = names.index_of(name)
return nil unless has return nil unless has
raise "internal error #{name}:#{has}" if has < 1 raise "internal error #{name}:#{has}" if has < 1
(1 + has / 2).to_i # to_i for opal has
end
def get_length()
@names.get_length()
end
def name_at( index )
@names.get(index)
end end
def type_at( index ) def type_at( index )
type_index = index * 2 @types.get(index)
get(type_index)
end end
def inspect def inspect
"Type[#{super}]" "Type[#{names.inspect}]"
end end
def sof_reference_name def sof_reference_name
"#{self.object_class.name}_Type" "#{@object_class.name}_Type"
end end
alias :name :sof_reference_name alias :name :sof_reference_name
def to_hash def to_hash
hash = Dictionary.new hash = Dictionary.new
each_pair do |name, type | @names.each_with_index do |name, index |
hash[name] = type hash[name] = @types[index]
end end
hash hash
end end
@ -220,8 +226,8 @@ module Parfait
def private_add_instance_variable( name , type) def private_add_instance_variable( name , type)
raise "Name shouldn't be nil" unless name raise "Name shouldn't be nil" unless name
raise "Value Type shouldn't be nil" unless type raise "Value Type shouldn't be nil" unless type
self.push(name) @names.push(name)
self.push(type) @types.push(type)
end end
end end