fix a bug of _class in Function

This commit is contained in:
blueloveTH 2023-10-15 04:44:10 +08:00
parent c3f10ae4d1
commit 53e4043e0e
3 changed files with 9 additions and 6 deletions

View File

@ -190,8 +190,12 @@ struct NativeFunc {
struct Function{ struct Function{
FuncDecl_ decl; FuncDecl_ decl;
PyObject* _module; PyObject* _module; // weak ref
PyObject* _class; // weak ref
NameDict_ _closure; NameDict_ _closure;
explicit Function(FuncDecl_ decl, PyObject* _module, PyObject* _class, NameDict_ _closure):
decl(decl), _module(_module), _class(_class), _closure(_closure) {}
}; };
template<> template<>
@ -203,7 +207,6 @@ struct Py_<Function> final: PyObject {
} }
void _obj_gc_mark() override { void _obj_gc_mark() override {
_value.decl->_gc_mark(); _value.decl->_gc_mark();
if(_value._module != nullptr) PK_OBJ_MARK(_value._module);
if(_value._closure != nullptr) gc_mark_namedict(*_value._closure); if(_value._closure != nullptr) gc_mark_namedict(*_value._closure);
} }

View File

@ -89,10 +89,10 @@ __NEXT_STEP:;
PyObject* obj; PyObject* obj;
if(decl->nested){ if(decl->nested){
NameDict_ captured = frame->_locals.to_namedict(); NameDict_ captured = frame->_locals.to_namedict();
obj = VAR(Function({decl, frame->_module, captured})); obj = VAR(Function(decl, frame->_module, nullptr, captured));
captured->set(decl->code->name, obj); captured->set(decl->code->name, obj);
}else{ }else{
obj = VAR(Function({decl, frame->_module})); obj = VAR(Function(decl, frame->_module, nullptr, nullptr));
} }
PUSH(obj); PUSH(obj);
} DISPATCH(); } DISPATCH();
@ -689,7 +689,7 @@ __NEXT_STEP:;
StrName _name(byte.arg); StrName _name(byte.arg);
PyObject* _0 = POPX(); PyObject* _0 = POPX();
if(is_non_tagged_type(_0, tp_function)){ if(is_non_tagged_type(_0, tp_function)){
_0->attr().set(__class__, TOP()); PK_OBJ_GET(Function, _0)._class = TOP();
} }
TOP()->attr().set(_name, _0); TOP()->attr().set(_name, _0);
} DISPATCH(); } DISPATCH();

View File

@ -123,7 +123,7 @@ void init_builtins(VM* _vm) {
}else if(args.size() == 0){ }else if(args.size() == 0){
FrameId frame = vm->top_frame(); FrameId frame = vm->top_frame();
if(frame->_callable != nullptr){ if(frame->_callable != nullptr){
class_arg = frame->_callable->attr().try_get_likely_found(__class__); class_arg = PK_OBJ_GET(Function, frame->_callable)._class;
if(frame->_locals.size() > 0) self_arg = frame->_locals[0]; if(frame->_locals.size() > 0) self_arg = frame->_locals[0];
} }
if(class_arg == nullptr || self_arg == nullptr){ if(class_arg == nullptr || self_arg == nullptr){