values.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "pocketpy/common/str.h"
  2. #include "pocketpy/common/vector.h"
  3. #include "pocketpy/pocketpy.h"
  4. #include "pocketpy/common/utils.h"
  5. #include "pocketpy/objects/object.h"
  6. #include "pocketpy/interpreter/vm.h"
  7. void py_newint(py_Ref out, int64_t val) {
  8. out->type = tp_int;
  9. out->is_ptr = false;
  10. out->_i64 = val;
  11. }
  12. void py_newfloat(py_Ref out, double val) {
  13. out->type = tp_float;
  14. out->is_ptr = false;
  15. out->_f64 = val;
  16. }
  17. void py_newbool(py_Ref out, bool val) {
  18. out->type = tp_bool;
  19. out->is_ptr = false;
  20. out->_bool = val;
  21. }
  22. void py_newnone(py_Ref out) {
  23. out->type = tp_none_type;
  24. out->is_ptr = false;
  25. }
  26. void py_newnotimplemented(py_Ref out) {
  27. out->type = tp_not_implemented_type;
  28. out->is_ptr = false;
  29. }
  30. void py_newellipsis(py_Ref out) {
  31. out->type = tp_ellipsis;
  32. out->is_ptr = false;
  33. }
  34. void py_newnil(py_Ref out) { out->type = 0; }
  35. void py_newfunction(py_Ref out, py_CFunction f, const char* sig) {
  36. py_newfunction2(out, f, sig, BindType_FUNCTION, NULL, NULL);
  37. }
  38. void py_newfunction2(py_Ref out,
  39. py_CFunction f,
  40. const char* sig,
  41. BindType bt,
  42. const char* docstring,
  43. const py_Ref upvalue) {}
  44. void py_newnativefunc(py_Ref out, py_CFunction f) {
  45. out->type = tp_nativefunc;
  46. out->is_ptr = false;
  47. out->_cfunc = f;
  48. }
  49. void py_bindmethod(py_Type type, const char *name, py_CFunction f){
  50. py_bindmethod2(type, name, f, BindType_FUNCTION);
  51. }
  52. void py_bindmethod2(py_Type type, const char *name, py_CFunction f, BindType bt){
  53. py_TValue tmp;
  54. py_newnativefunc(&tmp, f);
  55. py_setdict(py_tpobject(type), py_name(name), &tmp);
  56. }
  57. void py_bindnativefunc(py_Ref obj, const char *name, py_CFunction f){
  58. py_TValue tmp;
  59. py_newnativefunc(&tmp, f);
  60. py_setdict(obj, py_name(name), &tmp);
  61. }
  62. void py_newslice(py_Ref out, const py_Ref start, const py_Ref stop, const py_Ref step) {
  63. py_newobject(out, tp_slice, 3, 0);
  64. py_setslot(out, 0, start);
  65. py_setslot(out, 1, stop);
  66. py_setslot(out, 2, step);
  67. }
  68. void py_newobject(py_Ref out, py_Type type, int slots, int udsize) {
  69. pk_ManagedHeap* heap = &pk_current_vm->heap;
  70. PyObject* obj = pk_ManagedHeap__gcnew(heap, type, slots, udsize);
  71. out->type = type;
  72. out->is_ptr = true;
  73. out->_obj = obj;
  74. }