Skip to content

Commit 0b50594

Browse files
committed
Update table freezing page
1 parent 8395d53 commit 0b50594

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

docs/New Features/Table Freezing.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
---
22
sidebar_position: 2
33
---
4-
Tables can now be frozen at their current state to forbid any future modification. This action is irreversible and permanent for the lifespan of the table.
4+
Table freezing irreversibly and permanently forbids any future modifications to a given table.
55

6-
```pluto showLineNumbers
7-
-- Disallowing any edits to the global environment table.
6+
Note that table freezing is an optional feature that has to be compiled in by defining the `PLUTO_ENABLE_TABLE_FREEZING` macro in luaconf.h or your build config.
7+
8+
When enabled, it adds the following C APIs:
9+
- `lua_freezetable(L, idx)`
10+
- `lua_istablefrozen(L, idx)`
11+
- `lua_erriffrozen(L, idx)`
12+
13+
and table library functions:
14+
- `table.isfrozen`
15+
- `table.freeze`
16+
17+
```pluto norun title="Disallowing any edits to the global environment table"
818
table.freeze(_G)
919
10-
_G.string = {}
11-
-- file.pluto:4: attempt to modify frozen table.
20+
_G.string = {} -- attempt to modify frozen table.
1221
```
13-
```pluto showLineNumbers
14-
-- Creating a constant local that's associated with a frozen table.
22+
```pluto norun title="Creating a constant local that's associated with a frozen table"
1523
local Frozen <const> = table.freeze({ 1, 2, 3 })
1624
17-
Frozen = {}
18-
-- file.pluto:4: attempt to reassign constant.
25+
Frozen = {} -- attempt to reassign constant.
1926
20-
Frozen[1] = "new value"
21-
-- file.pluto:7: attempt to modify frozen table.
27+
Frozen[1] = "new value" -- attempt to modify frozen table.
2228
23-
rawset(Frozen, "key", "value")
24-
-- file.pluto:10: attempt to modify frozen table.
29+
rawset(Frozen, "key", "value") -- attempt to modify frozen table.
2530
```
26-
```pluto showLineNumbers
27-
--- Trying to swap the value with the debug library.
31+
```pluto norun title="Trying to swap the value with the debug library"
32+
local Frozen <const> = table.freeze({ 1, 2, 3 })
2833
for i = 1, 249 do
2934
local name, value = debug.getlocal(1, i)
3035
if name == "Frozen" then
31-
debug.setlocal(1, i, { ["key"] = "hello world" })
32-
-- file.pluto:5: attempt to modify local variable with a frozen table.
36+
debug.setlocal(1, i, { ["key"] = "hello world" }) -- attempt to modify local variable with a frozen table.
3337
end
3438
end
3539
```
40+
3641
### What will this prevent?
3742
All modifications to the table from within the Lua environment will be prevented, including those from the `debug` library.
3843

3944
:::caution
4045
If you're going to use this for a sandbox, ensure you call `table.freeze` before any users can access the Lua environment, otherwise they can replace that function.
4146
:::
42-
43-
:::info
44-
As of Pluto 0.11.0, these functions are unavailable by default. You can enable them by defining the `PLUTO_ENABLE_TABLE_FREEZING` macro.
45-
:::

0 commit comments

Comments
 (0)