common.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include <cmath>
  3. #include <cstring>
  4. #include <sstream>
  5. #include <regex>
  6. #include <stdexcept>
  7. #include <vector>
  8. #include <string>
  9. #include <chrono>
  10. #include <string_view>
  11. #include <iomanip>
  12. #include <memory>
  13. #include <iostream>
  14. #include <map>
  15. #include <set>
  16. #include <algorithm>
  17. #include <initializer_list>
  18. #include <variant>
  19. #include <type_traits>
  20. #define PK_VERSION "1.0.5"
  21. #include "config.h"
  22. /*******************************************************************************/
  23. #if PK_ENABLE_STD_FUNCTION
  24. #include <functional>
  25. #endif
  26. /*******************************************************************************/
  27. #if PK_ENABLE_THREAD
  28. #define THREAD_LOCAL thread_local
  29. #include <mutex>
  30. struct GIL {
  31. inline static std::mutex _mutex;
  32. explicit GIL() { _mutex.lock(); }
  33. ~GIL() { _mutex.unlock(); }
  34. };
  35. #define PK_GLOBAL_SCOPE_LOCK() auto _lock = GIL();
  36. #else
  37. #define THREAD_LOCAL
  38. #define PK_GLOBAL_SCOPE_LOCK()
  39. #endif
  40. /*******************************************************************************/
  41. #define PK_UNUSED(x) (void)(x)
  42. namespace pkpy{
  43. namespace std = ::std;
  44. template <size_t T>
  45. struct NumberTraits;
  46. template <>
  47. struct NumberTraits<4> {
  48. using int_t = int32_t;
  49. using float_t = float;
  50. template<typename... Args>
  51. static int_t stoi(Args&&... args) { return std::stoi(std::forward<Args>(args)...); }
  52. template<typename... Args>
  53. static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
  54. static constexpr int_t c0 = 0b00000000011111111111111111111100;
  55. static constexpr int_t c1 = 0b11111111111111111111111111111100;
  56. static constexpr int_t c2 = 0b00000000000000000000000000000011;
  57. };
  58. template <>
  59. struct NumberTraits<8> {
  60. using int_t = int64_t;
  61. using float_t = double;
  62. template<typename... Args>
  63. static int_t stoi(Args&&... args) { return std::stoll(std::forward<Args>(args)...); }
  64. template<typename... Args>
  65. static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
  66. static constexpr int_t c0 = 0b0000000000001111111111111111111111111111111111111111111111111100;
  67. static constexpr int_t c1 = 0b1111111111111111111111111111111111111111111111111111111111111100;
  68. static constexpr int_t c2 = 0b0000000000000000000000000000000000000000000000000000000000000011;
  69. };
  70. using Number = NumberTraits<sizeof(void*)>;
  71. using i64 = Number::int_t;
  72. using f64 = Number::float_t;
  73. static_assert(sizeof(i64) == sizeof(void*));
  74. static_assert(sizeof(f64) == sizeof(void*));
  75. static_assert(std::numeric_limits<f64>::is_iec559);
  76. struct Dummy { };
  77. struct DummyInstance { };
  78. struct DummyModule { };
  79. struct NoReturn { };
  80. struct Discarded { };
  81. struct Type {
  82. int index;
  83. Type(): index(-1) {}
  84. Type(int index): index(index) {}
  85. bool operator==(Type other) const noexcept { return this->index == other.index; }
  86. bool operator!=(Type other) const noexcept { return this->index != other.index; }
  87. operator int() const noexcept { return this->index; }
  88. };
  89. #define PK_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  90. #define PK_VAR_LAMBDA(x) ([](VM* vm, ArgsView args) { return VAR(x); })
  91. #define PK_NONE_LAMBDA(x) ([](VM* vm, ArgsView args) { x; return vm->None; })
  92. #ifdef POCKETPY_H
  93. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  94. #else
  95. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  96. #endif
  97. #define PK_ASSERT(x) if(!(x)) FATAL_ERROR();
  98. struct PyObject;
  99. #define PK_BITS(p) (reinterpret_cast<i64>(p))
  100. inline bool is_tagged(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) != 0b00; }
  101. inline bool is_int(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b01; }
  102. inline bool is_float(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b10; }
  103. inline bool is_special(PyObject* p) noexcept { return (PK_BITS(p) & 0b11) == 0b11; }
  104. inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept {
  105. return is_tagged(a) && is_tagged(b);
  106. }
  107. inline bool is_both_int(PyObject* a, PyObject* b) noexcept {
  108. return is_int(a) && is_int(b);
  109. }
  110. inline bool is_both_float(PyObject* a, PyObject* b) noexcept {
  111. return is_float(a) && is_float(b);
  112. }
  113. // special singals, is_tagged() for them is true
  114. inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null
  115. inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
  116. inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
  117. #define ADD_MODULE_PLACEHOLDER(name) namespace pkpy { inline void add_module_##name(void* vm) { (void)vm; } }
  118. } // namespace pkpy