The Battle for Wesnoth  1.19.0-dev
Namespaces | Macros | Typedefs | Functions | Variables
lua_common.cpp File Reference

Contains code common to the application and game lua kernels which cannot or should not go into the lua kernel base files. More...

#include "scripting/lua_common.hpp"
#include "config.hpp"
#include "scripting/push_check.hpp"
#include "tstring.hpp"
#include "variable.hpp"
#include "log.hpp"
#include "gettext.hpp"
#include "lua_jailbreak_exception.hpp"
#include "game_display.hpp"
#include <cstring>
#include <iterator>
#include <string>
Include dependency graph for lua_common.cpp:

Go to the source code of this file.

Namespaces

 lua_common
 

Macros

#define LOG_LUA   LOG_STREAM(info, log_scripting_lua)
 
#define WRN_LUA   LOG_STREAM(warn, log_scripting_lua)
 
#define ERR_LUA   LOG_STREAM(err, log_scripting_lua)
 
#define ERR_WML   LOG_STREAM(err, log_wml)
 
#define return_misformed()    do { lua_settop(L, initial_top); return false; } while (0)
 

Typedefs

typedef std::pair< vconfig::all_children_iterator, vconfig::all_children_iteratorlua_common::vconfig_child_range
 

Functions

static int lua_common::impl_gettext (lua_State *L)
 Creates a t_string object (__call metamethod). More...
 
static int lua_common::impl_gettext_tostr (lua_State *L)
 
int lua_common::intf_textdomain (lua_State *L)
 Creates an interface for gettext. More...
 
static void lua_common::tstring_concat_aux (lua_State *L, t_string &dst, int src)
 Converts a Lua value at position src and appends it to dst. More...
 
static int lua_common::impl_tstring_concat (lua_State *L)
 Appends a scalar to a t_string object (__concat metamethod). More...
 
static int lua_common::impl_tstring_len (lua_State *L)
 
static int lua_common::impl_tstring_collect (lua_State *L)
 Destroys a t_string object before it is collected (__gc metamethod). More...
 
static int lua_common::impl_tstring_lt (lua_State *L)
 
static int lua_common::impl_tstring_le (lua_State *L)
 
static int lua_common::impl_tstring_eq (lua_State *L)
 
static int lua_common::impl_tstring_tostring (lua_State *L)
 Converts a t_string object to a string (__tostring metamethod); that is, performs a translation. More...
 
static int lua_common::impl_vconfig_get (lua_State *L)
 Gets the parsed field of a vconfig object (_index metamethod). More...
 
static int lua_common::impl_vconfig_dir (lua_State *L)
 
static int lua_common::impl_vconfig_size (lua_State *L)
 Returns the number of a child of a vconfig object. More...
 
static int lua_common::impl_vconfig_collect (lua_State *L)
 Destroys a vconfig object before it is collected (__gc metamethod). More...
 
static int lua_common::impl_vconfig_pairs_iter (lua_State *L)
 Iterate through the attributes of a vconfig. More...
 
static int lua_common::impl_vconfig_pairs_collect (lua_State *L)
 Destroy a vconfig pairs iterator. More...
 
static int lua_common::impl_vconfig_pairs (lua_State *L)
 Construct an iterator to iterate through the attributes of a vconfig. More...
 
static int lua_common::impl_vconfig_ipairs_iter (lua_State *L)
 Iterate through the subtags of a vconfig. More...
 
static int lua_common::impl_vconfig_ipairs_collect (lua_State *L)
 Destroy a vconfig ipairs iterator. More...
 
static int lua_common::impl_vconfig_ipairs (lua_State *L)
 Construct an iterator to iterate through the subtags of a vconfig. More...
 
int lua_common::intf_tovconfig (lua_State *L)
 Creates a vconfig containing the WML table. More...
 
std::string lua_common::register_gettext_metatable (lua_State *L)
 Adds the gettext metatable. More...
 
std::string lua_common::register_tstring_metatable (lua_State *L)
 Adds the tstring metatable. More...
 
std::string lua_common::register_vconfig_metatable (lua_State *L)
 Adds the vconfig metatable. More...
 
void * operator new (std::size_t sz, lua_State *L, int nuv)
 
void operator delete (void *, lua_State *L, int)
 
bool luaW_getmetafield (lua_State *L, int idx, const char *key)
 Like luaL_getmetafield, but returns false if key is an empty string or begins with two underscores. More...
 
