Fawkes API  Fawkes Development Version
fawkes::LuaContext Class Reference

Lua C++ wrapper. More...

#include <>>

Inheritance diagram for fawkes::LuaContext:

Public Member Functions

 LuaContext (bool enable_tracebacks=true)
 Constructor. More...
 
 LuaContext (lua_State *L)
 Wrapper contstructor. More...
 
 ~LuaContext ()
 Destructor. More...
 
void setup_fam (bool auto_restart, bool conc_thread)
 Setup file alteration monitor. More...
 
RefPtr< FileAlterationMonitorget_fam () const
 Get file alteration monitor. More...
 
void set_start_script (const char *start_script)
 Set start script. More...
 
void set_finalization_calls (std::string finalize, std::string finalize_prepare, std::string finalize_cancel)
 Set code to execute during finalization. More...
 
void restart ()
 Restart Lua. More...
 
void add_package_dir (const char *path, bool prefix=false)
 Add a Lua package directory. More...
 
void add_cpackage_dir (const char *path, bool prefix=false)
 Add a Lua C package directory. More...
 
void add_package (const char *package)
 Add a default package. More...
 
lua_State * get_lua_state ()
 Get Lua state. More...
 
void lock ()
 Lock Lua state. More...
 
bool try_lock ()
 Try to lock the Lua state. More...
 
void unlock ()
 Unlock Lua state. More...
 
void do_file (const char *filename)
 Execute file. More...
 
void do_string (const char *format,...)
 Execute string. More...
 
void load_string (const char *s)
 Load Lua string. More...
 
void pcall (int nargs=0, int nresults=0, int errfunc=0)
 Protected call. More...
 
void set_usertype (const char *name, void *data, const char *type_name, const char *name_space=0)
 Assign usertype to global variable. More...
 
void set_string (const char *name, const char *value)
 Assign string to global variable. More...
 
void set_number (const char *name, lua_Number value)
 Assign number to global variable. More...
 
void set_boolean (const char *name, bool value)
 Assign boolean to global variable. More...
 
void set_integer (const char *name, lua_Integer value)
 Assign integer to global variable. More...
 
void set_cfunction (const char *name, lua_CFunction f)
 Assign cfunction to global variable. More...
 
void remove_global (const char *name)
 Remove global variable. More...
 
void set_global (const char *name)
 Set a global value. More...
 
void push_boolean (bool value)
 Push boolean on top of stack. More...
 
void push_fstring (const char *format,...)
 Push formatted string on top of stack. More...
 
void push_integer (lua_Integer value)
 Push integer on top of stack. More...
 
void push_light_user_data (void *p)
 Push light user data on top of stack. More...
 
void push_lstring (const char *s, size_t len)
 Push substring on top of stack. More...
 
void push_nil ()
 Push nil on top of stack. More...
 
void push_number (lua_Number value)
 Push number on top of stack. More...
 
void push_string (const char *value)
 Push string on top of stack. More...
 
void push_thread ()
 Push thread on top of stack. More...
 
void push_value (int idx)
 Push a copy of the element at the given index on top of the stack. More...
 
void push_vfstring (const char *format, va_list arg)
 Push formatted string on top of stack. More...
 
void push_usertype (void *data, const char *type_name, const char *name_space=0)
 Push usertype on top of stack. More...
 
void push_cfunction (lua_CFunction f)
 Push C function on top of stack. More...
 
std::string type_name (int idx)
 Get name of type of value at a given index. More...
 
void pop (int n)
 Pop value(s) from stack. More...
 
void remove (int idx)
 Remove value from stack. More...
 
int stack_size ()
 Get size of stack. More...
 
void create_table (int narr=0, int nrec=0)
 Create a table on top of the stack. More...
 
void set_table (int t_index=-3)
 Set value of a table. More...
 
void set_field (const char *key, int t_index=-2)
 Set field of a table. More...
 
void get_table (int idx)
 Get value from table. More...
 
void get_field (int idx, const char *k)
 Get named value from table. More...
 
void get_global (const char *name)
 Get global variable. More...
 
bool table_next (int idx)
 Iterate to next entry of table. More...
 
void raw_set (int idx)
 Set value without invoking meta methods. More...
 
