blob: 1adc3bc9d094598e89147ec33985fc2622e0167c [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* 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
13typedef struct {
14 OB_HEAD
15 node *func_node;
16 object *func_globals;
17} funcobject;
18
19object *
20newfuncobject(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
34node *
35getfuncnode(op)
36 object *op;
37{
38 if (!is_funcobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000039 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040 return NULL;
41 }
42 return ((funcobject *) op) -> func_node;
43}
44
45object *
46getfuncglobals(op)
47 object *op;
48{
49 if (!is_funcobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000050 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 return NULL;
52 }
53 return ((funcobject *) op) -> func_globals;
54}
55
56/* Methods */
57
58static void
59funcdealloc(op)
60 funcobject *op;
61{
62 /* XXX free node? */
63 DECREF(op->func_globals);
64 free((char *)op);
65}
66
67static void
68funcprint(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
78static object *
79funcrepr(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
89typeobject 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};