void luaW_pushvconfig (lua_State *L, const vconfig &cfg)
 Pushes a vconfig on the top of the stack. More...
 
void luaW_pushtstring (lua_State *L, const t_string &v)
 Pushes a t_string on the top of the stack. More...
 
void luaW_pushscalar (lua_State *L, const config::attribute_value &v)
 Converts an attribute value into a Lua object pushed at the top of the stack. More...
 
bool luaW_toscalar (lua_State *L, int index, config::attribute_value &v)
 Converts the value at the top of the stack to an attribute value. More...
 
bool luaW_totstring (lua_State *L, int index, t_string &str)
 Converts a scalar to a translatable string. More...
 
t_string luaW_checktstring (lua_State *L, int index)
 Converts a scalar to a translatable string. More...
 
bool luaW_iststring (lua_State *L, int index)
 
void luaW_filltable (lua_State *L, const config &cfg)
 Converts a config object to a Lua table. More...
 
static int impl_namedtuple_get (lua_State *L)
 
static int impl_namedtuple_dir (lua_State *L)
 
static int impl_namedtuple_tostring (lua_State *L)
 
void luaW_push_namedtuple (lua_State *L, const std::vector< std::string > &names)
 Push an empty "named tuple" onto the stack. More...
 
void luaW_pushlocation (lua_State *L, const map_location &ml)
 Converts a map location object to a Lua table pushed at the top of the stack. More...
 
bool luaW_tolocation (lua_State *L, int index, map_location &loc)
 Converts an optional table or pair of integers to a map location object. More...
 
map_location luaW_checklocation (lua_State *L, int index)
 Converts an optional table or pair of integers to a map location object. More...
 
int luaW_push_locationset (lua_State *L, const std::set< map_location > &locs)
 Converts a set of map locations to a Lua table pushed at the top of the stack. More...
 
std::set< map_locationluaW_check_locationset (lua_State *L, int idx)
 Converts a table of integer pairs to a set of map location objects. More...
 
void luaW_pushconfig (lua_State *L, const config &cfg)
 Converts a config object to a Lua table pushed at the top of the stack. More...
 
bool luaW_toconfig (lua_State *L, int index, config &cfg)
 Converts an optional table or vconfig to a config object. More...
 
config luaW_checkconfig (lua_State *L, int index)
 Converts an optional table or vconfig to a config object. More...
 
config luaW_checkconfig (lua_State *L, int index, const vconfig *&vcfg)
 Like the two-argument version, but if it was a vconfig, also returns a pointer to that vconfig. More...
 
bool luaW_tovconfig (lua_State *L, int index, vconfig &vcfg)
 Gets an optional vconfig from either a table or a userdata. More...
 
vconfig luaW_checkvconfig (lua_State *L, int index, bool allow_missing)
 Gets an optional vconfig from either a table or a userdata. More...
 
bool luaW_getglobal (lua_State *L, const std::vector< std::string > &path)
 Pushes the value found by following the variadic names (char *), if the value is not nil. More...
 
bool luaW_toboolean (lua_State *L, int n)
 
bool luaW_pushvariable (lua_State *L, variable_access_const &v)
 
bool luaW_checkvariable (lua_State *L, variable_access_create &v, int n)
 
bool luaW_tableget (lua_State *L, int index, const char *key)
 
std::string_view luaW_tostring (lua_State *L, int index)
 
std::string_view luaW_tostring_or_default (lua_State *L, int index, std::string_view def)
 
void chat_message (const std::string &caption, const std::string &msg)
 Displays a message in the chat window. More...
 
void push_error_handler (lua_State *L)
 
int luaW_pcall_internal (lua_State *L, int nArgs, int nRets)
 
bool luaW_pcall (lua_State *L, int nArgs, int nRets, bool allow_wml_error)
 Calls a Lua function stored below its nArgs arguments at the top of the stack. More...
 
int luaW_type_error (lua_State *L, int narg, const char *tname)
 
int luaW_type_error (lua_State *L, int narg, const char *kpath, const char *tname)
 

Variables

static const char gettextKey [] = "gettext"
 
static const char vconfigKey [] = "vconfig"
 
static const char vconfigpairsKey [] = "vconfig pairs"
 
static const char vconfigipairsKey [] = "vconfig ipairs"
 
static const char tstringKey [] = "translatable string"
 
static const char executeKey [] = "err"
 
static lg::log_domain log_scripting_lua ("scripting/lua")
 
static lg::log_domain log_wml ("wml")
 

Detailed Description

