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) {}
PyVar* try_get_name(StrName name);
NameDict_ to_namedict();
NameDict* to_namedict();
PyVar* begin() const { return a; }

View File

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

View File

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

View File

@ -183,11 +183,11 @@ PyVar VM::__run_top_frame() {
const FuncDecl_& decl = frame->co->func_decls[byte.arg];
PyVar obj;
if(decl->nested) {
NameDict_ captured = frame->_locals.to_namedict();
obj = VAR(Function(decl, frame->_module, nullptr, captured));
NameDict* captured = frame->_locals.to_namedict();
obj = new_object<Function>(tp_function, decl, frame->_module, nullptr, captured);
captured->set(decl->code->name, obj);
} else {
obj = VAR(Function(decl, frame->_module, nullptr, nullptr));
obj = new_object<Function>(tp_function, decl, frame->_module, nullptr, nullptr);
}
PUSH(obj);
}

View File

@ -8,8 +8,8 @@ PyVar* FastLocals::try_get_name(StrName name) {
return &a[index];
}
NameDict_ FastLocals::to_namedict() {
NameDict_ dict = std::make_shared<NameDict>();
NameDict* FastLocals::to_namedict() {
NameDict* dict = new NameDict();
for(int i=0; i<co->varnames_inv.count; i++){
auto entry = c11__getitem(c11_smallmap_entry_n2i, &co->varnames_inv, i);
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;
Dict* globals_dict = nullptr;
NameDict_ locals_closure = nullptr;
NameDict* locals_closure = nullptr;
Dict* locals_dict = nullptr;
if(is_none(globals)){
@ -588,7 +588,7 @@ PyVar VM::__py_exec_internal(const CodeObject_& code, PyVar globals, PyVar local
} else {
check_compatible_type(locals, VM::tp_dict);
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_closure->set(CAST(Str&, k), v);
});