From bf0f74586153dfa8d550e1cfefd83ca9e0354171 Mon Sep 17 00:00:00 2001 From: Tristan Konolige Date: Sat, 18 Apr 2020 17:04:37 -0600 Subject: lua: allow deepcopy of functions (#12136) --- runtime/doc/lua.txt | 5 ++++- runtime/lua/vim/shared.lua | 11 ++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'runtime') diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 800f24b5c9..7f376cbbf0 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -908,7 +908,10 @@ deep_equal({a}, {b}) *vim.deep_equal()* deepcopy({orig}) *vim.deepcopy()* Returns a deep copy of the given object. Non-table objects are copied as in a typical Lua assignment, whereas table objects - are copied recursively. + are copied recursively. Functions are naively copied, so + functions in the copied table point to the same functions as + those in the input table. Userdata and threads are not copied + and will throw an error. Parameters: ~ {orig} Table to copy diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 1bf1c63fd7..d18fcfaf95 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -8,6 +8,9 @@ local vim = vim or {} --- Returns a deep copy of the given object. Non-table objects are copied as --- in a typical Lua assignment, whereas table objects are copied recursively. +--- Functions are naively copied, so functions in the copied table point to the +--- same functions as those in the input table. Userdata and threads are not +--- copied and will throw an error. --- --@param orig Table to copy --@returns New table of copied keys and (nested) values. @@ -34,10 +37,16 @@ vim.deepcopy = (function() string = _id, ['nil'] = _id, boolean = _id, + ['function'] = _id, } return function(orig) - return deepcopy_funcs[type(orig)](orig) + local f = deepcopy_funcs[type(orig)] + if f then + return f(orig) + else + error("Cannot deepcopy object of type "..type(orig)) + end end end)() -- cgit