Move some of the automation machinery to libaegisub

This commit is contained in:
Thomas Goyne 2014-03-26 19:24:37 -07:00
parent 15ae2b0ccc
commit b8af29da0d
22 changed files with 220 additions and 130 deletions

View File

@ -10,9 +10,10 @@ CXXFLAGS += -I../libaegisub/include -I../src -I ../vendor/lua/src $(CXXFLAGS_WX)
CPPFLAGS += $(CPPFLAGS_BOOST) CPPFLAGS += $(CPPFLAGS_BOOST)
LIBS := -L../libaegisub -laegisub -L../vendor/lua -llua-aegisub $(LIBS) LIBS := -L../libaegisub -laegisub -L../vendor/lua -llua-aegisub $(LIBS)
LIBS += ../vendor/luabins/libluabins.a
LIBS += $(LIBS_WX) $(LIBS_BOOST) $(LIBS_ICU) LIBS += $(LIBS_WX) $(LIBS_BOOST) $(LIBS_ICU)
SRC += tests/aegisub.cpp ../src/auto4_regex.cpp SRC += tests/aegisub.cpp
test: $(LIB_SHARED) test: $(LIB_SHARED)
moon tests/runner.moon moon tests/runner.moon

View File

@ -18,7 +18,7 @@ select = select
type = type type = type
-- Get the boost::regex binding -- Get the boost::regex binding
regex = aegisub.__init_regex() regex = require 'aegisub.__re_impl'
-- Return the first n elements from ... -- Return the first n elements from ...
select_first = (n, a, ...) -> select_first = (n, a, ...) ->

View File

@ -14,15 +14,9 @@
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
#include <libaegisub/lua/modules.h>
#include "auto4_lua_utils.h"
namespace Automation4 { int regex_init(lua_State *L); }
extern "C" int luaopen_aegisub(lua_State *L) { extern "C" int luaopen_aegisub(lua_State *L) {
lua_pushstring(L, "aegisub"); agi::lua::preload_modules(L);
lua_newtable(L);
set_field(L, "__init_regex", Automation4::regex_init);
lua_settable(L, LUA_GLOBALSINDEX);
return 1; return 1;
} }

View File

@ -121,8 +121,6 @@
<ClInclude Include="$(SrcDir)auto4_base.h" /> <ClInclude Include="$(SrcDir)auto4_base.h" />
<ClInclude Include="$(SrcDir)auto4_lua.h" /> <ClInclude Include="$(SrcDir)auto4_lua.h" />
<ClInclude Include="$(SrcDir)auto4_lua_factory.h" /> <ClInclude Include="$(SrcDir)auto4_lua_factory.h" />
<ClInclude Include="$(SrcDir)auto4_lua_scriptreader.h" />
<ClInclude Include="$(SrcDir)auto4_lua_utils.h" />
<ClInclude Include="$(SrcDir)avisynth.h" /> <ClInclude Include="$(SrcDir)avisynth.h" />
<ClInclude Include="$(SrcDir)avisynth_wrap.h" /> <ClInclude Include="$(SrcDir)avisynth_wrap.h" />
<ClInclude Include="$(SrcDir)base_grid.h" /> <ClInclude Include="$(SrcDir)base_grid.h" />
@ -187,7 +185,6 @@
<ClInclude Include="$(SrcDir)include\aegisub\toolbar.h" /> <ClInclude Include="$(SrcDir)include\aegisub\toolbar.h" />
<ClInclude Include="$(SrcDir)include\aegisub\video_provider.h" /> <ClInclude Include="$(SrcDir)include\aegisub\video_provider.h" />
<ClInclude Include="$(SrcDir)initial_line_state.h" /> <ClInclude Include="$(SrcDir)initial_line_state.h" />
<ClInclude Include="$(SrcDir)lpeg.h" />
<ClInclude Include="$(SrcDir)main.h" /> <ClInclude Include="$(SrcDir)main.h" />
<ClInclude Include="$(SrcDir)mkv_wrap.h" /> <ClInclude Include="$(SrcDir)mkv_wrap.h" />
<ClInclude Include="$(SrcDir)options.h" /> <ClInclude Include="$(SrcDir)options.h" />
@ -308,8 +305,6 @@
<ClCompile Include="$(SrcDir)auto4_lua_assfile.cpp" /> <ClCompile Include="$(SrcDir)auto4_lua_assfile.cpp" />
<ClCompile Include="$(SrcDir)auto4_lua_dialog.cpp" /> <ClCompile Include="$(SrcDir)auto4_lua_dialog.cpp" />
<ClCompile Include="$(SrcDir)auto4_lua_progresssink.cpp" /> <ClCompile Include="$(SrcDir)auto4_lua_progresssink.cpp" />
<ClCompile Include="$(SrcDir)auto4_lua_scriptreader.cpp" />
<ClCompile Include="$(SrcDir)auto4_regex.cpp" />
<ClCompile Include="$(SrcDir)avisynth_wrap.cpp" /> <ClCompile Include="$(SrcDir)avisynth_wrap.cpp" />
<ClCompile Include="$(SrcDir)base_grid.cpp" /> <ClCompile Include="$(SrcDir)base_grid.cpp" />
<ClCompile Include="$(SrcDir)charset_detect.cpp" /> <ClCompile Include="$(SrcDir)charset_detect.cpp" />
@ -382,7 +377,6 @@
<ClCompile Include="$(SrcDir)hotkey.cpp" /> <ClCompile Include="$(SrcDir)hotkey.cpp" />
<ClCompile Include="$(SrcDir)hotkey_data_view_model.cpp" /> <ClCompile Include="$(SrcDir)hotkey_data_view_model.cpp" />
<ClCompile Include="$(SrcDir)initial_line_state.cpp" /> <ClCompile Include="$(SrcDir)initial_line_state.cpp" />
<ClCompile Include="$(SrcDir)lpeg.cpp" />
<ClCompile Include="$(SrcDir)main.cpp" /> <ClCompile Include="$(SrcDir)main.cpp" />
<ClCompile Include="$(SrcDir)menu.cpp" /> <ClCompile Include="$(SrcDir)menu.cpp" />
<ClCompile Include="$(SrcDir)mkv_wrap.cpp" /> <ClCompile Include="$(SrcDir)mkv_wrap.cpp" />

View File

@ -261,9 +261,6 @@
<ClInclude Include="$(SrcDir)export_fixstyle.h"> <ClInclude Include="$(SrcDir)export_fixstyle.h">
<Filter>Features\Export</Filter> <Filter>Features\Export</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(SrcDir)auto4_lua_scriptreader.h">
<Filter>Automation\Lua</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)auto4_lua.h"> <ClInclude Include="$(SrcDir)auto4_lua.h">
<Filter>Automation\Lua</Filter> <Filter>Automation\Lua</Filter>
</ClInclude> </ClInclude>
@ -615,12 +612,6 @@
<ClInclude Include="$(SrcDir)subs_controller.h"> <ClInclude Include="$(SrcDir)subs_controller.h">
<Filter>ASS</Filter> <Filter>ASS</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(SrcDir)auto4_lua_utils.h">
<Filter>Automation\Lua</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)lpeg.h">
<Filter>Automation\Lua</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)resolution_resampler.h"> <ClInclude Include="$(SrcDir)resolution_resampler.h">
<Filter>Features\Resolution resampler</Filter> <Filter>Features\Resolution resampler</Filter>
</ClInclude> </ClInclude>
@ -896,9 +887,6 @@
<ClCompile Include="$(SrcDir)auto4_lua_progresssink.cpp"> <ClCompile Include="$(SrcDir)auto4_lua_progresssink.cpp">
<Filter>Automation\Lua</Filter> <Filter>Automation\Lua</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="$(SrcDir)auto4_lua_scriptreader.cpp">
<Filter>Automation\Lua</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)dialog_automation.cpp"> <ClCompile Include="$(SrcDir)dialog_automation.cpp">
<Filter>Automation\UI</Filter> <Filter>Automation\UI</Filter>
</ClCompile> </ClCompile>
@ -1163,12 +1151,6 @@
<ClCompile Include="$(SrcDir)subs_controller.cpp"> <ClCompile Include="$(SrcDir)subs_controller.cpp">
<Filter>ASS</Filter> <Filter>ASS</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="$(SrcDir)auto4_regex.cpp">
<Filter>Automation\Lua</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)lpeg.cpp">
<Filter>Automation\Lua</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)resolution_resampler.cpp"> <ClCompile Include="$(SrcDir)resolution_resampler.cpp">
<Filter>Features\Resolution resampler</Filter> <Filter>Features\Resolution resampler</Filter>
</ClCompile> </ClCompile>

View File

