py_object.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "pocketpy/interpreter/vm.h"
  2. #include "pocketpy/common/sstream.h"
  3. #include "pocketpy/pocketpy.h"
  4. static bool object__new__(int argc, py_Ref argv) {
  5. if(argc == 0) return TypeError("object.__new__(): not enough arguments");
  6. py_Type cls = py_totype(py_arg(0));
  7. py_TypeInfo* ti = c11__at(py_TypeInfo, &pk_current_vm->types, cls);
  8. if(!ti->is_python) {
  9. return TypeError("object.__new__(%t) is not safe, use %t.__new__()", cls, cls);
  10. }
  11. py_newobject(py_retval(), cls, -1, 0);
  12. return true;
  13. }
  14. static bool object__hash__(int argc, py_Ref argv) {
  15. PY_CHECK_ARGC(1);
  16. assert(argv->is_ptr);
  17. py_newint(py_retval(), (py_i64)argv->_obj);
  18. return true;
  19. }
  20. static bool object__eq__(int argc, py_Ref argv) {
  21. PY_CHECK_ARGC(2);
  22. bool res = py_isidentical(py_arg(0), py_arg(1));
  23. py_newbool(py_retval(), res);
  24. return true;
  25. }
  26. static bool object__ne__(int argc, py_Ref argv) {
  27. PY_CHECK_ARGC(2);
  28. bool res = py_isidentical(py_arg(0), py_arg(1));
  29. py_newbool(py_retval(), !res);
  30. return true;
  31. }
  32. static bool object__repr__(int argc, py_Ref argv) {
  33. PY_CHECK_ARGC(1);
  34. assert(argv->is_ptr);
  35. c11_sbuf buf;
  36. c11_sbuf__ctor(&buf);
  37. pk_sprintf(&buf, "<%t object at %p>", argv->type, argv->_obj);
  38. c11_sbuf__py_submit(&buf, py_retval());
  39. return true;
  40. }
  41. static bool object__dict__getter(int argc, py_Ref argv) {
  42. PY_CHECK_ARGC(1);
  43. if(argv->is_ptr && argv->_obj->slots == -1) {
  44. pk_mappingproxy__namedict(py_retval(), argv);
  45. } else {
  46. py_newnone(py_retval());
  47. }
  48. return true;
  49. }
  50. static bool type__repr__(int argc, py_Ref argv) {
  51. PY_CHECK_ARGC(1);
  52. c11_sbuf buf;
  53. c11_sbuf__ctor(&buf);
  54. pk_sprintf(&buf, "<class '%t'>", py_totype(argv));
  55. c11_sbuf__py_submit(&buf, py_retval());
  56. return true;
  57. }
  58. static bool type__new__(int argc, py_Ref argv) {
  59. PY_CHECK_ARGC(2);
  60. py_Type type = py_typeof(py_arg(1));
  61. py_assign(py_retval(), py_tpobject(type));
  62. return true;
  63. }
  64. static bool type__base__getter(int argc, py_Ref argv) {
  65. PY_CHECK_ARGC(1);
  66. py_Type type = py_totype(argv);
  67. py_TypeInfo* ti = c11__at(py_TypeInfo, &pk_current_vm->types, type);
  68. if(ti->base) {
  69. py_assign(py_retval(), py_tpobject(ti->base));
  70. } else {
  71. py_newnone(py_retval());
  72. }
  73. return true;
  74. }
  75. static bool type__name__getter(int argc, py_Ref argv) {
  76. PY_CHECK_ARGC(1);
  77. py_Type type = py_totype(argv);
  78. py_TypeInfo* ti = c11__at(py_TypeInfo, &pk_current_vm->types, type);
  79. py_newstr(py_retval(), py_name2str(ti->name));
  80. return true;
  81. }
  82. static bool type__getitem__(int argc, py_Ref argv) {
  83. py_assign(py_retval(), argv);
  84. return true;
  85. }
  86. static bool type__module__getter(int argc, py_Ref argv) {
  87. PY_CHECK_ARGC(1);
  88. py_Type type = py_totype(argv);
  89. py_TypeInfo* ti = c11__at(py_TypeInfo, &pk_current_vm->types, type);
  90. if(py_isnil(&ti->module)) {
  91. py_newnone(py_retval());
  92. } else {
  93. py_Ref path = py_getdict(&ti->module, __path__);
  94. py_assign(py_retval(), path);
  95. }
  96. return true;
  97. }
  98. void pk_object__register() {
  99. // TODO: use staticmethod
  100. py_bindmagic(tp_object, __new__, object__new__);
  101. py_bindmagic(tp_object, __hash__, object__hash__);
  102. py_bindmagic(tp_object, __eq__, object__eq__);
  103. py_bindmagic(tp_object, __ne__, object__ne__);
  104. py_bindmagic(tp_object, __repr__, object__repr__);
  105. py_bindproperty(tp_object, "__dict__", object__dict__getter, NULL);
  106. py_bindmagic(tp_type, __repr__, type__repr__);
  107. py_bindmagic(tp_type, __new__, type__new__);
  108. py_bindmagic(tp_type, __getitem__, type__getitem__);
  109. py_bindproperty(tp_type, "__module__", type__module__getter, NULL);
  110. py_bindproperty(tp_type, "__base__", type__base__getter, NULL);
  111. py_bindproperty(tp_type, "__name__", type__name__getter, NULL);
  112. }