Skip to content

Commit 20912f9

Browse files
committed
chore: update table.* and tablepool annotations
1 parent 76c2905 commit 20912f9

File tree

7 files changed

+135
-7
lines changed

7 files changed

+135
-7
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
---@meta
2-
function table_clear() end
3-
return table_clear
2+
3+
--- Clear a table of its contents.
4+
---@param t table
5+
local function clear(t) end
6+
7+
return clear
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---@meta
2+
3+
--- Returns a shallow copy of the given Lua table.
4+
---
5+
--- This API can be JIT compiled.
6+
---
7+
--- Usage:
8+
---
9+
--- ```lua
10+
--- local clone = require "table.clone"
11+
---
12+
--- local x = {x=12, y={5, 6, 7}}
13+
--- local y = clone(x)
14+
--- ```
15+
---
16+
--- **Note:** We observe 7% over-all speedup in the edgelang-fan compiler's
17+
--- compiling speed whose Lua is generated by the fanlang compiler.
18+
---
19+
--- **Note bis:** Deep cloning is planned to be supported by adding `true` as a second argument.
20+
---
21+
---@param t table
22+
---@return table
23+
local function clone(t) end
24+
25+
return clone
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---@meta
2+
3+
--- Returns `true` when the given Lua table is a pure array-like Lua table, or
4+
--- `false` otherwise.
5+
---
6+
--- Empty Lua tables are treated as arrays.
7+
---
8+
--- This API can be JIT compiled.
9+
---
10+
--- Usage:
11+
---
12+
--- ```lua
13+
--- local isarray = require "table.isarray"
14+
---
15+
--- print(isarray{"a", true, 3.14}) -- true
16+
--- print(isarray{dog = 3}) -- false
17+
--- print(isarray{}) -- true
18+
--- ```
19+
---@param t table
20+
---@return boolean
21+
local function isarray(t) end
22+
23+
return isarray
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---@meta
2+
3+
--- Returns `true` when the given Lua table contains neither non-nil array elements nor non-nil key-value pairs, or `false` otherwise.
4+
---
5+
--- This API can be JIT compiled.
6+
--- Usage:
7+
---
8+
--- ```lua
9+
--- local isempty = require "table.isempty"
10+
---
11+
--- print(isempty({})) -- true
12+
--- print(isempty({nil, dog = nil})) -- true
13+
--- print(isempty({"a", "b"})) -- false
14+
--- print(isempty({nil, 3})) -- false
15+
--- print(isempty({cat = 3})) -- false
16+
--- ```
17+
---
18+
---@param t table
19+
---@return boolean
20+
local function isempty(t) end
21+
22+
return isempty
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
---@meta
2-
function table_new() end
3-
return table_new
2+
3+
--- Create a new table.
4+
---
5+
---@param narr integer number of array-like items
6+
---@param nrec integer number of hash-like items
7+
---@return table
8+
local function new(narr, nrec) end
9+
10+
return new
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---@meta
2+
3+
--- Returns the total number of elements in a given Lua table (i.e. from both the
4+
--- array and hash parts combined).
5+
---
6+
--- This API can be JIT compiled.
7+
---
8+
--- Usage:
9+
---
10+
--- ```lua
11+
--- local nkeys = require "table.nkeys"
12+
---
13+
--- print(nkeys({})) -- 0
14+
--- print(nkeys({ "a", nil, "b" })) -- 2
15+
--- print(nkeys({ dog = 3, cat = 4, bird = nil })) -- 2
16+
--- print(nkeys({ "a", dog = 3, cat = 4 })) -- 3
17+
--- ```
18+
---
19+
---@param t table
20+
---@return integer
21+
local function nkeys(t) end
22+
23+
return nkeys
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
---@meta
2-
tablepool={}
3-
function tablepool.release(tag, obj, noclear) end
4-
function tablepool.fetch(tag, narr, nrec) end
2+
local tablepool={}
3+
4+
--- Releases the already used Lua table, `tb`, into the table pool named `pool_name`. If the specified table pool does not exist, create it right away.
5+
---
6+
--- The caller must *not* continue using the released Lua table, `tb`, after this call. Otherwise random data corruption is expected.
7+
---
8+
--- The optional `no_clear` parameter specifies whether to clear the contents in the Lua table `tb` before putting it into the pool. Defaults to `false`, that is, always clearing the Lua table.
9+
---
10+
--- If you always initialize all the elements in the Lua table and always use the exactly same number of elements in the Lua table, then you can set this argument to `true` to avoid the overhead of explicit table clearing.
11+
---
12+
--- According to the current implementation, for maximum 200 Lua tables can be cached in an individual pool. We may make this configurable in the future. If the specified table pool already exceeds its size limit, then the `tb` table is subject to garbage collection. This behavior is to avoid potential memory leak due to unbalanced `fetch` and `release` method calls.
13+
---
14+
---@param pool_name string
15+
---@param tb table
16+
---@param no_clear boolean
17+
function tablepool.release(pool_name, tb, no_clear) end
18+
19+
--- Fetches a (free) Lua table from the table pool of the specified name `pool_name`.
20+
---
21+
--- If the pool does not exist or the pool is empty, simply create a Lua table whose array part has `narr` elements and whose hash table part has `nrec` elements.
22+
---
23+
---@param pool_name string
24+
---@param narr number size of the array-like part of the table
25+
---@param nrec number size of the hash-like part of the table
26+
---@return table
27+
function tablepool.fetch(pool_name, narr, nrec) end
28+
529
return tablepool

0 commit comments

Comments
 (0)