From 038a88eca543d84264067ceb372d21599791f120 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Thu, 29 Dec 2016 16:54:31 +0100 Subject: [PATCH] Functions defined within a string will now be shown fully. --- pretty.lua | 43 ++++++++++++++++++++++++++++++++++- test/test_pretty.lua | 53 +++++++++++++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/pretty.lua b/pretty.lua index b2bd302..4d1467f 100644 --- a/pretty.lua +++ b/pretty.lua @@ -192,9 +192,33 @@ local function get_function_info (f) for i = 1, info.nparams do info.params[i] = debug.getlocal(f, i) end for i = 1, info.nups do local k, v = debug.getupvalue(f, i); info.ups[k] = v end + if info.source:sub(1,1) == '=' then info.defined_how = 'C' + elseif info.source:sub(1,1) == '@' then info.defined_how = 'file' + else info.defined_how = 'string' + end + return info end +local function get_line_index (str, line_nr) + local index = 0 + for _ = 2, line_nr do + index = str:find('\n', index, true) + if not index then return #str end + index = index + 1 + end + return index +end + +local function get_full_function_str (str, start_line, end_line) + local start_line_index = get_line_index(str, start_line) + local end_line_index = get_line_index(str, end_line + 1) + + local matched_function = str:sub(start_line_index, end_line_index):match('function.+end') + return matched_function +end + + -------------------------------------------------------------------------------- -- Identifyer stuff @@ -447,9 +471,17 @@ local function format_primitive (value) return tostring(value) end +local function format_string_defined_function (value, options, depth) + local info = get_function_info(value) + local function_str = get_full_function_str(info.source, info.linedefined, info.lastlinedefined) + return function_str +end + local function format_function (value, options, depth) local info = get_function_info(value) + if info.defined_how == 'string' then return format_string_defined_function(value, options, depth) end + local l = {} -- Build function signature @@ -464,7 +496,16 @@ local function format_function (value, options, depth) if not options.more_function_info or depth ~= 0 then l[#l+1] = ' ... end' elseif options._all_function_info then - -- NOTE: This is for testing/debugging purposes. + -- NOTE: This is for testing/debugging/experimentation purposes. + + local file = io.open(info.short_src, 'r') + local function_str = get_full_function_str(file:read('*all'), info.linedefined, info.lastlinedefined) + file:close() + + l[#l+1] = '\n\t--[[ Function Body\n\t' + l[#l+1] = function_str + l[#l+1] = '\n\t--]]' + l[#l+1] = '\n\t--[[\n\tNative repr:' l[#l+1] = tostring(value) l[#l+1] = '\n\t' diff --git a/test/test_pretty.lua b/test/test_pretty.lua index 052ff16..bc55239 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -13,6 +13,7 @@ Approximate strings not similar enough: -------------------------------------------------------------------------------- local function format_test (t) + if t.longterm then return end SUITE:addTest(t.expect, function () local input_value = t.input local input_options = t.options @@ -222,14 +223,14 @@ do input = function () l = SOME_RANDOM_UPVALUE end, options = { more_function_info = true }, -- TODO: Make this more general, such that it won't fail when adding more tests above it. - expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 222]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend' + expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 223]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend' } format_test { input = function () SOME_RANDOM_UPVALUE = true end, options = { more_function_info = true }, -- TODO: Make this more general, such that it won't fail when adding more tests above it. - expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 229]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend' + expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 230]\n\t-- up_values: { SOME_RANDOM_UPVALUE = false }\n\n\t...\nend' } end @@ -237,7 +238,7 @@ format_test { input = function () end, options = { more_function_info = true }, -- TODO: Make this more general, such that it won't fail when adding more tests above it. - expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 237]\n\n\t...\nend' + expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 238]\n\n\t...\nend' } do @@ -252,20 +253,45 @@ do input = function () index = index + 1; return index end, options = { more_function_info = true }, -- TODO: Make this more general, such that it won't fail when adding more tests above it. - expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 252]\n\t-- up_values: { index = 0 }\n\n\t...\nend' + expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 253]\n\t-- up_values: { index = 0 }\n\n\t...\nend' } end format_test { input = loadstring('return function () end')(), - expect = 'function () ... end', + expect = 'function () end', +} + +format_test { + input = loadstring('return function () return function () end end')(), + expect = 'function () return function () end end', +} + +format_test { + longterm = true, + input = loadstring('return function () return function () end\nend')()(), + expect = 'function () end', +} + +format_test { + -- NOTE: This is HARD to fix. It's thus longerterm + longterm = true, + input = loadstring('return function () return function () end end')()(), + expect = 'function () end', } format_test { -- More function info allows one to even get the function whole, if it was defined in a string. input = loadstring('return function (a, b) return a + b end')(), options = { more_function_info = true }, - expect = 'function () return a + b end', + expect = 'function (a, b) return a + b end', +} + +format_test { + -- More function info allows one to even get the function whole, if it was defined in a string. + input = loadstring('return function (a, b)\n\treturn a + b\nend')(), + options = { more_function_info = true }, + expect = 'function (a, b)\n\treturn a + b\nend', } format_test { @@ -274,20 +300,14 @@ format_test { end, options = { more_function_info = true }, -- TODO: Make this more general, such that it won't fail when adding more tests above it. - expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Lines: 272 - 274]\n\n\t...\nend', + expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Lines: 298 - 300]\n\n\t...\nend', } format_test { input = function () --[[ NOTE: This function must cover a single line of code! ]] end, options = { more_function_info = true }, -- TODO: Make this more general, such that it won't fail when adding more tests above it. - expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 281]\n\n\t...\nend', -} - -format_test { - input = function () end, - options = { more_function_info = true, _all_function_info = true }, - expect = '', + expect = 'function ()\n\t-- source_file: \'./test/test_pretty.lua\' [Line: 307]\n\n\t...\nend', } format_test { @@ -298,10 +318,7 @@ format_test { -------------------------------------------------------------------------------- -- Userdata printing -format_test { - input = 'DUNNO MAYBE USE LUAJIT IN SOME WAY?', - expect = 'TODO', -} +-- TODO. First off figure out a way to test this stuff. -------------------------------------------------------------------------------- -- Thread printing