Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Function object implementation */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include "PROTO.h" |
| 6 | #include "object.h" |
| 7 | #include "node.h" |
| 8 | #include "stringobject.h" |
| 9 | #include "funcobject.h" |
| 10 | #include "objimpl.h" |
| 11 | #include "token.h" |
| 12 | |
| 13 | typedef struct { |
| 14 | OB_HEAD |
| 15 | node *func_node; |
| 16 | object *func_globals; |
| 17 | } funcobject; |
| 18 | |
| 19 | object * |
| 20 | newfuncobject(n, globals) |
| 21 | node *n; |
| 22 | object *globals; |
| 23 | { |
| 24 | funcobject *op = NEWOBJ(funcobject, &Functype); |
| 25 | if (op != NULL) { |
| 26 | op->func_node = n; |
| 27 | if (globals != NULL) |
| 28 | INCREF(globals); |
| 29 | op->func_globals = globals; |
| 30 | } |
| 31 | return (object *)op; |
| 32 | } |
| 33 | |
| 34 | node * |
| 35 | getfuncnode(op) |
| 36 | object *op; |
| 37 | { |
| 38 | if (!is_funcobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 39 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | return NULL; |
| 41 | } |
| 42 | return ((funcobject *) op) -> func_node; |
| 43 | } |
| 44 | |
| 45 | object * |
| 46 | getfuncglobals(op) |
| 47 | object *op; |
| 48 | { |
| 49 | if (!is_funcobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 50 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | return NULL; |
| 52 | } |
| 53 | return ((funcobject *) op) -> func_globals; |
| 54 | } |
| 55 | |
| 56 | /* Methods */ |
| 57 | |
| 58 | static void |
| 59 | funcdealloc(op) |
| 60 | funcobject *op; |
| 61 | { |
| 62 | /* XXX free node? */ |
| 63 | DECREF(op->func_globals); |
| 64 | free((char *)op); |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | funcprint(op, fp, flags) |
| 69 | funcobject *op; |
| 70 | FILE *fp; |
| 71 | int flags; |
| 72 | { |
| 73 | node *n = op->func_node; |
| 74 | n = CHILD(n, 1); |
| 75 | fprintf(fp, "<user function %s>", STR(n)); |
| 76 | } |
| 77 | |
| 78 | static object * |
| 79 | funcrepr(op) |
| 80 | funcobject *op; |
| 81 | { |
| 82 | char buf[100]; |
| 83 | node *n = op->func_node; |
| 84 | n = CHILD(n, 1); |
| 85 | sprintf(buf, "<user function %.80s>", STR(n)); |
| 86 | return newstringobject(buf); |
| 87 | } |
| 88 | |
| 89 | typeobject Functype = { |
| 90 | OB_HEAD_INIT(&Typetype) |
| 91 | 0, |
| 92 | "function", |
| 93 | sizeof(funcobject), |
| 94 | 0, |
| 95 | funcdealloc, /*tp_dealloc*/ |
| 96 | funcprint, /*tp_print*/ |
| 97 | 0, /*tp_getattr*/ |
| 98 | 0, /*tp_setattr*/ |
| 99 | 0, /*tp_compare*/ |
| 100 | funcrepr, /*tp_repr*/ |
| 101 | }; |