Commit 08509b08 authored by Kevin Modzelewski's avatar Kevin Modzelewski

get enough dealloc/traverse/clear functions in to get it to try the GC

parent 05fcd11f
...@@ -71,13 +71,14 @@ void setupAST() { ...@@ -71,13 +71,14 @@ void setupAST() {
// ::create takes care of registering the class as a GC root. // ::create takes care of registering the class as a GC root.
#define MAKE_CLS(name, base_cls) \ #define MAKE_CLS(name, base_cls) \
BoxedClass* name##_cls = BoxedClass::create(type_cls, base_cls, 0, 0, sizeof(BoxedAST), false, STRINGIFY(name)); \ BoxedClass* name##_cls \
= BoxedClass::create(type_cls, base_cls, 0, 0, sizeof(BoxedAST), false, STRINGIFY(name), NULL, NULL, false); \
ast_module->giveAttrBorrowed(STRINGIFY(name), name##_cls); \ ast_module->giveAttrBorrowed(STRINGIFY(name), name##_cls); \
type_to_cls[AST_TYPE::name] = name##_cls; \ type_to_cls[AST_TYPE::name] = name##_cls; \
name##_cls->giveAttr("__module__", boxString("_ast")); \ name##_cls->giveAttr("__module__", boxString("_ast")); \
name##_cls->freeze() name##_cls->freeze()
AST_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedAST), false, "AST"); AST_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedAST), false, "AST", NULL, NULL, false);
// ::create takes care of registering the class as a GC root. // ::create takes care of registering the class as a GC root.
AST_cls->giveAttr("__module__", boxString("_ast")); AST_cls->giveAttr("__module__", boxString("_ast"));
AST_cls->freeze(); AST_cls->freeze();
......
...@@ -1208,6 +1208,13 @@ public: ...@@ -1208,6 +1208,13 @@ public:
BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self); BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self);
return boxBool(self->iterator != self->iterator_end); return boxBool(self->iterator != self->iterator_end);
} }
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
}; };
Box* globals() { Box* globals() {
...@@ -1842,7 +1849,7 @@ void setupBuiltins() { ...@@ -1842,7 +1849,7 @@ void setupBuiltins() {
"Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is " "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is "
"the `nil' object; Ellipsis represents `...' in slices."); "the `nil' object; Ellipsis represents `...' in slices.");
ellipsis_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(Box), false, "ellipsis"); ellipsis_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(Box), false, "ellipsis", NULL, NULL, false);
ellipsis_cls->giveAttr("__repr__", new BoxedFunction(FunctionMetadata::create((void*)ellipsisRepr, STR, 1))); ellipsis_cls->giveAttr("__repr__", new BoxedFunction(FunctionMetadata::create((void*)ellipsisRepr, STR, 1)));
Ellipsis = new (ellipsis_cls) Box(); Ellipsis = new (ellipsis_cls) Box();
assert(Ellipsis->cls); assert(Ellipsis->cls);
...@@ -1857,7 +1864,8 @@ void setupBuiltins() { ...@@ -1857,7 +1864,8 @@ void setupBuiltins() {
"print", new BoxedBuiltinFunctionOrMethod(FunctionMetadata::create((void*)print, NONE, 0, true, true), "print", "print", new BoxedBuiltinFunctionOrMethod(FunctionMetadata::create((void*)print, NONE, 0, true, true), "print",
print_doc)); print_doc));
notimplemented_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(Box), false, "NotImplementedType"); notimplemented_cls
= BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(Box), false, "NotImplementedType", NULL, NULL, false);
notimplemented_cls->giveAttr("__repr__", notimplemented_cls->giveAttr("__repr__",
new BoxedFunction(FunctionMetadata::create((void*)notimplementedRepr, STR, 1))); new BoxedFunction(FunctionMetadata::create((void*)notimplementedRepr, STR, 1)));
notimplemented_cls->freeze(); notimplemented_cls->freeze();
...@@ -1969,7 +1977,7 @@ void setupBuiltins() { ...@@ -1969,7 +1977,7 @@ void setupBuiltins() {
{ None, None, None, autoDecref(boxInt(-1)) }, NULL, import_doc)); { None, None, None, autoDecref(boxInt(-1)) }, NULL, import_doc));
enumerate_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedEnumerate), enumerate_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedEnumerate),
false, "enumerate"); false, "enumerate", BoxedEnumerate::dealloc, NULL, true, BoxedEnumerate::traverse, NOCLEAR);
enumerate_cls->giveAttr( enumerate_cls->giveAttr(
"__new__", new BoxedFunction(FunctionMetadata::create((void*)BoxedEnumerate::new_, UNKNOWN, 3, false, false), "__new__", new BoxedFunction(FunctionMetadata::create((void*)BoxedEnumerate::new_, UNKNOWN, 3, false, false),
{ autoDecref(boxInt(0)) })); { autoDecref(boxInt(0)) }));
......
...@@ -180,6 +180,10 @@ public: ...@@ -180,6 +180,10 @@ public:
} }
Py_RETURN_TRUE; Py_RETURN_TRUE;
} }
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
}; };
...@@ -223,7 +227,8 @@ void setupThread() { ...@@ -223,7 +227,8 @@ void setupThread() {
thread_module->giveAttr("_count", new BoxedBuiltinFunctionOrMethod( thread_module->giveAttr("_count", new BoxedBuiltinFunctionOrMethod(
FunctionMetadata::create((void*)threadCount, BOXED_INT, 0), "_count")); FunctionMetadata::create((void*)threadCount, BOXED_INT, 0), "_count"));
thread_lock_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedThreadLock), false, "lock"); thread_lock_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedThreadLock), false, "lock",
BoxedThreadLock::dealloc, NULL, false);
thread_lock_cls->tp_dealloc = BoxedThreadLock::threadLockDestructor; thread_lock_cls->tp_dealloc = BoxedThreadLock::threadLockDestructor;
thread_lock_cls->has_safe_tp_dealloc = true; thread_lock_cls->has_safe_tp_dealloc = true;
thread_lock_cls->instances_are_nonzero = true; thread_lock_cls->instances_are_nonzero = true;
...@@ -245,7 +250,7 @@ void setupThread() { ...@@ -245,7 +250,7 @@ void setupThread() {
thread_lock_cls->freeze(); thread_lock_cls->freeze();
ThreadError = BoxedClass::create(type_cls, Exception, Exception->attrs_offset, Exception->tp_weaklistoffset, ThreadError = BoxedClass::create(type_cls, Exception, Exception->attrs_offset, Exception->tp_weaklistoffset,
Exception->tp_basicsize, false, "error"); Exception->tp_basicsize, false, "error", NULL, NULL, false);
ThreadError->giveAttr("__module__", boxString("thread")); ThreadError->giveAttr("__module__", boxString("thread"));
ThreadError->freeze(); ThreadError->freeze();
......
...@@ -1641,10 +1641,14 @@ extern "C" int PyMethod_ClearFreeList(void) noexcept { ...@@ -1641,10 +1641,14 @@ extern "C" int PyMethod_ClearFreeList(void) noexcept {
} }
void setupClassobj() { void setupClassobj() {
classobj_cls = BoxedClass::create(type_cls, object_cls, offsetof(BoxedClassobj, attrs), classobj_cls
offsetof(BoxedClassobj, weakreflist), sizeof(BoxedClassobj), false, "classobj"); = BoxedClass::create(type_cls, object_cls, offsetof(BoxedClassobj, attrs), offsetof(BoxedClassobj, weakreflist),
instance_cls = BoxedClass::create(type_cls, object_cls, offsetof(BoxedInstance, attrs), sizeof(BoxedClassobj), false, "classobj", (destructor)BoxedClassobj::dealloc, NULL, true,
offsetof(BoxedInstance, weakreflist), sizeof(BoxedInstance), false, "instance"); (traverseproc)BoxedClassobj::traverse, (inquiry)BoxedClassobj::clear);
instance_cls
= BoxedClass::create(type_cls, object_cls, offsetof(BoxedInstance, attrs), offsetof(BoxedInstance, weakreflist),
sizeof(BoxedInstance), false, "instance", (destructor)BoxedInstance::dealloc, NULL, true,
(traverseproc)BoxedInstance::traverse, (inquiry)BoxedInstance::clear);
classobj_cls->giveAttr("__new__", classobj_cls->giveAttr("__new__",
new BoxedFunction(FunctionMetadata::create((void*)classobjNew, UNKNOWN, 4, false, false))); new BoxedFunction(FunctionMetadata::create((void*)classobjNew, UNKNOWN, 4, false, false)));
......
...@@ -49,6 +49,16 @@ public: ...@@ -49,6 +49,16 @@ public:
Box** weakreflist; Box** weakreflist;
BoxedClassobj(BoxedString* name, BoxedTuple* bases) : bases(bases), name(name) {} BoxedClassobj(BoxedString* name, BoxedTuple* bases) : bases(bases), name(name) {}
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
static int clear(Box* self) noexcept {
Py_FatalError("unimplemented");
}
}; };
class BoxedInstance : public Box { class BoxedInstance : public Box {
...@@ -62,6 +72,16 @@ public: ...@@ -62,6 +72,16 @@ public:
BoxedInstance(BoxedClassobj* inst_cls) : inst_cls(inst_cls) {} BoxedInstance(BoxedClassobj* inst_cls) : inst_cls(inst_cls) {}
DEFAULT_CLASS(instance_cls); DEFAULT_CLASS(instance_cls);
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
static int clear(Box* self) noexcept {
Py_FatalError("unimplemented");
}
}; };
Box* instance_getattro(Box* cls, Box* attr) noexcept; Box* instance_getattro(Box* cls, Box* attr) noexcept;
......
...@@ -111,7 +111,7 @@ extern "C" int PyCode_GetArgCount(PyCodeObject* op) noexcept { ...@@ -111,7 +111,7 @@ extern "C" int PyCode_GetArgCount(PyCodeObject* op) noexcept {
} }
void setupCode() { void setupCode() {
code_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedCode), false, "code"); code_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedCode), false, "code", NULL, NULL, false);
code_cls->giveAttrBorrowed("__new__", None); // Hacky way of preventing users from instantiating this code_cls->giveAttrBorrowed("__new__", None); // Hacky way of preventing users from instantiating this
......
...@@ -812,15 +812,19 @@ void setupDict() { ...@@ -812,15 +812,19 @@ void setupDict() {
static PySequenceMethods dict_as_sequence; static PySequenceMethods dict_as_sequence;
dict_cls->tp_as_sequence = &dict_as_sequence; dict_cls->tp_as_sequence = &dict_as_sequence;
dict_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, dict_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedDictIterator), false,
sizeof(BoxedDictIterator), false, "dictionary-itemiterator"); "dictionary-itemiterator", (destructor)BoxedDictIterator::dealloc, NULL,
true, (traverseproc)BoxedDictIterator::traverse, NOCLEAR);
dict_keys_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedDictView),
false, "dict_keys"); dict_keys_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedDictView), false, "dict_keys",
dict_values_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedDictView), (destructor)BoxedDictView::dealloc, NULL, true,
false, "dict_values"); (traverseproc)BoxedDictView::traverse, NOCLEAR);
dict_items_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedDictView), dict_values_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedDictView), false, "dict_values",
false, "dict_items"); (destructor)BoxedDictView::dealloc, NULL, true,
(traverseproc)BoxedDictView::traverse, NOCLEAR);
dict_items_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedDictView), false, "dict_items",
(destructor)BoxedDictView::dealloc, NULL, true,
(traverseproc)BoxedDictView::traverse, NOCLEAR);
dict_iterator_cls->instances_are_nonzero = dict_keys_cls->instances_are_nonzero dict_iterator_cls->instances_are_nonzero = dict_keys_cls->instances_are_nonzero
= dict_values_cls->instances_are_nonzero = dict_items_cls->instances_are_nonzero = true; = dict_values_cls->instances_are_nonzero = dict_items_cls->instances_are_nonzero = true;
......
...@@ -37,6 +37,17 @@ public: ...@@ -37,6 +37,17 @@ public:
BoxedDictIterator(BoxedDict* d, IteratorType type); BoxedDictIterator(BoxedDict* d, IteratorType type);
DEFAULT_CLASS(dict_iterator_cls); DEFAULT_CLASS(dict_iterator_cls);
static void dealloc(BoxedDictIterator* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->d);
o->cls->tp_free(o);
}
static int traverse(BoxedDictIterator* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->d);
return 0;
}
}; };
Box* dictGetitem(BoxedDict* self, Box* k); Box* dictGetitem(BoxedDict* self, Box* k);
...@@ -55,6 +66,17 @@ class BoxedDictView : public Box { ...@@ -55,6 +66,17 @@ class BoxedDictView : public Box {
public: public:
BoxedDict* d; BoxedDict* d;
BoxedDictView(BoxedDict* d); BoxedDictView(BoxedDict* d);
static void dealloc(BoxedDictIterator* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->d);
o->cls->tp_free(o);
}
static int traverse(BoxedDictIterator* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->d);
return 0;
}
}; };
Box* dictViewKeysIter(Box* self); Box* dictViewKeysIter(Box* self);
......
...@@ -130,6 +130,16 @@ public: ...@@ -130,6 +130,16 @@ public:
return fi->frame_obj; return fi->frame_obj;
} }
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
static int clear(Box* self) noexcept {
Py_FatalError("unimplemented");
}
}; };
Box* getFrame(int depth) { Box* getFrame(int depth) {
...@@ -141,8 +151,9 @@ Box* getFrame(int depth) { ...@@ -141,8 +151,9 @@ Box* getFrame(int depth) {
} }
void setupFrame() { void setupFrame() {
frame_cls frame_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedFrame), false, "frame",
= BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedFrame), false, "frame"); (destructor)BoxedFrame::dealloc, NULL, true, (traverseproc)BoxedFrame::traverse,
(inquiry)BoxedFrame::clear);
frame_cls->tp_dealloc = BoxedFrame::simpleDestructor; frame_cls->tp_dealloc = BoxedFrame::simpleDestructor;
frame_cls->has_safe_tp_dealloc = true; frame_cls->has_safe_tp_dealloc = true;
......
...@@ -423,12 +423,6 @@ Box* generatorName(Box* _self, void* context) { ...@@ -423,12 +423,6 @@ Box* generatorName(Box* _self, void* context) {
return self->function->md->source->getName(); return self->function->md->source->getName();
} }
void generatorDestructor(Box* b) {
assert(isSubclass(b->cls, generator_cls));
BoxedGenerator* self = static_cast<BoxedGenerator*>(b);
freeGeneratorStack(self);
}
extern "C" int PyGen_NeedsFinalizing(PyGenObject* gen) noexcept { extern "C" int PyGen_NeedsFinalizing(PyGenObject* gen) noexcept {
Py_FatalError("unimplemented"); Py_FatalError("unimplemented");
#if 0 #if 0
...@@ -450,11 +444,21 @@ extern "C" int PyGen_NeedsFinalizing(PyGenObject* gen) noexcept { ...@@ -450,11 +444,21 @@ extern "C" int PyGen_NeedsFinalizing(PyGenObject* gen) noexcept {
#endif #endif
} }
static void generator_dealloc(BoxedGenerator* self) noexcept {
assert(isSubclass(self->cls, generator_cls));
freeGeneratorStack(self);
Py_FatalError("unimplemented");
}
static int generator_traverse(BoxedGenerator* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
void setupGenerator() { void setupGenerator() {
generator_cls generator_cls = BoxedClass::create(type_cls, object_cls, 0, offsetof(BoxedGenerator, weakreflist),
= BoxedClass::create(type_cls, object_cls, 0, offsetof(BoxedGenerator, weakreflist), sizeof(BoxedGenerator), false, "generator", (destructor)generator_dealloc, NULL,
sizeof(BoxedGenerator), false, "generator"); true, (traverseproc)generator_traverse, NOCLEAR);
generator_cls->tp_dealloc = generatorDestructor;
generator_cls->has_safe_tp_dealloc = true; generator_cls->has_safe_tp_dealloc = true;
generator_cls->giveAttr( generator_cls->giveAttr(
"__iter__", new BoxedFunction(FunctionMetadata::create((void*)generatorIter, typeFromClass(generator_cls), 1))); "__iter__", new BoxedFunction(FunctionMetadata::create((void*)generatorIter, typeFromClass(generator_cls), 1)));
......
...@@ -933,7 +933,7 @@ void setupImport() { ...@@ -933,7 +933,7 @@ void setupImport() {
imp_module->giveAttr("C_BUILTIN", boxInt(SearchResult::C_BUILTIN)); imp_module->giveAttr("C_BUILTIN", boxInt(SearchResult::C_BUILTIN));
imp_module->giveAttr("PY_FROZEN", boxInt(SearchResult::PY_FROZEN)); imp_module->giveAttr("PY_FROZEN", boxInt(SearchResult::PY_FROZEN));
null_importer_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(Box), false, "NullImporter"); null_importer_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(Box), false, "NullImporter", NULL, NULL, false);
null_importer_cls->giveAttr( null_importer_cls->giveAttr(
"__init__", "__init__",
new BoxedFunction(FunctionMetadata::create((void*)nullImporterInit, NONE, 2, false, false), { None })); new BoxedFunction(FunctionMetadata::create((void*)nullImporterInit, NONE, 2, false, false), { None }));
......
...@@ -130,6 +130,13 @@ public: ...@@ -130,6 +130,13 @@ public:
static Box* xrangeIteratorNext(Box* s) __attribute__((visibility("default"))) { static Box* xrangeIteratorNext(Box* s) __attribute__((visibility("default"))) {
return boxInt(xrangeIteratorNextUnboxed(s)); return boxInt(xrangeIteratorNextUnboxed(s));
} }
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
}; };
Box* xrange(Box* cls, Box* start, Box* stop, Box** args) { Box* xrange(Box* cls, Box* start, Box* stop, Box** args) {
...@@ -236,9 +243,10 @@ Box* xrangeReduce(Box* self) { ...@@ -236,9 +243,10 @@ Box* xrangeReduce(Box* self) {
} }
void setupXrange() { void setupXrange() {
xrange_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedXrange), false, "xrange"); xrange_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedXrange), false, "xrange", NULL, NULL, false);
xrange_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, xrange_iterator_cls
sizeof(BoxedXrangeIterator), false, "rangeiterator"); = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedXrangeIterator), false, "rangeiterator",
BoxedXrangeIterator::dealloc, NULL, true, BoxedXrangeIterator::traverse, NOCLEAR);
static PySequenceMethods xrange_as_sequence; static PySequenceMethods xrange_as_sequence;
xrange_cls->tp_as_sequence = &xrange_as_sequence; xrange_cls->tp_as_sequence = &xrange_as_sequence;
......
...@@ -189,7 +189,9 @@ bool calliter_hasnext(Box* b) { ...@@ -189,7 +189,9 @@ bool calliter_hasnext(Box* b) {
void setupIter() { void setupIter() {
seqiter_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedSeqIter), false, "iterator"); seqiter_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedSeqIter), false, "iterator",
(destructor)BoxedSeqIter::dealloc, NULL, true,
(traverseproc)BoxedSeqIter::traverse, NOCLEAR);
seqiter_cls->giveAttr("next", new BoxedFunction(FunctionMetadata::create((void*)seqiterNext, UNKNOWN, 1))); seqiter_cls->giveAttr("next", new BoxedFunction(FunctionMetadata::create((void*)seqiterNext, UNKNOWN, 1)));
seqiter_cls->giveAttr("__hasnext__", seqiter_cls->giveAttr("__hasnext__",
...@@ -201,8 +203,9 @@ void setupIter() { ...@@ -201,8 +203,9 @@ void setupIter() {
seqiter_cls->tp_iter = PyObject_SelfIter; seqiter_cls->tp_iter = PyObject_SelfIter;
seqiter_cls->tp_iternext = seqiter_next; seqiter_cls->tp_iternext = seqiter_next;
seqreviter_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedSeqIter), seqreviter_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedSeqIter), false, "reversed",
false, "reversed"); (destructor)BoxedSeqIter::dealloc, NULL, true,
(traverseproc)BoxedSeqIter::traverse, NOCLEAR);
seqreviter_cls->giveAttr("next", new BoxedFunction(FunctionMetadata::create((void*)seqiterNext, UNKNOWN, 1))); seqreviter_cls->giveAttr("next", new BoxedFunction(FunctionMetadata::create((void*)seqiterNext, UNKNOWN, 1)));
seqreviter_cls->giveAttr("__hasnext__", seqreviter_cls->giveAttr("__hasnext__",
...@@ -213,8 +216,9 @@ void setupIter() { ...@@ -213,8 +216,9 @@ void setupIter() {
seqreviter_cls->tp_iter = PyObject_SelfIter; seqreviter_cls->tp_iter = PyObject_SelfIter;
seqreviter_cls->tp_iternext = seqiter_next; seqreviter_cls->tp_iternext = seqiter_next;
iterwrapper_cls = BoxedClass::create(type_cls, object_cls, 0, 0, iterwrapper_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedIterWrapper), false, "iterwrapper",
sizeof(BoxedIterWrapper), false, "iterwrapper"); (destructor)BoxedIterWrapper::dealloc, NULL, true,
(traverseproc)BoxedIterWrapper::traverse, NOCLEAR);
iterwrapper_cls->giveAttr("next", new BoxedFunction(FunctionMetadata::create((void*)iterwrapperNext, UNKNOWN, 1))); iterwrapper_cls->giveAttr("next", new BoxedFunction(FunctionMetadata::create((void*)iterwrapperNext, UNKNOWN, 1)));
iterwrapper_cls->giveAttr("__hasnext__", iterwrapper_cls->giveAttr("__hasnext__",
......
...@@ -36,6 +36,19 @@ public: ...@@ -36,6 +36,19 @@ public:
BoxedSeqIter(Box* b, int64_t start) : b(b), idx(start), next(NULL) {} BoxedSeqIter(Box* b, int64_t start) : b(b), idx(start), next(NULL) {}
DEFAULT_CLASS(seqiter_cls); DEFAULT_CLASS(seqiter_cls);
static void dealloc(BoxedSeqIter* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->b);
Py_XDECREF(o->next);
o->cls->tp_free(o);
}
static int traverse(BoxedSeqIter* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->b);
Py_VISIT(self->next);
return 0;
}
}; };
extern BoxedClass* iterwrapper_cls; extern BoxedClass* iterwrapper_cls;
...@@ -49,6 +62,19 @@ public: ...@@ -49,6 +62,19 @@ public:
BoxedIterWrapper(Box* iter) : iter(iter), next(NULL) {} BoxedIterWrapper(Box* iter) : iter(iter), next(NULL) {}
DEFAULT_CLASS(iterwrapper_cls); DEFAULT_CLASS(iterwrapper_cls);
static void dealloc(BoxedIterWrapper* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->iter);
Py_XDECREF(o->next);
o->cls->tp_free(o);
}
static int traverse(BoxedIterWrapper* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->iter);
Py_VISIT(self->next);
return 0;
}
}; };
bool calliter_hasnext(Box* b); bool calliter_hasnext(Box* b);
......
...@@ -1321,10 +1321,12 @@ void setupList() { ...@@ -1321,10 +1321,12 @@ void setupList() {
static PyMappingMethods list_as_mapping; static PyMappingMethods list_as_mapping;
list_cls->tp_as_mapping = &list_as_mapping; list_cls->tp_as_mapping = &list_as_mapping;
list_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, list_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedListIterator), false, "listiterator",
sizeof(BoxedListIterator), false, "listiterator"); (destructor)BoxedListIterator::dealloc, NULL, true,
list_reverse_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, (traverseproc)BoxedListIterator::traverse, NOCLEAR);
sizeof(BoxedListIterator), false, "listreverseiterator"); list_reverse_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedListIterator), false,
"listreverseiterator", (destructor)BoxedListIterator::dealloc, NULL,
true, (traverseproc)BoxedListIterator::traverse, NOCLEAR);
list_iterator_cls->instances_are_nonzero = list_reverse_iterator_cls->instances_are_nonzero = true; list_iterator_cls->instances_are_nonzero = list_reverse_iterator_cls->instances_are_nonzero = true;
list_cls->giveAttr("__len__", new BoxedFunction(FunctionMetadata::create((void*)listLen, BOXED_INT, 1))); list_cls->giveAttr("__len__", new BoxedFunction(FunctionMetadata::create((void*)listLen, BOXED_INT, 1)));
......
...@@ -29,6 +29,17 @@ public: ...@@ -29,6 +29,17 @@ public:
BoxedListIterator(BoxedList* l, int start); BoxedListIterator(BoxedList* l, int start);
DEFAULT_CLASS(list_iterator_cls); DEFAULT_CLASS(list_iterator_cls);
static void dealloc(BoxedListIterator* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->l);
o->cls->tp_free(o);
}
static int traverse(BoxedListIterator* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->l);
return 0;
}
}; };
Box* listIter(Box* self) noexcept; Box* listIter(Box* self) noexcept;
......
...@@ -414,10 +414,12 @@ BoxedClass::BoxedClass(BoxedClass* base, int attrs_offset, int weaklist_offset, ...@@ -414,10 +414,12 @@ BoxedClass::BoxedClass(BoxedClass* base, int attrs_offset, int weaklist_offset,
bool ok_noclear = (clear == NOCLEAR); bool ok_noclear = (clear == NOCLEAR);
if (ok_noclear) if (ok_noclear)
clear = NULL; clear = NULL;
if (is_gc) { if (clear)
ASSERT(traverse, "%s", name); assert(traverse);
ASSERT(dealloc, "%s", name); if (traverse)
} assert(dealloc);
if (dealloc)
assert(traverse || !is_gc);
ASSERT(((bool)traverse == (bool)clear) || ok_noclear, "%s", name); ASSERT(((bool)traverse == (bool)clear) || ok_noclear, "%s", name);
classes.push_back(this); classes.push_back(this);
......
...@@ -43,6 +43,17 @@ public: ...@@ -43,6 +43,17 @@ public:
++it; ++it;
return rtn; return rtn;
} }
static void dealloc(BoxedSetIterator* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->s);
o->cls->tp_free(o);
}
static int traverse(BoxedSetIterator* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->s);
return 0;
}
}; };
Box* setiteratorHasnext(BoxedSetIterator* self) { Box* setiteratorHasnext(BoxedSetIterator* self) {
...@@ -804,6 +815,7 @@ using namespace pyston::set; ...@@ -804,6 +815,7 @@ using namespace pyston::set;
void BoxedSet::dealloc(Box* _o) noexcept { void BoxedSet::dealloc(Box* _o) noexcept {
BoxedSet* o = (BoxedSet*)_o; BoxedSet* o = (BoxedSet*)_o;
PyObject_GC_UnTrack(o);
for (auto p : o->s) { for (auto p : o->s) {
Py_DECREF(p.value); Py_DECREF(p.value);
} }
...@@ -852,8 +864,9 @@ void setupSet() { ...@@ -852,8 +864,9 @@ void setupSet() {
set_cls->tp_dealloc = frozenset_cls->tp_dealloc = BoxedSet::dealloc; set_cls->tp_dealloc = frozenset_cls->tp_dealloc = BoxedSet::dealloc;
set_cls->has_safe_tp_dealloc = frozenset_cls->has_safe_tp_dealloc = true; set_cls->has_safe_tp_dealloc = frozenset_cls->has_safe_tp_dealloc = true;
set_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, set_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedSetIterator), false, "setiterator",
sizeof(BoxedSetIterator), false, "setiterator"); (destructor)BoxedSetIterator::dealloc, NULL, true,
(traverseproc)BoxedSetIterator::traverse, NOCLEAR);
set_iterator_cls->giveAttr("__iter__", new BoxedFunction(FunctionMetadata::create( set_iterator_cls->giveAttr("__iter__", new BoxedFunction(FunctionMetadata::create(
(void*)setiteratorIter, typeFromClass(set_iterator_cls), 1))); (void*)setiteratorIter, typeFromClass(set_iterator_cls), 1)));
set_iterator_cls->giveAttr("__hasnext__", set_iterator_cls->giveAttr("__hasnext__",
......
...@@ -2405,6 +2405,17 @@ public: ...@@ -2405,6 +2405,17 @@ public:
++self->it; ++self->it;
return characters[c & UCHAR_MAX]; return characters[c & UCHAR_MAX];
} }
static void dealloc(BoxedStringIterator* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->s);
o->cls->tp_free(o);
}
static int traverse(BoxedStringIterator* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->s);
return 0;
}
}; };
Box* strIter(BoxedString* self) noexcept { Box* strIter(BoxedString* self) noexcept {
...@@ -2801,8 +2812,9 @@ void setupStr() { ...@@ -2801,8 +2812,9 @@ void setupStr() {
str_cls->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER; str_cls->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
str_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, str_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedStringIterator), false, "striterator",
sizeof(BoxedStringIterator), false, "striterator"); (destructor)BoxedStringIterator::dealloc, NULL, true,
(traverseproc)BoxedStringIterator::traverse, NOCLEAR);
str_iterator_cls->giveAttr( str_iterator_cls->giveAttr(
"__hasnext__", new BoxedFunction(FunctionMetadata::create((void*)BoxedStringIterator::hasnext, BOXED_BOOL, 1))); "__hasnext__", new BoxedFunction(FunctionMetadata::create((void*)BoxedStringIterator::hasnext, BOXED_BOOL, 1)));
str_iterator_cls->giveAttr( str_iterator_cls->giveAttr(
......
...@@ -35,6 +35,13 @@ public: ...@@ -35,6 +35,13 @@ public:
BoxedSuper(BoxedClass* type, Box* obj, BoxedClass* obj_type) : type(type), obj(obj), obj_type(obj_type) {} BoxedSuper(BoxedClass* type, Box* obj, BoxedClass* obj_type) : type(type), obj(obj), obj_type(obj_type) {}
DEFAULT_CLASS(super_cls); DEFAULT_CLASS(super_cls);
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
}; };
static const char* class_str = "__class__"; static const char* class_str = "__class__";
...@@ -185,7 +192,8 @@ Box* superInit(Box* _self, Box* _type, Box* obj) { ...@@ -185,7 +192,8 @@ Box* superInit(Box* _self, Box* _type, Box* obj) {
void setupSuper() { void setupSuper() {
super_cls super_cls
= BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedSuper), false, "super"); = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedSuper), false, "super",
(destructor)BoxedSuper::dealloc, NULL, true, (traverseproc)BoxedSuper::traverse, NOCLEAR);
// super_cls->giveAttr("__getattribute__", new BoxedFunction(FunctionMetadata::create((void*)superGetattribute, // super_cls->giveAttr("__getattribute__", new BoxedFunction(FunctionMetadata::create((void*)superGetattribute,
// UNKNOWN, 2))); // UNKNOWN, 2)));
......
...@@ -105,8 +105,9 @@ void BoxedTraceback::here(LineInfo lineInfo, Box** tb) { ...@@ -105,8 +105,9 @@ void BoxedTraceback::here(LineInfo lineInfo, Box** tb) {
} }
void setupTraceback() { void setupTraceback() {
traceback_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedTraceback), traceback_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedTraceback), false, "traceback",
false, "traceback"); (destructor)BoxedTraceback::dealloc, NULL, true,
(traverseproc)BoxedTraceback::traverse, (inquiry)BoxedTraceback::clear);
traceback_cls->giveAttr("getLines", traceback_cls->giveAttr("getLines",
new BoxedFunction(FunctionMetadata::create((void*)BoxedTraceback::getLines, UNKNOWN, 1))); new BoxedFunction(FunctionMetadata::create((void*)BoxedTraceback::getLines, UNKNOWN, 1)));
......
...@@ -41,6 +41,16 @@ public: ...@@ -41,6 +41,16 @@ public:
// somewhat equivalent to PyTraceBack_Here // somewhat equivalent to PyTraceBack_Here
static void here(LineInfo lineInfo, Box** tb); static void here(LineInfo lineInfo, Box** tb);
static void dealloc(Box* b) noexcept {
Py_FatalError("unimplemented");
}
static int traverse(Box* self, visitproc visit, void *arg) noexcept {
Py_FatalError("unimplemented");
}
static int clear(Box* self) noexcept {
Py_FatalError("unimplemented");
}
}; };
void printTraceback(Box* b); void printTraceback(Box* b);
......
...@@ -675,8 +675,9 @@ void setupTuple() { ...@@ -675,8 +675,9 @@ void setupTuple() {
static PyMappingMethods tuple_as_mapping; static PyMappingMethods tuple_as_mapping;
tuple_cls->tp_as_mapping = &tuple_as_mapping; tuple_cls->tp_as_mapping = &tuple_as_mapping;
tuple_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, tuple_iterator_cls = BoxedClass::create(type_cls, object_cls, 0, 0, sizeof(BoxedTupleIterator), false,
sizeof(BoxedTupleIterator), false, "tuple"); "tupleiterator", (destructor)BoxedTupleIterator::dealloc, NULL, true,
(traverseproc)BoxedTupleIterator::traverse, NOCLEAR);
tuple_cls->giveAttr("__new__", tuple_cls->giveAttr("__new__",
new BoxedFunction(FunctionMetadata::create((void*)tupleNew, UNKNOWN, 1, true, true))); new BoxedFunction(FunctionMetadata::create((void*)tupleNew, UNKNOWN, 1, true, true)));
......
...@@ -28,6 +28,17 @@ public: ...@@ -28,6 +28,17 @@ public:
BoxedTupleIterator(BoxedTuple* t); BoxedTupleIterator(BoxedTuple* t);
DEFAULT_CLASS(tuple_iterator_cls); DEFAULT_CLASS(tuple_iterator_cls);
static void dealloc(BoxedTupleIterator* o) noexcept {
PyObject_GC_UnTrack(o);
Py_DECREF(o->t);
o->cls->tp_free(o);
}
static int traverse(BoxedTupleIterator* self, visitproc visit, void *arg) noexcept {
Py_VISIT(self->t);
return 0;
}
}; };
Box* tupleIter(Box* self) noexcept; Box* tupleIter(Box* self) noexcept;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment