Example #1
0
LUALIB_API int luaopen_package(lua_State *L)
{
  int i;
  int noenv;
  luaL_newmetatable(L, "_LOADLIB");
  lj_lib_pushcf(L, lj_cf_package_unloadlib, 1);
  lua_setfield(L, -2, "__gc");
  luaL_register(L, LUA_LOADLIBNAME, package_lib);
  lua_pushvalue(L, -1);
  lua_replace(L, LUA_ENVIRONINDEX);
  lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0);
  for (i = 0; package_loaders[i] != NULL; i++) {
    lj_lib_pushcf(L, package_loaders[i], 1);
    lua_rawseti(L, -2, i+1);
  }
  lua_setfield(L, -2, "loaders");
  lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
  noenv = lua_toboolean(L, -1);
  lua_pop(L, 1);
  setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv);
  setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv);
  lua_pushliteral(L, LUA_PATH_CONFIG);
  lua_setfield(L, -2, "config");
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
  lua_setfield(L, -2, "loaded");
  luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
  lua_setfield(L, -2, "preload");
  lua_pushvalue(L, LUA_GLOBALSINDEX);
  luaL_register(L, NULL, package_global);
  lua_pop(L, 1);
  return 1;
}
Example #2
0
LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
                              const luaL_Reg *l, int nup) {
  if (libname) {
    int size = libsize(l);
    /* check whether lib already exists */
    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", size);
    lua_getfield(L, -1, libname);  /* get _LOADED[libname] */
    if (!lua_istable(L, -1)) {  /* not found? */
      lua_pop(L, 1);  /* remove previous result */
      /* try global variable (and create one if it does not exist) */
      if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
        luaL_error(L, "name conflict for module " LUA_QS, libname);
      lua_pushvalue(L, -1);
      lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */
    }
    lua_remove(L, -2);  /* remove _LOADED table */
    lua_insert(L, -(nup+1));  /* move library table to below upvalues */
  }
  for (; l->name; l++) {
    int i;
    for (i=0; i<nup; i++)  /* copy upvalues to the top */
      lua_pushvalue(L, -nup);
    lua_pushcclosure(L, l->func, nup);
    lua_setfield(L, -(nup+2), l->name);
  }
  lua_pop(L, nup);  /* remove upvalues */
}
Example #3
0
void luaL_pushmodule (lua_State *L, const char *modname, int sizehint) {
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);  /* get _LOADED table */
  if (lua_getfield(L, -1, modname) != LUA_TTABLE) {  /* no _LOADED[modname]? */
    lua_pop(L, 1);  /* remove previous result */
    /* try global variable (and create one if it does not exist) */
    lua_pushglobaltable(L);
    if (luaL_findtable(L, 0, modname, sizehint) != NULL)
      luaL_error(L, "name conflict for module '%s'", modname);
    lua_pushvalue(L, -1);
    lua_setfield(L, -3, modname);  /* _LOADED[modname] = new table */
  }
  lua_remove(L, -2);  /* remove _LOADED table */
}
Example #4
0
void lutok::state::findLib(const std::string& name, const int size, const int nup){
	const char * libname = name.c_str();

	luaL_findtable(_pimpl->lua_state, LUA_REGISTRYINDEX, "_LOADED", 1);
    lua_getfield(_pimpl->lua_state, -1, libname);  /* get _LOADED[libname] */
    if (!lua_istable(_pimpl->lua_state, -1)) {  /* not found? */
      lua_pop(_pimpl->lua_state, 1);  /* remove previous result */
      /* try global variable (and create one if it does not exist) */
	  if (luaL_findtable(_pimpl->lua_state, LUA_GLOBALSINDEX, libname, size ) != NULL)
        luaL_error(_pimpl->lua_state, "name conflict for module " LUA_QS, libname);
      lua_pushvalue(_pimpl->lua_state, -1);
      lua_setfield(_pimpl->lua_state, -3, libname);  /* _LOADED[libname] = new table */
    }
    lua_remove(_pimpl->lua_state, -2);  /* remove _LOADED table */
    lua_insert(_pimpl->lua_state, -(nup+1));  /* move library table to below upvalues */
}
Example #5
0
void
box_lua_tuple_init(struct lua_State *L)
{
	/* export C functions to Lua */
	luaL_findtable(L, LUA_GLOBALSINDEX, "box.internal", 1);
	luaL_newmetatable(L, tuplelib_name);
	luaL_register(L, NULL, lbox_tuple_meta);
	lua_setfield(L, -2, "tuple");
	lua_pop(L, 1); /* box.internal */
	luaL_register_type(L, tuple_iteratorlib_name,
			   lbox_tuple_iterator_meta);
	luaL_register_module(L, tuplelib_name, lbox_tuplelib);
	lua_pop(L, 1);

	luamp_set_encode_extension(luamp_encode_extension_box);

	/*
	 * Create special serializer for box.tuple.new().
	 * Disable storage optimization for excessively
	 * sparse arrays as a tuple always must be regular
	 * MP_ARRAY.
	 */
	luaL_serializer_create(&tuple_serializer);
	tuple_serializer.encode_sparse_ratio = 0;

	/* Get CTypeID for `struct tuple' */
	int rc = luaL_cdef(L, "struct tuple;");
	assert(rc == 0);
	(void) rc;
	CTID_STRUCT_TUPLE_REF = luaL_ctypeid(L, "struct tuple &");
	assert(CTID_STRUCT_TUPLE_REF != 0);
}
Example #6
0
int luaopen_stagedb_core( lua_State *L) {


    /* Register C functions. */
    luaL_findtable( L, LUA_GLOBALSINDEX, "stagedb.core", 8); // stagedb.core
#define REG(x) lua_pushcfunction( L, api_##x); lua_setfield( L, -2, #x)
    REG( init);
    REG( newconsolidation);
    REG( row);
    REG( consolidate);
    REG( serialize);
    REG( serialize_cancel);
    REG( reset);
    REG( close);
    REG( state);
    REG( trim);
#undef REG

    lua_newtable(   L); // mt
    lua_pushstring( L, MT_NAME); // stagedb.core, mt, name
    lua_setfield(   L, -2, "__type"); // stagedb.core, mt
    lua_pushvalue(  L, -2); // stagedb.core, mt, stagedb.core
    lua_setfield(   L, -2, "__index"); // stagedb.core, mt
    /* Register cleanup upon garbage collection. */
    lua_pushcfunction( L, api_close); // stagedb.core, mt, api_close
    lua_setfield( L, -2, "__gc"); // stagedb.core, mt
    lua_setfield(   L, LUA_REGISTRYINDEX, MT_NAME); // stagedb.core
    return 1;
}
Example #7
0
static int ll_module (lua_State *L) {
  const char* modname = luaL_checkstring(L, 1);
  int loaded = lua_gettop(L) + 1;  /* index of _LOADED table */
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  lua_getfield(L, loaded, modname);  /* get _LOADED[modname] */
  if (!lua_istable(L, -1)) {  /* not found? */
    lua_pop(L, 1);  /* remove previous result */
    /* try global variable (and create one if it does not exist) */
    if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != nullptr)
      return luaL_error(L, "name conflict for module " LUA_QS, modname);
    lua_pushvalue(L, -1);
    lua_setfield(L, loaded, modname);  /* _LOADED[modname] = new table */
  }
  /* check whether table already has a _NAME field */
  lua_getfield(L, -1, "_NAME");
  if (!lua_isnil(L, -1))  /* is table an initialized module? */
    lua_pop(L, 1);
  else {  /* no; initialize it */
    lua_pop(L, 1);
    modinit(L, modname);
  }
  lua_pushvalue(L, -1);
  setfenv(L);
  dooptions(L, loaded - 1);
  return 0;
}
Example #8
0
LUAPRELOAD_API int luapreload_fullluasocket(lua_State *L) {
	luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", 3);
	
	lua_pushcfunction(L, luaopen_socket_core);
	lua_setfield(L, -2, "socket.core");
	lua_pushcfunction(L, luaopen_mime_core);
	lua_setfield(L, -2, "mime.core");
	lua_pushcfunction(L, luaopen_ltn12);
	lua_setfield(L, -2, "ltn12");
	lua_pushcfunction(L, luaopen_mime);
	lua_setfield(L, -2, "mime");
	lua_pushcfunction(L, luaopen_socket);
	lua_setfield(L, -2, "socket");
	lua_pushcfunction(L, luaopen_socket_ftp);
	lua_setfield(L, -2, "socket.ftp");
	lua_pushcfunction(L, luaopen_socket_http);
	lua_setfield(L, -2, "socket.http");
	lua_pushcfunction(L, luaopen_socket_smtp);
	lua_setfield(L, -2, "socket.smtp");
	lua_pushcfunction(L, luaopen_socket_tp);
	lua_setfield(L, -2, "socket.tp");
	lua_pushcfunction(L, luaopen_socket_url);
	lua_setfield(L, -2, "socket.url");
	
	lua_pop(L, 1);
	return 0;
}
Example #9
0
mrp_funcbridge_t *mrp_funcbridge_create_cfunc(lua_State *L, const char *name,
                                              const char *signature,
                                              mrp_funcbridge_cfunc_t func,
                                              void *data)
{
    int builtin, top;
    mrp_funcbridge_t *fb;

    top = lua_gettop(L);

    if (luaL_findtable(L, LUA_GLOBALSINDEX, "builtin.method", 20))
        fb = NULL;
    else {
        builtin = lua_gettop(L);

        fb = create_funcbridge(L, 0, 1);

        fb->type = MRP_C_FUNCTION;
        fb->c.signature = strdup(signature);
        fb->c.func = func;
        fb->c.data = data;

        lua_pushstring(L, name);
        lua_pushvalue(L, -2);

        lua_rawset(L, builtin);

        lua_settop(L, top);
    }

    return fb;
}
Example #10
0
void PreloadAdditionalModules(lua_State* L) {
	luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", 1);
	//int preload = lua_gettop(L);

	lua_pushcfunction(L, luaopen_StackTracePlus);
	lua_setfield(L, -2, "stacktraceplus");

	lua_pop(L, 1);
}
static void add_ud_module(lsb_lua_sandbox *sb)
{
  lua_State *lua = lsb_get_lua(sb);
  luaL_findtable(lua, LUA_REGISTRYINDEX, "_PRELOADED", 1);
  lua_pushstring(lua, "ud");
  lua_pushcfunction(lua, luaopen_ud);
  lua_rawset(lua, -3);
  lua_pop(lua, 1); // remove the preloaded table
}
Example #12
0
void lj_lib_prereg(lua_State *L, const char *name, lua_CFunction f, GCtab *env)
{
  luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4);
  lua_pushcfunction(L, f);
  /* NOBARRIER: The function is new (marked white). */
  setgcref(funcV(L->top-1)->c.env, obj2gco(env));
  lua_setfield(L, -2, name);
  L->top--;
}
Example #13
0
JNIEXPORT jstring JNICALL Java_m_lua_Lua_LfindTable
		(JNIEnv* env, jobject thiz, jlong nativeObj, jint idx, jstring fname, jint szhint) {
	const char* name = (*env)->GetStringUTFChars(env, fname, JNI_FALSE);
	const char* ret = luaL_findtable((lua_State*) nativeObj, idx, name, szhint);
	(*env)->ReleaseStringUTFChars(env, fname, name);
	if (ret == 0) {
		return 0;
	}
	return (*env)->NewStringUTF(env, ret);
}
Example #14
0
static int findtable (lua_State *L) {
  if (lua_gettop(L)==1){
    lua_pushglobaltable(L);
    lua_insert(L, 1);
  }
  luaL_checktype(L, 1, LUA_TTABLE);
  const char *name = luaL_checklstring(L, 2, 0);
  luaL_findtable(L, 1, name, 0);
  return 1;
}
Example #15
0
void luax_initpreload(lua_State *L)
{
    luaL_Reg* lib = luax_preload_list;
    luaL_findtable(L, LUA_GLOBALSINDEX, "package.preload", sizeof(luax_preload_list)/sizeof(luax_preload_list[0])-1);
    for (; lib->func; lib++) {
        lua_pushstring(L, lib->name);
        lua_pushcfunction(L, lib->func);
        lua_rawset(L, -3);
    }
    lua_pop(L, 1);
}
Example #16
0
File: gluac.c Project: gooops/glua
// 获取GlibTable,压入栈底
int GetGlibTable(lua_State* L, const char* libname, int lsize)
{
	/* check whether lib already exists */
	luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
	lua_getfield(L, -1, libname);  /* get _LOADED[libname] */
	if (!lua_istable(L, -1)) 
	{  
		/* not found? */
		lua_pop(L, 1);  /* remove previous result */
		/* try global variable (and create one if it does not exist) */
		if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, lsize) != NULL)
		{
			return -1;
		}
		lua_pushvalue(L, -1);
		lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */
	}
	lua_remove(L, -2);  /* remove _LOADED table */
	return 0;
}
Example #17
0
static GCtab *lib_create_table(lua_State *L, const char *libname, int hsize)
{
  if (libname) {
    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
    lua_getfield(L, -1, libname);
    if (!tvistab(L->top-1)) {
      L->top--;
      if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, hsize) != NULL)
	lj_err_callerv(L, LJ_ERR_BADMODN, libname);
      settabV(L, L->top, tabV(L->top-1));
      L->top++;
      lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */
    }
    L->top--;
    settabV(L, L->top-1, tabV(L->top));
  } else {
    lua_createtable(L, 0, hsize);
  }
  return tabV(L->top-1);
}
Example #18
0
static void preload_modules(lua_State *lua)
{
  const luaL_Reg *lib = preload_module_list;
  luaL_findtable(lua, LUA_REGISTRYINDEX, "_PRELOADED",
                 libsize(preload_module_list));
  for (; lib->func; lib++) {
    lua_pushstring(lua, lib->name);
    lua_pushcfunction(lua, lib->func);
    lua_rawset(lua, -3);
  }
  lua_pop(lua, 1); // remove the preloaded table
}
Example #19
0
LUALIB_API int luaopen_package (lua_State *L) {
  int i;
  /* create new type _LOADLIB */
  luaL_newmetatable(L, "_LOADLIB");
  lua_pushcfunction(L, gctm);
  lua_setfield(L, -2, "__gc");
  /* create `package' table */
  luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
#if defined(LUA_COMPAT_LOADLIB) 
  lua_getfield(L, -1, "loadlib");
  lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
#endif
  lua_pushvalue(L, -1);
  lua_replace(L, LUA_ENVIRONINDEX);
  /* create `loaders' table */
  lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1);
  /* fill it with pre-defined loaders */
  for (i=0; loaders[i] != NULL; i++) {
    lua_pushcfunction(L, loaders[i]);
    lua_rawseti(L, -2, i+1);
  }
  lua_setfield(L, -2, "loaders");  /* put it in field `loaders' */
  setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT);  /* set field `path' */
  setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */
  /* store config information */
  lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n"
                     LUA_EXECDIR "\n" LUA_IGMARK);
  lua_setfield(L, -2, "config");
  /* set field `loaded' */
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
  lua_setfield(L, -2, "loaded");
  /* set field `preload' */
  luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 0);
  lua_setfield(L, -2, "preload");
  lua_pushvalue(L, LUA_GLOBALSINDEX);
  luaL_register(L, NULL, ll_funcs);  /* open lib into global table */
  lua_pop(L, 1);
  return 1;  /* return 'package' table */
}
Example #20
0
LUA_API void luaS_openextlibs(lua_State *L) {
	const luaL_Reg *lib;

	luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD",
		sizeof(s_lib_preload)/sizeof(s_lib_preload[0])-1);

	for (lib = s_lib_preload; lib->func; lib++) {
		lua_pushcfunction(L, lib->func);
		lua_setfield(L, -2, lib->name);
	}

	lua_pop(L, 1);
}
Example #21
0
/* adapted from Lua 5.2's lauxlib.c */
NUMLUA_API void nl_require (lua_State *L, const char *modname,
    lua_CFunction openf, int glb) {
  lua_pushcfunction(L, openf);
  lua_pushstring(L, modname);
  lua_call(L, 1, 1); /* open module */
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
  lua_pushvalue(L, -2);
  lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
  lua_pop(L, 1); /* _LOADED table */
  if (glb) {
    lua_pushvalue(L, -1); /* module */
    lua_setfield(L, LUA_GLOBALSINDEX, modname); /* _G[modname] = module */
  }
}
LUAMOD_API int luaopen_package (lua_State *L) {
  int i;
  /* create new type _LOADLIB */
  luaL_newmetatable(L, "_LOADLIB");
  lua_pushcfunction(L, gctm);
  lua_setfield(L, -2, "__gc");
  /* create `package' table */
  luaL_newlib(L, pk_funcs);
  /* create `loaders' table */
  lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
  /* fill it with pre-defined loaders */
  for (i=0; loaders[i] != NULL; i++) {
    lua_pushvalue(L, -2);  /* set 'package' as upvalue for all loaders */
    lua_pushcclosure(L, loaders[i], 1);
    lua_rawseti(L, -2, i+1);
  }
  lua_setfield(L, -2, "loaders");  /* put it in field `loaders' */
  /* set field 'path' */
  setpath(L, "path", LUA_PATHVERSION, LUA_PATH, LUA_PATH_DEFAULT);
  /* set field 'cpath' */
  setpath(L, "cpath", LUA_CPATHVERSION, LUA_CPATH, LUA_CPATH_DEFAULT);
  /* store config information */
  lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n"
                     LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
  lua_setfield(L, -2, "config");
  /* set field `loaded' */
  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
  lua_setfield(L, -2, "loaded");
  /* set field `preload' */
  luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
  lua_setfield(L, -2, "preload");
  lua_pushglobaltable(L);
  lua_pushvalue(L, -2);  /* set 'package' as upvalue for next lib */
  luaL_setfuncs(L, ll_funcs, 1);  /* open lib into global table */
  lua_pop(L, 1);  /* pop global table */
  return 1;  /* return 'package' table */
}
Example #23
0
void am_requiref(lua_State *L, const char *modname, lua_CFunction openf) {
#if defined(AM_LUA52) || defined(AM_LUA53)
    luaL_requiref(L, modname, openf, 1);
    lua_pop(L, 1);
#else
    lua_pushcfunction(L, openf);
    lua_pushstring(L, modname);  /* argument to open function */
    lua_call(L, 1, 1);  /* open module */
    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16);
    lua_pushvalue(L, -2);  /* make copy of module (call result) */
    lua_setfield(L, -2, modname);  /* _LOADED[modname] = module */
    lua_pop(L, 1); // _LOADED
    lua_setglobal(L, modname); // pops call result
