当前位置:网站首页>Analysis of lua source code
Analysis of lua source code
2022-06-12 13:57:00 【HyperCall】
This article is a navigation class , For the time being, let's start with something , Follow up to add slowly
Tips:LUA call C Language interface
- demand :C Language programming interface , Export to LUA call
- C Interface form :typedef int (lua_CFunction)(lua_State L)
- give an example :int add(lua_State* L), That is, the return value must be int type , There is only one parameter and it is lua_State Pointer types
- lua_State:L by LUA call C Interface , adopt L Class interface of / Member variables are accessible LUA related data
- Input parameter acquisition method : adopt L Pointer export method , Such as luaL_checknumber(L,1); Check the validity of the parameter and return the first input parameter ( From left to right )
- Output parameter transfer mode : Pass the output value by pressing the stack , Such as :lua_pushnumber(L,op1 + op2), And press a few parameters into the stack return A few , send LUA The stack can be balanced correctly
LUA Source reading notes (ver: 5.3.4)
Start with a function LUA Implementation mechanism , Some of the deeper content will produce a separate chapter , Such as :TValue structure …
The source code reading starts with the mathematical calculation library
- lmathlib.c
use C Language implements some mathematical calculation class function interfaces , for LUA call , Analyze a ABS function , understand LUA Data interface and usage of , as follows :
// Derived function for finding absolute value
static int math_abs (lua_State *L) {
// Conform to the export interface format convention
if (lua_isinteger(L, 1)) {
lua_Integer n = lua_tointeger(L, 1);
if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
lua_pushinteger(L, n);
}
else
lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
return 1;
}
// Which calls lua_isinteger Determine the input parameter type , Its function is implemented as :
LUA_API int lua_isinteger (lua_State *L, int idx) {
const TValue *o = index2value(L, idx);
return ttisinteger(o);
}
// among ttisinterger(0) Define... For macros , The expansion process of combined parameters is :
#define ttisinteger(o) checktag((o), LUA_VNUMINT)
#define ttisinteger(o) checktag((o), makevariant(LUA_TNUMBER, 0)) // among LUA_TNUMBER by 3
#define ttisinteger(o) checktag(o, 3)
#define ttisinteger(o) o->tt_ == 3 // among o The type of TValue
One can basically guess TValue There is a member variable in the structure tt_ Represents the type of variable , among 3 yes int The enumerated values , of TValue For detailed analysis, see :LUA TValue structure .( It is suggested to read and play this article before continuing )
go back to lua_isinteger Function , Find out about it TValue Type of o Variables are defined by functions index2value Call returned , See its implementation as follows :
static TValue *index2value (lua_State *L, int idx) {
CallInfo *ci = L->ci;
if (idx > 0) {
StkId o = ci->func + idx;
api_check(L, idx <= L->ci->top - (ci->func + 1), "unacceptable index");
if (o >= L->top) return &G(L)->nilvalue; // Out of stack space return nil empty
else return s2v(o);
}
else{
...//ID<=0 For the time being , It can be understood as wrong call
}
}
//api_check Define... For macros , The expansion process of combined parameters is :
#define api_check(l,e,msg) luai_apicheck(l,(e) && msg)
#define api_check(l,e,msg) luai_apicheck(L,(1 <= L->ci->top - (ci->func + 1) && "unacceptable index")
#define api_check(l,e,msg) luai_apicheck(L,(1 <= The maximum number of parameters supported by the stack space ) && "unacceptable index")
#define api_check(l,e,msg) (L, lua_assert(1)) / (L, lua_assert(0))
/* convert a 'StackValue' to a 'TValue' */
#define s2v(o) (&(o)->val)
You can basically guess ci->func Pointer to the top of the stack , adopt +idx To find the address of the input parameter , Through s2v obtain TValue
api_check It is responsible for checking whether the input parameters requested for access exceed the stack space , Prevent illegal access to data
among CallInfo structure ,StkId See the link for structure analysis :LUA CallInfo structure ,StkId Structural analysis .( It is suggested to read and play this article before continuing )
go back to math_abs Function , Where the parameter is called lua_tointeger function
lua_Integer n = lua_tointeger(L, 1);
And
#define lua_tointeger(L,i) lua_tointegerx(L,(i),NULL)
LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) {
lua_Integer res = 0;
const TValue *o = index2value(L, idx);
int isnum = tointeger(o, &res);
if (pisnum)
*pisnum = isnum;
return res;
}
namely lua_tointeger(L, 1) == lua_tointegerx (L, 1, NULL)
index2value The function has been parsed above , The function is to specify... In the stack structure by serial number ID Data extraction , Here we take the first parameter
here tointeger(o, &res) Equivalent to (o)->tt_ == 3 ? (*(&res) = (((o)->value_).i), 1) : luaV_tointeger(o,&res,F2Ieq))
There will be tt_ Is it 3 The judgment of the , Because in this version LUA take int and float Values are stored in lua_Integer in , So a distinction needs to be made here
- lstrlib.c
- lapi.c
- lobject.h
边栏推荐
- Running phase of SystemC
- Bridging and net
- Cdeforces 1638 C. inversion graph - simple thinking
- Now you must know the pointer
- Relevant knowledge points of cocoapods
- OAuth2学习中的一些高频问题的QA
- Paw advanced user guide
- Codeforces 1629 A. download more RAM - simple greed
- one × Convolution kernel of 1
- 阿里云开发板HaaS510解析串口JSON数据并发送属性
猜你喜欢

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging

SystemC common errors

Web3.0,「激发创造」的时代

拆改廣告機---業餘解壓

上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人

【视频课】android studio物联网APP设计制作全套教程--国庆期间全掌握

【mysql进阶】mysql索引数据结构的演变(四)

Behind the unsealing of Shanghai, this group of developers "cloud gathering" built an AI anti epidemic robot

简述CGI与FASTCGI区别

Dismantle and modify the advertising machine - Amateur decompression
随机推荐
阿里云开发板HaaS510响应UART串口指令
Codeforces 1637 A. sorting parts - simple thinking
Use of pytorch (to be supplemented)
Code debugging - print log output to file
Lua common built-in functions
Running phase of SystemC
Install RPM package offline using yum
CSDN blog points rule
Ffmpeg Learning Guide
CUDA error: CUBLAS_ STATUS_ NOT_ INITIALIZED when calling `cublasCreate(handle)`
Codeforces 1629 F1. Game on sum (easy version) - DP, game, thinking
SystemC:SC_ Thread and SC_ METHOD
Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())
Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging
Understanding recursion
Binary tree traversal
Behind the unsealing of Shanghai, this group of developers "cloud gathering" built an AI anti epidemic robot
NotePad 常用设置
阿里云开发板HaaS510连接物联网平台--HaaS征文
Paw advanced user guide