Interaction between Lua and C + + (1)

Source: Internet
Author: User
Tags lua

/*before listening to a story, when the creators of Java lectures, the beginning of a simple can not be a simple example, the continuous expansion, and finally become a complex and perfect program. An important and important concept is the stack. LUA interacts with other languages and exchanges data, which is done through stacks. */#include<iostream>using namespacestd;extern "C"   //This section is compiled in C instead of C + +{#include"lua.h"#include"Lualib.h"#include"Lauxlib.h"}classcluafn{ Public: Cluafn (void); ~cluafn (void); voidInit ();//initializing Lua object pointer parameters    voidClose ();//turn off Lua object pointers    BOOLLoadluafile (Const Char* Pfilename);//loading the specified LUA file    BOOLCallfilefn (Const Char* Pfunctionname,intNPARAM1,intNPARAM2);//executes a function in the specified LUA filePrivate: Lua_state* M_PSTATE;//This is the LUA state object pointer, and a LUA file corresponds to a};voidCluafn::init () {if(NULL = =m_pstate) {M_pstate= Lua_open ();//returns a Lua object pointerLual_openlibs (m_pstate);//load all the possible LUA base libraries, such as the string library that loads LUA, the IO Library, the math library , and so on.    }}voidCluafn:close () {if(NULL! =m_pstate)   {lua_close (m_pstate); //close the Lua object and release the pointerM_pstate =NULL; }}/*loading the specified LUA file here is a special explanation because Lua is a scripting language that compiles the LUA file itself so that it is compiled as much as possible in the initialization of the program when the file is loaded, because when you execute the Lual_dofile () function, LUA enables the parser, To analyze whether your script syntax conforms to LUA rules, and if you pass a file in a haphazard way, LUA will tell you that the file is syntactically incorrect and cannot be loaded. If you have a large Lua script and a lot of functions, the parser will be time-consuming, so when loading it, try to put it in the right place, and, for a LUA file, reloading Lual_dofile () will not make any sense except that it makes your CPU hot .*/BOOLCluafn:loadluafile (Const Char*pfilename) {    intNret =0; if(M_pstate = =NULL) {        return false; } nret=lual_dofile (M_pstate, pfilename); if(Nret! =0)    {        //lua_tostring (m_pstate,-1)-1 take m_pstate stack top data, i.e. error prompt string (-1 relative to top of stack, 1 relative to bottom of the stack absolute position)printf"Cluafn::loadluafile (%s) is file (%d) (%s) \ n", Pfilename, Nret, lua_tostring (m_pstate,-1)); return false; }    return true;}//executes a function in the specified LUA fileBOOLCluafn:callfilefn (Const Char*pfunctionname,intNPARAM1,intnParam2) {    intNret =0; if(M_pstate = =NULL) {        return false; } lua_getglobal (M_pstate, pfunctionname); //Verify that the LUA function is in the currently loaded LUA file and point the pointer to the function locationLua_pushnumber (M_pstate, nParam1);//Press stack operation to push data into the data stackLua_pushnumber (M_pstate, nParam2);}

Interaction between Lua and C + + (1)

Related Article

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.