common.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.3"
  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 GLOBAL_SCOPE_LOCK() auto _lock = GIL();
  36. #else
  37. #define THREAD_LOCAL
  38. #define GLOBAL_SCOPE_LOCK()
  39. #endif
  40. /*******************************************************************************/
  41. namespace pkpy{
  42. namespace std = ::std;
  43. template <size_t T>
  44. struct NumberTraits;
  45. template <>
  46. struct NumberTraits<4> {
  47. using int_t = int32_t;
  48. using float_t = float;
  49. template<typename... Args>
  50. static int_t stoi(Args&&... args) { return std::stoi(std::forward<Args>(args)...); }
  51. template<typename... Args>
  52. static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
  53. static constexpr int_t c0 = 0b00000000011111111111111111111100;
  54. static constexpr int_t c1 = 0b11111111111111111111111111111100;
  55. static constexpr int_t c2 = 0b00000000000000000000000000000011;
  56. };
  57. template <>
  58. struct NumberTraits<8> {
  59. using int_t = int64_t;
  60. using float_t = double;
  61. template<typename... Args>
  62. static int_t stoi(Args&&... args) { return std::stoll(std::forward<Args>(args)...); }
  63. template<typename... Args>
  64. static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
  65. static constexpr int_t c0 = 0b0000000000001111111111111111111111111111111111111111111111111100;
  66. static constexpr int_t c1 = 0b1111111111111111111111111111111111111111111111111111111111111100;
  67. static constexpr int_t c2 = 0b0000000000000000000000000000000000000000000000000000000000000011;
  68. };
  69. using Number = NumberTraits<sizeof(void*)>;
  70. using i64 = Number::int_t;
  71. using f64 = Number::float_t;
  72. static_assert(sizeof(i64) == sizeof(void*));
  73. static_assert(sizeof(f64) == sizeof(void*));
  74. static_assert(std::numeric_limits<f64>::is_iec559);
  75. struct Dummy { };
  76. struct DummyInstance { };
  77. struct DummyModule { };
  78. struct NoReturn { };
  79. struct Discarded { };
  80. struct Type {
  81. int index;
  82. Type(): index(-1) {}
  83. Type(int index): index(index) {}
  84. bool operator==(Type other) const noexcept { return this->index == other.index; }
  85. bool operator!=(Type other) const noexcept { return this->index != other.index; }
  86. operator int() const noexcept { return this->index; }
  87. };
  88. #define CPP_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  89. #ifdef POCKETPY_H
  90. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  91. #else
  92. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  93. #endif
  94. #define PK_ASSERT(x) if(!(x)) FATAL_ERROR();
  95. struct PyObject;
  96. #define BITS(p) (reinterpret_cast<i64>(p))
  97. inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; }
  98. inline bool is_int(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b01; }
  99. inline bool is_float(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b10; }
  100. inline bool is_special(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b11; }
  101. inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept {
  102. return is_tagged(a) && is_tagged(b);
  103. }
  104. inline bool is_both_int(PyObject* a, PyObject* b) noexcept {
  105. return is_int(a) && is_int(b);
  106. }
  107. // special singals, is_tagged() for them is true
  108. inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null
  109. inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
  110. inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
  111. #define ADD_MODULE_PLACEHOLDER(name) namespace pkpy { inline void add_module_##name(void* vm) { (void)vm; } }
  112. } // namespace pkpy