vm.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include "pocketpy/pocketpy.h"
  3. #include "pocketpy/interpreter/gc.h"
  4. #include "pocketpy/interpreter/frame.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. typedef struct pk_TypeInfo{
  9. StrName name;
  10. Type base;
  11. PyObject* obj; // the type object itself
  12. PyObject* module; // the module where the type is defined
  13. bool subclass_enabled;
  14. void (*dtor)(void*);
  15. void (*gc_mark)(void*);
  16. c11_vector/*T=StrName*/ annotated_fields;
  17. /* Magic Caches */
  18. // unary operators
  19. py_CFunction m__repr__, m__str__, m__hash__, m__len__;
  20. py_CFunction m__iter__, m__next__, m__neg__, m__invert__;
  21. // binary operators
  22. py_CFunction m__eq__, m__lt__, m__le__, m__gt__, m__ge__, m__contains__;
  23. py_CFunction m__add__, m__sub__, m__mul__, m__truediv__, m__floordiv__;
  24. py_CFunction m__mod__, m__pow__, m__matmul__;
  25. py_CFunction m__lshift__, m__rshift__, m__and__, m__xor__, m__or__;
  26. // indexer
  27. py_CFunction m__getitem__, m__setitem__, m__delitem__;
  28. // attribute access (internal use only)
  29. py_CFunction m__getattr__, m__setattr__, m__delattr__;
  30. // backdoors
  31. py_CFunction on_end_subclass; // for enum module
  32. } pk_TypeInfo;
  33. void pk_TypeInfo__ctor(pk_TypeInfo *self, StrName name, Type base, PyObject* obj, PyObject* module, bool subclass_enabled);
  34. void pk_TypeInfo__dtor(pk_TypeInfo* self);
  35. typedef struct pk_VM {
  36. Frame* top_frame;
  37. pk_NameDict modules;
  38. c11_vector/*T=pk_TypeInfo*/ types;
  39. PyObject* StopIteration; // a special Exception class
  40. PyObject* builtins; // builtins module
  41. PyObject* main; // __main__ module
  42. void (*_ceval_on_step)(struct pk_VM*, Frame*, Bytecode);
  43. unsigned char* (*_import_file)(struct pk_VM*, const char*);
  44. void (*_stdout)(struct pk_VM*, const char*);
  45. void (*_stderr)(struct pk_VM*, const char*);
  46. // singleton objects
  47. PyVar True, False, None, NotImplemented, Ellipsis;
  48. PyObject* __last_exception;
  49. PyObject* __curr_class;
  50. PyObject* __cached_object_new;
  51. FuncDecl_ __dynamic_func_decl;
  52. PyVar __vectorcall_buffer[PK_MAX_CO_VARNAMES];
  53. void* userdata; // for user-defined data (unused by pkpy itself)
  54. pk_ManagedHeap heap;
  55. ValueStack stack; // put `stack` at the end for better cache locality
  56. } pk_VM;
  57. void pk_VM__ctor(pk_VM* self);
  58. void pk_VM__dtor(pk_VM* self);
  59. Type pk_VM__new_type(pk_VM* self, const char* name, Type base, PyObject* module, bool subclass_enabled);
  60. PyObject* pk_VM__new_module(pk_VM* self, const char* name, const char* package);
  61. #ifdef __cplusplus
  62. }
  63. #endif