code.cpp 7.7 KB
Newer Older
Marius Wachtler's avatar
Marius Wachtler committed
1
// Copyright (c) 2014-2016 Dropbox, Inc.
2 3 4 5 6 7 8 9 10 11 12 13
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
14 15
//
#include "runtime/code.h"
16 17 18

#include <sstream>

19
#include "core/ast.h"
20 21 22 23 24
#include "runtime/objmodel.h"
#include "runtime/set.h"

namespace pyston {

25
extern "C" {
26
BoxedClass* code_cls;
27
}
28

29
BORROWED(Box*) BoxedCode::name(Box* b, void*) noexcept {
30
    RELEASE_ASSERT(b->cls == code_cls, "");
31 32
    BoxedCode* code = static_cast<BoxedCode*>(b);
    if (code->_name)
33 34
        return code->_name;
    return code->f->source->getName();
35
}
36

37
Box* BoxedCode::co_name(Box* b, void* arg) noexcept {
38 39 40
    return incref(name(b, arg));
}

41
BORROWED(Box*) BoxedCode::filename(Box* b, void*) noexcept {
42
    RELEASE_ASSERT(b->cls == code_cls, "");
43 44
    BoxedCode* code = static_cast<BoxedCode*>(b);
    if (code->_filename)
45 46 47 48
        return code->_filename;
    return code->f->source->getFn();
}

49
Box* BoxedCode::co_filename(Box* b, void* arg) noexcept {
50
    return incref(filename(b, arg));
51
}
52

53
Box* BoxedCode::firstlineno(Box* b, void*) noexcept {
54 55
    RELEASE_ASSERT(b->cls == code_cls, "");
    BoxedCode* code = static_cast<BoxedCode*>(b);
56
    FunctionMetadata* md = code->f;
57

58 59
    if (!md || !md->source)
        return boxInt(code->_firstline);
60

61
    if (md->source->ast->lineno == (uint32_t)-1)
62
        return boxInt(-1);
Kevin Modzelewski's avatar
Kevin Modzelewski committed
63

64
    return boxInt(md->source->ast->lineno);
65
}
66

67
Box* BoxedCode::argcount(Box* b, void*) noexcept {
68
    RELEASE_ASSERT(b->cls == code_cls, "");
69
    return boxInt(static_cast<BoxedCode*>(b)->f->num_args);
70
}
71

72
Box* BoxedCode::varnames(Box* b, void*) noexcept {
73 74 75 76 77
    RELEASE_ASSERT(b->cls == code_cls, "");
    BoxedCode* code = static_cast<BoxedCode*>(b);

    auto& param_names = code->f->param_names;
    if (!param_names.takes_param_names)
78
        return incref(EmptyTuple);
79

Kevin Modzelewski's avatar
Kevin Modzelewski committed
80
    std::vector<Box*> elts;
81
    for (auto sr : param_names.allArgsAsStr())
82
        elts.push_back(boxString(sr));
83 84 85 86
    auto rtn = BoxedTuple::create(elts.size(), &elts[0]);
    for (auto e : elts)
        Py_DECREF(e);
    return rtn;
87
}
88

89
Box* BoxedCode::flags(Box* b, void*) noexcept {
90 91 92 93
    RELEASE_ASSERT(b->cls == code_cls, "");
    BoxedCode* code = static_cast<BoxedCode*>(b);

    int flags = 0;
94
    if (code->f->param_names.has_vararg_name)
95
        flags |= CO_VARARGS;
96
    if (code->f->param_names.has_kwarg_name)
97 98 99 100
        flags |= CO_VARKEYWORDS;
    if (code->f->isGenerator())
        flags |= CO_GENERATOR;
    return boxInt(flags);
101 102
}

Kevin Modzelewski's avatar
Kevin Modzelewski committed
103
int BoxedCode::traverse(Box* self, visitproc visit, void* arg) noexcept {
104 105 106 107 108 109 110 111
    BoxedCode* o = static_cast<BoxedCode*>(self);
    Py_VISIT(o->_filename);
    Py_VISIT(o->_name);
    return 0;
}

void BoxedCode::dealloc(Box* b) noexcept {
    BoxedCode* o = static_cast<BoxedCode*>(b);
112 113 114

    PyObject_GC_UnTrack(o);

115 116
    Py_XDECREF(o->_filename);
    Py_XDECREF(o->_name);
117 118

    o->cls->tp_free(o);
119 120
}

121
FunctionMetadata* metadataFromCode(Box* code) {
122 123 124 125
    assert(code->cls == code_cls);
    return static_cast<BoxedCode*>(code)->f;
}

126 127 128 129 130 131 132 133 134 135
extern "C" PyCodeObject* PyCode_New(int argcount, int nlocals, int stacksize, int flags, PyObject* code,
                                    PyObject* consts, PyObject* names, PyObject* varnames, PyObject* freevars,
                                    PyObject* cellvars, PyObject* filename, PyObject* name, int firstlineno,
                                    PyObject* lnotab) noexcept {
    // Check if this is a dummy code object like PyCode_NewEmpty generates.
    // Because we currently support dummy ones only.
    bool is_dummy = argcount == 0 && nlocals == 0 && stacksize == 0 && flags == 0;
    is_dummy = is_dummy && code == EmptyString && lnotab == EmptyString;
    for (auto&& var : { consts, names, varnames, freevars, cellvars })
        is_dummy = is_dummy && var == EmptyTuple;
136 137 138 139
    // The follwing variables are not implemented but we allow them because there is currently
    // no way for code to retrieve them.
    auto temp_allowed = argcount || argcount || flags || varnames != EmptyTuple;
    RELEASE_ASSERT(is_dummy || temp_allowed, "not implemented");
140 141 142 143 144 145 146 147 148 149 150 151 152 153

    RELEASE_ASSERT(PyString_Check(filename), "");
    RELEASE_ASSERT(PyString_Check(name), "");

    return (PyCodeObject*)new BoxedCode(filename, name, firstlineno);
}

extern "C" PyCodeObject* PyCode_NewEmpty(const char* filename, const char* funcname, int firstlineno) noexcept {
    static PyObject* emptystring = NULL;
    static PyObject* nulltuple = NULL;
    PyObject* filename_ob = NULL;
    PyObject* funcname_ob = NULL;
    PyCodeObject* result = NULL;
    if (emptystring == NULL) {
154
        emptystring = PyGC_RegisterStaticConstant(PyString_FromString(""));
155 156 157 158
        if (emptystring == NULL)
            goto failed;
    }
    if (nulltuple == NULL) {
159
        nulltuple = PyGC_RegisterStaticConstant(PyTuple_New(0));
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        if (nulltuple == NULL)
            goto failed;
    }
    funcname_ob = PyString_FromString(funcname);
    if (funcname_ob == NULL)
        goto failed;
    filename_ob = PyString_FromString(filename);
    if (filename_ob == NULL)
        goto failed;

    result = PyCode_New(0,           /* argcount */
                        0,           /* nlocals */
                        0,           /* stacksize */
                        0,           /* flags */
                        emptystring, /* code */
                        nulltuple,   /* consts */
                        nulltuple,   /* names */
                        nulltuple,   /* varnames */
                        nulltuple,   /* freevars */
                        nulltuple,   /* cellvars */
                        filename_ob, /* filename */
                        funcname_ob, /* name */
                        firstlineno, /* firstlineno */
                        emptystring  /* lnotab */
                        );

failed:
    Py_XDECREF(funcname_ob);
    Py_XDECREF(filename_ob);
    return result;
190 191
}

192 193
extern "C" int PyCode_GetArgCount(PyCodeObject* op) noexcept {
    RELEASE_ASSERT(PyCode_Check((Box*)op), "");
194
    return unboxInt(autoDecref(BoxedCode::argcount((Box*)op, NULL)));
195 196
}

197
extern "C" BORROWED(PyObject*) PyCode_GetFilename(PyCodeObject* op) noexcept {
198 199 200
    RELEASE_ASSERT(PyCode_Check((Box*)op), "");
    return BoxedCode::filename((Box*)op, NULL);
}
201

202
extern "C" BORROWED(PyObject*) PyCode_GetName(PyCodeObject* op) noexcept {
203 204 205 206
    RELEASE_ASSERT(PyCode_Check((Box*)op), "");
    return BoxedCode::name((Box*)op, NULL);
}

207 208
extern "C" int PyCode_HasFreeVars(PyCodeObject* _code) noexcept {
    BoxedCode* code = (BoxedCode*)_code;
209
    return code->f->source->scoping.takesClosure() ? 1 : 0;
210 211
}

212
void setupCode() {
213
    code_cls
Kevin Modzelewski's avatar
Kevin Modzelewski committed
214 215
        = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedCode), false, "code", false,
                             (destructor)BoxedCode::dealloc, NULL, true, (traverseproc)BoxedCode::traverse, NOCLEAR);
216

217
    code_cls->giveAttrBorrowed("__new__", Py_None); // Hacky way of preventing users from instantiating this
218

219 220
    code_cls->giveAttrDescriptor("co_name", BoxedCode::co_name, NULL);
    code_cls->giveAttrDescriptor("co_filename", BoxedCode::co_filename, NULL);
221 222 223 224
    code_cls->giveAttrDescriptor("co_firstlineno", BoxedCode::firstlineno, NULL);
    code_cls->giveAttrDescriptor("co_argcount", BoxedCode::argcount, NULL);
    code_cls->giveAttrDescriptor("co_varnames", BoxedCode::varnames, NULL);
    code_cls->giveAttrDescriptor("co_flags", BoxedCode::flags, NULL);
225 226 227 228

    code_cls->freeze();
}
}