On Mon, 14 Mar 2005, William Coleda wrote:

> How does one call a PIR-defined sub from C?

use the Parrot_call_sub_* API.

> My current best guess is, to invoke the PIR sub "Tcl::Joe":
>
>       PMC *invokeme;
>       STRING *Tcl,*joe;
>       Tcl = string_from_const_cstring(INTERP, "Tcl", 3);
>       joe = string_from_const_cstring(INTERP, "joe", 3);
>       invokeme = Parrot_find_global(INTERP, Tcl, joe);
>       VTABLE_invoke(INTERP,invokeme,????);
>
> But I don't know what to put as the argument for "next" to VTABLE_invoke - I 
> see some items in dynclasses/*.pmc use the string "next", the code seems to 
> allow for NULL - neither of them seem to actually invoke my method. (a .sub 
> with a single "print" statement)
>
> Do I need to be setting up the calling conventions in my calling code? Is 
> there some other step I'm missing? Is this already documented somewhere?

here's how i do it in mod_parrot, using the Parrot_* datatypes and the
Parrot_call_sub_* API.  some of the logic has been factored out into
different functions for code reuse, but you get the idea:

Parrot_PMC get_sub_pmc(Parrot_Interp interp, char *namespace, char *name)
{
    Parrot_PMC sub;

    sub = Parrot_find_global(
        interp,
        namespace ? MAKE_PARROT_STRING(namespace) : NULL,
        MAKE_PARROT_STRING(name)
    );
    return(sub);
}

int modparrot_call_sub(Parrot_Interp interp, char *namespace, char *name,
    int *ret)
{
    Parrot_PMC sub;

    sub = get_sub_pmc(interp, namespace, name);
    if (!sub) {
        return(0);
    }
    *ret = Parrot_call_sub_ret_int(interp, sub, "Iv");
    return(1);
}

-jeff


Reply via email to