C Language calls LUA functions

Source: Internet
Author: User

Remember School, junior high school English textbooks, the internet is called surfing the Internet, Chinese is called online surfing. At that time, people often called the Internet a cyberspace. Now work, a lot of spare time for micro-blog, know, QQ, these fragmented reading makes people read easily, also can get some superficial information. However, they are time-consuming black holes, and time flies in such a minute, and at the end of the year, it will tell you how many questions you have answered and read as many words as a dream of red mansions. Just when you're still thinking, these shallow readings don't bring you deep, systematic knowledge. On your timeline, two adjacent messages are often eight poles too. And sometimes you go to see if the followers have updates and look forward to giving you a bright message. The result is often to take advantage of the disappointed and return. The information on the screen is endlessly rolling, so lively, as if everyone is partying, and my heart is so empty and lonely.

In the LUA API, the steps to invoke a function are simple: 1. Press in the function you want to call, using Lua_getglobal. 2. Press in the call parameter. 3. Use LUA_PCALL4 to eject the result from the stack.
For example, suppose you have a LUA function: function f (x, Y)
Return (x^2 * Math.sin (Y))/(1-x)
End
So, we can define a C function to encapsulate this call:/* calls a function ' F ' defined in Lua *
Double f (double x, double y) {
Double Z;lua_getglobal (L, "f"); Lua_pushnumber (L, x); Lua_pushnumber (l, y);/* Do the call (2 arguments, 1 result) * /if (Lua_pcall (L, 2, 1, 0)! = 0) error (L, "Error running function ' F ':%s", lua_tostring (L,-1));
if (!lua_isnumber (L,-1)) error (L, "function ' F ' must return a number"), Z = Lua_tonumber (l,-1); Lua_pop (L, 1); return z;
}
Lua_pcall before the result is pressed, the function, and parameter pops up, and if multiple results are returned, the first one is pressed in. If the Lua_pcall runs in error, it returns a non-0 value. Finish

C Language calls 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.