1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
|
|
|
Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
|
|
|
|
Netherlands.
|
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Class object implementation */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "allobjects.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "structmember.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
|
|
|
object *cl_bases; /* A tuple */
|
|
|
|
object *cl_methods; /* A dictionary */
|
|
|
|
} classobject;
|
|
|
|
|
|
|
|
object *
|
1990-12-20 11:06:42 -04:00
|
|
|
newclassobject(bases, methods)
|
1990-10-14 09:07:46 -03:00
|
|
|
object *bases; /* NULL or tuple of classobjects! */
|
|
|
|
object *methods;
|
|
|
|
{
|
|
|
|
classobject *op;
|
|
|
|
op = NEWOBJ(classobject, &Classtype);
|
|
|
|
if (op == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (bases != NULL)
|
|
|
|
INCREF(bases);
|
|
|
|
op->cl_bases = bases;
|
|
|
|
INCREF(methods);
|
|
|
|
op->cl_methods = methods;
|
|
|
|
return (object *) op;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Class methods */
|
|
|
|
|
|
|
|
static void
|
|
|
|
class_dealloc(op)
|
|
|
|
classobject *op;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (op->cl_bases != NULL)
|
|
|
|
DECREF(op->cl_bases);
|
|
|
|
DECREF(op->cl_methods);
|
|
|
|
free((ANY *)op);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
|
|
|
class_getattr(op, name)
|
|
|
|
register classobject *op;
|
|
|
|
register char *name;
|
|
|
|
{
|
|
|
|
register object *v;
|
|
|
|
v = dictlookup(op->cl_methods, name);
|
|
|
|
if (v != NULL) {
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
if (op->cl_bases != NULL) {
|
|
|
|
int n = gettuplesize(op->cl_bases);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
v = class_getattr(gettupleitem(op->cl_bases, i), name);
|
|
|
|
if (v != NULL)
|
|
|
|
return v;
|
1990-12-20 11:06:42 -04:00
|
|
|
err_clear();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
}
|
1990-10-14 16:58:09 -03:00
|
|
|
err_setstr(NameError, name);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
typeobject Classtype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
|
|
|
"class",
|
|
|
|
sizeof(classobject),
|
|
|
|
0,
|
|
|
|
class_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
class_getattr, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1991-04-04 06:42:10 -04:00
|
|
|
/* We're not done yet: next, we define instance objects... */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
|
|
|
classobject *cm_class; /* The class object */
|
|
|
|
object *cm_attr; /* A dictionary */
|
1991-04-04 06:42:10 -04:00
|
|
|
} instanceobject;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
object *
|
1991-04-04 06:42:10 -04:00
|
|
|
newinstanceobject(class)
|
1990-10-14 09:07:46 -03:00
|
|
|
register object *class;
|
|
|
|
{
|
1991-04-04 06:42:10 -04:00
|
|
|
register instanceobject *cm;
|
1990-10-14 09:07:46 -03:00
|
|
|
if (!is_classobject(class)) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-04-04 06:42:10 -04:00
|
|
|
cm = NEWOBJ(instanceobject, &Instancetype);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (cm == NULL)
|
|
|
|
return NULL;
|
|
|
|
INCREF(class);
|
|
|
|
cm->cm_class = (classobject *)class;
|
|
|
|
cm->cm_attr = newdictobject();
|
|
|
|
if (cm->cm_attr == NULL) {
|
|
|
|
DECREF(cm);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (object *)cm;
|
|
|
|
}
|
|
|
|
|
1991-04-04 06:42:10 -04:00
|
|
|
/* Instance methods */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
static void
|
1991-04-04 06:42:10 -04:00
|
|
|
instance_dealloc(cm)
|
|
|
|
register instanceobject *cm;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
DECREF(cm->cm_class);
|
|
|
|
if (cm->cm_attr != NULL)
|
|
|
|
DECREF(cm->cm_attr);
|
|
|
|
free((ANY *)cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static object *
|
1991-04-04 06:42:10 -04:00
|
|
|
instance_getattr(cm, name)
|
|
|
|
register instanceobject *cm;
|
1990-10-14 09:07:46 -03:00
|
|
|
register char *name;
|
|
|
|
{
|
|
|
|
register object *v = dictlookup(cm->cm_attr, name);
|
|
|
|
if (v != NULL) {
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
v = class_getattr(cm->cm_class, name);
|
|
|
|
if (v == NULL)
|
1990-10-21 19:15:08 -03:00
|
|
|
return v; /* class_getattr() has set the error */
|
1990-10-14 09:07:46 -03:00
|
|
|
if (is_funcobject(v)) {
|
|
|
|
object *w = newclassmethodobject(v, (object *)cm);
|
|
|
|
DECREF(v);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
DECREF(v);
|
1990-10-21 19:15:08 -03:00
|
|
|
err_setstr(NameError, name);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1991-04-04 06:42:10 -04:00
|
|
|
instance_setattr(cm, name, v)
|
|
|
|
instanceobject *cm;
|
1990-10-14 09:07:46 -03:00
|
|
|
char *name;
|
|
|
|
object *v;
|
|
|
|
{
|
|
|
|
if (v == NULL)
|
|
|
|
return dictremove(cm->cm_attr, name);
|
|
|
|
else
|
|
|
|
return dictinsert(cm->cm_attr, name, v);
|
|
|
|
}
|
|
|
|
|
1991-04-04 06:42:10 -04:00
|
|
|
typeobject Instancetype = {
|
1990-10-14 09:07:46 -03:00
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
1991-04-04 06:42:10 -04:00
|
|
|
"instance",
|
|
|
|
sizeof(instanceobject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1991-04-04 06:42:10 -04:00
|
|
|
instance_dealloc, /*tp_dealloc*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_print*/
|
1991-04-04 06:42:10 -04:00
|
|
|
instance_getattr, /*tp_getattr*/
|
|
|
|
instance_setattr, /*tp_setattr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* And finally, here are class method objects */
|
1991-04-04 06:42:10 -04:00
|
|
|
/* (Really methods of instances) */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
|
|
|
object *cm_func; /* The method function */
|
|
|
|
object *cm_self; /* The object to which this applies */
|
|
|
|
} classmethodobject;
|
|
|
|
|
|
|
|
object *
|
|
|
|
newclassmethodobject(func, self)
|
|
|
|
object *func;
|
|
|
|
object *self;
|
|
|
|
{
|
|
|
|
register classmethodobject *cm;
|
|
|
|
if (!is_funcobject(func)) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
cm = NEWOBJ(classmethodobject, &Classmethodtype);
|
|
|
|
if (cm == NULL)
|
|
|
|
return NULL;
|
|
|
|
INCREF(func);
|
|
|
|
cm->cm_func = func;
|
|
|
|
INCREF(self);
|
|
|
|
cm->cm_self = self;
|
|
|
|
return (object *)cm;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
classmethodgetfunc(cm)
|
|
|
|
register object *cm;
|
|
|
|
{
|
|
|
|
if (!is_classmethodobject(cm)) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((classmethodobject *)cm)->cm_func;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
classmethodgetself(cm)
|
|
|
|
register object *cm;
|
|
|
|
{
|
|
|
|
if (!is_classmethodobject(cm)) {
|
1990-10-21 19:15:08 -03:00
|
|
|
err_badcall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((classmethodobject *)cm)->cm_self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Class method methods */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#define OFF(x) offsetof(classmethodobject, x)
|
|
|
|
|
|
|
|
static struct memberlist classmethod_memberlist[] = {
|
|
|
|
{"cm_func", T_OBJECT, OFF(cm_func)},
|
|
|
|
{"cm_self", T_OBJECT, OFF(cm_self)},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
|
|
|
classmethod_getattr(cm, name)
|
|
|
|
register classmethodobject *cm;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
return getmember((char *)cm, classmethod_memberlist, name);
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static void
|
|
|
|
classmethod_dealloc(cm)
|
|
|
|
register classmethodobject *cm;
|
|
|
|
{
|
|
|
|
DECREF(cm->cm_func);
|
|
|
|
DECREF(cm->cm_self);
|
|
|
|
free((ANY *)cm);
|
|
|
|
}
|
|
|
|
|
|
|
|
typeobject Classmethodtype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
|
|
|
"class method",
|
|
|
|
sizeof(classmethodobject),
|
|
|
|
0,
|
|
|
|
classmethod_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
1990-12-20 11:06:42 -04:00
|
|
|
classmethod_getattr, /*tp_getattr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
|
|
|
};
|