lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great =?utf-8?q?admin=40saptasindhu. website?= once stated:
> 
> I understand that Lua is a scripting language, and hence has an
> interpreter, most probably with an in-built compiler (like Perl).
> 
> I fail to understand what is meant by Lua being embeddable.
> 
> Does it mean that the Lua interpreter (which is written in C) can be
> included in a larger C program?

  Yes.

> If yes, does that mean the larger C program can then run Lua scripts while
> the larger C program is running?

  Yes.

> If yes again, how do those Lua scripts being called from the larger C
> program get access to that C program's functions?

  By a similar process as loading a Lua module written in C.  An example
might be:

	static struct luaL_Reg const foo_stuff[] =
	{
	  { "read"   , foo_read   },
	  { "mangle" , foo_mangle },
	  { "bar"    , foo_bar    },
	  { "write"  , foo_write  },
	  { "close"  , foo_close  },
	  { NULL     , NULL       }
	};

	lua_State *create_lua_state(void)
	{
	  lua_State *L = luaL_newstate();
	  luaL_openlibs(L);	/* load standard Lua modules */
	  luaL_newlib(L,foo_stuff);
	  lua_setglobal(L,"foo");	/* set as global variable "foo" */
	}

  This makes available the functions to manipulate a foo in the Lua state.
There are other ways to do this, but that's the basic idea.

> For example, I can understand that a stand-alone interpreter for a
> scripting language would have a systems programming interface which
> exposes the low-level routines (like POSIX or WinAPI) to be used by that
> scripting language.
> 
> But, I cannot understand how a embeddable Lua interpreter could be useful?

  One use I've found is to use Lua as a configuration file.  For my blogging
engine [1], I use Lua for the configuration [2], which is loaded during
initialization [3] and values from that are read out.  That's a very
simplistic usage of Lua, but one that works well for my blogging engine.

  Another example:  I wrote my own syslog daemon [4] where the C code
handles all the networking and parsing of messages, but leaves the actual
processing of the logs to Lua.  

  -spc

[1]	https://github.com/spc476/mod_blog

[2]	https://github.com/spc476/mod_blog/blob/master/journal/blog.conf

[3]	https://github.com/spc476/mod_blog/blob/ec16fd2edf712ef9aa3944e2e26a2590d26a94a6/src/globals.c#L165

[4]	https://github.com/spc476/syslogintr