Contains code common to the application and game lua kernels which cannot or should not go into the lua kernel base files.

Currently contains implementation functions related to vconfig and gettext, also some macros to assist in writing C lua callbacks.

Definition in file lua_common.cpp.

Macro Definition Documentation

◆ ERR_LUA

#define ERR_LUA   LOG_STREAM(err, log_scripting_lua)

Definition at line 51 of file lua_common.cpp.

◆ ERR_WML

#define ERR_WML   LOG_STREAM(err, log_wml)

Definition at line 54 of file lua_common.cpp.

◆ LOG_LUA

#define LOG_LUA   LOG_STREAM(info, log_scripting_lua)

Definition at line 49 of file lua_common.cpp.

◆ return_misformed

#define return_misformed ( )     do { lua_settop(L, initial_top); return false; } while (0)

Definition at line 836 of file lua_common.cpp.

◆ WRN_LUA

#define WRN_LUA   LOG_STREAM(warn, log_scripting_lua)

Definition at line 50 of file lua_common.cpp.

Function Documentation

◆ chat_message()

void chat_message ( const std::string &  caption,
const std::string &  msg 
)

◆ impl_namedtuple_dir()

static int impl_namedtuple_dir ( lua_State *  L)
static

Definition at line 693 of file lua_common.cpp.

Referenced by luaW_push_namedtuple().

◆ impl_namedtuple_get()

static int impl_namedtuple_get ( lua_State *  L)
static

Definition at line 677 of file lua_common.cpp.

References i, and names.

Referenced by luaW_push_namedtuple().

◆ impl_namedtuple_tostring()

static int impl_namedtuple_tostring ( lua_State *  L)
static

Definition at line 699 of file lua_common.cpp.

References i, utils::join(), and lua_push().

Referenced by luaW_push_namedtuple().

◆ luaW_check_locationset()

std::set<map_location> luaW_check_locationset ( lua_State *  L,
int  idx 
)

◆ luaW_checkconfig() [1/2]

config luaW_checkconfig ( lua_State *  L,
int  index 
)

◆ luaW_checkconfig() [2/2]

config luaW_checkconfig ( lua_State *  L,
int  index,
const vconfig *&  vcfg 
)

Like the two-argument version, but if it was a vconfig, also returns a pointer to that vconfig.

Definition at line 925 of file lua_common.cpp.

References utf8::index(), luaW_checkconfig(), p, and vconfigKey.

◆ luaW_checklocation()

map_location luaW_checklocation ( lua_State *  L,
int  index 
)

◆ luaW_checktstring()

t_string luaW_checktstring ( lua_State *  L,
int  index 
)

◆ luaW_checkvariable()

bool luaW_checkvariable ( lua_State *  L,
variable_access_create v,
int  n 
)

◆ luaW_checkvconfig()

vconfig luaW_checkvconfig ( lua_State *  L,
int  index,
bool  allow_missing = false 
)

◆ luaW_filltable()

void luaW_filltable ( lua_State *  L,
const config cfg 
)

Converts a config object to a Lua table.

The destination table should be at the top of the stack on entry. It is still at the top on exit.

Definition at line 654 of file lua_common.cpp.

References config::all_children_range(), config::attribute_range(), luaW_push_namedtuple(), and luaW_pushscalar().

Referenced by luaW_pushconfig(), and luaW_pushvariable().

◆ luaW_getglobal()

bool luaW_getglobal ( lua_State *  L,
const std::vector< std::string > &  path 
)

◆ luaW_getmetafield()

bool luaW_getmetafield ( lua_State *  L,
int  idx,
const char *  key 
)

Like luaL_getmetafield, but returns false if key is an empty string or begins with two underscores.

Definition at line 524 of file lua_common.cpp.

References n.

Referenced by impl_animator_get(), impl_floating_label_getmethod(), impl_music_get(), impl_source_get(), impl_track_get(), and impl_unit_attack_get().

◆ luaW_iststring()

bool luaW_iststring ( lua_State *  L,
int  index 
)

Definition at line 643 of file lua_common.cpp.

References utf8::index(), and tstringKey.

Referenced by game_lua_kernel::intf_allow_end_turn(), and lua_stringx::intf_str_format().

◆ luaW_pcall()

bool luaW_pcall ( lua_State *  L,
int  nArgs,
int  nRets,
bool  allow_wml_error = false 
)

◆ luaW_pcall_internal()

int luaW_pcall_internal ( lua_State *  L,
int  nArgs,
int  nRets 
)