void raw_seti (int idx, int n)
 Set indexed value without invoking meta methods. More...
 
void raw_get (int idx)
 Get value without invoking meta methods. More...
 
void raw_geti (int idx, int n)
 Get indexed value without invoking meta methods. More...
 
lua_Number to_number (int idx)
 Retrieve stack value as number. More...
 
lua_Integer to_integer (int idx)
 Retrieve stack value as integer. More...
 
bool to_boolean (int idx)
 Retrieve stack value as boolean. More...
 
const char * to_string (int idx)
 Retrieve stack value as string. More...
 
void * to_userdata (int idx)
 Retrieve stack value as userdata. More...
 
void * to_pointer (int idx)
 Retrieve stack value as pointer. More...
 
void * to_usertype (int idx)
 Retrieve stack value as a tolua++ user type. More...
 
bool is_boolean (int idx)
 Check if stack value is a boolean. More...
 
bool is_cfunction (int idx)
 Check if stack value is a C function. More...
 
bool is_function (int idx)
 Check if stack value is a function. More...
 
bool is_light_user_data (int idx)
 Check if stack value is light user data. More...
 
bool is_nil (int idx)
 Check if stack value is nil. More...
 
bool is_number (int idx)
 Check if stack value is a number. More...
 
bool is_string (int idx)
 Check if stack value is a string. More...
 
bool is_table (int idx)
 Check if stack value is a table. More...
 
bool is_thread (int idx)
 Check if stack value is a thread. More...
 
size_t objlen (int idx)
 Get object length. More...
 
void setfenv (int idx=-2)
 Set function environment. More...
 
void add_watcher (LuaContextWatcher *watcher)
 Add a context watcher. More...
 
void remove_watcher (LuaContextWatcher *watcher)
 Remove a context watcher. More...
 
virtual void fam_event (const char *filename, unsigned int mask)
 Event has been raised. More...
 
void process_fam_events ()
 Process FAM events. More...
 
- Public Member Functions inherited from fawkes::FamListener
virtual ~FamListener ()
 Virtual empty destructor. More...
 

Additional Inherited Members

- Static Public Attributes inherited from fawkes::FamListener
static const unsigned int FAM_ACCESS = 0x00000001
 File was accessed. More...
 
static const unsigned int FAM_MODIFY = 0x00000002
 File was modified. More...
 
static const unsigned int FAM_ATTRIB = 0x00000004
 Metadata changed. More...
 
static const unsigned int FAM_CLOSE_WRITE = 0x00000008
 Writtable file was closed. More...
 
static const unsigned int FAM_CLOSE_NOWRITE = 0x00000010
 Unwrittable file closed. More...
 
static const unsigned int FAM_CLOSE = (FAM_CLOSE_WRITE | FAM_CLOSE_NOWRITE)
 Close. More...
 
static const unsigned int FAM_OPEN = 0x00000020
 File was opened. More...
 
static const unsigned int FAM_MOVED_FROM = 0x00000040
 File was moved from X. More...
 
static const unsigned int FAM_MOVED_TO = 0x00000080
 File was moved to Y. More...
 
static const unsigned int FAM_MOVE = (FAM_MOVED_FROM | FAM_MOVED_TO)
 Moves. More...
 
static const unsigned int FAM_CREATE = 0x00000100
 Subfile was created. More...
 
static const unsigned int FAM_DELETE = 0x00000200
 Subfile was deleted. More...
 
static const unsigned int FAM_DELETE_SELF = 0x00000400
 Self was deleted. More...
 
static const unsigned int FAM_MOVE_SELF = 0x00000800
 Self was moved. More...
 
static const unsigned int FAM_UNMOUNT = 0x00002000
 Backing fs was unmounted. More...
 
static const unsigned int FAM_Q_OVERFLOW = 0x00004000
 Event queued overflowed. More...
 
static const unsigned int FAM_IGNORED = 0x00008000
 File was ignored. More...
 
static const unsigned int FAM_ONLYDIR = 0x01000000
 Only watch the path if it is a directory. More...
 
static const unsigned int FAM_DONT_FOLLOW = 0x02000000
 Do not follow a sym link. More...
 
static const unsigned int FAM_MASK_ADD = 0x20000000
 Add to the mask of an already existing watch. More...
 
