1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include "../psyfunc.h"
#include "pfuncobject.h"
#include "pclassobject.h"
/***************************************************************/
/*** Virtual functions ***/
static source_virtual_t psyco_computed_function;
static bool compute_function(PsycoObject* po, vinfo_t* v)
{
vinfo_t* newobj;
vinfo_t* fcode;
vinfo_t* fglobals;
vinfo_t* fdefaults;
fcode = vinfo_getitem(v, iFUNC_CODE);
if (fcode == NULL)
return false;
fglobals = vinfo_getitem(v, iFUNC_GLOBALS);
if (fglobals == NULL)
return false;
fdefaults = vinfo_getitem(v, iFUNC_DEFAULTS);
if (fdefaults == NULL)
return false;
newobj = psyco_generic_call(po, PyFunction_New,
CfReturnRef|CfPyErrIfNull,
"vv", fcode, fglobals);
if (newobj == NULL)
return false;
if (!psyco_knowntobe(fdefaults, (long) NULL)) {
if (!psyco_generic_call(po, PyFunction_SetDefaults,
CfNoReturnValue|CfPyErrIfNonNull,
"vv", newobj, fdefaults))
return false;
}
/* move the resulting non-virtual Python object back into 'v' */
vinfo_move(po, v, newobj);
return true;
}
#if 0 /* not needed currently */
static PyObject* direct_compute_function(vinfo_t* v, char* data)
{
PyObject* fcode;
PyObject* fglobals;
PyObject* fdefaults;
PyObject* result = NULL;
fcode = direct_xobj_vinfo(vinfo_getitem(v, iFUNC_CODE), data);
fglobals = direct_xobj_vinfo(vinfo_getitem(v, iFUNC_GLOBALS), data);
fdefaults = direct_xobj_vinfo(vinfo_getitem(v, iFUNC_DEFAULTS), data);
if (!PyErr_Occurred() && fcode != NULL && fglobals != NULL) {
result = PyFunction_New(fcode, fglobals);
if (result != NULL && fdefaults != NULL) {
if (PyFunction_SetDefaults(result, fdefaults) != 0) {
Py_DECREF(result);
result = NULL;
}
}
}
Py_XDECREF(fdefaults);
Py_XDECREF(fglobals);
Py_XDECREF(fcode);
return result;
}
#endif
DEFINEFN
vinfo_t* PsycoFunction_New(PsycoObject* po, vinfo_t* fcode,
vinfo_t* fglobals, vinfo_t* fdefaults)
{
vinfo_t* r;
/* fdefaults contains potential arguments; mutable objects there
must be forced out of virtual-time right now */
if (fdefaults != NULL && !psyco_forking(po, fdefaults->array))
return NULL;
/* Build a virtual function object */
r = vinfo_new(VirtualTime_New(&psyco_computed_function));
r->array = array_new(FUNC_TOTAL);
r->array->items[iOB_TYPE] =
vinfo_new(CompileTime_New((long)(&PyFunction_Type)));
vinfo_incref(fcode);
r->array->items[iFUNC_CODE] = fcode;
vinfo_incref(fglobals);
r->array->items[iFUNC_GLOBALS] = fglobals;
if (fdefaults == NULL)
fdefaults = psyco_vi_Zero();
else
vinfo_incref(fdefaults);
r->array->items[iFUNC_DEFAULTS] = fdefaults;
return r;
}
/***************************************************************/
/*** function objects meta-implementation ***/
DEFINEFN
vinfo_t* pfunction_call(PsycoObject* po, vinfo_t* func,
vinfo_t* arg, vinfo_t* kw)
{
if (psyco_knowntobe(kw, (long) NULL)) {
PyCodeObject* co;
vinfo_t* fcode;
vinfo_t* fglobals;
vinfo_t* fdefaults;
if (!is_virtualtime(func->source)) {
/* run-time or compile-time values: promote the
function object as a whole into compile-time */
vinfo_t* result;
PyObject* f = psyco_pyobj_atcompiletime(po, func);
PyObject* glob;
PyObject* defl;
if (f == NULL)
return NULL;
co = (PyCodeObject*) PyFunction_GET_CODE(f);
if (PyCode_GetNumFree(co) > 0)
goto fallback;
glob = PyFunction_GET_GLOBALS(f);
defl = PyFunction_GET_DEFAULTS(f);
Py_INCREF(glob);
fglobals = vinfo_new(CompileTime_NewSk(sk_new
((long)glob, SkFlagPyObj)));
if (defl == NULL)
fdefaults = psyco_vi_Zero();
else {
Py_INCREF(defl);
fdefaults = vinfo_new(CompileTime_NewSk(sk_new
((long)defl, SkFlagPyObj)));
}
result = psyco_call_pyfunc(po, co, fglobals, fdefaults,
arg, po->pr.auto_recursion);
vinfo_decref(fdefaults, po);
vinfo_decref(fglobals, po);
return result;
}
else {
/* virtual-time function objects: read the
individual components */
fcode = vinfo_getitem(func, iFUNC_CODE);
if (fcode == NULL)
return NULL;
co = (PyCodeObject*)psyco_pyobj_atcompiletime(po, fcode);
if (co == NULL)
return NULL;
fglobals = vinfo_getitem(func, iFUNC_GLOBALS);
if (fglobals == NULL)
return NULL;
fdefaults = vinfo_getitem(func, iFUNC_DEFAULTS);
if (fdefaults == NULL)
return NULL;
return psyco_call_pyfunc(po, co, fglobals, fdefaults,
arg, po->pr.auto_recursion);
}
}
fallback:
#if NEW_STYLE_TYPES /* Python >= 2.2b1 */
return psyco_generic_call(po, PyFunction_Type.tp_call,
CfReturnRef|CfPyErrIfNull,
"vvv", func, arg, kw);
#else
/* PyFunction_Type.tp_call == NULL... */
return psyco_generic_call(po, PyEval_CallObjectWithKeywords,
CfReturnRef|CfPyErrIfNull,
"vvv", func, arg, kw);
#endif
}
#if NEW_STYLE_TYPES /* Python >= 2.2b1 */
static vinfo_t* pfunc_descr_get(PsycoObject* po, PyObject* func,
vinfo_t* obj, PyObject* type)
{
/* see comments of pmember_get() in pdescrobject.c. */
/* XXX obj is never Py_None here in the current implementation,
but could be if called by other routines than
PsycoObject_GenericGetAttr(). */
return PsycoMethod_New(func, obj, type);
}
#endif /* NEW_STYLE_TYPES */
INITIALIZATIONFN
void psy_funcobject_init(void)
{
#if NEW_STYLE_TYPES /* Python >= 2.2b1 */
Psyco_DefineMeta(PyFunction_Type.tp_call, pfunction_call);
Psyco_DefineMeta(PyFunction_Type.tp_descr_get, pfunc_descr_get);
#endif
/* function object are mutable;
they must be forced out of virtual-time across function calls */
INIT_SVIRTUAL_NOCALL(psyco_computed_function, compute_function, 1);
}
|