lua

how to callback a lua function from a c function

Hi, I have a C function (A) test_callback accepting a pointer to a function(B) as the parameter and A will "callback" B. //typedef int(*data_callback_t)(int i); int test_callback(data_callback_t f) { f(3); } int datacallback(int a ) { printf("called back %d\n",a); return 0; } //example test_callback(datacallback); ...

Lua pattern matching vs. regular expressions

Hello, I'm currently learning lua. regarding pattern-matching in lua I found the following sentence in the lua documentation on lua.org: Nevertheless, pattern matching in Lua is a powerful tool and includes some features that are difficult to match with standard POSIX implementations. As I'm familiar with posix regular expressions...

In Lua, can I easily select the Nth result without custom functions?

Suppose I am inserting a string into a table as follows: table.insert(tbl, mystring) and that mystring is generated by replacing all occurrences of "a" with "b" in input: mystring = string.gsub(input, "a", "b") The obvious way to combine the two into one statement doesn't work, because gsub returns two results: table.insert(tbl, s...

Lua : How to check if one of the values associated with the specified key of a table is nil, from api

In lua, it's legal to do this : table={} bar if(table[key]==nil) then foo However, using C API, I couldn't find way to check if there's a nil value on the specified position. lua_getglobal(L,"table"); lua_gettable(L,key); If there's a nil value stored in table[key], lua_gettable would give me the "unprotected error in call to Lua A...

How to get number of entries in a Lua table?

Sounds like a "let me google it for you" question, but somehow I can't find an answer. The Lua # operator only counts entries with integer keys, and so does table.getn: tbl = {} tbl["test"] = 47 tbl[1] = 48 print(#tbl, table.getn(tbl)) -- prints "1 1" count = 0 for _ in pairs(tbl) do count = count + 1 end print(count) ...

Wiki-fying a text using LPeg

Long story coming up, but I'll try to keep it brief. I have many pure-text paragraphs which I extract from a system and re-output in wiki format so that the copying of said data is not such an arduous task. This all goes really well, except that there are no automatic references being generated for the 'topics' we have pages for, which e...

Register C function in Lua table

How to register a C function in Lua, but not in a global context, but as a table field? ...

Load a Lua script into a table named after filename

I load scripts using luaL_loadfile and then lua_pcall from my game, and was wondering if instead of loading them into the global table, I could load them into a table named after their filename? For example: I have I file called "Foo.lua", which contains this: function DoSomething() --something end After loading it I want to be ...

How to handle unknown initializer functions in lua?

I want to load data written in a variant of lua (eyeonScript). However, the data is peppered with references to initialization functions that are not in plain lua: Redden = BrightnessContrast { Inputs = { Red = Input { Value = 0, }, }, } Standard lua gives "attempt to call a nil value" or "unexpecte...

Launching Vim via Lua

I'm writing a simple little Lua commandline app that will build a static website. I'm storing my fragments in a sqlite database. Retrieving the data from the db is straightforward as is saving it; my question comes from editing the data. Is there an elegant way to pipe the data from Lua to vim? Can vim edit a memory buffer and return it...

Lua metatable Objects cannot be purge from memory?

Hi there, I'm using a proprietary platform that reported memory usage in realtime on screen. I decided to use a Class.lua I found on http://lua-users.org/wiki/SimpleLuaClasses However, I noticed memory issues when purging object created by this using a simple Account class. Specifically, I would start with say 146k of memory used, crea...

webframeworks in lua?

Are there any good webframeworks in Lua? Thanks! ...

destructors on gc-ed lua objects

Hi! I know that Lua is gc-ed. I know that Lua can deal with c objects via userdata. Here is my question: is there anyway to register a function so that it's called when a C userdata object is gc-ed by lua? [Basically a destructor]. Thanks! ...

quasiquote/quote in lua?

In Lisp, I can have: (a b c d e f g) which means look up b, c, d, e, f, g look up a; apply value of a to above Then, I can also have: `(a b c d e f g) which is the equiv to (list 'a 'b 'c 'd 'e 'f 'g) Now, in lua, I can have: [snipplet 1] foo = { Foo, {Cat, cat}, {Dog, dog} }; Which ends up most likely expanding int...

lua string printing

In C, I have format strings, something like: char *msg = "wlll you marry me" fprintf(stderr, "%s, %s?", name, msg); Now, can I do something similar in lua with format strings? I.e. I want something functionally equivalent to: name .. ", " .. msg .. "?" but not so ugly, in lua. Okay, so I can do string.format("%s, %s?", name, msg),...

Returning a lua table on SWIG call

I have a class with a method called GetEnemiesLua. I have bound this class to lua using SWIG, and I can call this method using my lua code. I am trying to get the method to return a lua table of objects. Here is my current code: void CGame::GetEnemiesLua(){ std::vector<Unit*> enemies = callback->GetEnemyUnits(); if( enemies.empty())...

llvm bindings in lua?

Does LLVM have lua bindings? I'm not interested in using LLVM as a backend /JIT for Lua. I want access to LLVM through lua. Thanks! ...

In Lua, how to pass vararg to another function while also taking a peek at them?

It seems that in Lua, I can either pass vararg on to another function, or take a peek at them through arg, but not both. Here's an example: function a(marker, ...) print(marker) print(#arg, arg[1],arg[2]) end function b(marker, ...) print(marker) destination("--2--", ...) end function c(marker, ...) print(marker) print(#ar...

Lua + SWIG Monkey Patching

I have used SWIG to bind a set of classes to lua. I know C++ itself doesn't support monkey patching, and I'm not trying to modify my C++ objects, merely their lua representations. The problem comes if I want to start monkey patching the lua tables and objects exported by SWIG, so that I can modify the API presented on the lua side. e.g....

Self referencing userdata and garbage collection

Because my userdata objects reference themselves, I need to delete and nil a variable for the garbage collector to work. Lua code: obj = object:new() -- -- Some time later obj:delete() -- Removes the self reference obj = nil -- Ready for collection C Code: typedef struct { int self; // Reference to the object int callb...