static const unsigned int FAM_ISDIR = 0x40000000
 Event occurred against dir. More...
 
static const unsigned int FAM_ONESHOT = 0x80000000
 Only send event once. More...
 
static const unsigned int FAM_ALL_EVENTS
 All events which a program can wait on. More...
 

Detailed Description

Lua C++ wrapper.

This thin wrapper allows for easy integration of Fawkes into other applications. It provides convenience methods to some Lua and tolua++ features like setting global variables or pushing/popping values.

It allows raw access to the Lua state since this class does not and should not provide all the features Lua provides. If you use this make sure that you lock the Lua context to avoid multi-threading problems (if that is a possible concern in your application).

LuaContext can use a FileAlterationMonitor on all added package and C package directories. If anything changes in these directories the Lua instance is then automatically restarted (closed, re-opened and re-initialized).

Author
Tim Niemueller

Definition at line 47 of file context.h.

Constructor & Destructor Documentation

◆ LuaContext() [1/2]

fawkes::LuaContext::LuaContext ( bool  enable_tracebacks = true)

Constructor.

Parameters
enable_tracebacksif true an error function is installed at the top of the stackand used for pcalls where errfunc is 0.

Definition at line 66 of file context.cpp.

Referenced by get_fam().

◆ LuaContext() [2/2]

fawkes::LuaContext::LuaContext ( lua_State *  L)

Wrapper contstructor.

This wraps around an existing Lua state. It does not initialize the state in the sense that it would add variables etc. It only provides convenient access to the state methods via a C++ interface. It's mainly intended to be used to create a LuaContext to be passed to LuaContextWatcher::lua_restarted(). The state is not closed on destruction as is done when using the other ctor.

Parameters
LLua state to wrap

Definition at line 87 of file context.cpp.

◆ ~LuaContext()

fawkes::LuaContext::~LuaContext ( )

Destructor.

Definition at line 98 of file context.cpp.

References fawkes::Thread::cancel(), do_string(), fawkes::Thread::join(), and fawkes::Mutex::lock().

Member Function Documentation

◆ add_cpackage_dir()

void fawkes::LuaContext::add_cpackage_dir ( const char *  path,
bool  prefix = false 
)

Add a Lua C package directory.

The directory is added to the search path for lua C packages. Files with a .so suffix will be considered as Lua modules.

Parameters
pathpath to add
prefixif true, insert path at the beginning of the search path, append to end otherwise

Definition at line 360 of file context.cpp.

References do_string(), and lock().

Referenced by LuaAgentPeriodicExecutionThread::init(), and LuaAgentContinuousExecutionThread::init().

◆ add_package()

void fawkes::LuaContext::add_package ( const char *  package)

Add a default package.

Packages that are added this way are automatically loaded now and on restart().

Parameters
packagepackage to add

Definition at line 383 of file context.cpp.

References do_string(), and lock().

Referenced by fawkes::LuaInterfaceImporter::add_interface(), LuaAgentPeriodicExecutionThread::init(), LuaAgentContinuousExecutionThread::init(), SkillerNavGraphFeature::init_lua_context(), and fawkes::LuaInterfaceImporter::write().

◆ add_package_dir()

void fawkes::LuaContext::add_package_dir ( const char *  path,
bool  prefix = false 
)

Add a Lua package directory.

The directory is added to the search path for lua packages. Files with a .lua suffix will be considered as Lua modules.

Parameters
pathpath to add
prefixif true, insert path at the beginning of the search path, append to end otherwise

Definition at line 331 of file context.cpp.

References do_string(), and lock().

Referenced by LuaAgentPeriodicExecutionThread::init(), and LuaAgentContinuousExecutionThread::init().

◆ add_watcher()

void fawkes::LuaContext::add_watcher ( fawkes::LuaContextWatcher watcher)

Add a context watcher.

Parameters
watcherwatcher to add

Definition at line 1396 of file context.cpp.

Referenced by fawkes::LuaInterfaceImporter::LuaInterfaceImporter().

◆ create_table()

void fawkes::LuaContext::create_table ( int  narr = 0,
int  nrec = 0 
)

Create a table on top of the stack.

Parameters
narrnumber of array elements
nrecnumber of non-array elements

