| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #pragma once
- #include "common.h"
- #include "vm.h"
- namespace pkpy {
- #define PY_CLASS(T, mod, name) \
- static Type _type(VM* vm) { \
- static const StrName __x0(#mod); \
- static const StrName __x1(#name); \
- return PK_OBJ_GET(Type, vm->_modules[__x0]->attr(__x1)); \
- } \
- static void _check_type(VM* vm, PyObject* val){ \
- if(!vm->isinstance(val, T::_type(vm))){ \
- vm->TypeError("expected '" #mod "." #name "', got " + OBJ_NAME(vm->_t(val)).escape()); \
- } \
- } \
- static PyObject* register_class(VM* vm, PyObject* mod) { \
- if(OBJ_NAME(mod) != #mod) { \
- auto msg = fmt("register_class() failed: ", OBJ_NAME(mod), " != ", #mod); \
- throw std::runtime_error(msg); \
- } \
- PyObject* type = vm->new_type_object(mod, #name, vm->tp_object); \
- T::_register(vm, mod, type); \
- type->attr()._try_perfect_rehash(); \
- return type; \
- }
- #define VAR_T(T, ...) vm->heap.gcnew<T>(T::_type(vm), T(__VA_ARGS__))
- int c99_sizeof(VM*, const Str&);
- inline PyObject* py_var(VM* vm, void* p);
- inline PyObject* py_var(VM* vm, char* p);
- struct VoidP{
- PY_CLASS(VoidP, c, void_p)
- void* ptr;
- int base_offset;
- VoidP(void* ptr): ptr(ptr), base_offset(1){}
- VoidP(): ptr(nullptr), base_offset(1){}
- bool operator==(const VoidP& other) const {
- return ptr == other.ptr && base_offset == other.base_offset;
- }
- bool operator!=(const VoidP& other) const {
- return ptr != other.ptr || base_offset != other.base_offset;
- }
- bool operator<(const VoidP& other) const { return ptr < other.ptr; }
- bool operator<=(const VoidP& other) const { return ptr <= other.ptr; }
- bool operator>(const VoidP& other) const { return ptr > other.ptr; }
- bool operator>=(const VoidP& other) const { return ptr >= other.ptr; }
- Str hex() const{
- std::stringstream ss;
- ss << std::hex << reinterpret_cast<intptr_t>(ptr);
- return "0x" + ss.str();
- }
- static void _register(VM* vm, PyObject* mod, PyObject* type);
- };
- struct C99Struct{
- PY_CLASS(C99Struct, c, struct)
- static constexpr int INLINE_SIZE = 24;
- char _inlined[INLINE_SIZE];
- char* p;
- int size;
- C99Struct(int new_size){
- this->size = new_size;
- if(size <= INLINE_SIZE){
- p = _inlined;
- }else{
- p = (char*)malloc(size);
- }
- }
- template<typename T>
- C99Struct(std::monostate _, const T& data): C99Struct(sizeof(T)){
- static_assert(std::is_pod_v<T>);
- static_assert(!std::is_pointer_v<T>);
- memcpy(p, &data, this->size);
- }
- C99Struct(void* p, int size): C99Struct(size){
- if(p != nullptr) memcpy(this->p, p, size);
- }
- C99Struct(const C99Struct& other): C99Struct(other.p, other.size){}
- ~C99Struct(){ if(p!=_inlined) free(p); }
- static void _register(VM* vm, PyObject* mod, PyObject* type);
- };
- struct ReflField{
- std::string_view name;
- int offset;
- bool operator<(const ReflField& other) const{ return name < other.name; }
- bool operator==(const ReflField& other) const{ return name == other.name; }
- bool operator!=(const ReflField& other) const{ return name != other.name; }
- bool operator<(std::string_view other) const{ return name < other; }
- bool operator==(std::string_view other) const{ return name == other; }
- bool operator!=(std::string_view other) const{ return name != other; }
- };
- struct ReflType{
- std::string_view name;
- size_t size;
- std::vector<ReflField> fields;
- };
- inline static std::map<std::string_view, ReflType> _refl_types;
- inline void add_refl_type(std::string_view name, size_t size, std::vector<ReflField> fields){
- ReflType type{name, size, std::move(fields)};
- std::sort(type.fields.begin(), type.fields.end());
- _refl_types[name] = std::move(type);
- }
- struct C99ReflType final: ReflType{
- PY_CLASS(C99ReflType, c, _refl)
- C99ReflType(const ReflType& type){
- this->name = type.name;
- this->size = type.size;
- this->fields = type.fields;
- }
- static void _register(VM* vm, PyObject* mod, PyObject* type);
- };
- static_assert(sizeof(Py_<C99Struct>) <= 64);
- static_assert(sizeof(Py_<Tuple>) <= 64);
- inline PyObject* py_var(VM* vm, void* p){
- return VAR_T(VoidP, p);
- }
- inline PyObject* py_var(VM* vm, char* p){
- return VAR_T(VoidP, p);
- }
- /***********************************************/
- template<typename T>
- T to_void_p(VM* vm, PyObject* var){
- static_assert(std::is_pointer_v<T>);
- if(var == vm->None) return nullptr; // None can be casted to any pointer implicitly
- VoidP& p = CAST(VoidP&, var);
- return reinterpret_cast<T>(p.ptr);
- }
- template<typename T>
- T to_c99_struct(VM* vm, PyObject* var){
- static_assert(std::is_pod_v<T>);
- C99Struct& pod = CAST(C99Struct&, var);
- return *reinterpret_cast<T*>(pod.p);
- }
- template<typename T>
- std::enable_if_t<std::is_pod_v<T> && !std::is_pointer_v<T>, PyObject*> py_var(VM* vm, const T& data){
- return VAR_T(C99Struct, std::monostate(), data);
- }
- /*****************************************************************/
- struct NativeProxyFuncCBase {
- virtual PyObject* operator()(VM* vm, ArgsView args) = 0;
- static void check_args_size(VM* vm, ArgsView args, int n){
- if (args.size() != n){
- vm->TypeError("expected " + std::to_string(n) + " arguments, got " + std::to_string(args.size()));
- }
- }
- };
- template<typename Ret, typename... Params>
- struct NativeProxyFuncC final: NativeProxyFuncCBase {
- static constexpr int N = sizeof...(Params);
- using _Fp = Ret(*)(Params...);
- _Fp func;
- NativeProxyFuncC(_Fp func) : func(func) {}
- PyObject* operator()(VM* vm, ArgsView args) override {
- check_args_size(vm, args, N);
- return call<Ret>(vm, args, std::make_index_sequence<N>());
- }
- template<typename __Ret, size_t... Is>
- PyObject* call(VM* vm, ArgsView args, std::index_sequence<Is...>){
- if constexpr(std::is_void_v<__Ret>){
- func(py_cast<Params>(vm, args[Is])...);
- return vm->None;
- }else{
- __Ret ret = func(py_cast<Params>(vm, args[Is])...);
- return VAR(std::move(ret));
- }
- }
- };
- inline PyObject* _any_c_wrapper(VM* vm, ArgsView args){
- NativeProxyFuncCBase* pf = lambda_get_userdata<NativeProxyFuncCBase*>(args.begin());
- return (*pf)(vm, args);
- }
- template<typename T>
- inline void bind_any_c_fp(VM* vm, PyObject* obj, Str name, T fp){
- static_assert(std::is_pod_v<T>);
- static_assert(std::is_pointer_v<T>);
- auto proxy = new NativeProxyFuncC(fp);
- PyObject* func = VAR(NativeFunc(_any_c_wrapper, proxy->N, false));
- _CAST(NativeFunc&, func).set_userdata(proxy);
- obj->attr().set(name, func);
- }
- void add_module_c(VM* vm);
- } // namespace pkpy
|