"""

library creates a set of FuncWrappers, which are just a binding of
name to function ptr, for the built-in library functions.

"""

import chelpers

class LibraryWrapper:

    """ LibraryWrapper is an object that is initialized with all the
    info needed to access library functions for all backends.
    Depending on what sort of machine this is running on, some of that
    info may not be valid, but it should be consistent: for example,
    when not running on a PPC machine, the ppc_info may not be valid,
    but it should be initialized to something that is the same each
    time to allow for unit testing even when not on a PPC machine.

    Each field and its contents are described here:

    c_func_ptr --- the C function pointer.  For simpler architectures,
    such as X86, this is all you need to call the function and it
    points at the beginning of the assembly.  For other architures,
    such as Linux PPC, this is a pointer to the transition vector.

    ppc_info --- the data to be found at the PPC transition vector is
    contained here in a tuple, derefenced.  The tuple contains (ASM,
    TOC, ENV) where ASM is the function pointer, TOC is a pointer to
    its Table of Contents, and the third word is the Environment
    pointer, which we don't use.  When not running on a PPC, this data
    will probably not be valid as discussed above. """

    def __init__ (self, fname):
        self.fname = fname
        self.c_func_ptr = chelpers.lookup_library (fname)
        self.ppc_info   = chelpers.ppc_lookup_library (fname)
        pass

    def __str__ (self):
        return "<%s>" % self.fname

    pass

errfetch = LibraryWrapper ('Pynto_ErrFetch')
freeobject = LibraryWrapper ('Pynto_FreeObject')
buildtuple = LibraryWrapper ('Pynto_BuildTuple')
callobject = LibraryWrapper ('PyObject_Call')
getattr = LibraryWrapper ('PyObject_GetAttr')
setattr = LibraryWrapper ('PyObject_SetAttr')
objistrue = LibraryWrapper ('PyObject_IsTrue')
richcomp = LibraryWrapper ('PyObject_RichCompare')
contains = LibraryWrapper ('chelpers_IN')
truth = LibraryWrapper ('chelpers_truth')
falsehood = LibraryWrapper ('chelpers_falsehood')
trace = LibraryWrapper ('chelpers_trace')
trace_indent = LibraryWrapper ('chelpers_trace_indent')
trace_undent = LibraryWrapper ('chelpers_trace_undent')
