The Battle for Wesnoth  1.15.2+dev
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Static Private Member Functions | Private Attributes | List of all members
lua_kernel_base Class Reference

#include <lua_kernel_base.hpp>

Inheritance diagram for lua_kernel_base:
Inheritance graph

Classes

struct  command_log
 Log implementation. More...
 

Public Types

using external_log_type = std::function< void(const std::string &)>
 
typedef std::function< void(char const *, char const *)> error_handler
 

Public Member Functions

 lua_kernel_base ()
 
virtual ~lua_kernel_base ()
 
void run_lua_tag (const config &cfg)
 Runs a [lua] tag. More...
 
void run (char const *prog, const std::string &name, int nArgs=0)
 Runs a plain script. More...
 
void throwing_run (char const *prog, const std::string &name, int nArgs, bool in_interpreter=false)
 Runs a plain script, but reports errors by throwing lua_error. More...
 
void interactive_run (char const *prog)
 Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it normally. More...
 
void load_package ()
 Loads the package library into lua environment. More...
 
void load_core ()
 Loads the "core" library into the Lua environment. More...
 
std::vector< std::string > get_global_var_names ()
 Get tab completion strings. More...
 
std::vector< std::string > get_attribute_names (const std::string &var_path)
 Gets all attribute names of an extended variable name. More...
 
virtual std::string my_name ()
 User-visible name of the lua kernel that they are talking to. More...
 
const std::stringstream & get_log ()
 Access / manipulate logging of lua kernel activities. More...
 
void add_log (const char *str)
 
void clear_log ()
 
void set_external_log (external_log_type lg)
 
virtual void log_error (char const *msg, char const *context="Lua error")
 Error reporting mechanisms, used by virtual methods protected_call and load_string. More...
 
virtual void throw_exception (char const *msg, char const *context="Lua error")
 
virtual uint32_t get_random_seed ()
 
lua_Stateget_state ()
 
void add_widget_definition (const std::string &type, const std::string &id)
 
int intf_show_lua_console (lua_State *L)
 

Static Public Member Functions

template<typename T >
static T & get_lua_kernel (lua_State *L)
 

Protected Member Functions

int intf_print (lua_State *L)
 Replacement print function – instead of printing to std::cout, print to the command log. More...
 
bool protected_call (int nArgs, int nRets, error_handler)
 
bool load_string (char const *prog, const std::string &name, error_handler)
 
virtual bool protected_call (int nArgs, int nRets)
 
virtual bool load_string (char const *prog, const std::string &name)
 
int intf_dofile (lua_State *L)
 Loads and executes a Lua file. More...
 
int intf_require (lua_State *L)
 Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package. More...
 
int intf_kernel_type (lua_State *L)
 
virtual int impl_game_config_get (lua_State *L)
 
virtual int impl_game_config_set (lua_State *L)
 

Static Protected Member Functions

static bool protected_call (lua_State *L, int nArgs, int nRets, error_handler)
 

Protected Attributes

lua_StatemState
 
command_log cmd_log_
 

Static Private Member Functions

static lua_kernel_base *& get_lua_kernel_base_ptr (lua_State *L)
 

Private Attributes

std::vector< std::tuple< std::string, std::string > > registered_widget_definitions_
 

Detailed Description

Definition at line 26 of file lua_kernel_base.hpp.

Member Typedef Documentation

◆ error_handler

typedef std::function<void(char const*, char const*)> lua_kernel_base::error_handler

Definition at line 69 of file lua_kernel_base.hpp.

◆ external_log_type

using lua_kernel_base::external_log_type = std::function<void(const std::string &)>

Definition at line 62 of file lua_kernel_base.hpp.

Constructor & Destructor Documentation

◆ lua_kernel_base()

lua_kernel_base::lua_kernel_base ( )

Definition at line 374 of file lua_kernel_base.cpp.

