Add option for lua parts array to use native data using ffi

This commit is contained in:
Simon Robertshaw
2011-12-17 21:30:00 +00:00
parent 03352ca1f4
commit 8ae0384f09
2 changed files with 35 additions and 13 deletions

View File

@@ -101,16 +101,22 @@ void luacon_open(){
lua_setfield(l, tptPropertiesVersion, "build"); lua_setfield(l, tptPropertiesVersion, "build");
lua_setfield(l, tptProperties, "version"); lua_setfield(l, tptProperties, "version");
/*lua_newtable(l); #ifdef FFI
tptParts = lua_gettop(l); //LuaJIT's ffi gives us direct access to parts data, no need for nested metatables. HOWEVER, this is in no way safe, it's entirely possible for someone to try to read parts[-10]
lua_newtable(l); lua_pushlightuserdata(l, parts);
tptPartsMeta = lua_gettop(l); lua_setfield(l, tptProperties, "partsdata");
lua_pushcfunction(l, luacon_partswrite);
lua_setfield(l, tptPartsMeta, "__newindex"); luaL_dostring (l, "ffi = require(\"ffi\")\n\
lua_pushcfunction(l, luacon_partsread); ffi.cdef[[\n\
lua_setfield(l, tptPartsMeta, "__index"); typedef struct { int type; int life, ctype; float x, y, vx, vy; float temp; float pavg[2]; int flags; int tmp; int tmp2; unsigned int dcolour; } particle;\n\
lua_setmetatable(l, tptParts); ]]\n\
lua_setfield(l, tptProperties, "parts");*/ tpt.parts = ffi.cast(\"particle *\", tpt.partsdata)\n\
ffi = nil\n\
tpt.partsdata = nil");
//Since ffi is REALLY REALLY dangrous, we'll remove it from the environment completely (TODO)
#else
//This uses a lot of memory (60MB+), but very good performance
lua_newtable(l); lua_newtable(l);
tptParts = lua_gettop(l); tptParts = lua_gettop(l);
for(i = 0; i < NPART; i++) for(i = 0; i < NPART; i++)
@@ -132,6 +138,19 @@ void luacon_open(){
} }
lua_setfield(l, tptProperties, "parts"); lua_setfield(l, tptProperties, "parts");
//Poor performance (nested metatabled created on get/set) but good little memory usage
/*lua_newtable(l);
tptParts = lua_gettop(l);
lua_newtable(l);
tptPartsMeta = lua_gettop(l);
lua_pushcfunction(l, luacon_partswrite);
lua_setfield(l, tptPartsMeta, "__newindex");
lua_pushcfunction(l, luacon_partsread);
lua_setfield(l, tptPartsMeta, "__index");
lua_setmetatable(l, tptParts);
lua_setfield(l, tptProperties, "parts");*/
#endif
lua_newtable(l); lua_newtable(l);
tptElements = lua_gettop(l); tptElements = lua_gettop(l);
for(i = 1; i < PT_NUM; i++) for(i = 1; i < PT_NUM; i++)
@@ -192,6 +211,7 @@ void luacon_open(){
lua_el_mode[i] = 0; lua_el_mode[i] = 0;
} }
} }
#ifndef FFI
int luacon_partread(lua_State* l){ int luacon_partread(lua_State* l){
int format, offset; int format, offset;
char * tempstring; char * tempstring;
@@ -294,6 +314,7 @@ int luacon_partsread(lua_State* l){
int luacon_partswrite(lua_State* l){ int luacon_partswrite(lua_State* l){
return luaL_error(l, "Not writable"); return luaL_error(l, "Not writable");
} }
#endif
int luacon_particle_getproperty(char * key, int * format) int luacon_particle_getproperty(char * key, int * format)
{ {
int offset; int offset;

View File

@@ -1666,9 +1666,6 @@ int main(int argc, char *argv[])
fmt.callback = mixaudio; fmt.callback = mixaudio;
fmt.userdata = NULL; fmt.userdata = NULL;
#ifdef LUACONSOLE
luacon_open();
#endif
#ifdef MT #ifdef MT
numCores = core_count(); numCores = core_count();
#endif #endif
@@ -1679,6 +1676,10 @@ int main(int argc, char *argv[])
init_can_move(); init_can_move();
clear_sim(); clear_sim();
#ifdef LUACONSOLE
luacon_open();
#endif
colour_mode = COLOUR_DEFAULT; colour_mode = COLOUR_DEFAULT;
init_display_modes(); init_display_modes();