Definition at line 1002 of file context.cpp.

Referenced by SkillerExecutionThread::lua_restarted(), and fawkes::LuaInterfaceImporter::write().

◆ do_file()

void fawkes::LuaContext::do_file ( const char *  filename)

Execute file.

Parameters
filenamefilet to load and excute.

Definition at line 436 of file context.cpp.

References do_string(), and lock().

Referenced by get_fam(), and set_start_script().

◆ do_string()

void fawkes::LuaContext::do_string ( const char *  format,
  ... 
)

◆ fam_event()

void fawkes::LuaContext::fam_event ( const char *  filename,
unsigned int  mask 
)
virtual

Event has been raised.

Parameters
filenamename of the file that triggered the event
maskmask indicating the event. Currently inotify event flags are used, see inotify.h.

Implements fawkes::FamListener.

Definition at line 1443 of file context.cpp.

References restart().

◆ get_fam()

RefPtr< FileAlterationMonitor > fawkes::LuaContext::get_fam ( ) const

Get file alteration monitor.

Returns
reference counted pointer to file alteration monitor. Note that the pointer might be invalid if setup_fam() has not been called.

Definition at line 147 of file context.cpp.

References do_file(), do_string(), and LuaContext().

Referenced by LuaAgentContinuousExecutionThread::init().

◆ get_field()

void fawkes::LuaContext::get_field ( int  idx,
const char *  k 
)

Get named value from table.

Retrieves the t[k], where k is the given key and t is a table at the given index idx. The value is pushed onto the stack.

Parameters
idxindex of the table
kkey of the table entry

Definition at line 1069 of file context.cpp.

Referenced by fawkes::LuaInterfaceImporter::add_interface().

◆ get_global()

void fawkes::LuaContext::get_global ( const char *  name)

Get global variable.

Parameters
namename of the global variable

Definition at line 1126 of file context.cpp.

Referenced by fawkes::LuaInterfaceImporter::add_interface(), SkillerNavGraphFeature::finalize_lua_context(), and SkillerNavGraphFeature::init_lua_context().

◆ get_lua_state()

lua_State * fawkes::LuaContext::get_lua_state ( )

Get Lua state.

Allows for raw modification of the used Lua state. Remember proper locking!

Returns
Currently used Lua state

Definition at line 400 of file context.cpp.

◆ get_table()

void fawkes::LuaContext::get_table ( int  idx)

Get value from table.

Assumes that an index k is at the top of the stack. Then t[k] is retrieved, where t is a table at the given index idx. The resulting value is pushed onto the stack, while the key k is popped from the stack, thus the value replaces the key.

Parameters
idxindex of the table on the stack

Definition at line 1056 of file context.cpp.

◆ is_boolean()

bool fawkes::LuaContext::is_boolean ( int  idx)

Check if stack value is a boolean.

Parameters
idxstack index of value
Returns
true if value is a boolean, false otherwise

Definition at line 1247 of file context.cpp.

◆ is_cfunction()

bool fawkes::LuaContext::is_cfunction ( int  idx)

Check if stack value is a C function.

Parameters
idxstack index of value
Returns
true if value is a C function, false otherwise

Definition at line 1258 of file context.cpp.

◆ is_function()

bool fawkes::LuaContext::is_function ( int  idx)

Check if stack value is a function.

Parameters
idxstack index of value
Returns
true if value is a function, false otherwise

Definition at line 1269 of file context.cpp.

◆ is_light_user_data()

bool fawkes::LuaContext::is_light_user_data ( int  idx)

Check if stack value is light user data.

Parameters
idxstack index of value
Returns
true if value is light user data , false otherwise

Definition at line 1280 of file context.cpp.

◆ is_nil()

bool fawkes::LuaContext::is_nil ( int  idx)

Check if stack value is nil.

Parameters
idxstack index of value
Returns
true if value is nil, false otherwise

Definition at line 1291 of file context.cpp.

◆ is_number()

bool fawkes::LuaContext::is_number ( int  idx)

Check if stack value is a number.

Parameters
idxstack index of value
Returns
true if value is a number, false otherwise

Definition at line 1302 of file context.cpp.

◆ is_string()

bool fawkes::LuaContext::is_string ( int  idx)

