Stack C ++ function call lua script file

In recent problem has been to find the function stack like, just have the opportunity today and share it.

Want lua script file transfer function in C ++ using relatively simple, because lua scripts come and C \ C ++ through the stack to stop, according to the number of interactive,
So the original reason is also very simple, just need to put function lua script pushed onto the stack
Then sent to the function parameters may also be needed in the order from left to right is pressed into the stack,
Then able to perform this function a
After the function is executed, it will move from being deleted just pushed us to, according to the number of the stack, then the function's return value onto the stack
Li original way, but it is through the stack to stop, according to several interactive
 
Look at a small order process:
This sequence Cheng will demonstrate how to adjust the overall amount of the lua script file with

void main()
{
lua_State* pluaState=luaL_newstate();
luaL_openlibs(pluaState);
// Lua script execution
luaL_dofile(pluaState,"../Script/test.lua");
 
 // The script file Pi amount of PSA into the heap Village
lua_getglobal(pluaState,"Pi");
 cout << "read script lua amount Pi:" << (float) lua_tonumber (pluaState, 1) << endl;
lua_pop(pluaState,1);
 
 // release resources
lua_close(pluaState);
getchar();
}

-------------------------------------------------- ------- the following is a lua script file -------------------------------------- -----------------------------------------
- there is only one line of code defines a global quantitative pi
Pi=3.1415926

This part of the order process in bold face the reality of this place is the function code
Lua_getglobal function () is used to overall volume lua script file transformer onto the stack, this function has two parameters, the first parameter is lua state machine (in number with the stored data),
The second parameter is the amount you want to get a global name identifier ..

Lua_tonumber function () is read, according to the number of designated stack location and converted into numerical, the first parameter to this function is lua state machine, the second parameter is the position of the data to be read
Finally, we will read out the data and then converted to a floating-point number, according to the number printed to the console by cout ..

The last function lua_pop () does not matter, but in order to continue following the current party and then lua stack according to the number of stops for the interactive operation, adjustment to the best use of this function ..
Of this function is to delete the stack according to the number specified location

Daily truth
love, sometimes not vows of commitment required, but she must needs meticulous care and greetings to detail; love, sometimes do not need to tragic Butterfly butterfly, but she must needs heart have a tacit understanding with the appeals; love, sometimes do not need to follow Fei from the female, but she must need to profound feelings of support and understanding.

Tune with lua scripts with similar functions and quantitative global dimming lua script file, as little reason as the original, but with a twist
Bold part code before the code replacement, the rest does not change

// function call with lua script
lua_getglobal(pluaState,"Add");   
lua_pushnumber(pluaState,1);
lua_pushnumber(pluaState,1);
lua_call(pluaState,2,1);
cout<<"1+1="<<(int)lua_tonumber(pluaState,1)<<endl;
lua_pop(pluaState,1);

    
-------------------------------------------------- --------- the following is a script file ------------------------------------- ----------------------
- function, which in ten brief, an adder is considered realistic operation, returns the result of adding two numbers
function Add(intX,intY)
return intX+intY;
end ------------------------------------------------- the following is a script file ------------------------------------ ---------- -----------------------

This C ++ sequence above process:
lua_getglobal (pluaState, "Add"); This function Add lua script () function is pushed onto the stack
lua_pushnumber (pluaState, 1); This function will assign numeric data onto the stack number in
lua_pushnumber (pluaState, 1); ibid.
lua_call (pluaState, 2,1); performing functions in the stack just been pressed, the last two parameters of this function are the number of arguments and return values ​​of the number of
After the function is executed will have been deleted is forced into the stack data number, and then will return the value onto the stack
cout<<"1+1="<<(int)lua_tonumber(pluaState,1)<<endl;
lua_pop(pluaState,1);
It is now more than two pieces of code in the stack return value of the output, and then delete ,, he will continue following the current party and lua stack stop, according to several interactive

 

    
 

The end of the article to share some of jokes quotations under programmer: Real Programmers like and sell popcorn, they use the CPU heat emanating popcorn, you can hear what the program is running according to rice flower burst of speed.

Reproduced in: https: //www.cnblogs.com/jiangu66/archive/2013/04/25/3043293.html

Intelligent Recommendation

C++----function stack call

First, the function stack call Take the following simple code as an example to analyze the process of function call By observing assembly language, The first step is to first open the stack frame of t...

C++ function stack call

Function stack call First, what is the stack and its characteristics? In a computer system, the stack is defined as a special container, the user can push the data onto the stack, or pop the data that...

C ++ function call stack

1. how function arguments passed to the parameter? There is no parameter to open up memory? If the parameter memory opened up, where to open up? 2. returns the return value of the function to the call...

[C++] function call stack

The execution process of the program can be regarded as a continuous function call. When the execution of a function is completed, the program should return to the next instruction of the calling inst...

C function call stack

"Linux Kernel Complete Notes" section 3.4: C and assembler call each other C function call mechanism Most programs on the CPU use the stack to support function call operations. The stack is ...

More Recommendation

C ++ stack function call

First, the function of the C ++ library 1.1 header file 1.2 Stack Refremrough Operation Function mainly includes: Stack size .size () The judgment stack is empty .Empty () Stack top insert (in the sta...

c++ call lua error display stack information

This article implements how win32 displays stack information when c++ calls lua error Test.lua file Method 1: Implementation information inside the script   Method 2: C++ source code implementati...

Call LUA in C language (3)-pass variables to Lua script

1. C code 2, the content of the Lua script 3. Compile gcc -o xxx.c xxx   /usr/local/lib/liblua.a  -ldl  -lm -lm is to connect to the corresponding math library, otherwise you will still...

0 basis lua learning (xviii) C call stack Lua ---- 02Lua

1. Lua communicate with C, why the use of a virtual stack? When data is exchanged between the Lua and C there are two problems: Dynamic does not match the static type system Automatic and manual ...

Implementing a function to compile notes --- lua lua call a C ++ function class

This article is a compilation of posts on function This class is basically where did you get from LuaPlus, why not support the use LuaPlus, because that stuff I actually do not know how to compile, co...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top