#include #include "execution.hpp" #include "types.hpp" #include "stack_isolation.hpp" #include "value.hpp" namespace ui_scripting { /*************************************************************** * Constructors **************************************************************/ value::value(const game::hks::HksObject& value) : value_(value) { } value::value(const int value) { game::hks::HksObject obj{}; obj.t = game::hks::TNUMBER; obj.v.number = static_cast(value); this->value_ = obj; } value::value(const unsigned int value) { game::hks::HksObject obj{}; obj.t = game::hks::TNUMBER; obj.v.number = static_cast(value); this->value_ = obj; } value::value(const bool value) : value(static_cast(value)) { } value::value(const float value) { game::hks::HksObject obj{}; obj.t = game::hks::TNUMBER; obj.v.number = static_cast(value); this->value_ = obj; } value::value(const double value) : value(static_cast(value)) { } value::value(const char* value) { game::hks::HksObject obj{}; stack_isolation _; const auto state = *game::hks::lua_state; game::hks::hksi_lua_pushlstring(state, value, (unsigned int)strlen(value)); obj = state->m_apistack.top[-1]; this->value_ = obj; } value::value(const std::string& value) : value(value.data()) { } value::value(const lightuserdata& value) { this->value_.t = game::hks::TLIGHTUSERDATA; this->value_.v.ptr = value.ptr; } value::value(const userdata& value) { this->value_.t = game::hks::TUSERDATA; this->value_.v.ptr = value.ptr; } value::value(const table& value) { this->value_.t = game::hks::TTABLE; this->value_.v.ptr = value.ptr; } value::value(const function& value) { this->value_.t = game::hks::TIFUNCTION; this->value_.v.ptr = value.ptr; } /*************************************************************** * Integer **************************************************************/ template <> bool value::is() const { return this->get_raw().t == game::hks::TNUMBER; } template <> bool value::is() const { return this->is(); } template <> bool value::is() const { return this->is(); } template <> int value::get() const { return static_cast(this->get_raw().v.number); } template <> unsigned int value::get() const { return static_cast(this->get_raw().v.number); } template <> bool value::get() const { return this->get_raw().v.native != 0; } /*************************************************************** * Float **************************************************************/ template <> bool value::is() const { return this->get_raw().t == game::hks::TNUMBER; } template <> bool value::is() const { return this->is(); } template <> float value::get() const { return this->get_raw().v.number; } template <> double value::get() const { return static_cast(this->get_raw().v.number); } /*************************************************************** * String **************************************************************/ template <> bool value::is() const { return this->get_raw().t == game::hks::TSTRING; } template <> bool value::is() const { return this->is(); } template <> const char* value::get() const { return this->get_raw().v.str->m_data; } template <> std::string value::get() const { return this->get(); } bool value::operator==(const value& other) { if (this->get_raw().t != other.get_raw().t) { return false; } if (this->get_raw().t == game::hks::TSTRING) { return this->get() == other.get(); } return this->get_raw().v.native == other.get_raw().v.native; } /*************************************************************** * Lightuserdata **************************************************************/ template <> bool value::is() const { return this->get_raw().t == game::hks::TLIGHTUSERDATA; } template <> lightuserdata value::get() const { return this->get_raw().v.ptr; } /*************************************************************** * Userdata **************************************************************/ template <> bool value::is() const { return this->get_raw().t == game::hks::TUSERDATA; } template <> userdata value::get() const { return this->get_raw().v.ptr; } /*************************************************************** * Table **************************************************************/ template <> bool value::is() const { return this->get_raw().t == game::hks::TTABLE; } template <> table value::get() const { return this->get_raw().v.table; } /*************************************************************** * Function **************************************************************/ template <> bool value::is() const { return this->get_raw().t == game::hks::TIFUNCTION || this->get_raw().t == game::hks::TCFUNCTION; } template <> function value::get() const { return this->get_raw().v.ptr; } /*************************************************************** * **************************************************************/ const game::hks::HksObject& value::get_raw() const { return this->value_; } }