petdance/scratch — Issue #199: method overload in pir subclass of pmc pccmethod leaves object undefined
Author: petdance
Created: 2011-11-14 05:55:13+00:00
State: open
Labels: bug, library, Pri-normal, Sev-medium

while creating a subclass of Exporter to test inter-language exports,
i stumbled across a bug that i can't seem to fix.

i've created a subclass of Exporter called MyExporter. MyExporter has
an overridden 'add_global' method, which adds a '&' sigil to each
global specified by the user and calls the method in the superclass.

i've also created a high-level language that uses the '&' sigil on
subs, and i import and aim to call the hll sub. however, the code
never finishes 'add_method', as it segfaults inside the supermethod
with an undefined 'exp' object. that is, from src/pmc/exporter.pmc,
line 260:

Parrot_Exporter *exp = PARROT_EXPORTER(SELF);

after this line, 'exp' is NULL, but 'SELF' (or after macro expansion
'pmc') is not. this is not the case when 'add_method' has not been
overridden. here's the pir code that exposes the bug:

.sub 'main' :main
.local pmc exp, ns

exp = new 'MyExporter'

ns = get_root_namespace ['foo']
exp.'source'( ns )

exp.'add_global'('bar')

say "NEVER REACHES HERE"

exp.'import'()

'&bar'() ## THIS WILL SUCCEED!
'bar'() ## THIS WILL FAIL!
## THAT'S NOT WHAT I WANT!!!
.end

## create a subclass of Exporter that overrides 'add_global'
## it adds a '&' sigil to each requested global
.namespace ['MyExporter']
.sub 'onload' :load :init :anon
.local pmc class
class = subclass 'Exporter', 'MyExporter'
.return ()
.end

.sub 'add_global' :method
.param string glb :optional
.param int has_glb :opt_flag

.local pmc args, super
args = new .Hash
super = new .Super, self

unless has_glb goto no_global
glb = concat '&', glb
.return super.'add_global'( glb )
no_global:
.return super.'add_global'()
.end

# create a high-level language that uses '&' sigil for sub names
.HLL 'foo', ''
.sub '&bar'
say 'hello, world!'
.end

note that switching from a tail method call to a regular method call
has no effect.

also note that my code also exposes a problem with the current
Exporter implementation: the imported sub name does not match the
requested name. i have yet to address this as the current bug is
blocking progress.

~jerry

Originally http://trac.parrot.org/parrot/ticket/1286