py_ops.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. pk_TypeInfo* types = (pk_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. pk_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. if(py_call(tmp, 1, val)) return true;
  71. return vm->is_stopiteration ? 0 : -1;
  72. }
  73. bool py_getattr(py_Ref self, py_Name name) {
  74. // https://docs.python.org/3/howto/descriptor.html#invocation-from-an-instance
  75. py_Type type = self->type;
  76. // handle super() proxy
  77. if(py_istype(self, tp_super)) {
  78. self = py_getslot(self, 0);
  79. type = *(py_Type*)py_touserdata(self);
  80. }
  81. py_Ref cls_var = py_tpfindname(type, name);
  82. if(cls_var) {
  83. // handle descriptor
  84. if(py_istype(cls_var, tp_property)) {
  85. py_Ref getter = py_getslot(cls_var, 0);
  86. return py_call(getter, 1, self);
  87. }
  88. }
  89. // handle instance __dict__
  90. if(self->is_ptr && self->_obj->slots == -1) {
  91. if(!py_istype(self, tp_type)) {
  92. py_Ref res = py_getdict(self, name);
  93. if(res) {
  94. py_assign(py_retval(), res);
  95. return true;
  96. }
  97. } else {
  98. py_Type* inner_type = py_touserdata(self);
  99. py_Ref res = py_tpfindname(*inner_type, name);
  100. if(res) {
  101. if(py_istype(res, tp_staticmethod)) {
  102. res = py_getslot(res, 0);
  103. } else if(py_istype(res, tp_classmethod)) {
  104. // TODO: make a closure
  105. assert(false);
  106. }
  107. py_assign(py_retval(), res);
  108. return true;
  109. }
  110. }
  111. }
  112. if(cls_var) {
  113. // bound method is non-data descriptor
  114. switch(cls_var->type) {
  115. case tp_function: assert(false);
  116. case tp_nativefunc: assert(false);
  117. case tp_staticmethod: assert(false);
  118. case tp_classmethod: assert(false);
  119. default: {
  120. py_assign(py_retval(), cls_var);
  121. return true;
  122. }
  123. }
  124. }
  125. return AttributeError(self, name);
  126. }
  127. bool py_setattr(py_Ref self, py_Name name, py_Ref val) {
  128. py_Type type = self->type;
  129. // handle super() proxy
  130. if(py_istype(self, tp_super)) {
  131. self = py_getslot(self, 0);
  132. type = *(py_Type*)py_touserdata(self);
  133. }
  134. py_Ref cls_var = py_tpfindname(type, name);
  135. if(cls_var) {
  136. // handle descriptor
  137. if(py_istype(cls_var, tp_property)) {
  138. py_Ref setter = py_getslot(cls_var, 1);
  139. if(!py_isnone(setter)) {
  140. py_push(setter);
  141. py_push(self);
  142. py_push(val);
  143. return py_vectorcall(1, 0);
  144. } else {
  145. return TypeError("readonly attribute: '%n'", name);
  146. }
  147. }
  148. }
  149. // handle instance __dict__
  150. if(self->is_ptr && self->_obj->slots == -1) {
  151. py_setdict(self, name, val);
  152. return true;
  153. }
  154. return TypeError("cannot set attribute");
  155. }
  156. bool py_delattr(py_Ref self, py_Name name) {
  157. if(self->is_ptr && self->_obj->slots == -1) {
  158. if(py_deldict(self, name)) return true;
  159. return AttributeError(self, name);
  160. }
  161. return TypeError("cannot delete attribute");
  162. }
  163. bool py_getitem(py_Ref self, py_Ref key) {
  164. py_push(self);
  165. py_push(key);
  166. bool ok = pk_callmagic(__getitem__, 2, py_peek(-2));
  167. py_shrink(2);
  168. return ok;
  169. }
  170. bool py_setitem(py_Ref self, py_Ref key, py_Ref val) {
  171. py_push(self);
  172. py_push(key);
  173. py_push(val);
  174. bool ok = pk_callmagic(__setitem__, 3, py_peek(-3));
  175. py_shrink(3);
  176. return ok;
  177. }
  178. bool py_delitem(py_Ref self, py_Ref key) {
  179. py_push(self);
  180. py_push(key);
  181. bool ok = pk_callmagic(__delitem__, 2, py_peek(-2));
  182. py_shrink(2);
  183. return ok;
  184. }
  185. int py_equal(py_Ref lhs, py_Ref rhs){
  186. if(!py_eq(lhs, rhs)) return -1;
  187. return py_bool(py_retval());
  188. }
  189. int py_less(py_Ref lhs, py_Ref rhs){
  190. if(!py_lt(lhs, rhs)) return -1;
  191. return py_bool(py_retval());
  192. }