diff --git a/docs/source/api/table.rst b/docs/source/api/table.rst index 5aa39245..6c5c80ca 100644 --- a/docs/source/api/table.rst +++ b/docs/source/api/table.rst @@ -93,6 +93,15 @@ This function returns the size of a table. It is only well-defined in the case o table& new_usertype(const std::string& name, constructors ctor, Args&&... args); This class of functions creates a new :doc:`usertype` with the specified arguments, providing a few extra details for constructors. After creating a usertype with the specified argument, it passes it to :ref:`set_usertype`. + +.. code-block:: cpp + :caption: function: creating an enum + :name: new-enum + + template + basic_table_core& new_enum(const std::string& name, Args&&... args); + +Use this function to create an enumeration type in Lua. By default, the enum will be made read-only, which creates a tiny performance hit to make the values stored in this table behave exactly like a read-only enumeration in C++. If you plan on changing the enum values in Lua, set the ``read_only`` template parameter in your ``new_enum`` call to false. The arguments are expected to come in ``key, value, key, value, ...`` list. .. _set_usertype: diff --git a/sol/call.hpp b/sol/call.hpp index 45796c0b..9f7ef5b9 100644 --- a/sol/call.hpp +++ b/sol/call.hpp @@ -299,7 +299,7 @@ namespace sol { typedef sol::constructor_list F; static int call(lua_State* L, F&) { - static const auto& metakey = usertype_traits::metatable; + const auto& metakey = usertype_traits::metatable; int argcount = lua_gettop(L); call_syntax syntax = argcount > 0 ? stack::get_call_syntax(L, metakey, 1) : call_syntax::dot; argcount -= static_cast(syntax); @@ -331,6 +331,7 @@ namespace sol { struct onmatch { template int operator()(types, index_value, types r, types a, lua_State* L, int, int start, F& f) { + const auto& metakey = usertype_traits::metatable; T** pointerpointer = reinterpret_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); reference userdataref(L, -1); T*& referencepointer = *pointerpointer; @@ -341,7 +342,7 @@ namespace sol { stack::call_into_lua<1, false>(r, a, L, start, func, detail::implicit_wrapper(obj)); userdataref.push(); - luaL_getmetatable(L, &usertype_traits::metatable[0]); + luaL_getmetatable(L, &metakey[0]); if (type_of(L, -1) == type::nil) { lua_pop(L, 1); std::string err = "sol: unable to get usertype metatable for "; diff --git a/sol/stack_check.hpp b/sol/stack_check.hpp index fd920fc4..2e76884b 100644 --- a/sol/stack_check.hpp +++ b/sol/stack_check.hpp @@ -34,7 +34,8 @@ namespace sol { namespace stack_detail { template inline bool check_metatable(lua_State* L, int index = -2) { - luaL_getmetatable(L, &usertype_traits::metatable[0]); + const auto& metakey = usertype_traits::metatable; + luaL_getmetatable(L, &metakey[0]); const type expectedmetatabletype = static_cast(lua_type(L, -1)); if (expectedmetatabletype != type::nil) { if (lua_rawequal(L, -1, index) == 1) { @@ -272,7 +273,7 @@ namespace sol { return true; if (stack_detail::check_metatable>(L)) return true; - bool success = true; + bool success = false; #ifndef SOL_NO_EXCEPTIONS lua_getfield(L, -1, &detail::base_class_check_key()[0]); if (type_of(L, -1) != type::nil) { diff --git a/sol/stack_push.hpp b/sol/stack_push.hpp index 9a4d551a..84e7f727 100644 --- a/sol/stack_push.hpp +++ b/sol/stack_push.hpp @@ -150,8 +150,8 @@ namespace sol { template struct pusher, std::is_unsigned>::value>> { static int push(lua_State* L, const T& value) { - typedef std::make_signed_t signed_int; - return stack::push(L, static_cast(value)); + lua_pushinteger(L, static_cast(value)); + return 1; } };