Skip to content

Commit

Permalink
generate inline compat code for table.pack
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Aug 31, 2024
1 parent d12f534 commit b8b5bb8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
8 changes: 6 additions & 2 deletions spec/cli/gen_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 9 additions & 6 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,17 @@ do
move: function<A>({A}, integer, integer, integer, ? {A}): {A}
pack: function<T>(T...): PackTable<T>
pack: function(any...): {any:any}
pack: function<T>(T...): PackTable<T> --[[needs_compat]]
pack: function(any...): {any:any} --[[needs_compat]]
remove: function<A>({A}, ? integer): A
sort: function<A>({A}, ? SortFunction<A>)
unpack: function<A1, A2, A3, A4, A5>({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]]
unpack: function<A1, A2, A3, A4>({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]]
unpack: function<A1, A2, A3>({A1, A2, A3}): A1, A2, A3 --[[needs_compat]]
unpack: function<A1, A2>({A1, A2}): A1, A2 --[[needs_compat]]
unpack: function<A>({A}, ? number, ? number): A... --[[needs_compat]]
unpack: function<A1, A2>({A1, A2}): A1, A2 --[[needs_compat]]
unpack: function<A1, A2, A3>({A1, A2, A3}): A1, A2, A3 --[[needs_compat]]
unpack: function<A1, A2, A3, A4>({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]]
unpack: function<A1, A2, A3, A4, A5>({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]]
end
global record utf8
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
15 changes: 9 additions & 6 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -347,17 +347,17 @@ do
move: function<A>({A}, integer, integer, integer, ? {A}): {A}
pack: function<T>(T...): PackTable<T>
pack: function(any...): {any:any}
pack: function<T>(T...): PackTable<T> --[[needs_compat]]
pack: function(any...): {any:any} --[[needs_compat]]
remove: function<A>({A}, ? integer): A
sort: function<A>({A}, ? SortFunction<A>)
unpack: function<A1, A2, A3, A4, A5>({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]]
unpack: function<A1, A2, A3, A4>({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]]
unpack: function<A1, A2, A3>({A1, A2, A3}): A1, A2, A3 --[[needs_compat]]
unpack: function<A1, A2>({A1, A2}): A1, A2 --[[needs_compat]]
unpack: function<A>({A}, ? number, ? number): A... --[[needs_compat]]
unpack: function<A1, A2>({A1, A2}): A1, A2 --[[needs_compat]]
unpack: function<A1, A2, A3>({A1, A2, A3}): A1, A2, A3 --[[needs_compat]]
unpack: function<A1, A2, A3, A4>({A1, A2, A3, A4}): A1, A2, A3, A4 --[[needs_compat]]
unpack: function<A1, A2, A3, A4, A5>({A1, A2, A3, A4, A5}): A1, A2, A3, A4, A5 --[[needs_compat]]
end
global record utf8
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 `...`:
Expand Down

0 comments on commit b8b5bb8

Please sign in to comment.