@ -65,6 +65,9 @@
<ClInclude Include="$(SrcDir)include\libaegisub\line_iterator.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\line_iterator.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\line_wrap.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\line_wrap.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\log.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\log.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\lua\modules.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\lua\script_reader.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\lua\utils.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\make_unique.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\make_unique.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\mru.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\mru.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\of_type_adaptor.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\of_type_adaptor.h" />
@ -82,6 +85,7 @@
<ClInclude Include="$(SrcDir)include\libaegisub\util_osx.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\util_osx.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\vfr.h" /> <ClInclude Include="$(SrcDir)include\libaegisub\vfr.h" />
<ClInclude Include="$(SrcDir)lagi_pre.h" /> <ClInclude Include="$(SrcDir)lagi_pre.h" />
<ClInclude Include="$(SrcDir)lua\modules\lpeg.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="$(SrcDir)windows\lagi_pre.cpp"> <ClCompile Include="$(SrcDir)windows\lagi_pre.cpp">
@ -116,6 +120,10 @@
<ClCompile Include="$(SrcDir)common\thesaurus.cpp" /> <ClCompile Include="$(SrcDir)common\thesaurus.cpp" />
<ClCompile Include="$(SrcDir)common\util.cpp" /> <ClCompile Include="$(SrcDir)common\util.cpp" />
<ClCompile Include="$(SrcDir)common\vfr.cpp" /> <ClCompile Include="$(SrcDir)common\vfr.cpp" />
<ClCompile Include="$(SrcDir)lua\modules.cpp" />
<ClCompile Include="$(SrcDir)lua\modules\lpeg.cpp" />
<ClCompile Include="$(SrcDir)lua\modules\re.cpp" />
<ClCompile Include="$(SrcDir)lua\script_reader.cpp" />
<ClCompile Include="$(SrcDir)windows\access.cpp" /> <ClCompile Include="$(SrcDir)windows\access.cpp" />
<ClCompile Include="$(SrcDir)windows\charset_conv_win.cpp" /> <ClCompile Include="$(SrcDir)windows\charset_conv_win.cpp" />
<ClCompile Include="$(SrcDir)windows\fs.cpp" /> <ClCompile Include="$(SrcDir)windows\fs.cpp" />

View File

@ -21,6 +21,12 @@
<Filter Include="ASS"> <Filter Include="ASS">
<UniqueIdentifier>{c97bd8a3-ebb7-4c43-9ca0-4ab8479d9388}</UniqueIdentifier> <UniqueIdentifier>{c97bd8a3-ebb7-4c43-9ca0-4ab8479d9388}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Lua">
<UniqueIdentifier>{ce495b25-1630-42e1-8ab0-053c313448f0}</UniqueIdentifier>
</Filter>
<Filter Include="Lua\Modules">
<UniqueIdentifier>{2cf3ad94-e8a9-4a89-b47e-29523d90fd08}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="$(SrcDir)common\charset_6937.h"> <ClInclude Include="$(SrcDir)common\charset_6937.h">
@ -173,6 +179,18 @@
<ClInclude Include="$(SrcDir)include\libaegisub\make_unique.h"> <ClInclude Include="$(SrcDir)include\libaegisub\make_unique.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\lua\utils.h">
<Filter>Lua</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\lua\script_reader.h">
<Filter>Lua</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)lua\modules\lpeg.h">
<Filter>Lua\Modules</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\lua\modules.h">
<Filter>Lua</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="$(SrcDir)windows\lagi_pre.cpp"> <ClCompile Include="$(SrcDir)windows\lagi_pre.cpp">
@ -283,6 +301,18 @@
<ClCompile Include="$(SrcDir)common\character_count.cpp"> <ClCompile Include="$(SrcDir)common\character_count.cpp">
<Filter>Source Files\Common</Filter> <Filter>Source Files\Common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="$(SrcDir)lua\script_reader.cpp">
<Filter>Lua</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)lua\modules\lpeg.cpp">
<Filter>Lua\Modules</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)lua\modules\re.cpp">
<Filter>Lua\Modules</Filter>
</ClCompile>
<ClCompile Include="$(SrcDir)lua\modules.cpp">
<Filter>Lua</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="$(SrcDir)include\libaegisub\charsets.def"> <None Include="$(SrcDir)include\libaegisub\charsets.def">

View File

@ -41,6 +41,10 @@ SRC += \
common/thesaurus.cpp \ common/thesaurus.cpp \
common/util.cpp \ common/util.cpp \
common/vfr.cpp \ common/vfr.cpp \
lua/modules.cpp \
lua/modules/lpeg.cpp \
lua/modules/re.cpp \
lua/script_reader.cpp \
unix/access.cpp \ unix/access.cpp \
unix/fs.cpp \ unix/fs.cpp \
unix/log.cpp \ unix/log.cpp \

View File

