remove NameDict_

This commit is contained in:
blueloveTH 2024-06-15 23:31:45 +08:00
parent a8db1cc5e1
commit e299564b9c
6 changed files with 17 additions and 11 deletions

View File

@ -19,7 +19,7 @@ struct FastLocals {
FastLocals(const CodeObject* co, PyVar* a) : co(co), a(a) {} FastLocals(const CodeObject* co, PyVar* a) : co(co), a(a) {}
PyVar* try_get_name(StrName name); PyVar* try_get_name(StrName name);
NameDict_ to_namedict(); NameDict* to_namedict();
PyVar* begin() const { return a; } PyVar* begin() const { return a; }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "pocketpy/common/any.h" #include "pocketpy/common/any.h"
#include "pocketpy/common/traits.hpp"
#include "pocketpy/objects/tuplelist.hpp" #include "pocketpy/objects/tuplelist.hpp"
#include "pocketpy/objects/namedict.hpp" #include "pocketpy/objects/namedict.hpp"
#include "pocketpy/objects/sourcedata.hpp" #include "pocketpy/objects/sourcedata.hpp"
@ -175,15 +176,21 @@ struct NativeFunc {
}; };
struct Function { struct Function {
PK_ALWAYS_PASS_BY_POINTER(Function)
FuncDecl_ decl; FuncDecl_ decl;
PyObject* _module; // weak ref PyObject* _module; // weak ref
PyObject* _class; // weak ref PyObject* _class; // weak ref
NameDict_ _closure; NameDict* _closure; // strong ref
explicit Function(FuncDecl_ decl, PyObject* _module, PyObject* _class, NameDict_ _closure) : Function(FuncDecl_ decl, PyObject* _module, PyObject* _class, NameDict* _closure) :
decl(decl), _module(_module), _class(_class), _closure(_closure) {} decl(decl), _module(_module), _class(_class), _closure(_closure) {}
void _gc_mark(VM*) const; void _gc_mark(VM*) const;
~Function() {
delete _closure;
}
}; };
template <typename T> template <typename T>

View File

@ -59,6 +59,5 @@ struct NameDict {
}; };
static_assert(sizeof(NameDict) <= 128); static_assert(sizeof(NameDict) <= 128);
using NameDict_ = std::shared_ptr<NameDict>;
} // namespace pkpy } // namespace pkpy

View File

@ -183,11 +183,11 @@ PyVar VM::__run_top_frame() {
const FuncDecl_& decl = frame->co->func_decls[byte.arg]; const FuncDecl_& decl = frame->co->func_decls[byte.arg];
PyVar obj; PyVar 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, nullptr, captured)); obj = new_object<Function>(tp_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, nullptr, nullptr)); obj = new_object<Function>(tp_function, decl, frame->_module, nullptr, nullptr);
} }
PUSH(obj); PUSH(obj);
} }

View File

@ -8,8 +8,8 @@ PyVar* FastLocals::try_get_name(StrName name) {
return &a[index]; return &a[index];
} }
NameDict_ FastLocals::to_namedict() { NameDict* FastLocals::to_namedict() {
NameDict_ dict = std::make_shared<NameDict>(); NameDict* dict = new NameDict();
for(int i=0; i<co->varnames_inv.count; i++){ for(int i=0; i<co->varnames_inv.count; i++){
auto entry = c11__getitem(c11_smallmap_entry_n2i, &co->varnames_inv, i); auto entry = c11__getitem(c11_smallmap_entry_n2i, &co->varnames_inv, i);
PyVar value = a[entry.value]; PyVar value = a[entry.value];

View File

@ -561,7 +561,7 @@ PyVar VM::__py_exec_internal(const CodeObject_& code, PyVar globals, PyVar local
PyObject* globals_obj = nullptr; PyObject* globals_obj = nullptr;
Dict* globals_dict = nullptr; Dict* globals_dict = nullptr;
NameDict_ locals_closure = nullptr; NameDict* locals_closure = nullptr;
Dict* locals_dict = nullptr; Dict* locals_dict = nullptr;
if(is_none(globals)){ if(is_none(globals)){
@ -588,7 +588,7 @@ PyVar VM::__py_exec_internal(const CodeObject_& code, PyVar globals, PyVar local
} else { } else {
check_compatible_type(locals, VM::tp_dict); check_compatible_type(locals, VM::tp_dict);
locals_dict = &PK_OBJ_GET(Dict, locals); locals_dict = &PK_OBJ_GET(Dict, locals);
locals_closure = std::make_shared<NameDict>(); locals_closure = new NameDict();
locals_dict->apply([&](PyVar k, PyVar v) { locals_dict->apply([&](PyVar k, PyVar v) {
locals_closure->set(CAST(Str&, k), v); locals_closure->set(CAST(Str&, k), v);
}); });