Check if stack value is a string.

Parameters
idxstack index of value
Returns
true if value is a string, false otherwise

Definition at line 1313 of file context.cpp.

◆ is_table()

bool fawkes::LuaContext::is_table ( int  idx)

Check if stack value is a table.

Parameters
idxstack index of value
Returns
true if value is a table, false otherwise

Definition at line 1324 of file context.cpp.

◆ is_thread()

bool fawkes::LuaContext::is_thread ( int  idx)

Check if stack value is a thread.

Parameters
idxstack index of value
Returns
true if value is a thread, false otherwise

Definition at line 1335 of file context.cpp.

◆ load_string()

void fawkes::LuaContext::load_string ( const char *  s)

Load Lua string.

Loads the Lua string and places it as a function on top of the stack.

Parameters
sstring to load

Definition at line 601 of file context.cpp.

◆ lock()

◆ objlen()

size_t fawkes::LuaContext::objlen ( int  idx)

Get object length.

Parameters
idxstack index of value
Returns
size of object

Definition at line 1346 of file context.cpp.

◆ pcall()

void fawkes::LuaContext::pcall ( int  nargs = 0,
int  nresults = 0,
int  errfunc = 0 
)

Protected call.

Calls the function on top of the stack. Errors are handled gracefully.

Parameters
nargsnumber of arguments
nresultsnumber of results
errfuncstack index of an error handling function
Exceptions
Exceptionthrown for generic runtime error or if the error function could not be executed.
OutOfMemoryExceptionthrown if not enough memory was available

Definition at line 629 of file context.cpp.

◆ pop()

void fawkes::LuaContext::pop ( int  n)

Pop value(s) from stack.

Parameters
nnumber of values to pop

Definition at line 964 of file context.cpp.

References lock(), and stack_size().

Referenced by fawkes::LuaInterfaceImporter::add_interface(), and SkillerExecutionThread::lua_restarted().

◆ process_fam_events()

void fawkes::LuaContext::process_fam_events ( )

Process FAM events.

Definition at line 1436 of file context.cpp.

Referenced by LuaAgentPeriodicExecutionThread::loop(), and SkillerExecutionThread::loop().

◆ push_boolean()

void fawkes::LuaContext::push_boolean ( bool  value)

Push boolean on top of stack.

Parameters
valuevalue to push

Definition at line 797 of file context.cpp.

References lock().

◆ push_cfunction()

void fawkes::LuaContext::push_cfunction ( lua_CFunction  f)

Push C function on top of stack.

Parameters
fC Function to push

Definition at line 943 of file context.cpp.

References lock().

◆ push_fstring()

void fawkes::LuaContext::push_fstring ( const char *  format,
  ... 
)

Push formatted string on top of stack.

Parameters
formatstring format
See also
man 3 sprintf

Definition at line 809 of file context.cpp.

References lock().

◆ push_integer()

void fawkes::LuaContext::push_integer ( lua_Integer  value)

Push integer on top of stack.

Parameters
valuevalue to push

Definition at line 823 of file context.cpp.

References lock().

◆ push_light_user_data()

void fawkes::LuaContext::push_light_user_data ( void *  p)

Push light user data on top of stack.

Parameters
ppointer to light user data to push

Definition at line 834 of file context.cpp.

References lock().

◆ push_lstring()

void fawkes::LuaContext::push_lstring ( const char *  s,
size_t  len 
)

Push substring on top of stack.

Parameters
sstring to push
lenlength of string to push

Definition at line 846 of file context.cpp.

References lock().

◆ push_nil()

void fawkes::LuaContext::push_nil ( )

Push nil on top of stack.

Definition at line 856 of file context.cpp.

References lock().

Referenced by SkillerNavGraphFeature::finalize_lua_context(), and SkillerExecutionThread::lua_restarted().

◆ push_number()

void fawkes::LuaContext::push_number ( lua_Number  value)

Push number on top of stack.

Parameters
valuevalue to push

Definition at line 867 of file context.cpp.

References lock().

◆ push_string()

void fawkes::LuaContext::push_string ( const char *  value)

Push string on top of stack.

Parameters
valuevalue to push

Definition at line 878 of file context.cpp.

References lock().

