basic send
This commit is contained in:
parent
e92f949a71
commit
ce3505710a
@ -4,4 +4,42 @@ Here we have a placeholder for things i am currently developing.
|
|||||||
|
|
||||||
Basically Parfait is the smallest amount of code needed to make a ruby-like OO system work.
|
Basically Parfait is the smallest amount of code needed to make a ruby-like OO system work.
|
||||||
|
|
||||||
A work in progress that started from here : http://salama.github.io/2014/06/10/more-clarity.html
|
A work in progress that started from here : http://salama.github.io/2014/06/10/more-clarity.html went on here
|
||||||
|
http://salama.github.io/2014/07/05/layers-vs-passes.html
|
||||||
|
|
||||||
|
And i finally came to the conclusion that Parfait is the ruby runtime. Aha
|
||||||
|
|
||||||
|
Run - time
|
||||||
|
|
||||||
|
not compile - time
|
||||||
|
|
||||||
|
always mixing those up: As such it is not loaded at compile time.
|
||||||
|
|
||||||
|
A step back: the code (program) we compile runs at run - time.
|
||||||
|
And so does parfait. So all we have to do is compile it with the program.
|
||||||
|
|
||||||
|
And thus parfait can be used at run-time.
|
||||||
|
|
||||||
|
It's too simple: just slips off the mind like a fish into water.
|
||||||
|
|
||||||
|
#### Example: Message send
|
||||||
|
|
||||||
|
I felt a little stupid that it took me so long to notice that sending a message is very closely relateed to the
|
||||||
|
existing ruby method Object.send
|
||||||
|
|
||||||
|
Off course object.send takes symbol and the arguments and has the receiver, so all the elements of our Messaage are there.
|
||||||
|
And the process that Object.send needs to do is exactly that: send that message, ie find the correct method according to
|
||||||
|
the old walk up the inheritance tree rules and dispatch it.
|
||||||
|
|
||||||
|
And as all this happens at runtime, "all" we have to do is code this logic. And since it is at runtime, we can do it in ruby
|
||||||
|
(as i said, this get's compiled and run, just like the program).
|
||||||
|
|
||||||
|
But what about the infinite loop problem:
|
||||||
|
|
||||||
|
There was a little step left out: Off course the method gets compiled at compile-time and so we don't just blindly dispatch:
|
||||||
|
we catch the simple cases that we know about: layout, type instance variables and compile time known functions. Part of
|
||||||
|
those are some that we just don't allow to be overridden.
|
||||||
|
|
||||||
|
Also what in ruby is object.send is Message.send in salama, as it is the message we are sending and which defines all the
|
||||||
|
data we need (not the object). The object receives, it does not send.
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# this is not a "normal" ruby file, ie it is not required by salama
|
# this is not a "normal" ruby file, ie it is not required by salama
|
||||||
# instead it is parsed by salama to define part of the salama that runs
|
# instead it is parsed by salama to define part of the program that runs
|
||||||
|
|
||||||
class Array < BaseObject
|
class Array < BaseObject
|
||||||
def initialize size
|
def initialize size
|
||||||
|
24
lib/parfait/message.rb
Normal file
24
lib/parfait/message.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# this is not a "normal" ruby file, ie it is not required by salama
|
||||||
|
# instead it is parsed by salama to define part of the program that runs
|
||||||
|
|
||||||
|
class Message
|
||||||
|
def send
|
||||||
|
# Find the method for the given object (receiver) according to ruby dispatch rules:
|
||||||
|
# - see if the receiver object has a (singleton) method by the name
|
||||||
|
# - get receivers class and look for instance methods of the name
|
||||||
|
# - go up inheritance tree
|
||||||
|
# - start over with method_missing instead
|
||||||
|
# -> guaranteed to end at object.method_missing
|
||||||
|
method = @receiver.get_singeton_method @method_name
|
||||||
|
unless method
|
||||||
|
cl = @receiver.layout.object_class
|
||||||
|
method = cl.get_instance_or_super_method @method_name
|
||||||
|
end
|
||||||
|
unless method
|
||||||
|
message = Message.new( @receiver , :method_missing , [@method_name] + @args)
|
||||||
|
message.send
|
||||||
|
else
|
||||||
|
method.call
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,2 +1,6 @@
|
|||||||
|
# this is not a "normal" ruby file, ie it is not required by salama
|
||||||
|
# instead it is parsed by salama to define part of the program that runs
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user