py_ops.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "pocketpy/interpreter/vm.h"
  2. #include "pocketpy/objects/base.h"
  3. #include "pocketpy/pocketpy.h"
  4. bool py_isidentical(py_Ref lhs, py_Ref rhs) {
  5. if(lhs->type != rhs->type) return false;
  6. switch(lhs->type) {
  7. case tp_int: return lhs->_i64 == rhs->_i64;
  8. case tp_float: return lhs->_f64 == rhs->_f64;
  9. case tp_bool: return lhs->_bool == rhs->_bool;
  10. case tp_nativefunc: return lhs->_cfunc == rhs->_cfunc;
  11. case tp_NoneType: return true;
  12. case tp_NotImplementedType: return true;
  13. case tp_ellipsis: return true;
  14. // fallback to pointer comparison
  15. default: return lhs->is_ptr && rhs->is_ptr && lhs->_obj == rhs->_obj;
  16. }
  17. }
  18. int py_bool(py_Ref val) {
  19. switch(val->type) {
  20. case tp_bool: return val->_bool;
  21. case tp_int: return val->_i64 != 0;
  22. case tp_float: return val->_f64 != 0;
  23. case tp_NoneType: return 0;
  24. default: {
  25. py_Ref tmp = py_tpfindmagic(val->type, __bool__);
  26. if(tmp) {
  27. if(!py_call(tmp, 1, val)) return -1;
  28. if(!py_checkbool(py_retval())) return -1;
  29. return py_tobool(py_retval());
  30. } else {
  31. tmp = py_tpfindmagic(val->type, __len__);
  32. if(tmp) {
  33. if(!py_call(tmp, 1, val)) return -1;
  34. if(!py_checkint(py_retval())) return -1;
  35. return py_toint(py_retval());
  36. } else {
  37. return 1; // True
  38. }
  39. }
  40. }
  41. }
  42. }
  43. bool py_hash(py_Ref val, int64_t* out) {
  44. py_Type t = val->type;
  45. py_TypeInfo* types = (py_TypeInfo*)pk_current_vm->types.data;
  46. do {
  47. py_Ref _hash = &types[t].magic[__hash__];
  48. if(py_isnone(_hash)) break;
  49. py_Ref _eq = &types[t].magic[__eq__];
  50. if(!py_isnil(_hash) && !py_isnil(_eq)) {
  51. if(!py_call(_hash, 1, val)) return false;
  52. if(!py_checkint(py_retval())) return false;
  53. *out = py_toint(py_retval());
  54. return true;
  55. }
  56. t = types[t].base;
  57. } while(t);
  58. return TypeError("unhashable type: '%t'", val->type);
  59. }
  60. bool py_iter(py_Ref val) {
  61. py_Ref tmp = py_tpfindmagic(val->type, __iter__);
  62. if(!tmp) return TypeError("'%t' object is not iterable", val->type);
  63. return py_call(tmp, 1, val);
  64. }
  65. int py_next(py_Ref val) {
  66. VM* vm = pk_current_vm;
  67. vm->is_stopiteration = false;
  68. py_Ref tmp = py_tpfindmagic(val->type, __next__);
  69. if(!tmp) return TypeError("'%t' object is not an iterator", val->type);
  70. py_StackRef p0 = py_peek(0);
  71. if(py_call(tmp, 1, val)) return true;
  72. if(vm->curr_exception.type == tp_StopIteration) {
  73. py_clearexc(p0);
  74. vm->is_stopiteration = true;
  75. }
  76. return vm->is_stopiteration ? 0 : -1;
  77. }
  78. bool py_getattr(py_Ref self, py_Name name) {
  79. // https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance
  80. py_Type type = self->type;
  81. // handle super() proxy
  82. if(py_istype(self, tp_super)) {
  83. self = py_getslot(self, 0);
  84. type = *(py_Type*)py_touserdata(self);
  85. }
  86. py_Ref cls_var = py_tpfindname(type, name);
  87. if(cls_var) {
  88. // handle descriptor
  89. if(py_istype(cls_var, tp_property)) {
  90. py_Ref getter = py_getslot(cls_var, 0);
  91. return py_call(getter, 1, self);
  92. }
  93. }
  94. // handle instance __dict__
  95. if(self->is_ptr && self->_obj->slots == -1) {
  96. if(!py_istype(self, tp_type)) {
  97. py_Ref res = py_getdict(self, name);
  98. if(res) {
  99. py_assign(py_retval(), res);
  100. return true;
  101. }
  102. } else {
  103. py_Type* inner_type = py_touserdata(self);
  104. py_Ref res = py_tpfindname(*inner_type, name);
  105. if(res) {
  106. if(py_istype(res, tp_staticmethod)) {
  107. res = py_getslot(res, 0);
  108. } else if(py_istype(res, tp_classmethod)) {
  109. res = py_getslot(res, 0);
  110. py_newboundmethod(py_retval(), self, res);
  111. return true;
  112. }
  113. py_assign(py_retval(), res);
  114. return true;
  115. }
  116. }
  117. }
  118. if(cls_var) {
  119. // bound method is non-data descriptor
  120. switch(cls_var->type) {
  121. case tp_function: {
  122. py_newboundmethod(py_retval(), self, cls_var);
  123. return true;
  124. }
  125. case tp_nativefunc: {
  126. py_newboundmethod(py_retval(), self, cls_var);
  127. return true;
  128. }
  129. case tp_staticmethod: {
  130. py_assign(py_retval(), py_getslot(cls_var, 0));
  131. return true;
  132. }
  133. case tp_classmethod: {
  134. py_newboundmethod(py_retval(), py_tpobject(type), py_getslot(cls_var, 0));
  135. return true;
  136. }
  137. default: {
  138. py_assign(py_retval(), cls_var);
  139. return true;
  140. }
  141. }
  142. }
  143. if(self->type == tp_module) {
  144. py_Ref path = py_getdict(self, __path__);
  145. c11_sbuf buf;
  146. c11_sbuf__ctor(&buf);
  147. pk_sprintf(&buf, "%v.%n", py_tosv(path), name);
  148. c11_string* new_path = c11_sbuf__submit(&buf);
  149. int res = py_import(new_path->data);
  150. c11_string__delete(new_path);
  151. if(res == -1) {
  152. return false;
  153. } else if(res == 1) {
  154. return true;
  155. }
  156. }
  157. return AttributeError(self, name);
  158. }
  159. bool py_setattr(py_Ref self, py_Name name, py_Ref val) {
  160. py_Type type = self->type;
  161. // handle super() proxy
  162. if(py_istype(self, tp_super)) {
  163. self = py_getslot(self, 0);
  164. type = *(py_Type*)py_touserdata(self);
  165. }
  166. py_Ref cls_var = py_tpfindname(type, name);
  167. if(cls_var) {
  168. // handle descriptor
  169. if(py_istype(cls_var, tp_property)) {
  170. py_Ref setter = py_getslot(cls_var, 1);
  171. if(!py_isnone(setter)) {
  172. py_push(setter);
  173. py_push(self);
  174. py_push(val);
  175. return py_vectorcall(1, 0);
  176. } else {
  177. return TypeError("readonly attribute: '%n'", name);
  178. }
  179. }
  180. }
  181. // handle instance __dict__
  182. if(self->is_ptr && self->_obj->slots == -1) {
  183. py_setdict(self, name, val);
  184. return true;
  185. }
  186. return TypeError("cannot set attribute");
  187. }
  188. bool py_delattr(py_Ref self, py_Name name) {
  189. if(self->is_ptr && self->_obj->slots == -1) {
  190. if(py_deldict(self, name)) return true;
  191. return AttributeError(self, name);
  192. }
  193. return TypeError("cannot delete attribute");
  194. }
  195. bool py_getitem(py_Ref self, py_Ref key) {
  196. py_push(self);
  197. py_push(key);
  198. bool ok = pk_callmagic(__getitem__, 2, py_peek(-2));
  199. py_shrink(2);
  200. return ok;
  201. }
  202. bool py_setitem(py_Ref self, py_Ref key, py_Ref val) {
  203. py_push(self);
  204. py_push(key);
  205. py_push(val);
  206. bool ok = pk_callmagic(__setitem__, 3, py_peek(-3));
  207. py_shrink(3);
  208. return ok;
  209. }
  210. bool py_delitem(py_Ref self, py_Ref key) {
  211. py_push(self);
  212. py_push(key);
  213. bool ok = pk_callmagic(__delitem__, 2, py_peek(-2));
  214. py_shrink(2);
  215. return ok;
  216. }
  217. int py_equal(py_Ref lhs, py_Ref rhs) {
  218. if(!py_eq(lhs, rhs)) return -1;
  219. return py_bool(py_retval());
  220. }
  221. int py_less(py_Ref lhs, py_Ref rhs) {
  222. if(!py_lt(lhs, rhs)) return -1;
  223. return py_bool(py_retval());
  224. }