refactor 2

This commit is contained in:
blueloveTH 2024-02-07 11:47:41 +08:00
parent b0dc1e4a69
commit 8a2ee65301
4 changed files with 23 additions and 7 deletions

View File

@ -7,8 +7,8 @@ with open("include/pocketpy/opcodes.h", "rt", encoding='utf-8') as f:
pipeline = [ pipeline = [
["config.h", "export.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h"], ["config.h", "export.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h"],
["obj.h", "dict.h", "codeobject.h", "frame.h"], ["obj.h", "dict.h", "codeobject.h", "frame.h", "profiler.h"],
["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h", "profiler.h"], ["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h"],
["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"], ["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"],
["pocketpy.h", "pocketpy_c.h"] ["pocketpy.h", "pocketpy_c.h"]
] ]

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include "bindings.h"
namespace pkpy { namespace pkpy {
void add_module_line_profiler(VM* vm); struct LineProfiler{
};
} // namespace pkpy } // namespace pkpy

View File

@ -10,6 +10,7 @@
#include "str.h" #include "str.h"
#include "tuplelist.h" #include "tuplelist.h"
#include "dict.h" #include "dict.h"
#include "profiler.h"
namespace pkpy{ namespace pkpy{
@ -152,6 +153,8 @@ public:
void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr; void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr;
LineProfiler* _profiler = nullptr;
PrintFunc _stdout; PrintFunc _stdout;
PrintFunc _stderr; PrintFunc _stderr;
unsigned char* (*_import_handler)(const char*, int, int*); unsigned char* (*_import_handler)(const char*, int, int*);

View File

@ -247,6 +247,19 @@ void add_module_gc(VM* vm){
struct LineProfilerW{ struct LineProfilerW{
PY_CLASS(LineProfilerW, line_profiler, LineProfiler) PY_CLASS(LineProfilerW, line_profiler, LineProfiler)
LineProfiler profiler;
void enable_by_count(VM* vm){
if(vm->_profiler){
vm->ValueError("only one profiler can be enabled at a time");
}
vm->_profiler = &profiler;
}
void disable_by_count(VM* vm){
vm->_profiler = nullptr;
}
static void _register(VM* vm, PyObject* mod, PyObject* type){ static void _register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_default_constructor<LineProfilerW>(type); vm->bind_default_constructor<LineProfilerW>(type);
@ -257,13 +270,13 @@ struct LineProfilerW{
vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){ vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){
LineProfilerW& self = PK_OBJ_GET(LineProfilerW, view[0]); LineProfilerW& self = PK_OBJ_GET(LineProfilerW, view[0]);
// self.enable_by_count(vm); self.enable_by_count(vm);
PyObject* func = view[1]; PyObject* func = view[1];
const Tuple& args = CAST(Tuple&, view[2]); const Tuple& args = CAST(Tuple&, view[2]);
for(PyObject* arg : args) vm->s_data.push(arg); for(PyObject* arg : args) vm->s_data.push(arg);
vm->s_data.push(func); vm->s_data.push(func);
PyObject* ret = vm->vectorcall(args.size()); PyObject* ret = vm->vectorcall(args.size());
// self.disable_by_count(vm); self.disable_by_count(vm);
return ret; return ret;
}); });