|
| 1 | +// Code objects |
| 2 | + |
| 3 | +package py |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// Code object |
| 11 | +type Code struct { |
| 12 | + // Object_HEAD |
| 13 | + co_argcount int // #arguments, except *args |
| 14 | + co_kwonlyargcount int // #keyword only arguments |
| 15 | + co_nlocals int // #local variables |
| 16 | + co_stacksize int // #entries needed for evaluation stack |
| 17 | + co_flags int // CO_..., see below |
| 18 | + co_code Object // instruction opcodes |
| 19 | + co_consts Object // list (constants used) |
| 20 | + co_names Object // list of strings (names used) |
| 21 | + co_varnames Object // tuple of strings (local variable names) |
| 22 | + co_freevars Object // tuple of strings (free variable names) |
| 23 | + co_cellvars Object // tuple of strings (cell variable names) |
| 24 | + // The rest doesn't count for hash or comparisons |
| 25 | + co_cell2arg *byte // Maps cell vars which are arguments. |
| 26 | + co_filename Object // unicode (where it was loaded from) |
| 27 | + co_name Object // unicode (name, for reference) |
| 28 | + co_firstlineno int // first source line number |
| 29 | + co_lnotab Object // string (encoding addr<->lineno mapping) See Objects/lnotab_notes.txt for details. |
| 30 | + |
| 31 | + co_weakreflist Object // to support weakrefs to code objects |
| 32 | +} |
| 33 | + |
| 34 | +const ( |
| 35 | + // Masks for co_flags above |
| 36 | + CO_OPTIMIZED = 0x0001 |
| 37 | + CO_NEWLOCALS = 0x0002 |
| 38 | + CO_VARARGS = 0x0004 |
| 39 | + CO_VARKEYWORDS = 0x0008 |
| 40 | + CO_NESTED = 0x0010 |
| 41 | + CO_GENERATOR = 0x0020 |
| 42 | + |
| 43 | + // The CO_NOFREE flag is set if there are no free or cell |
| 44 | + // variables. This information is redundant, but it allows a |
| 45 | + // single flag test to determine whether there is any extra |
| 46 | + // work to be done when the call frame it setup. |
| 47 | + |
| 48 | + CO_NOFREE = 0x0040 |
| 49 | + CO_GENERATOR_ALLOWED = 0x1000 |
| 50 | + CO_FUTURE_DIVISION = 0x2000 |
| 51 | + CO_FUTURE_ABSOLUTE_IMPORT = 0x4000 // do absolute imports by default |
| 52 | + CO_FUTURE_WITH_STATEMENT = 0x8000 |
| 53 | + CO_FUTURE_PRINT_FUNCTION = 0x10000 |
| 54 | + CO_FUTURE_UNICODE_LITERALS = 0x20000 |
| 55 | + CO_FUTURE_BARRY_AS_BDFL = 0x40000 |
| 56 | + |
| 57 | + // This value is found in the co_cell2arg array when the |
| 58 | + // associated cell variable does not correspond to an |
| 59 | + // argument. The maximum number of arguments is 255 (indexed |
| 60 | + // up to 254), so 255 work as a special flag. |
| 61 | + CO_CELL_NOT_AN_ARG = 255 |
| 62 | + |
| 63 | + CO_MAXBLOCKS = 20 // Max static block nesting within a function |
| 64 | +) |
| 65 | + |
| 66 | +func intern_strings(tuple Tuple) { |
| 67 | + for _, v_ := range tuple { |
| 68 | + v := v_.(String) |
| 69 | + fmt.Printf("Interning '%s'\n", v) |
| 70 | + // FIXME |
| 71 | + //PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const NAME_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" |
| 76 | + |
| 77 | +// all_name_chars(s): true iff all chars in s are valid NAME_CHARS |
| 78 | +func all_name_chars(s String) bool { |
| 79 | + for _, c := range s { |
| 80 | + if strings.IndexRune(NAME_CHARS, c) < 0 { |
| 81 | + return false |
| 82 | + } |
| 83 | + } |
| 84 | + return true |
| 85 | +} |
| 86 | + |
| 87 | +// Make a new code object |
| 88 | +func NewCode(argcount int, kwonlyargcount int, |
| 89 | + nlocals int, stacksize int, flags int, |
| 90 | + code Object, consts_ Object, names_ Object, |
| 91 | + varnames_ Object, freevars_ Object, cellvars_ Object, |
| 92 | + filename_ Object, name_ Object, firstlineno int, |
| 93 | + lnotab_ Object) (co *Code) { |
| 94 | + |
| 95 | + var cell2arg *byte |
| 96 | + |
| 97 | + // Type assert the objects |
| 98 | + consts := consts_.(Tuple) |
| 99 | + names := names_.(Tuple) |
| 100 | + varnames := varnames_.(Tuple) |
| 101 | + freevars := freevars_.(Tuple) |
| 102 | + cellvars := cellvars_.(Tuple) |
| 103 | + name := name_.(String) |
| 104 | + filename := filename_.(String) |
| 105 | + lnotab := lnotab_.(Bytes) |
| 106 | + |
| 107 | + // Check argument types |
| 108 | + if argcount < 0 || kwonlyargcount < 0 || nlocals < 0 { |
| 109 | + panic("Bad arguments to NewCode") |
| 110 | + } |
| 111 | + |
| 112 | + // Ensure that the filename is a ready Unicode string |
| 113 | + // FIXME |
| 114 | + // if PyUnicode_READY(filename) < 0 { |
| 115 | + // return nil; |
| 116 | + // } |
| 117 | + |
| 118 | + n_cellvars := len(cellvars) |
| 119 | + intern_strings(names) |
| 120 | + intern_strings(varnames) |
| 121 | + intern_strings(freevars) |
| 122 | + intern_strings(cellvars) |
| 123 | + /* Intern selected string constants */ |
| 124 | + for i := len(consts) - 1; i >= 0; i-- { |
| 125 | + v := consts[i].(String) |
| 126 | + if !all_name_chars(v) { |
| 127 | + continue |
| 128 | + } |
| 129 | + // FIXME PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
| 130 | + } |
| 131 | + /* Create mapping between cells and arguments if needed. */ |
| 132 | + if n_cellvars != 0 { |
| 133 | + total_args := argcount + kwonlyargcount |
| 134 | + if (flags & CO_VARARGS) != 0 { |
| 135 | + total_args++ |
| 136 | + } |
| 137 | + if (flags & CO_VARKEYWORDS) != 0 { |
| 138 | + total_args++ |
| 139 | + } |
| 140 | + used_cell2arg := false |
| 141 | + cell2arg := make([]byte, n_cellvars) |
| 142 | + for i := range cell2arg { |
| 143 | + cell2arg[i] = CO_CELL_NOT_AN_ARG |
| 144 | + } |
| 145 | + // Find cells which are also arguments. |
| 146 | + for i, cell := range cellvars { |
| 147 | + for j := 0; j < total_args; j++ { |
| 148 | + arg := varnames[j] |
| 149 | + if cell != arg { |
| 150 | + cell2arg[i] = byte(j) |
| 151 | + used_cell2arg = true |
| 152 | + break |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + if !used_cell2arg { |
| 157 | + cell2arg = nil |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + // FIXME co = PyObject_NEW(PyCodeObject, &PyCode_Type); |
| 162 | + |
| 163 | + co.co_argcount = argcount |
| 164 | + co.co_kwonlyargcount = kwonlyargcount |
| 165 | + co.co_nlocals = nlocals |
| 166 | + co.co_stacksize = stacksize |
| 167 | + co.co_flags = flags |
| 168 | + co.co_code = code |
| 169 | + co.co_consts = consts |
| 170 | + co.co_names = names |
| 171 | + co.co_varnames = varnames |
| 172 | + co.co_freevars = freevars |
| 173 | + co.co_cellvars = cellvars |
| 174 | + co.co_cell2arg = cell2arg |
| 175 | + co.co_filename = filename |
| 176 | + co.co_name = name |
| 177 | + co.co_firstlineno = firstlineno |
| 178 | + co.co_lnotab = lnotab |
| 179 | + co.co_weakreflist = nil |
| 180 | + return co |
| 181 | +} |
0 commit comments