From 8a2ee6530179a5bfa47aebb7c0406b5dc0431b0d Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 7 Feb 2024 11:47:41 +0800 Subject: [PATCH] refactor 2 --- amalgamate.py | 4 ++-- include/pocketpy/profiler.h | 6 +++--- include/pocketpy/vm.h | 3 +++ src/modules.cpp | 17 +++++++++++++++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/amalgamate.py b/amalgamate.py index 021c6bf7..865e272b 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -7,8 +7,8 @@ with open("include/pocketpy/opcodes.h", "rt", encoding='utf-8') as f: pipeline = [ ["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"], - ["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h", "profiler.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"], ["_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"] ] diff --git a/include/pocketpy/profiler.h b/include/pocketpy/profiler.h index 93356245..20971ef4 100644 --- a/include/pocketpy/profiler.h +++ b/include/pocketpy/profiler.h @@ -1,9 +1,9 @@ #pragma once -#include "bindings.h" - namespace pkpy { -void add_module_line_profiler(VM* vm); +struct LineProfiler{ + +}; } // namespace pkpy diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index b0f859b6..8f760584 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -10,6 +10,7 @@ #include "str.h" #include "tuplelist.h" #include "dict.h" +#include "profiler.h" namespace pkpy{ @@ -152,6 +153,8 @@ public: void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr; + LineProfiler* _profiler = nullptr; + PrintFunc _stdout; PrintFunc _stderr; unsigned char* (*_import_handler)(const char*, int, int*); diff --git a/src/modules.cpp b/src/modules.cpp index b42618bb..7e04b472 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -247,6 +247,19 @@ void add_module_gc(VM* vm){ struct LineProfilerW{ 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){ vm->bind_default_constructor(type); @@ -257,13 +270,13 @@ struct LineProfilerW{ vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){ LineProfilerW& self = PK_OBJ_GET(LineProfilerW, view[0]); - // self.enable_by_count(vm); + self.enable_by_count(vm); PyObject* func = view[1]; const Tuple& args = CAST(Tuple&, view[2]); for(PyObject* arg : args) vm->s_data.push(arg); vm->s_data.push(func); PyObject* ret = vm->vectorcall(args.size()); - // self.disable_by_count(vm); + self.disable_by_count(vm); return ret; });