lua-users home
lua-l archive

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


This is a result of my experiment for an easyer API, tentatively named
as EasyLua, as a simpler way to embed Lua into a C-program, not C++.
While the formal C-API is just fine, but this is a subset for beginers.
The main idea of my method is to minimize thinking about the lua-stack
on accessing Lua data from a C-program.
It was deviced after I was wondering if we can use following way to
refer Lua values from a C language.

  int nframe_inlua   = el_num(el_var("nframe"));
  char * title_inlua = el_str(el_var("title"));

Here, the function el_var() will keep the name of the global variable
on the stack, then on calling el_num(), it invokes
lua_gettable(L,GLOBALSINDEX) and calls lua_tonumber().
For writing a value into Lua, it might be following way.

  el_setnum(el_var("nframe"), 100);

For these two ways for reading and writing, we just need to maintain
the stack contents properly using the return value of el_var().
Using two stack elements, table and index can be specified.
So, I would like to access a field in a Lua table
like following way. First, the table is assigned an integer
handle, in this sample, number 2 is assigned.

  el_table(2,"user");
  el_num(el_field(2,"uid");
  el_setnum(el_field(2,"uid"),9601); /** user.uid=9601 **/

Is it simple enough for beginers?
Since most of program in my work is a standalone program that will
only use several of Lua variables, including a few tables.
Probably there are discussions for this method, and improvements
are needed. This method is less efficient as the formal API, but
it is much more comprehensive. This EasyLua API does not
provide full set of functionality yet.
Again, this subset API is to lower the hardle for beginers.
For those who understand the formal C-API of Lua, it will
be better to use the formal one.

Is there already such a method used by somebody?
My implementation is uploaded on my site:
http://www.cbrc.jp/~ueno/devzone/ezlua01.tgz

It is still experimental, but I would be happy if people test
and examine. In this implementation, the name of global variables
or filed name was saved in the Lua stack, and they are cleaned
after a value is obtained. Calling Lua function is a bit tricky
because the Lua stack have to be used for the argument and
return values. EasyLua takes a table for return values.

  ip=el_var("print");
  el_pushnum("something");	/** the same as official API **/
  el_pushstr(10.0);
  ir=el_call(ip);
  el_num(el_index(ir,1)));      /** if return values exist **/

Any comments are welcome.

---
Yutaka Ueno