Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there any way to dynamically add a method to a class?

7 views
Skip to first unread message

Joseph Ryan

unread,
Aug 25, 2003, 7:20:02 PM8/25/03
to perl6-i...@perl.org
So, I know how to use find_method to get a method from an object;
but is there any way to dynamically add a method to a class?
Basically, I want to do something like this:

newclass P2, "Foo"
new P1, P2

addr I0, _Foo::somemethod
setmethod P1, "somemethod", I0
findmethod P0, P1, "somemethod"
invoke

So, how do I do it? :) The only way I could figure out that might
work was to make my own pmc class that held an alias to the class
pmc itself as a property, and then treat the class itself as hash;
however that seems extremely hackish, as well as pretty slow, as it
would take an extra lookup to find the method. Any tips would be
greatly appreciated.

- Joe

Dan Sugalski

unread,
Sep 4, 2003, 9:42:51 AM9/4/03
to Joseph Ryan, perl6-i...@perl.org
On Mon, 25 Aug 2003, Joseph Ryan wrote:

> So, I know how to use find_method to get a method from an object;
> but is there any way to dynamically add a method to a class?
> Basically, I want to do something like this:
>
> newclass P2, "Foo"
> new P1, P2
>
> addr I0, _Foo::somemethod
> setmethod P1, "somemethod", I0
> findmethod P0, P1, "somemethod"
> invoke
>
> So, how do I do it? :)

What's supposed to happen is that each class has a backing namespace, and
methods all live in that namespace. Generally objects, no matter what
their HLL class, will be PMCs that have subclassed (at the parrot level)
ParrotObject.

Anyway, for a perl/python/ruby object of class Foo, to add a new method
you'd just add a new sub/method name/PMC binding to the Foo namespace.

Dan

Leopold Toetsch

unread,
Sep 4, 2003, 10:31:42 AM9/4/03
to Dan Sugalski, perl6-i...@perl.org
Dan Sugalski <d...@sidhe.org> wrote:

> Anyway, for a perl/python/ruby object of class Foo, to add a new method
> you'd just add a new sub/method name/PMC binding to the Foo namespace.

Can I translate that to: "some_method" in class "Foo" has a Sub PMC in
the global stash, i.e. is retrievable by:

.local Sub meth
meth = global "&Foo::some_method" # perlish mangling

or, how else would that look like in PASM?
BTW what is the linked list in the global stash for?

> Dan

leo

Dan Sugalski

unread,
Sep 4, 2003, 11:13:30 AM9/4/03
to Leopold Toetsch, perl6-i...@perl.org
On Thu, 4 Sep 2003, Leopold Toetsch wrote:

> Dan Sugalski <d...@sidhe.org> wrote:
>
> > Anyway, for a perl/python/ruby object of class Foo, to add a new method
> > you'd just add a new sub/method name/PMC binding to the Foo namespace.
>
> Can I translate that to: "some_method" in class "Foo" has a Sub PMC in
> the global stash, i.e. is retrievable by:
>
> .local Sub meth
> meth = global "&Foo::some_method" # perlish mangling
>
> or, how else would that look like in PASM?

Pretty much like that. Pure pasm'd do a store_global into the right stash,
but not much past that.

> BTW what is the linked list in the global stash for?

Python. It's got nested global namespaces--what's supposed to happen is
that when you do a lookup, if it fails you walk up the chain looking for
the symbol until you find it or run out of chain. That way you can have
local overrides of global namespaces.

It's... an interesting way to do things, but it does it, so there's
support in for it.

Dan

Joseph Ryan

unread,
Sep 4, 2003, 3:52:33 PM9/4/03
to Dan Sugalski, perl6-i...@perl.org
Dan Sugalski wrote:

I'm a bit lost here; what does that mean? How do I do it? Does

.sub _Foo::somemethod
print "Foo-ey goodness."
.end

add "somemethod" to the "Foo" namespace? Or would it have to be:

