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

Should we explicitly cast from Lua's luaL_checkinteger() et al?

Lua 5.3 deprecates luaL_checkint, luaL_checklong, luaL_optint, luaL_optlong, which were just convenience macros calling luaL_{check|opt}integer. While we can still use them (because of -DLUA_COMPAT_5_2), they aren't mentioned in the user manual and…
Niccolo M.
  • 3,363
  • 2
  • 22
  • 39
3
votes
1 answer

Lua arguments at wrong memory location

When parsing variables from lua, lua is acting strangely. C++: int LuaManager::SetTimer(lua_State *pLua) { if (!lua_isstring(pLua, 0)) throw "expected: string"; if (!lua_isnumber(pLua, 1)) throw "expected: number"; std::string callback…
R4VANG3R
  • 415
  • 1
  • 4
  • 11
3
votes
2 answers

How can I pre-compile the Lua that I pass to luaL_loadbuffer()?

I am using the Lua "C" API function luaL_loadbuffer() to run Lua code. I have a small handful of Lua chunks that I am calling many, many times. But every time I call luaL_loadbuffer() the chunk gets recompiled. This seems hugely inefficient. Most…
the.jxc
  • 3,373
  • 21
  • 21
3
votes
1 answer

lua5.2 call c dll in windows

My C code is below: skypeAnalyzer.h #include "lua.h" #include "lualib.h" #include "lauxlib.h" #include "stdio.h" #include "stdlib.h" //dll export func int _declspec(dllexport) luaopen_skypeAnalyzer(lua_State* L); skypeAnalyzer.c #include…
3
votes
2 answers

Lua package containing subpackages

I have written a number of modules for Lua in C. Each of these contains a Lua userdata type and I load and use them like this: A = require("A") B = require("B") a = A.new(3,{1,2,3}) b1 = B.new(1) b2 = B.new(2) * b1 Now I would like to put both…
1k5
  • 342
  • 6
  • 15
3
votes
3 answers

Nested lua_CFunction calls

What is the best way to deal with nested lua_CFunction calls? Assume I have two function like this: static int function2(lua_State *L) { int i = luaL_checkint(L, 1); /* do something */ return 1; }; static int function1(lua_State *L) { …
1k5
  • 342
  • 6
  • 15
3
votes
1 answer

retrieving the module object on Lua

I have a C program that uses Lua to run some scripts. I need to open the Lua libraries via C code like luaopen_socket_core(myLuaState), for some reasons I can't load the modules from the Lua code, like socket = require "luasocket". Once understood…
3
votes
2 answers

Getting array value from index using Lua C Api

I have this array: a = {{4,2,2,6}, {2,1,1,2}} How can I retrieve an index from that array to a C program? For example: a[1] -- {4,2,2,6} a[1][2] -- 2
Victor Martins
  • 739
  • 7
  • 21
3
votes
1 answer

Create properties and methods Lua C++

This is rather tricky to explain and I could not find anything on this in the documentation or anywhere on the net so I thought this would be a suitable place for this question. I'm trying to register properties and methods on an object in Lua using…
3
votes
1 answer

Lua :new from C API

I am working on a scripting layer for my game engine. Currently I am using a Script as a Class, adding a method to the "Table" named new. This function basically created an instantiated copy of the Class. I call this function from the C API when an…
Don Duvall
  • 839
  • 6
  • 10
3
votes
1 answer

How do I handle errors in Lua when executing arbitrary strings?

I'm going for absolute minimalism here. (It's been a while since I've worked with the Lua C API.) #include #include #include using namespace std; int main(int argc, char** argv) { lua_State* state =…
TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
3
votes
1 answer

seeking a "weak Lua registry"

Is there a way for C to reference an object in Lua 5.2 in the same way that you would use the reference system except that these references are weak and may be garbage collected at any time? In other words, is there a registry of all current Lua…
Paul
  • 2,973
  • 6
  • 31
  • 40
2
votes
1 answer

Calling Lua Function from C: Minimal example results in LUA_ERRRUN

I want to call an external lua_5.2 function from C, so I made a minimal example to try it out. The minimal testfile: --- filename: play.lua function hello() print("Hello World!\n") end Trying to call this function from C: #include…
kollie
  • 86
  • 5
2
votes
1 answer

How do user values in Lua C API and lua_newuserdatauv function in particular work?

The documentation for lua_newuserdatauv(lua_State *L, size_t size, int nuvalue) says This function creates and pushes on the stack a new full userdata, with nuvalue associated Lua values, called user values, plus an associated block of raw memory…
Sun of A beach
  • 171
  • 1
  • 10
2
votes
1 answer

Not able to modify C++ object passed as parameter to Lua function

I have been working on SDL2 2D Game Engine for several years now. Just ditched inheritance approach to define game entities with composition approach where I have Entity class and it has vector of Component classes and recently I got into lua,…
kaktusas2598
  • 641
  • 1
  • 7
  • 28