values.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "pocketpy/pocketpy.h"
  2. #include "pocketpy/common/utils.h"
  3. #include "pocketpy/objects/object.h"
  4. #include "pocketpy/interpreter/vm.h"
  5. void py_newint(py_Ref out, int64_t val) {
  6. out->type = tp_int;
  7. out->is_ptr = false;
  8. out->_i64 = val;
  9. }
  10. void py_newfloat(py_Ref out, double val) {
  11. out->type = tp_float;
  12. out->is_ptr = false;
  13. out->_f64 = val;
  14. }
  15. void py_newbool(py_Ref out, bool val) {
  16. out->type = tp_bool;
  17. out->is_ptr = false;
  18. out->_bool = val;
  19. }
  20. void py_newnone(py_Ref out) {
  21. out->type = tp_none_type;
  22. out->is_ptr = false;
  23. }
  24. void py_newnotimplemented(py_Ref out) {
  25. out->type = tp_not_implemented_type;
  26. out->is_ptr = false;
  27. }
  28. void py_newellipsis(py_Ref out) {
  29. out->type = tp_ellipsis;
  30. out->is_ptr = false;
  31. }
  32. void py_newnull(py_Ref out) { out->type = 0; }
  33. void py_newstr(py_Ref out, const char* data) {
  34. pk_ManagedHeap* heap = &pk_current_vm->heap;
  35. PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str));
  36. py_Str__ctor(PyObject__value(obj), data);
  37. out->type = tp_str;
  38. out->is_ptr = true;
  39. out->_obj = obj;
  40. }
  41. void py_newstrn(py_Ref out, const char* data, int size) {
  42. pk_ManagedHeap* heap = &pk_current_vm->heap;
  43. PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str));
  44. py_Str__ctor2((py_Str*)PyObject__value(obj), data, size);
  45. out->type = tp_str;
  46. out->is_ptr = true;
  47. out->_obj = obj;
  48. }
  49. void py_newStr_(py_Ref out, py_Str input) {
  50. pk_ManagedHeap* heap = &pk_current_vm->heap;
  51. PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_str, 0, sizeof(py_Str));
  52. py_Str* userdata = PyObject__value(obj);
  53. *userdata = input;
  54. out->type = tp_str;
  55. out->is_ptr = true;
  56. out->_obj = obj;
  57. }
  58. void py_newbytes(py_Ref out, const unsigned char* data, int size) {
  59. pk_ManagedHeap* heap = &pk_current_vm->heap;
  60. // 4 bytes size + data
  61. PyObject* obj = pk_ManagedHeap__gcnew(heap, tp_bytes, 0, sizeof(int) + size);
  62. int* psize = (int*)PyObject__value(obj);
  63. *psize = size;
  64. memcpy(psize + 1, data, size);
  65. out->type = tp_bytes;
  66. out->is_ptr = true;
  67. out->_obj = obj;
  68. }
  69. void py_newfunction(py_Ref out, py_CFunction f, const char* sig) {
  70. py_newfunction2(out, f, sig, BindType_FUNCTION, NULL, NULL);
  71. }
  72. void py_newfunction2(py_Ref out,
  73. py_CFunction f,
  74. const char* sig,
  75. BindType bt,
  76. const char* docstring,
  77. const py_Ref upvalue) {}
  78. void py_newnativefunc(py_Ref out, py_CFunction f) {
  79. out->type = tp_nativefunc;
  80. out->is_ptr = false;
  81. out->_cfunc = f;
  82. }
  83. void py_bindmethod(py_Type type, const char *name, py_CFunction f){
  84. py_bindmethod2(type, name, f, BindType_FUNCTION);
  85. }
  86. void py_bindmethod2(py_Type type, const char *name, py_CFunction f, BindType bt){
  87. py_TValue tmp;
  88. py_newnativefunc(&tmp, f);
  89. py_setdict(py_tpobject(type), py_name(name), &tmp);
  90. }
  91. void py_bindnativefunc(py_Ref obj, const char *name, py_CFunction f){
  92. py_TValue tmp;
  93. py_newnativefunc(&tmp, f);
  94. py_setdict(obj, py_name(name), &tmp);
  95. }
  96. void py_newslice(py_Ref out, const py_Ref start, const py_Ref stop, const py_Ref step) {
  97. py_newobject(out, tp_slice, 3, 0);
  98. py_setslot(out, 0, start);
  99. py_setslot(out, 1, stop);
  100. py_setslot(out, 2, step);
  101. }
  102. void py_newobject(py_Ref out, py_Type type, int slots, int udsize) {
  103. pk_ManagedHeap* heap = &pk_current_vm->heap;
  104. PyObject* obj = pk_ManagedHeap__gcnew(heap, type, slots, udsize);
  105. out->type = type;
  106. out->is_ptr = true;
  107. out->_obj = obj;
  108. }