diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2017-09-16 17:19:35 +0200 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2017-09-16 17:19:35 +0200 |
commit | 09b201c87c904066e1de5d44cf1e02538970c168 (patch) | |
tree | e56be07e08699f920482212f07342acf8f9526ae | |
parent | 3b52d81dcf1b5d6d49e1837612e207b2cab6b74d (diff) | |
download | lua-compat-5.3-09b201c87c904066e1de5d44cf1e02538970c168.tar.gz lua-compat-5.3-09b201c87c904066e1de5d44cf1e02538970c168.tar.bz2 lua-compat-5.3-09b201c87c904066e1de5d44cf1e02538970c168.zip |
Add tests for `luaL_load{buffer,file}x`.
Fix bug regarding empty input in `lua_load`.
Adapt error message and error code.
-rw-r--r-- | c-api/compat-5.3.c | 19 | ||||
-rwxr-xr-x | tests/test.lua | 157 | ||||
-rw-r--r-- | tests/testmod.c | 29 |
3 files changed, 129 insertions, 76 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index 4bd984a..e87808c 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c | |||
@@ -447,7 +447,7 @@ COMPAT53_API int luaL_fileresult (lua_State *L, int stat, const char *fname) { | |||
447 | 447 | ||
448 | static int compat53_checkmode (lua_State *L, const char *mode, const char *modename, int err) { | 448 | static int compat53_checkmode (lua_State *L, const char *mode, const char *modename, int err) { |
449 | if (mode && strchr(mode, modename[0]) == NULL) { | 449 | if (mode && strchr(mode, modename[0]) == NULL) { |
450 | lua_pushfstring(L, "attempt to load a %s chunk when 'mode' is '%s'", modename, mode); | 450 | lua_pushfstring(L, "attempt to load a %s chunk (mode is '%s')", modename, mode); |
451 | return err; | 451 | return err; |
452 | } | 452 | } |
453 | return LUA_OK; | 453 | return LUA_OK; |
@@ -475,17 +475,16 @@ static const char *compat53_reader (lua_State *L, void *ud, size_t *size) { | |||
475 | 475 | ||
476 | 476 | ||
477 | COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) { | 477 | COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) { |
478 | int status = LUA_OK; | ||
478 | compat53_reader_data compat53_data = { reader, data, 1, 0, 0 }; | 479 | compat53_reader_data compat53_data = { reader, data, 1, 0, 0 }; |
479 | compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size)); | 480 | compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size)); |
480 | if (compat53_data.peeked_data && compat53_data.peeked_data_size) { | 481 | if (compat53_data.peeked_data && compat53_data.peeked_data_size && |
481 | int status = LUA_OK; | 482 | compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */ |
482 | if (compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */ | 483 | status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX); |
483 | status = compat53_checkmode(L, mode, "binary", LUA_ERRFILE); | 484 | else |
484 | else | 485 | status = compat53_checkmode(L, mode, "text", LUA_ERRSYNTAX); |
485 | status = compat53_checkmode(L, mode, "text", LUA_ERRFILE); | 486 | if (status != LUA_OK) |
486 | if (status != LUA_OK) | 487 | return status; |
487 | return status; | ||
488 | } | ||
489 | /* we need to call the original 5.1 version of lua_load! */ | 488 | /* we need to call the original 5.1 version of lua_load! */ |
490 | #undef lua_load | 489 | #undef lua_load |
491 | return lua_load(L, compat53_reader, &compat53_data, source); | 490 | return lua_load(L, compat53_reader, &compat53_data, source); |
diff --git a/tests/test.lua b/tests/test.lua index 582f55e..8fafdf3 100755 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -657,133 +657,158 @@ ___'' | |||
657 | print("testing C API ...") | 657 | print("testing C API ...") |
658 | local mod = require("testmod") | 658 | local mod = require("testmod") |
659 | ___'' | 659 | ___'' |
660 | print(mod.isinteger(1)) | 660 | print("isinteger", mod.isinteger(1)) |
661 | print(mod.isinteger(0)) | 661 | print("isinteger", mod.isinteger(0)) |
662 | print(mod.isinteger(1234567)) | 662 | print("isinteger", mod.isinteger(1234567)) |
663 | print(mod.isinteger(12.3)) | 663 | print("isinteger", mod.isinteger(12.3)) |
664 | print(mod.isinteger(math.huge)) | 664 | print("isinteger", mod.isinteger(math.huge)) |
665 | print(mod.isinteger(math.sqrt(-1))) | 665 | print("isinteger", mod.isinteger(math.sqrt(-1))) |
666 | 666 | ||
667 | 667 | ||
668 | ___'' | 668 | ___'' |
669 | print(mod.rotate(1, 1, 2, 3, 4, 5, 6)) | 669 | print("rotate", mod.rotate(1, 1, 2, 3, 4, 5, 6)) |
670 | print(mod.rotate(-1, 1, 2, 3, 4, 5, 6)) | 670 | print("rotate", mod.rotate(-1, 1, 2, 3, 4, 5, 6)) |
671 | print(mod.rotate(4, 1, 2, 3, 4, 5, 6)) | 671 | print("rotate", mod.rotate(4, 1, 2, 3, 4, 5, 6)) |
672 | print(mod.rotate(-4, 1, 2, 3, 4, 5, 6)) | 672 | print("rotate", mod.rotate(-4, 1, 2, 3, 4, 5, 6)) |
673 | 673 | ||
674 | 674 | ||
675 | ___'' | 675 | ___'' |
676 | print(mod.strtonum("+123")) | 676 | print("strtonum", mod.strtonum("+123")) |
677 | print(mod.strtonum(" 123 ")) | 677 | print("strtonum", mod.strtonum(" 123 ")) |
678 | print(mod.strtonum("-1.23")) | 678 | print("strtonum", mod.strtonum("-1.23")) |
679 | print(mod.strtonum(" 123 abc")) | 679 | print("strtonum", mod.strtonum(" 123 abc")) |
680 | print(mod.strtonum("jkl")) | 680 | print("strtonum", mod.strtonum("jkl")) |
681 | 681 | ||
682 | 682 | ||
683 | ___'' | 683 | ___'' |
684 | local a, b, c = mod.requiref() | 684 | local a, b, c = mod.requiref() |
685 | print( type(a), type(b), type(c), | 685 | print("requiref", type(a), type(b), type(c), |
686 | a.boolean, b.boolean, c.boolean, | 686 | a.boolean, b.boolean, c.boolean, |
687 | type(requiref1), type(requiref2), type(requiref3)) | 687 | type(requiref1), type(requiref2), type(requiref3)) |
688 | 688 | ||
689 | ___'' | 689 | ___'' |
690 | local proxy, backend = {}, {} | 690 | local proxy, backend = {}, {} |
691 | setmetatable(proxy, { __index = backend, __newindex = backend }) | 691 | setmetatable(proxy, { __index = backend, __newindex = backend }) |
692 | print(rawget(proxy, 1), rawget(backend, 1)) | 692 | print("geti/seti", rawget(proxy, 1), rawget(backend, 1)) |
693 | print(mod.getseti(proxy, 1)) | 693 | print("geti/seti", mod.getseti(proxy, 1)) |
694 | print(rawget(proxy, 1), rawget(backend, 1)) | 694 | print("geti/seti", rawget(proxy, 1), rawget(backend, 1)) |
695 | print(mod.getseti(proxy, 1)) | 695 | print("geti/seti", mod.getseti(proxy, 1)) |
696 | print(rawget(proxy, 1), rawget(backend, 1)) | 696 | print("geti/seti", rawget(proxy, 1), rawget(backend, 1)) |
697 | 697 | ||
698 | -- tests for Lua 5.1 | 698 | -- tests for Lua 5.1 |
699 | ___'' | 699 | ___'' |
700 | print(mod.tonumber(12)) | 700 | print("tonumber", mod.tonumber(12)) |
701 | print(mod.tonumber("12")) | 701 | print("tonumber", mod.tonumber("12")) |
702 | print(mod.tonumber("0")) | 702 | print("tonumber", mod.tonumber("0")) |
703 | print(mod.tonumber(false)) | 703 | print("tonumber", mod.tonumber(false)) |
704 | print(mod.tonumber("error")) | 704 | print("tonumber", mod.tonumber("error")) |
705 | 705 | ||
706 | ___'' | 706 | ___'' |
707 | print(mod.tointeger(12)) | 707 | print("tointeger", mod.tointeger(12)) |
708 | print(mod.tointeger("12")) | 708 | print("tointeger", mod.tointeger("12")) |
709 | print(mod.tointeger("0")) | 709 | print("tointeger", mod.tointeger("0")) |
710 | print( "aaa" ) | 710 | print("tointeger", mod.tointeger(math.pi)) |
711 | print(mod.tointeger(math.pi)) | 711 | print("tointeger", mod.tointeger(false)) |
712 | print( "bbb" ) | 712 | print("tointeger", mod.tointeger("error")) |
713 | print(mod.tointeger(false)) | ||
714 | print(mod.tointeger("error")) | ||
715 | 713 | ||
716 | ___'' | 714 | ___'' |
717 | print(mod.len("123")) | 715 | print("len", mod.len("123")) |
718 | print(mod.len({ 1, 2, 3})) | 716 | print("len", mod.len({ 1, 2, 3})) |
719 | print(pcall(mod.len, true)) | 717 | print("len", pcall(mod.len, true)) |
720 | local ud, meta = mod.newproxy() | 718 | local ud, meta = mod.newproxy() |
721 | meta.__len = function() return 5 end | 719 | meta.__len = function() return 5 end |
722 | print(mod.len(ud)) | 720 | print("len", mod.len(ud)) |
723 | meta.__len = function() return true end | 721 | meta.__len = function() return true end |
724 | print(pcall(mod.len, ud)) | 722 | print("len", pcall(mod.len, ud)) |
725 | 723 | ||
726 | ___'' | 724 | ___'' |
727 | print(mod.copy(true, "string", {}, 1)) | 725 | print("copy", mod.copy(true, "string", {}, 1)) |
728 | 726 | ||
729 | ___'' | 727 | ___'' |
730 | print(mod.rawxetp()) | 728 | print("rawgetp/rawsetp", mod.rawxetp()) |
731 | print(mod.rawxetp("I'm back")) | 729 | print("rawgetp/rawsetp", mod.rawxetp("I'm back")) |
732 | 730 | ||
733 | ___'' | 731 | ___'' |
734 | print(F(mod.globals()), mod.globals() == _G) | 732 | print("globals", F(mod.globals()), mod.globals() == _G) |
735 | 733 | ||
736 | ___'' | 734 | ___'' |
737 | local t = {} | 735 | local t = {} |
738 | print(F(mod.subtable(t))) | 736 | print("getsubtable", F(mod.subtable(t))) |
739 | local x, msg = mod.subtable(t) | 737 | local x, msg = mod.subtable(t) |
740 | print(F(x, msg, x == t.xxx)) | 738 | print("getsubtable", F(x, msg, x == t.xxx)) |
741 | 739 | ||
742 | ___'' | 740 | ___'' |
743 | print(F(mod.udata())) | 741 | print("udata", F(mod.udata())) |
744 | print(mod.udata("nosuchtype")) | 742 | print("udata", mod.udata("nosuchtype")) |
745 | 743 | ||
746 | ___'' | 744 | ___'' |
747 | print(F(mod.uservalue())) | 745 | print("uservalue", F(mod.uservalue())) |
748 | 746 | ||
749 | ___'' | 747 | ___'' |
750 | print(mod.getupvalues()) | 748 | print("upvalues", mod.getupvalues()) |
751 | 749 | ||
752 | ___'' | 750 | ___'' |
753 | print(mod.absindex("hi", true)) | 751 | print("absindex", mod.absindex("hi", true)) |
754 | 752 | ||
755 | ___'' | 753 | ___'' |
756 | print(mod.arith(2, 1)) | 754 | print("arith", mod.arith(2, 1)) |
757 | print(mod.arith(3, 5)) | 755 | print("arith", mod.arith(3, 5)) |
758 | 756 | ||
759 | ___'' | 757 | ___'' |
760 | print(mod.compare(1, 1)) | 758 | print("compare", mod.compare(1, 1)) |
761 | print(mod.compare(2, 1)) | 759 | print("compare", mod.compare(2, 1)) |
762 | print(mod.compare(1, 2)) | 760 | print("compare", mod.compare(1, 2)) |
763 | 761 | ||
764 | ___'' | 762 | ___'' |
765 | print(mod.tolstring("string")) | 763 | print("tolstring", mod.tolstring("string")) |
766 | local t = setmetatable({}, { | 764 | local t = setmetatable({}, { |
767 | __tostring = function(v) return "mytable" end | 765 | __tostring = function(v) return "mytable" end |
768 | }) | 766 | }) |
769 | print(mod.tolstring(t)) | 767 | print("tolstring", mod.tolstring(t)) |
770 | local t = setmetatable({}, { | 768 | local t = setmetatable({}, { |
771 | __tostring = function(v) return nil end | 769 | __tostring = function(v) return nil end |
772 | }) | 770 | }) |
773 | print(pcall(mod.tolstring, t)) | 771 | print("tolstring", pcall(mod.tolstring, t)) |
774 | local ud, meta = mod.newproxy() | 772 | local ud, meta = mod.newproxy() |
775 | meta.__name = "XXX" | 773 | meta.__name = "XXX" |
776 | print(mod.tolstring(ud):gsub(":.*$", ": yyy")) | 774 | print("tolstring", mod.tolstring(ud):gsub(":.*$", ": yyy")) |
777 | 775 | ||
778 | ___'' | 776 | ___'' |
779 | print(mod.pushstring()) | 777 | print("pushstring", mod.pushstring()) |
780 | 778 | ||
781 | ___'' | 779 | ___'' |
782 | print(mod.buffer()) | 780 | print("Buffer", mod.buffer()) |
783 | 781 | ||
784 | ___'' | 782 | ___'' |
785 | print(mod.exec("exit 0")) | 783 | print("execresult", mod.exec("exit 0")) |
786 | print(mod.exec("exit 1")) | 784 | print("execresult", mod.exec("exit 1")) |
787 | print(mod.exec("exit 25")) | 785 | print("execresult", mod.exec("exit 25")) |
786 | |||
787 | ___'' | ||
788 | do | ||
789 | local bin = string.dump(function() end) | ||
790 | local modes = { "t", "b", "bt" } | ||
791 | local codes = { | ||
792 | "", "return true", bin, "invalidsource", "\27invalidbinary" | ||
793 | } | ||
794 | for _,m in ipairs(modes) do | ||
795 | for i,c in ipairs(codes) do | ||
796 | print("loadbufferx", m, i, F(mod.loadstring(c, m))) | ||
797 | end | ||
798 | end | ||
799 | |||
800 | ___'' | ||
801 | local bom = "\239\187\191" | ||
802 | local shebang = "#!/usr/bin/env lua\n" | ||
803 | codes[#codes+1] = bom .. shebang .. "return true" | ||
804 | codes[#codes+1] = bom .. shebang .. bin | ||
805 | codes[#codes+1] = bom .. shebang .. "invalidsource" | ||
806 | codes[#codes+1] = bom .. shebang .. "\027invalidbinary" | ||
807 | for _,m in ipairs(modes) do | ||
808 | for i,c in ipairs(codes) do | ||
809 | print("loadfilex", m, i, F(mod.loadfile(c, m))) | ||
810 | end | ||
811 | end | ||
812 | end | ||
788 | ___'' | 813 | ___'' |
789 | 814 | ||
diff --git a/tests/testmod.c b/tests/testmod.c index 868136b..1034d20 100644 --- a/tests/testmod.c +++ b/tests/testmod.c | |||
@@ -274,6 +274,33 @@ static int test_exec (lua_State *L) { | |||
274 | return luaL_execresult(L, system(cmd)); | 274 | return luaL_execresult(L, system(cmd)); |
275 | } | 275 | } |
276 | 276 | ||
277 | static int test_loadstring (lua_State *L) { | ||
278 | size_t len = 0; | ||
279 | char const* s = luaL_checklstring(L, 1, &len); | ||
280 | char const* mode = luaL_optstring(L, 2, "bt"); | ||
281 | lua_pushinteger(L, luaL_loadbufferx(L, s, len, s, mode)); | ||
282 | return 2; | ||
283 | } | ||
284 | |||
285 | static int test_loadfile (lua_State *L) { | ||
286 | char filename[L_tmpnam+1] = { 0 }; | ||
287 | size_t len = 0; | ||
288 | char const* s = luaL_checklstring(L, 1, &len); | ||
289 | char const* mode = luaL_optstring(L, 2, "bt"); | ||
290 | if (tmpnam(filename)) { | ||
291 | FILE* f = fopen(filename, "wb"); | ||
292 | if (f) { | ||
293 | fwrite(s, 1, len, f); | ||
294 | fclose(f); | ||
295 | lua_pushinteger(L, luaL_loadfilex(L, filename, mode)); | ||
296 | remove(filename); | ||
297 | return 2; | ||
298 | } else | ||
299 | remove(filename); | ||
300 | } | ||
301 | return 0; | ||
302 | } | ||
303 | |||
277 | 304 | ||
278 | static const luaL_Reg funcs[] = { | 305 | static const luaL_Reg funcs[] = { |
279 | { "isinteger", test_isinteger }, | 306 | { "isinteger", test_isinteger }, |
@@ -297,6 +324,8 @@ static const luaL_Reg funcs[] = { | |||
297 | { "pushstring", test_pushstring }, | 324 | { "pushstring", test_pushstring }, |
298 | { "buffer", test_buffer }, | 325 | { "buffer", test_buffer }, |
299 | { "exec", test_exec }, | 326 | { "exec", test_exec }, |
327 | { "loadstring", test_loadstring }, | ||
328 | { "loadfile", test_loadfile }, | ||
300 | { NULL, NULL } | 329 | { NULL, NULL } |
301 | }; | 330 | }; |
302 | 331 | ||