parrotcode: Object and Class semantics for Parrot | |
Contents | Documentation |
docs/pdds/pdd15_objects.pod - Object and Class semantics for Parrot
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.
Should we have a super or next opcode?
This PDD describes the semantics of Parrot's object and class systems. The PDD is divided into two parts, the semantics expressed to user programs through PMCs, and the default back-end class scheme.
Note that the class system is not the single mandated class scheme, merely the one designed to express the semantics needed for Perl 6, ruby, and python. Alternate class systems are certainly possible, and direct compatibility with the system as described here isn't strictly necessary.
This is a reasonably straightforward object system. It assumes that objects have:
and that you can:
Additionally we assume that all objects can have properties on them, as all PMCs can have properties. The property get/set method may be overridden on a per-class basis as any other vtable method may be.
For classes, we assume that:
And we further assume that classes can:
This list is likely not definitive, but it's enough to start with. It also doesn't address the semantics of method calls, which need to be dealt with, possibly separately. With that in mind, the object system supports these features with a combination of PMC classes (not to be confused with object classes) and opcodes.
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,
ParrotClass
and ParrotObject
.
ParrotObject PMCs are the actual objects,
and hold all the per-object instance data.
ParrotClass PMCs hold all the class-specific information.
Instantiating a new OO class creates a new ParrotClass PMC,
and enters the new OO class into Parrot's PMC class table,
at which point it is indistinguishable from any other PMC class.
(This doesn't mean that non-ParrotClass things can be subclassed or treated as an OO class.
Neither is that forbidden.
Just unimplemented)
It's important to note that all 'standard' classes are ParrotClass PMC instances, and all 'standard' objects are ParrotObject PMCs. We do not create a brand new PMC class for each OO class, and they all share the ParrotClass or ParrotObject vtable, respectively. This distinction is mostly an artifact of the implementation, and may change in the future.
While the internals of the class and object PMCs should be considered black boxes, here's some documentation as to what they are for implementation purposes.
The ParrotClass PMC holds a 6 element array, which is:
Note that the attribute catalog holds all the attributes for an object. This includes the attributes in the object's class as well as all the attributes defined in all the parent classes. (Multiple inheritance makes this necessary -- the offsets of a class' attributes will change from child class to child class)
{{ NOTE: one feature I'd like to have is some way to access a list of the object's attributes. The particular use I have in mind is dumping the attributes of an object a la Data::Dumper. It abuses the notion of 'opaque object', so I could be persuaded it's not a good idea. On the other hand, it could be an introspective capability offered by Parrot but not directly exposed in the HLLs. }}
ParrotClass PMCs also have the "I am a class" flag set on them.
The ParrotObject PMC is an array of meta-information and attributes. The elements of this array are:
Note that ParrotObject PMCs also have the "I am an object" flag set on them.
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 ParrotClass-style class or not you've got bigger problems.
Currently Parrot only supports mutating a class' metainformation for ParrotClass 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 ParrotObject objects and ParrotClass 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 int FooType
.local pmc MyObject
find_type FooType, "Foo"
new MyObject, FooType
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
Assuming we've an object that has class Foo
in it somewhere and want to get the second attribute b
out of it:
.local int BaseOffset
.local int BOffset
classoffset BaseOffset, $P0, "Foo"
BOffset = BaseOffset + 1
getattribute $P1, $P0, BOffset
Or with named access, if it isn't time critical:
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. (Which doesn't appear in the class list anywhere, though arguably it ought to)
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.
Do be aware that, right now, you should not add attributes or parents to a class that's been subclassed or has had objects instantiated. This will leave the internal structures of the classes and objects in an inconsistent state and things won't work at all the way you want them to. At the moment parrot won't warn if you do this, but it will soon. The restriction on parent list changes and attribute addition will be lifted in future releases, though doing so will be an expensive operation.
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:
Since every object system on the planet shares a common set of terms but uses them completely differently, this section defines
The following list a set of languages, then within each language what the parrot term translates to.
None.
None.
None.
Maintainer: Dan Sugalski
Class: Internals
PDD Number: 15
Version: 1.2
Status: Developing
Last Modified: February 09, 2004
PDD Format: 1
Language: English
|