aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2017-09-16 17:19:35 +0200
committerPhilipp Janda <siffiejoe@gmx.net>2017-09-16 17:19:35 +0200
commit09b201c87c904066e1de5d44cf1e02538970c168 (patch)
treee56be07e08699f920482212f07342acf8f9526ae
parent3b52d81dcf1b5d6d49e1837612e207b2cab6b74d (diff)
downloadlua-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.c19
-rwxr-xr-xtests/test.lua157
-rw-r--r--tests/testmod.c29
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
448static int compat53_checkmode (lua_State *L, const char *mode, const char *modename, int err) { 448static 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
477COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) { 477COMPAT53_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 @@ ___''
657print("testing C API ...") 657print("testing C API ...")
658local mod = require("testmod") 658local mod = require("testmod")
659___'' 659___''
660print(mod.isinteger(1)) 660print("isinteger", mod.isinteger(1))
661print(mod.isinteger(0)) 661print("isinteger", mod.isinteger(0))
662print(mod.isinteger(1234567)) 662print("isinteger", mod.isinteger(1234567))
663print(mod.isinteger(12.3)) 663print("isinteger", mod.isinteger(12.3))
664print(mod.isinteger(math.huge)) 664print("isinteger", mod.isinteger(math.huge))
665print(mod.isinteger(math.sqrt(-1))) 665print("isinteger", mod.isinteger(math.sqrt(-1)))
666 666
667 667
668___'' 668___''
669print(mod.rotate(1, 1, 2, 3, 4, 5, 6)) 669print("rotate", mod.rotate(1, 1, 2, 3, 4, 5, 6))
670print(mod.rotate(-1, 1, 2, 3, 4, 5, 6)) 670print("rotate", mod.rotate(-1, 1, 2, 3, 4, 5, 6))
671print(mod.rotate(4, 1, 2, 3, 4, 5, 6)) 671print("rotate", mod.rotate(4, 1, 2, 3, 4, 5, 6))
672print(mod.rotate(-4, 1, 2, 3, 4, 5, 6)) 672print("rotate", mod.rotate(-4, 1, 2, 3, 4, 5, 6))
673 673
674 674
675___'' 675___''
676print(mod.strtonum("+123")) 676print("strtonum", mod.strtonum("+123"))
677print(mod.strtonum(" 123 ")) 677print("strtonum", mod.strtonum(" 123 "))
678print(mod.strtonum("-1.23")) 678print("strtonum", mod.strtonum("-1.23"))
679print(mod.strtonum(" 123 abc")) 679print("strtonum", mod.strtonum(" 123 abc"))
680print(mod.strtonum("jkl")) 680print("strtonum", mod.strtonum("jkl"))
681 681
682 682
683___'' 683___''
684local a, b, c = mod.requiref() 684local a, b, c = mod.requiref()
685print( type(a), type(b), type(c), 685print("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___''
690local proxy, backend = {}, {} 690local proxy, backend = {}, {}
691setmetatable(proxy, { __index = backend, __newindex = backend }) 691setmetatable(proxy, { __index = backend, __newindex = backend })
692print(rawget(proxy, 1), rawget(backend, 1)) 692print("geti/seti", rawget(proxy, 1), rawget(backend, 1))
693print(mod.getseti(proxy, 1)) 693print("geti/seti", mod.getseti(proxy, 1))
694print(rawget(proxy, 1), rawget(backend, 1)) 694print("geti/seti", rawget(proxy, 1), rawget(backend, 1))
695print(mod.getseti(proxy, 1)) 695print("geti/seti", mod.getseti(proxy, 1))
696print(rawget(proxy, 1), rawget(backend, 1)) 696print("geti/seti", rawget(proxy, 1), rawget(backend, 1))
697 697
698-- tests for Lua 5.1 698-- tests for Lua 5.1
699___'' 699___''
700print(mod.tonumber(12)) 700print("tonumber", mod.tonumber(12))
701print(mod.tonumber("12")) 701print("tonumber", mod.tonumber("12"))
702print(mod.tonumber("0")) 702print("tonumber", mod.tonumber("0"))
703print(mod.tonumber(false)) 703print("tonumber", mod.tonumber(false))
704print(mod.tonumber("error")) 704print("tonumber", mod.tonumber("error"))
705 705
706___'' 706___''
707print(mod.tointeger(12)) 707print("tointeger", mod.tointeger(12))
708print(mod.tointeger("12")) 708print("tointeger", mod.tointeger("12"))
709print(mod.tointeger("0")) 709print("tointeger", mod.tointeger("0"))
710print( "aaa" ) 710print("tointeger", mod.tointeger(math.pi))
711print(mod.tointeger(math.pi)) 711print("tointeger", mod.tointeger(false))
712print( "bbb" ) 712print("tointeger", mod.tointeger("error"))
713print(mod.tointeger(false))
714print(mod.tointeger("error"))
715 713
716___'' 714___''
717print(mod.len("123")) 715print("len", mod.len("123"))
718print(mod.len({ 1, 2, 3})) 716print("len", mod.len({ 1, 2, 3}))
719print(pcall(mod.len, true)) 717print("len", pcall(mod.len, true))
720local ud, meta = mod.newproxy() 718local ud, meta = mod.newproxy()
721meta.__len = function() return 5 end 719meta.__len = function() return 5 end
722print(mod.len(ud)) 720print("len", mod.len(ud))
723meta.__len = function() return true end 721meta.__len = function() return true end
724print(pcall(mod.len, ud)) 722print("len", pcall(mod.len, ud))
725 723
726___'' 724___''
727print(mod.copy(true, "string", {}, 1)) 725print("copy", mod.copy(true, "string", {}, 1))
728 726
729___'' 727___''
730print(mod.rawxetp()) 728print("rawgetp/rawsetp", mod.rawxetp())
731print(mod.rawxetp("I'm back")) 729print("rawgetp/rawsetp", mod.rawxetp("I'm back"))
732 730
733___'' 731___''
734print(F(mod.globals()), mod.globals() == _G) 732print("globals", F(mod.globals()), mod.globals() == _G)
735 733
736___'' 734___''
737local t = {} 735local t = {}
738print(F(mod.subtable(t))) 736print("getsubtable", F(mod.subtable(t)))
739local x, msg = mod.subtable(t) 737local x, msg = mod.subtable(t)
740print(F(x, msg, x == t.xxx)) 738print("getsubtable", F(x, msg, x == t.xxx))
741 739
742___'' 740___''
743print(F(mod.udata())) 741print("udata", F(mod.udata()))
744print(mod.udata("nosuchtype")) 742print("udata", mod.udata("nosuchtype"))
745 743
746___'' 744___''
747print(F(mod.uservalue())) 745print("uservalue", F(mod.uservalue()))
748 746
749___'' 747___''
750print(mod.getupvalues()) 748print("upvalues", mod.getupvalues())
751 749
752___'' 750___''
753print(mod.absindex("hi", true)) 751print("absindex", mod.absindex("hi", true))
754 752
755___'' 753___''
756print(mod.arith(2, 1)) 754print("arith", mod.arith(2, 1))
757print(mod.arith(3, 5)) 755print("arith", mod.arith(3, 5))
758 756
759___'' 757___''
760print(mod.compare(1, 1)) 758print("compare", mod.compare(1, 1))
761print(mod.compare(2, 1)) 759print("compare", mod.compare(2, 1))
762print(mod.compare(1, 2)) 760print("compare", mod.compare(1, 2))
763 761
764___'' 762___''
765print(mod.tolstring("string")) 763print("tolstring", mod.tolstring("string"))
766local t = setmetatable({}, { 764local t = setmetatable({}, {
767 __tostring = function(v) return "mytable" end 765 __tostring = function(v) return "mytable" end
768}) 766})
769print(mod.tolstring(t)) 767print("tolstring", mod.tolstring(t))
770local t = setmetatable({}, { 768local t = setmetatable({}, {
771 __tostring = function(v) return nil end 769 __tostring = function(v) return nil end
772}) 770})
773print(pcall(mod.tolstring, t)) 771print("tolstring", pcall(mod.tolstring, t))
774local ud, meta = mod.newproxy() 772local ud, meta = mod.newproxy()
775meta.__name = "XXX" 773meta.__name = "XXX"
776print(mod.tolstring(ud):gsub(":.*$", ": yyy")) 774print("tolstring", mod.tolstring(ud):gsub(":.*$", ": yyy"))
777 775
778___'' 776___''
779print(mod.pushstring()) 777print("pushstring", mod.pushstring())
780 778
781___'' 779___''
782print(mod.buffer()) 780print("Buffer", mod.buffer())
783 781
784___'' 782___''
785print(mod.exec("exit 0")) 783print("execresult", mod.exec("exit 0"))
786print(mod.exec("exit 1")) 784print("execresult", mod.exec("exit 1"))
787print(mod.exec("exit 25")) 785print("execresult", mod.exec("exit 25"))
786
787___''
788do
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
812end
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
277static 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
285static 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
278static const luaL_Reg funcs[] = { 305static 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