Referenced by SkillerNavGraphFeature::finalize_lua_context(), SkillerNavGraphFeature::init_lua_context(), and SkillerExecutionThread::lua_restarted().

◆ push_thread()

void fawkes::LuaContext::push_thread ( )

Push thread on top of stack.

Definition at line 888 of file context.cpp.

References lock().

◆ push_usertype()

void fawkes::LuaContext::push_usertype ( void *  data,
const char *  type_name,
const char *  name_space = 0 
)

Push usertype on top of stack.

Parameters
datausertype data
type_nametype name of the data
name_spaceC++ namespace of type, prepended to type_name

Definition at line 925 of file context.cpp.

References lock(), and type_name().

Referenced by fawkes::LuaInterfaceImporter::add_interface(), SkillerNavGraphFeature::init_lua_context(), SkillerExecutionThread::lua_restarted(), and fawkes::LuaInterfaceImporter::write().

◆ push_value()

void fawkes::LuaContext::push_value ( int  idx)

Push a copy of the element at the given index on top of the stack.

Parameters
idxindex of the value to copy

Definition at line 899 of file context.cpp.

References lock().

◆ push_vfstring()

void fawkes::LuaContext::push_vfstring ( const char *  format,
va_list  arg 
)

Push formatted string on top of stack.

Parameters
formatstring format
argvariadic argument list
See also
man 3 sprintf

Definition at line 912 of file context.cpp.

References lock().

◆ raw_get()

void fawkes::LuaContext::raw_get ( int  idx)

Get value without invoking meta methods.

Similar to get_table(), but does raw access, i.e. without invoking meta-methods.

Parameters
idxindex of the table

Definition at line 1104 of file context.cpp.

◆ raw_geti()

void fawkes::LuaContext::raw_geti ( int  idx,
int  n 
)

Get indexed value without invoking meta methods.

Pushes t[n] onto the stack, where t is a table at index idx.

Parameters
idxindex of the table
nindex in the table

Definition at line 1116 of file context.cpp.

◆ raw_set()

void fawkes::LuaContext::raw_set ( int  idx)

Set value without invoking meta methods.

Similar to set_table(), but does raw access, i.e. without invoking meta-methods.

Parameters
idxindex of the table

Definition at line 1080 of file context.cpp.

◆ raw_seti()

void fawkes::LuaContext::raw_seti ( int  idx,
int  n 
)

Set indexed value without invoking meta methods.

Sets t[n]=v, where t is a table at index idx and v is the value at the top of the stack.

Parameters
idxindex of the table
nindex in the table

Definition at line 1093 of file context.cpp.

Referenced by fawkes::LuaInterfaceImporter::add_interface(), and fawkes::LuaInterfaceImporter::write().

◆ remove()

void fawkes::LuaContext::remove ( int  idx)

Remove value from stack.

Parameters
idxindex of element to remove

Definition at line 977 of file context.cpp.

References lock(), and stack_size().

◆ remove_global()

void fawkes::LuaContext::remove_global ( const char *  name)

Remove global variable.

Assigns nil to the given variable and removes it from internal assignment maps.

Parameters
namename of value to remove

Definition at line 1138 of file context.cpp.

References lock().

◆ remove_watcher()

void fawkes::LuaContext::remove_watcher ( fawkes::LuaContextWatcher watcher)

Remove a context watcher.

Parameters
watcherwatcher to remove

Definition at line 1406 of file context.cpp.

Referenced by SkillerExecutionThread::finalize(), and fawkes::LuaInterfaceImporter::~LuaInterfaceImporter().

◆ restart()

void fawkes::LuaContext::restart ( )

Restart Lua.

Creates a new Lua state, initializes it, anf if this went well the current state is swapped with the new state.

Definition at line 288 of file context.cpp.

References do_string(), lock(), fawkes::LibLogger::log_error(), fawkes::LibLogger::log_warn(), fawkes::MutexLocker::relock(), and fawkes::MutexLocker::unlock().

Referenced by LuaAgentContinuousExecutionThread::fam_event(), and fam_event().

◆ set_boolean()

void fawkes::LuaContext::set_boolean ( const char *  name,
bool  value 
)

Assign boolean to global variable.

Parameters
namename of global variable to assign the value to
valuevalue to assign

