Lua script calls the C scenario, using the C API to access the table constructed by the script

Source: Internet
Author: User

Lua calls C

Some system services are integrated in LUA parsing, so system resources can be accessed in the script, for example, LUA scripts can invoke the file system interface, can call the math library,

However, there are always some system services or extensions that are not accessible in Lua scripts, and if these system services or extensions are implemented using C language,

You can then use the LUA Library's encapsulation of the C library to encapsulate the functionality into a LUA interface so that the script calls these LUA interfaces to invoke these functions.

-------

In this case, the Lua script is used as the host program.

C Call Lua

Another scenario is the C program as the host program, invoking LUA scripts, such as the Lua script to do the configuration file function,

The C language calls CAPI to run the Lua script and get the values configured in it.

This example, in the first case, describes how to obtain table parameters for LUA scripts in C.

LUA C API

Lua C API Description: http://www.cnblogs.com/stephen-liu74/archive/2012/07/18/2433428.html

Use the GetField interface primarily to access the value of a key in the table:

lua_getfield

[-0, +1, e]

void Lua_getfield (lua_state *l, int index, const char *k);

Pushes onto the stack the value t[k] , where is the value at the t given valid index. As in Lua, this function could trigger a metamethod for the "Index" event (see§2.8).

Ways to traverse a table:

http://blog.csdn.net/chencong112/article/details/6908041

Http://www.cnblogs.com/chuanwei-zhang/p/4077247.html

Lua_getglobal (L, T); Lua_pushnil (l);  while (Lua_next (L,-2)) {    /* At this time the stack-1 is value,-2 is key    */1 c10>);}

= Lua_objlen (L,-1);  for 1; I <= Len; i++) {    Lua_pushinteger (L, i);     -2);     /* This is the t[i] element at the    top of the stack */1);}

DEMO

Description: Uses C to encapsulate a LUA interface that passes in a table and accesses a key value in this table in C.

-- Temperature Conversion table (Celsius to Farenheit) require " Test " test.printtable ({["a"]="aa"})

#include<stdlib.h>#include<math.h>#defineLmathlib_c#defineLua_lib#include"lua.h"#include"Lauxlib.h"#include"Lualib.h"Static intPrintTable (Lua_state *L) {printf ("%s\n","Hello World"); intn =lua_gettop (L); if(n! =1) {printf ("%s\n","PrintTable must has one arg"); }  if(Lua_istable (L,-1) {Lua_getfield (L),-1,"a"); printf ("Arg one table[a]=%s\n", Lua_tostring (L,-1)); }  Else{printf ("%s\n","printtable arg must be table"); }  return 0;}Static ConstLual_reg testlib[] = {  {"printtable", printtable}, {null, NULL}}; Lualib_apiintLuaopen_test (Lua_state *l) {lual_register (L,"Test", Testlib); return 1;}

Print:

: ~/share_windows/opensource/lua/lua-5.1.5$ Lua./test/test.lua
Hello World
Arg one TABLE[A]=AA

Lua script calls the C scenario, using the C API to access the table constructed by the script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.