Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose C++ functions to a lua script?

Tags:

c++

function

lua

I just successfully created an lua project. (A simple code that runs an lua script so far.)
But how would I make a c++ function and a c++ variable available for the lua script now?

As an example:

int Add(int x, int y) {
    return x + y;
}

and

float myFloatValue = 6.0

I'm very new to c++ so I really hope that it won't be too complicated. Here is the code I got so far btw:

#include "stdafx.h"
extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}

using namespace System;

int main(array<System::String ^> ^args)
{
    lua_State* luaInt;
    luaInt = lua_open();
    luaL_openlibs (luaInt);
    luaL_dofile (luaInt, "abc.lua");
    lua_close(luaInt);
    return 0;
}
like image 730
Forivin Avatar asked Oct 15 '13 12:10

Forivin


People also ask

How do you call a function in C Lua?

Moreover, for a C function to be called from Lua, we must register it, that is, we must give its address to Lua in an appropriate way. When Lua calls a C function, it uses the same kind of stack that C uses to call Lua. The C function gets its arguments from the stack and pushes the results on the stack.

Can you write C in Lua?

Lua provides an interface allowing to call C functions in the Lua environment. Lua uses a virtual stack for passing values between Lua and C. The Lua function pushes the parameters into the stack, then C function consumes the values and pushes the result values to stack.

What is Lua C API?

The C API is the set of functions that allow C code to interact with Lua. It comprises functions to read and write Lua global variables, to call Lua functions, to run pieces of Lua code, to register C functions so that they can later be called by Lua code, and so on.


1 Answers

I'll go with John Zwinck's answer as experience has proven to me that using Lua all by itself is a pain in the butt. But, if you want to know the answer check the rest.

For registering C/C++ functions you need to first make your function look like a standard C function pattern which Lua provides:

extern "C" int MyFunc(lua_State* L)
{
  int a = lua_tointeger(L, 1); // First argument
  int b = lua_tointeger(L, 2); // Second argument
  int result = a + b;

  lua_pushinteger(L, result);

  return 1; // Count of returned values
}

Every function that needs to be registered in Lua should follow this pattern. Return type of int, single parameter of lua_State* L. And count of returned values.

Then, you need to register it in Lua's register table so you can expose it to your script's context:

lua_register(L, "MyFunc", MyFunc);

For registering simple variables you can write this:

lua_pushinteger(L, 10);
lua_setglobal(L, "MyVar");

After that, you're able to call your function from a Lua script. Keep in mind that you should register all of your objects before running any script with that specific Lua state that you've used to register them.

In Lua:

print(MyFunc(10, MyVar))

Result:

20

like image 168
MahanGM Avatar answered Oct 05 '22 10:10

MahanGM