From 8d327c50445b42eb1045aefd443ecf08eee442c9 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 9 Nov 2024 08:45:06 -0800 Subject: [PATCH] bugfix: print(nil) call_protected still uses the `{...}` pattern which has this gotcha. But it's not exposed in the product, at least. --- 0050-print_to_output | 3 ++- 0181-pack | 7 +++++++ 0182-map_nil | 11 +++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 0181-pack create mode 100644 0182-map_nil diff --git a/0050-print_to_output b/0050-print_to_output index 089ce18..fc0ced8 100644 --- a/0050-print_to_output +++ b/0050-print_to_output @@ -1,5 +1,6 @@ print_to_output = function(...) - local line = table.concat(map({...}, tostring), ' ') + local args = pack(...) + local line = table.concat(map_nil(args, tostring), ' ') table.insert(Current_pane.output_editor_state.lines, {data=line}) end \ No newline at end of file diff --git a/0181-pack b/0181-pack new file mode 100644 index 0000000..5913950 --- /dev/null +++ b/0181-pack @@ -0,0 +1,7 @@ +-- like table.pack from Lua 5.2 +-- https://stackoverflow.com/questions/7183998/in-lua-what-is-the-right-way-to-handle-varargs-which-contains-nil/7186820#7186820 +pack = function(...) + local result = {...} + result.n = select('#', ...) + return result +end \ No newline at end of file diff --git a/0182-map_nil b/0182-map_nil new file mode 100644 index 0000000..abfc13a --- /dev/null +++ b/0182-map_nil @@ -0,0 +1,11 @@ +-- like map, but support nils inside arr +-- arr must provide its length in an 'n' field, +-- as returned by 'unpack'. +map_nil = function(arr, f) + local result = {} + for i=1,arr.n do + result[i] = f(arr[i]) + end + result.n = arr.n + return result +end \ No newline at end of file -- 2.45.2