forked from mia/Aegisub
Switch automation tests to busted
This requires shuffling a bunch of stuff around to get a CLI lua executable that uses an automation-like environment, but that's something that'll be nice to have in the future anyway. Busted indirectly depends on lfs and we currently can't use external binary modules, so add a copy of lfs to the repo and build it with Aegisub.
This commit is contained in:
parent
b8af29da0d
commit
2adcc76cf6
19 changed files with 1530 additions and 548 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -49,6 +49,7 @@ acconf.h
|
|||
acconf.h.in
|
||||
aclocal.m4
|
||||
autom4te.cache
|
||||
automation/aegisub-lua
|
||||
conf.sh
|
||||
config.log
|
||||
config.status
|
||||
|
|
|
@ -2,9 +2,7 @@ include ../Makefile.inc
|
|||
|
||||
PRECOMPILED_HEADER_NAME = ../libaegisub/lagi_pre.h
|
||||
|
||||
LIB_SHARED = aegisub.so
|
||||
LIB_VERSION = 0.1.0
|
||||
BUILD_LIB = no
|
||||
PROGRAM = aegisub-lua
|
||||
|
||||
CXXFLAGS += -I../libaegisub/include -I../src -I ../vendor/lua/src $(CXXFLAGS_WX)
|
||||
CPPFLAGS += $(CPPFLAGS_BOOST)
|
||||
|
@ -15,8 +13,8 @@ LIBS += $(LIBS_WX) $(LIBS_BOOST) $(LIBS_ICU)
|
|||
|
||||
SRC += tests/aegisub.cpp
|
||||
|
||||
test: $(LIB_SHARED)
|
||||
moon tests/runner.moon
|
||||
test: $(PROGRAM)
|
||||
LUA=./aegisub-lua busted -p 'moon' tests/modules
|
||||
|
||||
.PHONY: test
|
||||
|
||||
|
@ -58,3 +56,4 @@ install: install-share
|
|||
EXTRA_DIST = $(DATA_AUTOMATION)
|
||||
|
||||
include ../Makefile.target
|
||||
-include tests/*.d
|
||||
|
|
|
@ -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
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
|
@ -15,8 +15,59 @@
|
|||
// Aegisub Project http://www.aegisub.org/
|
||||
|
||||
#include <libaegisub/lua/modules.h>
|
||||
#include <libaegisub/lua/script_reader.h>
|
||||
#include <libaegisub/lua/utils.h>
|
||||
|
||||
extern "C" int luaopen_aegisub(lua_State *L) {
|
||||
agi::lua::preload_modules(L);
|
||||
return 1;
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <lauxlib.h>
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
|
||||
using namespace agi::lua;
|
||||
|
||||
namespace {
|
||||
void check(lua_State *L, int status) {
|
||||
if (status && !lua_isnil(L, -1)) {
|
||||
fprintf(stderr, "%s\n", get_string_or_default(L, -1).c_str());
|
||||
exit(status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: aegisub-lua <script> [args]\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Init lua state
|
||||
lua_State *L = lua_open();
|
||||
agi::lua::preload_modules(L);
|
||||
Install(L, {"include"});
|
||||
push_value(L, luaopen_debug); lua_call(L, 0, 0);
|
||||
|
||||
// Build arg table for scripts
|
||||
lua_createtable(L, argc - 1, 0);
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
lua_pushstring(L, argv[i]);
|
||||
lua_rawseti(L, -2, i - 1);
|
||||
}
|
||||
lua_setglobal(L, "arg");
|
||||
|
||||
// Stack needs to be error handler -> function -> args
|
||||
lua_pushcfunction(L, add_stack_trace);
|
||||
|
||||
try {
|
||||
check(L, !LoadFile(L, argv[1]));
|
||||
} catch (agi::Exception const& e) {
|
||||
fprintf(stderr, "%s\n", e.GetChainedMessage().c_str());
|
||||
}
|
||||
|
||||
for (int i = 2; i < argc; ++i)
|
||||
lua_pushstring(L, argv[i]);
|
||||
|
||||
int base = lua_gettop(L) - argc + 1;
|
||||
check(L, lua_pcall(L, argc - 2, LUA_MULTRET, base));
|
||||
}
|
||||
|
||||
|
|
|
@ -12,308 +12,312 @@
|
|||
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
require 'aegisub'
|
||||
require 'lunatest'
|
||||
re = require 'aegisub.re'
|
||||
|
||||
export test_compile = ->
|
||||
describe 'compile', ->
|
||||
it 'should return a object with the regexp bound', ->
|
||||
r = re.compile '.'
|
||||
|
||||
res = r\find 'abc'
|
||||
assert_not_nil res
|
||||
assert_equal 3, #res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 3, #res
|
||||
|
||||
res = r\find 'bc'
|
||||
assert_not_nil res
|
||||
assert_equal 2, #res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 2, #res
|
||||
|
||||
export test_compile_bad_call_is_error = ->
|
||||
it 'should throw an error when it is not called with \\', ->
|
||||
r = re.compile '.'
|
||||
assert_error -> r.find 'bc'
|
||||
assert.is.error -> r.find 'bc'
|
||||
|
||||
export test_find_bol_only_matches_once = ->
|
||||
it 'should throw an error when given an invalid regex', ->
|
||||
assert.is.error -> re.compile '('
|
||||
|
||||
it 'should throw an error when given an empty regex', ->
|
||||
assert.is.error -> re.compile ''
|
||||
|
||||
describe 'find', ->
|
||||
it 'should only match once when using ^', ->
|
||||
res = re.find 'aaa', '^a'
|
||||
assert_not_nil res
|
||||
assert_equal 1, #res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 1, #res
|
||||
|
||||
export test_find_finds_characters_not_bytes = ->
|
||||
it 'should match codepoints with ., not bytes', ->
|
||||
res = re.find '☃☃', '.'
|
||||
assert_not_nil res
|
||||
assert_equal 2, #res
|
||||
assert_equal '☃', res[1].str
|
||||
assert_equal 1, res[1].first
|
||||
assert_equal 3, res[1].last
|
||||
assert_equal '☃', res[2].str
|
||||
assert_equal 4, res[2].first
|
||||
assert_equal 6, res[2].last
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 2, #res
|
||||
assert.is.equal '☃', res[1].str
|
||||
assert.is.equal 1, res[1].first
|
||||
assert.is.equal 3, res[1].last
|
||||
assert.is.equal '☃', res[2].str
|
||||
assert.is.equal 4, res[2].first
|
||||
assert.is.equal 6, res[2].last
|
||||
|
||||
export test_find_no_matches_returns_nil = ->
|
||||
assert_nil(re.find 'a', 'b')
|
||||
it 'should return nil when there are no matches', ->
|
||||
assert.is.nil(re.find 'a', 'b')
|
||||
|
||||
export test_find_on_empty_string_returns_nil = ->
|
||||
assert_nil(re.find '', '.')
|
||||
it 'should return nil when called on an empty string', ->
|
||||
assert.is.nil(re.find '', '.')
|
||||
|
||||
export test_find_plain = ->
|
||||
it 'should return full match information', ->
|
||||
res = re.find 'aa', '.'
|
||||
assert_not_nil res
|
||||
assert_equal 2, #res
|
||||
assert_equal 'a', res[1].str
|
||||
assert_equal 1, res[1].first
|
||||
assert_equal 1, res[1].last
|
||||
assert_equal 'a', res[2].str
|
||||
assert_equal 2, res[2].first
|
||||
assert_equal 2, res[2].last
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 2, #res
|
||||
assert.is.equal 'a', res[1].str
|
||||
assert.is.equal 1, res[1].first
|
||||
assert.is.equal 1, res[1].last
|
||||
assert.is.equal 'a', res[2].str
|
||||
assert.is.equal 2, res[2].first
|
||||
assert.is.equal 2, res[2].last
|
||||
|
||||
export test_find_zero_length_match = ->
|
||||
it 'should be able to handle a zero-length match', ->
|
||||
res = re.find 'abc', '^'
|
||||
assert_not_nil res
|
||||
assert_equal 1, #res
|
||||
assert_equal '', res[1].str
|
||||
assert_equal 1, res[1].first
|
||||
assert_equal 0, res[1].last
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 1, #res
|
||||
assert.is.equal '', res[1].str
|
||||
assert.is.equal 1, res[1].first
|
||||
assert.is.equal 0, res[1].last
|
||||
|
||||
export test_flags_in_wrong_position_is_error = ->
|
||||
assert_error -> re.sub 'a', re.ICASE, 'b', 'c'
|
||||
assert_error -> re.sub 'a', 'b', re.ICASE, 'c'
|
||||
describe 'flag handling', ->
|
||||
it 'should be an error to pass flags somewhere other than the end', ->
|
||||
assert.is.error -> re.sub 'a', re.ICASE, 'b', 'c'
|
||||
assert.is.error -> re.sub 'a', 'b', re.ICASE, 'c'
|
||||
|
||||
export test_invalid_regex_syntax_is_error = ->
|
||||
assert_error -> re.compile '('
|
||||
|
||||
export test_match_plain = ->
|
||||
describe 'match', ->
|
||||
it 'should be able to extract values from multiple match groups', ->
|
||||
res = re.match '{250 1173 380}Help!', '(\\d+) (\\d+) (\\d+)'
|
||||
assert_not_nil res
|
||||
assert_equal 4, #res
|
||||
assert_equal '250 1173 380', res[1].str
|
||||
assert_equal 2, res[1].first
|
||||
assert_equal 13, res[1].last
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 4, #res
|
||||
assert.is.equal '250 1173 380', res[1].str
|
||||
assert.is.equal 2, res[1].first
|
||||
assert.is.equal 13, res[1].last
|
||||
|
||||
assert_equal '250', res[2].str
|
||||
assert_equal 2, res[2].first
|
||||
assert_equal 4, res[2].last
|
||||
assert.is.equal '250', res[2].str
|
||||
assert.is.equal 2, res[2].first
|
||||
assert.is.equal 4, res[2].last
|
||||
|
||||
assert_equal '1173', res[3].str
|
||||
assert_equal 6, res[3].first
|
||||
assert_equal 9, res[3].last
|
||||
assert.is.equal '1173', res[3].str
|
||||
assert.is.equal 6, res[3].first
|
||||
assert.is.equal 9, res[3].last
|
||||
|
||||
assert_equal '380', res[4].str
|
||||
assert_equal 11, res[4].first
|
||||
assert_equal 13, res[4].last
|
||||
assert.is.equal '380', res[4].str
|
||||
assert.is.equal 11, res[4].first
|
||||
assert.is.equal 13, res[4].last
|
||||
|
||||
export test_match_zero_length_match = ->
|
||||
it 'should handle zero length matches', ->
|
||||
res = re.match 'abc', '^'
|
||||
assert_not_nil res
|
||||
assert_equal 1, #res
|
||||
assert_equal '', res[1].str
|
||||
assert_equal 1, res[1].first
|
||||
assert_equal 0, res[1].last
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 1, #res
|
||||
assert.is.equal '', res[1].str
|
||||
assert.is.equal 1, res[1].first
|
||||
assert.is.equal 0, res[1].last
|
||||
|
||||
export test_split_delim_not_found_gives_input_string_in_table = ->
|
||||
describe 'split', ->
|
||||
it 'should return the input string in a table when the delimiter does not appear in the string', ->
|
||||
res = re.split 'abc', ','
|
||||
assert_not_nil res
|
||||
assert_equal 1, #res
|
||||
assert_equal 'abc', res[1]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 1, #res
|
||||
assert.is.equal 'abc', res[1]
|
||||
|
||||
export test_split_empty_string_returns_empty_table = ->
|
||||
it 'should return an empty table when given an empty string', ->
|
||||
res = re.split '', ','
|
||||
assert_not_nil res
|
||||
assert_equal 0, #res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 0, #res
|
||||
|
||||
export test_split_max_splits_noskip = ->
|
||||
it 'should honor the max splits argument', ->
|
||||
res = re.split 'a,,b,c', ',', false, 1
|
||||
assert_not_nil res
|
||||
assert_equal 2, #res
|
||||
assert_equal 'a', res[1]
|
||||
assert_equal ',b,c', res[2]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 2, #res
|
||||
assert.is.equal 'a', res[1]
|
||||
assert.is.equal ',b,c', res[2]
|
||||
|
||||
export test_split_max_splits_skip = ->
|
||||
it 'should not count skipped segments in the maximum when skipping empty', ->
|
||||
res = re.split 'a,,b,c', ',', true, 1
|
||||
assert_not_nil res
|
||||
assert_equal 2, #res
|
||||
assert_equal 'a', res[1]
|
||||
assert_equal ',b,c', res[2]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 2, #res
|
||||
assert.is.equal 'a', res[1]
|
||||
assert.is.equal ',b,c', res[2]
|
||||
|
||||
res = re.split 'a,,b,c,d', ',', true, 2
|
||||
assert_not_nil res
|
||||
assert_equal 3, #res
|
||||
assert_equal 'a', res[1]
|
||||
assert_equal 'b', res[2]
|
||||
assert_equal 'c,d', res[3]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 3, #res
|
||||
assert.is.equal 'a', res[1]
|
||||
assert.is.equal 'b', res[2]
|
||||
assert.is.equal 'c,d', res[3]
|
||||
|
||||
export test_split_multi_character_delimeter = ->
|
||||
it 'should support multi-character delimiters', ->
|
||||
res = re.split 'a::b::c:d', '::'
|
||||
assert_not_nil res
|
||||
assert_equal 3, #res
|
||||
assert_equal 'a', res[1]
|
||||
assert_equal 'b', res[2]
|
||||
assert_equal 'c:d', res[3]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 3, #res
|
||||
assert.is.equal 'a', res[1]
|
||||
assert.is.equal 'b', res[2]
|
||||
assert.is.equal 'c:d', res[3]
|
||||
|
||||
export test_split_plain = ->
|
||||
it 'should not fail on basic splits', ->
|
||||
res = re.split 'a,,b,c', ','
|
||||
assert_not_nil res
|
||||
assert_equal 4, #res
|
||||
assert_equal 'a', res[1]
|
||||
assert_equal '', res[2]
|
||||
assert_equal 'b', res[3]
|
||||
assert_equal 'c', res[4]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 4, #res
|
||||
assert.is.equal 'a', res[1]
|
||||
assert.is.equal '', res[2]
|
||||
assert.is.equal 'b', res[3]
|
||||
assert.is.equal 'c', res[4]
|
||||
|
||||
export test_split_skip_empty = ->
|
||||
it 'should be able to skip empty segments', ->
|
||||
res = re.split 'a,,b,c', ',', true
|
||||
assert_not_nil res
|
||||
assert_equal 3, #res
|
||||
assert_equal 'a', res[1]
|
||||
assert_equal 'b', res[2]
|
||||
assert_equal 'c', res[3]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 3, #res
|
||||
assert.is.equal 'a', res[1]
|
||||
assert.is.equal 'b', res[2]
|
||||
assert.is.equal 'c', res[3]
|
||||
|
||||
export test_sub_bad_find_is_error = ->
|
||||
assert_error -> re.sub 'aa', 5, 'b'
|
||||
it 'should be able to split on word boundaries', ->
|
||||
res = re.split 'aa bb cc', '\\b', true
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 5, #res
|
||||
assert.is.equal res[1], 'aa'
|
||||
assert.is.equal res[2], ' '
|
||||
assert.is.equal res[3], 'bb'
|
||||
assert.is.equal res[4], ' '
|
||||
assert.is.equal res[5], 'cc'
|
||||
|
||||
export test_sub_bad_haystack_is_error = ->
|
||||
assert_error -> re.sub 5, 'a', 'b'
|
||||
it 'should be able to split on the beginning of the buffer', ->
|
||||
res = re.split 'aa bb cc', '\\A'
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 2, #res
|
||||
assert.is.equal '', res[1]
|
||||
assert.is.equal 'aa bb cc', res[2]
|
||||
|
||||
export test_sub_bad_replacement_is_error = ->
|
||||
assert_error -> re.sub 'aa', 'a', 5
|
||||
describe 'sub', ->
|
||||
it 'should throw an error when given a non-string to search for', ->
|
||||
assert.is.error -> re.sub 'aa', 5, 'b'
|
||||
|
||||
export test_sub_capture_groups = ->
|
||||
assert_equal 'aabbaabb', re.sub 'abab', '(a)(b)', (str) -> str .. str
|
||||
it 'should throw an error when given a non-string to search in', ->
|
||||
assert.is.error -> re.sub 5, 'a', 'b'
|
||||
|
||||
export test_sub_empty_string_returns_empty = ->
|
||||
assert_equal '', re.sub '', 'b', 'c'
|
||||
it 'should throw an error when given a non-string to replace with', ->
|
||||
assert.is.error -> re.sub 'aa', 'a', 5
|
||||
|
||||
export test_sub_function = ->
|
||||
it 'should pass capture groups to the replacement function if given one', ->
|
||||
assert.is.equal 'aabbaabb', re.sub 'abab', '(a)(b)', (str) -> str .. str
|
||||
|
||||
it 'should return an empty string when given an empty string', ->
|
||||
assert.is.equal '', re.sub '', 'b', 'c'
|
||||
|
||||
it 'should support functions for replacements in addition to strings', ->
|
||||
add_one = (str) -> tostring tonumber(str) + 1
|
||||
|
||||
res = re.sub '{\\k10}a{\\k15}b{\\k30}c', '\\\\k([[:digit:]]+)', add_one
|
||||
assert_not_nil res
|
||||
assert_equal '{\\k11}a{\\k16}b{\\k31}c', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal '{\\k11}a{\\k16}b{\\k31}c', res
|
||||
|
||||
export test_sub_function_no_capture_group_passes_full_match = ->
|
||||
it 'should pass the full match to the replacement function if there are no capture groups', ->
|
||||
found = {}
|
||||
drop_match = (str) ->
|
||||
table.insert found, str
|
||||
''
|
||||
|
||||
res = re.sub '{\\k10}a{\\k15}b{\\k30}c', '\\\\k[[:digit:]]+', drop_match
|
||||
assert_not_nil res
|
||||
assert_equal '{}a{}b{}c', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal '{}a{}b{}c', res
|
||||
|
||||
assert_equal 3, #found
|
||||
assert_equal '\\k10', found[1]
|
||||
assert_equal '\\k15', found[2]
|
||||
assert_equal '\\k30', found[3]
|
||||
assert.is.equal 3, #found
|
||||
assert.is.equal '\\k10', found[1]
|
||||
assert.is.equal '\\k15', found[2]
|
||||
assert.is.equal '\\k30', found[3]
|
||||
|
||||
export test_sub_function_returning_non_string_returns_unchanged_input = ->
|
||||
it 'should return the input unchanged if the replacement function does not return a string', ->
|
||||
found = {}
|
||||
mutate_external = (str) ->
|
||||
table.insert found, str
|
||||
nil
|
||||
|
||||
res = re.sub '{\\k10}a{\\k15}b{\\k30}c', '\\\\k([[:digit:]]+)', mutate_external
|
||||
assert_not_nil res
|
||||
assert_equal '{\\k10}a{\\k15}b{\\k30}c', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal '{\\k10}a{\\k15}b{\\k30}c', res
|
||||
|
||||
assert_equal 3, #found
|
||||
assert_equal '10', found[1]
|
||||
assert_equal '15', found[2]
|
||||
assert_equal '30', found[3]
|
||||
assert.is.equal 3, #found
|
||||
assert.is.equal '10', found[1]
|
||||
assert.is.equal '15', found[2]
|
||||
assert.is.equal '30', found[3]
|
||||
|
||||
export test_sub_icase = ->
|
||||
it 'should be able to do case-insensitive replacements on English', ->
|
||||
res = re.sub '{\\K10}a{\\K15}b{\\k30}c', '\\\\k', '\\\\kf', re.ICASE
|
||||
assert_not_nil res
|
||||
assert_equal '{\\kf10}a{\\kf15}b{\\kf30}c', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal '{\\kf10}a{\\kf15}b{\\kf30}c', res
|
||||
|
||||
export test_sub_icase_greek = ->
|
||||
it 'should be able to do case-insensitive replacements on Greek', ->
|
||||
res = re.sub '!συνεργ!', 'Συνεργ', 'foo', re.ICASE
|
||||
assert_not_nil res
|
||||
assert_equal '!foo!', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal '!foo!', res
|
||||
|
||||
export test_sub_max_replace_count = ->
|
||||
it 'should be able to limit the number of replacements', ->
|
||||
res = re.sub 'aaa', 'a', 'b', 2
|
||||
assert_not_nil res
|
||||
assert_equal 'bba', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'bba', res
|
||||
|
||||
export test_sub_no_matches_leaves_unchanged = ->
|
||||
assert_equal('a', re.sub 'a', 'b', 'c')
|
||||
it 'should return the input unchanged if there are no matches', ->
|
||||
assert.is.equal('a', re.sub 'a', 'b', 'c')
|
||||
|
||||
export test_sub_plain = ->
|
||||
it 'should be able to do simple string replacements', ->
|
||||
res = re.sub '{\\k10}a{\\k15}b{\\k30}c', '\\\\k', '\\\\kf'
|
||||
assert_not_nil res
|
||||
assert_equal '{\\kf10}a{\\kf15}b{\\kf30}c', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal '{\\kf10}a{\\kf15}b{\\kf30}c', res
|
||||
|
||||
export test_sub_zero_length_match_bol = ->
|
||||
it 'should replace only once when given a zero-length-bol-match', ->
|
||||
res = re.sub 'abc', '^', 'd'
|
||||
assert_not_nil res
|
||||
assert_equal 'dabc', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dabc', res
|
||||
|
||||
export test_sub_zero_length_match_bow = ->
|
||||
it 'should replace only once when given a zero-length-bow-match', ->
|
||||
res = re.sub 'abc abc', '\\<', 'd'
|
||||
assert_not_nil res
|
||||
assert_equal 'dabc dabc', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dabc dabc', res
|
||||
|
||||
export test_sub_zero_length_match_bob = ->
|
||||
it 'should replace only once when given a zero-length-bob-match', ->
|
||||
res = re.sub 'abc', '\\A', 'd'
|
||||
assert_not_nil res
|
||||
assert_equal 'dabc', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dabc', res
|
||||
|
||||
assert_empty_match_and_return_d = (str) ->
|
||||
assert_equal '', str
|
||||
assert_empty_match_and_return_d = (str) ->
|
||||
assert.is.equal '', str
|
||||
'd'
|
||||
|
||||
export test_sub_zero_length_match_bol_with_function_replacement = ->
|
||||
it 'should replace only once when given a zero-length-bol-match and a function replacements', ->
|
||||
res = re.sub 'abc', '^', assert_empty_match_and_return_d
|
||||
assert_not_nil res
|
||||
assert_equal 'dabc', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dabc', res
|
||||
|
||||
export test_sub_zero_length_match_bow_with_function_replacement = ->
|
||||
it 'should replace only once when given a zero-length-bow-match and a function replacements', ->
|
||||
res = re.sub 'abc abc', '\\<', assert_empty_match_and_return_d
|
||||
assert_not_nil res
|
||||
assert_equal 'dabc dabc', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dabc dabc', res
|
||||
|
||||
export test_sub_zero_length_match_bob_with_function_replacement = ->
|
||||
it 'should replace only once when given a zero-length-bob-match and a function replacements', ->
|
||||
res = re.sub 'abc', '\\A', assert_empty_match_and_return_d
|
||||
assert_not_nil res
|
||||
assert_equal 'dabc', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dabc', res
|
||||
|
||||
export test_sub_zero_length_match_eol = ->
|
||||
it 'should replace only once when given a zero-length-eol-match', ->
|
||||
res = re.sub 'abc', '$', 'd'
|
||||
assert_not_nil res
|
||||
assert_equal 'abcd', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'abcd', res
|
||||
|
||||
export test_sub_zero_length_match_eol_with_function_replacement = ->
|
||||
it 'should replace only once when given a zero-length-eol-match and a function replacements', ->
|
||||
res = re.sub 'abc', '$', assert_empty_match_and_return_d
|
||||
assert_not_nil res
|
||||
assert_equal 'abcd', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'abcd', res
|
||||
|
||||
export test_sub_zero_length_match_mid = ->
|
||||
it 'should apply unanchored zero-length matches at each point in the string', ->
|
||||
res = re.sub 'abc', 'e?', 'd'
|
||||
assert_not_nil res
|
||||
assert_equal 'dadbdcd', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dadbdcd', res
|
||||
|
||||
res = re.sub 'abc', 'b*', 'd'
|
||||
assert_not_nil res
|
||||
assert_equal 'daddcd', res
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'daddcd', res
|
||||
|
||||
export test_sub_zero_length_match_mid_with_function_replacement = ->
|
||||
it 'should apply unanchored zero-length matches with function insertions at each point in the string', ->
|
||||
res = re.sub 'abc', 'e?', assert_empty_match_and_return_d
|
||||
assert_not_nil res
|
||||
assert_equal 'dadbdcd', res
|
||||
|
||||
export test_zero_length_regex_is_error = ->
|
||||
assert_error -> re.compile ''
|
||||
|
||||
export test_split_word_boundary = ->
|
||||
res = re.split 'aa bb cc', '\\b', true
|
||||
assert_not_nil res
|
||||
assert_equal 5, #res
|
||||
assert_equal res[1], 'aa'
|
||||
assert_equal res[2], ' '
|
||||
assert_equal res[3], 'bb'
|
||||
assert_equal res[4], ' '
|
||||
assert_equal res[5], 'cc'
|
||||
|
||||
export test_split_bob = ->
|
||||
res = re.split 'aa bb cc', '\\A'
|
||||
assert_not_nil res
|
||||
assert_equal 2, #res
|
||||
assert_equal '', res[1]
|
||||
assert_equal 'aa bb cc', res[2]
|
||||
assert.is.not.nil res
|
||||
assert.is.equal 'dadbdcd', res
|
||||
|
||||
|
|
|
@ -12,28 +12,36 @@
|
|||
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
require 'lunatest'
|
||||
unicode = require 'aegisub.unicode'
|
||||
|
||||
export test_char_widths = ->
|
||||
assert_equal 1, unicode.charwidth 'a'
|
||||
assert_equal 2, unicode.charwidth 'ß'
|
||||
assert_equal 3, unicode.charwidth 'c'
|
||||
assert_equal 4, unicode.charwidth '🄓'
|
||||
describe 'charwidth', ->
|
||||
it 'should return 1 for an ascii character', ->
|
||||
assert.is.equal 1, unicode.charwidth 'a'
|
||||
it 'should return 2 for a two byte character', ->
|
||||
assert.is.equal 2, unicode.charwidth 'ß'
|
||||
it 'should return 3 for a three byte character', ->
|
||||
assert.is.equal 3, unicode.charwidth 'c'
|
||||
it 'should return 4 for a four byte character', ->
|
||||
assert.is.equal 4, unicode.charwidth '🄓'
|
||||
|
||||
export test_char_iterator = ->
|
||||
describe 'char_iterator', ->
|
||||
it 'should iterator over multi-byte codepoints', ->
|
||||
chars = [c for c in unicode.chars 'aßc🄓']
|
||||
assert_equal 4, #chars
|
||||
assert_equal chars[1], 'a'
|
||||
assert_equal chars[2], 'ß'
|
||||
assert_equal chars[3], 'c'
|
||||
assert_equal chars[4], '🄓'
|
||||
assert.is.equal 4, #chars
|
||||
assert.is.equal chars[1], 'a'
|
||||
assert.is.equal chars[2], 'ß'
|
||||
assert.is.equal chars[3], 'c'
|
||||
assert.is.equal chars[4], '🄓'
|
||||
|
||||
export test_len = ->
|
||||
assert_equal 4, unicode.len 'aßc🄓'
|
||||
describe 'len', ->
|
||||
it 'should give length in codepoints', ->
|
||||
assert.is.equal 4, unicode.len 'aßc🄓'
|
||||
|
||||
export test_codepoint = ->
|
||||
assert_equal 97, unicode.codepoint 'a'
|
||||
assert_equal 223, unicode.codepoint 'ß'
|
||||
assert_equal 0xFF43, unicode.codepoint 'c'
|
||||
assert_equal 0x1F113, unicode.codepoint '🄓'
|
||||
describe 'codepoint', ->
|
||||
it 'should give codepoint as an integer for a string', ->
|
||||
assert.is.equal 97, unicode.codepoint 'a'
|
||||
assert.is.equal 223, unicode.codepoint 'ß'
|
||||
assert.is.equal 0xFF43, unicode.codepoint 'c'
|
||||
assert.is.equal 0x1F113, unicode.codepoint '🄓'
|
||||
it 'should give ignore codepoints after the first', ->
|
||||
assert.is.equal 97, unicode.codepoint 'abc'
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
-- Copyright (c) 2013, 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.
|
||||
|
||||
require 'lunatest'
|
||||
require 'lfs'
|
||||
|
||||
package.path ..= ';include/?.lua;'
|
||||
package.moonpath ..= ';include/?.moon;'
|
||||
|
||||
for file in lfs.dir 'tests/modules'
|
||||
require "tests.modules.#{file\gsub '\.[^.]+$', ''}" unless file\sub(1, 1) == '.'
|
||||
|
||||
lunatest.run()
|
|
@ -121,9 +121,11 @@
|
|||
<ClCompile Include="$(SrcDir)common\util.cpp" />
|
||||
<ClCompile Include="$(SrcDir)common\vfr.cpp" />
|
||||
<ClCompile Include="$(SrcDir)lua\modules.cpp" />
|
||||
<ClCompile Include="$(SrcDir)lua\modules\lfs.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)lua\utils.cpp" />
|
||||
<ClCompile Include="$(SrcDir)windows\access.cpp" />
|
||||
<ClCompile Include="$(SrcDir)windows\charset_conv_win.cpp" />
|
||||
<ClCompile Include="$(SrcDir)windows\fs.cpp" />
|
||||
|
|
|
@ -304,6 +304,12 @@
|
|||
<ClCompile Include="$(SrcDir)lua\script_reader.cpp">
|
||||
<Filter>Lua</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="$(SrcDir)lua\utils.cpp">
|
||||
<Filter>Lua</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="$(SrcDir)lua\modules\lfs.cpp">
|
||||
<Filter>Lua\Modules</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="$(SrcDir)lua\modules\lpeg.cpp">
|
||||
<Filter>Lua\Modules</Filter>
|
||||
</ClCompile>
|
||||
|
|
|
@ -42,9 +42,11 @@ SRC += \
|
|||
common/util.cpp \
|
||||
common/vfr.cpp \
|
||||
lua/modules.cpp \
|
||||
lua/modules/lfs.cpp \
|
||||
lua/modules/lpeg.cpp \
|
||||
lua/modules/re.cpp \
|
||||
lua/script_reader.cpp \
|
||||
lua/utils.cpp \
|
||||
unix/access.cpp \
|
||||
unix/fs.cpp \
|
||||
unix/log.cpp \
|
||||
|
|
|
@ -16,8 +16,14 @@
|
|||
|
||||
#include <libaegisub/fs_fwd.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct lua_State;
|
||||
|
||||
namespace agi { namespace lua {
|
||||
/// Load a Lua or Moonscript file at the given path
|
||||
bool LoadFile(lua_State *L, agi::fs::path const& filename);
|
||||
/// Install our module loader and add include_path to the module search
|
||||
/// path of the given lua state
|
||||
void Install(lua_State *L, std::vector<fs::path> const& include_path);
|
||||
} }
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
// Aegisub Project http://www.aegisub.org/
|
||||
|
||||
#include <libaegisub/fs.h>
|
||||
#include <libaegisub/log.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
|
@ -59,34 +58,13 @@ inline void set_field(lua_State *L, const char *name, T value) {
|
|||
lua_setfield(L, -2, name);
|
||||
}
|
||||
|
||||
inline std::string get_string_or_default(lua_State *L, int idx) {
|
||||
size_t len = 0;
|
||||
const char *str = lua_tolstring(L, idx, &len);
|
||||
if (!str)
|
||||
return "<not a string>";
|
||||
return std::string(str, len);
|
||||
}
|
||||
std::string get_string_or_default(lua_State *L, int idx);
|
||||
|
||||
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);
|
||||
}
|
||||
std::string get_string(lua_State *L, int idx);
|
||||
|
||||
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);
|
||||
}
|
||||
std::string check_string(lua_State *L, int idx);
|
||||
|
||||
inline std::string get_global_string(lua_State *L, const char *name) {
|
||||
lua_getglobal(L, name);
|
||||
std::string ret;
|
||||
if (lua_isstring(L, -1))
|
||||
ret = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return ret;
|
||||
}
|
||||
std::string get_global_string(lua_State *L, const char *name);
|
||||
|
||||
template<typename T, typename... Args>
|
||||
T *make(lua_State *L, const char *mt, Args&&... args) {
|
||||
|
@ -120,34 +98,16 @@ void lua_for_each(lua_State *L, Func&& func) {
|
|||
lua_pop(L, 1); // pop table
|
||||
}
|
||||
|
||||
int add_stack_trace(lua_State *L);
|
||||
|
||||
#ifdef _DEBUG
|
||||
struct LuaStackcheck {
|
||||
lua_State *L;
|
||||
int startstack;
|
||||
|
||||
void check_stack(int additional) {
|
||||
int top = lua_gettop(L);
|
||||
if (top - additional != startstack) {
|
||||
LOG_D("automation/lua") << "lua stack size mismatch.";
|
||||
dump();
|
||||
assert(top - additional == startstack);
|
||||
}
|
||||
}
|
||||
void check_stack(int additional);
|
||||
void dump();
|
||||
|
||||
void dump() {
|
||||
int top = lua_gettop(L);
|
||||
LOG_D("automation/lua/stackdump") << "--- dumping lua stack...";
|
||||
for (int i = top; i > 0; i--) {
|
||||
lua_pushvalue(L, i);
|
||||
std::string type(lua_typename(L, lua_type(L, -1)));
|
||||
if (lua_isstring(L, i))
|
||||
LOG_D("automation/lua/stackdump") << type << ": " << lua_tostring(L, -1);
|
||||
else
|
||||
LOG_D("automation/lua/stackdump") << type;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
LOG_D("automation/lua") << "--- end dump";
|
||||
}
|
||||
LuaStackcheck(lua_State *L) : L(L), startstack(lua_gettop(L)) { }
|
||||
~LuaStackcheck() { check_stack(0); }
|
||||
};
|
||||
|
|
|
@ -18,20 +18,32 @@
|
|||
|
||||
#include "libaegisub/lua/utils.h"
|
||||
|
||||
int luaopen_lpeg(lua_State *L);
|
||||
#include <lualib.h>
|
||||
|
||||
extern "C" int luaopen_luabins(lua_State *L);
|
||||
extern "C" int luaopen_re_impl(lua_State *L);
|
||||
int luaopen_lfs(lua_State *L);
|
||||
int luaopen_lpeg(lua_State *L);
|
||||
|
||||
namespace agi { namespace lua {
|
||||
int regex_init(lua_State *L);
|
||||
|
||||
void preload_modules(lua_State *L) {
|
||||
push_value(L, luaopen_base); lua_call(L, 0, 0);
|
||||
push_value(L, luaopen_io); 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_package); 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);
|
||||
|
||||
lua_getglobal(L, "package");
|
||||
lua_getfield(L, -1, "preload");
|
||||
|
||||
set_field(L, "aegisub.__re_impl", luaopen_re_impl);
|
||||
set_field(L, "lfs", luaopen_lfs);
|
||||
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);
|
||||
}
|
||||
|
|
891
libaegisub/lua/modules/lfs.cpp
Normal file
891
libaegisub/lua/modules/lfs.cpp
Normal file
|
@ -0,0 +1,891 @@
|
|||
/*
|
||||
** LuaFileSystem
|
||||
** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
|
||||
**
|
||||
** File system manipulation library.
|
||||
** This library offers these functions:
|
||||
** lfs.attributes (filepath [, attributename])
|
||||
** lfs.chdir (path)
|
||||
** lfs.currentdir ()
|
||||
** lfs.dir (path)
|
||||
** lfs.lock (fh, mode)
|
||||
** lfs.lock_dir (path)
|
||||
** lfs.mkdir (path)
|
||||
** lfs.rmdir (path)
|
||||
** lfs.setmode (filepath, mode)
|
||||
** lfs.symlinkattributes (filepath [, attributename]) -- thanks to Sam Roberts
|
||||
** lfs.touch (filepath [, atime [, mtime]])
|
||||
** lfs.unlock (fh)
|
||||
**
|
||||
** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
|
||||
*/
|
||||
|
||||
#ifndef _WIN32
|
||||
#ifndef _AIX
|
||||
#define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */
|
||||
#else
|
||||
#define _LARGE_FILES 1 /* AIX */
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define _LARGEFILE64_SOURCE
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <sys/locking.h>
|
||||
#ifdef __BORLANDC__
|
||||
#include <utime.h>
|
||||
#else
|
||||
#include <sys/utime.h>
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <utime.h>
|
||||
#endif
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#define chdir_error strerror(errno)
|
||||
|
||||
#define LFS_VERSION "1.6.2"
|
||||
#define LFS_LIBNAME "lfs"
|
||||
|
||||
#if LUA_VERSION_NUM < 502
|
||||
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
|
||||
#endif
|
||||
|
||||
/* Define 'strerror' for systems that do not implement it */
|
||||
#ifdef NO_STRERROR
|
||||
#define strerror(_) "System unable to describe the error"
|
||||
#endif
|
||||
|
||||
/* Define 'getcwd' for systems that do not implement it */
|
||||
#ifdef NO_GETCWD
|
||||
#define getcwd(p,s) NULL
|
||||
#define getcwd_error "Function 'getcwd' not provided by system"
|
||||
#else
|
||||
#define getcwd_error strerror(errno)
|
||||
#ifdef _WIN32
|
||||
/* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
|
||||
#define LFS_MAXPATHLEN MAX_PATH
|
||||
#else
|
||||
/* For MAXPATHLEN: */
|
||||
#include <sys/param.h>
|
||||
#define LFS_MAXPATHLEN MAXPATHLEN
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define DIR_METATABLE "directory metatable"
|
||||
typedef struct dir_data {
|
||||
int closed;
|
||||
#ifdef _WIN32
|
||||
intptr_t hFile;
|
||||
char pattern[MAX_PATH+1];
|
||||
#else
|
||||
DIR *dir;
|
||||
#endif
|
||||
} dir_data;
|
||||
|
||||
#define LOCK_METATABLE "lock metatable"
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifdef __BORLANDC__
|
||||
#define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m))
|
||||
#define STAT_STRUCT struct stati64
|
||||
#else
|
||||
#define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m))
|
||||
#define STAT_STRUCT struct _stati64
|
||||
#endif
|
||||
#define STAT_FUNC _stati64
|
||||
#define LSTAT_FUNC STAT_FUNC
|
||||
#else
|
||||
#define _O_TEXT 0
|
||||
#define _O_BINARY 0
|
||||
#define lfs_setmode(L,file,m) ((void)L, (void)file, (void)m, 0)
|
||||
#define STAT_STRUCT struct stat
|
||||
#define STAT_FUNC stat
|
||||
#define LSTAT_FUNC lstat
|
||||
#endif
|
||||
|
||||
/*
|
||||
** Utility functions
|
||||
*/
|
||||
static int pusherror(lua_State *L, const char *info)
|
||||
{
|
||||
lua_pushnil(L);
|
||||
if (info==NULL)
|
||||
lua_pushstring(L, strerror(errno));
|
||||
else
|
||||
lua_pushfstring(L, "%s: %s", info, strerror(errno));
|
||||
lua_pushinteger(L, errno);
|
||||
return 3;
|
||||
}
|
||||
|
||||
static int pushresult(lua_State *L, int i, const char *info)
|
||||
{
|
||||
if (i==-1)
|
||||
return pusherror(L, info);
|
||||
lua_pushinteger(L, i);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** This function changes the working (current) directory
|
||||
*/
|
||||
static int change_dir (lua_State *L) {
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
if (chdir(path)) {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n",
|
||||
path, chdir_error);
|
||||
return 2;
|
||||
} else {
|
||||
lua_pushboolean (L, 1);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** This function returns the current directory
|
||||
** If unable to get the current directory, it returns nil
|
||||
** and a string describing the error
|
||||
*/
|
||||
static int get_dir (lua_State *L) {
|
||||
char *path;
|
||||
/* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
|
||||
char buf[LFS_MAXPATHLEN];
|
||||
if ((path = getcwd(buf, LFS_MAXPATHLEN)) == NULL) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, getcwd_error);
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
lua_pushstring(L, path);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Check if the given element on the stack is a file and returns it.
|
||||
*/
|
||||
static FILE *check_file (lua_State *L, int idx, const char *funcname) {
|
||||
FILE **fh = (FILE **)luaL_checkudata (L, idx, "FILE*");
|
||||
if (fh == NULL) {
|
||||
luaL_error (L, "%s: not a file", funcname);
|
||||
return 0;
|
||||
} else if (*fh == NULL) {
|
||||
luaL_error (L, "%s: closed file", funcname);
|
||||
return 0;
|
||||
} else
|
||||
return *fh;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
**
|
||||
*/
|
||||
static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname) {
|
||||
int code;
|
||||
#ifdef _WIN32
|
||||
/* lkmode valid values are:
|
||||
LK_LOCK Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
|
||||
LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
|
||||
LK_NBRLCK Same as _LK_NBLCK.
|
||||
LK_RLCK Same as _LK_LOCK.
|
||||
LK_UNLCK Unlocks the specified bytes, which must have been previously locked.
|
||||
|
||||
Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
|
||||
|
||||
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
|
||||
*/
|
||||
int lkmode;
|
||||
switch (*mode) {
|
||||
case 'r': lkmode = LK_NBLCK; break;
|
||||
case 'w': lkmode = LK_NBLCK; break;
|
||||
case 'u': lkmode = LK_UNLCK; break;
|
||||
default : return luaL_error (L, "%s: invalid mode", funcname);
|
||||
}
|
||||
if (!len) {
|
||||
fseek (fh, 0L, SEEK_END);
|
||||
len = ftell (fh);
|
||||
}
|
||||
fseek (fh, start, SEEK_SET);
|
||||
#ifdef __BORLANDC__
|
||||
code = locking (fileno(fh), lkmode, len);
|
||||
#else
|
||||
code = _locking (fileno(fh), lkmode, len);
|
||||
#endif
|
||||
#else
|
||||
struct flock f;
|
||||
switch (*mode) {
|
||||
case 'w': f.l_type = F_WRLCK; break;
|
||||
case 'r': f.l_type = F_RDLCK; break;
|
||||
case 'u': f.l_type = F_UNLCK; break;
|
||||
default : return luaL_error (L, "%s: invalid mode", funcname);
|
||||
}
|
||||
f.l_whence = SEEK_SET;
|
||||
f.l_start = (off_t)start;
|
||||
f.l_len = (off_t)len;
|
||||
code = fcntl (fileno(fh), F_SETLK, &f);
|
||||
#endif
|
||||
return (code != -1);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef struct lfs_Lock {
|
||||
HANDLE fd;
|
||||
} lfs_Lock;
|
||||
static int lfs_lock_dir(lua_State *L) {
|
||||
size_t pathl; HANDLE fd;
|
||||
lfs_Lock *lock;
|
||||
char *ln;
|
||||
const char *lockfile = "/lockfile.lfs";
|
||||
const char *path = luaL_checklstring(L, 1, &pathl);
|
||||
ln = (char*)malloc(pathl + strlen(lockfile) + 1);
|
||||
if(!ln) {
|
||||
lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
|
||||
}
|
||||
strcpy(ln, path); strcat(ln, lockfile);
|
||||
if((fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_NEW,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)) == INVALID_HANDLE_VALUE) {
|
||||
int en = GetLastError();
|
||||
free(ln); lua_pushnil(L);
|
||||
if(en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION)
|
||||
lua_pushstring(L, "File exists");
|
||||
else
|
||||
lua_pushstring(L, strerror(en));
|
||||
return 2;
|
||||
}
|
||||
free(ln);
|
||||
lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock));
|
||||
lock->fd = fd;
|
||||
luaL_getmetatable (L, LOCK_METATABLE);
|
||||
lua_setmetatable (L, -2);
|
||||
return 1;
|
||||
}
|
||||
static int lfs_unlock_dir(lua_State *L) {
|
||||
lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
|
||||
if(lock->fd != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(lock->fd);
|
||||
lock->fd=INVALID_HANDLE_VALUE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
typedef struct lfs_Lock {
|
||||
char *ln;
|
||||
} lfs_Lock;
|
||||
static int lfs_lock_dir(lua_State *L) {
|
||||
lfs_Lock *lock;
|
||||
size_t pathl;
|
||||
char *ln;
|
||||
const char *lockfile = "/lockfile.lfs";
|
||||
const char *path = luaL_checklstring(L, 1, &pathl);
|
||||
lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock));
|
||||
ln = (char*)malloc(pathl + strlen(lockfile) + 1);
|
||||
if(!ln) {
|
||||
lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
|
||||
}
|
||||
strcpy(ln, path); strcat(ln, lockfile);
|
||||
if(symlink("lock", ln) == -1) {
|
||||
free(ln); lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno)); return 2;
|
||||
}
|
||||
lock->ln = ln;
|
||||
luaL_getmetatable (L, LOCK_METATABLE);
|
||||
lua_setmetatable (L, -2);
|
||||
return 1;
|
||||
}
|
||||
static int lfs_unlock_dir(lua_State *L) {
|
||||
auto lock = (lfs_Lock *)luaL_checkudata(L, 1, LOCK_METATABLE);
|
||||
if(lock->ln) {
|
||||
unlink(lock->ln);
|
||||
free(lock->ln);
|
||||
lock->ln = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
|
||||
static const int mode[] = {_O_BINARY, _O_TEXT};
|
||||
static const char *const modenames[] = {"binary", "text", NULL};
|
||||
int op = luaL_checkoption(L, arg, NULL, modenames);
|
||||
int res = lfs_setmode(L, f, mode[op]);
|
||||
if (res != -1) {
|
||||
int i;
|
||||
lua_pushboolean(L, 1);
|
||||
for (i = 0; modenames[i] != NULL; i++) {
|
||||
if (mode[i] == res) {
|
||||
lua_pushstring(L, modenames[i]);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
lua_pushnil(L);
|
||||
exit:
|
||||
return 2;
|
||||
} else {
|
||||
int en = errno;
|
||||
lua_pushnil(L);
|
||||
lua_pushfstring(L, "%s", strerror(en));
|
||||
lua_pushinteger(L, en);
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
static int lfs_f_setmode(lua_State *L) {
|
||||
return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
|
||||
}
|
||||
|
||||
/*
|
||||
** Locks a file.
|
||||
** @param #1 File handle.
|
||||
** @param #2 String with lock mode ('w'rite, 'r'ead).
|
||||
** @param #3 Number with start position (optional).
|
||||
** @param #4 Number with length (optional).
|
||||
*/
|
||||
static int file_lock (lua_State *L) {
|
||||
FILE *fh = check_file (L, 1, "lock");
|
||||
const char *mode = luaL_checkstring (L, 2);
|
||||
const long start = luaL_optlong (L, 3, 0);
|
||||
long len = luaL_optlong (L, 4, 0);
|
||||
if (_file_lock (L, fh, mode, start, len, "lock")) {
|
||||
lua_pushboolean (L, 1);
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L, "%s", strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Unlocks a file.
|
||||
** @param #1 File handle.
|
||||
** @param #2 Number with start position (optional).
|
||||
** @param #3 Number with length (optional).
|
||||
*/
|
||||
static int file_unlock (lua_State *L) {
|
||||
FILE *fh = check_file (L, 1, "unlock");
|
||||
const long start = luaL_optlong (L, 2, 0);
|
||||
long len = luaL_optlong (L, 3, 0);
|
||||
if (_file_lock (L, fh, "u", start, len, "unlock")) {
|
||||
lua_pushboolean (L, 1);
|
||||
return 1;
|
||||
} else {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L, "%s", strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Creates a link.
|
||||
** @param #1 Object to link to.
|
||||
** @param #2 Name of link.
|
||||
** @param #3 True if link is symbolic (optional).
|
||||
*/
|
||||
static int make_link(lua_State *L)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
const char *oldpath = luaL_checkstring(L, 1);
|
||||
const char *newpath = luaL_checkstring(L, 2);
|
||||
return pushresult(L,
|
||||
(lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL);
|
||||
#else
|
||||
return pusherror(L, "make_link is not supported on Windows");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Creates a directory.
|
||||
** @param #1 Directory path.
|
||||
*/
|
||||
static int make_dir (lua_State *L) {
|
||||
const char *path = luaL_checkstring (L, 1);
|
||||
int fail;
|
||||
#ifdef _WIN32
|
||||
fail = _mkdir (path);
|
||||
#else
|
||||
fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
|
||||
S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
|
||||
#endif
|
||||
if (fail) {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L, "%s", strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean (L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Removes a directory.
|
||||
** @param #1 Directory path.
|
||||
*/
|
||||
static int remove_dir (lua_State *L) {
|
||||
const char *path = luaL_checkstring (L, 1);
|
||||
int fail;
|
||||
|
||||
fail = rmdir (path);
|
||||
|
||||
if (fail) {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L, "%s", strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean (L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Directory iterator
|
||||
*/
|
||||
static int dir_iter (lua_State *L) {
|
||||
#ifdef _WIN32
|
||||
struct _finddata_t c_file;
|
||||
#else
|
||||
struct dirent *entry;
|
||||
#endif
|
||||
dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
|
||||
luaL_argcheck (L, d->closed == 0, 1, "closed directory");
|
||||
#ifdef _WIN32
|
||||
if (d->hFile == 0L) { /* first entry */
|
||||
if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) {
|
||||
lua_pushnil (L);
|
||||
lua_pushstring (L, strerror (errno));
|
||||
d->closed = 1;
|
||||
return 2;
|
||||
} else {
|
||||
lua_pushstring (L, c_file.name);
|
||||
return 1;
|
||||
}
|
||||
} else { /* next entry */
|
||||
if (_findnext (d->hFile, &c_file) == -1L) {
|
||||
/* no more entries => close directory */
|
||||
_findclose (d->hFile);
|
||||
d->closed = 1;
|
||||
return 0;
|
||||
} else {
|
||||
lua_pushstring (L, c_file.name);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if ((entry = readdir (d->dir)) != NULL) {
|
||||
lua_pushstring (L, entry->d_name);
|
||||
return 1;
|
||||
} else {
|
||||
/* no more entries => close directory */
|
||||
closedir (d->dir);
|
||||
d->closed = 1;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Closes directory iterators
|
||||
*/
|
||||
static int dir_close (lua_State *L) {
|
||||
dir_data *d = (dir_data *)lua_touserdata (L, 1);
|
||||
#ifdef _WIN32
|
||||
if (!d->closed && d->hFile) {
|
||||
_findclose (d->hFile);
|
||||
}
|
||||
#else
|
||||
if (!d->closed && d->dir) {
|
||||
closedir (d->dir);
|
||||
}
|
||||
#endif
|
||||
d->closed = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Factory of directory iterators
|
||||
*/
|
||||
static int dir_iter_factory (lua_State *L) {
|
||||
const char *path = luaL_checkstring (L, 1);
|
||||
dir_data *d;
|
||||
lua_pushcfunction (L, dir_iter);
|
||||
d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
|
||||
luaL_getmetatable (L, DIR_METATABLE);
|
||||
lua_setmetatable (L, -2);
|
||||
d->closed = 0;
|
||||
#ifdef _WIN32
|
||||
d->hFile = 0L;
|
||||
if (strlen(path) > MAX_PATH-2)
|
||||
luaL_error (L, "path too long: %s", path);
|
||||
else
|
||||
sprintf (d->pattern, "%s/*", path);
|
||||
#else
|
||||
d->dir = opendir (path);
|
||||
if (d->dir == NULL)
|
||||
luaL_error (L, "cannot open %s: %s", path, strerror (errno));
|
||||
#endif
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Creates directory metatable.
|
||||
*/
|
||||
static int dir_create_meta (lua_State *L) {
|
||||
luaL_newmetatable (L, DIR_METATABLE);
|
||||
|
||||
/* Method table */
|
||||
lua_newtable(L);
|
||||
lua_pushcfunction (L, dir_iter);
|
||||
lua_setfield(L, -2, "next");
|
||||
lua_pushcfunction (L, dir_close);
|
||||
lua_setfield(L, -2, "close");
|
||||
|
||||
/* Metamethods */
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_pushcfunction (L, dir_close);
|
||||
lua_setfield (L, -2, "__gc");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
** Creates lock metatable.
|
||||
*/
|
||||
static int lock_create_meta (lua_State *L) {
|
||||
luaL_newmetatable (L, LOCK_METATABLE);
|
||||
|
||||
/* Method table */
|
||||
lua_newtable(L);
|
||||
lua_pushcfunction(L, lfs_unlock_dir);
|
||||
lua_setfield(L, -2, "free");
|
||||
|
||||
/* Metamethods */
|
||||
lua_setfield(L, -2, "__index");
|
||||
lua_pushcfunction(L, lfs_unlock_dir);
|
||||
lua_setfield(L, -2, "__gc");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef S_ISDIR
|
||||
#define S_ISDIR(mode) (mode&_S_IFDIR)
|
||||
#endif
|
||||
#ifndef S_ISREG
|
||||
#define S_ISREG(mode) (mode&_S_IFREG)
|
||||
#endif
|
||||
#ifndef S_ISLNK
|
||||
#define S_ISLNK(mode) (0)
|
||||
#endif
|
||||
#ifndef S_ISSOCK
|
||||
#define S_ISSOCK(mode) (0)
|
||||
#endif
|
||||
#ifndef S_ISFIFO
|
||||
#define S_ISFIFO(mode) (0)
|
||||
#endif
|
||||
#ifndef S_ISCHR
|
||||
#define S_ISCHR(mode) (mode&_S_IFCHR)
|
||||
#endif
|
||||
#ifndef S_ISBLK
|
||||
#define S_ISBLK(mode) (0)
|
||||
#endif
|
||||
#endif
|
||||
/*
|
||||
** Convert the inode protection mode to a string.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
static const char *mode2string (unsigned short mode) {
|
||||
#else
|
||||
static const char *mode2string (mode_t mode) {
|
||||
#endif
|
||||
if ( S_ISREG(mode) )
|
||||
return "file";
|
||||
else if ( S_ISDIR(mode) )
|
||||
return "directory";
|
||||
else if ( S_ISLNK(mode) )
|
||||
return "link";
|
||||
else if ( S_ISSOCK(mode) )
|
||||
return "socket";
|
||||
else if ( S_ISFIFO(mode) )
|
||||
return "named pipe";
|
||||
else if ( S_ISCHR(mode) )
|
||||
return "char device";
|
||||
else if ( S_ISBLK(mode) )
|
||||
return "block device";
|
||||
else
|
||||
return "other";
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Set access time and modification values for file
|
||||
*/
|
||||
static int file_utime (lua_State *L) {
|
||||
const char *file = luaL_checkstring (L, 1);
|
||||
struct utimbuf utb, *buf;
|
||||
|
||||
if (lua_gettop (L) == 1) /* set to current date/time */
|
||||
buf = NULL;
|
||||
else {
|
||||
utb.actime = (time_t)luaL_optnumber (L, 2, 0);
|
||||
utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime);
|
||||
buf = &utb;
|
||||
}
|
||||
if (utime (file, buf)) {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L, "%s", strerror (errno));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean (L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* inode protection mode */
|
||||
static void push_st_mode (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushstring (L, mode2string (info->st_mode));
|
||||
}
|
||||
/* device inode resides on */
|
||||
static void push_st_dev (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_dev);
|
||||
}
|
||||
/* inode's number */
|
||||
static void push_st_ino (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_ino);
|
||||
}
|
||||
/* number of hard links to the file */
|
||||
static void push_st_nlink (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_nlink);
|
||||
}
|
||||
/* user-id of owner */
|
||||
static void push_st_uid (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_uid);
|
||||
}
|
||||
/* group-id of owner */
|
||||
static void push_st_gid (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_gid);
|
||||
}
|
||||
/* device type, for special file inode */
|
||||
static void push_st_rdev (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_rdev);
|
||||
}
|
||||
/* time of last access */
|
||||
static void push_st_atime (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, info->st_atime);
|
||||
}
|
||||
/* time of last data modification */
|
||||
static void push_st_mtime (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, info->st_mtime);
|
||||
}
|
||||
/* time of last file status change */
|
||||
static void push_st_ctime (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, info->st_ctime);
|
||||
}
|
||||
/* file size, in bytes */
|
||||
static void push_st_size (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_size);
|
||||
}
|
||||
#ifndef _WIN32
|
||||
/* blocks allocated for file */
|
||||
static void push_st_blocks (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_blocks);
|
||||
}
|
||||
/* optimal file system I/O blocksize */
|
||||
static void push_st_blksize (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushnumber (L, (lua_Number)info->st_blksize);
|
||||
}
|
||||
#endif
|
||||
static void push_invalid (lua_State *L, STAT_STRUCT *info) {
|
||||
luaL_error(L, "invalid attribute name");
|
||||
#ifndef _WIN32
|
||||
info->st_blksize = 0; /* never reached */
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
** Convert the inode protection mode to a permission list.
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
static const char *perm2string (unsigned short mode) {
|
||||
static char perms[] = "---------\0";
|
||||
int i;
|
||||
for (i=0;i<9;i++) perms[i]='-';
|
||||
if (mode & _S_IREAD)
|
||||
{ perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; }
|
||||
if (mode & _S_IWRITE)
|
||||
{ perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; }
|
||||
if (mode & _S_IEXEC)
|
||||
{ perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; }
|
||||
return perms;
|
||||
}
|
||||
#else
|
||||
static const char *perm2string (mode_t mode) {
|
||||
static char perms[] = "---------\0";
|
||||
int i;
|
||||
for (i=0;i<9;i++) perms[i]='-';
|
||||
if (mode & S_IRUSR) perms[0] = 'r';
|
||||
if (mode & S_IWUSR) perms[1] = 'w';
|
||||
if (mode & S_IXUSR) perms[2] = 'x';
|
||||
if (mode & S_IRGRP) perms[3] = 'r';
|
||||
if (mode & S_IWGRP) perms[4] = 'w';
|
||||
if (mode & S_IXGRP) perms[5] = 'x';
|
||||
if (mode & S_IROTH) perms[6] = 'r';
|
||||
if (mode & S_IWOTH) perms[7] = 'w';
|
||||
if (mode & S_IXOTH) perms[8] = 'x';
|
||||
return perms;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* permssions string */
|
||||
static void push_st_perm (lua_State *L, STAT_STRUCT *info) {
|
||||
lua_pushstring (L, perm2string (info->st_mode));
|
||||
}
|
||||
|
||||
typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info);
|
||||
|
||||
struct _stat_members {
|
||||
const char *name;
|
||||
_push_function push;
|
||||
};
|
||||
|
||||
struct _stat_members members[] = {
|
||||
{ "mode", push_st_mode },
|
||||
{ "dev", push_st_dev },
|
||||
{ "ino", push_st_ino },
|
||||
{ "nlink", push_st_nlink },
|
||||
{ "uid", push_st_uid },
|
||||
{ "gid", push_st_gid },
|
||||
{ "rdev", push_st_rdev },
|
||||
{ "access", push_st_atime },
|
||||
{ "modification", push_st_mtime },
|
||||
{ "change", push_st_ctime },
|
||||
{ "size", push_st_size },
|
||||
{ "permissions", push_st_perm },
|
||||
#ifndef _WIN32
|
||||
{ "blocks", push_st_blocks },
|
||||
{ "blksize", push_st_blksize },
|
||||
#endif
|
||||
{ NULL, push_invalid }
|
||||
};
|
||||
|
||||
/*
|
||||
** Get file or symbolic link information
|
||||
*/
|
||||
static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
|
||||
int i;
|
||||
STAT_STRUCT info;
|
||||
const char *file = luaL_checkstring (L, 1);
|
||||
|
||||
if (st(file, &info)) {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L, "cannot obtain information from file `%s'", file);
|
||||
return 2;
|
||||
}
|
||||
if (lua_isstring (L, 2)) {
|
||||
int v;
|
||||
const char *member = lua_tostring (L, 2);
|
||||
if (strcmp (member, "mode") == 0) v = 0;
|
||||
#ifndef _WIN32
|
||||
else if (strcmp (member, "blocks") == 0) v = 11;
|
||||
else if (strcmp (member, "blksize") == 0) v = 12;
|
||||
#endif
|
||||
else /* look for member */
|
||||
for (v = 1; members[v].name; v++)
|
||||
if (*members[v].name == *member)
|
||||
break;
|
||||
/* push member value and return */
|
||||
members[v].push (L, &info);
|
||||
return 1;
|
||||
} else if (!lua_istable (L, 2))
|
||||
/* creates a table if none is given */
|
||||
lua_newtable (L);
|
||||
/* stores all members in table on top of the stack */
|
||||
for (i = 0; members[i].name; i++) {
|
||||
lua_pushstring (L, members[i].name);
|
||||
members[i].push (L, &info);
|
||||
lua_rawset (L, -3);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get file information using stat.
|
||||
*/
|
||||
static int file_info (lua_State *L) {
|
||||
return _file_info_ (L, STAT_FUNC);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get symbolic link information using lstat.
|
||||
*/
|
||||
static int link_info (lua_State *L) {
|
||||
return _file_info_ (L, LSTAT_FUNC);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Assumes the table is on top of the stack.
|
||||
*/
|
||||
static void set_info (lua_State *L) {
|
||||
lua_pushliteral (L, "_COPYRIGHT");
|
||||
lua_pushliteral (L, "Copyright (C) 2003-2012 Kepler Project");
|
||||
lua_settable (L, -3);
|
||||
lua_pushliteral (L, "_DESCRIPTION");
|
||||
lua_pushliteral (L, "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution");
|
||||
lua_settable (L, -3);
|
||||
lua_pushliteral (L, "_VERSION");
|
||||
lua_pushliteral (L, "LuaFileSystem " LFS_VERSION);
|
||||
lua_settable (L, -3);
|
||||
}
|
||||
|
||||
|
||||
static const struct luaL_Reg fslib[] = {
|
||||
{"attributes", file_info},
|
||||
{"chdir", change_dir},
|
||||
{"currentdir", get_dir},
|
||||
{"dir", dir_iter_factory},
|
||||
{"link", make_link},
|
||||
{"lock", file_lock},
|
||||
{"mkdir", make_dir},
|
||||
{"rmdir", remove_dir},
|
||||
{"symlinkattributes", link_info},
|
||||
{"setmode", lfs_f_setmode},
|
||||
{"touch", file_utime},
|
||||
{"unlock", file_unlock},
|
||||
{"lock_dir", lfs_lock_dir},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
int luaopen_lfs (lua_State *L) {
|
||||
dir_create_meta (L);
|
||||
lock_create_meta (L);
|
||||
luaL_newlib (L, fslib);
|
||||
lua_pushvalue(L, -1);
|
||||
lua_setglobal(L, LFS_LIBNAME);
|
||||
set_info (L);
|
||||
return 1;
|
||||
}
|
|
@ -17,8 +17,11 @@
|
|||
#include "libaegisub/lua/script_reader.h"
|
||||
|
||||
#include "libaegisub/file_mapping.h"
|
||||
#include "libaegisub/log.h"
|
||||
#include "libaegisub/lua/utils.h"
|
||||
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <lauxlib.h>
|
||||
|
||||
namespace agi { namespace lua {
|
||||
|
@ -68,4 +71,75 @@ namespace agi { namespace lua {
|
|||
lua_pop(L, 1); // Remove the extra nil for the stackchecker
|
||||
return true;
|
||||
}
|
||||
|
||||
static int module_loader(lua_State *L) {
|
||||
int pretop = lua_gettop(L);
|
||||
std::string module(luaL_checkstring(L, -1));
|
||||
boost::replace_all(module, ".", LUA_DIRSEP);
|
||||
|
||||
// Get the lua package include path (which the user may have modified)
|
||||
lua_getglobal(L, "package");
|
||||
lua_getfield(L, -1, "path");
|
||||
std::string package_paths(luaL_checkstring(L, -1));
|
||||
lua_pop(L, 2);
|
||||
|
||||
boost::char_separator<char> sep(";");
|
||||
for (auto filename : boost::tokenizer<boost::char_separator<char>>(package_paths, sep)) {
|
||||
boost::replace_all(filename, "?", module);
|
||||
|
||||
// If there's a .moon file at that path, load it instead of the
|
||||
// .lua file
|
||||
agi::fs::path path = filename;
|
||||
if (agi::fs::HasExtension(path, "lua")) {
|
||||
agi::fs::path moonpath = path;
|
||||
moonpath.replace_extension("moon");
|
||||
if (agi::fs::FileExists(moonpath))
|
||||
path = moonpath;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!LoadFile(L, path))
|
||||
return luaL_error(L, "Error loading Lua module \"%s\":\n%s", path.string().c_str(), luaL_checkstring(L, 1));
|
||||
break;
|
||||
}
|
||||
catch (agi::fs::FileNotFound const&) {
|
||||
// Not an error so swallow and continue on
|
||||
}
|
||||
catch (agi::fs::NotAFile const&) {
|
||||
// Not an error so swallow and continue on
|
||||
}
|
||||
catch (agi::Exception const& e) {
|
||||
return luaL_error(L, "Error loading Lua module \"%s\":\n%s", path.string().c_str(), e.GetChainedMessage().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
return lua_gettop(L) - pretop;
|
||||
}
|
||||
|
||||
void Install(lua_State *L, std::vector<fs::path> const& include_path) {
|
||||
// set the module load path to include_path
|
||||
lua_getglobal(L, "package");
|
||||
push_value(L, "path");
|
||||
#ifdef _WIN32
|
||||
// No point in checking any of the default locations on Windows since
|
||||
// there won't be anything there
|
||||
push_value(L, "");
|
||||
#else
|
||||
push_value(L, "path");
|
||||
lua_gettable(L, -3);
|
||||
#endif
|
||||
|
||||
for (auto const& path : include_path) {
|
||||
lua_pushfstring(L, ";%s/?.lua;%s/?/init.lua", path.string().c_str(), path.string().c_str());
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Replace the default lua module loader with our unicode compatible one
|
||||
lua_getfield(L, -1, "loaders");
|
||||
push_value(L, module_loader);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
} }
|
||||
|
|
178
libaegisub/lua/utils.cpp
Normal file
178
libaegisub/lua/utils.cpp
Normal file
|
@ -0,0 +1,178 @@
|
|||
// 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/utils.h"
|
||||
|
||||
#include "libaegisub/log.h"
|
||||
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
namespace agi { namespace lua {
|
||||
std::string get_string_or_default(lua_State *L, int idx) {
|
||||
size_t len = 0;
|
||||
const char *str = lua_tolstring(L, idx, &len);
|
||||
if (!str)
|
||||
return "<not a string>";
|
||||
return std::string(str, len);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
std::string get_global_string(lua_State *L, const char *name) {
|
||||
lua_getglobal(L, name);
|
||||
std::string ret;
|
||||
if (lua_isstring(L, -1))
|
||||
ret = lua_tostring(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int moon_line(lua_State *L, int lua_line, std::string const& file) {
|
||||
if (luaL_dostring(L, "return require 'moonscript.line_tables'")) {
|
||||
lua_pop(L, 1); // pop error message
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
push_value(L, file);
|
||||
lua_rawget(L, -2);
|
||||
|
||||
if (!lua_istable(L, -1)) {
|
||||
lua_pop(L, 2);
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
lua_rawgeti(L, -1, lua_line);
|
||||
if (!lua_isnumber(L, -1)) {
|
||||
lua_pop(L, 3);
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
auto char_pos = static_cast<size_t>(lua_tonumber(L, -1));
|
||||
lua_pop(L, 3);
|
||||
|
||||
// The moonscript line tables give us a character offset into the file,
|
||||
// so now we need to map that to a line number
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, ("raw moonscript: " + file).c_str());
|
||||
if (!lua_isstring(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
size_t moon_len;
|
||||
auto moon = lua_tolstring(L, -1, &moon_len);
|
||||
return std::count(moon, moon + std::min(moon_len, char_pos), '\n') + 1;
|
||||
}
|
||||
|
||||
int add_stack_trace(lua_State *L) {
|
||||
int level = 1;
|
||||
if (lua_isnumber(L, 2)) {
|
||||
level = (int)lua_tointeger(L, 2);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
const char *err = lua_tostring(L, 1);
|
||||
if (!err) return 1;
|
||||
|
||||
std::string message = err;
|
||||
if (lua_gettop(L))
|
||||
lua_pop(L, 1);
|
||||
|
||||
// Strip the location from the error message since it's redundant with
|
||||
// the stack trace
|
||||
boost::regex location(R"(^\[string ".*"\]:[0-9]+: )");
|
||||
message = regex_replace(message, location, "", boost::format_first_only);
|
||||
|
||||
std::vector<std::string> frames;
|
||||
frames.emplace_back(std::move(message));
|
||||
|
||||
lua_Debug ar;
|
||||
while (lua_getstack(L, level++, &ar)) {
|
||||
lua_getinfo(L, "Snl", &ar);
|
||||
|
||||
if (ar.what[0] == 't')
|
||||
frames.emplace_back("(tail call)");
|
||||
else {
|
||||
bool is_moon = false;
|
||||
std::string file = ar.source;
|
||||
if (file == "=[C]")
|
||||
file = "<C function>";
|
||||
else if (boost::ends_with(file, ".moon"))
|
||||
is_moon = true;
|
||||
|
||||
auto real_line = [&](int line) {
|
||||
return is_moon ? moon_line(L, line, file) : line;
|
||||
};
|
||||
|
||||
std::string function = ar.name ? ar.name : "";
|
||||
if (*ar.what == 'm')
|
||||
function = "<main>";
|
||||
else if (*ar.what == 'C')
|
||||
function = '?';
|
||||
else if (!*ar.namewhat)
|
||||
function = str(boost::format("<anonymous function at lines %d-%d>") % real_line(ar.linedefined) % real_line(ar.lastlinedefined - 1));
|
||||
|
||||
frames.emplace_back(str(boost::format(" File \"%s\", line %d\n%s") % file % real_line(ar.currentline) % function));
|
||||
}
|
||||
}
|
||||
|
||||
push_value(L, join(frames | boost::adaptors::reversed, "\n"));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#ifdef _DEBUG
|
||||
void LuaStackcheck::check_stack(int additional) {
|
||||
int top = lua_gettop(L);
|
||||
if (top - additional != startstack) {
|
||||
LOG_D("automation/lua") << "lua stack size mismatch.";
|
||||
dump();
|
||||
assert(top - additional == startstack);
|
||||
}
|
||||
}
|
||||
|
||||
void LuaStackcheck::dump() {
|
||||
int top = lua_gettop(L);
|
||||
LOG_D("automation/lua/stackdump") << "--- dumping lua stack...";
|
||||
for (int i = top; i > 0; i--) {
|
||||
lua_pushvalue(L, i);
|
||||
std::string type(lua_typename(L, lua_type(L, -1)));
|
||||
if (lua_isstring(L, i))
|
||||
LOG_D("automation/lua/stackdump") << type << ": " << lua_tostring(L, -1);
|
||||
else
|
||||
LOG_D("automation/lua/stackdump") << type;
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
LOG_D("automation/lua") << "--- end dump";
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
|
@ -60,16 +60,9 @@
|
|||
#include <algorithm>
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <boost/range/adaptor/reversed.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <lualib.h>
|
||||
#include <lauxlib.h>
|
||||
#include <mutex>
|
||||
#include <wx/clipbrd.h>
|
||||
|
@ -83,7 +76,6 @@ using namespace agi::lua;
|
|||
using namespace Automation4;
|
||||
|
||||
namespace {
|
||||
|
||||
wxString get_wxstring(lua_State *L, int idx)
|
||||
{
|
||||
return wxString::FromUTF8(lua_tostring(L, idx));
|
||||
|
@ -322,7 +314,7 @@ namespace {
|
|||
}
|
||||
|
||||
class LuaFeature {
|
||||
int myid;
|
||||
int myid = 0;
|
||||
protected:
|
||||
lua_State *L;
|
||||
|
||||
|
@ -331,7 +323,7 @@ namespace {
|
|||
|
||||
void GetFeatureFunction(const char *function) const;
|
||||
|
||||
LuaFeature(lua_State *L);
|
||||
LuaFeature(lua_State *L) : L(L) { }
|
||||
};
|
||||
|
||||
/// Run a lua function on a background thread
|
||||
|
@ -382,7 +374,7 @@ namespace {
|
|||
void ProcessSubs(AssFile *subs, wxWindow *export_dialog) override;
|
||||
};
|
||||
class LuaScript final : public Script {
|
||||
lua_State *L;
|
||||
lua_State *L = nullptr;
|
||||
|
||||
std::string name;
|
||||
std::string description;
|
||||
|
@ -398,7 +390,6 @@ namespace {
|
|||
void Destroy();
|
||||
|
||||
static int LuaInclude(lua_State *L);
|
||||
static int LuaModuleLoader(lua_State *L);
|
||||
|
||||
public:
|
||||
LuaScript(agi::fs::path const& filename);
|
||||
|
@ -425,7 +416,6 @@ namespace {
|
|||
|
||||
LuaScript::LuaScript(agi::fs::path const& filename)
|
||||
: Script(filename)
|
||||
, L(nullptr)
|
||||
{
|
||||
Create();
|
||||
}
|
||||
|
@ -440,14 +430,7 @@ namespace {
|
|||
LuaStackcheck stackcheck(L);
|
||||
|
||||
// register standard libs
|
||||
push_value(L, luaopen_base); lua_call(L, 0, 0);
|
||||
push_value(L, luaopen_io); 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_package); 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);
|
||||
agi::lua::preload_modules(L);
|
||||
preload_modules(L);
|
||||
stackcheck.check_stack(0);
|
||||
|
||||
// dofile and loadfile are replaced with include
|
||||
|
@ -464,28 +447,9 @@ namespace {
|
|||
push_value(L, &pairs<table_ipairs>);
|
||||
lua_setglobal(L, "ipairs");
|
||||
|
||||
// set the module load path to include_path
|
||||
lua_getglobal(L, "package");
|
||||
push_value(L, "path");
|
||||
#ifdef __WXMSW__
|
||||
push_value(L, "");
|
||||
#else
|
||||
push_value(L, "path");
|
||||
lua_gettable(L, -3);
|
||||
#endif
|
||||
|
||||
for (auto const& path : include_path) {
|
||||
lua_pushfstring(L, ";%s/?.lua;%s/?/init.lua", path.string().c_str(), path.string().c_str());
|
||||
lua_concat(L, 2);
|
||||
}
|
||||
|
||||
lua_settable(L, -3);
|
||||
|
||||
// Replace the default lua module loader with our unicode compatible one
|
||||
lua_getfield(L, -1, "loaders");
|
||||
push_value(L, LuaModuleLoader);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pop(L, 2);
|
||||
// Replace the default lua module loader with our unicode compatible
|
||||
// one and set the module search path
|
||||
Install(L, include_path);
|
||||
stackcheck.check_stack(0);
|
||||
|
||||
// prepare stuff in the registry
|
||||
|
@ -522,10 +486,6 @@ namespace {
|
|||
lua_settable(L, LUA_GLOBALSINDEX);
|
||||
stackcheck.check_stack(0);
|
||||
|
||||
// Preload packaged binary modules
|
||||
preload_modules(L);
|
||||
stackcheck.check_stack(0);
|
||||
|
||||
// load user script
|
||||
if (!LoadFile(L, GetFilename())) {
|
||||
std::string err = get_string_or_default(L, 1);
|
||||
|
@ -625,53 +585,6 @@ namespace {
|
|||
return (LuaScript*)ptr;
|
||||
}
|
||||
|
||||
/// @brief Module loader which uses our include rather than Lua's, for unicode file support
|
||||
/// @param L The Lua state
|
||||
/// @return Always 1 per loader_Lua?
|
||||
int LuaScript::LuaModuleLoader(lua_State *L)
|
||||
{
|
||||
int pretop = lua_gettop(L);
|
||||
std::string module(luaL_checkstring(L, -1));
|
||||
boost::replace_all(module, ".", LUA_DIRSEP);
|
||||
|
||||
// Get the lua package include path (which the user may have modified)
|
||||
lua_getglobal(L, "package");
|
||||
lua_getfield(L, -1, "path");
|
||||
std::string package_paths(luaL_checkstring(L, -1));
|
||||
lua_pop(L, 2);
|
||||
|
||||
boost::char_separator<char> sep(";");
|
||||
for (auto filename : boost::tokenizer<boost::char_separator<char>>(package_paths, sep)) {
|
||||
boost::replace_all(filename, "?", module);
|
||||
|
||||
// If there's a .moon file at that path, load it instead of the
|
||||
// .lua file
|
||||
agi::fs::path path = filename;
|
||||
if (agi::fs::HasExtension(path, "lua")) {
|
||||
agi::fs::path moonpath = path;
|
||||
moonpath.replace_extension("moon");
|
||||
if (agi::fs::FileExists(moonpath))
|
||||
path = moonpath;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!LoadFile(L, path))
|
||||
return luaL_error(L, "Error loading Lua module \"%s\":\n%s", path.string().c_str(), luaL_checkstring(L, 1));
|
||||
break;
|
||||
}
|
||||
catch (agi::fs::FileNotFound const&) {
|
||||
// Not an error so swallow and continue on
|
||||
}
|
||||
catch (agi::fs::NotAFile const&) {
|
||||
// Not an error so swallow and continue on
|
||||
}
|
||||
catch (agi::Exception const& e) {
|
||||
return luaL_error(L, "Error loading Lua module \"%s\":\n%s", path.string().c_str(), e.GetChainedMessage().c_str());
|
||||
}
|
||||
}
|
||||
|
||||
return lua_gettop(L) - pretop;
|
||||
}
|
||||
|
||||
int LuaScript::LuaInclude(lua_State *L)
|
||||
{
|
||||
|
@ -702,101 +615,6 @@ namespace {
|
|||
return lua_gettop(L) - pretop;
|
||||
}
|
||||
|
||||
static int moon_line(lua_State *L, int lua_line, std::string const& file)
|
||||
{
|
||||
if (luaL_dostring(L, "return require 'moonscript.line_tables'")) {
|
||||
lua_pop(L, 1); // pop error message
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
push_value(L, file);
|
||||
lua_rawget(L, -2);
|
||||
|
||||
if (!lua_istable(L, -1)) {
|
||||
lua_pop(L, 2);
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
lua_rawgeti(L, -1, lua_line);
|
||||
if (!lua_isnumber(L, -1)) {
|
||||
lua_pop(L, 3);
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
auto char_pos = static_cast<size_t>(lua_tonumber(L, -1));
|
||||
lua_pop(L, 3);
|
||||
|
||||
// The moonscript line tables give us a character offset into the file,
|
||||
// so now we need to map that to a line number
|
||||
lua_getfield(L, LUA_REGISTRYINDEX, ("raw moonscript: " + file).c_str());
|
||||
if (!lua_isstring(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
return lua_line;
|
||||
}
|
||||
|
||||
size_t moon_len;
|
||||
auto moon = lua_tolstring(L, -1, &moon_len);
|
||||
return std::count(moon, moon + std::min(moon_len, char_pos), '\n') + 1;
|
||||
}
|
||||
|
||||
static int add_stack_trace(lua_State *L)
|
||||
{
|
||||
int level = 1;
|
||||
if (lua_isnumber(L, 2)) {
|
||||
level = (int)lua_tointeger(L, 2);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
||||
const char *err = lua_tostring(L, 1);
|
||||
if (!err) return 1;
|
||||
|
||||
std::string message = err;
|
||||
if (lua_gettop(L))
|
||||
lua_pop(L, 1);
|
||||
|
||||
// Strip the location from the error message since it's redundant with
|
||||
// the stack trace
|
||||
boost::regex location(R"(^\[string ".*"\]:[0-9]+: )");
|
||||
message = regex_replace(message, location, "", boost::format_first_only);
|
||||
|
||||
std::vector<std::string> frames;
|
||||
frames.emplace_back(std::move(message));
|
||||
|
||||
lua_Debug ar;
|
||||
while (lua_getstack(L, level++, &ar)) {
|
||||
lua_getinfo(L, "Snl", &ar);
|
||||
|
||||
if (ar.what[0] == 't')
|
||||
frames.emplace_back("(tail call)");
|
||||
else {
|
||||
bool is_moon = false;
|
||||
std::string file = ar.source;
|
||||
if (file == "=[C]")
|
||||
file = "<C function>";
|
||||
else if (boost::ends_with(file, ".moon"))
|
||||
is_moon = true;
|
||||
|
||||
auto real_line = [&](int line) {
|
||||
return is_moon ? moon_line(L, line, file) : line;
|
||||
};
|
||||
|
||||
std::string function = ar.name ? ar.name : "";
|
||||
if (*ar.what == 'm')
|
||||
function = "<main>";
|
||||
else if (*ar.what == 'C')
|
||||
function = '?';
|
||||
else if (!*ar.namewhat)
|
||||
function = str(boost::format("<anonymous function at lines %d-%d>") % real_line(ar.linedefined) % real_line(ar.lastlinedefined - 1));
|
||||
|
||||
frames.emplace_back(str(boost::format(" File \"%s\", line %d\n%s") % file % real_line(ar.currentline) % function));
|
||||
}
|
||||
}
|
||||
|
||||
push_value(L, join(frames | boost::adaptors::reversed, "\n"));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void LuaThreadedCall(lua_State *L, int nargs, int nresults, std::string const& title, wxWindow *parent, bool can_open_config)
|
||||
{
|
||||
bool failed = false;
|
||||
|
@ -827,12 +645,6 @@ namespace {
|
|||
}
|
||||
|
||||
// LuaFeature
|
||||
LuaFeature::LuaFeature(lua_State *L)
|
||||
: myid(0)
|
||||
, L(L)
|
||||
{
|
||||
}
|
||||
|
||||
void LuaFeature::RegisterFeature()
|
||||
{
|
||||
myid = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||
|
|
|
@ -42,8 +42,9 @@
|
|||
#include "utils.h"
|
||||
|
||||
#include <libaegisub/exception.h>
|
||||
#include <libaegisub/make_unique.h>
|
||||
#include <libaegisub/log.h>
|
||||
#include <libaegisub/lua/utils.h>
|
||||
#include <libaegisub/make_unique.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
|
|
@ -41,8 +41,9 @@
|
|||
#include "utils.h"
|
||||
#include "validators.h"
|
||||
|
||||
#include <libaegisub/make_unique.h>
|
||||
#include <libaegisub/log.h>
|
||||
#include <libaegisub/lua/utils.h>
|
||||
#include <libaegisub/make_unique.h>
|
||||
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
#include <boost/range/adaptors.hpp>
|
||||
|
|
4
vendor/lua/Makefile
vendored
4
vendor/lua/Makefile
vendored
|
@ -2,7 +2,7 @@ include ../../Makefile.inc
|
|||
|
||||
LIB = liblua-aegisub.a
|
||||
|
||||
CXXFLAGS += -Wno-empty-body -Wno-deprecated-declarations
|
||||
CXXFLAGS += -Wno-empty-body -Wno-deprecated-declarations -DLUA_USE_DLOPEN -DLUA_USE_POSIX
|
||||
|
||||
SRC = \
|
||||
src/lapi.cpp \
|
||||
|
@ -31,8 +31,6 @@ SRC = \
|
|||
src/ltable.cpp \
|
||||
src/ltablib.cpp \
|
||||
src/ltm.cpp \
|
||||
src/lua.cpp \
|
||||
src/luac.cpp \
|
||||
src/lundump.cpp \
|
||||
src/lvm.cpp \
|
||||
src/lzio.cpp \
|
||||
|
|
Loading…
Reference in a new issue