From 15e926d43a1c3a6ff10a75ef98c1941e75c852bb Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 28 Aug 2024 00:12:29 -0300 Subject: [PATCH] generate inline compat code for table.pack --- spec/cli/gen_spec.lua | 8 ++++++-- tl.lua | 15 +++++++++------ tl.tl | 15 +++++++++------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/spec/cli/gen_spec.lua b/spec/cli/gen_spec.lua index fd3d67f51..7f537e109 100644 --- a/spec/cli/gen_spec.lua +++ b/spec/cli/gen_spec.lua @@ -297,6 +297,7 @@ describe("tl gen", function() local t = {1, 2, 3, 4} print(table.unpack(t)) + local t2 = table.pack(1, 2, "any") local n = 42 local maxi = math.maxinteger local mini = math.mininteger @@ -315,6 +316,7 @@ describe("tl gen", function() local t = { 1, 2, 3, 4 } print(table.unpack(t)) + local t2 = table.pack(1, 2, "any") local n = 42 local maxi = math.maxinteger local mini = math.mininteger @@ -330,9 +332,10 @@ describe("tl gen", function() ]] local output_code_with_optional_compat = [[ - local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local _tl_math_maxinteger = math.maxinteger or math.pow(2, 53); local _tl_math_mininteger = math.mininteger or -math.pow(2, 53) - 1; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack + local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local _tl_math_maxinteger = math.maxinteger or math.pow(2, 53); local _tl_math_mininteger = math.mininteger or -math.pow(2, 53) - 1; local table = _tl_compat and _tl_compat.table or table; local _tl_table_pack = table.pack or function(...) return { n = select("#", ...), ... } end; local _tl_table_unpack = unpack or table.unpack local t = { 1, 2, 3, 4 } print(_tl_table_unpack(t)) + local t2 = _tl_table_pack(1, 2, "any") local n = 42 local maxi = _tl_math_maxinteger local mini = _tl_math_mininteger @@ -348,9 +351,10 @@ describe("tl gen", function() ]] local output_code_with_required_compat = [[ - local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local _tl_math_maxinteger = math.maxinteger or math.pow(2, 53); local _tl_math_mininteger = math.mininteger or -math.pow(2, 53) - 1; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack + local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local math = _tl_compat and _tl_compat.math or math; local _tl_math_maxinteger = math.maxinteger or math.pow(2, 53); local _tl_math_mininteger = math.mininteger or -math.pow(2, 53) - 1; local table = _tl_compat and _tl_compat.table or table; local _tl_table_pack = table.pack or function(...) return { n = select("#", ...), ... } end; local _tl_table_unpack = unpack or table.unpack local t = { 1, 2, 3, 4 } print(_tl_table_unpack(t)) + local t2 = _tl_table_pack(1, 2, "any") local n = 42 local maxi = _tl_math_maxinteger local mini = _tl_math_mininteger diff --git a/tl.lua b/tl.lua index 8495d9727..d8e10b3e5 100644 --- a/tl.lua +++ b/tl.lua @@ -347,17 +347,17 @@ do move: function({A}, integer, integer, integer, ? {A}): {A} - pack: function(T...): PackTable - pack: function(any...): {any:any} + pack: function(T...): PackTable --[[needs_compat]] + pack: function(any...): {any:any} --[[needs_compat]] remove: function({A}, ? integer): A sort: function({A}, ? SortFunction) - unpack: function({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]] - unpack: function({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]] - unpack: function({A1, A2, A3}): A1, A2, A3 --[[needs_compat]] - unpack: function({A1, A2}): A1, A2 --[[needs_compat]] unpack: function({A}, ? number, ? number): A... --[[needs_compat]] + unpack: function({A1, A2}): A1, A2 --[[needs_compat]] + unpack: function({A1, A2, A3}): A1, A2, A3 --[[needs_compat]] + unpack: function({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]] + unpack: function({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]] end global record utf8 @@ -6833,6 +6833,8 @@ local function add_compat_entries(program, used_set, gen_compat) for _, name in ipairs(used_list) do if name == "table.unpack" then load_code(name, "local _tl_table_unpack = unpack or table.unpack") + elseif name == "table.pack" then + load_code(name, [[local _tl_table_pack = table.pack or function(...) return { n = select("#", ...), ... } end]]) elseif name == "bit32" then load_code(name, "local bit32 = bit32; if not bit32 then local p, m = " .. req("bit32") .. "; if p then bit32 = m end") elseif name == "mt" then @@ -6964,6 +6966,7 @@ tl.new_env = function(opts) local table_t = (stdlib_globals["table"].t).def math_t.fields["maxinteger"].needs_compat = true math_t.fields["mininteger"].needs_compat = true + table_t.fields["pack"].needs_compat = true table_t.fields["unpack"].needs_compat = true diff --git a/tl.tl b/tl.tl index e2d273f7c..cec12f333 100644 --- a/tl.tl +++ b/tl.tl @@ -347,17 +347,17 @@ do move: function({A}, integer, integer, integer, ? {A}): {A} - pack: function(T...): PackTable - pack: function(any...): {any:any} + pack: function(T...): PackTable --[[needs_compat]] + pack: function(any...): {any:any} --[[needs_compat]] remove: function({A}, ? integer): A sort: function({A}, ? SortFunction) - unpack: function({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]] - unpack: function({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]] - unpack: function({A1, A2, A3}): A1, A2, A3 --[[needs_compat]] - unpack: function({A1, A2}): A1, A2 --[[needs_compat]] unpack: function({A}, ? number, ? number): A... --[[needs_compat]] + unpack: function({A1, A2}): A1, A2 --[[needs_compat]] + unpack: function({A1, A2, A3}): A1, A2, A3 --[[needs_compat]] + unpack: function({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]] + unpack: function({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]] end global record utf8 @@ -6833,6 +6833,8 @@ local function add_compat_entries(program: Node, used_set: {string: boolean}, ge for _, name in ipairs(used_list) do if name == "table.unpack" then load_code(name, "local _tl_table_unpack = unpack or table.unpack") + elseif name == "table.pack" then + load_code(name, [[local _tl_table_pack = table.pack or function(...) return { n = select("#", ...), ... } end]]) elseif name == "bit32" then load_code(name, "local bit32 = bit32; if not bit32 then local p, m = " .. req("bit32") .. "; if p then bit32 = m end") elseif name == "mt" then @@ -6964,6 +6966,7 @@ tl.new_env = function(opts?: EnvOptions): Env, string local table_t = (stdlib_globals["table"].t as TypeDeclType).def as RecordType math_t.fields["maxinteger"].needs_compat = true math_t.fields["mininteger"].needs_compat = true + table_t.fields["pack"].needs_compat = true table_t.fields["unpack"].needs_compat = true -- only global scope and vararg functions accept `...`: