#pragma once #ifdef _MSC_VER #pragma warning (disable:4267) #pragma warning (disable:4101) #define _CRT_NONSTDC_NO_DEPRECATE #define strdup _strdup #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PK_VERSION "0.9.5" #define PK_EXTRA_CHECK 0 #if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__) #define PK_ENABLE_FILEIO 0 #else #define PK_ENABLE_FILEIO 1 #endif #if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__) typedef int32_t i64; typedef float f64; #define S_TO_INT std::stoi #define S_TO_FLOAT std::stof #else typedef int64_t i64; typedef double f64; #define S_TO_INT std::stoll #define S_TO_FLOAT std::stod #endif namespace pkpy{ namespace std = ::std; struct Dummy { }; struct DummyInstance { }; struct DummyModule { }; struct Type { int index; Type(): index(-1) {} Type(int index): index(index) {} bool operator==(Type other) const noexcept { return this->index == other.index; } bool operator!=(Type other) const noexcept { return this->index != other.index; } operator int() const noexcept { return this->index; } }; //#define THREAD_LOCAL thread_local #define THREAD_LOCAL #define CPP_LAMBDA(x) ([](VM* vm, Args& args) { return x; }) #define CPP_NOT_IMPLEMENTED() ([](VM* vm, Args& args) { vm->NotImplementedError(); return vm->None; }) #ifdef POCKETPY_H #define UNREACHABLE() throw std::runtime_error( "L" + std::to_string(__LINE__) + " UNREACHABLE()!"); #else #define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!"); #endif const float kLocalsLoadFactor = 0.67f; const float kInstAttrLoadFactor = 0.67f; const float kTypeAttrLoadFactor = 0.5f; static_assert(sizeof(i64) == sizeof(int*)); static_assert(sizeof(f64) == sizeof(int*)); static_assert(std::numeric_limits::is_iec559); static_assert(std::numeric_limits::is_iec559); struct PyObject; #define BITS(p) (reinterpret_cast(p)) inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; } inline bool is_int(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b01; } inline bool is_float(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b10; } inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept { return is_tagged(a) && is_tagged(b); } inline bool is_both_int(PyObject* a, PyObject* b) noexcept { return is_int(a) && is_int(b); } template class queue{ std::list list; public: void push(const T& t){ list.push_back(t); } void push(T&& t){ list.push_back(std::move(t)); } void pop(){ list.pop_front(); } void clear(){ list.clear(); } bool empty() const { return list.empty(); } size_t size() const { return list.size(); } T& front(){ return list.front(); } const T& front() const { return list.front(); } const std::list& data() const { return list; } }; template class stack{ std::vector vec; public: void push(const T& t){ vec.push_back(t); } void push(T&& t){ vec.push_back(std::move(t)); } void pop(){ vec.pop_back(); } void clear(){ vec.clear(); } bool empty() const { return vec.empty(); } size_t size() const { return vec.size(); } T& top(){ return vec.back(); } const T& top() const { return vec.back(); } const std::vector& data() const { return vec; } }; } // namespace pkpy