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
2
votes
1 answer

Lua sandbox _ENV with personal baselib (require, assert etc)

Lua 5.3.2 I have a service written in Lua/C, that executes lua files in the same Lua_State. I need to provide all the standard libraries for the file execution environment. The simplest thing is to execute files this way: loadfile(file_path, "bt",…
marsgpl
  • 552
  • 2
  • 12
2
votes
1 answer

How to limit the amount of characters in a Lua string

I have a question: how do I make the lua_tostring function limit the amount of entries. For example 8 characters limit: poppy_name = "command"; // OK, within the limit; poppy_name = "commander"; // Fail, Is out of range, show a message the permitted…
Marcos Silva
  • 53
  • 1
  • 6
2
votes
1 answer

Set methods to lua tables from C/C++/Rust

I want to set extra "methods" to some tables that are in some lua_State. The state has the code like this: obj = {} function obj:method1() print("value from second method = " .. self.method2()) end I load it by do_file() method. After this I…
Revertron
  • 1,213
  • 2
  • 13
  • 17
2
votes
1 answer

How to compare 2 functions passed as argument

My application is able to push a function in a Todo list by using a code like : function test() print("coucou") end todo:pushtask( test ) -- First push todo:pushtask( test ) -- Second push Internally, the todo list is a C table of…
2
votes
1 answer

Calling C library function from Lua

I'm trying to load the following C library in Lua: #include #include #include #include #include void stackDump(lua_State* lua) { int i, t; int top = lua_gettop(lua); printf("Stack…
Doddy
  • 1,311
  • 1
  • 17
  • 31
2
votes
2 answers

When writing a Lua-facing function in C, what's a good way to check if an argument supports table-like lookups?

Here's a potential pattern that can check if an argument is a table: int my_fn(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); // .. do stuff with the table .. } This works whenever the first argument is a table. However, other Lua types…
Tyler
  • 28,498
  • 11
  • 90
  • 106
2
votes
1 answer

Lua and C++ Binding - What does this line mean?

The following code is to bind a C++ class to Lua. void registerPerson(lua_State *lua, Person *person) { //We assume that the person is a valid pointer Person **pptr = (Person**)lua_newuserdata(lua, sizeof(Person*)); *pptr = person;…
Alex
  • 245
  • 1
  • 2
  • 6
2
votes
1 answer

multiple Lua VM's detected

I'm using the Lua52.exe binary that you can download from Lua's site. I want to extend it's functionality with a DLL that I write. So I wrote a DLL where I included the Lua source to my VS DLL project. That code is below. When I do the following in…
user441521
  • 6,942
  • 23
  • 88
  • 160
2
votes
0 answers

Lua setting metatable functions for C++

I found 2 types of code for implementing metatable functions. I don't really understand the first, and what's the difference with the second. The first: lua_newtable(l); int methods = lua_gettop(l); luaL_newmetatable(l, "MapCreator"); int metatable…
neiji93
  • 21
  • 4
2
votes
1 answer

Extract userdata from table,using lua c API

I use lua c api to loop the variables in a table, like this lua script: array = {0,1,2,3} lua c api lua_getglobal(l, "array"); if(lua_isnil(l, -1)) { } lua_pushnil(l); while(lua_next(l, -2)) { int value=(int)lua_tonumber(l, -1); …
BPS1945
  • 153
  • 1
  • 1
  • 9
2
votes
1 answer

How to get the complicated arguments of a Lua function in C language?

My Lua functions will call the C functions, one of which is very complicated as below, how can I get all the arguments in C? The argument colors is an array of {color, x, y} struct type elements, and it has uncertain number count.the argument region…
Suge
  • 2,808
  • 3
  • 48
  • 79
2
votes
2 answers

./lua/addtest.lua:9: attempt to index local 'testobj' (a userdata value)]]

test.exe call addTest.lua and set the lua_testobj to the table, and addTest.lua call testobj.dll, but testobj.dll can not get the "lua_testobj" error msg is addTest.lua:9 attempt to index local 'testobj' (a userdata value) test.exe L =…
Sleepwom
  • 227
  • 6
  • 15
2
votes
1 answer

How does luabind implicitly cast objects?

Without going into too many irrelevant details about a large multi-library project I'm working on I'd like to ask a question about a specific event that occurred in it. I'm using Luabind to have my C++ code interface with my Lua code and I have a…
Jake Kiesel
  • 341
  • 4
  • 10
2
votes
0 answers

Lua issue when attempting to run a script on a separate lua state

I'm new to embedding Lua and have managed to get this far along my journey. I store the result of luaL_loadfile into a global so it can be called repeatedly. My current issue at hand is that when I attempt to run a script inside of a script on a…
bsdunx
  • 137
  • 7
2
votes
2 answers

Create new C library in lua

I want to know how i can create and use a new C library in lua 5.2.3. I can't use dynamic library (require, shared library, ...) due to I am on an embedded system. I found an answer but it is for lua 5.0 (http://www.lua.org/pil/26.2.html) and so it…
Subas
  • 65
  • 6