/* Copyright 2015-2018 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. * * In no event and under no legal theory, whether in tort (including negligence), * contract, or otherwise, unless required by applicable law (such as deliberate * and grossly negligent acts) or agreed to in writing, shall any Contributor be * liable for any damages, including any direct, indirect, special, incidental, * or consequential damages of any character arising as a result of this License or * out of the use or inability to use the software (including but not limited to damages * for loss of goodwill, work stoppage, computer failure or malfunction, or any and * all other commercial damages or losses), even if such Contributor has been advised * of the possibility of such damages. */ #include "pch.h" #include "LuaBindings.h" #include "GraphicsAccessories.h" using namespace std; namespace Diligent { template Type ReadIntValueFromLua( lua_State *L, int Index ) { CheckType( L, Index, LUA_TNUMBER ); int isnum; auto Val = lua_tonumberx( L, Index, &isnum ); if( !isnum ) { auto Str = lua_tostring( L, Index ); SCRIPT_PARSING_ERROR( L, "Failed to convert parameter ", Str, " to int" ); } if( static_cast(Val) != Val ) { SCRIPT_PARSING_ERROR( L, "Parameter value (", Val, ") is not integer. Truncating to int" ); } return static_cast(Val); } template Type ReadFPValueFromLua( lua_State *L, int Index ) { CheckType( L, Index, LUA_TNUMBER ); int isnum; auto Val = lua_tonumberx( L, Index, &isnum ); if( !isnum ) { auto Str = lua_tostring( L, Index ); SCRIPT_PARSING_ERROR( L, "Failed to convert parameter ", Str, " to floating point" ); } return static_cast(Val); } template<> int ReadValueFromLua( lua_State *L, int Index ) { return ReadIntValueFromLua( L, Index ); } template<> double ReadValueFromLua( lua_State *L, int Index ) { return ReadFPValueFromLua( L, Index ); } template<> float ReadValueFromLua( lua_State *L, int Index ) { return ReadFPValueFromLua( L, Index ); } template<> String ReadValueFromLua( lua_State *L, int Index ) { CheckType( L, Index, LUA_TSTRING ); auto Str = lua_tostring( L, Index ); return String( Str ); } template<> const Char* ReadValueFromLua( lua_State *L, int Index ) { CheckType( L, Index, LUA_TSTRING ); auto Str = lua_tostring( L, Index ); return Str; } template<> Bool ReadValueFromLua( lua_State *L, int Index ) { CheckType( L, Index, LUA_TBOOLEAN ); auto Val = lua_toboolean( L, Index ); return Val ? True : False; } template<> Uint32 ReadValueFromLua( lua_State *L, int Index ) { return ReadIntValueFromLua( L, Index ); } template<> Uint8 ReadValueFromLua( lua_State *L, int Index ) { return ReadIntValueFromLua( L, Index ); } template<> void PushValue( lua_State *L, double Val ) { lua_pushnumber( L, Val ); } template<> void PushValue( lua_State *L, const float& Val ) { lua_pushnumber( L, Val ); } template<> void PushValue( lua_State *L, const Int32& Val ) { lua_pushnumber( L, Val ); } template<> void PushValue( lua_State *L, const Uint8& Val ) { lua_pushnumber( L, Val ); } template<> void PushValue( lua_State *L, const Uint32& Val ) { lua_pushnumber( L, Val ); } template<> void PushValue( lua_State *L, const Char* Val ) { lua_pushstring( L, Val ); } template<> void PushValue( lua_State *L, const Char* const& Val ) { lua_pushstring( L, Val ); } template<> void PushValue( lua_State *L, const String& Val ) { lua_pushstring( L, Val.c_str() ); } template<> void PushValue( lua_State *L, bool Val ) { lua_pushboolean( L, Val ); } template<> void PushValue( lua_State *L, const bool &Val ) { lua_pushboolean( L, Val ); } template void ParseNumericArray( lua_State *L, int StackIndex, std::vector< Uint8 >& RawData ) { typedef typename VALUE_TYPE2CType::CType ElemType; CheckType( L, StackIndex, LUA_TTABLE ); auto ArraySize = lua_rawlen( L, StackIndex ); auto ElemSize = sizeof( ElemType ); RawData.reserve( ArraySize * ElemSize ); ParseLuaArray( L, StackIndex, &RawData, [ &]( void* pBasePointer, int StackIndex, int NewArrayIndex ) { VERIFY( pBasePointer == &RawData, "Sanity check failed" ); auto CurrIndex = RawData.size() / ElemSize; if(static_cast(CurrIndex) != NewArrayIndex - 1 ) SCRIPT_PARSING_ERROR( L, "Explicit array indices are not allowed in array initialization. Provided index ", NewArrayIndex - 1, " conflicts with actual index ", CurrIndex, "." ); RawData.resize( (CurrIndex + 1) * ElemSize ); auto CurrValue = ReadValueFromLua( L, StackIndex ); reinterpret_cast(RawData[CurrIndex * ElemSize]) = static_cast(CurrValue); } ); } NumericArrayLoader::NumericArrayLoader() : m_ValueTypeBinder( 0, "VALUE_TYPE", m_ValueTypeEnumMapping ) { DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_INT8 ); DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_INT16 ); DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_INT32 ); DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_UINT8 ); DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_UINT16 ); DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_UINT32 ); DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_FLOAT16 ); DEFINE_ENUM_ELEMENT_MAPPING( m_ValueTypeEnumMapping, VT_FLOAT32 ); VERIFY( m_ValueTypeEnumMapping.m_Str2ValMap.size() == VT_NUM_TYPES - 1, "Unexpected map size. Did you update VALUE_TYPE enum?" ); VERIFY( m_ValueTypeEnumMapping.m_Val2StrMap.size() == VT_NUM_TYPES - 1, "Unexpected map size. Did you update VALUE_TYPE enum?" ); m_ParseFuncJumpTbl.insert( make_pair( VT_INT8, ParseNumericArray ) ); m_ParseFuncJumpTbl.insert( make_pair( VT_INT16, ParseNumericArray ) ); m_ParseFuncJumpTbl.insert( make_pair( VT_INT32, ParseNumericArray ) ); m_ParseFuncJumpTbl.insert( make_pair( VT_UINT8, ParseNumericArray ) ); m_ParseFuncJumpTbl.insert( make_pair( VT_UINT16, ParseNumericArray ) ); m_ParseFuncJumpTbl.insert( make_pair( VT_UINT32, ParseNumericArray ) ); //m_ParseFuncJumpTbl.insert( make_pair( VT_FLOAT16, ParseNumericArray ) ); m_ParseFuncJumpTbl.insert( make_pair( VT_FLOAT32, ParseNumericArray ) ); }; void NumericArrayLoader::LoadArray( lua_State *L, int StackIndex, std::vector< Uint8 >& RawData ) { VALUE_TYPE ValueType; m_ValueTypeBinder.SetValue( L, StackIndex-1, &ValueType ); auto it = m_ParseFuncJumpTbl.find( ValueType ); if( it != m_ParseFuncJumpTbl.end() ) { it->second( L, StackIndex, RawData ); } else { SCRIPT_PARSING_ERROR( L, "No method to parse array of value VALUE_TYPE==", ValueType); } } // Special version of luaL_testudata() which takes an array of allowed metatables void *luaL_testudata( lua_State *L, int ud, const std::vector &MetatableNames ) { void *p = lua_touserdata( L, ud ); if( p != nullptr ) { // value is a userdata? if( lua_getmetatable( L, ud ) ) // -0 | +(0|1) -> +(0|1) { // does it have a metatable? bool bMatchingMTFound = false; for( auto mtname = MetatableNames.begin(); mtname != MetatableNames.end() && !bMatchingMTFound; ++mtname ) { // get correct metatable luaL_getmetatable( L, mtname->c_str() ); // -0 | +1 -> +1 if( lua_rawequal( L, -1, -2 ) ) // are the same? bMatchingMTFound = true; // pop correct metatable lua_pop( L, 1 ); // -1 | +0 -> -1 } // pop user metatable lua_pop( L, 1 ); // -1 | +0 -> -1 return bMatchingMTFound ? p : nullptr; } } return nullptr; // value is not a userdata with a metatable } }