References cmd_log_, luaL_Reg::func, Gen, randomness::generator, get_lua_kernel_base_ptr(), impl_name_generator_call(), impl_name_generator_collect(), Interp, lua_gui2::intf_add_dialog_tree_node(), lua_fileops::intf_canonical_path(), intf_compare_versions(), lua_formula_bridge::intf_compile_formula(), intf_deprecated_message(), lua_map_location::intf_distance_between(), lua_formula_bridge::intf_eval_formula(), lua_map_location::intf_get_adjacent_tiles(), lua_gui2::intf_get_dialog_value(), lua_map_location::intf_get_direction(), intf_get_image_size(), lua_map_location::intf_get_in_basis_N_NE(), intf_get_language(), lua_map_location::intf_get_relative_dir(), intf_get_time_stamp(), lua_fileops::intf_have_file(), intf_load(), intf_log(), intf_name_generator(), intf_random(), lua_fileops::intf_read_file(), lua_gui2::intf_remove_dialog_item(), intf_require(), lua_map_location::intf_rotate_right_around_center(), lua_gui2::intf_set_dialog_active(), lua_gui2::intf_set_dialog_callback(), lua_gui2::intf_set_dialog_canvas(), lua_gui2::intf_set_dialog_focus(), lua_gui2::intf_set_dialog_markup(), lua_gui2::intf_set_dialog_tooltip(), lua_gui2::intf_set_dialog_value(), lua_gui2::intf_set_dialog_visible(), lua_common::intf_textdomain(), lua_map_location::intf_tiles_adjacent(), lua_map_location::intf_vector_diff(), lua_map_location::intf_vector_negation(), lua_map_location::intf_vector_sum(), lua_rng::load_tables(), log_error(), lua_createtable(), lua_getglobal(), lua_gettable(), lua_istable, lua_newtable, lua_newuserdata(), lua_next(), lua_pop, lua_pushcfunction, lua_pushnil(), lua_pushstring(), LUA_REGISTRYINDEX, lua_setfield(), lua_setglobal(), lua_setmetatable(), lua_settop(), lua_tostring, luaL_newmetatable(), luaL_requiref(), luaL_setfuncs(), luaopen_base(), luaopen_coroutine(), luaopen_debug(), luaopen_math(), luaopen_os(), luaopen_string(), luaopen_table(), luaopen_utf8(), luaW_getglobal(), lua_wml::luaW_open(), lua_stringx::luaW_open(), lua_gui2::luaW_open(), mState, my_name(), protected_call(), push_error_handler(), lua_common::register_gettext_metatable(), lua_cpp::register_metatable(), lua_formula_bridge::register_metatables(), lua_common::register_tstring_metatable(), and lua_gui2::show_dialog().

◆ ~lua_kernel_base()

lua_kernel_base::~lua_kernel_base ( )
virtual

Member Function Documentation

◆ add_log()

void lua_kernel_base::add_log ( const char *  str)
inline

Definition at line 59 of file lua_kernel_base.hpp.

References cmd_log_.

◆ add_widget_definition()

void lua_kernel_base::add_widget_definition ( const std::string &  type,
const std::string &  id 
)
inline

Definition at line 79 of file lua_kernel_base.hpp.

References registered_widget_definitions_.

◆ clear_log()

void lua_kernel_base::clear_log ( )
inline

◆ get_attribute_names()

std::vector< std::string > lua_kernel_base::get_attribute_names ( const std::string &  input)

◆ get_global_var_names()

std::vector< std::string > lua_kernel_base::get_global_var_names ( )

Get tab completion strings.

Gets all the global variable names in the Lua environment.

This is useful for tab completion.

Definition at line 925 of file lua_kernel_base.cpp.

References lua_getglobal(), lua_gettop(), lua_isstring(), lua_next(), lua_pop, lua_pushnil(), lua_settop(), lua_tostring, and mState.

Referenced by gui2::dialogs::lua_interpreter::lua_model::get_globals().

◆ get_log()

const std::stringstream& lua_kernel_base::get_log ( )
inline

Access / manipulate logging of lua kernel activities.

Definition at line 58 of file lua_kernel_base.hpp.

References cmd_log_, flush(), and lua_kernel_base::command_log::log_.

Referenced by gui2::dialogs::lua_interpreter::lua_model::lua_model().

◆ get_lua_kernel()

template<typename T >
static T& lua_kernel_base::get_lua_kernel ( lua_State L)
inlinestatic

◆ get_lua_kernel_base_ptr()

lua_kernel_base *& lua_kernel_base::get_lua_kernel_base_ptr ( lua_State L)
staticprivate

