From 0f4c7d19ba8e7c5673683316c500a81e8f649b14 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Tue, 27 Jun 2023 12:26:03 +0800 Subject: [PATCH] ... --- src/common.h | 2 +- src/obj.h | 7 ++++++- src/str.h | 7 +++++++ src/vm.h | 42 ++++++++++++++++++++++++++++-------------- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/common.h b/src/common.h index 693fa4d9..bf1be086 100644 --- a/src/common.h +++ b/src/common.h @@ -112,7 +112,7 @@ struct Type { #define PK_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; }) #define PK_VAR_LAMBDA(x) ([](VM* vm, ArgsView args) { return VAR(x); }) -#define PK_NONE_LAMBDA(x) ([](VM* vm, ArgsView args) { x; return vm->None; }) +#define PK_ACTION(x) ([](VM* vm, ArgsView args) { x; return vm->None; }) #ifdef POCKETPY_H #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!"); diff --git a/src/obj.h b/src/obj.h index 623c4c94..979fdacb 100644 --- a/src/obj.h +++ b/src/obj.h @@ -30,6 +30,8 @@ struct NativeFunc { UserData _userdata; bool _has_userdata; + const char* signature; + template void set_userdata(T data) { static_assert(std::is_trivially_copyable_v); @@ -49,8 +51,9 @@ struct NativeFunc { return reinterpret_cast(_userdata); } - NativeFunc(NativeFuncC f, int argc, bool method){ + NativeFunc(NativeFuncC f, int argc, bool method, const char* sig=nullptr){ this->f = f; + this->signature = sig; this->argc = argc; if(argc != -1) this->argc += (int)method; _lua_f = nullptr; @@ -300,6 +303,8 @@ __T _py_cast(VM* vm, PyObject* obj) { #define CAST_F(x) vm->num_to_float(x) +#define CAST_DEFAULT(T, i, default_value) (i < args.size()) ? py_cast(vm, args[index]) : (default_value) + /*****************************************************************/ template<> struct Py_ final: PyObject { diff --git a/src/str.h b/src/str.h index cf9fbbcf..a39c5b66 100644 --- a/src/str.h +++ b/src/str.h @@ -426,6 +426,13 @@ struct FastStrStream{ } }; +struct CString{ + const char* ptr; + CString(const char* ptr): ptr(ptr) {} + CString(const Str& str): ptr(str.c_str_temp()) {} + operator const char*() const { return ptr; } +}; + // unary operators const StrName __repr__ = StrName::get("__repr__"); const StrName __str__ = StrName::get("__str__"); diff --git a/src/vm.h b/src/vm.h index 8959d9fe..0348af7f 100644 --- a/src/vm.h +++ b/src/vm.h @@ -461,19 +461,19 @@ public: } template - PyObject* bind_func(Str type, Str name, NativeFuncC fn) { - return bind_func(_find_type_object(type), name, fn); + PyObject* bind_func(Str type, Str name, NativeFuncC fn, const char* sig=nullptr) { + return bind_func(_find_type_object(type), name, fn, sig); } template - PyObject* bind_method(Str type, Str name, NativeFuncC fn) { - return bind_method(_find_type_object(type), name, fn); + PyObject* bind_method(Str type, Str name, NativeFuncC fn, const char* sig=nullptr) { + return bind_method(_find_type_object(type), name, fn, sig); } template - PyObject* bind_constructor(__T&& type, NativeFuncC fn) { + PyObject* bind_constructor(__T&& type, NativeFuncC fn, const char* sig=nullptr) { static_assert(ARGC==-1 || ARGC>=1); - return bind_func(std::forward<__T>(type), "__new__", fn); + return bind_func(std::forward<__T>(type), "__new__", fn, sig); } template @@ -494,8 +494,8 @@ public: } template - PyObject* bind_builtin_func(Str name, NativeFuncC fn) { - return bind_func(builtins, name, fn); + PyObject* bind_builtin_func(Str name, NativeFuncC fn, const char* sig=nullptr) { + return bind_func(builtins, name, fn, sig); } int normalized_index(int index, int size){ @@ -545,6 +545,11 @@ public: TypeError("expected " + OBJ_NAME(_t(type)).escape() + ", got " + OBJ_NAME(_t(obj)).escape()); } + void check_args_size(int size, int min_size, int max_size){ + if(size >= min_size && size <= max_size) return; + TypeError(fmt("expected ", min_size, "-", max_size, " arguments, got ", size)); + } + void check_non_tagged_type(PyObject* obj, Type type){ if(is_non_tagged_type(obj, type)) return; TypeError("expected " + OBJ_NAME(_t(type)).escape() + ", got " + OBJ_NAME(_t(obj)).escape()); @@ -676,9 +681,9 @@ public: PyObject* format(Str, PyObject*); void setattr(PyObject* obj, StrName name, PyObject* value); template - PyObject* bind_method(PyObject*, Str, NativeFuncC); + PyObject* bind_method(PyObject*, Str, NativeFuncC, const char* sig=nullptr); template - PyObject* bind_func(PyObject*, Str, NativeFuncC); + PyObject* bind_func(PyObject*, Str, NativeFuncC, const char* sig=nullptr); void _error(Exception); PyObject* _run_top_frame(); void post_init(); @@ -813,6 +818,15 @@ template<> inline bool _py_cast(VM* vm, PyObject* obj){ return obj == vm->True; } +template<> inline CString py_cast(VM* vm, PyObject* obj){ + vm->check_non_tagged_type(obj, vm->tp_str); + return PK_OBJ_GET(Str, obj).c_str_temp(); +} + +template<> inline CString _py_cast(VM* vm, PyObject* obj){ + return PK_OBJ_GET(Str, obj).c_str_temp(); +} + inline PyObject* py_var(VM* vm, const char val[]){ return VAR(Str(val)); } @@ -1505,16 +1519,16 @@ inline void VM::setattr(PyObject* obj, StrName name, PyObject* value){ } template -PyObject* VM::bind_method(PyObject* obj, Str name, NativeFuncC fn) { +PyObject* VM::bind_method(PyObject* obj, Str name, NativeFuncC fn, const char* sig) { check_non_tagged_type(obj, tp_type); - PyObject* nf = VAR(NativeFunc(fn, ARGC, true)); + PyObject* nf = VAR(NativeFunc(fn, ARGC, true, sig)); obj->attr().set(name, nf); return nf; } template -PyObject* VM::bind_func(PyObject* obj, Str name, NativeFuncC fn) { - PyObject* nf = VAR(NativeFunc(fn, ARGC, false)); +PyObject* VM::bind_func(PyObject* obj, Str name, NativeFuncC fn, const char* sig) { + PyObject* nf = VAR(NativeFunc(fn, ARGC, false, sig)); obj->attr().set(name, nf); return nf; }