#endif
}
Example #24
0
static void
register_3rd_party (lua_State *L)
{
	static const luaL_Reg libs[] = {
		{ "lpeg", luaopen_lpeg },
		{ NULL, NULL }
	};

	const luaL_Reg *lib;

	luaL_findtable (L, LUA_REGISTRYINDEX, "_PRELOAD", 16);
	for (lib = libs; lib->name != NULL; lib++) {
		lua_pushcfunction (L, lib->func);
		lua_setfield (L, -2, lib->name);
	}
}
Example #25
0
LUALIB_API void luaL_openlibs(lua_State *L)
{
  const luaL_Reg *lib;
  for (lib = lj_lib_load; lib->func; lib++) {
    lua_pushcfunction(L, lib->func);
    lua_pushstring(L, lib->name);
    lua_call(L, 1, 0);
  }
  luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD",
		 sizeof(lj_lib_preload)/sizeof(lj_lib_preload[0])-1);
  for (lib = lj_lib_preload; lib->func; lib++) {
    lua_pushcfunction(L, lib->func);
    lua_setfield(L, -2, lib->name);
  }
  lua_pop(L, 1);
}
Example #26
0
void
luaL_register_module(struct lua_State *L, const char *modname,
		     const struct luaL_Reg *methods)
{
	assert(methods != NULL && modname != NULL); /* use luaL_register instead */
	lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
	if (strchr(modname, '.') == NULL) {
		/* root level, e.g. box */
		lua_getfield(L, -1, modname); /* get package.loaded.modname */
		if (!lua_istable(L, -1)) {  /* module is not found */
			lua_pop(L, 1);  /* remove previous result */
			lua_newtable(L);
			lua_pushvalue(L, -1);
			lua_setfield(L, -3, modname);  /* _LOADED[modname] = new table */
		}
	} else {
		/* 1+ level, e.g. box.space */
		if (luaL_findtable(L, -1, modname, 0) != NULL)
			luaL_error(L, "Failed to register library");
	}
	lua_remove(L, -2);  /* remove _LOADED table */
	luaL_register(L, NULL, methods);
}
Example #27
0
int main(int argc, char *argv[]) {
  lua_State *L;
  int i, status;

  L = luaL_newstate();
  if (L == NULL) {
    fprintf(stderr, "PANIC: failed to create main state!\n");
    return 1;
  }

  luaL_openlibs(L);
  luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 1);
  lua_pushcfunction(L, luaopen_lpeg);
  lua_setfield(L, -2, "lpeg");
  lua_pop(L, 1);

  lua_createtable(L, argc, 0);
  for (i = 0; i < argc; i++) {
    lua_pushstring(L, argv[i]);
    lua_rawseti(L, -2, i);
  }
  lua_setglobal(L, "arg");

  lua_pushcfunction(L, traceback);
  lua_getglobal(L, "require");
  lua_pushliteral(L, "shine");
  status = lua_pcall(L, 1, 1, -3);

  if (status) {
    fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
    lua_close(L);
    return 1;
  }

  lua_close(L);
  return 0;
}
ngx_int_t
ngx_stream_lua_clfactory_loadfile(lua_State *L, const char *filename)
{
    int                         c, status, readstatus;
    ngx_flag_t                  sharp;

    ngx_stream_lua_clfactory_file_ctx_t        lf;

    /* index of filename on the stack */
    int                         fname_index;

    sharp = 0;
    fname_index = lua_gettop(L) + 1;

    lf.extraline = 0;
    lf.file_type = NGX_LUA_TEXT_FILE;

    lf.begin_code.ptr = CLFACTORY_BEGIN_CODE;
    lf.begin_code_len = CLFACTORY_BEGIN_SIZE;
    lf.end_code.ptr = CLFACTORY_END_CODE;
    lf.end_code_len = CLFACTORY_END_SIZE;

    lua_pushfstring(L, "@%s", filename);

    lf.f = fopen(filename, "r");
    if (lf.f == NULL) {
        return ngx_stream_lua_clfactory_errfile(L, "open", fname_index);
    }

    c = getc(lf.f);

    if (c == '#') {  /* Unix exec. file? */
        lf.extraline = 1;

        while ((c = getc(lf.f)) != EOF && c != '\n') {
            /* skip first line */
        }

        if (c == '\n') {
            c = getc(lf.f);
        }

        sharp = 1;
    }

    if (c == LUA_SIGNATURE[0] && filename) {  /* binary file? */
        lf.f = freopen(filename, "rb", lf.f);  /* reopen in binary mode */

        if (lf.f == NULL) {
            return ngx_stream_lua_clfactory_errfile(L, "reopen", fname_index);
        }

        /* check whether lib jit exists */
        luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1);
        lua_getfield(L, -1, "jit");  /* get _LOADED["jit"] */

        if (lua_istable(L, -1)) {
            lf.file_type = NGX_LUA_BT_LJ;

        } else {
            lf.file_type = NGX_LUA_BT_LUA;
        }

        lua_pop(L, 2);

        /*
         * Loading bytecode with an extra header is disabled for security
         * reasons. This may circumvent the usual check for bytecode vs.
         * Lua code by looking at the first char. Since this is a potential
         * security violation no attempt is made to echo the chunkname either.
         */
        if (lf.file_type == NGX_LUA_BT_LJ && sharp) {

            if (filename) {
                fclose(lf.f);  /* close file (even in case of errors) */
            }

            filename = lua_tostring(L, fname_index) + 1;
            lua_pushfstring(L, "bad byte-code header in %s", filename);
            lua_remove(L, fname_index);

            return LUA_ERRFILE;
        }

        while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) {
            /* skip eventual `#!...' */
        }

        status = ngx_stream_lua_clfactory_bytecode_prepare(L, &lf, fname_index);

        if (status != 0) {
            return status;
        }

        lf.extraline = 0;
    }

    if (lf.file_type == NGX_LUA_TEXT_FILE) {
        ungetc(c, lf.f);
    }

    lf.sent_begin = lf.sent_end = 0;
    status = lua_load(L, ngx_stream_lua_clfactory_getF, &lf,
                      lua_tostring(L, -1));

    readstatus = ferror(lf.f);

    if (filename) {
        fclose(lf.f);  /* close file (even in case of errors) */
    }

    if (readstatus) {
        lua_settop(L, fname_index);  /* ignore results from `lua_load' */
        return ngx_stream_lua_clfactory_errfile(L, "read", fname_index);
    }

    lua_remove(L, fname_index);

    return status;
}
Example #29
0
const char* LuaState::FindTable(int idx, const char *fname, int szhint)
{
	return luaL_findtable(m_state, idx, fname, szhint);
}
Example #30
0
lsb_heka_sandbox* lsb_heka_create_input(void *parent,
                                        const char *lua_file,
                                        const char *state_file,
                                        const char *lsb_cfg,
                                        lsb_logger logger,
                                        lsb_heka_im_input im)
{
  if (!lua_file) {
    if (logger) logger(__FUNCTION__, 3, "lua_file must be specified");
    return NULL;
  }

  if (!im) {
    if (logger) logger(__FUNCTION__, 3, "inject_message callback must be "
                       "specified");
    return NULL;
  }

  lsb_heka_sandbox *hsb = calloc(1, sizeof(lsb_heka_sandbox));
  if (!hsb) {
    if (logger) logger(__FUNCTION__, 3, "memory allocation failed");
    return NULL;
  }

  hsb->type = 'i';
  hsb->parent = parent;
  hsb->msg = NULL;
  hsb->cb.iim = im;
  hsb->name = NULL;
  hsb->hostname = NULL;

  hsb->lsb = lsb_create(hsb, lua_file, lsb_cfg, logger);
  if (!hsb->lsb) {
    free(hsb);
    return NULL;
  }

  lua_State *lua = lsb_get_lua(hsb->lsb);
  set_restrictions(lua, hsb);

// todo link print to the logger or a no-op
  lsb_add_function(hsb->lsb, heka_decode_message, "decode_message");
  lsb_add_function(hsb->lsb, read_message, "read_message");
  lsb_add_function(hsb->lsb, inject_message_input, "inject_message");
// inject_payload is intentionally excluded from input plugins
// you can construct whatever you need with inject_message

// preload the Heka stream reader module
  luaL_findtable(lua, LUA_REGISTRYINDEX, "_PRELOADED", 1);
  lua_pushstring(lua, mozsvc_heka_stream_reader_table);
  lua_pushcfunction(lua, luaopen_heka_stream_reader);
  lua_rawset(lua, -3);
  lua_pop(lua, 1); // remove the preloaded table

  if (lsb_init(hsb->lsb, state_file)) {
    if (logger) logger(hsb->name, 3, lsb_get_error(hsb->lsb));
    lsb_destroy(hsb->lsb);
    free(hsb->hostname);
    free(hsb->name);
    free(hsb);
    return NULL;
  }

// remove output function
  lua_pushnil(lua);
  lua_setglobal(lua, "output");

  return hsb;
}