@ -0,0 +1,21 @@
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
struct lua_State;
namespace agi { namespace lua {
void preload_modules(lua_State *L);
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org> // Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
@ -11,16 +11,13 @@
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
/// @file auto4_lua_scriptreader.h // Aegisub Project http://www.aegisub.org/
/// @see auto4_lua_scriptreader.cpp
/// @ingroup scripting
///
#include <libaegisub/fs_fwd.h> #include <libaegisub/fs_fwd.h>
struct lua_State; struct lua_State;
namespace Automation4 { namespace agi { namespace lua {
bool LoadFile(lua_State *L, agi::fs::path const& filename); bool LoadFile(lua_State *L, agi::fs::path const& filename);
} } }

View File

@ -1,4 +1,4 @@
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org> // Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
@ -14,16 +14,15 @@
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
#include <libaegisub/fs.h>
#include <libaegisub/log.h> #include <libaegisub/log.h>
#include <libaegisub/fs.h>
#include <lua.h> #include <lua.h>
#include <lualib.h>
#include <lauxlib.h> #include <lauxlib.h>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <wx/string.h>
namespace agi { namespace lua {
inline void push_value(lua_State *L, bool value) { lua_pushboolean(L, value); } inline void push_value(lua_State *L, bool value) { lua_pushboolean(L, value); }
inline void push_value(lua_State *L, const char *value) { lua_pushstring(L, value); } inline void push_value(lua_State *L, const char *value) { lua_pushstring(L, value); }
@ -33,15 +32,12 @@ inline void push_value(lua_State *L, void *p) { lua_pushlightuserdata(L, p); }
template<typename Integer> template<typename Integer>
typename std::enable_if<std::is_integral<Integer>::value>::type typename std::enable_if<std::is_integral<Integer>::value>::type
push_value(lua_State *L, Integer value) { lua_pushinteger(L, value); } push_value(lua_State *L, Integer value) {
lua_pushinteger(L, static_cast<lua_Integer>(value));
inline void push_value(lua_State *L, wxString const& value) {
lua_pushstring(L, value.utf8_str());
} }
inline void push_value(lua_State *L, agi::fs::path const& value) { inline void push_value(lua_State *L, fs::path const& value) {
std::string strval = value.string(); lua_pushstring(L, value.string().c_str());
lua_pushlstring(L, strval.c_str(), strval.size());
} }
inline void push_value(lua_State *L, std::string const& value) { inline void push_value(lua_State *L, std::string const& value) {
@ -63,22 +59,26 @@ inline void set_field(lua_State *L, const char *name, T value) {
lua_setfield(L, -2, name); lua_setfield(L, -2, name);
} }
inline wxString get_wxstring(lua_State *L, int idx) {
return wxString::FromUTF8(lua_tostring(L, idx));
}
inline wxString check_wxstring(lua_State *L, int idx) {
return wxString::FromUTF8(luaL_checkstring(L, idx));
}
inline std::string get_string_or_default(lua_State *L, int idx) { inline std::string get_string_or_default(lua_State *L, int idx) {
size_t len = 0; size_t len = 0;
const char *str = lua_tolstring(L, idx, &len); const char *str = lua_tolstring(L, idx, &len);
if (!str) if (!str)
str = "<not a string>"; return "<not a string>";
return std::string(str, len); return std::string(str, len);
} }
inline std::string get_string(lua_State *L, int idx) {
size_t len = 0;
const char *str = lua_tolstring(L, idx, &len);
return std::string(str ? str : "", len);
}
inline std::string check_string(lua_State *L, int idx) {
size_t len = 0;
const char *str = luaL_checklstring(L, idx, &len);
return std::string(str ? str : "", len);
}
inline std::string get_global_string(lua_State *L, const char *name) { inline std::string get_global_string(lua_State *L, const char *name) {
lua_getglobal(L, name); lua_getglobal(L, name);
std::string ret; std::string ret;
@ -88,6 +88,20 @@ inline std::string get_global_string(lua_State *L, const char *name) {
return ret; return ret;
} }
template<typename T, typename... Args>
T *make(lua_State *L, const char *mt, Args&&... args) {
auto obj = static_cast<T*>(lua_newuserdata(L, sizeof(T)));
new(obj) T(std::forward<Args>(args)...);
luaL_getmetatable(L, mt);
lua_setmetatable(L, -2);
return obj;
}
template<typename T>
T& get(lua_State *L, int idx, const char *mt) {
return *static_cast<T *>(luaL_checkudata(L, idx, mt));
}
struct LuaForEachBreak {}; struct LuaForEachBreak {};
template<typename Func> template<typename Func>
@ -144,3 +158,5 @@ struct LuaStackcheck {
LuaStackcheck(lua_State*) { } LuaStackcheck(lua_State*) { }
}; };
#endif #endif
} }

View File

@ -0,0 +1,38 @@
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
#include "libaegisub/lua/modules.h"
#include "libaegisub/lua/utils.h"
int luaopen_lpeg(lua_State *L);
extern "C" int luaopen_luabins(lua_State *L);
extern "C" int luaopen_re_impl(lua_State *L);
namespace agi { namespace lua {
int regex_init(lua_State *L);
void preload_modules(lua_State *L) {
lua_getglobal(L, "package");
lua_getfield(L, -1, "preload");
set_field(L, "lpeg", luaopen_lpeg);
set_field(L, "luabins", luaopen_luabins);
set_field(L, "aegisub.__re_impl", luaopen_re_impl);
lua_pop(L, 2);
}
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org> // Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
@ -14,34 +14,33 @@
// //
// Aegisub Project http://www.aegisub.org/ // Aegisub Project http://www.aegisub.org/
#include "auto4_lua_utils.h" #include "libaegisub/lua/utils.h"
#include <boost/regex/icu.hpp> #include <boost/regex/icu.hpp>
#include <lauxlib.h>
namespace { namespace {
using namespace agi::lua;
boost::u32regex& get_regex(lua_State *L) { boost::u32regex& get_regex(lua_State *L) {
return *static_cast<boost::u32regex*>(luaL_checkudata(L, 1, "aegisub.regex")); return get<boost::u32regex>(L, 1, "aegisub.regex");
} }
boost::smatch& get_smatch(lua_State *L) { boost::smatch& get_smatch(lua_State *L) {
return *static_cast<boost::smatch*>(luaL_checkudata(L, 1, "aegisub.smatch")); return get<boost::smatch>(L, 1, "aegisub.smatch");
} }
int regex_matches(lua_State *L) { int regex_matches(lua_State *L) {
lua_pushboolean(L, u32regex_match(luaL_checkstring(L, 2), get_regex(L))); lua_pushboolean(L, u32regex_match(check_string(L, 2), get_regex(L)));
return 1; return 1;
} }
int regex_match(lua_State *L) { int regex_match(lua_State *L) {
auto re = get_regex(L); auto re = get_regex(L);
std::string str = luaL_checkstring(L, 2); std::string str = check_string(L, 2);
int start = lua_tointeger(L, 3); int start = lua_tointeger(L, 3);
auto result = static_cast<boost::smatch*>(lua_newuserdata(L, sizeof(boost::smatch))); auto result = make<boost::smatch>(L, "aegisub.smatch");
new(result) boost::smatch;
luaL_getmetatable(L, "aegisub.smatch");
lua_setmetatable(L, -2);
if (!u32regex_search(str.cbegin() + start, str.cend(), *result, re, if (!u32regex_search(str.cbegin() + start, str.cend(), *result, re,
start > 0 ? boost::match_prev_avail | boost::match_not_bob : boost::match_default)) start > 0 ? boost::match_prev_avail | boost::match_not_bob : boost::match_default))
{ {
@ -67,7 +66,7 @@ int regex_get_match(lua_State *L) {
int regex_search(lua_State *L) { int regex_search(lua_State *L) {
auto re = get_regex(L); auto re = get_regex(L);
std::string str = luaL_checkstring(L, 2); std::string str = check_string(L, 2);
int start = luaL_checkinteger(L, 3) - 1; int start = luaL_checkinteger(L, 3) - 1;
boost::smatch result; boost::smatch result;
if (!u32regex_search(str.cbegin() + start, str.cend(), result, re, if (!u32regex_search(str.cbegin() + start, str.cend(), result, re,
@ -84,8 +83,8 @@ int regex_search(lua_State *L) {
int regex_replace(lua_State *L) { int regex_replace(lua_State *L) {
auto re = get_regex(L); auto re = get_regex(L);
const auto replacement = luaL_checkstring(L, 2); const auto replacement = check_string(L, 2);
const std::string str = luaL_checkstring(L, 3); const std::string str = check_string(L, 3);
int max_count = luaL_checkinteger(L, 4); int max_count = luaL_checkinteger(L, 4);
// Can't just use regex_replace here since it can only do one or infinite replacements // Can't just use regex_replace here since it can only do one or infinite replacements
@ -111,12 +110,11 @@ int regex_replace(lua_State *L) {
} }
int regex_compile(lua_State *L) { int regex_compile(lua_State *L) {
std::string pattern(luaL_checkstring(L, 1)); std::string pattern(check_string(L, 1));
int flags = luaL_checkinteger(L, 2); int flags = luaL_checkinteger(L, 2);
boost::u32regex *re = static_cast<boost::u32regex*>(lua_newuserdata(L, sizeof(boost::u32regex))); auto re = make<boost::u32regex>(L, "aegisub.regex");
try { try {
new(re) boost::u32regex;
*re = boost::make_u32regex(pattern, boost::u32regex::perl | flags); *re = boost::make_u32regex(pattern, boost::u32regex::perl | flags);
} }
catch (std::exception const& e) { catch (std::exception const& e) {
@ -127,9 +125,6 @@ int regex_compile(lua_State *L) {
// can report the original call site // can report the original call site
} }
luaL_getmetatable(L, "aegisub.regex");
lua_setmetatable(L, -2);
return 1; return 1;
} }
@ -178,8 +173,7 @@ int regex_init_flags(lua_State *L) {
} }
namespace Automation4 { extern "C" int luaopen_re_impl(lua_State *L) {
int regex_init(lua_State *L) {
if (luaL_newmetatable(L, "aegisub.regex")) { if (luaL_newmetatable(L, "aegisub.regex")) {
set_field(L, "__gc", regex_gc); set_field(L, "__gc", regex_gc);
lua_pop(L, 1); lua_pop(L, 1);
@ -201,4 +195,3 @@ int regex_init(lua_State *L) {
set_field(L, "init_flags", regex_init_flags); set_field(L, "init_flags", regex_init_flags);
return 1; return 1;
} }
}

View File

@ -1,4 +1,4 @@
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org> // Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
// //
// Permission to use, copy, modify, and distribute this software for any // Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above // purpose with or without fee is hereby granted, provided that the above
@ -11,21 +11,17 @@
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
/// @file auto4_lua_scriptreader.cpp #include "libaegisub/lua/script_reader.h"
/// @brief Script-file reader for Lua 5.1-based scripting engine
/// @ingroup scripting
///
#include "auto4_lua_scriptreader.h" #include "libaegisub/file_mapping.h"
#include "libaegisub/lua/utils.h"
#include "auto4_lua_utils.h" #include <lauxlib.h>
#include <libaegisub/file_mapping.h> namespace agi { namespace lua {
#include <lua.h>
namespace Automation4 {
bool LoadFile(lua_State *L, agi::fs::path const& raw_filename) { bool LoadFile(lua_State *L, agi::fs::path const& raw_filename) {
auto filename = raw_filename; auto filename = raw_filename;
try { try {
@ -37,7 +33,7 @@ namespace Automation4 {
agi::read_file_mapping file(filename); agi::read_file_mapping file(filename);
auto buff = file.read(); auto buff = file.read();
size_t size = file.size(); size_t size = static_cast<size_t>(file.size());
// Discard the BOM if present // Discard the BOM if present
if (size >= 3 && buff[0] == -17 && buff[1] == -69 && buff[2] == -65) { if (size >= 3 && buff[0] == -17 && buff[1] == -69 && buff[2] == -65) {
@ -72,4 +68,4 @@ namespace Automation4 {
lua_pop(L, 1); // Remove the extra nil for the stackchecker lua_pop(L, 1); // Remove the extra nil for the stackchecker
return true; return true;
} }
} } }

View File

@ -106,7 +106,7 @@ charset_detect.o: CXXFLAGS += -D_X86_
font_file_lister_fontconfig.o: CXXFLAGS += $(CFLAGS_FONTCONFIG) font_file_lister_fontconfig.o: CXXFLAGS += $(CFLAGS_FONTCONFIG)
text_file_reader.o: CXXFLAGS += -D_X86_ text_file_reader.o: CXXFLAGS += -D_X86_
video_provider_manager.o: CXXFLAGS += $(CFLAGS_FFMS2) video_provider_manager.o: CXXFLAGS += $(CFLAGS_FFMS2)
auto4_lua.o auto4_lua_assfile.o auto4_lua_dialog.o auto4_lua_progresssink.o auto4_lua_regex.o auto4_lua_scriptreader.o auto4_regex.o lpeg.o: CXXFLAGS += $(CFLAGS_LUA) auto4_lua.o auto4_lua_assfile.o auto4_lua_dialog.o auto4_lua_progresssink.o: CXXFLAGS += $(CFLAGS_LUA)
SRC += \ SRC += \
MatroskaParser.c \ MatroskaParser.c \
@ -147,8 +147,6 @@ SRC += \
auto4_lua_assfile.cpp \ auto4_lua_assfile.cpp \
auto4_lua_dialog.cpp \ auto4_lua_dialog.cpp \
auto4_lua_progresssink.cpp \ auto4_lua_progresssink.cpp \
auto4_lua_scriptreader.cpp \
auto4_regex.cpp \
avisynth_wrap.cpp \ avisynth_wrap.cpp \
base_grid.cpp \ base_grid.cpp \
charset_detect.cpp \ charset_detect.cpp \
@ -200,7 +198,6 @@ SRC += \
hotkey.cpp \ hotkey.cpp \
hotkey_data_view_model.cpp \ hotkey_data_view_model.cpp \
initial_line_state.cpp \ initial_line_state.cpp \
lpeg.cpp \
main.cpp \ main.cpp \
menu.cpp \ menu.cpp \
mkv_wrap.cpp \ mkv_wrap.cpp \

View File

@ -34,14 +34,13 @@
#include "auto4_lua.h" #include "auto4_lua.h"
#include "auto4_lua_utils.h"
#include "ass_attachment.h" #include "ass_attachment.h"
#include "ass_dialogue.h" #include "ass_dialogue.h"
#include "ass_info.h" #include "ass_info.h"
#include "ass_file.h" #include "ass_file.h"
#include "ass_style.h" #include "ass_style.h"
#include "auto4_lua_factory.h" #include "auto4_lua_factory.h"
#include "auto4_lua_scriptreader.h" #include "command/command.h"
#include "compat.h" #include "compat.h"
#include "include/aegisub/context.h" #include "include/aegisub/context.h"
#include "main.h" #include "main.h"
@ -52,6 +51,9 @@
#include "utils.h" #include "utils.h"
#include <libaegisub/access.h> #include <libaegisub/access.h>
#include <libaegisub/lua/modules.h>
#include <libaegisub/lua/script_reader.h>
#include <libaegisub/lua/utils.h>
#include <libaegisub/path.h> #include <libaegisub/path.h>
#include <libaegisub/make_unique.h> #include <libaegisub/make_unique.h>
@ -67,8 +69,9 @@
#include <boost/tokenizer.hpp> #include <boost/tokenizer.hpp>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <lualib.h>
#include <lauxlib.h>
#include <mutex> #include <mutex>
#include <wx/clipbrd.h> #include <wx/clipbrd.h>
#include <wx/filefn.h> #include <wx/filefn.h>
#include <wx/filename.h> #include <wx/filename.h>
@ -76,13 +79,21 @@
#include <wx/msgdlg.h> #include <wx/msgdlg.h>
#include <wx/window.h> #include <wx/window.h>
// Forward declarations of stuff with no public headers using namespace agi::lua;
int luaopen_lpeg (lua_State *L); using namespace Automation4;
extern "C" int luaopen_luabins(lua_State * L);
namespace Automation4 { int regex_init(lua_State *L); }
namespace { namespace {
using namespace Automation4;
wxString get_wxstring(lua_State *L, int idx)
{
return wxString::FromUTF8(lua_tostring(L, idx));
}
wxString check_wxstring(lua_State *L, int idx)
{
return wxString::FromUTF8(luaL_checkstring(L, idx));
}
void set_context(lua_State *L, const agi::Context *c) void set_context(lua_State *L, const agi::Context *c)
{ {
// Explicit cast is needed to discard the const // Explicit cast is needed to discard the const
@ -115,7 +126,7 @@ namespace {
int get_translation(lua_State *L) int get_translation(lua_State *L)
{ {
wxString str(check_wxstring(L, 1)); wxString str(check_wxstring(L, 1));
push_value(L, _(str)); push_value(L, _(str).utf8_str());
return 1; return 1;
} }
@ -426,19 +437,18 @@ namespace {
try { try {
// create lua environment // create lua environment
L = lua_open(); L = lua_open();
LuaStackcheck _stackcheck(L); LuaStackcheck stackcheck(L);
// register standard libs // register standard libs
push_value(L, luaopen_base); lua_call(L, 0, 0); push_value(L, luaopen_base); lua_call(L, 0, 0);
push_value(L, luaopen_io); lua_call(L, 0, 0); push_value(L, luaopen_io); lua_call(L, 0, 0);
push_value(L, luaopen_lpeg); lua_call(L, 0, 0);
push_value(L, luaopen_math); lua_call(L, 0, 0); push_value(L, luaopen_math); lua_call(L, 0, 0);
push_value(L, luaopen_os); lua_call(L, 0, 0); push_value(L, luaopen_os); lua_call(L, 0, 0);
push_value(L, luaopen_package); lua_call(L, 0, 0); push_value(L, luaopen_package); lua_call(L, 0, 0);
push_value(L, luaopen_string); lua_call(L, 0, 0); push_value(L, luaopen_string); lua_call(L, 0, 0);
push_value(L, luaopen_table); lua_call(L, 0, 0); push_value(L, luaopen_table); lua_call(L, 0, 0);
push_value(L, luaopen_luabins); lua_call(L, 0, 0); agi::lua::preload_modules(L);
_stackcheck.check_stack(0); stackcheck.check_stack(0);
// dofile and loadfile are replaced with include // dofile and loadfile are replaced with include
lua_pushnil(L); lua_pushnil(L);
@ -476,19 +486,19 @@ namespace {
push_value(L, LuaModuleLoader); push_value(L, LuaModuleLoader);
lua_rawseti(L, -2, 2); lua_rawseti(L, -2, 2);
lua_pop(L, 2); lua_pop(L, 2);
_stackcheck.check_stack(0); stackcheck.check_stack(0);
// prepare stuff in the registry // prepare stuff in the registry
// store the script's filename // store the script's filename
push_value(L, GetFilename().stem()); push_value(L, GetFilename().stem());
lua_setfield(L, LUA_REGISTRYINDEX, "filename"); lua_setfield(L, LUA_REGISTRYINDEX, "filename");
_stackcheck.check_stack(0); stackcheck.check_stack(0);
// reference to the script object // reference to the script object
push_value(L, this); push_value(L, this);
lua_setfield(L, LUA_REGISTRYINDEX, "aegisub"); lua_setfield(L, LUA_REGISTRYINDEX, "aegisub");
_stackcheck.check_stack(0); stackcheck.check_stack(0);
// make "aegisub" table // make "aegisub" table
lua_pushstring(L, "aegisub"); lua_pushstring(L, "aegisub");
@ -504,14 +514,17 @@ namespace {
set_field(L, "decode_path", decode_path); set_field(L, "decode_path", decode_path);
set_field(L, "cancel", cancel_script); set_field(L, "cancel", cancel_script);
set_field(L, "lua_automation_version", 4); set_field(L, "lua_automation_version", 4);
set_field(L, "__init_regex", regex_init);
set_field(L, "__init_clipboard", clipboard_init); set_field(L, "__init_clipboard", clipboard_init);
set_field(L, "file_name", get_file_name); set_field(L, "file_name", get_file_name);
set_field(L, "gettext", get_translation); set_field(L, "gettext", get_translation);
// store aegisub table to globals // store aegisub table to globals
lua_settable(L, LUA_GLOBALSINDEX); lua_settable(L, LUA_GLOBALSINDEX);
_stackcheck.check_stack(0); stackcheck.check_stack(0);
// Preload packaged binary modules
preload_modules(L);
stackcheck.check_stack(0);
// load user script // load user script
if (!LoadFile(L, GetFilename())) { if (!LoadFile(L, GetFilename())) {
@ -519,7 +532,7 @@ namespace {
lua_pop(L, 1); lua_pop(L, 1);
throw ScriptLoadError(err); throw ScriptLoadError(err);
} }
_stackcheck.check_stack(1); stackcheck.check_stack(1);
// and execute it // and execute it
// this is where features are registered // this is where features are registered
@ -530,7 +543,7 @@ namespace {
lua_pop(L, 1); lua_pop(L, 1);
throw ScriptLoadError(err); throw ScriptLoadError(err);
} }
_stackcheck.check_stack(0); stackcheck.check_stack(0);
lua_getglobal(L, "version"); lua_getglobal(L, "version");
if (lua_isnumber(L, -1) && lua_tointeger(L, -1) == 3) { if (lua_isnumber(L, -1) && lua_tointeger(L, -1) == 3) {
@ -548,7 +561,7 @@ namespace {
lua_pop(L, 1); lua_pop(L, 1);
// if we got this far, the script should be ready // if we got this far, the script should be ready
_stackcheck.check_stack(0); stackcheck.check_stack(0);
} }
catch (agi::Exception const& e) { catch (agi::Exception const& e) {

View File

@ -34,8 +34,6 @@
#include "auto4_base.h" #include "auto4_base.h"
#include "command/command.h"
#include <deque> #include <deque>
#include <vector> #include <vector>

View File

@ -34,7 +34,6 @@
#include "auto4_lua.h" #include "auto4_lua.h"
#include "auto4_lua_utils.h"
#include "ass_dialogue.h" #include "ass_dialogue.h"
#include "ass_info.h" #include "ass_info.h"
#include "ass_file.h" #include "ass_file.h"
@ -44,6 +43,7 @@
#include <libaegisub/exception.h> #include <libaegisub/exception.h>
#include <libaegisub/make_unique.h> #include <libaegisub/make_unique.h>
#include <libaegisub/lua/utils.h>
#include <algorithm> #include <algorithm>
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
@ -51,6 +51,8 @@
#include <memory> #include <memory>
namespace { namespace {
using namespace agi::lua;
DEFINE_SIMPLE_EXCEPTION_NOINNER(BadField, Automation4::MacroRunError, "automation/macro/bad_field") DEFINE_SIMPLE_EXCEPTION_NOINNER(BadField, Automation4::MacroRunError, "automation/macro/bad_field")
BadField bad_field(const char *expected_type, const char *name, const char *line_clasee) BadField bad_field(const char *expected_type, const char *name, const char *line_clasee)
{ {

View File

@ -34,7 +34,6 @@
#include "auto4_lua.h" #include "auto4_lua.h"
#include "auto4_lua_utils.h"
#include "ass_style.h" #include "ass_style.h"
#include "colour_button.h" #include "colour_button.h"
#include "compat.h" #include "compat.h"
@ -43,6 +42,7 @@
#include "validators.h" #include "validators.h"
#include <libaegisub/make_unique.h> #include <libaegisub/make_unique.h>
#include <libaegisub/lua/utils.h>
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <boost/range/adaptors.hpp> #include <boost/range/adaptors.hpp>
@ -64,6 +64,7 @@
#include <wx/valnum.h> #include <wx/valnum.h>
#include <wx/window.h> #include <wx/window.h>
using namespace agi::lua;
namespace { namespace {
inline void get_if_right_type(lua_State *L, std::string &def) { inline void get_if_right_type(lua_State *L, std::string &def) {
if (lua_isstring(L, -1)) if (lua_isstring(L, -1))

View File

@ -34,7 +34,7 @@
#include "auto4_lua.h" #include "auto4_lua.h"
#include "auto4_lua_utils.h" #include <libaegisub/lua/utils.h>
#include <wx/filedlg.h> #include <wx/filedlg.h>
@ -51,6 +51,11 @@ namespace {
lua_pushnil(L); lua_pushnil(L);
lua_setfield(L, idx, name); lua_setfield(L, idx, name);
} }
wxString check_wxstring(lua_State *L, int idx)
{
return wxString::FromUTF8(luaL_checkstring(L, idx));
}
} }
namespace Automation4 { namespace Automation4 {