[Lua] LUA calls C/C + + functions/libraries (function stack mode)

Source: Internet
Author: User
Tags cos lua

Test.cpp file

/*lua call C + + function/library (function stack mode) */#include <iostream>using namespace std; #include <lua.hpp>/* when we need to invoke c/c+ in Lua + function, all functions must satisfy the following function signature: typedef int (*lua_cfunction) (Lua_state *l); in other words, all functions must receive a lua_state as a parameter and return an integer value. Because this function uses the LUA stack as a parameter, it can read any number and any type of arguments from the stack. The return value of this function indicates how much of the return value is pressed into the LUA stack when the function returns. (because LUA functions are capable of returning multiple values) */static int math_abs (lua_state *l) {lua_pushnumber (L, ABS ((int) Lual_checknumber (l, 1)));//Get incoming parameter return 1;}    static int Math_cos (Lua_state *l) {lua_pushnumber (l, cos ((double) Lual_checknumber (L, 1))); return 1;}    static int Math_sin (Lua_state *l) {lua_pushnumber (L, sin (double) lual_checknumber (L, 1)); return 1;}    static int ShowMessage (Lua_state * l) {lua_pushnumber (l, 1000);    printf ("show message and push \ n"); return-1;}    Register function void Regist_function (lua_state *l) {//stack after setting a LUA callable global function name Lua_pushcfunction (L, showmessage);    Lua_setglobal (L, "showmessage");    C calls Lua Lua_getglobal (L, "showmessage");    Lua_pcall (L, 0, 0, 0); printf ("Get the ShowMessage pushed value%f \ n ", Lua_tonumber (L,-1)); #define Lua_register (L,n,f) (Lua_pushcfunction (L, (f)), Lua_setglobal (L, (n)))//lua_register as defined above, all Lua_ Pushcfunction (L, showmessage); Lua_setglobal (L, "showmessage");    <==>lua_register (L, "ShowMessage", showmessage);    Lua_register (L, "cos", math_cos);    Test Lua_getglobal (L, "COS");    Lua_pushnumber (L, 0.5);    if (0! = Lua_pcall (L, 1, 1, 0)) {printf ("CPP call LUA function failed\n");    } printf ("cos (0.5) =%f\n", Lua_tonumber (L,-1)); Lua_pop (L, 1);} Registry function void Regist_lib (Lua_state *l) {static const Lual_reg mathlib[] = {"ABS", Math_abs}, {"Cos", m    Ath_cos}, {"Sin", math_sin}, {null, NULL}};    Lual_register (L, "Dy_math", mathlib);    Test double SINV = 30*3.1415926/180.0;    Lua_getglobal (L, "SIN");    Lua_pushnumber (L, SINV);    if (0! = Lua_pcall (L, 1, 1, 0)) {printf ("CPP call LUA function failed\n"); } printf ("Sin (%f) =%f\n",SINV, Lua_tonumber (L,-1)); Lua_pop (L, 1);}    int main () {lua_state *l = Lual_newstate ();    Lual_openlibs (L);    Char *luapath= "Luacallctest.lua";            Lual_dofile (L, Luapath);    Regist_function (L);    Regist_lib (L);    Lua_close (L);    System ("pause"); return 0;}

Luacallctest.lua file

--region luacallctest.luafunction cos (a)    print ("called Cos in Lua script")    --lua Call the C + + function    return COS (a) Endfunction sin (a)    print ("Called Sin in Lua script")    --lua call C/C + + library function    return Dy_math.sin (a) endfunction ShowMessage ()    showmessage () End--end region
Results

[Lua] LUA calls C/C + + functions/libraries (function stack mode)

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.