<commit_before>/* nci.c
Copyright (C) 2001-2009, Parrot Foundation.
$Id$

=head1 NAME

src/nci/api.c - Native Call Interface routines

=head1 DESCRIPTION

This file implements the interface to the Parrot Native Call Interface system,
which builds parrot to C call frames.

=head2 Functions

=over 4

=cut

*/

#include "parrot/parrot.h"
#include "parrot/nci.h"
#include "api.str"

/* HEADERIZER HFILE: include/parrot/nci.h */

/*

=item C<PMC * build_call_func(PARROT_INTERP, STRING *signature)>

This function serves a single purpose. It takes the function signature for a
C function we want to call and returns a PMC with a pointer to a function
that can call it.

=cut

*/

PARROT_CANNOT_RETURN_NULL
PMC *
build_call_func(PARROT_INTERP, ARGIN(STRING *signature))
{
    ASSERT_ARGS(build_call_func)

    PMC * const iglobals = interp->iglobals;
    PMC *nci_funcs;
    PMC *thunk;

    /* fixup empty signatures */
    if (STRING_IS_EMPTY(signature))
        signature = CONST_STRING(interp, "v");

    if (PMC_IS_NULL(iglobals))
        PANIC(interp, "iglobals isn't created yet");

    nci_funcs = VTABLE_get_pmc_keyed_int(interp, iglobals, IGLOBALS_NCI_FUNCS);
    if (PMC_IS_NULL(nci_funcs))
        PANIC(interp, "iglobals.nci_funcs isn't created_yet");

    thunk = VTABLE_get_pmc_keyed_str(interp, nci_funcs, signature);

    if (PMC_IS_NULL(thunk)) {
        /* try to dynamically build a thunk */
        PMC *nci_fb_cb = VTABLE_get_pmc_keyed_int(interp, iglobals, IGLOBALS_NCI_FB_CB);
        if (!PMC_IS_NULL(nci_fb_cb)) {
            void *cb_ptr = VTABLE_get_pointer(interp, nci_fb_cb);
            nci_fb_func_t cb = (nci_fb_func_t)D2FPTR(cb_ptr);
            if (cb_ptr) {
                PMC *nci_fb_ud = VTABLE_get_pmc_keyed_int(interp, iglobals, IGLOBALS_NCI_FB_UD);
                thunk = cb(signature, nci_fb_ud);
            }
        }
    }

    if (!PMC_IS_NULL(thunk)) {
        PARROT_ASSERT(thunk->vtable);
        PARROT_ASSERT(thunk->vtable->base_type == enum_class_UnManagedStruct);
        return thunk;
    }

    Parrot_ex_throw_from_c_args(interp, NULL,
        EXCEPTION_UNIMPLEMENTED,
        "No NCI thunk available for signature '%S'", signature);
}

/*

=back

=cut

*/

/*
 * Local variables:<commit_msg>fix order of arguments passed to frame builder callback<commit_after>/* nci.c
Copyright (C) 2001-2009, Parrot Foundation.
$Id$

=head1 NAME

src/nci/api.c - Native Call Interface routines

=head1 DESCRIPTION

This file implements the interface to the Parrot Native Call Interface system,
which builds parrot to C call frames.

=head2 Functions

=over 4

=cut

*/

#include "parrot/parrot.h"
#include "parrot/nci.h"
#include "api.str"

/* HEADERIZER HFILE: include/parrot/nci.h */

/*

=item C<PMC * build_call_func(PARROT_INTERP, STRING *signature)>

This function serves a single purpose. It takes the function signature for a
C function we want to call and returns a PMC with a pointer to a function
that can call it.

=cut

*/

PARROT_CANNOT_RETURN_NULL
PMC *
build_call_func(PARROT_INTERP, ARGIN(STRING *signature))
{
    ASSERT_ARGS(build_call_func)

    PMC * const iglobals = interp->iglobals;
    PMC *nci_funcs;
    PMC *thunk;

    /* fixup empty signatures */
    if (STRING_IS_EMPTY(signature))
        signature = CONST_STRING(interp, "v");

    if (PMC_IS_NULL(iglobals))
        PANIC(interp, "iglobals isn't created yet");

    nci_funcs = VTABLE_get_pmc_keyed_int(interp, iglobals, IGLOBALS_NCI_FUNCS);
    if (PMC_IS_NULL(nci_funcs))
        PANIC(interp, "iglobals.nci_funcs isn't created_yet");

    thunk = VTABLE_get_pmc_keyed_str(interp, nci_funcs, signature);

    if (PMC_IS_NULL(thunk)) {
        /* try to dynamically build a thunk */
        PMC *nci_fb_cb = VTABLE_get_pmc_keyed_int(interp, iglobals, IGLOBALS_NCI_FB_CB);
        if (!PMC_IS_NULL(nci_fb_cb)) {
            void *cb_ptr = VTABLE_get_pointer(interp, nci_fb_cb);
            nci_fb_func_t cb = (nci_fb_func_t)D2FPTR(cb_ptr);
            if (cb_ptr) {
                PMC *nci_fb_ud = VTABLE_get_pmc_keyed_int(interp, iglobals, IGLOBALS_NCI_FB_UD);
                thunk = cb(nci_fb_ud, signature);
            }
        }
    }

    if (!PMC_IS_NULL(thunk)) {
        PARROT_ASSERT(thunk->vtable);
        PARROT_ASSERT(thunk->vtable->base_type == enum_class_UnManagedStruct);
        return thunk;
    }

    Parrot_ex_throw_from_c_args(interp, NULL,
        EXCEPTION_UNIMPLEMENTED,
        "No NCI thunk available for signature '%S'", signature);
}

/*

=back

=cut

*/

/*
 * Local variables:
{'fraction_non_alphanumeric': 0.1009700176366843, 'fraction_numerical': 0.005731922398589065, 'mean_word_length': 3.2923368022705772, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2916773', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1724, 'n_words': 379}