From bdca062ba89879794e916194383f4996f3f00e65 Mon Sep 17 00:00:00 2001 From: Ilari Liusvaara Date: Sat, 25 Jan 2014 16:16:32 +0200 Subject: [PATCH] Lua: Make static constructor methods for MMAP_STRUCT and ZIPWRITER --- src/lua/memory.cpp | 15 ++++++++------- src/lua/sysrc.lua | 2 ++ src/lua/zip.cpp | 32 ++++++++++++++++---------------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/lua/memory.cpp b/src/lua/memory.cpp index 74fb20ba..92b74095 100644 --- a/src/lua/memory.cpp +++ b/src/lua/memory.cpp @@ -127,6 +127,11 @@ public: x.first->write(L, x.second); return 0; } + static int create(lua::state& L, lua::parameters& P) + { + lua::_class::create(L); + return 1; + } int map(lua::state& L, const std::string& fname); std::string print() { @@ -542,12 +547,6 @@ namespace return 1; }); - lua::fnptr gui_cbitmap(lua_func_misc, "memory.map_structure", [](lua::state& L, - const std::string& fname) -> int { - lua::_class::create(L); - return 1; - }); - template int memory_scattergather(lua::state& L, const std::string& fname) { uint64_t val = 0; @@ -649,7 +648,9 @@ namespace lua_registerX mrt("memory.registertrace"); lua_registerX murt("memory.unregistertrace"); - lua::_class class_mmap_struct(lua_class_memory, "MMAP_STRUCT", {}, { + lua::_class class_mmap_struct(lua_class_memory, "MMAP_STRUCT", { + {"new", &lua_mmap_struct::create}, + }, { {"__index", &lua_mmap_struct::index}, {"__newindex", &lua_mmap_struct::newindex}, {"__call", &lua_mmap_struct::map}, diff --git a/src/lua/sysrc.lua b/src/lua/sysrc.lua index 89c9b3e0..c78c7e0b 100644 --- a/src/lua/sysrc.lua +++ b/src/lua/sysrc.lua @@ -35,3 +35,5 @@ bit.bxor=bit.parity; local _lookup_class = lookup_class; memory2=_lookup_class("VMALIST").new(); callback=_lookup_class("CALLBACKS_LIST").new(); +memory.map_structure=_lookup_class("MMAP_STRUCT").new; +zip.create=_lookup_class("ZIPWRITER").new; diff --git a/src/lua/zip.cpp b/src/lua/zip.cpp index a65d2a88..c900e8ab 100644 --- a/src/lua/zip.cpp +++ b/src/lua/zip.cpp @@ -11,6 +11,17 @@ namespace { if(w) delete w; } + static int create(lua::state& L, lua::parameters& P) + { + auto filename = P.arg(); + auto compression = P.arg_opt(9); + if(compression < 0) + compression = 0; + if(compression > 9) + compression = 9; + lua::_class::create(L, filename, compression); + return 1; + } int commit(lua::state& L, const std::string& fname) { if(!w) @@ -70,7 +81,9 @@ namespace std::string file; }; - lua::_class class_zipwriter(lua_class_fileio, "ZIPWRITER", {}, { + lua::_class class_zipwriter(lua_class_fileio, "ZIPWRITER", { + {"new", lua_zip_writer::create}, + }, { {"commit", &lua_zip_writer::commit}, {"rollback", &lua_zip_writer::rollback}, {"close_file", &lua_zip_writer::close_file}, @@ -85,21 +98,8 @@ namespace file_open = NULL; } - lua::fnptr lua_zip(lua_func_zip, "zip.create", [](lua::state& L, - const std::string& fname) -> int { - unsigned compression = 9; - std::string filename = L.get_string(1, fname.c_str()); - L.get_numeric_argument(2, compression, fname.c_str()); - if(compression < 0) - compression = 0; - if(compression > 9) - compression = 9; - lua::_class::create(L, filename, compression); - return 1; - }); - - lua::fnptr lua_enumerate_zip(lua_func_zip, "zip.enumerate", [](lua::state& L, - const std::string& fname) -> int { + lua::fnptr lua_enumerate_zip(lua_func_zip, "zip.enumerate", [](lua::state& L, const std::string& fname) + -> int { std::string filename = L.get_string(1, fname.c_str()); bool invert = false; if(L.type(2) != LUA_TNONE && L.type(2) != LUA_TNIL) -- 2.11.4.GIT