naev 0.11.5
nlua.c File Reference

Handles creating and setting up basic Lua environments. More...

#include "nlua.h"
#include "log.h"
#include "conf.h"
#include "debug.h"
#include "lua_enet.h"
#include "lutf8lib.h"
#include "ndata.h"
#include "nfile.h"
#include "nlua_cli.h"
#include "nlua_audio.h"
#include "nlua_commodity.h"
#include "nlua_data.h"
#include "nlua_diff.h"
#include "nlua_faction.h"
#include "nlua_file.h"
#include "nlua_jump.h"
#include "nlua_linopt.h"
#include "nlua_naev.h"
#include "nlua_news.h"
#include "nlua_outfit.h"
#include "nlua_pilot.h"
#include "nlua_spob.h"
#include "nlua_player.h"
#include "nlua_rnd.h"
#include "nlua_safelanes.h"
#include "nlua_spfx.h"
#include "nlua_shiplog.h"
#include "nlua_system.h"
#include "nlua_time.h"
#include "nlua_var.h"
#include "nlua_vec2.h"
#include "nluadef.h"
#include "nstring.h"

Go to the source code of this file.

Functions

static int nlua_package_loader_lua (lua_State *L)
 load( string module ) – searcher function to replace package.loaders[2] (Lua 5.1), i.e., for Lua modules.
 
static int nlua_package_loader_c (lua_State *L)
 load( string module ) – searcher function to replace package.loaders[3] (Lua 5.1), i.e., for C modules.
 
static int nlua_package_loader_croot (lua_State *L)
 load( string module ) – searcher function to replace package.loaders[4] (Lua 5.1), i.e., for C packages.
 
static int nlua_require (lua_State *L)
 include( string module )
 
static lua_State * nlua_newState (void)
 Wrapper around luaL_newstate.
 
static int nlua_loadBasic (lua_State *L)
 Loads specially modified basic stuff.
 
static int luaB_loadstring (lua_State *L)
 Replacement for the internal Lua loadstring().
 
static int nlua_gettext (lua_State *L)
 gettext support.
 
static int nlua_ngettext (lua_State *L)
 gettext support for singular and plurals.
 
static int nlua_pgettext (lua_State *L)
 gettext support with context.
 
static int nlua_gettext_noop (lua_State *L)
 gettext support (noop). Does not actually do anything, but gets detected by gettext.
 
static int nlua_log2 (lua_State *L)
 Implements the Lua function math.log2 (base-2 logarithm).
 
static int nlua_os_getenv (lua_State *L)
 Implements the Lua function os.getenv. In the sandbox we only make a fake $HOME visible.
 
static int nlua_panic (lua_State *L)
 Handles what to do when Lua panics.
 
void lua_init (void)
 
void lua_exit (void)
 
int nlua_dobufenv (nlua_env env, const char *buff, size_t sz, const char *name)
 
int nlua_dofileenv (nlua_env env, const char *filename)
 
int nlua_dochunkenv (nlua_env env, int chunk, const char *name)
 
nlua_env nlua_newEnv (void)
 
void nlua_freeEnv (nlua_env env)
 
void nlua_pushenv (lua_State *L, nlua_env env)
 
void nlua_getenv (lua_State *L, nlua_env env, const char *name)
 
void nlua_setenv (lua_State *L, nlua_env env, const char *name)
 
void nlua_register (nlua_env env, const char *libname, const luaL_Reg *l, int metatable)
 
int nlua_loadStandard (nlua_env env)
 Loads the standard Naev Lua API.
 
int nlua_errTrace (lua_State *L)
 Gets a trace from Lua.
 
int nlua_pcall (nlua_env env, int nargs, int nresults)
 
int nlua_refenv (nlua_env env, const char *name)
 Gets the reference of a global in a lua environment.
 
int nlua_refenvtype (nlua_env env, const char *name, int type)
 Gets the reference of a global in a lua environment if it matches a type.
 
int nlua_reffield (int objref, const char *name)
 Gets the reference to the specified field from an object reference.
 
int nlua_ref (lua_State *L, int idx)
 Creates a new reference to a Lua structure at a position.
 
void nlua_unref (lua_State *L, int idx)
 Removes a reference set with nlua_ref.
 
void nlua_resize (void)
 Propagates a resize event to all the environments forcibly.
 

Variables

lua_State * naevL = NULL
 
nlua_env __NLUA_CURENV = LUA_NOREF
 
static char * common_script
 
static size_t common_sz
 
static int nlua_envs = LUA_NOREF
 
static const luaL_Reg gettext_methods []
 

Detailed Description

Handles creating and setting up basic Lua environments.

Definition in file nlua.c.

Function Documentation

◆ lua_exit()

