C pmclass - parrot object inheritance, interaction vtables/methods/object identities

0 views
Skip to first unread message

Mehmet Yavuz Selim Soyturk

unread,
Mar 8, 2007, 12:52:57 PM3/8/07
to perl6-i...@perl.org
Hello,

I'm implementing ECMAscript (javascript) which has prototype based
inheritance. An object is some sort of hash which has some properties.
An object can have a prototype, from which it inherits all of the
properties. And the objects prototype can also have a prototype, from
which the object inherits too, and so on.

In ECMAScript each object can be used as a scope (I think that it's
called LexPad here). Examples of this are:
* the global object == the global scope
* the with statement
So property lookup of an object has to be fast enough.

First, I implemented it in such a way, and it worked:

=== pjsobject.pmc ====

#define HASH(o) PMC_pmc_val(o)
#define PROTO(o) PMC_struct_val(o)

pmclass PjsObject {
PMC* get_pmc_keyed(PMC* key) {
x = SELF
lookup in HASH(x),
if not found, x = PROTO(x), lookup...
}
....
}

======================

But then I needed to implement the get_string vtable, which needed to
invoke the sub that I get with SELF.get_pmc_keyed('toString'), for
which I didn't find a solution. So I ended up renaming PjsObject to
PjsObjectBase, extending it by a parrot class called PjsObject.

.namespace [ 'PjsObject' ]

.sub get_string :vtable :method
$P0 = self['toString']
# ...
.end

But then PROTO(x) does not work anymore, because x is most of the time
not a raw PjsObjectBase, but it's PjsObject which wraps my
PjsObjectBase. I solved that problem with some ugly hacks, such as
recursive vtables etc. My solution was to implement the get_pmc vtable
like this:

PMC* get_pmc() {
return SELF;
}

which gives back my raw PjsObjectBase. Now I can find the prototype
object of a PjsObjectBase with VTABLE_get_pmc(PROTO(x)).
As far as I understand from the documentation it should have been the
other way around, the wrapper should have returned the wrapped, not
the wrapped itself, but it works.

But there are some problems. If a subclass of PjsObjectBase overrides
my get_pmc vtable I have a problem. Also I don't know if get_pmc
automagically is called in some situations (maybe by some opcodes?)
which could break my solution.

So what do you think? Is the get_pmc solution acceptable, or is there
a better solution?

Many thanks.

--
Mehmet

Reply all
Reply to author
Forward
0 new messages