Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created July 2, 2009 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judofyr/139660 to your computer and use it in GitHub Desktop.
Save judofyr/139660 to your computer and use it in GitHub Desktop.
Objects
=======
Here's an object:
+------------+
| Object |
+------------+
If we expand it we can see it contains three parts:
^
|
|
Prototype
|
|
+-----------Object-----------+
| | |
| Members | C++ |
| | |
+--------------+-------------+
- Prototype, a pointer to a object
- Members, a hash table with pointers to objects
- C++ objects in core objects (and C++ extensions)
The C++ objects are the objects which for example holds the content in
an Array or a String. In pure-Snow code there is now C++ objects which
you have control over.
Actions
-------
So there are basically two things we want to do with objects:
When you SomeObject.clone() it allocates a new object, sets the prototype to
SomeObject and copies over the C++ stuff.
When you SomeObject.copy() it allocates a new object, sets the prototype to
SomeObject.prototype and copies over the C++ stuff and the members.
+------------+
| Object | <--\
+------------+ \
\
^ \
| \
| \
Prototype Prototype
| \
| \
+----Base----+ +---Copied---+
| | | |
| #abc | | #abc |
| #foo | | #foo |
| #bar | | #bar |
| | | |
+------------+ +------------+
^
|
|
Prototype
|
|
+---Cloned---+
| |
| (empty) |
| |
+------------+
Base.more: 123
Cloned.more //=> 123
Copied.more //=> Error
Object.even: 123
Base.even //=> 123
Cloned.even //=> 123
Copied.even //=> 123
Object.__call__: Object.clone // Maybe...
Core Objects
------------
- Object
- true (no #copy or #clone)
- false (no #copy or #clone)
- nil (no #copy or #clone)
Classes
=======
Classes are implemented using the object system above:
^
|
|
Prototype
|
|
+----Class---+
| | +------------+ +------------+
| #new | #instance_prototype -> | Object | -- Instance of Class -- > | Object |
| | +------------+ <-- Prototype -- +------------+
+------------+
^
^ |
| |
| |
Prototype Prototype
| |
| |
+--Subclass--+ |
| | +------------+ +------------+
| (empty) | #instance_prototype -> | Object | -- Instance of Subclass -- > | Object |
| | +------------+ <-- Prototype -- +------------+
+------------+
// Pseudo-implementation
Class: Object.clone()
Class.instance_prototype: Object.clone()
Class.extend: { .instance_prototype.object_eval(it) }
Class.allocate: {
.instance_prototype.clone()
}
Class.new: {
instance: .allocate()
instance.initialize()
instance
}
Class.copy: {
klass: .prototype.copy.call_with_self(self)
klass.instance_prototype: klass.instance_prototype.copy()
klass
}
Class.clone: {
klass: .prototype.clone.call_with_self(self)
klass.instance_prototype: klass.instance_prototype.clone()
klass
}
Class.subclass: {
klass: .clone()
klass.extend(it) if it
klass
}
class: {
Class.subclass(it)
}
Core Classes
------------
- Class
- Array
- Hash
- String (no #allocate)
- Numeric (no #allocate)
- Float/Integer are subclasses of Numeric
- Symbol (no #allocate)
- Function (no #allocate)
Class.new() does not return a subclass; that's what Class.subclass is for.
There are no allocators for classes which can only be represented by syntax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment