cffi.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. #pragma once
  2. #include "common.h"
  3. #include "vm.h"
  4. namespace pkpy {
  5. #define PY_CLASS(T, mod, name) \
  6. static Type _type(VM* vm) { \
  7. static const StrName __x0(#mod); \
  8. static const StrName __x1(#name); \
  9. return PK_OBJ_GET(Type, vm->_modules[__x0]->attr(__x1)); \
  10. } \
  11. static void _check_type(VM* vm, PyObject* val){ \
  12. if(!vm->isinstance(val, T::_type(vm))){ \
  13. vm->TypeError("expected '" #mod "." #name "', got " + OBJ_NAME(vm->_t(val)).escape()); \
  14. } \
  15. } \
  16. static PyObject* register_class(VM* vm, PyObject* mod) { \
  17. if(OBJ_NAME(mod) != #mod) { \
  18. auto msg = fmt("register_class() failed: ", OBJ_NAME(mod), " != ", #mod); \
  19. throw std::runtime_error(msg); \
  20. } \
  21. PyObject* type = vm->new_type_object(mod, #name, vm->tp_object); \
  22. T::_register(vm, mod, type); \
  23. type->attr()._try_perfect_rehash(); \
  24. return type; \
  25. }
  26. #define VAR_T(T, ...) vm->heap.gcnew<T>(T::_type(vm), T(__VA_ARGS__))
  27. static int c99_sizeof(VM*, const Str&);
  28. struct VoidP{
  29. PY_CLASS(VoidP, c, void_p)
  30. void* ptr;
  31. int base_offset;
  32. VoidP(void* ptr): ptr(ptr), base_offset(1){}
  33. VoidP(): ptr(nullptr), base_offset(1){}
  34. bool operator==(const VoidP& other) const {
  35. return ptr == other.ptr && base_offset == other.base_offset;
  36. }
  37. bool operator!=(const VoidP& other) const {
  38. return ptr != other.ptr || base_offset != other.base_offset;
  39. }
  40. bool operator<(const VoidP& other) const { return ptr < other.ptr; }
  41. bool operator<=(const VoidP& other) const { return ptr <= other.ptr; }
  42. bool operator>(const VoidP& other) const { return ptr > other.ptr; }
  43. bool operator>=(const VoidP& other) const { return ptr >= other.ptr; }
  44. Str hex() const{
  45. std::stringstream ss;
  46. ss << std::hex << reinterpret_cast<intptr_t>(ptr);
  47. return "0x" + ss.str();
  48. }
  49. static void _register(VM* vm, PyObject* mod, PyObject* type){
  50. vm->bind_default_constructor<VoidP>(type);
  51. vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){
  52. std::string s = CAST(Str&, args[0]).str();
  53. size_t size;
  54. intptr_t ptr = std::stoll(s, &size, 16);
  55. if(size != s.size()) vm->ValueError("invalid literal for void_p(): " + s);
  56. return VAR_T(VoidP, (void*)ptr);
  57. });
  58. vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
  59. VoidP& self = _CAST(VoidP&, args[0]);
  60. return VAR(self.hex());
  61. });
  62. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  63. VoidP& self = _CAST(VoidP&, obj);
  64. std::stringstream ss;
  65. ss << "<void* at " << self.hex();
  66. if(self.base_offset != 1) ss << ", base_offset=" << self.base_offset;
  67. ss << ">";
  68. return VAR(ss.str());
  69. });
  70. #define BIND_CMP(name, op) \
  71. vm->bind##name(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){ \
  72. if(!is_non_tagged_type(rhs, VoidP::_type(vm))) return vm->NotImplemented; \
  73. return VAR(_CAST(VoidP&, lhs) op _CAST(VoidP&, rhs)); \
  74. });
  75. BIND_CMP(__eq__, ==)
  76. BIND_CMP(__lt__, <)
  77. BIND_CMP(__le__, <=)
  78. BIND_CMP(__gt__, >)
  79. BIND_CMP(__ge__, >=)
  80. #undef BIND_CMP
  81. vm->bind__hash__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  82. VoidP& self = _CAST(VoidP&, obj);
  83. return reinterpret_cast<i64>(self.ptr);
  84. });
  85. vm->bind_method<1>(type, "set_base_offset", [](VM* vm, ArgsView args){
  86. VoidP& self = _CAST(VoidP&, args[0]);
  87. if(is_non_tagged_type(args[1], vm->tp_str)){
  88. const Str& type = _CAST(Str&, args[1]);
  89. self.base_offset = c99_sizeof(vm, type);
  90. }else{
  91. self.base_offset = CAST(int, args[1]);
  92. }
  93. return vm->None;
  94. });
  95. vm->bind_method<0>(type, "get_base_offset", [](VM* vm, ArgsView args){
  96. VoidP& self = _CAST(VoidP&, args[0]);
  97. return VAR(self.base_offset);
  98. });
  99. vm->bind_method<1>(type, "offset", [](VM* vm, ArgsView args){
  100. VoidP& self = _CAST(VoidP&, args[0]);
  101. i64 offset = CAST(i64, args[1]);
  102. return VAR_T(VoidP, (char*)self.ptr + offset * self.base_offset);
  103. });
  104. vm->bind__add__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  105. VoidP& self = _CAST(VoidP&, lhs);
  106. i64 offset = CAST(i64, rhs);
  107. return VAR_T(VoidP, (char*)self.ptr + offset);
  108. });
  109. vm->bind__sub__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  110. VoidP& self = _CAST(VoidP&, lhs);
  111. i64 offset = CAST(i64, rhs);
  112. return VAR_T(VoidP, (char*)self.ptr - offset);
  113. });
  114. #define BIND_SETGET(T, name) \
  115. vm->bind_method<0>(type, "read_" name, [](VM* vm, ArgsView args){ \
  116. VoidP& self = _CAST(VoidP&, args[0]); \
  117. return VAR(*(T*)self.ptr); \
  118. }); \
  119. vm->bind_method<1>(type, "write_" name, [](VM* vm, ArgsView args){ \
  120. VoidP& self = _CAST(VoidP&, args[0]); \
  121. *(T*)self.ptr = CAST(T, args[1]); \
  122. return vm->None; \
  123. });
  124. BIND_SETGET(char, "char")
  125. BIND_SETGET(unsigned char, "uchar")
  126. BIND_SETGET(short, "short")
  127. BIND_SETGET(unsigned short, "ushort")
  128. BIND_SETGET(int, "int")
  129. BIND_SETGET(unsigned int, "uint")
  130. BIND_SETGET(long, "long")
  131. BIND_SETGET(unsigned long, "ulong")
  132. BIND_SETGET(long long, "longlong")
  133. BIND_SETGET(unsigned long long, "ulonglong")
  134. BIND_SETGET(float, "float")
  135. BIND_SETGET(double, "double")
  136. BIND_SETGET(bool, "bool")
  137. vm->bind_method<0>(type, "read_void_p", [](VM* vm, ArgsView args){
  138. VoidP& self = _CAST(VoidP&, args[0]);
  139. return VAR_T(VoidP, *(void**)self.ptr);
  140. });
  141. vm->bind_method<1>(type, "write_void_p", [](VM* vm, ArgsView args){
  142. VoidP& self = _CAST(VoidP&, args[0]);
  143. VoidP& other = CAST(VoidP&, args[0]);
  144. self.ptr = other.ptr;
  145. return vm->None;
  146. });
  147. vm->bind_method<1>(type, "read_bytes", [](VM* vm, ArgsView args){
  148. VoidP& self = _CAST(VoidP&, args[0]);
  149. i64 size = CAST(i64, args[1]);
  150. std::vector<char> buffer(size);
  151. memcpy(buffer.data(), self.ptr, size);
  152. return VAR(Bytes(std::move(buffer)));
  153. });
  154. vm->bind_method<1>(type, "write_bytes", [](VM* vm, ArgsView args){
  155. VoidP& self = _CAST(VoidP&, args[0]);
  156. Bytes& bytes = CAST(Bytes&, args[1]);
  157. memcpy(self.ptr, bytes.data(), bytes.size());
  158. return vm->None;
  159. });
  160. }
  161. };
  162. struct C99Struct{
  163. PY_CLASS(C99Struct, c, struct)
  164. static constexpr int INLINE_SIZE = 24;
  165. char _inlined[INLINE_SIZE];
  166. char* p;
  167. int size;
  168. void _init(int new_size){
  169. this->size = new_size;
  170. if(size <= INLINE_SIZE){
  171. p = _inlined;
  172. }else{
  173. p = (char*)malloc(size);
  174. }
  175. }
  176. template<typename T>
  177. C99Struct(const T& data){
  178. static_assert(std::is_pod_v<T>);
  179. static_assert(!std::is_pointer_v<T>);
  180. _init(sizeof(T));
  181. memcpy(p, &data, this->size);
  182. }
  183. C99Struct() { p = _inlined; }
  184. C99Struct(void* p, int size){
  185. _init(size);
  186. if(p!=nullptr) memcpy(this->p, p, size);
  187. }
  188. ~C99Struct(){ if(p!=_inlined) free(p); }
  189. C99Struct(const C99Struct& other){
  190. _init(other.size);
  191. memcpy(p, other.p, size);
  192. }
  193. static void _register(VM* vm, PyObject* mod, PyObject* type){
  194. vm->bind_default_constructor<C99Struct>(type);
  195. vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){
  196. C99Struct& self = _CAST(C99Struct&, args[0]);
  197. return VAR_T(VoidP, self.p);
  198. });
  199. vm->bind_method<0>(type, "size", [](VM* vm, ArgsView args){
  200. C99Struct& self = _CAST(C99Struct&, args[0]);
  201. return VAR(self.size);
  202. });
  203. vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){
  204. const C99Struct& self = _CAST(C99Struct&, args[0]);
  205. return VAR_T(C99Struct, self);
  206. });
  207. vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  208. C99Struct& self = _CAST(C99Struct&, lhs);
  209. if(!is_non_tagged_type(rhs, C99Struct::_type(vm))) return vm->NotImplemented;
  210. C99Struct& other = _CAST(C99Struct&, rhs);
  211. bool ok = self.size == other.size && memcmp(self.p, other.p, self.size) == 0;
  212. return VAR(ok);
  213. });
  214. // patch VoidP
  215. type = vm->_t(VoidP::_type(vm));
  216. vm->bind_method<1>(type, "read_struct", [](VM* vm, ArgsView args){
  217. VoidP& self = _CAST(VoidP&, args[0]);
  218. const Str& type = CAST(Str&, args[1]);
  219. int size = c99_sizeof(vm, type);
  220. return VAR_T(C99Struct, self.ptr, size);
  221. });
  222. vm->bind_method<1>(type, "write_struct", [](VM* vm, ArgsView args){
  223. VoidP& self = _CAST(VoidP&, args[0]);
  224. C99Struct& other = CAST(C99Struct&, args[1]);
  225. memcpy(self.ptr, other.p, other.size);
  226. return vm->None;
  227. });
  228. }
  229. };
  230. struct ReflField{
  231. std::string_view name;
  232. int offset;
  233. bool operator<(const ReflField& other) const{ return name < other.name; }
  234. bool operator==(const ReflField& other) const{ return name == other.name; }
  235. bool operator!=(const ReflField& other) const{ return name != other.name; }
  236. bool operator<(std::string_view other) const{ return name < other; }
  237. bool operator==(std::string_view other) const{ return name == other; }
  238. bool operator!=(std::string_view other) const{ return name != other; }
  239. };
  240. struct ReflType{
  241. std::string_view name;
  242. size_t size;
  243. std::vector<ReflField> fields;
  244. };
  245. inline static std::map<std::string_view, ReflType> _refl_types;
  246. inline void add_refl_type(std::string_view name, size_t size, std::vector<ReflField> fields){
  247. ReflType type{name, size, std::move(fields)};
  248. std::sort(type.fields.begin(), type.fields.end());
  249. _refl_types[name] = std::move(type);
  250. }
  251. inline static int c99_sizeof(VM* vm, const Str& type){
  252. auto it = _refl_types.find(type.sv());
  253. if(it != _refl_types.end()) return it->second.size;
  254. vm->ValueError("not a valid c99 type");
  255. return 0;
  256. }
  257. struct C99ReflType final: ReflType{
  258. PY_CLASS(C99ReflType, c, _refl)
  259. C99ReflType(const ReflType& type){
  260. this->name = type.name;
  261. this->size = type.size;
  262. this->fields = type.fields;
  263. }
  264. static void _register(VM* vm, PyObject* mod, PyObject* type){
  265. vm->bind_notimplemented_constructor<C99ReflType>(type);
  266. vm->bind_method<0>(type, "__call__", [](VM* vm, ArgsView args){
  267. C99ReflType& self = _CAST(C99ReflType&, args[0]);
  268. return VAR_T(C99Struct, nullptr, self.size);
  269. });
  270. vm->bind_method<0>(type, "name", [](VM* vm, ArgsView args){
  271. C99ReflType& self = _CAST(C99ReflType&, args[0]);
  272. return VAR(self.name);
  273. });
  274. vm->bind_method<0>(type, "size", [](VM* vm, ArgsView args){
  275. C99ReflType& self = _CAST(C99ReflType&, args[0]);
  276. return VAR(self.size);
  277. });
  278. vm->bind__getitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj, PyObject* key){
  279. C99ReflType& self = _CAST(C99ReflType&, obj);
  280. const Str& name = CAST(Str&, key);
  281. auto it = std::lower_bound(self.fields.begin(), self.fields.end(), name.sv());
  282. if(it == self.fields.end() || it->name != name.sv()){
  283. vm->KeyError(key);
  284. return vm->None;
  285. }
  286. return VAR(it->offset);
  287. });
  288. }
  289. };
  290. static_assert(sizeof(Py_<C99Struct>) <= 64);
  291. static_assert(sizeof(Py_<Tuple>) <= 64);
  292. inline PyObject* py_var(VM* vm, void* p){
  293. return VAR_T(VoidP, p);
  294. }
  295. inline PyObject* py_var(VM* vm, char* p){
  296. return VAR_T(VoidP, p);
  297. }
  298. /***********************************************/
  299. template<typename T>
  300. T to_void_p(VM* vm, PyObject* var){
  301. static_assert(std::is_pointer_v<T>);
  302. VoidP& p = CAST(VoidP&, var);
  303. return reinterpret_cast<T>(p.ptr);
  304. }
  305. template<typename T>
  306. T to_c99_struct(VM* vm, PyObject* var){
  307. static_assert(std::is_pod_v<T>);
  308. C99Struct& pod = CAST(C99Struct&, var);
  309. return *reinterpret_cast<T*>(pod.p);
  310. }
  311. template<typename T>
  312. std::enable_if_t<std::is_pod_v<T> && !std::is_pointer_v<T>, PyObject*> py_var(VM* vm, const T& data){
  313. return VAR_T(C99Struct, data);
  314. }
  315. /*****************************************************************/
  316. struct NativeProxyFuncCBase {
  317. virtual PyObject* operator()(VM* vm, ArgsView args) = 0;
  318. static void check_args_size(VM* vm, ArgsView args, int n){
  319. if (args.size() != n){
  320. vm->TypeError("expected " + std::to_string(n) + " arguments, got " + std::to_string(args.size()));
  321. }
  322. }
  323. };
  324. template<typename Ret, typename... Params>
  325. struct NativeProxyFuncC final: NativeProxyFuncCBase {
  326. static constexpr int N = sizeof...(Params);
  327. using _Fp = Ret(*)(Params...);
  328. _Fp func;
  329. NativeProxyFuncC(_Fp func) : func(func) {}
  330. PyObject* operator()(VM* vm, ArgsView args) override {
  331. check_args_size(vm, args, N);
  332. return call<Ret>(vm, args, std::make_index_sequence<N>());
  333. }
  334. template<typename __Ret, size_t... Is>
  335. PyObject* call(VM* vm, ArgsView args, std::index_sequence<Is...>){
  336. if constexpr(std::is_void_v<__Ret>){
  337. func(py_cast<Params>(vm, args[Is])...);
  338. return vm->None;
  339. }else{
  340. __Ret ret = func(py_cast<Params>(vm, args[Is])...);
  341. return VAR(std::move(ret));
  342. }
  343. }
  344. };
  345. inline PyObject* _any_c_wrapper(VM* vm, ArgsView args){
  346. NativeProxyFuncCBase* pf = lambda_get_userdata<NativeProxyFuncCBase*>(args.begin());
  347. return (*pf)(vm, args);
  348. }
  349. template<typename T>
  350. inline void bind_any_c_fp(VM* vm, PyObject* obj, Str name, T fp){
  351. static_assert(std::is_pod_v<T>);
  352. static_assert(std::is_pointer_v<T>);
  353. auto proxy = new NativeProxyFuncC(fp);
  354. PyObject* func = VAR(NativeFunc(_any_c_wrapper, proxy->N, false));
  355. _CAST(NativeFunc&, func).set_userdata(proxy);
  356. obj->attr().set(name, func);
  357. }
  358. inline void add_module_c(VM* vm){
  359. PyObject* mod = vm->new_module("c");
  360. vm->bind_func<1>(mod, "malloc", [](VM* vm, ArgsView args){
  361. i64 size = CAST(i64, args[0]);
  362. return VAR(malloc(size));
  363. });
  364. vm->bind_func<1>(mod, "free", [](VM* vm, ArgsView args){
  365. void* p = CAST(void*, args[0]);
  366. free(p);
  367. return vm->None;
  368. });
  369. vm->bind_func<1>(mod, "sizeof", [](VM* vm, ArgsView args){
  370. const Str& type = CAST(Str&, args[0]);
  371. i64 size = c99_sizeof(vm, type);
  372. return VAR(size);
  373. });
  374. vm->bind_func<1>(mod, "refl", [](VM* vm, ArgsView args){
  375. const Str& key = CAST(Str&, args[0]);
  376. auto it = _refl_types.find(key.sv());
  377. if(it == _refl_types.end()) vm->ValueError("reflection type not found");
  378. const ReflType& rt = it->second;
  379. return VAR_T(C99ReflType, rt);
  380. });
  381. vm->bind_func<3>(mod, "memset", [](VM* vm, ArgsView args){
  382. void* p = CAST(void*, args[0]);
  383. memset(p, CAST(int, args[1]), CAST(size_t, args[2]));
  384. return vm->None;
  385. });
  386. vm->bind_func<3>(mod, "memcpy", [](VM* vm, ArgsView args){
  387. void* dst = CAST(void*, args[0]);
  388. void* src = CAST(void*, args[1]);
  389. i64 size = CAST(i64, args[2]);
  390. memcpy(dst, src, size);
  391. return vm->None;
  392. });
  393. VoidP::register_class(vm, mod);
  394. C99Struct::register_class(vm, mod);
  395. C99ReflType::register_class(vm, mod);
  396. mod->attr().set("NULL", VAR_T(VoidP, nullptr));
  397. add_refl_type("char", sizeof(char), {});
  398. add_refl_type("uchar", sizeof(unsigned char), {});
  399. add_refl_type("short", sizeof(short), {});
  400. add_refl_type("ushort", sizeof(unsigned short), {});
  401. add_refl_type("int", sizeof(int), {});
  402. add_refl_type("uint", sizeof(unsigned int), {});
  403. add_refl_type("long", sizeof(long), {});
  404. add_refl_type("ulong", sizeof(unsigned long), {});
  405. add_refl_type("longlong", sizeof(long long), {});
  406. add_refl_type("ulonglong", sizeof(unsigned long long), {});
  407. add_refl_type("float", sizeof(float), {});
  408. add_refl_type("double", sizeof(double), {});
  409. add_refl_type("bool", sizeof(bool), {});
  410. add_refl_type("void_p", sizeof(void*), {});
  411. }
  412. struct Int32Flags{
  413. PY_CLASS(Int32Flags, builtins, int32flags)
  414. uint32_t value;
  415. Int32Flags(uint32_t value) : value(value) {}
  416. static void _register(VM* vm, PyObject* mod, PyObject* type){
  417. vm->bind_constructor<-1>(type, [](VM* vm, ArgsView args){
  418. vm->check_args_size(args.size(), 1+0, 1+1);
  419. uint32_t value = CAST_DEFAULT(uint32_t, 1, 0);
  420. return VAR_T(Int32Flags, value);
  421. });
  422. vm->bind__repr__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* self){
  423. uint32_t value = _CAST(Int32Flags&, self).value;
  424. std::string str;
  425. for(int i=0; i<32; ++i) str += (value & (1<<i)) ? '1' : '0';
  426. std::reverse(str.begin(), str.end());
  427. return VAR(str);
  428. });
  429. vm->bind__and__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  430. uint32_t value = _CAST(Int32Flags&, lhs).value;
  431. if(!is_non_tagged_type(rhs, Int32Flags::_type(vm))) return vm->NotImplemented;
  432. return VAR_T(Int32Flags, value & _CAST(Int32Flags&, rhs).value);
  433. });
  434. vm->bind__or__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  435. uint32_t value = _CAST(Int32Flags&, lhs).value;
  436. if(!is_non_tagged_type(rhs, Int32Flags::_type(vm))) return vm->NotImplemented;
  437. return VAR_T(Int32Flags, value | _CAST(Int32Flags&, rhs).value);
  438. });
  439. vm->bind__xor__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  440. uint32_t value = _CAST(Int32Flags&, lhs).value;
  441. if(!is_non_tagged_type(rhs, Int32Flags::_type(vm))) return vm->NotImplemented;
  442. return VAR_T(Int32Flags, value ^ _CAST(Int32Flags&, rhs).value);
  443. });
  444. vm->bind__lshift__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  445. uint32_t value = _CAST(Int32Flags&, lhs).value;
  446. if(!is_int(rhs)) return vm->NotImplemented;
  447. return VAR_T(Int32Flags, value << _CAST(i64, rhs));
  448. });
  449. vm->bind__rshift__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* lhs, PyObject* rhs){
  450. uint32_t value = _CAST(Int32Flags&, lhs).value;
  451. if(!is_int(rhs)) return vm->NotImplemented;
  452. return VAR_T(Int32Flags, value >> _CAST(i64, rhs));
  453. });
  454. vm->bind__getitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* self, PyObject* i){
  455. uint32_t value = _CAST(Int32Flags&, self).value;
  456. int index = CAST(i64, i);
  457. index = vm->normalized_index(index, 32);
  458. int ok = value & (1 << index);
  459. return VAR(ok);
  460. });
  461. vm->bind__setitem__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* self, PyObject* i, PyObject* v){
  462. uint32_t& value = _CAST(Int32Flags&, self).value;
  463. int index = CAST(i64, i);
  464. index = vm->normalized_index(index, 32);
  465. bool ok = CAST(i64, v) != 0;
  466. if(ok) value |= (1 << index);
  467. else value &= ~(1 << index);
  468. });
  469. }
  470. };
  471. } // namespace pkpy