void lua_exit ( void )

Definition at line 214 of file nlua.c.

◆ lua_init()

void lua_init ( void )

Definition at line 180 of file nlua.c.

◆ luaB_loadstring()

static int luaB_loadstring ( lua_State * L)
static

Replacement for the internal Lua loadstring().

Definition at line 196 of file nlua.c.

◆ nlua_dobufenv()

int nlua_dobufenv ( nlua_env env,
const char * buff,
size_t sz,
const char * name )

Definition at line 230 of file nlua.c.

◆ nlua_dochunkenv()

int nlua_dochunkenv ( nlua_env env,
int chunk,
const char * name )

Definition at line 287 of file nlua.c.

◆ nlua_dofileenv()

int nlua_dofileenv ( nlua_env env,
const char * filename )

Definition at line 269 of file nlua.c.

◆ nlua_errTrace()

int nlua_errTrace ( lua_State * L)

Gets a trace from Lua.

Definition at line 830 of file nlua.c.

◆ nlua_freeEnv()

void nlua_freeEnv ( nlua_env env)

Definition at line 400 of file nlua.c.

◆ nlua_getenv()

void nlua_getenv ( lua_State * L,
nlua_env env,
const char * name )

Definition at line 436 of file nlua.c.

◆ nlua_gettext()

static int nlua_gettext ( lua_State * L)
static

gettext support.

Lua usage parameter: _( str ) Lua function parameter: str String to gettext on. Lua return parameter: The string converted to gettext.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: gettext

Definition at line 88 of file nlua.c.

◆ nlua_gettext_noop()

static int nlua_gettext_noop ( lua_State * L)
static

gettext support (noop). Does not actually do anything, but gets detected by gettext.

Lua usage parameter: N_( str ) Lua function parameter: str String to gettext on. Lua return parameter: The string converted to gettext.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: gettext_noop

Definition at line 139 of file nlua.c.

◆ nlua_loadBasic()

static int nlua_loadBasic ( lua_State * L)
static

Loads specially modified basic stuff.

Parameters
LLua State to load the basic stuff into.
Returns
0 on success.

Definition at line 509 of file nlua.c.

◆ nlua_loadStandard()

int nlua_loadStandard ( nlua_env env)

Loads the standard Naev Lua API.

Loads the modules:

  • naev
  • var
  • space
    • spob
    • system
    • jumps
  • time
  • player
  • pilot
  • rnd
  • diff
  • faction
  • vec2
  • outfit
  • commodity

Only is missing:

  • misn
  • tk
  • hook
  • music
  • ai
Parameters
envEnvironment.
Returns
0 on success.

Definition at line 798 of file nlua.c.

◆ nlua_log2()

static int nlua_log2 ( lua_State * L)
static

Implements the Lua function math.log2 (base-2 logarithm).

Definition at line 147 of file nlua.c.

◆ nlua_newEnv()

nlua_env nlua_newEnv ( void )

Definition at line 316 of file nlua.c.

◆ nlua_newState()

static lua_State * nlua_newState ( void )
static

Wrapper around luaL_newstate.

Returns
A newly created lua_State.

Definition at line 492 of file nlua.c.

◆ nlua_ngettext()

static int nlua_ngettext ( lua_State * L)
static

gettext support for singular and plurals.

Lua usage parameter: ngettext( msgid1, msgid2, n ) Lua function parameter: msgid1 Singular form. Lua function parameter: msgid2 Plural form. Lua function parameter: n Number of elements. Lua return parameter: The string converted to gettext.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: ngettext

Definition at line 105 of file nlua.c.

◆ nlua_os_getenv()

static int nlua_os_getenv ( lua_State * L)
static

Implements the Lua function os.getenv. In the sandbox we only make a fake $HOME visible.

Definition at line 157 of file nlua.c.

◆ nlua_package_loader_c()

static int nlua_package_loader_c ( lua_State * L)
static

load( string module ) – searcher function to replace package.loaders[3] (Lua 5.1), i.e., for C modules.

Parameters
LLua Environment.
Returns
Stack depth (1), and on the stack: a loader function, a string explaining there is none, or nil (no explanation).

Definition at line 655 of file nlua.c.

◆ nlua_package_loader_croot()

static int nlua_package_loader_croot ( lua_State * L)
static

load( string module ) – searcher function to replace package.loaders[4] (Lua 5.1), i.e., for C packages.

Parameters
LLua Environment.
Returns
Stack depth (1), and on the stack: a loader function, a string explaining there is none, or nil (no explanation).

Definition at line 674 of file nlua.c.

◆ nlua_package_loader_lua()

static int nlua_package_loader_lua ( lua_State * L)
static

