parrotcode: Object and Class semantics for Parrot | |
Contents | Documentation |
docs/pdds/pdd15_objects.pod - Object and Class semantics for Parrot
This PDD describes the semantics of Parrot's object and class systems.
$Revision$
An object is a value that incorporates both data and behavior related to that data.
A class defines a pattern of characteristics and behaviors from which objects are constructed.
An attribute is a slot in an object that contains a value, generally a PMC. Attributes are referenced by class name/attribute name pairs.
Attributes are set on a class-wide basis, and all the objects of a class will have the same set of attributes. Most OO languages don't allow attribute changes to existing classes, but Parrot's base attribute system does allow it. In order to safely support advanced dynamic features in HLLs, attributes are not accessible via fixed attribute offsets, but only via named lookup.
A method is a piece of code that you invoke by name through an object. Methods implement the behaviour of an object.
Also called the super-class. The parent class is, in an inheritance situation, the class being derived from. If A derives from B, B is the parent class of A.
Also called the sub-class. The child class is, in an inheritance situation, the class doing the deriving. If A derives from B, A is the child class.
A role adds attributes and methods into a class without inheritance.
The composed class retains a list of roles applied to it (so they can be checked with does
),
but otherwise maintains no distinction between composed attributes and methods and those defined in the class.
An object that is transparently (to the user) embedded in another object. Delegate objects are used in those cases where we can't inherit from a class because the class is from a different object universe.
A property is a role that only adds attributes and accessors.
Properties are generally assigned at runtime, and a particular property may or may not exist on a PMC at any particular time. Properties are not restricted to objects as such--any PMC may have a property attached to it.
An interface is a role that only adds methods.
There are four pieces to the object implementation. There are the PMCs for the classes and objects, the opcodes the engine uses to do objecty things, the specific vtable methods used to perform those objecty things, and the supporting code provided by the interpreter engine to do the heavy lifting.
Please note that Parrot, in general, does not restrict operations on objects and classes. If a language has restrictions on what can be done with them, the language is responsible for making sure that disallowed things do not happen. For example, Parrot permits multiple inheritance, and will not stop code that adds a new parent to an existing class. If a language doesn't allow for multiple inheritance it must not emit code which would add multiple parents to a class. (Parrot may, at some point, allow imposition of runtime restrictions on a class, but currently it doesn't)
There are two PMC classes,
Class
and Object
.
Class PMCs hold all the class-specific information.
Instantiating a new OO class creates a new Class PMC,
and enters the new OO class into Parrot's PMC class table,
at which point it is indistinguishable from any other PMC class.
It's important to note that 'standard' classes are Class PMC instances, or instances of a subclass of the Class PMC, and 'standard' objects are Object PMCs. It isn't necessary to create a brand new low-level PMC class for each OO class, and they all share the Class or Object vtable, respectively.
An instance of the Class PMC has ten attributes, which are:
The attribute catalog holds only the attributes defined in a particular class. When instantiating an object, the object data store is created as a ResizablePMCArray, so doesn't need any specific details of the class's attribute structure. As attributes are set in the object (based on the index in the lookup table), the Array expands to accommodate the attribute indexes that are actually used. In the common case, a relatively small set near the lower index range is all that will be used.
When setting the attribute cache it is necessary to scan all parent classes as well as the instantiated class for attributes defined there. The inheritance rules (MRO) for a particular HLL will determine which child class attributes override which parent class attributes. The cache is only set on individual accesses to a particular attribute.
(If a parent class changes its set of attributes, should that change appear in later instantiations of objects from child classes? If so, all of these classes would need to be re-constructed as a result of the change; note that any already instantiated objects would refer to the old class. NOTE: flag old classes with an "updated" status, to notify objects of the old class that they should rebless themselves into the new class next time they access the old class?)
Class PMCs also have the "I am a class" flag set on them.
obj = class.'new'( 'myattrib' => "Foo" )
[Stream-of-consciousness to be cleaned up later] The class registry has a much diminished role in this implementation. Its only responsibility is maintaining a mapping of unique IDs to class objects throughout the system. (We may eventually be able to eliminate the registry altogether.) It can't be used for looking up classes by name, because it's possible to have multiple classes with the same name in the same namespace. (When you extend an existing class, it actually creates a new class, that replaces the old class in the Namespace, but the old class can't be thrown away if it has objects instantiated in it. The old objects still point to the old class and do their method resolution and attribute lookup through that class. If a class hasn't been instantiated, adding a method doesn't create a new class. If it has been instantiated, it creates a new class the first time it's extended, and then doesn't create a new class until it is instantiated again.) The class registry needs to have names removed entirely (it doesn't care about names anymore). Low-level PMC types also need entries in the namespace hierarchy.
The Namespace always points to the most current incarnation of the class. All the class objects that nominally exist in a namespace point to that namespace object.
A class can be garbage collected when it has no instantiatated objects and no Namespace object referencing it. When a class is garbage collected, it should remove itself from the registry.
Object
PMCs are the actual objects, and hold all the per-object instance data.
The Object PMC is an array of meta-information and attributes. The elements of this array are:
A list of the object's attributes is accessible from the class. The attribute cache is the most straightforward way to retrieve a complete list of attributes visible to the object, but the first time you introspect for a complete list the class may have to calculate the list by traversing the inheritance hierarchy.
Object PMCs have the "I am an object" flag set on them.
Object PMCs have no methods aside from those defined in their associated class. They do have vtable methods providing access to certain low-level information about the object, method call functionality, etc. See the sections below on Objects and Vtables.
[stream-of-consciousness again] In addition to a value type, objects can have a container type. The container type can't be stored in the object itself, because a single object may live within multiple containers. So, the container type (when it exists) is stored in the LexPad or Namespace entry for a particular variable.
In a static language like C#.Net: B isa A A o1 = new B(); B o2 = new B();
o1.x; # retrieves A's attribute o2.x; # retrieves B's attribute
o1.foo(); # call's B's method o2.foo(); # call's B's method
An instance of the Role PMC has five attributes, which are:
The following ops are provided to deal with objects. Please note that method calls are governed by Parrot's calling conventions, and as such objects, method PMCs, return continuations, and parameters must be in the right places, though some ops will put parameters where they need to go.
Null
.To make this work all PMCs must have the following vtable entries. They may, for non-objects, throw an exception.
The catalog metadata for objects is considered to be attributes on the class, so to get the offset for a class for an object, you fetch the object's class then look up the offset attribute from it. (The class attributes are detailed later) This is safe in general, since the only code reasonably querying a class' attribute list is the class code itself, and if a class doesn't know whether it's a Class-style class or not you've got bigger problems.
Currently Parrot only supports mutating a class' metainformation for Class classes. This is a restriction which will be lifted at some point soon.
The bytecode is isolated from most of the internal details of the implementation. This allows both for flexibility in the implementation and forward compatibility, generally good things. It also allows for multiple concurrent interoperable object systems. The major thrust is for transparent use of objects, though most class activity (including creation of subclasses and modifications of existing classes) should be transparent as well.
The following examples all assume we're working with basic Object objects and Class classes.
To create a new class Foo
which has no parent classes:
newclass $P0, "Foo"
To create a class Foo
with the parents A
and B
, the code would be:
getclass $P0, "A"
getclass $P1, "B"
subclass $P2, $P0, "Foo"
addparent $P2, $P1
Adding the attributes a
and b
to the new class Foo
:
newclass $P0, "Foo"
addattribute $P0, "a" # This is offset 0 + classoffset
addattribute $P0, "b" # This is offset 1 + classoffset
Assuming we want an object of class Foo
:
.local pmc FooClass
.local pmc MyObject
find_class FooClass, "Foo"
new MyObject, FooClass
Calling the method Xyzzy
on an object, assuming the PDD03 calling conventions are respected:
callmethod "Xyzzy"
set S0, "Xyzzy"
callmethod
Or, if a return continuation needs constructing:
callmethodcc "Xyzzy"
set S0, "Xyzzy"
callmethodcc
With named access:
getattribute $P1, $P0, "Foo\x0b"
To get a new class, you can do a newclass
, which creates a new class with no parents besides Parrot's default super-ish parent class.
To get a new child class, you have two potential options:
Both ways work. It is, however, more efficient to use the first method, and just subclass the immediate parent class of your new class.
When adding in extra parents in a multiple-inheritance scenario, subclass the first class in the immediate parent list then use the addparent
op to add in the rest of the immediate parents.
Classes may override the vtable methods, allowing objects of a class to behave like a primitive PMC. Each vtable slot has a corresponding named method that Parrot looks for in your class hierarchy when an object is used in a primitive context.
To use these properly at a low-level requires a good working knowledge of the way Parrot works--generally for higher-level languages the language compiler or runtime will provide easier-to-use wrappers. These methods are all prototyped, and take a single fixed argument list, and return at most a single value.
While vtable methods may take a continuation, those continuations may not escape the vtable method's execution. This is due to the way that vtable methods are called by the interpreter--once a vtable method is exited any continuation taken within it is no longer valid and may not be used.
Note that any class method that wishes to use Parrot's multi-method dispatch system may do so. This is, in fact, encouraged, though it is not required. In the absence of explicit multimethod dispatch, a left-side wins scheme is used.
The following list details the raw method names:
This PDD is due an overhaul. This requirements section is for language implementers to list the OO-related needs of their language in so as to aid that.
Ruby: Just like small talk, everything is an object. I'm hoping to be able to implement core Ruby classes(String, Array, Hash, Module, etc) something like this.
ParrotClass | RubyClass String | | \ / RubyString
Ruby: Objectspace in Ruby allows the programmer to iterate through every live object in the system. There is some debate about how to make this play nice with different garbage collection schemes.
A class is a collection of methods and attributes. It would be desirable, for those classes whose definition is fully known at compile time, to have a convenient way to have the class along with its attributes and methods stored into a PBC file rather than created at runtime. However, creation of new classes at runtime will be needed too.
Ruby: Ruby has meta classes. It would be nice if classes were objects in Parrots OO model.
Attributes are instance data associated with a class (or role, however those are supported). They may not always be of a type specified by a PMC, though boxing/unboxing is of course an option.
Perl 6: All attributes are opaque (not externally visible, even to any subclasses).
.Net: Attributes may be private (not externally visible), public (always externally visible), protected (only visible to subclasses) and internal (only visible inside the current assembly - the closest correspondence in Parrot is perhaps only visible inside the same PBC file). Additionally, it is allowable for a subclass to introduce an attribute of the same name as the a parent class has, and they both exist depending on what type an instance of the class is currently viewed as being (read: there is a difference between the type of the reference and the type of the value).
Ruby: Attributes can be dynamically added and removed at runtime.
Perl 6: Methods may be public (anyone can invoke them) or private (only invokable by the class they are defined in). Additionally, submethods are methods that do not get inherited.
.Net: Like attributes, methods may be public, private, protected or internal.
Ruby: has a method_missing that gets called when method resolution fails to find a method. Methods can be dynamically added and removed at runtime.
A constructor is run when an object is instantiated.
.Net: There may be many constructors for an object (provided they all have different signatures), and the correct one is called based upon the passed parameters.
Perl 6: Multiple inheritance.
.Net: Single inheritance.
Ruby: Single inheritance but support for mixins of Ruby modules.
An interface specifies a set of methods that must be implemented by a class that inherits (or implements) the interface, but does not provide any form of implementation for them.
.Net: Interfaces are pretty much what was just describe above. XXX Need to check behavior of you implement two interfaces with methods of the same name.
A role consists of a set of methods and attributes. It cannot be instantiated on its own, but must be composed into a class. When this happens its methods and attributes become of that classes methods and attributes. This may happen at compile time or runtime, however when a role is composed into a class at runtime then what really happens is that a new anonymous class is created with the role composed into it and then the namespace entry for the existing class is updated to refer to the new one. Note that this means classes must be garbage collectable, with all those referred to by a namespace or with objects of that class existing being marked live.
Perl 6: Roles pretty much are a Perl 6 thing, so the definition above contains all that is needed. An open question is whether Parrot worry about collision detection? For compile time composition that's easy to punt to the compiler; for runtime composition, that's not so easy though.
Perl 6: Reflection provides access to a list of methods that a class has, its parent classes and the roles it does, as well as the name of the class and its memory address. For methods, their name, signature, return type and whether the method is declared multi are available.
.Net: Reflection provides access to a list of attributes and methods as well as the name of the class and its parent. The types of attributes and signatures of methods are also available.
An inner class is essentially a class defined within a class. Therefore it has access to things private to its outer class.
Perl 6: Inner classes are allowed, and may also be private.
.Net: Inner classes are allowed and may be private, public, protected or internal.
Delegation is where a method call is "forwarded" to another class. Parrot may provide support for simple cases of it directly, or could just provide a "no method matched" fallback method that the compiler fills out to implement the delegation.
Perl 6: Delegation support is highly flexible, even allowing a regex to match method names that should be delegated to a particular object.
Prototype-based OO has no classes. All objects are cloned from existing objects and modified. Requires lightweight singleton creation, without a needing separate class for every instance object. (Self, JavaScript, and Io are examples of prototype-based 00.)
Should we have a super or next opcode?
The interaction between objects and PMCs is currently underspecified.
The following list a set of languages, then within each language what the Parrot term translates to.
None.
None.
None.
|