Definition at line 730 of file context.cpp.

References lock().

◆ set_cfunction()

void fawkes::LuaContext::set_cfunction ( const char *  name,
lua_CFunction  f 
)

Assign cfunction to global variable.

Parameters
namename of global variable to assign the value to
ffunction

Definition at line 781 of file context.cpp.

References lock().

Referenced by LuaAgentContinuousExecutionThread::init().

◆ set_field()

void fawkes::LuaContext::set_field ( const char *  key,
int  t_index = -2 
)

Set field of a table.

Does the equivalent to t[k] = v, where t is the value at the given valid index and v is the value at the top of the stack. This function pops the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex" event.

Parameters
keykey of the field to set
t_indexindex of the table on the stack, defaults to the element just below the value to set (-2, second last element on the stack).

Definition at line 1030 of file context.cpp.

Referenced by fawkes::LuaInterfaceImporter::add_interface(), and fawkes::LuaInterfaceImporter::write().

◆ set_finalization_calls()

void fawkes::LuaContext::set_finalization_calls ( std::string  finalize,
std::string  finalize_prepare,
std::string  finalize_cancel 
)

Set code to execute during finalization.

Parameters
finalizecode string to execute (via do_string()) when eventually finalizing a context
finalize_preparecode string to execute (via do_string()) before finalization is performed, for example during a context restart before the new context is initialized
finalize_cancelcode string to execute (via do_string()) if, during a restart, the initialization of the new context failed and therefore the previously prepared finalization must be cancelled

Definition at line 1423 of file context.cpp.

◆ set_global()

void fawkes::LuaContext::set_global ( const char *  name)

Set a global value.

Sets the global variable with the given name to the value currently on top of the stack. No check whatsoever regarding the name is done.

Parameters
namename of the variable to assign

Definition at line 1042 of file context.cpp.

Referenced by SkillerExecutionThread::lua_restarted(), and fawkes::LuaInterfaceImporter::write().

◆ set_integer()

void fawkes::LuaContext::set_integer ( const char *  name,
lua_Integer  value 
)

Assign integer to global variable.

Parameters
namename of global variable to assign the value to
valuevalue to assign

Definition at line 764 of file context.cpp.

References lock().

◆ set_number()

void fawkes::LuaContext::set_number ( const char *  name,
lua_Number  value 
)

Assign number to global variable.

Parameters
namename of global variable to assign the value to
valuevalue to assign

Definition at line 747 of file context.cpp.

References lock().

◆ set_start_script()

void fawkes::LuaContext::set_start_script ( const char *  start_script)

Set start script.

The script will be executed once immediately in this method, make sure you call this after all other init-relevant routines like add_* if you need to access these in the start script!

Parameters
start_scriptscript to execute now and on restart(). If the string is the path and name of an accessible file it is loaded via do_file(), otherwise it is considered to be the name of a module and loaded via Lua's require(). Note however, that if you use a module, special care has to be taken to correctly modify the global environment!

Definition at line 266 of file context.cpp.

References do_file(), and do_string().

Referenced by LuaAgentPeriodicExecutionThread::init(), and LuaAgentContinuousExecutionThread::init().

◆ set_string()

void fawkes::LuaContext::set_string ( const char *  name,
const char *  value 
)

Assign string to global variable.

Parameters
namename of global variable to assign the value to
valuevalue to assign

Definition at line 713 of file context.cpp.

References lock().

Referenced by LuaAgentPeriodicExecutionThread::init(), and LuaAgentContinuousExecutionThread::init().

◆ set_table()

void fawkes::LuaContext::set_table ( int  t_index = -3)

Set value of a table.

Sets value t[k] = v. t is the table at the given index, by default it is the third-last entry (index is -3). v is the value at the top of the stack, k is the element just below the top.

Parameters
t_indexindex of the table on the stack

Definition at line 1015 of file context.cpp.

Referenced by SkillerNavGraphFeature::finalize_lua_context(), SkillerNavGraphFeature::init_lua_context(), and SkillerExecutionThread::lua_restarted().

◆ set_usertype()

void fawkes::LuaContext::set_usertype ( const char *  name,
void *  data,
const char *  type_name,
const char *  name_space = 0 
)

