From f29004fe17557ec58337e37cbd0273717af47a55 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 14 Apr 2017 13:24:29 +0200 Subject: [PATCH] Improved resilience of library. --- library.lua | 12 +++++++----- pretty.lua | 7 +++---- test/test_pretty.lua | 2 +- test/test_resilience.lua | 15 +++++++++++++-- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/library.lua b/library.lua index 4a6b59d..ba93082 100644 --- a/library.lua +++ b/library.lua @@ -2,10 +2,12 @@ local function_library = {} local function library_function ( func_def ) - -- TODO: Add clause requiring that func_def.func is a builtin. - if func_def.func then - function_library[func_def.func] = func_def - end + if not func_def.func then return false end + + local is_func_builtin = not debug or not debug.getinfo or (debug.getinfo(func_def.func, 'S').what == 'C') + if not is_func_builtin then return false end + + function_library[func_def.func] = func_def end -------------------------------------------------------------------------------- @@ -1334,7 +1336,7 @@ end -------------------------------------------------------------------------------- -- FFI --- TODO +-- TODO: Add FFI documentation -- Documentation is here: http://luajit.org/ext_ffi_api.html -------------------------------------------------------------------------------- diff --git a/pretty.lua b/pretty.lua index 1beefbc..415ea5d 100644 --- a/pretty.lua +++ b/pretty.lua @@ -462,10 +462,9 @@ local TYPE_TO_FORMAT_FUNC = { ['thread'] = format_coroutine, ['table'] = format_table, - -- TODO - ['function'] = format_function, - ['userdata'] = format_primitive, - ['cdata'] = format_primitive, -- Luajit exclusive ? + ['function'] = format_function, -- TODO: Improve a little + ['userdata'] = format_primitive, -- TODO + ['cdata'] = format_primitive, -- TODO & Luajit only } function format_value (value, _, depth, l) diff --git a/test/test_pretty.lua b/test/test_pretty.lua index 4ae1db1..829f1bd 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -124,7 +124,7 @@ format_test { -------------------------------------------------------------------------------- -- Userdata printing --- TODO. First off figure out a way to test this stuff. +-- TODO: Figure out a way to print userdata. -- Maybe look into using the one available debug.getupvalue(pairs, 1) -------------------------------------------------------------------------------- diff --git a/test/test_resilience.lua b/test/test_resilience.lua index 305ae22..16a6fca 100644 --- a/test/test_resilience.lua +++ b/test/test_resilience.lua @@ -57,7 +57,7 @@ end) SUITE:addTest('a_very_small_part_of_math', function () -- This tests whether one could load the library with an empty env, without -- an error. - local env = { math = { abs = math.abs } } + local env = { debug = debug, math = { abs = math.abs } } local library = setfenv(loadfile('./library.lua'), env)() assert( library[math.abs], 'Why is math.abs not defined in the library?' ) @@ -66,13 +66,24 @@ end) SUITE:addTest('redefined part of math', function () -- This tests whether the library ignores a redefined function of an -- builtin, when defining documentation. - local env = { setfenv = setfenv, loadfile = function() end } + local env = { debug = debug, setfenv = setfenv, loadfile = function() end } local library = setfenv(loadfile('./library.lua'), env)() assert( library[env.setfenv], 'Why is setfenv not defined in the library?' ) assert( not library[env.loadfile], 'Why is loadfile defined in the library?' ) end) +SUITE:addTest('redefined part of math 2', function () + -- This tests whether the library allows a redefined function of an + -- builtin, when defining documentation, due to not being able to determine + -- the origin of the function. + local env = { setfenv = setfenv, loadfile = function() end } + local library = setfenv(loadfile('./library.lua'), env)() + + assert( library[env.setfenv], 'Why is setfenv not defined in the library?' ) + assert( library[env.loadfile], 'Why is loadfile not defined in the library?' ) +end) + -------------------------------------------------------------------------------- return SUITE