This commit is contained in:
blueloveTH 2023-06-27 12:26:03 +08:00
parent 72b9f3a277
commit 0f4c7d19ba
4 changed files with 42 additions and 16 deletions

View File

@ -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()!");

View File

@ -30,6 +30,8 @@ struct NativeFunc {
UserData _userdata;
bool _has_userdata;
const char* signature;
template <typename T>
void set_userdata(T data) {
static_assert(std::is_trivially_copyable_v<T>);
@ -49,8 +51,9 @@ struct NativeFunc {
return reinterpret_cast<const T&>(_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<T>(vm, args[index]) : (default_value)
/*****************************************************************/
template<>
struct Py_<List> final: PyObject {

View File

@ -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__");

View File

@ -461,19 +461,19 @@ public:
}
template<int ARGC>
PyObject* bind_func(Str type, Str name, NativeFuncC fn) {
return bind_func<ARGC>(_find_type_object(type), name, fn);
PyObject* bind_func(Str type, Str name, NativeFuncC fn, const char* sig=nullptr) {
return bind_func<ARGC>(_find_type_object(type), name, fn, sig);
}
template<int ARGC>
PyObject* bind_method(Str type, Str name, NativeFuncC fn) {
return bind_method<ARGC>(_find_type_object(type), name, fn);
PyObject* bind_method(Str type, Str name, NativeFuncC fn, const char* sig=nullptr) {
return bind_method<ARGC>(_find_type_object(type), name, fn, sig);
}
template<int ARGC, typename __T>
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<ARGC>(std::forward<__T>(type), "__new__", fn);
return bind_func<ARGC>(std::forward<__T>(type), "__new__", fn, sig);
}
template<typename T, typename __T>
@ -494,8 +494,8 @@ public:
}
template<int ARGC>
PyObject* bind_builtin_func(Str name, NativeFuncC fn) {
return bind_func<ARGC>(builtins, name, fn);
PyObject* bind_builtin_func(Str name, NativeFuncC fn, const char* sig=nullptr) {
return bind_func<ARGC>(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<int ARGC>
PyObject* bind_method(PyObject*, Str, NativeFuncC);
PyObject* bind_method(PyObject*, Str, NativeFuncC, const char* sig=nullptr);
template<int ARGC>
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<bool>(VM* vm, PyObject* obj){
return obj == vm->True;
}
template<> inline CString py_cast<CString>(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<CString>(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<int ARGC>
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<int ARGC>
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;
}