2018-05-14 11:55:01 +03:00
|
|
|
module Parfait
|
|
|
|
|
2019-08-06 18:33:27 +03:00
|
|
|
# An Object is conceptually a hash like structure. It is dynamic and
|
2015-05-14 19:54:38 +03:00
|
|
|
# you want to store values by name (instance variable names).
|
|
|
|
#
|
2019-08-06 18:33:27 +03:00
|
|
|
# One could (like mri), store the names in each object, but that is wasteful in both
|
2019-09-30 17:09:13 +03:00
|
|
|
# time and space (time for access, space to store implicitly known names ).
|
2019-08-06 18:33:27 +03:00
|
|
|
# Instead we store only the values, and access them by index (bit like c++).
|
2016-02-25 12:03:11 -08:00
|
|
|
# The Type allows the mapping of names to index.
|
2015-04-08 20:24:50 +03:00
|
|
|
|
2019-08-06 18:33:27 +03:00
|
|
|
# The Type of an object describes the memory layout of the object. In a c analogy,
|
|
|
|
# it is the information defined in a struct.
|
2016-02-25 12:03:11 -08:00
|
|
|
# The Type is a list of the names of instance variables, and their value types (int etc).
|
2015-05-11 18:55:49 +03:00
|
|
|
#
|
2016-02-25 12:03:11 -08:00
|
|
|
# Every object has a Type to describe it, so it's *first* instance variable is **always**
|
|
|
|
# "type". This means the name "type" is the first name in the list
|
|
|
|
# for every Type instance.
|
2015-05-17 14:40:02 +03:00
|
|
|
|
2016-02-25 12:03:11 -08:00
|
|
|
# But, as we want every Object to have a class, the Type carries that class.
|
2019-09-30 17:09:13 +03:00
|
|
|
# So the type of type has an entry "object_class", ir Type has an instance object_class.
|
2015-04-08 20:24:50 +03:00
|
|
|
|
2015-10-26 14:33:36 +02:00
|
|
|
# But Objects must also be able to carry methods themselves (ruby calls singleton_methods)
|
2019-09-30 17:09:13 +03:00
|
|
|
# and those too are stored in the Type. Those type instances are called singleton
|
|
|
|
# types, in analogy to the singleton classes they represent.
|
|
|
|
# In other words, "usually" a type represents a whole group of objects (instances of a
|
|
|
|
# class at the time the type was the instance_type). But for Singletons, ie objects
|
|
|
|
# that have a singleton class, the type is only for that object.
|
2015-10-26 14:33:36 +02:00
|
|
|
|
2019-09-30 17:09:13 +03:00
|
|
|
# An object is an "List" (memory location) of values of length n
|
2019-08-06 18:33:27 +03:00
|
|
|
# The Type is a list of n names and n types that describe the values stored in an
|
|
|
|
# actual object.
|
2015-05-11 18:55:49 +03:00
|
|
|
# Together they turn the object into a hash like structure
|
2015-04-08 20:24:50 +03:00
|
|
|
|
2019-08-06 18:33:27 +03:00
|
|
|
# For types to be a useful concept, they have to be unique and immutable. Any "change",
|
|
|
|
# like adding a name/type pair, will result in a new type instance.
|
2019-09-30 17:09:13 +03:00
|
|
|
# Type identity can be checked by the hash function, so two types are the same when their
|
|
|
|
# hashes are the same. The hash is made up of hashing all instance names and the class
|
|
|
|
# name.
|
2016-12-06 15:08:29 +02:00
|
|
|
|
2019-09-30 17:09:13 +03:00
|
|
|
# The Space class carries a hash of types of the systems, which is used to ensure that
|
2016-12-06 15:08:29 +02:00
|
|
|
# there is only one instance of every type. Hash and equality are defined on type
|
|
|
|
# for this to work.
|
|
|
|
|
2016-02-25 11:50:10 -08:00
|
|
|
class Type < Object
|
2016-12-31 15:20:02 +02:00
|
|
|
|
2019-09-10 00:18:20 +03:00
|
|
|
attr_reader :object_class , :names , :types , :methods
|
2018-08-11 19:15:34 +03:00
|
|
|
|
|
|
|
def self.type_length
|
2019-09-30 17:09:13 +03:00
|
|
|
6
|
2018-08-11 19:15:34 +03:00
|
|
|
end
|
2015-10-26 12:22:32 +02:00
|
|
|
|
2019-09-30 17:09:13 +03:00
|
|
|
# This is the default way to create new type, because we add it to the
|
|
|
|
# global list, to space.
|
|
|
|
# The hash (actually the keys of the hash) and the object_class define the
|
|
|
|
# identity of the type, which can be checked with the hash function.
|
|
|
|
# single is by default 0, meaning you have to specify explicitly (1) for
|
|
|
|
# it to be a "singleton" type (see class description)
|
|
|
|
def self.for_hash( hash , object_class = :Object , single = 0)
|
2019-09-18 22:07:05 +03:00
|
|
|
name = object_class
|
|
|
|
if(object_class.is_a?(Symbol))
|
2019-09-18 22:36:56 +03:00
|
|
|
object_class = Object.object_space.get_class_by_name(object_class)
|
2019-09-18 22:07:05 +03:00
|
|
|
end
|
|
|
|
raise "No such class #{name}" unless object_class
|
2016-12-30 19:17:59 +02:00
|
|
|
hash = {type: object_class.name }.merge(hash) unless hash[:type]
|
2019-09-30 17:09:13 +03:00
|
|
|
new_type = Type.new( object_class , hash , single)
|
2019-09-18 22:36:56 +03:00
|
|
|
Object.object_space.add_type(new_type)
|
2016-12-08 12:50:25 +02:00
|
|
|
end
|
|
|
|
|
2019-09-18 22:07:05 +03:00
|
|
|
# should not be called directly. Use Type.for_hash instead, that adds the
|
2019-09-30 17:09:13 +03:00
|
|
|
# type to the global list and does sym->class conversion if neccessary
|
|
|
|
def initialize( object_class , hash , single )
|
2015-05-20 17:17:11 +03:00
|
|
|
super()
|
2019-09-30 17:09:13 +03:00
|
|
|
@is_single = single
|
2019-09-23 00:07:30 +03:00
|
|
|
@object_class = object_class
|
2019-09-09 20:26:54 +03:00
|
|
|
@methods = nil
|
|
|
|
@names = List.new
|
|
|
|
@types = List.new
|
2016-12-30 18:41:36 +02:00
|
|
|
raise "No type Type in #{hash}" unless hash[:type]
|
|
|
|
private_add_instance_variable(:type , hash[:type]) #first
|
2019-09-09 20:26:54 +03:00
|
|
|
hash.keys.each do |name |
|
|
|
|
private_add_instance_variable(name , hash[name]) unless name == :type
|
2016-12-27 20:34:11 +02:00
|
|
|
end
|
2016-12-14 13:21:55 +02:00
|
|
|
end
|
|
|
|
|
2019-09-30 17:09:13 +03:00
|
|
|
def is_single?
|
|
|
|
@is_single == 1
|
|
|
|
end
|
|
|
|
|
2018-07-14 11:03:16 +03:00
|
|
|
def class_name
|
2019-09-23 00:07:30 +03:00
|
|
|
@object_class&.name
|
2018-07-14 11:03:16 +03:00
|
|
|
end
|
|
|
|
|
2016-12-27 20:34:11 +02:00
|
|
|
def to_s
|
2018-07-27 12:16:06 +03:00
|
|
|
str = "#{class_name}-["
|
|
|
|
first = false
|
2019-09-23 00:07:30 +03:00
|
|
|
@names.each do |name|
|
2018-07-27 12:16:06 +03:00
|
|
|
unless(first)
|
|
|
|
first = true
|
|
|
|
str += ":#{name}"
|
|
|
|
else
|
|
|
|
str += ", :#{name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
str + "]"
|
2016-12-27 20:34:11 +02:00
|
|
|
end
|
|
|
|
|
2016-12-14 13:21:55 +02:00
|
|
|
def method_names
|
|
|
|
names = List.new
|
2019-09-09 20:26:54 +03:00
|
|
|
return names unless @methods
|
2019-09-10 12:33:57 +03:00
|
|
|
@methods.each_method do |method|
|
2016-12-14 13:21:55 +02:00
|
|
|
names.push method.name
|
|
|
|
end
|
|
|
|
names
|
|
|
|
end
|
|
|
|
|
2018-03-18 22:09:27 +05:30
|
|
|
def create_method( method_name , arguments , frame)
|
2016-12-30 13:33:07 +02:00
|
|
|
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a?(Symbol)
|
2016-12-14 13:21:55 +02:00
|
|
|
#puts "Self: #{self.class} clazz: #{clazz.name}"
|
2018-03-22 21:06:22 +05:30
|
|
|
raise "frame must be a type, not:#{frame}" unless frame.is_a?(Type)
|
2017-01-17 21:25:18 +02:00
|
|
|
found = get_method( method_name )
|
2018-03-22 21:06:22 +05:30
|
|
|
if found
|
2018-03-24 12:21:46 +02:00
|
|
|
#puts "redefining method #{method_name}" #TODO, this surely must get more complicated
|
2018-07-06 20:01:17 +03:00
|
|
|
raise "attempt to redifine method for different type " unless self == found.self_type
|
2018-03-22 21:06:22 +05:30
|
|
|
found.init(arguments , frame)
|
|
|
|
return found
|
|
|
|
else
|
2018-07-30 10:21:43 +03:00
|
|
|
add_method CallableMethod.new( method_name , self , arguments , frame )
|
2018-03-18 22:09:27 +05:30
|
|
|
end
|
2016-12-14 13:21:55 +02:00
|
|
|
end
|
|
|
|
|
2016-12-30 13:33:07 +02:00
|
|
|
def add_method( method )
|
2018-07-07 09:11:09 +03:00
|
|
|
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? CallableMethod
|
2016-12-14 13:21:55 +02:00
|
|
|
raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
|
2018-07-06 20:01:17 +03:00
|
|
|
if self.is_a?(Class) and (method.self_type != self)
|
2016-12-14 13:21:55 +02:00
|
|
|
raise "Adding to wrong class, should be #{method.for_class}"
|
|
|
|
end
|
2018-04-02 16:36:43 +03:00
|
|
|
if get_method( method.name )
|
|
|
|
remove_method(method.name)
|
2016-12-14 13:21:55 +02:00
|
|
|
end
|
2019-09-10 12:33:57 +03:00
|
|
|
method.set_next( @methods )
|
2019-09-09 20:26:54 +03:00
|
|
|
@methods = method
|
2019-09-21 18:07:58 +03:00
|
|
|
# puts "ADD method to #{self.inspect}:#{method.name}"
|
2016-12-14 13:21:55 +02:00
|
|
|
method
|
|
|
|
end
|
|
|
|
|
2016-12-30 13:33:07 +02:00
|
|
|
def remove_method( method_name )
|
2019-09-10 12:33:57 +03:00
|
|
|
raise "No such method #{method_name} in #{self.name}" unless @methods
|
|
|
|
if( @methods.name == method_name)
|
|
|
|
@methods = @methods.next_callable
|
2018-04-02 16:36:43 +03:00
|
|
|
return true
|
|
|
|
end
|
2019-09-10 12:33:57 +03:00
|
|
|
method = @methods
|
2018-08-12 13:10:44 +03:00
|
|
|
while(method && method.next_callable)
|
|
|
|
if( method.next_callable.name == method_name)
|
|
|
|
method.set_next( method.next_callable.next_callable )
|
2018-04-02 16:36:43 +03:00
|
|
|
return true
|
|
|
|
else
|
2018-08-12 13:10:44 +03:00
|
|
|
method = method.next_callable
|
2018-04-02 16:36:43 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
raise "No such method #{method_name} in #{self.name}"
|
2016-12-14 13:21:55 +02:00
|
|
|
end
|
|
|
|
|
2016-12-30 13:33:07 +02:00
|
|
|
def get_method( fname )
|
|
|
|
raise "get_method #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
|
2019-09-10 12:33:57 +03:00
|
|
|
return nil unless @methods
|
|
|
|
@methods.each_method do |m|
|
2016-12-14 13:21:55 +02:00
|
|
|
return m if(m.name == fname )
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2018-04-02 16:36:43 +03:00
|
|
|
def methods_length
|
2019-09-10 12:33:57 +03:00
|
|
|
return 0 unless @methods
|
2018-04-02 16:36:43 +03:00
|
|
|
len = 0
|
2019-09-10 12:33:57 +03:00
|
|
|
@methods.each_method { len += 1}
|
2018-04-02 16:36:43 +03:00
|
|
|
return len
|
|
|
|
end
|
|
|
|
|
2015-06-03 10:01:59 +03:00
|
|
|
def == other
|
|
|
|
self.object_id == other.object_id
|
|
|
|
end
|
|
|
|
|
2015-05-19 20:29:33 +03:00
|
|
|
# add the name of an instance variable
|
2016-12-08 12:50:25 +02:00
|
|
|
# Type objects are immutable, so a new object is returned
|
|
|
|
# As types are also unique, two same adds will result in identical results
|
|
|
|
def add_instance_variable( name , type )
|
2016-12-13 18:46:03 +02:00
|
|
|
raise "No nil name" unless name
|
|
|
|
raise "No nil type" unless type
|
2019-09-19 15:48:27 +03:00
|
|
|
return self if @names.index_of(name)
|
2016-12-08 12:50:25 +02:00
|
|
|
hash = to_hash
|
|
|
|
hash[name] = type
|
2019-09-18 22:07:05 +03:00
|
|
|
return Type.for_hash( hash , object_class)
|
2015-05-16 14:01:48 +03:00
|
|
|
end
|
|
|
|
|
2015-10-26 12:57:54 +02:00
|
|
|
def instance_length
|
2019-09-10 12:33:57 +03:00
|
|
|
@names.get_length()
|
2015-10-22 11:02:46 +03:00
|
|
|
end
|
|
|
|
|
2015-11-18 15:36:43 +02:00
|
|
|
# index of the variable when using get_internal_word
|
2018-05-14 11:55:01 +03:00
|
|
|
# (get_internal_word is 0 based and 0 is always the type)
|
2016-12-13 18:46:03 +02:00
|
|
|
def variable_index( name )
|
2019-09-10 12:33:57 +03:00
|
|
|
has = @names.index_of(name)
|
2015-10-17 10:03:39 +03:00
|
|
|
return nil unless has
|
2018-05-14 11:55:01 +03:00
|
|
|
raise "internal error #{name}:#{has}" if has < 0
|
2016-12-29 18:42:38 +02:00
|
|
|
has
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_length()
|
2019-09-10 12:33:57 +03:00
|
|
|
@names.get_length()
|
2016-12-29 18:42:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def name_at( index )
|
2019-09-10 12:33:57 +03:00
|
|
|
raise "No names #{index}" unless @names
|
|
|
|
@names.get(index)
|
2015-07-21 15:40:25 +03:00
|
|
|
end
|
|
|
|
|
2016-12-13 18:46:03 +02:00
|
|
|
def type_at( index )
|
2019-09-10 12:33:57 +03:00
|
|
|
@types.get(index)
|
2015-11-09 23:28:10 +02:00
|
|
|
end
|
|
|
|
|
2018-07-15 12:30:50 +03:00
|
|
|
def type_for( name )
|
|
|
|
index = variable_index(name)
|
|
|
|
return nil unless index
|
|
|
|
type_at(index)
|
|
|
|
end
|
|
|
|
|
2015-10-13 14:46:07 +03:00
|
|
|
def inspect
|
2019-09-10 12:33:57 +03:00
|
|
|
"Type[#{@names.inspect}]"
|
2015-10-13 14:46:07 +03:00
|
|
|
end
|
|
|
|
|
2018-05-14 11:55:01 +03:00
|
|
|
def rxf_reference_name
|
2019-09-10 12:33:57 +03:00
|
|
|
"#{@object_class.name}_Type"
|
2015-06-19 19:50:53 +03:00
|
|
|
end
|
2018-05-14 11:55:01 +03:00
|
|
|
alias :name :rxf_reference_name
|
2015-10-26 17:24:28 +02:00
|
|
|
|
2016-12-31 14:51:06 +02:00
|
|
|
def each
|
2018-05-14 11:55:01 +03:00
|
|
|
index = 0
|
|
|
|
while( index < get_length() )
|
2016-12-31 14:51:06 +02:00
|
|
|
yield( name_at(index) , type_at(index) )
|
|
|
|
index += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-02 16:36:43 +03:00
|
|
|
def each_method(&block)
|
2019-09-10 12:33:57 +03:00
|
|
|
return unless @methods
|
|
|
|
@methods.each_method(&block)
|
2018-04-02 16:36:43 +03:00
|
|
|
end
|
2019-09-10 12:33:57 +03:00
|
|
|
|
2016-12-08 12:50:25 +02:00
|
|
|
def to_hash
|
2016-12-31 14:51:06 +02:00
|
|
|
hash = {}
|
|
|
|
each do |name , type|
|
2018-05-14 11:55:01 +03:00
|
|
|
raise "Name nil #{type}" unless name
|
|
|
|
raise "Type nil #{name}" unless type
|
2016-12-31 14:51:06 +02:00
|
|
|
hash[name] = type
|
2016-12-08 12:50:25 +02:00
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2016-12-07 23:35:51 +02:00
|
|
|
def hash
|
2016-12-31 14:51:06 +02:00
|
|
|
index = 1
|
2019-09-22 19:10:47 +03:00
|
|
|
name = object_class.is_a?(Symbol) ? object_class : object_class.name
|
|
|
|
hash_code = Type.str_hash(name)
|
2016-12-31 14:51:06 +02:00
|
|
|
each do |name , type|
|
|
|
|
item_hash = Type.str_hash(name) + Type.str_hash(type)
|
|
|
|
hash_code += item_hash + (item_hash / 256 ) * index
|
|
|
|
index += 1
|
|
|
|
end
|
2016-12-31 19:53:43 +02:00
|
|
|
hash_code % (2 ** 62)
|
2016-12-31 14:51:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.str_hash(str)
|
|
|
|
if RUBY_ENGINE == 'opal'
|
|
|
|
hash = 5381
|
|
|
|
str.to_s.each_char do |c|
|
|
|
|
hash = ((hash << 5) + hash) + c.to_i; # hash * 33 + c without getting bignums
|
|
|
|
end
|
|
|
|
hash % (2 ** 51)
|
|
|
|
else
|
|
|
|
str.hash
|
|
|
|
end
|
2016-12-08 12:50:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def private_add_instance_variable( name , type)
|
|
|
|
raise "Name shouldn't be nil" unless name
|
|
|
|
raise "Value Type shouldn't be nil" unless type
|
2019-09-10 12:33:57 +03:00
|
|
|
@names.push(name)
|
|
|
|
@types.push(type)
|
2016-12-07 23:35:51 +02:00
|
|
|
end
|
2016-12-08 12:50:25 +02:00
|
|
|
|
2015-05-11 18:55:49 +03:00
|
|
|
end
|
2015-04-08 20:24:50 +03:00
|
|
|
end
|