From b1eb38c0091a932720b58d0c08d4e67efdb3950b Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 18 Apr 2023 19:31:43 +0800 Subject: [PATCH] ... --- src/memory.h | 2 +- src/obj.h | 5 ++++- src/pocketpy.h | 10 +++++----- src/vm.h | 40 +++++++++++++++++++++++++++++----------- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/memory.h b/src/memory.h index 6e630252..c9f3e66e 100644 --- a/src/memory.h +++ b/src/memory.h @@ -112,7 +112,7 @@ struct DoubleLinkedList{ int size() const { return _size; } - void apply(std::function func){ + void apply(void (*func)(T*)){ LinkedListNode* p = head.next; while(p != &tail){ LinkedListNode* next = p->next; diff --git a/src/obj.h b/src/obj.h index 800f4a9e..114f1707 100644 --- a/src/obj.h +++ b/src/obj.h @@ -11,7 +11,10 @@ struct Frame; struct Function; class VM; -typedef std::function NativeFuncRaw; +typedef PyObject* (*NativeFuncC)(VM*, ArgsView); +typedef std::function NativeFuncCpp; +using NativeFuncRaw = std::variant; + typedef shared_ptr CodeObject_; struct NativeFunc { diff --git a/src/pocketpy.h b/src/pocketpy.h index 40a26fc2..4886be57 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -739,10 +739,10 @@ struct Random{ static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_static_method<0>(type, "__new__", CPP_LAMBDA(VAR_T(Random))); - vm->bind_method<1>(type, "seed", native_proxy_callable(&Random::seed)); - vm->bind_method<2>(type, "randint", native_proxy_callable(&Random::randint)); - vm->bind_method<0>(type, "random", native_proxy_callable(&Random::random)); - vm->bind_method<2>(type, "uniform", native_proxy_callable(&Random::uniform)); + vm->bind_cpp_method<1>(type, "seed", native_proxy_callable(&Random::seed)); + vm->bind_cpp_method<2>(type, "randint", native_proxy_callable(&Random::randint)); + vm->bind_cpp_method<0>(type, "random", native_proxy_callable(&Random::random)); + vm->bind_cpp_method<2>(type, "uniform", native_proxy_callable(&Random::uniform)); } }; @@ -956,7 +956,7 @@ extern "C" { for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr; std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++); pkpy::PyObject* obj = vm->_modules.contains(mod) ? vm->_modules[mod] : vm->new_module(mod); - vm->bind_func<-1>(obj, name, [ret_code, f_header](pkpy::VM* vm, pkpy::ArgsView args){ + vm->bind_cpp_func<-1>(obj, name, [ret_code, f_header](pkpy::VM* vm, pkpy::ArgsView args){ std::stringstream ss; ss << f_header; for(int i=0; i namespace pkpy{ @@ -231,7 +230,7 @@ public: return call_method(self, callable, args...); } - PyObject* property(NativeFuncRaw fget){ + PyObject* property(NativeFuncC fget){ PyObject* p = builtins->attr("property"); PyObject* method = heap.gcnew(tp_native_func, NativeFunc(fget, 1, false)); return call(p, method); @@ -264,12 +263,12 @@ public: } template - void bind_func(Str type, Str name, NativeFuncRaw fn) { + void bind_func(Str type, Str name, NativeFuncC fn) { bind_func(_find_type(type), name, fn); } template - void bind_method(Str type, Str name, NativeFuncRaw fn) { + void bind_method(Str type, Str name, NativeFuncC fn) { bind_method(_find_type(type), name, fn); } @@ -279,12 +278,12 @@ public: } template - void _bind_methods(std::vector types, Str name, NativeFuncRaw fn) { + void _bind_methods(std::vector types, Str name, NativeFuncC fn) { for(auto& type: types) bind_method(type, name, fn); } template - void bind_builtin_func(Str name, NativeFuncRaw fn) { + void bind_builtin_func(Str name, NativeFuncC fn) { bind_func(builtins, name, fn); } @@ -379,9 +378,13 @@ public: PyObject* get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err=true, bool fallback=false); void setattr(PyObject* obj, StrName name, PyObject* value); template - void bind_method(PyObject*, Str, NativeFuncRaw); + void bind_method(PyObject*, Str, NativeFuncC); template - void bind_func(PyObject*, Str, NativeFuncRaw); + void bind_func(PyObject*, Str, NativeFuncC); + template + void bind_cpp_method(PyObject*, Str, NativeFuncCpp); + template + void bind_cpp_func(PyObject*, Str, NativeFuncCpp); void _error(Exception); PyObject* _run_top_frame(); void post_init(); @@ -392,7 +395,11 @@ inline PyObject* NativeFunc::operator()(VM* vm, ArgsView args) const{ if(argc != -1 && args_size != argc) { vm->TypeError(fmt("expected ", argc, " arguments, but got ", args_size)); } - return f(vm, args); + if(std::holds_alternative(f)){ + return std::get(f)(vm, args); + }else{ + return std::get(f)(vm, args); + } } inline void CodeObject::optimize(VM* vm){ @@ -1035,13 +1042,24 @@ inline void VM::setattr(PyObject* obj, StrName name, PyObject* value){ } template -void VM::bind_method(PyObject* obj, Str name, NativeFuncRaw fn) { +void VM::bind_method(PyObject* obj, Str name, NativeFuncC fn) { check_type(obj, tp_type); obj->attr().set(name, VAR(NativeFunc(fn, ARGC, true))); } template -void VM::bind_func(PyObject* obj, Str name, NativeFuncRaw fn) { +void VM::bind_cpp_method(PyObject* obj, Str name, NativeFuncCpp fn) { + check_type(obj, tp_type); + obj->attr().set(name, VAR(NativeFunc(fn, ARGC, true))); +} + +template +void VM::bind_func(PyObject* obj, Str name, NativeFuncC fn) { + obj->attr().set(name, VAR(NativeFunc(fn, ARGC, false))); +} + +template +void VM::bind_cpp_func(PyObject* obj, Str name, NativeFuncCpp fn) { obj->attr().set(name, VAR(NativeFunc(fn, ARGC, false))); }