LUABIND_API void set_package_preload( lua_State * L, char const* modulename, object const& loader) { loader.push(L); lua_pushcclosure(L, &proxy_loader, 1); object const proxy_ldr(from_stack(L, -1)); lua_pop(L, 1); // pop do_load. lua_pushcfunction(L, &do_set_package_preload); // Must use object for correct popping in case of errors: object do_set(from_stack(L, -1)); lua_pop(L, 1); do_set(modulename, proxy_ldr); }
class_info get_class_info(const object& o) { lua_State* L = o.interpreter(); class_info result; o.push(L); detail::object_rep* obj = static_cast<detail::object_rep*>(lua_touserdata(L, -1)); lua_pop(L, 1); result.name = obj->crep()->name(); obj->crep()->get_table(L); object methods(from_stack(L, -1)); methods.swap(result.methods); lua_pop(L, 1); result.attributes = newtable(L); typedef detail::class_rep::property_map map_type; std::size_t index = 1; for (map_type::const_iterator i = obj->crep()->properties().begin(); i != obj->crep()->properties().end(); ++i, ++index) { result.attributes[index] = i->first; } return result; }
//! convert from Lua to C++ boost::array<T, N> from(lua_State* L, int index) { boost::array<T, N> v; object table(from_stack(L, index)); for (std::size_t i = 0; i < v.size(); ++i) { v[i] = object_cast<T>(table[i + 1]); } return v; }
//! convert from Lua to C++ boost::multi_array<T, 1> from(lua_State* L, int index) { std::size_t size = luaL_len(L, index); boost::multi_array<T, 1> v(boost::extents[size]); object table(from_stack(L, index)); for (std::size_t i = 0; i < v.size(); ++i) { v[i] = object_cast<T>(table[i + 1]); } return v; }
void register_(lua_State* L) const { object fn = make_function(L, f, deduce_signature(f), policies); add_overload( object(from_stack(L, -1)) , name , fn ); }
/** * Convert from Lua to C++. */ halmd::raw_array<T> from(lua_State* L, int index) { std::size_t len = luaL_len(L, index); object table(from_stack(L, index)); LOG_TRACE("convert Lua table of size " << len << " to halmd::raw_array<" << demangled_name<T>() << ">"); halmd::raw_array<T> v(len); for (std::size_t i = 0; i < len; ++i) { v[i] = object_cast<T>(table[i + 1]); } return v; }
LUABIND_API class_info get_class_info(argument const& o) { lua_State* L = o.interpreter(); o.push(L); detail::object_rep* obj = detail::get_instance(L, -1); if (!obj) { class_info result; result.name = lua_typename(L, lua_type(L, -1)); lua_pop(L, 1); result.methods = newtable(L); result.attributes = newtable(L); return result; } lua_pop(L, 1); obj->crep()->get_table(L); object table(from_stack(L, -1)); lua_pop(L, 1); class_info result; result.name = obj->crep()->name(); result.methods = newtable(L); result.attributes = newtable(L); std::size_t index = 1; for (iterator i(table), e; i != e; ++i) { if (type(*i) != LUA_TFUNCTION) continue; // We have to create a temporary `object` here, otherwise the proxy // returned by operator->() will mess up the stack. This is a known // problem that probably doesn't show up in real code very often. object member(*i); member.push(L); detail::stack_pop pop(L, 1); if (lua_tocfunction(L, -1) == &detail::property_tag) { result.attributes[index++] = i.key(); } else { result.methods[i.key()] = *i; } } return result; }
//! convert from Lua to C++ std::vector<T> from(lua_State* L, int index) { std::size_t len = luaL_len(L, index); object table(from_stack(L, index)); LOG_TRACE("convert Lua table of size " << len << " to std::vector<" << demangled_name<T>() << ">"); std::vector<T> v; v.reserve(len); for (std::size_t i = 0; i < len; ++i) { v.push_back(object_cast<T>(table[i + 1])); } return v; }
void ReadScriptDescriptor::DEBUG_PrintGlobals() { cout << "SCRIPT DEBUG: Printing script's global variables:" << endl; object o(from_stack(_lstack, LUA_GLOBALSINDEX)); for (luabind::iterator it(o), end; it != end; ++it) { cout << it.key() << " = " << (*it) << " ::: data type = " << type(*it) << endl; if (luabind::type(*it) == LUA_TTABLE) { if (object_cast<string>(it.key()) != "_G") DEBUG_PrintTable(object(*it), 1); } } cout << endl; }
object ReadScriptDescriptor::ReadFunctionPointer(const std::string &key) { if(_open_tables.size() == 0) { // The function should be in the global space lua_getglobal(_lstack, key.c_str()); luabind::object o(from_stack(_lstack, STACK_TOP)); if(!o.is_valid()) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because it was unable to access the function " << "for the global key: " << key << std::endl; return luabind::object(); } if(type(o) != LUA_TFUNCTION) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because the data retrieved was not a function " << "for the global key: " << key << std::endl; return luabind::object(); } return o; } else { // The function should be an element of the most recently opened table luabind::object o(from_stack(_lstack, STACK_TOP)); if(type(o) != LUA_TTABLE) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because the top of the stack was not a table " << "for the table element key: " << key << std::endl; return luabind::object(); } if(type(o[key]) != LUA_TFUNCTION) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because the data retrieved was not a function " << "for the table element key: " << key << std::endl; return luabind::object(); } return o[key]; } } // object ReadScriptDescriptor::ReadFunctionPointer(string key)
LUABIND_API object make_function_aux(lua_State* L, function_object* impl) { void* storage = lua_newuserdata(L, sizeof(function_object*)); push_function_metatable(L); *(function_object**)storage = impl; lua_setmetatable(L, -2); lua_pushlightuserdata(L, &function_tag); lua_pushcclosure(L, impl->entry, 2); stack_pop pop(L, 1); return object(from_stack(L, -1)); }
END_TEST START_TEST( test_apuntador_arreglo) { const tipo_dato VALORES[3][3] = { { 10, 20, 30 }, { 100, 200, 300 }, { 1000, 2000, 3000 } }; void *ptr = NULL; ptr = (void *) VALORES; zlog_fini(); ck_assert_msg(!from_stack(ptr), "El valor %p no es apuntador valido", ptr); }
void ScriptComponent::RunScript( ) { try { resume< void >( m_state ); } catch( error& e ) { object error_msg( from_stack( e.state( ) , -1) ); std::stringstream logMessage; logMessage << error_msg; Logger::Get( )->Warn( logMessage.str( ) ); } }
LUABIND_API object make_function_aux( lua_State* L, int arity , function_callback const& call , function_callback const& score , signature_callback const& signature ) { void* storage = lua_newuserdata(L, sizeof(function)); push_function_metatable(L); new (storage) function(arity, call, score, signature); lua_setmetatable(L, -2); lua_pushcclosure(L, &function_dispatcher, 1); stack_pop pop(L, 1); return object(from_stack(L, -1)); }
END_TEST START_TEST( test_apuntador_allocado) { tipo_dato **valores = NULL; valores = (tipo_dato **) malloc(3 * sizeof(tipo_dato *)); for (int i = 0; i < 3; i++) { *(valores + i) = (tipo_dato *) malloc(10 * sizeof(tipo_dato)); } zlog_fini(); ck_assert_msg(from_stack(*valores), "El valor %p no es apuntador valido", *valores); }
object ReadScriptDescriptor::ReadFunctionPointer(int32 key) { // Fucntion is always a table element for integer keys luabind::object o(from_stack(_lstack, STACK_TOP)); if (type(o) != LUA_TTABLE) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because the top of the stack was not a table " << "for the table element key: " << key << endl; return o; } if (type(o[key]) != LUA_TFUNCTION) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because the data retrieved was not a function " << "for the table element key: " << key << endl; return o; } return o[key]; } // object ReadScriptDescriptor::ReadFunctionPointer(int32 key)
void ScriptComponent::Update( const float& deltaMilliseconds ) { for ( IScriptFunctionHandler::FunctionList::iterator i = m_updateHandlers.begin( ); i != m_updateHandlers.end( ); ++i ) { try { call_function< void >( ( *i )->GetFunction( ), deltaMilliseconds ); } catch( error& e ) { object error_msg( from_stack( e.state( ) , -1) ); std::stringstream logMessage; logMessage << error_msg; Logger::Get( )->Warn( logMessage.str( ) ); } } for ( IScriptFunctionHandler::FunctionList::iterator i = m_updateHandlers.begin( ); i != m_updateHandlers.end( ); ) { if ( ( *i )->IsMarkedForDeletion( ) ) { delete ( *i ); i = m_updateHandlers.erase( i ); } else { ++i; } } for ( IScriptFunctionHandler::FunctionList::iterator i = m_eventHandlers.begin( ); i != m_eventHandlers.end( ); ) { if ( ( *i )->IsMarkedForDeletion( ) ) { delete ( *i ); i = m_eventHandlers.erase( i ); } else { ++i; } } }
void ReadScriptDescriptor::DEBUG_PrintGlobals() { PRINT_WARNING << "SCRIPT DEBUG: Printing script's global variables:" << std::endl; // Push the global table on top of the stack lua_pushglobaltable(_lstack); object o(from_stack(_lstack, -1)); // -1 is the value on top of the stack, here the global table index for(luabind::iterator it(o), end; it != end; ++it) { PRINT_WARNING << it.key() << " = " << (*it) << " ::: data type = " << type(*it) << std::endl; if(luabind::type(*it) == LUA_TTABLE) { if(object_cast<std::string>(it.key()) != "_G") DEBUG_PrintTable(object(*it), 1); } } PRINT_WARNING << std::endl; // Remove the table afterwards lua_pop(_lstack, 1); }
module_::module_(lua_State* L, char const* name) { if (name) { lua_getglobal(L, name); if (!lua_istable(L, -1)) { lua_pop(L, 1); lua_newtable(L); lua_pushvalue(L, -1); lua_setglobal(L, name); } } else { lua_pushglobaltable(L); } m_table = object(from_stack(L, -1)); lua_pop(L, 1); }
// Attempts to get the size of the most recently opened table uint32 ReadScriptDescriptor::GetTableSize() { if (_open_tables.size() == 0) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because there were no open tables to get the size of" << endl; return 0; } // lua returns 0 on table sizes in a couple situations // 1. the indexes don't start from what lua expects // 2. a hash table instead of an array table. // So we'll just count the table size ourselves object o(from_stack(_lstack, STACK_TOP)); if (type(o) != LUA_TTABLE) { IF_PRINT_WARNING(SCRIPT_DEBUG) << "failed because the top of the stack is not a table." << endl; return 0; } uint32 table_size = 0; for (luabind::iterator i(o); i != private_script::TABLE_END; ++i) table_size++; return table_size; }
T to_cpp(lua_State* L, by_const_reference<T>, int index) { return T(from_stack(L, index)); }
void ScriptComponent::OnEvent( const IEvent* event ) { for ( IScriptFunctionHandler::FunctionList::iterator i = m_eventHandlers.begin( ); i != m_eventHandlers.end( ); ++i ) { EventType eventType = event->GetEventType( ); if ( event->GetEventType( ) == ALL_EVENTS ) { ScriptEvent* scriptEvent = ( ScriptEvent* ) event; try { switch( scriptEvent->GetParamType( ) ) { case ScriptEvent::PARAMCOMBO_NONE: call_function< void >( ( *i )->GetFunction( ), scriptEvent->GetEventName( ) ); break; case ScriptEvent::PARAMCOMBO_INT: call_function< void >( ( *i )->GetFunction( ), scriptEvent->GetEventName( ), scriptEvent->GetValue1AsInt( ) ); break; case ScriptEvent::PARAMCOMBO_STRING: call_function< void >( ( *i )->GetFunction( ), scriptEvent->GetEventName( ), scriptEvent->GetValue1AsString( ) ); break; case ScriptEvent::PARAMCOMBO_STRING_STRING: call_function< void >( ( *i )->GetFunction( ), scriptEvent->GetEventName( ), scriptEvent->GetValue1AsString( ), scriptEvent->GetValue2AsString( ) ); break; case ScriptEvent::PARAMCOMBO_STRING_INT: call_function< void >( ( *i )->GetFunction( ), scriptEvent->GetEventName( ), scriptEvent->GetValue1AsString( ), scriptEvent->GetValue2AsInt( ) ); break; case ScriptEvent::PARAMCOMBO_INT_STRING: call_function< void >( ( *i )->GetFunction( ), scriptEvent->GetEventName( ), scriptEvent->GetValue1AsInt( ), scriptEvent->GetValue2AsString( ) ); break; case ScriptEvent::PARAMCOMBO_INT_INT: call_function< void >( ( *i )->GetFunction( ), scriptEvent->GetEventName( ), scriptEvent->GetValue1AsInt( ), scriptEvent->GetValue2AsInt( ) ); break; } } catch( error& e ) { object error_msg( from_stack( e.state( ) , -1) ); std::stringstream logMessage; logMessage << error_msg; Logger::Get( )->Warn( logMessage.str( ) ); } } } }
//! convert from Lua to C++ boost::optional<T> from(lua_State* L, int index) { return luaponte::object_cast<T>(from_stack(L, index)); }
//! compute Lua to C++ conversion score static int compute_score(lua_State* L, int index) { return luaponte::object_cast_nothrow<T>(from_stack(L, index)) ? 0 : -1; }
FType from(lua_State* L, int index) { return LuaFunction<FResultType>( luabind::object(from_stack(L, index))); }