naev 0.12.6
nlua.c File Reference

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

#include "nlua.h"
#include "array.h"
#include "cmark_wrap.h"
#include "conf.h"
#include "console.h"
#include "debug.h"
#include "log.h"
#include "lua_enet.h"
#include "lutf8lib.h"
#include "lyaml.h"
#include "ndata.h"
#include "nlua_audio.h"
#include "nlua_cli.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_player.h"
#include "nlua_rnd.h"
#include "nlua_safelanes.h"
#include "nlua_shiplog.h"
#include "nlua_spfx.h"
#include "nlua_spob.h"
#include "nlua_system.h"
#include "nlua_time.h"
#include "nlua_var.h"
#include "nlua_vec2.h"
#include "nluadef.h"
#include "nstring.h"
Include dependency graph for nlua.c:

Go to the source code of this file.

Data Structures

struct  LuaCache_t
 Cache structure for loading chunks. More...

Functions

static int nlua_package_preload (lua_State *L)
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 lua_cache_cmp (const void *p1, const void *p2)
 Compares two Lua caches.
static int nlua_errTraceInternal (lua_State *L, int idx)
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_warn (lua_State *L, int idx)
void lua_clearCache (void)
 Clears the cached stuff.
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 (const char *name)
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.
int nlua_helperTags (lua_State *L, int idx, char *const *tags)
 Helper function to deal with tags.

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 LuaCache_tlua_cache = NULL
static const luaL_Reg gettext_methods []
static const lua_CFunction loaders []

Detailed Description

Handles creating and setting up basic Lua environments.

Definition in file nlua.c.

Function Documentation

◆ lua_cache_cmp()

int lua_cache_cmp ( const void * p1,
const void * p2 )
static

Compares two Lua caches.

Definition at line 623 of file nlua.c.

◆ lua_clearCache()

void lua_clearCache ( void )

Clears the cached stuff.

Definition at line 269 of file nlua.c.

◆ lua_exit()

void lua_exit ( void )

Definition at line 240 of file nlua.c.

◆ lua_init()

void lua_init ( void )

Definition at line 203 of file nlua.c.

◆ luaB_loadstring()

int luaB_loadstring ( lua_State * L)
static

Replacement for the internal Lua loadstring().

Definition at line 222 of file nlua.c.

◆ nlua_dobufenv()

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

Definition at line 289 of file nlua.c.

◆ nlua_dochunkenv()

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

Definition at line 335 of file nlua.c.

◆ nlua_dofileenv()

int nlua_dofileenv ( nlua_env env,
const char * filename )

Definition at line 319 of file nlua.c.

◆ nlua_errTrace()

int nlua_errTrace ( lua_State * L)

Gets a trace from Lua.

Definition at line 946 of file nlua.c.

◆ nlua_errTraceInternal()

int nlua_errTraceInternal ( lua_State * L,
int idx )
static

Definition at line 951 of file nlua.c.

◆ nlua_freeEnv()

void nlua_freeEnv ( nlua_env env)

Definition at line 458 of file nlua.c.

◆ nlua_getenv()

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

Definition at line 493 of file nlua.c.

◆ nlua_gettext()

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 108 of file nlua.c.

◆ nlua_gettext_noop()

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 160 of file nlua.c.

◆ nlua_helperTags()

int nlua_helperTags ( lua_State * L,
int idx,
char *const * tags )

Helper function to deal with tags.

Definition at line 1118 of file nlua.c.

◆ nlua_loadBasic()

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 566 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 914 of file nlua.c.

◆ nlua_log2()

int nlua_log2 ( lua_State * L)
static

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

Definition at line 168 of file nlua.c.

◆ nlua_newEnv()

nlua_env nlua_newEnv ( const char * name)

Definition at line 362 of file nlua.c.

◆ nlua_newState()

lua_State * nlua_newState ( void )
static

Wrapper around luaL_newstate.

Returns
A newly created lua_State.

Definition at line 549 of file nlua.c.

◆ nlua_ngettext()

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 125 of file nlua.c.

◆ nlua_os_getenv()

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 179 of file nlua.c.

◆ nlua_package_loader_c()

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 768 of file nlua.c.

◆ nlua_package_loader_croot()

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 793 of file nlua.c.

◆ nlua_package_loader_lua()

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 659 of file nlua.c.

◆ nlua_package_preload()

int nlua_package_preload ( lua_State * L)
static

Definition at line 630 of file nlua.c.

◆ nlua_panic()

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 194 of file nlua.c.

◆ nlua_pcall()

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

Definition at line 985 of file nlua.c.

◆ nlua_pgettext()

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 143 of file nlua.c.

◆ nlua_pushenv()

void nlua_pushenv ( lua_State * L,
nlua_env env )

Definition at line 480 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 1079 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 1029 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 1047 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 1063 of file nlua.c.

◆ nlua_register()

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

Definition at line 527 of file nlua.c.

◆ nlua_require()

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 807 of file nlua.c.

◆ nlua_resize()

void nlua_resize ( void )

Propagates a resize event to all the environments forcibly.

Definition at line 1097 of file nlua.c.

◆ nlua_setenv()

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

Definition at line 508 of file nlua.c.

◆ nlua_unref()

void nlua_unref ( lua_State * L,
int idx )

Removes a reference set with nlua_ref.

Definition at line 1088 of file nlua.c.

◆ nlua_warn()

int nlua_warn ( lua_State * L,
int idx )

Definition at line 251 of file nlua.c.

Variable Documentation

◆ __NLUA_CURENV

nlua_env __NLUA_CURENV = LUA_NOREF

Current environment.

Definition at line 55 of file nlua.c.

◆ common_script

char* common_script
static

Common script to run when creating environments.

Definition at line 57 of file nlua.c.

◆ common_sz

size_t common_sz
static

Common script size.

Definition at line 58 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:143
static int nlua_gettext_noop(lua_State *L)
gettext support (noop). Does not actually do anything, but gets detected by gettext.
Definition nlua.c:160
static int nlua_gettext(lua_State *L)
gettext support.
Definition nlua.c:108
static int nlua_ngettext(lua_State *L)
gettext support for singular and plurals.
Definition nlua.c:125

Vector metatable methods.

Definition at line 89 of file nlua.c.

◆ loaders

const lua_CFunction loaders[]
static
Initial value:
= {
static int nlua_package_loader_croot(lua_State *L)
load( string module ) – searcher function to replace package.loaders[4] (Lua 5.1),...
Definition nlua.c:793
static int nlua_package_loader_lua(lua_State *L)
load( string module ) – searcher function to replace package.loaders[2] (Lua 5.1),...
Definition nlua.c:659
static int nlua_package_loader_c(lua_State *L)
load( string module ) – searcher function to replace package.loaders[3] (Lua 5.1),...
Definition nlua.c:768

Our loaders.

Definition at line 96 of file nlua.c.

◆ lua_cache

LuaCache_t* lua_cache = NULL
static

Definition at line 68 of file nlua.c.

◆ naevL

lua_State* naevL = NULL

Global Naev Lua state.

Definition at line 54 of file nlua.c.

◆ nlua_envs

int nlua_envs = LUA_NOREF
static

Definition at line 59 of file nlua.c.