Lua vs. C + + Interaction series: Calling Lua functions via C (1) __jquery

Source: Internet
Author: User
Tags lua

1. Lua is written through ANSI C, and LUA interacts with C to follow certain protocol rules. The LUA 5.1 Reference manual explicitly stipulates how the functions written by Lua are called by C code.

C code can invoke any function written by LUA code. This article mainly takes the global function to do the demonstration.


2, first of all, the C language calls the LUA function protocol rule description. The protocol rules are described in detail in the description of the Reference Manual in Lua 5.1 the void Lua_call (lua_state *l, int nargs, int nresults) .



void Lua_call (lua_state *l, int nargs, int nresults);

To call a function for your must use the following protocol:

The function of the "a" to be called is pushed onto the stack;

Then, the arguments to the function are pushed into direct order;

This is, the "argument" is pushed.

Finally you call Lua_call;

Nargs is the number of arguments this you pushed onto the stack.

All arguments and the function value are popped from the stack ' when the ' function is called.

The function results are pushed onto the stack when the function returns.

The number of results is adjusted to nresults and unless Nresults is Lua_multret.

In this case, all results from the function are pushed. Lua takes care the returned values fit into the stack space.
The function results are pushed onto the stack in direct order (the ' is pushed-a-i ' The top of the, and the top of the stack.

Calling the LUA function must use the following protocol: 1. Press the function name into the virtual stack. 2, from left to right in turn, press the parameters into the virtual stack 3, call the Lua_call () function.

In the Lua_call () function,Nargs Specifies the number of parameters for the LUA code function,nresults Specifies the number of results to be pushed back into the virtual stack when the LUA code function call completes.

3. Call the LUA code global function in C code

Sample_1.cpp file

#include <iostream>
using namespace std;
extern "C" 
{
	#include "lua.h"
	#include "lauxlib.h"
	#include "lualib.h"
}
///To facilitate explanation, in C In code, the LUA code function is called with calllua_xxx named
int  calllua_sum (lua_state *l,int A, int b)
{
	//_g["name"] The function presses into the virtual stack above
	lua_getglobal (L, "sum");
	On-time LUA function order, from left to right to press into the parameter
	lua_pushnumber (l,a);
	Lua_pushnumber (l,b);
	Call the Lua_call () function and actually call the LUA code function
	Lua_call (l,2,1);
	When the LUA code function call ends, the result value is pressed into the virtual stack
    int sum= (int) lua_tonumber (l,-1);
	return sum;
}
int main (int argc, char **argv)
{         //create lua_state
	  lua_state *l = Lua_open ();  /* Create State
	  ///Registration standard library
	  lual_openlibs (L);
	  Implementation
	  of Sample_1.lua Lual_dofile (L, "Sample_1.lua");
	  Call the LUA code function
	  int sum =calllua_sum (l,1, 2);
	  cout<< "sum:" <<sum<<endl;
	  Closure of Lua_state
	  Lua_close (L);
           return 1;
}

Sample_1.lua file

Print ("Example_1.lua")
--Defines the global variable  sum function functions
sum (a,b) return
  a+b
end


4. Call any function in the LUA code in C code. Take the field function in table as an example

int  calllua_fieldsum (lua_state *l,int A, int b)
{
	//push _g["luatable"] table data structure onto the virtual stack
	Lua_getglobal (L, "luatable");
	The luatable["Fieldsum"] function is pressed into the virtual stack above
	Lua_getfield (l,-1, "fieldsum");
	Remove the _g["luatable" from the virtual stack
	lua_remove (l,-2);
	On-time LUA function order, from left to right to press into the parameter
	lua_pushnumber (l,a);
	Lua_pushnumber (l,b);
	Call the Lua_call () function and actually call the LUA code function
	Lua_call (l,2,1);
	When the LUA code function call ends, the result value is pressed into the virtual stack
       int sum= (int) lua_tonumber (l,-1);
	return sum;
}

--Define a global table
luatable={}
--Define a function in the table field
luatable["Fieldsum"]=function (a,b) return a+b end

The LUA code function is the simplest one to invoke in Lua when interacting with C + + language. Because the LUA C API is designed for C, C + + contains the C language, the rationale for calling the LUA code function has always been.

C + +, however, provides a template mechanism at compile time, which allows for greater flexibility. At the same time, in the lua_pushxxx () function, only a few basic data types are provided, which can be extended by itself.



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.