Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | /* Frame object implementation */ |
| 26 | |
| 27 | #include "allobjects.h" |
| 28 | |
| 29 | #include "compile.h" |
| 30 | #include "frameobject.h" |
| 31 | #include "opcode.h" |
| 32 | #include "structmember.h" |
| 33 | |
| 34 | #define OFF(x) offsetof(frameobject, x) |
| 35 | |
| 36 | static struct memberlist frame_memberlist[] = { |
| 37 | {"f_back", T_OBJECT, OFF(f_back)}, |
| 38 | {"f_code", T_OBJECT, OFF(f_code)}, |
| 39 | {"f_globals", T_OBJECT, OFF(f_globals)}, |
| 40 | {"f_locals", T_OBJECT, OFF(f_locals)}, |
| 41 | {NULL} /* Sentinel */ |
| 42 | }; |
| 43 | |
| 44 | static object * |
| 45 | frame_getattr(f, name) |
| 46 | frameobject *f; |
| 47 | char *name; |
| 48 | { |
| 49 | return getmember((char *)f, frame_memberlist, name); |
| 50 | } |
| 51 | |
| 52 | static void |
| 53 | frame_dealloc(f) |
| 54 | frameobject *f; |
| 55 | { |
| 56 | XDECREF(f->f_back); |
| 57 | XDECREF(f->f_code); |
| 58 | XDECREF(f->f_globals); |
| 59 | XDECREF(f->f_locals); |
| 60 | XDEL(f->f_valuestack); |
| 61 | XDEL(f->f_blockstack); |
| 62 | DEL(f); |
| 63 | } |
| 64 | |
| 65 | typeobject Frametype = { |
| 66 | OB_HEAD_INIT(&Typetype) |
| 67 | 0, |
| 68 | "frame", |
| 69 | sizeof(frameobject), |
| 70 | 0, |
| 71 | frame_dealloc, /*tp_dealloc*/ |
| 72 | 0, /*tp_print*/ |
| 73 | frame_getattr, /*tp_getattr*/ |
| 74 | 0, /*tp_setattr*/ |
| 75 | 0, /*tp_compare*/ |
| 76 | 0, /*tp_repr*/ |
| 77 | 0, /*tp_as_number*/ |
| 78 | 0, /*tp_as_sequence*/ |
| 79 | 0, /*tp_as_mapping*/ |
| 80 | }; |
| 81 | |
| 82 | frameobject * |
| 83 | newframeobject(back, code, globals, locals, nvalues, nblocks) |
| 84 | frameobject *back; |
| 85 | codeobject *code; |
| 86 | object *globals; |
| 87 | object *locals; |
| 88 | int nvalues; |
| 89 | int nblocks; |
| 90 | { |
| 91 | frameobject *f; |
| 92 | if ((back != NULL && !is_frameobject(back)) || |
| 93 | code == NULL || !is_codeobject(code) || |
| 94 | globals == NULL || !is_dictobject(globals) || |
| 95 | locals == NULL || !is_dictobject(locals) || |
| 96 | nvalues < 0 || nblocks < 0) { |
| 97 | err_badcall(); |
| 98 | return NULL; |
| 99 | } |
| 100 | f = NEWOBJ(frameobject, &Frametype); |
| 101 | if (f != NULL) { |
| 102 | if (back) |
| 103 | INCREF(back); |
| 104 | f->f_back = back; |
| 105 | INCREF(code); |
| 106 | f->f_code = code; |
| 107 | INCREF(globals); |
| 108 | f->f_globals = globals; |
| 109 | INCREF(locals); |
| 110 | f->f_locals = locals; |
| 111 | f->f_valuestack = NEW(object *, nvalues+1); |
| 112 | f->f_blockstack = NEW(block, nblocks+1); |
| 113 | f->f_nvalues = nvalues; |
| 114 | f->f_nblocks = nblocks; |
| 115 | f->f_iblock = 0; |
| 116 | if (f->f_valuestack == NULL || f->f_blockstack == NULL) { |
| 117 | err_nomem(); |
| 118 | DECREF(f); |
| 119 | f = NULL; |
| 120 | } |
| 121 | } |
| 122 | return f; |
| 123 | } |
| 124 | |
| 125 | /* Block management */ |
| 126 | |
| 127 | void |
| 128 | setup_block(f, type, handler, level) |
| 129 | frameobject *f; |
| 130 | int type; |
| 131 | int handler; |
| 132 | int level; |
| 133 | { |
| 134 | block *b; |
| 135 | if (f->f_iblock >= f->f_nblocks) { |
| 136 | fprintf(stderr, "XXX block stack overflow\n"); |
| 137 | abort(); |
| 138 | } |
| 139 | b = &f->f_blockstack[f->f_iblock++]; |
| 140 | b->b_type = type; |
| 141 | b->b_level = level; |
| 142 | b->b_handler = handler; |
| 143 | } |
| 144 | |
| 145 | block * |
| 146 | pop_block(f) |
| 147 | frameobject *f; |
| 148 | { |
| 149 | block *b; |
| 150 | if (f->f_iblock <= 0) { |
| 151 | fprintf(stderr, "XXX block stack underflow\n"); |
| 152 | abort(); |
| 153 | } |
| 154 | b = &f->f_blockstack[--f->f_iblock]; |
| 155 | return b; |
| 156 | } |