Definition at line 1010 of file lua_kernel_base.cpp.

References lua_getextraspace.

Referenced by get_lua_kernel(), and lua_kernel_base().

◆ get_random_seed()

uint32_t lua_kernel_base::get_random_seed ( )
virtual

Reimplemented in mapgen_lua_kernel.

Definition at line 1022 of file lua_kernel_base.cpp.

References seed_rng::next_seed().

Referenced by get_lua_kernel(), and mapgen_lua_kernel::get_random_seed().

◆ get_state()

lua_State* lua_kernel_base::get_state ( )
inline

Definition at line 78 of file lua_kernel_base.hpp.

References mState.

◆ impl_game_config_get()

int lua_kernel_base::impl_game_config_get ( lua_State L)
protectedvirtual

◆ impl_game_config_set()

int lua_kernel_base::impl_game_config_set ( lua_State L)
protectedvirtual

Reimplemented in game_lua_kernel.

Definition at line 889 of file lua_kernel_base.cpp.

References luaL_argerror(), and luaL_checkstring.

Referenced by game_lua_kernel::impl_game_config_set().

◆ interactive_run()

void lua_kernel_base::interactive_run ( char const *  prog)

Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it normally.

Throws exceptions.

Definition at line 755 of file lua_kernel_base.cpp.

References cmd_log_, i, Interp, load_string(), lua_getfield(), lua_gettop(), LUA_MULTRET, lua_pop, lua_pushvalue(), LUA_REGISTRYINDEX, lua_setfield(), lua_settop(), lua_setupvalue(), luaW_getglobal(), mState, protected_call(), throw_exception(), and throwing_run().

Referenced by gui2::dialogs::lua_interpreter::lua_model::execute().

◆ intf_dofile()

int lua_kernel_base::intf_dofile ( lua_State L)
protected

Loads and executes a Lua file.

  • Arg 1: string containing the file name.
  • Ret *: values returned by executing the file body.

Definition at line 804 of file lua_kernel_base.cpp.

References lua_fileops::load_file(), lua_call, lua_gettop(), LUA_MULTRET, lua_rotate(), and luaL_checkstring.

◆ intf_kernel_type()

int lua_kernel_base::intf_kernel_type ( lua_State L)
protected

Definition at line 867 of file lua_kernel_base.cpp.

References lua_push(), and my_name().

◆ intf_print()

int lua_kernel_base::intf_print ( lua_State L)
protected

Replacement print function – instead of printing to std::cout, print to the command log.

Intended to be bound to this' command_log at registration time.

Definition at line 100 of file lua_kernel_base.cpp.

References cmd_log_, DBG_LUA, i, LOG_LUA, lua_call, lua_getglobal(), lua_gettop(), lua_pop, lua_pushvalue(), and lua_tostring.

◆ intf_require()

int lua_kernel_base::intf_require ( lua_State L)
protected

Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package.

Stores the result of the script in wesnoth.package and returns it.

  • Arg 1: string containing the file name.
  • Ret 1: value returned by the script.

Definition at line 824 of file lua_kernel_base.cpp.

References DBG_LUA, game_config::debug_lua, lua_fileops::load_file(), log_error(), lua_getglobal(), lua_isnil, lua_pop, lua_pushstring(), lua_pushvalue(), lua_rawget(), lua_settable(), luaL_argerror(), luaL_checkstring, and protected_call().

Referenced by lua_kernel_base().

◆ intf_show_lua_console()

int lua_kernel_base::intf_show_lua_console ( lua_State L)

◆ load_core()

void lua_kernel_base::load_core ( )

Loads the "core" library into the Lua environment.

Without this, most Wesnoth Lua won't work. Cannot be called from the constructor because it needs to call virtual functions.

Definition at line 909 of file lua_kernel_base.cpp.

References cmd_log_, lua_pushstring(), lua_settop(), luaW_getglobal(), mState, and protected_call().

Referenced by lua_map_generator::lua_map_generator().

◆ load_package()

void lua_kernel_base::load_package ( )

Loads the package library into lua environment.

Loads the "package" package into the Lua environment.

Only in allow in unsafe modes.

This action is inherently unsafe, as Lua scripts will now be able to load C libraries on their own, hence granting them the same privileges as the Wesnoth binary itself.

