py_ops.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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) {
  70. TypeError("'%t' object is not an iterator", val->type);
  71. return -1;
  72. }
  73. py_StackRef p0 = py_peek(0);
  74. if(py_call(tmp, 1, val)) return true;
  75. if(vm->curr_exception.type == tp_StopIteration) {
  76. py_clearexc(p0);
  77. vm->is_stopiteration = true;
  78. }
  79. return vm->is_stopiteration ? 0 : -1;
  80. }
  81. bool py_getattr(py_Ref self, py_Name name) {
  82. // https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance
  83. py_Type type = self->type;
  84. // handle super() proxy
  85. if(py_istype(self, tp_super)) {
  86. self = py_getslot(self, 0);
  87. type = *(py_Type*)py_touserdata(self);
  88. }
  89. py_Ref cls_var = py_tpfindname(type, name);
  90. if(cls_var) {
  91. // handle descriptor
  92. if(py_istype(cls_var, tp_property)) {
  93. py_Ref getter = py_getslot(cls_var, 0);
  94. return py_call(getter, 1, self);
  95. }
  96. }
  97. // handle instance __dict__
  98. if(self->is_ptr && self->_obj->slots == -1) {
  99. if(!py_istype(self, tp_type)) {
  100. py_Ref res = py_getdict(self, name);
  101. if(res) {
  102. py_assign(py_retval(), res);
  103. return true;
  104. }
  105. } else {
  106. py_Type* inner_type = py_touserdata(self);
  107. py_Ref res = py_tpfindname(*inner_type, name);
  108. if(res) {
  109. if(py_istype(res, tp_staticmethod)) {
  110. res = py_getslot(res, 0);
  111. } else if(py_istype(res, tp_classmethod)) {
  112. res = py_getslot(res, 0);
  113. py_newboundmethod(py_retval(), self, res);
  114. return true;
  115. }
  116. py_assign(py_retval(), res);
  117. return true;
  118. }
  119. }
  120. }
  121. if(cls_var) {
  122. // bound method is non-data descriptor
  123. switch(cls_var->type) {
  124. case tp_function: {
  125. py_newboundmethod(py_retval(), self, cls_var);
  126. return true;
  127. }
  128. case tp_nativefunc: {
  129. py_newboundmethod(py_retval(), self, cls_var);
  130. return true;
  131. }
  132. case tp_staticmethod: {
  133. py_assign(py_retval(), py_getslot(cls_var, 0));
  134. return true;
  135. }
  136. case tp_classmethod: {
  137. py_newboundmethod(py_retval(), py_tpobject(type), py_getslot(cls_var, 0));
  138. return true;
  139. }
  140. default: {
  141. py_assign(py_retval(), cls_var);
  142. return true;
  143. }
  144. }
  145. }
  146. if(self->type == tp_module) {
  147. py_Ref path = py_getdict(self, __path__);
  148. c11_sbuf buf;
  149. c11_sbuf__ctor(&buf);
  150. pk_sprintf(&buf, "%v.%n", py_tosv(path), name);
  151. c11_string* new_path = c11_sbuf__submit(&buf);
  152. int res = py_import(new_path->data);
  153. c11_string__delete(new_path);
  154. if(res == -1) {
  155. return false;
  156. } else if(res == 1) {
  157. return true;
  158. }
  159. }
  160. return AttributeError(self, name);
  161. }
  162. bool py_setattr(py_Ref self, py_Name name, py_Ref val) {
  163. py_Type type = self->type;
  164. // handle super() proxy
  165. if(py_istype(self, tp_super)) {
  166. self = py_getslot(self, 0);
  167. type = *(py_Type*)py_touserdata(self);
  168. }
  169. py_Ref cls_var = py_tpfindname(type, name);
  170. if(cls_var) {
  171. // handle descriptor
  172. if(py_istype(cls_var, tp_property)) {
  173. py_Ref setter = py_getslot(cls_var, 1);
  174. if(!py_isnone(setter)) {
  175. py_push(setter);
  176. py_push(self);
  177. py_push(val);
  178. return py_vectorcall(1, 0);
  179. } else {
  180. return TypeError("readonly attribute: '%n'", name);
  181. }
  182. }
  183. }
  184. // handle instance __dict__
  185. if(self->is_ptr && self->_obj->slots == -1) {
  186. py_setdict(self, name, val);
  187. return true;
  188. }
  189. return TypeError("cannot set attribute");
  190. }
  191. bool py_delattr(py_Ref self, py_Name name) {
  192. if(self->is_ptr && self->_obj->slots == -1) {
  193. if(py_deldict(self, name)) return true;
  194. return AttributeError(self, name);
  195. }
  196. return TypeError("cannot delete attribute");
  197. }
  198. bool py_getitem(py_Ref self, py_Ref key) {
  199. py_push(self);
  200. py_push(key);
  201. bool ok = pk_callmagic(__getitem__, 2, py_peek(-2));
  202. py_shrink(2);
  203. return ok;
  204. }
  205. bool py_setitem(py_Ref self, py_Ref key, py_Ref val) {
  206. py_push(self);
  207. py_push(key);
  208. py_push(val);
  209. bool ok = pk_callmagic(__setitem__, 3, py_peek(-3));
  210. py_shrink(3);
  211. return ok;
  212. }
  213. bool py_delitem(py_Ref self, py_Ref key) {
  214. py_push(self);
  215. py_push(key);
  216. bool ok = pk_callmagic(__delitem__, 2, py_peek(-2));
  217. py_shrink(2);
  218. return ok;
  219. }
  220. int py_equal(py_Ref lhs, py_Ref rhs) {
  221. if(!py_eq(lhs, rhs)) return -1;
  222. return py_bool(py_retval());
  223. }
  224. int py_less(py_Ref lhs, py_Ref rhs) {
  225. if(!py_lt(lhs, rhs)) return -1;
  226. return py_bool(py_retval());
  227. }