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

How to create Lua table from C structure

I'm trying to create a Lua table from C. I've got a problem: I'm trying to create a Lua table using C. The table should be like this: CharList = { [1] = {name = "Kyra", is_monster = false} [2] = {name = "Saya", is_monster = false} [3] =…
2
votes
1 answer

Lua crashed when metatable __index pointing to a function and returned value is not used

I'm trying to use function as metatable __index to achieve more flexibiilty, but it crashed when the returned value is not assigned to some variable in Lua side. This is the C++ part, it just creates a table variable whose __index is pointing to a C…
jiandingzhe
  • 1,881
  • 15
  • 35
2
votes
1 answer

What is the difference with dofile, loadfile and loadstring(file:read()())?

Would there be any performance boost from using one of the function. Is there a internal difference from using these two function, if so what are they.
user12136376
2
votes
1 answer

require()ing a dll within a subdirectory in Lua

Lua's require() function, if called on a .dll, will look for a function called luaopen_. What should I do if I want to say require("folder1.folder2.library")? It's not like I can name a function luaopen_folder1.folder2.library. I…
NetherGranite
  • 1,940
  • 1
  • 14
  • 42
2
votes
1 answer

How do I compile code that uses Lua 5.1's C API?

I have code that #includes the files lua.h, lapi.h, lualib.h, and lauxlib.h from Lua's source. Now I have to actually compile this code. My first thought is to include all of the .c files in the Lua's source code or just figure out which of those .c…
NetherGranite
  • 1,940
  • 1
  • 14
  • 42
2
votes
1 answer

Lua C API: Delete metatable created with luaL_newmetatable?

How can I delete a metatable foo created with luaL_newmetatable( L, "foo" );, so that luaL_getmetatable( L, "foo" ); will push a NIL value again?
Larpi
  • 21
  • 2
2
votes
1 answer

Lifetime of Lua object "globally returned" to C

So I have a very simple Lua script like this: return coroutine.create(function () coroutine.yield(1) end) And then in C I run it and gets the returned value lua_State* l = luaL_newstate(); if(luaL_dostring(l, script) == LUA_OK) { lua_State* co =…
KevinResoL
  • 982
  • 8
  • 19
2
votes
0 answers

using lua_call/lua_pcall without popping the stack

When I use lua_call or lua_pcall, in addition to running the lua chunk, it also pops the chunk from the stack. Is there any way I can run the chunk while still keeping the chunk on the stack?
Ace shinigami
  • 1,374
  • 2
  • 12
  • 25
2
votes
1 answer

Lua c API change library after creation

I am trying to wrap ncurses in Lua using the C API. I am working with the stdscr pointer: This is NULL before initscr is called, and initscr is called from Lua by design of my bindings. So in the driver function I do this: // Driver…
AlgoRythm
  • 1,196
  • 10
  • 31
2
votes
1 answer

Without knowing the userdata implementation, can I use its fields from inside C++?

I can obtain a userdata from inside my C++ code. I want to know if I can cast it to something so I can dereference its fields and invoke its methods without going through Lua. In other words: Once I obtain an userdata and put it in the Lua stack,…
FinnTheHuman
  • 1,115
  • 13
  • 29
2
votes
0 answers

Is there a way to tell apart a Lua closure from a C closure?

Is there a way to find out through the Lua C API if a function value is a Lua or a C closure? The first thing that came to my mind was the lua_type function but apparently it can only can tell if something is a function. The reason I am asking I'm…
hugomg
  • 68,213
  • 24
  • 160
  • 246
2
votes
1 answer

What's the difference between "dofile" in lua and "luaL_dofile" in the C API?

Using the Lua 5.3.4 C API, this works: luaL_dostring(lua, "dofile('mnemonics.lua')"); But this fails to execute the file: luaL_dofile(lua, "mnemonics.lua"); Instead, it reports "attempt to call a string value". When I replace it…
Octa9on
  • 183
  • 8
2
votes
1 answer

Lua Meta Event Argument Order

I'm using the Lua API to override meta-events for my own C++ objects wrapped as userdata. However, some meta-events can take multiple arguments that may be userdata or regular values which I can convert to a userdata. For example, __add, __eq,…
Lars
  • 233
  • 2
  • 9
2
votes
1 answer

How to check if two values in Lua are primitively equal?

When overriding equality operators in Lua (via the "__eq" metamethod), is there a way to still check for primitive equality (ie. not calling the overridden __eq, but checking if the two table values are referentially the same?) I need to do this…
BadZen
  • 4,083
  • 2
  • 25
  • 48
2
votes
1 answer

Insert function inside subtable using Lua C API

I am making my own game engine, using Lua C API. I got such Lua table hierarchy: my_lib = { system = { ... }, keyboard = { ... }, graphics = { ... }, ... } Also I got some C function, I want to register, something like that: inline static…