Assign usertype to global variable.

Parameters
namename of global variable to assign the value to
datausertype data
type_nametype name of the data
name_spaceC++ namespace of type, prepended to type_name

Definition at line 689 of file context.cpp.

References lock(), and type_name().

Referenced by LuaAgentPeriodicExecutionThread::init(), and LuaAgentContinuousExecutionThread::init().

◆ setfenv()

void fawkes::LuaContext::setfenv ( int  idx = -2)

Set function environment.

Sets the table on top of the stack as environment of the function at the given stack index.

Parameters
idxstack index of function

Definition at line 1358 of file context.cpp.

◆ setup_fam()

void fawkes::LuaContext::setup_fam ( bool  auto_restart,
bool  conc_thread 
)

Setup file alteration monitor.

Setup an internal file alteration monitor that can react to changes on Lua files and packages.

Parameters
auto_restartautomatically restart the Lua context in case of an event
conc_threadtrue to run a concurrent thread for event processing. If and only if you set this to false, ensure that process_fam_events() periodically.

Definition at line 128 of file context.cpp.

References fawkes::Thread::start().

Referenced by LuaAgentPeriodicExecutionThread::init(), and LuaAgentContinuousExecutionThread::init().

◆ stack_size()

int fawkes::LuaContext::stack_size ( )

Get size of stack.

Returns
number of elements on the stack

Definition at line 991 of file context.cpp.

Referenced by pop(), and remove().

◆ table_next()

bool fawkes::LuaContext::table_next ( int  idx)

Iterate to next entry of table.

Parameters
idxstack index of table
Returns
true if there was another iterable value in the table, false otherwise

Definition at line 1160 of file context.cpp.

Referenced by SkillerExecutionThread::lua_restarted().

◆ to_boolean()

bool fawkes::LuaContext::to_boolean ( int  idx)

Retrieve stack value as boolean.

Parameters
idxstack index of value
Returns
value as boolean

Definition at line 1193 of file context.cpp.

◆ to_integer()

lua_Integer fawkes::LuaContext::to_integer ( int  idx)

Retrieve stack value as integer.

Parameters
idxstack index of value
Returns
value as integer

Definition at line 1182 of file context.cpp.

◆ to_number()

lua_Number fawkes::LuaContext::to_number ( int  idx)

Retrieve stack value as number.

Parameters
idxstack index of value
Returns
value as number

Definition at line 1171 of file context.cpp.

◆ to_pointer()

void * fawkes::LuaContext::to_pointer ( int  idx)

Retrieve stack value as pointer.

Parameters
idxstack index of value
Returns
value as pointer, maybe NULL

Definition at line 1225 of file context.cpp.

◆ to_string()

const char * fawkes::LuaContext::to_string ( int  idx)

Retrieve stack value as string.

Parameters
idxstack index of value
Returns
value as string

Definition at line 1204 of file context.cpp.

Referenced by SkillerExecutionThread::lua_restarted().

◆ to_userdata()

void * fawkes::LuaContext::to_userdata ( int  idx)

Retrieve stack value as userdata.

Parameters
idxstack index of value
Returns
value as userdata, maybe NULL

Definition at line 1214 of file context.cpp.

◆ to_usertype()

void * fawkes::LuaContext::to_usertype ( int  idx)

Retrieve stack value as a tolua++ user type.

Parameters
idxstack index of value
Returns
value as pointer, maybe NULL

Definition at line 1236 of file context.cpp.

Referenced by SkillerExecutionThread::lua_restarted().

◆ try_lock()

bool fawkes::LuaContext::try_lock ( )

Try to lock the Lua state.

Returns
true if the state has been locked, false otherwise.

Definition at line 418 of file context.cpp.

References fawkes::Mutex::try_lock().

◆ type_name()

std::string fawkes::LuaContext::type_name ( int  idx)

Get name of type of value at a given index.

Parameters
idxindex of value to get type for
Returns
name of type of the value at the given index

Definition at line 955 of file context.cpp.

Referenced by push_usertype(), and set_usertype().

◆ unlock()

void fawkes::LuaContext::unlock ( )

Unlock Lua state.

Definition at line 426 of file context.cpp.

References fawkes::Mutex::unlock().


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