◆ luaW_push_locationset()

int luaW_push_locationset ( lua_State *  L,
const std::set< map_location > &  locs 
)

Converts a set of map locations to a Lua table pushed at the top of the stack.

Definition at line 798 of file lua_common.cpp.

References i, and luaW_pushlocation().

Referenced by ai::cfun_ai_get_avoid(), game_lua_kernel::impl_schedule_get(), game_lua_kernel::intf_get_locations(), intf_mg_get_locations_part2(), intf_mg_get_tiles_radius(), and intf_parse_shroud_bitmap().

◆ luaW_push_namedtuple()

void luaW_push_namedtuple ( lua_State *  L,
const std::vector< std::string > &  names 
)

Push an empty "named tuple" onto the stack.

A named tuple is an array where each index can also be accessed by name. Once it's pushed, you can set the elements, eg with lua_rawseti.

Definition at line 712 of file lua_common.cpp.

References impl_namedtuple_dir(), impl_namedtuple_get(), impl_namedtuple_tostring(), lua_push(), and names.

Referenced by lua_common::impl_vconfig_get(), game_lua_kernel::intf_find_cost_map(), game_lua_kernel::intf_find_reach(), game_lua_kernel::intf_find_vision_range(), intf_named_tuple(), luaW_filltable(), and luaW_pushlocation().

◆ luaW_pushconfig()

void luaW_pushconfig ( lua_State *  L,
const config cfg 
)

◆ luaW_pushlocation()

void luaW_pushlocation ( lua_State *  L,
const map_location ml 
)

◆ luaW_pushscalar()

void luaW_pushscalar ( lua_State *  L,
const config::attribute_value v 
)

Converts an attribute value into a Lua object pushed at the top of the stack.

Definition at line 578 of file lua_common.cpp.

References config_attribute_value::apply_visitor().

Referenced by impl_preferences_get(), lua_common::impl_vconfig_get(), lua_common::impl_vconfig_pairs_iter(), luaW_filltable(), and luaW_pushvariable().

◆ luaW_pushtstring()

void luaW_pushtstring ( lua_State *  L,
const t_string v 
)

◆ luaW_pushvariable()

bool luaW_pushvariable ( lua_State *  L,
variable_access_const v 
)

◆ luaW_pushvconfig()

void luaW_pushvconfig ( lua_State *  L,
const vconfig cfg 
)

◆ luaW_tableget()

bool luaW_tableget ( lua_State *  L,
int  index,
const char *  key 
)

◆ luaW_toboolean()

bool luaW_toboolean ( lua_State *  L,
int  n 
)

◆ luaW_toconfig()

bool luaW_toconfig ( lua_State *  L,
int  index,
config cfg 
)

Converts an optional table or vconfig to a config object.

Parameters
Lthe pointer to the lua interpreter.
indexstack position of the table.
cfgthe config to write the data to.
Returns
false if some attributes had not the proper type.
Note
If the table has holes in the integer keys or floating-point keys, some keys will be ignored and the error will go undetected.

Definition at line 839 of file lua_common.cpp.

References config::add_child(), config::clear(), i, utf8::index(), luaW_toscalar(), return_misformed, config_attribute_value::str(), config::valid_attribute(), config::valid_tag(), and vconfigKey.

Referenced by mapgen_lua_kernel::create_scenario(), ai::lua_ai_context::get_arguments(), ai::lua_ai_context::get_persistent_data(), impl_context_accessor(), impl_music_set(), impl_sndsrc_set(), game_lua_kernel::intf_add_event(), game_lua_kernel::intf_add_undo_actions(), game_lua_kernel::intf_fire_event(), game_lua_kernel::intf_log_replay(), intf_modify_ai_old(), lua_wml::intf_wml_valid(), lua_check_impl::lua_to_or_default(), luaW_checkconfig(), luaW_checkvariable(), luaW_tovconfig(), game_lua_kernel::save_game(), lua_gui2::show_message_dialog(), and ai::lua_object< T >::to_type().

◆ luaW_tolocation()

bool luaW_tolocation ( lua_State *  L,
int  index,
map_location loc 
)

Converts an optional table or pair of integers to a map location object.

Parameters
Lthe pointer to the lua interpreter.
indexstack position of the table or first integer.
locthe location to write to.
Returns
false if a map location couldn't be matched.

Definition at line 741 of file lua_common.cpp.

References utf8::index(), map_location::set_wml_x(), map_location::set_wml_y(), and vconfig::unconstructed_vconfig().

