Lua study Note 2-Use your c Functions in Lua

Source: Internet
Author: User

I don't know where csdn has taken note 2. It was written for 2nd times.

Lua study Note 2
Use your own c Functions in Lua

 

 

Okay. The last time we learned how to write a function in the Lua script and apply it to the C file.
Now let's learn another interaction method.

Purpose of this section: to call a self-written function in a Lua script. Focus on writing rules for C functions that can be used by Lua scripts.

 

Don't talk about anything, Rtfs

 

------- The following is the Lua script --------
-- Test. Lua
Luac_messagebox ("last is showmessage! This is real MessageBox! ");
--------- The luaedit syntax test is used to test the syntax --------------

// ------------ The following is the test. cpp file ----------------
// ================================================ ========================================================== ========================================
// Lua test object
// C ++ source lua_test.cpp
// ================================================ ========================================================== ========================================
// ================================================ ========================================================== ========================================
// Include files
// ================================================ ========================================================== ========================================
Extern "C"
{
# Include "D: // my documents // Visual Studio 2005 // projects // Lua. H"
# Include "D: // my documents // Visual Studio 2005 // projects // Lua // lualib. H"
# Include "D: // my documents // Visual Studio 2005 // projects // Lua // lauxlib. H"
}

# Include <windows. h>
# Include <stdio. h>
# Include <string>
Using namespace STD;
// ================================================ ========================================================== ========================================
// Libraries
// ================================================ ========================================================== ========================================
# Pragma comment (Lib, "d: // my documents // Visual Studio 2005 // projects // Lua // release // Lua. lib ")
// ================================================ ========================================================== ========================================
// Global variables
// ================================================ ========================================================== ========================================
Lua_state * l;
// ================================================ ========================================================== ========================================
// Lua Functions
// ================================================ ========================================================== ========================================
Double F (Double X, Double Y)
{
Double ret;
Lua_getglobal (L, "F ");
Lua_pushnumber (L, X );
Lua_pushnumber (L, y );
Lua_call (l, 2, 1 );
// Lua_pcall (l, 2, 1, 0 );

Ret = lua_tonumber (L,-1 );
// Lua_pop (L, 1 );
Return ret;
}
// ================================================ ========================================================== ========================================
// C/C ++ Functions
// ================================================ ========================================================== ========================================
Int luac_messagebox (lua_state * l)
{
Char message [256] = "";
Int I;

// Obtain the number of parameters
Int n = lua_gettop (L );

// Save all parameters
For (I = 1, j = 0; I <= N; I ++)
{
If (lua_isstring (L, I ))
Strcpy (message, lua_tostring (L, I ));
}

// Execution Logic
MessageBox (null, message, "Lua test", mb_ OK );

// Return value Stack

// Return the number of pressure stack parameters
Return 0;
}
// ================================================ ========================================================== ========================================
// Main Functions
// ================================================ ========================================================== ========================================
Int main (void)
{
Int error;

L = lua_open ();
Luaopen_base (L );
Lual_openlibs (L );

// Register C/C ++ Functions
Lua_register (L, "luac_messagebox", luac_messagebox );

// Load the script
// Added error handling
If (error = lual_dofile (L, "test. Lua "))! = 0)
{
MessageBox (null, "error: script execution error! "," Lua test ", mb_ OK );
Return 0;
}

 
Getchar ();
Lua_close (L );
Return 1;
}

 

 

This is the benefit of a series of articles: Even if csdn has lost the original one, I can rewrite the lost one based on the context.

As you can see, I marked all the key points in bold. Sufficient comments are provided. Let's sort it out one by one:

1,Luac_messagebox ("last is showmessage! This is real MessageBox! ");
This is the Lua script that calls a self-written function. Let's look at how to implement this function.

2,Int luac_messagebox (lua_state * l)
This is the entity of the function. To make your function used by the Lua script, you must follow the following rules:
Use the int function name (lua_state * l) to declare and define the function.
Use the Lua stack for parameter exchange.
Call the register function of Lua.

3. For the 2nd rules, Lua Passes parameters according to the _ fastcall rules, that is, from left to right.
The order in which Lua presses the stack is: first, the function presses the stack, and then the parameter presses the stack. After the execution is complete, the parameters and functions are all output from the stack, and the returned values are pushed to the stack in order. Of course, there are many similar parameter exchange rules, but don't worry.

4. retrun 0;
The return value in the C function refers to the number of returned values. Because the Lua script supports multiple return values, combined with the preceding rule and this rule.

5. After compiling the function body in the C file, you must register the function, for example:
Lua_register (L, "luac_messagebox", luac_messagebox );
The second parameter is the function name in the Lua script, and the third parameter is the function name in the C file.

6. What else is not involved? Stack pressure returned value! However, I would like to leave this question for the moment, because I will (already) discuss it in detail in note 4. Therefore, familiarize yourself with this basic process.

7. OK. The advantage of rewriting is that I can directly move the things in note 4. Haha, another time travel.
Lua has many stack-based functions that compress various return values. You can search for "lua_push" in the Lua reference manual and get the following results:
lua_pushboolean
lua_pushinteger
lua_pushfstring
lua_pushlstring
lua_pushnil
lua_pushnumber
.....
There are many such functions. For more information, see the reference manual !).
But in this example, we have no returned value, so retrun 0.

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.