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
20
votes
4 answers

Print stacktrace from C code with embedded lua

If I understand this correctly, Lua by default will call the debug library "debug.traceback" when an error occurs. However, when embedding Lua into C code like done in the example here: Simple Lua API Example We only have available the error message…
hookenz
  • 36,432
  • 45
  • 177
  • 286
12
votes
1 answer

Execution time limit for a Lua script called from the C API

luaL_loadfile(mState, path.c_str()); lua_pcall(mState, 0, 0, 0); Is there a way to put an execution time limit (say 10-20 seconds) for those two C++ statements, that load and then execute a lua file? Since the Lua file is untrusted I don't want a…
Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
8
votes
3 answers

How to create nested Lua tables using the C API

I want to create a table like myTable = { [0] = { ["a"] = 4, ["b"] = 2 }, [1] = { ["a"] = 13, ["b"] = 37 } } using the C API? My current approach is lua_createtable(L, 0, 2); int c = lua_gettop(L); lua_pushstring(L, "a"); lua_pushnumber(L,…
Etan
  • 17,014
  • 17
  • 89
  • 148
7
votes
2 answers

Cloning a Lua table in Lua C API

There are heaps of examples of how to clone a Lua table in Lua, however I wasn't able to find any example of how to do it with the native Lua C API. I tried to do it by hand twice, but ended up with a real (although working) mess. Does anyone have…
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
7
votes
1 answer

Iterating over table of tables with the Lua C API

I'm trying to iterate over a table of tables in Lua and output: The key of each table. The key / value pair of each entry in each table. Here is the code: void print_table(lua_State *L) { lua_pushnil(L); while(lua_next(L, -2) != 0) { …
lamma
  • 73
  • 1
  • 5
7
votes
2 answers

Get all errors from lua_pcall(L, 0, 0, 0)

Is it possible to get all the errors in the lua stack from C/C++? here is what I've tried c++ int main() { lua_State* L = luaL_newstate(); luaL_openlibs(L); if (luaL_loadfile(L, "LuaBridgeScript.lua")) { throw…
Zephilinox
  • 73
  • 2
  • 5
7
votes
1 answer

lua_open returns null using luaJIT

Using the recent luaJIT lua_open returns null. This does not happen with the regular lua library. lua_State *L = lua_open(); std::cout << L << std::endl; Output: 0x0 How can I get luaJIT to work? SSCCE: #include #include…
Appleshell
  • 7,088
  • 6
  • 47
  • 96
7
votes
5 answers

Primary source for WoW lua API?

I've been looking for first-hand information on the World of Warcraft addon API. There are a couple wikis that are pretty good, but their reference links only point internally. Surely there is some information published by Blizzard on the topic. Can…
bukzor
  • 37,539
  • 11
  • 77
  • 111
6
votes
2 answers

From C, how can I print the contents of the Lua stack?

My C program probably has a silly bug. There is a certain point where the Lua stack doesn't contain the values that I think it should. In order to debug it, I want to print the contents of the Lua stack at a certain point of my program. How can I…
hugomg
  • 68,213
  • 24
  • 160
  • 246
6
votes
3 answers

Multiple scripts in a single Lua state and working with _ENV

I'm currently learning how to use the Lua C API and while I've had success binding functions between C/C++ and Lua, I have a few questions: Is it a good idea to load multiple scripts into a single lua_State? Is there a way to close specific chunks?…
inzombiak
  • 171
  • 2
  • 14
6
votes
3 answers

How to execute an untrusted Lua file in its own environment from the C API

I want to execute an untrusted .lua file in its own environment by calling lua_setfenv() so that it cannot affect any of my code. The documentation for that function though only explains how to call a function, not how to execute a file. Currently…
Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
6
votes
1 answer

lua_Integer and lua_createtable (table size limit)

In Lua 5.3 table related functions in the C API receive and return lua_Integer. void lua_rawgeti (lua_State *L, int idx, lua_Integer n); void lua_rawseti (lua_State *L, int idx, lua_Integer n); lua_Integer luaL_len (lua_State *L, int index); But,…
Adam
  • 3,053
  • 2
  • 26
  • 29
6
votes
1 answer

lua c read nested tables

below is the lua table i need to read from C: listen = { { port = 1234, address = "192.168.1.1", userdata = "liunx" }, { port = 1235, address = "192.168.1.2", userdata = "liunx1" }, { port = 1236, address = "192.168.1.3", userdata =…
liunx
  • 751
  • 4
  • 13
  • 32
6
votes
1 answer

Lua userdata array access and methods

I am writing in C a userdata type for use in Lua. It has some array-type properties and various methods aswell. Right now if u is of this type, I use u:set(k,v) resp. u:get(k) to access data and e.g. u:sort() as method. For this I set __index to a…
1k5
  • 342
  • 6
  • 15
6
votes
2 answers

Get Lua table size in C

How can I get a size of a Lua table in C? static int lstage_build_polling_table (lua_State * L) { lua_settop(L, 1); luaL_checktype(L, 1, LUA_TTABLE); lua_objlen(L,1); int len = lua_tointeger(L,1); printf("%d\n",len); …
briba
  • 2,857
  • 2
  • 31
  • 59
1
2 3
13 14