internal.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "pocketpy/interpreter/typeinfo.h"
  2. #include "pocketpy/objects/codeobject.h"
  3. #include "pocketpy/objects/sourcedata.h"
  4. #include "pocketpy/pocketpy.h"
  5. #include "pocketpy/common/utils.h"
  6. #include "pocketpy/common/sstream.h"
  7. #include "pocketpy/objects/object.h"
  8. #include "pocketpy/interpreter/vm.h"
  9. #include "pocketpy/compiler/compiler.h"
  10. VM* pk_current_vm;
  11. py_GlobalRef py_True;
  12. py_GlobalRef py_False;
  13. py_GlobalRef py_None;
  14. py_GlobalRef py_NIL;
  15. static VM pk_default_vm;
  16. static VM* pk_all_vm[16];
  17. void py_initialize() {
  18. MemoryPools__initialize();
  19. py_Name__initialize();
  20. pk_current_vm = pk_all_vm[0] = &pk_default_vm;
  21. // initialize some convenient references
  22. static py_TValue _True, _False, _None, _NIL;
  23. py_newbool(&_True, true);
  24. py_newbool(&_False, false);
  25. py_newnone(&_None);
  26. py_newnil(&_NIL);
  27. py_True = &_True;
  28. py_False = &_False;
  29. py_None = &_None;
  30. py_NIL = &_NIL;
  31. VM__ctor(&pk_default_vm);
  32. }
  33. void py_finalize() {
  34. for(int i = 1; i < 16; i++) {
  35. VM* vm = pk_all_vm[i];
  36. if(vm) {
  37. VM__dtor(vm);
  38. free(vm);
  39. }
  40. }
  41. VM__dtor(&pk_default_vm);
  42. pk_current_vm = NULL;
  43. py_Name__finalize();
  44. MemoryPools__finalize();
  45. }
  46. void py_switchvm(int index) {
  47. if(index < 0 || index >= 16) c11__abort("invalid vm index");
  48. if(!pk_all_vm[index]) {
  49. pk_all_vm[index] = malloc(sizeof(VM));
  50. VM__ctor(pk_all_vm[index]);
  51. }
  52. pk_current_vm = pk_all_vm[index];
  53. }
  54. void py_resetvm() {
  55. VM* vm = pk_current_vm;
  56. VM__dtor(vm);
  57. memset(vm, 0, sizeof(VM));
  58. VM__ctor(vm);
  59. }
  60. int py_currentvm() {
  61. for(int i = 0; i < 16; i++) {
  62. if(pk_all_vm[i] == pk_current_vm) return i;
  63. }
  64. return -1;
  65. }
  66. void* py_getvmctx(){
  67. return pk_current_vm->ctx;
  68. }
  69. void py_setvmctx(void* ctx){
  70. pk_current_vm->ctx = ctx;
  71. }
  72. void py_sys_setargv(int argc, char** argv) {
  73. py_GlobalRef sys = py_getmodule("sys");
  74. py_Ref argv_list = py_getdict(sys, py_name("argv"));
  75. py_list_clear(argv_list);
  76. for(int i = 0; i < argc; i++) {
  77. py_newstr(py_list_emplace(argv_list), argv[i]);
  78. }
  79. }
  80. py_Callbacks* py_callbacks() { return &pk_current_vm->callbacks; }
  81. const char* pk_opname(Opcode op) {
  82. const static char* OP_NAMES[] = {
  83. #define OPCODE(name) #name,
  84. #include "pocketpy/xmacros/opcodes.h"
  85. #undef OPCODE
  86. };
  87. return OP_NAMES[op];
  88. }
  89. bool py_call(py_Ref f, int argc, py_Ref argv) {
  90. if(f->type == tp_nativefunc) {
  91. return py_callcfunc(f->_cfunc, argc, argv);
  92. } else {
  93. py_StackRef p0 = py_peek(0);
  94. py_push(f);
  95. py_pushnil();
  96. for(int i = 0; i < argc; i++)
  97. py_push(py_offset(argv, i));
  98. bool ok = py_vectorcall(argc, 0);
  99. pk_current_vm->stack.sp = p0;
  100. return ok;
  101. }
  102. }
  103. #ifndef NDEBUG
  104. bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) {
  105. py_StackRef p0 = py_peek(0);
  106. py_newnil(py_retval());
  107. bool ok = f(argc, argv);
  108. if(!ok) {
  109. if(!py_checkexc(true)) {
  110. c11__abort("py_CFunction returns `false` but no exception is set!");
  111. }
  112. return false;
  113. }
  114. if(py_peek(0) != p0) {
  115. c11__abort("py_CFunction corrupts the stack! Did you forget to call `py_pop()`?");
  116. }
  117. if(py_isnil(py_retval())) {
  118. c11__abort(
  119. "py_CFunction returns nothing! Did you forget to call `py_newnone(py_retval())`?");
  120. }
  121. if(py_checkexc(true)) { c11__abort("py_CFunction returns `true` but an exception is set!"); }
  122. return true;
  123. }
  124. #endif
  125. bool py_vectorcall(uint16_t argc, uint16_t kwargc) {
  126. return VM__vectorcall(pk_current_vm, argc, kwargc, false) != RES_ERROR;
  127. }
  128. py_Ref py_retval() { return &pk_current_vm->last_retval; }
  129. bool py_pushmethod(py_Name name) {
  130. bool ok = pk_loadmethod(py_peek(-1), name);
  131. if(ok) pk_current_vm->stack.sp++;
  132. return ok;
  133. }
  134. bool pk_loadmethod(py_StackRef self, py_Name name) {
  135. // NOTE: `out` and `out_self` may overlap with `self`
  136. if(name == __new__ && py_istype(self, tp_type)) {
  137. // __new__ acts like a @staticmethod
  138. // T.__new__(...)
  139. py_Ref cls_var = py_tpfindmagic(py_totype(self), name);
  140. if(cls_var) {
  141. self[0] = *cls_var;
  142. self[1] = *py_NIL;
  143. return true;
  144. }
  145. return false;
  146. }
  147. py_Type type;
  148. // handle super() proxy
  149. if(py_istype(self, tp_super)) {
  150. type = *(py_Type*)py_touserdata(self);
  151. *self = *py_getslot(self, 0);
  152. } else {
  153. type = self->type;
  154. }
  155. py_Ref cls_var = py_tpfindname(type, name);
  156. if(cls_var != NULL) {
  157. switch(cls_var->type) {
  158. case tp_function:
  159. case tp_nativefunc: {
  160. py_TValue self_bak = *self;
  161. // `out` may overlap with `self`. If we assign `out`, `self` may be corrupted.
  162. self[0] = *cls_var;
  163. self[1] = self_bak;
  164. break;
  165. }
  166. case tp_staticmethod:
  167. self[0] = *py_getslot(cls_var, 0);
  168. self[1] = *py_NIL;
  169. break;
  170. case tp_classmethod:
  171. self[0] = *py_getslot(cls_var, 0);
  172. self[1] = pk__type_info(type)->self;
  173. break;
  174. default: c11__unreachedable();
  175. }
  176. return true;
  177. }
  178. return false;
  179. }
  180. py_Ref py_tpfindmagic(py_Type t, py_Name name) {
  181. assert(py_ismagicname(name));
  182. py_TypeInfo* ti = pk__type_info(t);
  183. do {
  184. py_Ref f = &ti->magic[name];
  185. if(!py_isnil(f)) return f;
  186. ti = ti->base_ti;
  187. } while(ti);
  188. return NULL;
  189. }
  190. py_Ref py_tpfindname(py_Type t, py_Name name) {
  191. py_TypeInfo* ti = pk__type_info(t);
  192. do {
  193. py_Ref res = py_getdict(&ti->self, name);
  194. if(res) return res;
  195. ti = ti->base_ti;
  196. } while(ti);
  197. return NULL;
  198. }
  199. py_Ref py_tpgetmagic(py_Type type, py_Name name) {
  200. assert(py_ismagicname(name));
  201. return pk__type_info(type)->magic + name;
  202. }
  203. py_Ref py_tpobject(py_Type type) {
  204. assert(type);
  205. return &pk__type_info(type)->self;
  206. }
  207. const char* py_tpname(py_Type type) {
  208. if(!type) return "nil";
  209. py_Name name = pk__type_info(type)->name;
  210. return py_name2str(name);
  211. }
  212. bool py_tpcall(py_Type type, int argc, py_Ref argv) {
  213. return py_call(py_tpobject(type), argc, argv);
  214. }
  215. bool pk_callmagic(py_Name name, int argc, py_Ref argv) {
  216. assert(argc >= 1);
  217. assert(py_ismagicname(name));
  218. py_Ref tmp = py_tpfindmagic(argv->type, name);
  219. if(!tmp) return AttributeError(argv, name);
  220. return py_call(tmp, argc, argv);
  221. }
  222. bool StopIteration() { return py_exception(tp_StopIteration, ""); }