diff --git a/amalgamate.py b/amalgamate.py index 46c899c3..021c6bf7 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -8,8 +8,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"], - ["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "line_profiler.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"], + ["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h", "profiler.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/modules.h b/include/pocketpy/modules.h index ac01155c..7a7be649 100644 --- a/include/pocketpy/modules.h +++ b/include/pocketpy/modules.h @@ -10,5 +10,6 @@ void add_module_math(VM* vm); void add_module_traceback(VM* vm); void add_module_dis(VM* vm); void add_module_gc(VM* vm); +void add_module_line_profiler(VM* vm); } // namespace pkpy \ No newline at end of file diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 57df6ceb..89e11d07 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -16,5 +16,4 @@ #include "csv.h" #include "dataclasses.h" #include "array2d.h" -#include "line_profiler.h" #include "modules.h" diff --git a/include/pocketpy/line_profiler.h b/include/pocketpy/profiler.h similarity index 100% rename from include/pocketpy/line_profiler.h rename to include/pocketpy/profiler.h diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index be3abab0..b0f859b6 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -150,10 +150,7 @@ public: // cached code objects for FSTRING_EVAL std::map _cached_codes; - // for user defined logic void (*_ceval_on_step)(VM*, Frame*, Bytecode bc) = nullptr; - // for line_profiler (users not to use this) - void (*_ceval_on_step_profile)(VM*, Frame*, Bytecode bc) = nullptr; PrintFunc _stdout; PrintFunc _stderr; diff --git a/src/ceval.cpp b/src/ceval.cpp index cb491f61..bc8d6f95 100644 --- a/src/ceval.cpp +++ b/src/ceval.cpp @@ -67,8 +67,7 @@ PyObject* VM::_run_top_frame(){ { #define CEVAL_STEP_CALLBACK() \ - if(_ceval_on_step) _ceval_on_step(this, frame.get(), byte); \ - if(_ceval_on_step_profile) _ceval_on_step_profile(this, frame.get(), byte); + if(_ceval_on_step) _ceval_on_step(this, frame.get(), byte); #define DISPATCH_OP_CALL() { frame = top_frame(); goto __NEXT_FRAME; } __NEXT_FRAME: diff --git a/src/line_profiler.cpp b/src/line_profiler.cpp deleted file mode 100644 index 76ac28f7..00000000 --- a/src/line_profiler.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "pocketpy/line_profiler.h" - -namespace pkpy{ - -struct LineProfiler{ - PY_CLASS(LineProfiler, line_profiler, LineProfiler) - - std::set _functions; - - static void _register(VM* vm, PyObject* mod, PyObject* type){ - vm->bind_default_constructor(type); - - vm->bind(type, "add_function(self, func)", [](VM* vm, ArgsView args){ - // ... - return vm->None; - }); - - vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){ - LineProfiler& self = PK_OBJ_GET(LineProfiler, view[0]); - // enable_by_count - 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()); - // disable_by_count - return ret; - }); - - vm->bind(type, "print_stats(self)", [](VM* vm, ArgsView args){ - // ... - return vm->None; - }); - } -}; - -void add_module_line_profiler(VM *vm){ - PyObject* mod = vm->new_module("line_profiler"); - LineProfiler::register_class(vm, mod); -} - -} // namespace pkpy \ No newline at end of file diff --git a/src/modules.cpp b/src/modules.cpp index 374cefec..b42618bb 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -223,18 +223,16 @@ void add_module_traceback(VM* vm){ void add_module_dis(VM* vm){ PyObject* mod = vm->new_module("dis"); - static const auto get_code = [](VM* vm, PyObject* obj)->CodeObject_{ + vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) { + CodeObject_ code; + PyObject* obj = args[0]; if(is_type(obj, vm->tp_str)){ const Str& source = CAST(Str, obj); - return vm->compile(source, "", EXEC_MODE); + code = vm->compile(source, "", EXEC_MODE); } PyObject* f = obj; if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, obj).func; - return CAST(Function&, f).decl->code; - }; - - vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) { - CodeObject_ code = get_code(vm, args[0]); + code = CAST(Function&, f).decl->code; vm->stdout_write(vm->disassemble(code)); return vm->None; }); @@ -245,5 +243,40 @@ void add_module_gc(VM* vm){ vm->bind_func<0>(mod, "collect", PK_LAMBDA(VAR(vm->heap.collect()))); } +// line_profiler wrapper +struct LineProfilerW{ + PY_CLASS(LineProfilerW, line_profiler, LineProfiler) + + static void _register(VM* vm, PyObject* mod, PyObject* type){ + vm->bind_default_constructor(type); + + vm->bind(type, "add_function(self, func)", [](VM* vm, ArgsView args){ + // ... + return vm->None; + }); + + vm->bind(type, "runcall(self, func, *args)", [](VM* vm, ArgsView view){ + LineProfilerW& self = PK_OBJ_GET(LineProfilerW, view[0]); + // 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); + return ret; + }); + + vm->bind(type, "print_stats(self)", [](VM* vm, ArgsView args){ + // ... + return vm->None; + }); + } +}; + +void add_module_line_profiler(VM *vm){ + PyObject* mod = vm->new_module("line_profiler"); + LineProfilerW::register_class(vm, mod); +} } // namespace pkpy \ No newline at end of file diff --git a/src/profiler.cpp b/src/profiler.cpp new file mode 100644 index 00000000..690fd01b --- /dev/null +++ b/src/profiler.cpp @@ -0,0 +1,7 @@ +#include "pocketpy/profiler.h" + +namespace pkpy{ + + + +} // namespace pkpy \ No newline at end of file