Integration of Lua script learning notes in Delphi 2 _ call the Lua function (details)

Source: Internet
Author: User

I have studied in detail the subtle process of the main program calling the Lua function.
Call the Lua Function

Function add (x, y)
Return x + y
End

After the Lua library has been opened and the script is loaded, the main program uses the following method to call

Lua_getglobal (L, "add"); // press the Add parameter in the called Lua.
Lua_pushnumber (L, x); // press the 1st parameters of ADD.
Lua_pushnumber (L, Y); // press the 2nd parameters of ADD.

Then use lua_pcall to call if (lua_pcall (l, 2, 1, 0) <> 0) then
Begin
Showmessage (format ('error F: % s', [lua_tostring (L,-1)]);
End;

The above if is used to determine whether the lua_pcall call is correct. If the call is correct, lua_pcall returns 0,
If an error occurs, the error message lua_tostring (L,-1) {the error message is saved here}

Of course, you can also use Lua's API to determine the type of the returned variable for Data check.

If not lua_isnumber (L,-1) then
Showmessage ('the returned data is not a number !! ');

After no error occurs, the value of lua_tonumber (L,-1) is returned by the Add function in Lua.

C: = lua_tonumber (L,-1 );
// Use
Lua_pop (L, 1); // The output stack. press a few to get the output. Parameter 2

Note: Careful friends will find the error message lua_tostring (L,-1) After lua_pcall is called)
The result lua_tonumber (L,-1) returned by the Add function is the same as "-1". Why?
The reason is that when Lua exchanges data with the main program, all values are saved in the stack.
When the Lua function encounters an error, Lua will import the error information to the stack, that is,-1.

NOTE 2: lua_pcall will adjust the number of returned results according to your requirements, discard unnecessary results, and use nil to supplement the results.
Before the result is imported into the stack, lua_pcall removes the functions and parameters in the stack.
If the function returns multiple results, the first result is first written into the stack. Therefore, if n results are returned,
The position of the first returned result in the stack is-N, and the position of the last returned result in the stack is-1.

Continue to save user data in Lua tomorrow. (This is very important in my opinion, because I have not yet found that Lua can register the functions of class functions, so you can only use the APIS provided by Lua to save user data, to register the communication between extended Lua functions ).

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.