-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPyCode.hpp
More file actions
140 lines (117 loc) · 4.14 KB
/
Copy pathPyCode.hpp
File metadata and controls
140 lines (117 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "PyObject.hpp"
#include "executable/Program.hpp"
#include <span>
namespace py {
class PyCode : public PyBaseObject
{
public:
// implementation details
const std::unique_ptr<Function> m_function;
const size_t m_register_count;
const std::vector<size_t> m_cell2arg;
// code object public attributes
// number of arguments (not including keyword only arguments, * or ** args)
const size_t m_arg_count;
// string of raw compiled bytecode
// const std::string m_code;
// tuple of names of cell variables (referenced by containing scopes)
const std::vector<std::string> m_cellvars;
// tuple of constants used in the bytecode
PyTuple *m_consts{ nullptr };
// name of file in which this code object was created
const std::string m_filename;
// number of first line in Python source code
const size_t m_first_line_number;
// bitmap of CO_* flags
CodeFlags m_flags;
// encoded mapping of line numbers to bytecode indices
PyDict *m_lnotab{ nullptr };
// tuple of names of free variables (referenced via a function’s closure)
const std::vector<std::string> m_freevars;
// number of positional only arguments
const size_t m_positional_only_arg_count;
// number of keyword only arguments (not including ** arg)
const size_t m_kwonly_arg_count;
// name with which this code object was defined
const std::string m_name;
// tuple of names other than arguments and function locals
const std::vector<std::string> m_names;
// number of local variables
const size_t m_nlocals;
// virtual machine stack space required
const size_t m_stack_size;
// tuple of names of arguments and local variables
const std::vector<std::string> m_varnames;
std::shared_ptr<Program> m_program;
PyCode(PyType *);
PyCode(std::unique_ptr<Function> &&function,
std::vector<size_t> &&cell2arg,
size_t arg_count,
std::vector<std::string> &&cellvars,
PyTuple *consts,
std::string &&filename,
size_t first_line_number,
CodeFlags flags,
std::vector<std::string> &&freevars,
size_t positional_arg_count,
size_t kwonly_arg_count,
size_t stack_size,
std::string &&name,
std::vector<std::string> &&names,
size_t nlocals,
std::vector<std::string> &&varnames);
public:
~PyCode();
static PyResult<PyCode *> create(std::unique_ptr<Function> &&function,
std::vector<size_t> cell2arg,
size_t arg_count,
std::vector<std::string> cellvars,
PyTuple *consts,
std::string filename,
size_t first_line_number,
CodeFlags flags,
std::vector<std::string> freevars,
size_t positional_arg_count,
size_t kwonly_arg_count,
size_t stack_size,
std::string name,
std::vector<std::string> names,
size_t nlocals,
std::vector<std::string> varnames);
static PyResult<PyCode *> create(std::shared_ptr<Program> program);
PyObject *call(PyTuple *args, PyDict *kwargs);
const std::vector<std::string> &varnames() const { return m_varnames; }
std::string to_string() const override;
PyResult<PyObject *> __repr__() const;
size_t register_count() const;
size_t freevars_count() const;
size_t cellvars_count() const;
size_t arg_count() const;
size_t kwonly_arg_count() const;
CodeFlags flags() const;
const std::vector<size_t> &cell2arg() const;
const PyTuple *consts() const;
const std::string &name() const { return m_name; }
const std::vector<std::string> &names() const;
const std::unique_ptr<Function> &function() const { return m_function; }
const std::shared_ptr<Program> &program() const { return m_program; }
static std::function<std::unique_ptr<TypePrototype>()> type_factory();
PyType *static_type() const override;
void visit_graph(Visitor &) override;
std::vector<uint8_t> serialize() const;
PyResult<PyObject *> eval(PyObject *globals,
PyObject *locals,
PyTuple *args,
PyDict *kwargs,
const std::vector<Value> &defaults,
const std::vector<Value> &kw_defaults,
const std::vector<Value> &closure,
PyString *name) const;
PyObject *make_function(const std::string &function_name,
const std::vector<py::Value> &default_values,
const std::vector<py::Value> &kw_default_values,
PyTuple *closure) const;
static std::pair<PyResult<PyCode *>, size_t> deserialize(std::span<const uint8_t> &,
std::shared_ptr<Program>);
};
}// namespace py