Definition at line 901 of file lua_kernel_base.cpp.

References lua_call, lua_pushcfunction, lua_pushstring(), luaopen_package(), and mState.

Referenced by game_launcher::init_lua_script().

◆ load_string() [1/2]

bool lua_kernel_base::load_string ( char const *  prog,
const std::string &  name,
error_handler  e_h 
)
protected

◆ load_string() [2/2]

bool lua_kernel_base::load_string ( char const *  prog,
const std::string &  name 
)
protectedvirtual

Definition at line 643 of file lua_kernel_base.cpp.

References load_string(), and log_error().

◆ log_error()

void lua_kernel_base::log_error ( char const *  msg,
char const *  context = "Lua error" 
)
virtual

Error reporting mechanisms, used by virtual methods protected_call and load_string.

Reimplemented in game_lua_kernel.

Definition at line 627 of file lua_kernel_base.cpp.

References ERR_LUA.

Referenced by intf_require(), intf_show_lua_console(), application_lua_kernel::load_script_from_file(), application_lua_kernel::load_script_from_string(), load_string(), game_lua_kernel::log_error(), lua_kernel_base(), protected_call(), run(), and set_external_log().

◆ my_name()

virtual std::string lua_kernel_base::my_name ( )
inlinevirtual

User-visible name of the lua kernel that they are talking to.

Reimplemented in game_lua_kernel, application_lua_kernel, and mapgen_lua_kernel.

Definition at line 55 of file lua_kernel_base.hpp.

Referenced by gui2::dialogs::lua_interpreter::lua_model::get_name(), intf_kernel_type(), and lua_kernel_base().

◆ protected_call() [1/3]

bool lua_kernel_base::protected_call ( int  nArgs,
int  nRets,
error_handler  e_h 
)
protected

◆ protected_call() [2/3]

bool lua_kernel_base::protected_call ( lua_State L,
int  nArgs,
int  nRets,
error_handler  e_h 
)
staticprotected

◆ protected_call() [3/3]

bool lua_kernel_base::protected_call ( int  nArgs,
int  nRets 
)
protectedvirtual

Definition at line 637 of file lua_kernel_base.cpp.

References log_error(), and protected_call().

◆ run()

void lua_kernel_base::run ( char const *  prog,
const std::string &  name,
int  nArgs = 0 
)

Runs a plain script.

Doesn't throw lua_error.

Definition at line 744 of file lua_kernel_base.cpp.

References cmd_log_, e, log_error(), throwing_run(), and game::error::what().

Referenced by game_launcher::init_lua_script(), run_lua_tag(), and SYNCED_COMMAND_HANDLER_FUNCTION().

◆ run_lua_tag()

void lua_kernel_base::run_lua_tag ( const config cfg)

Runs a [lua] tag.

Doesn't throw lua_error.

Definition at line 718 of file lua_kernel_base.cpp.

References config::child(), luaW_pushconfig(), mState, and run().

Referenced by game_lua_kernel::initialize().

◆ set_external_log()

void lua_kernel_base::set_external_log ( external_log_type  lg)
inline

◆ throw_exception()

void lua_kernel_base::throw_exception ( char const *  msg,
char const *  context = "Lua error" 
)
virtual

◆ throwing_run()

void lua_kernel_base::throwing_run ( char const *  prog,
const std::string &  name,
int  nArgs,
bool  in_interpreter = false 
)

Runs a plain script, but reports errors by throwing lua_error.

Definition at line 729 of file lua_kernel_base.cpp.

References cmd_log_, Interp, load_string(), lua_getfield(), lua_insert, LUA_MULTRET, lua_pop, LUA_REGISTRYINDEX, lua_setupvalue(), mState, protected_call(), and throw_exception().

Referenced by interactive_run(), and run().

Member Data Documentation

◆ cmd_log_

command_log lua_kernel_base::cmd_log_
protected

◆ mState

lua_State* lua_kernel_base::mState
protected

◆ registered_widget_definitions_

std::vector<std::tuple<std::string, std::string> > lua_kernel_base::registered_widget_definitions_
private

Definition at line 143 of file lua_kernel_base.hpp.

Referenced by add_widget_definition(), and ~lua_kernel_base().


The documentation for this class was generated from the following files: