mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
add py_op
This commit is contained in:
parent
b1516a6ad0
commit
a6a834da29
@ -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
|
||||
|
12
src/vm.cpp
12
src/vm.cpp
@ -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){
|
||||
|
Loading…
x
Reference in New Issue
Block a user