From 8ae0384f09f8f94eb9cc3f07e235114e5d589b36 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Sat, 17 Dec 2011 21:30:00 +0000 Subject: [PATCH] Add option for lua parts array to use native data using ffi --- src/luaconsole.c | 41 +++++++++++++++++++++++++++++++---------- src/main.c | 7 ++++--- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/luaconsole.c b/src/luaconsole.c index 815132821..42495e796 100644 --- a/src/luaconsole.c +++ b/src/luaconsole.c @@ -101,16 +101,22 @@ void luacon_open(){ lua_setfield(l, tptPropertiesVersion, "build"); lua_setfield(l, tptProperties, "version"); - /*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");*/ +#ifdef FFI + //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_pushlightuserdata(l, parts); + lua_setfield(l, tptProperties, "partsdata"); + + luaL_dostring (l, "ffi = require(\"ffi\")\n\ +ffi.cdef[[\n\ +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\ +]]\n\ +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); tptParts = lua_gettop(l); for(i = 0; i < NPART; i++) @@ -132,6 +138,19 @@ void luacon_open(){ } 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); tptElements = lua_gettop(l); for(i = 1; i < PT_NUM; i++) @@ -192,6 +211,7 @@ void luacon_open(){ lua_el_mode[i] = 0; } } +#ifndef FFI int luacon_partread(lua_State* l){ int format, offset; char * tempstring; @@ -294,6 +314,7 @@ int luacon_partsread(lua_State* l){ int luacon_partswrite(lua_State* l){ return luaL_error(l, "Not writable"); } +#endif int luacon_particle_getproperty(char * key, int * format) { int offset; diff --git a/src/main.c b/src/main.c index ef150acb3..f732f2ace 100644 --- a/src/main.c +++ b/src/main.c @@ -1666,9 +1666,6 @@ int main(int argc, char *argv[]) fmt.callback = mixaudio; fmt.userdata = NULL; -#ifdef LUACONSOLE - luacon_open(); -#endif #ifdef MT numCores = core_count(); #endif @@ -1679,6 +1676,10 @@ int main(int argc, char *argv[]) init_can_move(); clear_sim(); +#ifdef LUACONSOLE + luacon_open(); +#endif + colour_mode = COLOUR_DEFAULT; init_display_modes();