Referenced by ai::ai_recall(), ai::ai_recruit(), impl_add_animation(), impl_source_set(), impl_terrainmap_get(), impl_terrainmap_set(), lua_map_location::intf_distance_between(), game_lua_kernel::intf_erase_unit(), game_lua_kernel::intf_fire_event(), lua_map_location::intf_get_adjacent_tiles(), lua_map_location::intf_get_direction(), lua_map_location::intf_get_in_basis_N_NE(), lua_map_location::intf_get_relative_dir(), lua_map_location::intf_get_tiles_in_radius(), game_lua_kernel::intf_get_time_area(), game_lua_kernel::intf_get_time_of_day(), game_lua_kernel::intf_get_unit(), game_lua_kernel::intf_get_units(), game_lua_kernel::intf_match_unit(), game_lua_kernel::intf_put_unit(), lua_map_location::intf_rotate_right_around_center(), lua_map_location::intf_tiles_adjacent(), intf_unit_defense(), intf_unit_jamming_cost(), intf_unit_movement_cost(), intf_unit_vision_cost(), lua_map_location::intf_vector_diff(), lua_map_location::intf_vector_negation(), lua_map_location::intf_vector_sum(), load_fake_units(), lua_check_impl::lua_to_or_default(), luaW_checklocation(), luaW_to_locationset(), and luaW_tofaivariant().

◆ luaW_toscalar()

bool luaW_toscalar ( lua_State *  L,
int  index,
config::attribute_value v 
)

Converts the value at the top of the stack to an attribute value.

Definition at line 583 of file lua_common.cpp.

References utf8::index(), luaW_toboolean(), and tstringKey.

Referenced by impl_preferences_set(), and luaW_toconfig().

◆ luaW_tostring()

std::string_view luaW_tostring ( lua_State *  L,
int  index 
)

◆ luaW_tostring_or_default()

std::string_view luaW_tostring_or_default ( lua_State *  L,
int  index,
std::string_view  def 
)

Definition at line 1085 of file lua_common.cpp.

References utf8::index().

Referenced by lua_check_impl::lua_to_or_default().

◆ luaW_totstring()

bool luaW_totstring ( lua_State *  L,
int  index,
t_string str 
)

◆ luaW_tovconfig()

bool luaW_tovconfig ( lua_State *  L,
int  index,
vconfig vcfg 
)

Gets an optional vconfig from either a table or a userdata.

Returns
false in case of failure.

Definition at line 934 of file lua_common.cpp.

References utf8::index(), luaW_toconfig(), and vconfigKey.

Referenced by game_lua_kernel::intf_add_time_area(), game_lua_kernel::intf_find_cost_map(), luaW_checkvconfig(), luaW_tofaivariant(), and ai::lua_object< T >::to_type().

◆ luaW_type_error() [1/2]

int luaW_type_error ( lua_State *  L,
int  narg,
const char *  kpath,
const char *  tname 
)

Definition at line 1188 of file lua_common.cpp.

References wfl::msg().

◆ luaW_type_error() [2/2]

int luaW_type_error ( lua_State *  L,
int  narg,
const char *  tname 
)

◆ operator delete()

void operator delete ( void *  p,
lua_State *  L,
int  nuv 
)

Definition at line 516 of file lua_common.cpp.

◆ operator new()

void* operator new ( std::size_t  sz,
lua_State *  L,
int  nuv 
)

Definition at line 511 of file lua_common.cpp.

◆ push_error_handler()

void push_error_handler ( lua_State *  L)

Definition at line 1102 of file lua_common.cpp.

References executeKey, and luaW_getglobal().

Referenced by lua_kernel_base::lua_kernel_base().

Variable Documentation

◆ executeKey

const char executeKey[] = "err"
static

Definition at line 46 of file lua_common.cpp.

Referenced by luaW_pcall_internal(), and push_error_handler().

◆ gettextKey

const char gettextKey[] = "gettext"
static

◆ log_scripting_lua

lg::log_domain log_scripting_lua("scripting/lua") ( "scripting/lua"  )
static

◆ log_wml

lg::log_domain log_wml("wml") ( "wml"  )
static

◆ tstringKey

const char tstringKey[] = "translatable string"
static

◆ vconfigipairsKey

const char vconfigipairsKey[] = "vconfig ipairs"
static

◆ vconfigKey

const char vconfigKey[] = "vconfig"
static

◆ vconfigpairsKey

const char vconfigpairsKey[] = "vconfig pairs"
static