[Add] Lua wrappers for luaL_newState and luaopen_base.

This commit is contained in:
Allanis 2013-06-29 17:58:01 +01:00
parent 185039ae62
commit d692035039
6 changed files with 35 additions and 9 deletions

View File

@ -262,7 +262,7 @@ static int ai_loadProfile(char* filename) {
strlen(filename)-strlen(AI_PREFIX)-strlen(AI_SUFFIX)+1, strlen(filename)-strlen(AI_PREFIX)-strlen(AI_SUFFIX)+1,
"%s", filename+strlen(AI_PREFIX)); "%s", filename+strlen(AI_PREFIX));
profiles[nprofiles-1].L = luaL_newstate(); profiles[nprofiles-1].L = llua_newState();
if(profiles[nprofiles-1].L == NULL) { if(profiles[nprofiles-1].L == NULL) {
ERR("Unable to create a new Lua state"); ERR("Unable to create a new Lua state");

View File

@ -3,9 +3,8 @@
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include "lua.h" #include "llua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lualib.h"
#include "lephisto.h" #include "lephisto.h"
#include "log.h" #include "log.h"
@ -97,7 +96,7 @@ int conf_loadConfig(const char* file) {
int i = 0; int i = 0;
double d = 0.; double d = 0.;
lua_State* L = luaL_newstate(); lua_State* L = llua_newState();
if(luaL_dofile(L, file) == 0) { if(luaL_dofile(L, file) == 0) {
/* Conf file exists indeed. */ /* Conf file exists indeed. */
/* Global. */ /* Global. */

View File

@ -64,6 +64,28 @@ static const luaL_reg tk_methods[] = {
{ 0, 0 } { 0, 0 }
}; };
/* Wrapper around luaL_newstate. */
lua_State* llua_newState(void) {
lua_State* L;
/* Try to create the new state. */
L = luaL_newstate();
if(L == NULL) {
WARN("Failed to create new lua state.");
return NULL;
}
return L;
}
/* Load a specially modified version of base. */
int llua_loadBase(lua_State* L) {
luaopen_base(L); /* Open base. */
/* replace package.loaders with custom one. */
return 0;
}
/* Individual libraries. */ /* Individual libraries. */
int lua_loadLephisto(lua_State* L) { int lua_loadLephisto(lua_State* L) {
luaL_register(L, "lephisto", lephisto_methods); luaL_register(L, "lephisto", lephisto_methods);

View File

@ -1,7 +1,11 @@
#pragma once #pragma once
#include "lua.h" #include "lua.h"
/* Individual libraries. */ /* Standard lua stuff wrappers. */
lua_State* llua_newState (void); /* Creates a new state */
int llua_loadBase(lua_State* L);
/* Individual custom libraries. */
int lua_loadLephisto(lua_State* L); /* Always read only. */ int lua_loadLephisto(lua_State* L); /* Always read only. */
int lua_loadSpace(lua_State* L, int readonly); int lua_loadSpace(lua_State* L, int readonly);
int lua_loadTime(lua_State* L, int readonly); int lua_loadTime(lua_State* L, int readonly);

View File

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
#include "llua.h"
#include "lluadef.h" #include "lluadef.h"
#include "rng.h" #include "rng.h"
@ -100,13 +101,13 @@ static int mission_init(Mission* mission, MissionData* misn, int load) {
mission->data = misn; mission->data = misn;
/* Init lua. */ /* Init lua. */
mission->L = luaL_newstate(); mission->L = llua_newState();
if(mission->L == NULL) { if(mission->L == NULL) {
ERR("Unable to create a new lua state."); ERR("Unable to create a new lua state.");
return -1; return -1;
} }
luaopen_base(mission->L); /* Can be useful. */ llua_loadBase(mission->L); /* Pairs and such. */
luaopen_string(mission->L); /* string.format can be very useful. */ luaopen_string(mission->L); /* string.format can be very useful. */
misn_loadLibs(mission->L); /* Load our custom libraries. */ misn_loadLibs(mission->L); /* Load our custom libraries. */
@ -152,7 +153,7 @@ static int mission_meetCond(MissionData* misn) {
if(mission_cond_L == NULL) { if(mission_cond_L == NULL) {
/* Must create the conditional environment. */ /* Must create the conditional environment. */
mission_cond_L = luaL_newstate(); mission_cond_L = llua_newState();
misn_loadCondLibs(mission_cond_L); misn_loadCondLibs(mission_cond_L);
} }

View File

@ -371,7 +371,7 @@ static int music_luaInit(void) {
if(music_lua != NULL) if(music_lua != NULL)
music_luaQuit(); music_luaQuit();
music_lua = luaL_newstate(); music_lua = llua_newState();
/*luaL_openlibs(music_lua); */ /*luaL_openlibs(music_lua); */