add py_op

This commit is contained in:
blueloveTH 2024-05-11 12:44:42 +08:00
parent b1516a6ad0
commit a6a834da29
2 changed files with 16 additions and 1 deletions

View File

@ -156,6 +156,7 @@ public:
PyObject* __curr_class;
PyObject* __cached_object_new;
std::map<std::string_view, CodeObject_> __cached_codes;
std::map<std::string_view, PyObject*> __cached_op_funcs;
FuncDecl_ __dynamic_func_decl;
#if PK_ENABLE_PROFILER
@ -209,6 +210,8 @@ public:
return !py_eq(lhs, rhs);
}
PyObject* py_op(std::string_view name); // (name) -> operator.name
void py_exec(std::string_view, PyObject*, PyObject*); // exec(source, globals, locals)
PyObject* py_eval(std::string_view, PyObject*, PyObject*); // eval(source, globals, locals)
#endif

View File

@ -244,6 +244,18 @@ namespace pkpy{
return false;
}
PyObject* VM::py_op(std::string_view name){
PyObject* func;
auto it = __cached_op_funcs.find(name);
if(it == __cached_op_funcs.end()){
func = py_import("operator")->attr(StrName::get(name));
__cached_op_funcs[name] = func;
}else{
func = it->second;
}
return func;
}
i64 VM::normalized_index(i64 index, int size){
if(index < 0) index += size;
if(index < 0 || index >= size){