News:
Welcome to the Updated Lua Forum!
1 - General / Integration / Re: LUA whitelisting libraries and functions
Started by kng - Last post by shawn on: September 07, 2010, 07:10:21 am
after some reading i managed to find out that i need to make use of the lua_setfenv function to setup the enviorment but is it as simple as i think it is just loading the C structure into a table and calling lua_setfenv?
2 - General / Integration / LUA whitelisting libraries and functions
Started by kng - Last post by kng on: September 07, 2010, 02:48:30 am
i have a structure in C containing all the libraries and functions inside the library i wish to load simply put its a whitelist of the enviroment i wish to used. please see the example below
_G = { "*" },
debug = {"*"},
coroutine = {"*"},
io = {"*"},
math = {"*"},
table = {"*"},
string = {"*"},
os = {"*"},
My main question is how am i able to implement such a whitelist to alter my sandbox enviroment. Any reading material and code examples are greatly appreciated
_G = { "*" },
debug = {"*"},
coroutine = {"*"},
io = {"*"},
math = {"*"},
table = {"*"},
string = {"*"},
os = {"*"},
My main question is how am i able to implement such a whitelist to alter my sandbox enviroment. Any reading material and code examples are greatly appreciated
3 - General / Integration / Re: Integrating Lua; getting lightuserdata..
Started by debu - Last post by dirkf on: September 06, 2010, 01:46:10 pm
With full userdata you're letting Lua allocate (and garbage collect) memory for the objects by calling lua_newuserdata() instead of malloc (or via placement new in C++). This can solve or at least simplify the ownership issues I mentioned.
Via metatables you can then 'decorate' this userdata so it behaves like an actual object in Lua: you can access its members as if you access a field in a table, call a method on it, ... You can even, via the __gc metamethod, be notified when the GC is cleaning up your object so you can release any resources you might store in the object. See this example I posted in another thread.
I don't know Obj-C enough to be able to tell if and how to do this but since it's closely related to C I guess it should be possible.
Via metatables you can then 'decorate' this userdata so it behaves like an actual object in Lua: you can access its members as if you access a field in a table, call a method on it, ... You can even, via the __gc metamethod, be notified when the GC is cleaning up your object so you can release any resources you might store in the object. See this example I posted in another thread.
I don't know Obj-C enough to be able to tell if and how to do this but since it's closely related to C I guess it should be possible.
4 - General / Language / Re: Slowdown when allocating large numbers of tables
Started by rhoark - Last post by steved on: September 06, 2010, 12:36:51 am
It would be interesting to print out the value of collectgarbage("count") to find out how much memory Lua is managing.
Yes, it does seem like a case of overactive garbage collection....the 2Gig number seems rather suspicious as well!
Yes, it does seem like a case of overactive garbage collection....the 2Gig number seems rather suspicious as well!
5 - General / News Feeds / lao 1.0.0
Started by STPeters - Last post by News Bot on: September 05, 2010, 02:45:27 pm
From: Linus Sjögren
lao is a module that provides Lua bindings to libao, a library by
xiph.org for simple audio output.
This is the first public release, and as such, there may be some minor
issues, like small memory leaks and/or segfaults.
Any tips/patches are appreciated.
http://xiph.org/ao/ - xiph.orgs page on libao
http://github.com/thelinx/lao/ - the lao project page
http://thelinx.github.com/lao/ - documentation
http://github.com/TheLinx/lao/raw/luarocks/ao-1.0.0-1.rockspec -
LuaRock (has been submitted for inclusion)
I hope this is useful to someone!
lao is a module that provides Lua bindings to libao, a library by
xiph.org for simple audio output.
This is the first public release, and as such, there may be some minor
issues, like small memory leaks and/or segfaults.
Any tips/patches are appreciated.
http://xiph.org/ao/ - xiph.orgs page on libao
http://github.com/thelinx/lao/ - the lao project page
http://thelinx.github.com/lao/ - documentation
http://github.com/TheLinx/lao/raw/luarocks/ao-1.0.0-1.rockspec -
LuaRock (has been submitted for inclusion)
I hope this is useful to someone!
6 - General / Integration / Re: Integrating Lua; getting lightuserdata..
Started by debu - Last post by debu on: September 05, 2010, 05:22:08 am
Hi Dirkf,
Thanks a lot for your detailed response, I think that's pretty much cleared up all things I didn't know relating to light userdata. The potential problem you mentioned, of having the object be deallocated while the script is still accessing it, could be an issue, but also I imagine it is just a matter of making sure the script itself is deallocated before any game-objects, and their Dictionaries, are deallocated. Hopefully wouldn't be too annoying to make that work.
So what's the deal with normal userdata, and tables/metatables? Is it actually possible to have Obj-C types returned to Lua, and not store them in light userdata?
Thanks again for your help, I'll try and experiment with what you've told me.. Really appreciate it!
debu
Thanks a lot for your detailed response, I think that's pretty much cleared up all things I didn't know relating to light userdata. The potential problem you mentioned, of having the object be deallocated while the script is still accessing it, could be an issue, but also I imagine it is just a matter of making sure the script itself is deallocated before any game-objects, and their Dictionaries, are deallocated. Hopefully wouldn't be too annoying to make that work.
So what's the deal with normal userdata, and tables/metatables? Is it actually possible to have Obj-C types returned to Lua, and not store them in light userdata?
Thanks again for your help, I'll try and experiment with what you've told me.. Really appreciate it!
debu
7 - General / Language / Slowdown when allocating large numbers of tables
Started by rhoark - Last post by rhoark on: September 04, 2010, 03:51:31 pm
I'm using linked lists in my program and have found that if I get past about 20 million links the program slows down and eventually just stops. I think as its slowing down its spending more and more time in the garbage collector and eventually never comes back from it. To isolate the issue from the rest of my algorithms, I made a program that does nothing but add to the end of a linked list and the problem is still evident (this test is where I got the figure of 20 million.)
It's just a straightforward doubly-linked list. Here's the append function:
and new:
I grabbed a win64 precompiled binary, but since it seems to reliably garbage collect when the process is somewhere between 1.8 and 1.9 GB, I'm wondering if its still trying to constrain itself to the 2 GB address space limit for a win32 user process and freaking out as the actual data with live references approaches 2 GB. It ought to take more than 400 million links to fill 2 GB though, unless tables have a lot more memory overhead than I think.
It's just a straightforward doubly-linked list. Here's the append function:
Code: [Select]
function link:append(something)
x = link:new(); x.prev = self; x.nex = self.nex; x.value = something
if self.nex then self.nex.prev = x end
self.nex = x
return x
endand new:
Code: [Select]
function link:new(initializer)
local obj = initializer or {}
setmetatable(obj, self)
self.__index = self
return obj
endI grabbed a win64 precompiled binary, but since it seems to reliably garbage collect when the process is somewhere between 1.8 and 1.9 GB, I'm wondering if its still trying to constrain itself to the 2 GB address space limit for a win32 user process and freaking out as the actual data with live references approaches 2 GB. It ought to take more than 400 million links to fill 2 GB though, unless tables have a lot more memory overhead than I think.
8 - General / Integration / Re: Question regarding lua_yield
Started by jain1982 - Last post by dirkf on: September 03, 2010, 01:43:14 pm
Okay, I had a look. It's not explicitly mentioned in PiL2 but judging from the source code it's supposed to be the thread you're yielding (what a surprise...).
9 - General / Integration / Re: Integrating Lua; getting lightuserdata..
Started by debu - Last post by dirkf on: September 03, 2010, 01:32:39 pm
You can have a C function implementing getValueFromDictionary() that does the lookup and pushes a pointer to the matching object into Lua. Since it's a light userdata (just a pointer) you can only pass it around in Lua. When passing such variables to testCollision(), which is also implemented in C, it can use those pointers to access the internals of the structures to do its thing.
Here's a (contrived) example:
And a test script:
This is one approach; it has its drawbacks. For example, when using light userdata you must take care to keep those pointers valid as long as a Lua script is using it. See the "Programming in Lua, 2nd edition" book for more.
Here's a (contrived) example:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
typedef struct {
const char* key;
const char* value;
} DictEntry;
static DictEntry dict[] = {
{ .key = "box1", .value = "foo" },
{ .key = "box2", .value = "bar" },
{ .key = "box3", .value = "foo" }
};
static int getValueFromDictionary(lua_State *L)
{
int i;
DictEntry *entry = NULL;
const char *key = luaL_checkstring(L, 1);
for (i = 0; i < sizeof(dict)/sizeof(dict[0]); i++) {
if (strcmp(key, dict[i].key) == 0)
entry = &dict[i];
}
if (entry)
lua_pushlightuserdata(L, entry);
else
lua_pushnil(L);
return 1;
}
static int testCollision(lua_State *L)
{
DictEntry *entry1 = lua_touserdata(L, 1);
DictEntry *entry2 = lua_touserdata(L, 2);
if (strcmp(entry1->value, entry2->value) == 0)
lua_pushboolean(L, 1);
else
lua_pushboolean(L, 0);
return 1;
}
int main(void)
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_register(L, "getValueFromDictionary", getValueFromDictionary);
lua_register(L, "testCollision", testCollision);
luaL_dofile(L, "test.lua");
lua_close(L);
return EXIT_SUCCESS;
}
And a test script:
Code: [Select]
local obj1 = getValueFromDictionary("box1")
local obj2 = getValueFromDictionary("box2")
print(testCollision(obj1, obj2)) --> prints 'false'
local obj3 = getValueFromDictionary("box3")
print(testCollision(obj1, obj3)) --> prints 'true'
This is one approach; it has its drawbacks. For example, when using light userdata you must take care to keep those pointers valid as long as a Lua script is using it. See the "Programming in Lua, 2nd edition" book for more.
10 - General / Integration / Integrating Lua; getting lightuserdata..
Started by debu - Last post by debu on: September 03, 2010, 09:10:53 am
Hi all,
I'm new here, and fairly new to C based languages, so I hope you can excuse any glaring errors in my question.
I'm working with Objective-C, on an iPhone project (not bothered about the problems with Apple and submitting apps with Lua), and am trying to integrate Lua. I've read a lot, and spoken to a few people, but there are still a few gaps in my knowledge. Here's what I want to be able to do, ultimately:
Have Dictionaries and Arrays in Obj-C accessible from Lua. Have functions in Obj-C accessible in Lua. Be able to pass values from the Dictionaries and Arrays that are in the engine code into the functions also in the engine code, but for that whole process to be done in Lua.
I've read about how to expose C functions to Lua so it can call them, that basically makes sense to me. The part I'm less clear on is how to get values out of Dictionaries/Arrays whilst in the Lua script file, to then pass them to the exposed functions. I read that this might be possible by creating getter functions, which are also exposed to Lua, which return lightuserdata (which I understand to just be a pointer).
If that's possible, I'd assume I could do something like this (assuming two exposed functions; 1) getValueFromDictionary(), 2) testCollisions() ):
(I'm not sure how Lua syntax is, so please excuse any mistakes!)
var lightuserdata object1 = getValueFromDictionary("box1");
var lightuserdata object2 = getValueFromDictionary("box2");
if (testCollision(object1, object2)) { //do something }
For one, is this possible, and for two how would it be done inside C, for returning the value of something as lightuserdata?
Is this the right way to go about doing this, too? I heard talk of using tables, metatables, something like that, but I didn't see how it related to getting types from C. Any help would be really appreciated, again my apologies if I haven't explained my question well.
debu
I'm new here, and fairly new to C based languages, so I hope you can excuse any glaring errors in my question.
I'm working with Objective-C, on an iPhone project (not bothered about the problems with Apple and submitting apps with Lua), and am trying to integrate Lua. I've read a lot, and spoken to a few people, but there are still a few gaps in my knowledge. Here's what I want to be able to do, ultimately:
Have Dictionaries and Arrays in Obj-C accessible from Lua. Have functions in Obj-C accessible in Lua. Be able to pass values from the Dictionaries and Arrays that are in the engine code into the functions also in the engine code, but for that whole process to be done in Lua.
I've read about how to expose C functions to Lua so it can call them, that basically makes sense to me. The part I'm less clear on is how to get values out of Dictionaries/Arrays whilst in the Lua script file, to then pass them to the exposed functions. I read that this might be possible by creating getter functions, which are also exposed to Lua, which return lightuserdata (which I understand to just be a pointer).
If that's possible, I'd assume I could do something like this (assuming two exposed functions; 1) getValueFromDictionary(), 2) testCollisions() ):
(I'm not sure how Lua syntax is, so please excuse any mistakes!)
var lightuserdata object1 = getValueFromDictionary("box1");
var lightuserdata object2 = getValueFromDictionary("box2");
if (testCollision(object1, object2)) { //do something }
For one, is this possible, and for two how would it be done inside C, for returning the value of something as lightuserdata?
Is this the right way to go about doing this, too? I heard talk of using tables, metatables, something like that, but I didn't see how it related to getting types from C. Any help would be really appreciated, again my apologies if I haven't explained my question well.
debu