// Copyright (C) 2012-2015 ChaosForge Ltd // http://chaosforge.org/ // // This file is part of Nova libraries. // For conditions of distribution and use, see copying.txt file in root folder. #include "nv/lua/lua_values.hh" #include "nv/lua/lua_raw.hh" using nv::lua::linteger; using nv::lua::lnumber; using nv::lua::lunsigned; int nv::lua::detail::upvalue_index( int i ) { return lua_upvalueindex(i); } bool nv::lua::detail::is_userdata( lua_State *L, int index, const char* metatable ) { return luaL_testudata( L, index, metatable ) != 0; } void* nv::lua::detail::raw_check_userdata( lua_State *L, int index, const char* metatable ) { return luaL_testudata( L, index, metatable ); } void* nv::lua::detail::raw_allocate_userdata( lua_State *L, size_t size, const char* metatable ) { void* result = lua_newuserdata(L, size); luaL_setmetatable( L, metatable ); return result; } void nv::lua::detail::pop_and_discard( lua_State *L, int count ) { lua_pop( L, count ); } void nv::lua::detail::push_nil( lua_State *L ) { lua_pushnil( L ); } void nv::lua::detail::push_unsigned( lua_State *L, lunsigned v ) { lua_pushunsigned( L, v ); } void nv::lua::detail::push_integer ( lua_State *L, linteger v ) { lua_pushinteger( L, v ); } void nv::lua::detail::push_number ( lua_State *L, lnumber v ) { lua_pushnumber( L, v ); } void nv::lua::detail::push_bool ( lua_State *L, bool v ) { lua_pushboolean( L, v ); } void nv::lua::detail::push_string_view( lua_State *L, string_view s ) { lua_pushlstring( L, s.data(), s.size() ); } void nv::lua::detail::push_pointer ( lua_State *L, void* p ) { lua_pushlightuserdata( L, p ); } void nv::lua::detail::push_ref_object ( lua_State *L, ref object ) { lua_rawgeti( L, LUA_REGISTRYINDEX, object.get() ); } lunsigned nv::lua::detail::to_unsigned( lua_State *L, int index ) { return lua_tounsigned( L, index ); } linteger nv::lua::detail::to_integer ( lua_State *L, int index ) { return lua_tointeger( L, index ); } lnumber nv::lua::detail::to_number ( lua_State *L, int index ) { return lua_tonumber( L, index ); } bool nv::lua::detail::to_bool ( lua_State *L, int index ) { return lua_toboolean( L, index ) != 0; } nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index ) { size_t length = 0; const char* result = lua_tolstring( L, index, &length ); return string_view( result, length ); } void* nv::lua::detail::to_pointer ( lua_State *L, int index ) { return lua_touserdata( L, index ); } void* nv::lua::detail::to_ref_object ( lua_State *L, int index ) { void* o = nullptr; if ( lua_istable( L , index ) ) { lua_pushstring( L, "__ptr" ); lua_rawget( L, index ); if ( lua_isuserdata( L, -1 ) ) { o = lua_touserdata( L, -1 ); } lua_pop( L, 1 ); } return o; } lunsigned nv::lua::detail::to_unsigned( lua_State *L, int index, lunsigned def ) { return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tounsigned( L, index ) : def ); } linteger nv::lua::detail::to_integer ( lua_State *L, int index, linteger def ) { return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tointeger( L, index ) : def ); } lnumber nv::lua::detail::to_number ( lua_State *L, int index, lnumber def ) { return ( lua_type( L, index ) == LUA_TNUMBER ? lua_tonumber( L, index ) : def ); } bool nv::lua::detail::to_bool ( lua_State *L, int index, bool def ) { return ( lua_type( L, index ) == LUA_TBOOLEAN ? lua_toboolean( L, index ) != 0 : def ); } nv::string_view nv::lua::detail::to_string_view( lua_State *L, int index, string_view def ) { return ( lua_type( L, index ) == LUA_TSTRING ? nlua_tostringview( L, index ) : def ); } void* nv::lua::detail::to_pointer ( lua_State *L, int index, void* def ) { return ( lua_type( L, index ) == LUA_TUSERDATA ? lua_touserdata( L, index ) : def ); } void* nv::lua::detail::to_ref_object( lua_State *L, int index, void* def ) { void* o = def; if ( lua_istable( L , index ) ) { lua_pushliteral( L, "__ptr" ); lua_rawget( L, index ); if ( lua_isuserdata( L, -1 ) ) { o = lua_touserdata( L, -1 ); } lua_pop( L, 1 ); } return o; }