|
1 | 1 | ---
|
2 | 2 | sidebar_position: 2
|
3 | 3 | ---
|
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. |
5 | 5 |
|
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" |
8 | 18 | table.freeze(_G)
|
9 | 19 |
|
10 |
| -_G.string = {} |
11 |
| --- file.pluto:4: attempt to modify frozen table. |
| 20 | +_G.string = {} -- attempt to modify frozen table. |
12 | 21 | ```
|
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" |
15 | 23 | local Frozen <const> = table.freeze({ 1, 2, 3 })
|
16 | 24 |
|
17 |
| -Frozen = {} |
18 |
| --- file.pluto:4: attempt to reassign constant. |
| 25 | +Frozen = {} -- attempt to reassign constant. |
19 | 26 |
|
20 |
| -Frozen[1] = "new value" |
21 |
| --- file.pluto:7: attempt to modify frozen table. |
| 27 | +Frozen[1] = "new value" -- attempt to modify frozen table. |
22 | 28 |
|
23 |
| -rawset(Frozen, "key", "value") |
24 |
| --- file.pluto:10: attempt to modify frozen table. |
| 29 | +rawset(Frozen, "key", "value") -- attempt to modify frozen table. |
25 | 30 | ```
|
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 }) |
28 | 33 | for i = 1, 249 do
|
29 | 34 | local name, value = debug.getlocal(1, i)
|
30 | 35 | 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. |
33 | 37 | end
|
34 | 38 | end
|
35 | 39 | ```
|
| 40 | + |
36 | 41 | ### What will this prevent?
|
37 | 42 | All modifications to the table from within the Lua environment will be prevented, including those from the `debug` library.
|
38 | 43 |
|
39 | 44 | :::caution
|
40 | 45 | 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.
|
41 | 46 | :::
|
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