bytecode.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // sol2
  2. // The MIT License (MIT)
  3. // Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. // this software and associated documentation files (the "Software"), to deal in
  6. // the Software without restriction, including without limitation the rights to
  7. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. // the Software, and to permit persons to whom the Software is furnished to do so,
  9. // subject to the following conditions:
  10. // The above copyright notice and this permission notice shall be included in all
  11. // copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. #ifndef SOL_BYTECODE_HPP
  19. #define SOL_BYTECODE_HPP
  20. #include <sol/compatibility.hpp>
  21. #include <sol/string_view.hpp>
  22. #include <vector>
  23. #include <cstdint>
  24. #include <cstddef>
  25. namespace sol {
  26. template <typename Allocator = std::allocator<std::byte>>
  27. class basic_bytecode : private std::vector<std::byte, Allocator> {
  28. private:
  29. using base_t = std::vector<std::byte, Allocator>;
  30. public:
  31. using typename base_t::allocator_type;
  32. using typename base_t::const_iterator;
  33. using typename base_t::const_pointer;
  34. using typename base_t::const_reference;
  35. using typename base_t::const_reverse_iterator;
  36. using typename base_t::difference_type;
  37. using typename base_t::iterator;
  38. using typename base_t::pointer;
  39. using typename base_t::reference;
  40. using typename base_t::reverse_iterator;
  41. using typename base_t::size_type;
  42. using typename base_t::value_type;
  43. using base_t::base_t;
  44. using base_t::operator=;
  45. using base_t::data;
  46. using base_t::empty;
  47. using base_t::max_size;
  48. using base_t::size;
  49. using base_t::at;
  50. using base_t::operator[];
  51. using base_t::back;
  52. using base_t::front;
  53. using base_t::begin;
  54. using base_t::cbegin;
  55. using base_t::cend;
  56. using base_t::end;
  57. using base_t::crbegin;
  58. using base_t::crend;
  59. using base_t::rbegin;
  60. using base_t::rend;
  61. using base_t::get_allocator;
  62. using base_t::swap;
  63. using base_t::clear;
  64. using base_t::emplace;
  65. using base_t::emplace_back;
  66. using base_t::erase;
  67. using base_t::insert;
  68. using base_t::pop_back;
  69. using base_t::push_back;
  70. using base_t::reserve;
  71. using base_t::resize;
  72. using base_t::shrink_to_fit;
  73. string_view as_string_view() const {
  74. return string_view(reinterpret_cast<const char*>(this->data()), this->size());
  75. }
  76. };
  77. template <typename Container>
  78. inline int basic_insert_dump_writer(lua_State*, const void* memory, size_t memory_size, void* userdata_pointer) {
  79. using storage_t = Container;
  80. const std::byte* p_code = static_cast<const std::byte*>(memory);
  81. storage_t& bc = *static_cast<storage_t*>(userdata_pointer);
  82. #if SOL_IS_OFF(SOL_EXCEPTIONS)
  83. bc.insert(bc.cend(), p_code, p_code + memory_size);
  84. #else
  85. try {
  86. bc.insert(bc.cend(), p_code, p_code + memory_size);
  87. }
  88. catch (...) {
  89. return -1;
  90. }
  91. #endif
  92. return 0;
  93. }
  94. using bytecode = basic_bytecode<>;
  95. constexpr inline auto bytecode_dump_writer = &basic_insert_dump_writer<bytecode>;
  96. } // namespace sol
  97. #endif // SOL_BYTECODE_HPP