py_object.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "pocketpy/interpreter/vm.h"
  2. #include "pocketpy/common/sstream.h"
  3. #include "pocketpy/pocketpy.h"
  4. bool pk__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 = pk__type_info(cls);
  8. if(!ti->is_python) {
  9. return TypeError("object.__new__(%t) is not safe, use %t.__new__() instead", 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(), (intptr_t)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__(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__(int argc, py_Ref argv) {
  65. PY_CHECK_ARGC(1);
  66. py_TypeInfo* ti = pk__type_info(py_totype(argv));
  67. if(ti->base) {
  68. py_assign(py_retval(), &ti->base_ti->self);
  69. } else {
  70. py_newnone(py_retval());
  71. }
  72. return true;
  73. }
  74. static bool type__name__(int argc, py_Ref argv) {
  75. PY_CHECK_ARGC(1);
  76. py_TypeInfo* ti = pk__type_info(py_totype(argv));
  77. py_assign(py_retval(), py_name2ref(ti->name));
  78. return true;
  79. }
  80. static bool type__getitem__(int argc, py_Ref argv) {
  81. py_assign(py_retval(), argv);
  82. return true;
  83. }
  84. static bool type__or__(int argc, py_Ref argv) {
  85. py_assign(py_retval(), argv);
  86. return true;
  87. }
  88. static bool type__module__(int argc, py_Ref argv) {
  89. PY_CHECK_ARGC(1);
  90. py_TypeInfo* ti = pk__type_info(py_totype(argv));
  91. if(py_isnil(&ti->module)) {
  92. py_newnone(py_retval());
  93. } else {
  94. py_Ref path = py_getdict(&ti->module, __path__);
  95. py_assign(py_retval(), path);
  96. }
  97. return true;
  98. }
  99. static bool type__annotations__(int argc, py_Ref argv) {
  100. PY_CHECK_ARGC(1);
  101. py_TypeInfo* ti = pk__type_info(py_totype(argv));
  102. if(py_isnil(&ti->annotations)) {
  103. py_newdict(py_retval());
  104. } else {
  105. py_assign(py_retval(), &ti->annotations);
  106. }
  107. return true;
  108. }
  109. void pk_object__register() {
  110. py_bindmagic(tp_object, __new__, pk__object_new);
  111. py_bindmagic(tp_object, __hash__, object__hash__);
  112. py_bindmagic(tp_object, __eq__, object__eq__);
  113. py_bindmagic(tp_object, __ne__, object__ne__);
  114. py_bindmagic(tp_object, __repr__, object__repr__);
  115. py_bindmagic(tp_type, __repr__, type__repr__);
  116. py_bindmagic(tp_type, __new__, type__new__);
  117. py_bindmagic(tp_type, __getitem__, type__getitem__);
  118. py_bindmagic(tp_type, __or__, type__or__);
  119. py_bindproperty(tp_type, "__module__", type__module__, NULL);
  120. py_bindproperty(tp_type, "__base__", type__base__, NULL);
  121. py_bindproperty(tp_type, "__name__", type__name__, NULL);
  122. py_bindproperty(tp_object, "__dict__", object__dict__, NULL);
  123. py_bindproperty(tp_type, "__annotations__", type__annotations__, NULL);
  124. }