Questions tagged [lua-api]

Anything related to Lua C Application Programming Interface (Lua C-API).

Lua has been designed to be easily integrated with C applications, thus it provides a rich API to interact with C code, which is called the Lua C-API.

The Lua C-API can be used both for:

  • enabling the Lua engine to communicate with an host application (the application which embeds the engine), and

  • enabling library code for Lua written in C to communicate with the Lua engine that loads the library.

197 questions
6
votes
1 answer

Read Lua table from C

I'm trying to pass a Lua table to my C program but I don´t know how to do it. My Lua code: local stages = {} stages[1] = stage1 stages[2] = stage2 stages[3] = stage3 lstage.buildpollingtable(stages) My C code: static int lstage_build_polling_table…
briba
  • 2,857
  • 2
  • 31
  • 59
6
votes
1 answer

Weak table and GC finalizer using C API

I am attempting to create a GC finalizer for a function value by storing it in a weak table using the C API. I started off by writing a prototype in pure Lua 5.2: local function myfinalizer() print 'Called finalizer' end function myfunc() …
Adam
  • 3,053
  • 2
  • 26
  • 29
5
votes
1 answer

How to create Lua table with name C-API

How to create Lua table from C-API like this: TableName = {a, b, c} How to set table name? I only know how to create table and put values, but don't know how to set name of table. Code for creating table without name: lua_createtable(L, 0,…
BORSHEVIK
  • 794
  • 1
  • 8
  • 12
5
votes
2 answers

How to register C++ class constructor to Lua userdata and use it by default

Using the Lua C API, I registered a simple Object class to Lua, like this: // My C++ Object class class Object { private: double x; public: Object(double x) : x(x){} }; // Create and return instance of Object class to Lua int…
Erunehtar
  • 1,583
  • 1
  • 19
  • 38
4
votes
1 answer

Call a function returned by a lua script from C

Given a lua file like -- foo.lua return function (i) return i end How can I load this file with the C API and call the returned function? I just need function calls beginning with luaL_loadfile/luaL_dostring.
Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
4
votes
2 answers

lua_isstring() check for real strings in Lua

int lua_isstring (lua_State *L, int index); This function returns 1 if the value at the given acceptable index is a string or a number (which is always convertible to a string), and 0 otherwise. (Source) Is there a (more elegant) way to…
user1511417
  • 1,880
  • 3
  • 20
  • 41
4
votes
2 answers

Storing a C structure in the Lua registry

As I'm integrating Lua into my C program, I've been using a static pointer to a C struct to store an object I need to reuse in methods that I bind to the Lua state. However, this does not work once I split my Lua lib from the main program, so it…
raphink
  • 3,625
  • 1
  • 28
  • 39
4
votes
1 answer

C++ Lua Getting Value From Lua Table

I am trying to get values from a Lua table. This is what I have written in Program.cpp: lua_State* lua = luaL_newstate(); luaL_openlibs(lua); luaL_dofile(program->getLuaState(), "Script.lua"); lua_getglobal(lua, "table"); lua_pushstring(lua,…
Erik W
  • 2,590
  • 4
  • 20
  • 33
4
votes
1 answer

Adding a userdata metatable to a lua table

I've got a scripting system working well using userdata objects. However, I now want to have a property on my userdata that can take a regular table. I think what I should do is create a normal table and set the metatable to use my current set of…
Phil Lello
  • 8,377
  • 2
  • 25
  • 34
4
votes
2 answers

lua function argument expected near

I try to use lua in a C++ project. For lua executing I write this: #include ... luaEngine = luaL_newstate(); luaL_openlibs(luaEngine); register_results(luaEngine); // For register c++ object in the LUA script as…
Elijah
  • 81
  • 1
  • 5
4
votes
2 answers

Lua 5.2 - C++ object within an object (using lua_lightuserdata)

edit: [SOLUTION IN ANSWER 2] I am new to LUA and am having trouble trying to do what I want. I have a C++ object that looks like this: C++ Object definitions struct TLimit { bool enabled; double value; TLimit() : enabled(false),…
4
votes
2 answers

Can luaL_loadbuffer load multiple files in one call?

I know how to load a Lua file via luaL_loadbuffer. Now I have many Lua files, more than 100. I am thinking about how to speed up the loading process. One way I figured out is: put all files into one, and then load this file using luaL_loadbuffer (I…
gzyuan888
  • 93
  • 1
  • 6
4
votes
2 answers

Luad using stand-alone Lua

EDIT: This has been tracked down to a more general problem with shared libraries, the d runtime and os x. See here: Initializing the D runtime on OS X I'm trying to get a simple d function accessible from the Lua stand-alone interpreter. I couldn't…
John_C
  • 788
  • 5
  • 17
3
votes
0 answers

How to return shared_ptr nullptr on a method with reference&?

My code is: template static shared_ptr& getSharedPtr(lua_State* L, int32_t arg) { return *static_cast*>(lua_touserdata(L, arg)); } This method returns a reference to a shared_ptr, so the actual reference counter…
3
votes
1 answer

How to get metatable set by lua from Lua C API

Lua: a = { b = "c", d = { e = "f", g = "h" } } setmetatable(a.d, {__ismt = true}) cfun(a) --call C function to iterate over table a C: int cfun(lua_State *L) { lua_pushnil(L); while (lua_next(L, -2) != 0) …
John Doe
  • 63
  • 4
1
2
3
13 14