.sub Foo::somemethod
print "Foo-ey goodness."
.end

Thanks for the reply,

- Joe

Luke Palmer

unread,
Sep 4, 2003, 4:27:47 PM9/4/03
to Joseph Ryan, Dan Sugalski, perl6-i...@perl.org

No... well, imcc will probably support that eventually. It would be
(note the conditional; i.e. you can't do this object stuff yet):

.sub _main
newsub $P0, .Method, __foo_somemethod
global "Foo::somemethod" = $P0
...
.end

.sub __foo_somemethod
print "Foo-ey goodness."
.end

If C<global> is smart. If not:

.sub_main
newsub $P0, .Method, __foo_somemethod
$P1 = global "Foo::"
$P1["somemethod"] = $P0
...
.end

Note that __foo_somemethod is a completely arbitrary name. It could
have been called _boozebar for all these ops care.

Luke

Jonathan Worthington

unread,
Sep 4, 2003, 6:08:27 PM9/4/03
to perl6-i...@perl.org
Hi,

I want to include pdump and disassemble in POW, and they seem not to be
compiled by a normal Configure/make. So, I went to compile them, and while
I can get pdump.obj and disassemble.obj, I'm having trouble linking them.

Please could somebody put me out of my misery and tell me what I need to
link them against? :-) I've played around with it for quite a while by
trying to figure out what I should be using, and looked through the docs
(didn't find anything to help).

Many thanks,

Jonathan

Luke Palmer

unread,
Sep 4, 2003, 11:05:54 PM9/4/03
to Jonathan Worthington, perl6-i...@perl.org

Oops. Well, I said I<off-list>, didn't I? Doesn't mutt know what I
mean!? %-)

Luke

Luke Palmer writes:
> Hi Jonathan,

> POW?
>
> My Makefile has those two targets... but just in case you can't do that
> (maybe something to do with that POW thing), here are the command lines
> + outputs:
>
> % make pdump
> gcc -o pdump pdump.o packdump.o -L/usr/local/lib -g blib/lib/libparrot.a -lpthread -lnsl -ldl -lm -lcrypt -lutil
>
> % make disassemble
> disassemble.c
> gcc -o disassemble disassemble.o -L/usr/local/lib -g blib/lib/libparrot.a -lpthread -lnsl -ldl -lm -lcrypt -lutil
>
> Hope this helped.
>
> Luke
>
> > Many thanks,
> >
> > Jonathan
> >

Luke Palmer

unread,
Sep 4, 2003, 11:02:11 PM9/4/03
to Jonathan Worthington, perl6-i...@perl.org
Hi Jonathan,

POW?

Leopold Toetsch

unread,
Sep 5, 2003, 4:17:11 AM9/5/03
to Jonathan Worthington, perl6-i...@perl.org
Jonathan Worthington wrote:

> Hi,
>
> I want to include pdump and disassemble


$(MAKE) world

(since now ;-)
leo

Nicholas Clark

unread,
Sep 5, 2003, 11:14:19 AM9/5/03
to Luke Palmer, Jonathan Worthington, perl6-i...@perl.org
On Thu, Sep 04, 2003 at 09:05:54PM -0600, Luke Palmer wrote:
>
> Oops. Well, I said I<off-list>, didn't I? Doesn't mutt know what I
> mean!? %-)

It's not that smart :-)

But it does spot the references headers and thread messages that are actually
replies to existing messages, even when the subject and bodies are completely
different.

If people (Luke is innocent of this) want to start new threads, please could
they cut&paste the list address from an existing message into a new
message, rather than hitting "reply" and then doing a mass delete.

(Unless they have a Ministry of Truth approved mailer which does remove
all traced of the previous thread. I think genuine Hotmail qualifies)

Nicholas Clark

Jonathan Worthington

unread,
Sep 5, 2003, 1:09:18 PM9/5/03
to Leopold Toetsch, perl6-i...@perl.org
Thanks! That's really great. :-)

Or would be... Just went to rebuild it on Win32 with latest CVS checkout:-

io/io_win32.c
io_win32.c
io/io_win32.c(212) : error C2371: 'PIO_win32_flush' : redefinition;
different basic types
io/io_win32.c(49) : see declaration of 'PIO_win32_flush'
NMAKE : fatal error U1077: 'C:\Perl\bin\perl.exe' : return code '0x2'
Stop.

Am almost certian that's not related to Leo's change, but any ideas? And
while I'm on it I'll just report:-

interpreter.c(679) : warning C4020: 'Parrot_getenv' : too many actual
parameters
platform.c(65) : warning C4029: declared formal parameter list different
from definition

Thanks,

Jonathan


Vladimir Lipskiy

unread,
Sep 5, 2003, 10:42:12 PM9/5/03
to perl6-internals, Jonathan Worthington
> io/io_win32.c
> io_win32.c
> io/io_win32.c(212) : error C2371: 'PIO_win32_flush' : redefinition;
> different basic types
> io/io_win32.c(49) : see declaration of 'PIO_win32_flush'
> NMAKE : fatal error U1077: 'C:\Perl\bin\perl.exe' : return code '0x2'
> Stop.

I succeeded in fixing it(io_win32.c.diff), but there and then I got

libparrot_s.lib(io_buf.obj) : error LNK2001: unresolved external symbol
_PIO_win
32_getblksize
libparrot_s.lib(io_buf.obj) : error LNK2001: unresolved external symbol
_PIO_win
32_isatty
parrot.exe : fatal error LNK1120: 2 unresolved externals
NMAKE : fatal error U1077: 'link' : return code '0x460'
Stop.

Any ideas?

> interpreter.c(679) : warning C4020: 'Parrot_getenv' : too many actual
> parameters
> platform.c(65) : warning C4029: declared formal parameter list different
> from definition

Hmm ... This doesn't happen to me. Maybe some good-natured guy has
already fixed that.

io_win32.c.diff

Vladimir Lipskiy

unread,
Sep 5, 2003, 11:43:53 PM9/5/03
to perl6-internals, Vladimir Lipskiy
me wrote:
> libparrot_s.lib(io_buf.obj) : error LNK2001: unresolved external symbol
> _PIO_win
> 32_getblksize
> libparrot_s.lib(io_buf.obj) : error LNK2001: unresolved external symbol
> _PIO_win
> 32_isatty
> parrot.exe : fatal error LNK1120: 2 unresolved externals
> NMAKE : fatal error U1077: 'link' : return code '0x460'
> Stop.
>
> Any ideas?

Yup. There

static INTVAL
PIO_win32_isatty(PIOHANDLE fd)
{

and

static INTVAL
PIO_win32_getblksize(PIOHANDLE fd)
{

those indentifiers had internal linkage. Bi-bi. Fixed.


io_win32.c.diff

Jonathan Worthington

unread,
Sep 7, 2003, 10:53:30 AM9/7/03
to perl6-internals

Works for me. Thanks! :-) I believe this patch has yet to be applied.

BTW, make world works great - thanks again, Leo.

Jonathan


Vladimir Lipskiy

unread,
Sep 7, 2003, 7:53:46 PM9/7/03
to Jonathan Worthington, perl6-internals
> I believe this patch has yet to be applied.

Don't think so. Juergen is engaged in improving
fdopen semantics and can't sync the source to
apply the patch, probably. Leo thinks that it should
be done by Juergen since now he got commit
privs.

Juergen Boemmels

unread,
Sep 11, 2003, 1:22:00 PM9/11/03
to Vladimir Lipskiy, perl6-internals
"Vladimir Lipskiy" <fors...@kaluga.ru> writes:

> static INTVAL
> PIO_win32_getblksize(PIOHANDLE fd)
> {
>
> those indentifiers had internal linkage. Bi-bi. Fixed.

Thanks applied.
boe

0 new messages