load( string module ) – searcher function to replace package.loaders[2] (Lua 5.1), i.e., for Lua modules.

Parameters
LLua Environment.
Returns
Stack depth (1), and on the stack: a loader function, a string explaining there is none, or nil (no explanation).

Definition at line 574 of file nlua.c.

◆ nlua_panic()

static int nlua_panic ( lua_State * L)
static

Handles what to do when Lua panics.

By default it uses exit( EXIT_FAILURE );, but we want to generate a backtrace or let gdb catch it if possible.

Definition at line 171 of file nlua.c.

◆ nlua_pcall()

int nlua_pcall ( nlua_env env,
int nargs,
int nresults )

Definition at line 861 of file nlua.c.

◆ nlua_pgettext()

static int nlua_pgettext ( lua_State * L)
static

gettext support with context.

Lua usage parameter: pgettext( context, msg ) Lua function parameter: context Context of the message. Lua function parameter: msg Message to translate. Lua return parameter: The string converted to gettext.

Parameters
LLua State
Returns
Number of variables on the Lua State stack.

Lua function: pgettext

Definition at line 123 of file nlua.c.

◆ nlua_pushenv()

void nlua_pushenv ( lua_State * L,
nlua_env env )

Definition at line 423 of file nlua.c.

◆ nlua_ref()

int nlua_ref ( lua_State * L,
int idx )

Creates a new reference to a Lua structure at a position.

Definition at line 952 of file nlua.c.

◆ nlua_refenv()

int nlua_refenv ( nlua_env env,
const char * name )

Gets the reference of a global in a lua environment.

Parameters
envEnvironment.
nameName of the global to get.
Returns
LUA_NOREF if no global found, reference otherwise.

Definition at line 903 of file nlua.c.

◆ nlua_refenvtype()

int nlua_refenvtype ( nlua_env env,
const char * name,
int type )

Gets the reference of a global in a lua environment if it matches a type.

Parameters
envEnvironment.
nameName of the global to get.
typeType to match, e.g., LUA_TFUNCTION.
Returns
LUA_NOREF if no global found, reference otherwise.

Definition at line 920 of file nlua.c.

◆ nlua_reffield()

int nlua_reffield ( int objref,
const char * name )

Gets the reference to the specified field from an object reference.

Parameters
objrefReference to the object to be indexed.
nameName of the field to get.
Returns
LUA_NOREF if no field found, reference otherwise.

Definition at line 936 of file nlua.c.

◆ nlua_register()

void nlua_register ( nlua_env env,
const char * libname,
const luaL_Reg * l,
int metatable )

Definition at line 470 of file nlua.c.

◆ nlua_require()

static int nlua_require ( lua_State * L)
static

include( string module )

Loads a module into the current Lua state from inside the data file.

Parameters
LLua Environment to load modules into.
Returns
The return value of the chunk, or true.

Definition at line 688 of file nlua.c.

◆ nlua_resize()

void nlua_resize ( void )

Propagates a resize event to all the environments forcibly.

Definition at line 970 of file nlua.c.

◆ nlua_setenv()

void nlua_setenv ( lua_State * L,
nlua_env env,
const char * name )

Definition at line 451 of file nlua.c.

◆ nlua_unref()

void nlua_unref ( lua_State * L,
int idx )

Removes a reference set with nlua_ref.

Definition at line 961 of file nlua.c.

Variable Documentation

◆ __NLUA_CURENV

nlua_env __NLUA_CURENV = LUA_NOREF

Definition at line 52 of file nlua.c.

◆ common_script

char* common_script
static

Common script to run when creating environments.

Definition at line 53 of file nlua.c.

◆ common_sz

size_t common_sz
static

Common script size.

Definition at line 54 of file nlua.c.

◆ gettext_methods

const luaL_Reg gettext_methods[]
static
Initial value:
= {
{ "gettext", nlua_gettext },
{ "ngettext", nlua_ngettext },
{ "pgettext", nlua_pgettext },
{ "gettext_noop", nlua_gettext_noop },
{0,0}
}
static int nlua_pgettext(lua_State *L)
gettext support with context.
Definition nlua.c:123
static int nlua_gettext_noop(lua_State *L)
gettext support (noop). Does not actually do anything, but gets detected by gettext.
Definition nlua.c:139
static int nlua_gettext(lua_State *L)
gettext support.
Definition nlua.c:88
static int nlua_ngettext(lua_State *L)
gettext support for singular and plurals.
Definition nlua.c:105

Vector metatable methods.

Definition at line 72 of file nlua.c.

◆ naevL

lua_State* naevL = NULL

Definition at line 51 of file nlua.c.

◆ nlua_envs

int nlua_envs = LUA_NOREF
static

Definition at line 55 of file nlua.c.