Overview
The previous LUA-C interaction function Registration describes how to register C functions with Lua for use in Lua.
When the C-provided interface for fetching data is called in Lua, how does C convert multiple struct types to LUA's nested table types?
Here is an example to illustrate the C data type
typedef struct STUDENT
{
int age ; < age
int grade; < grade
Char name[32]; < name
}student;
typedef struct School
{
char phone[32]; < telephone
int totalpeople; ///< total number
}school;
define an interface to get data
int Get_student_info (student* p_student)
{
assert (NULL! = p_student);
P_student->age = 8;
P_student->grade = 3;
strncpy (P_student->name, "xiaoming", +);
return 0;
}
int Get_school_info (school* p_school)
{
assert (NULL! = p_school);
P_school->totalpeople = $;
strncpy (P_school->phone, "1008611", +);
return 0;
}
interface implementations registered to LUA calls
#define JL_GET_ARRAY_MAX///< Get the maximum number of properties at once #define Jl_key_buf_max///<
Keyword maximum cache length static void Format_student (Lua_state *l) {student student_info;
memset (&student_info, 0, sizeof (student_info));
int ret = Get_student_info (&student_info);
if (0! = ret) {assert (false);
} else {lua_newtable (L);
Lua_pushstring (L, student_info.name);
Lua_setfield (L,-2, "name");
Lua_pushinteger (L, student_info.age);
Lua_setfield (L,-2, "age");
Lua_pushinteger (L, Student_info.grade);
Lua_setfield (L,-2, "grade");
Nested student table into the parameter table lua_pushstring (L, "student"); Lua_insert (L,-2); /* Student Table */lua_settable (L,-3);
/* Param.student = student */}} static void Format_school (Lua_state *l) {School school_info;
memset (&school_info, 0, sizeof (school_info));
int ret = Get_school_info (&school_info);
if (0! = ret) {assert (false);
} else {lua_newtable (L); Lua_pushstring (L, School_infO.phone);
Lua_setfield (L,-2, "phone");
Lua_pushinteger (L, school_info.totalpeople);
Lua_setfield (L,-2, "totalpeople");
Nested school table into the parameter table lua_pushstring (L, "school"); Lua_insert (L,-2); /* School Table */lua_settable (L,-3); /* Param.school = School */}}//Here we register the function parameter for LUA to the array {"School", "student"} int lua_get_info (Lua_state *l) {lual_checkt Ype (L, 1, lua_ttable); Detects if the pass-through is table int array_len = Lua_rawlen (L, 1);
The length of the array char Keys[jl_get_array_max][jl_key_buf_max] = {0}; int Len_max = (Array_len <= jl_get_array_max)?
Array_len:jl_get_array_max;
int idx = 1;
int keys_num = 0;
Take element for (int i = 0; i < Len_max; i++) {Lua_rawgeti (L, 1, idx++);
size_t Tmp_len = 0;
Const char* P_STR = lual_checklstring (L,-1, &tmp_len);
if ((0 < Tmp_len) && (Tmp_len < Jl_key_buf_max)) {strcpy (Keys[keys_num], p_str);
Keys_num + = 1;
} lua_pop (L, 1);
}//Return to Table lua_newtable (L); for (int k = 0; k < keys_num; ++k{if (0 = = strcmp (Keys[k], "school")) {Format_school (L);//Spell School Table} else if (0 = = strcmp (Keys[k], "s Tudent ")) {format_student (L);//Spell Student Table}} return 1;
Indicates that a parameter is returned}
test code in LUA
Local B_APP_API = require (APP_API)
local param = b_app_api.get_info{"school", "student"}
assert (param ~= nil) C3/>print (Param.student.age, Param.student.grade, param.student.name)
print (Param.school.phone, param.school.totalPeople)
Result:
[Lua-print]: 8 3 xiaoming
[lua-print]: 1008611 500