Skip to content

Commit e32b8f0

Browse files
Sainanwell-in-that-case
authored andcommitted
Compatibility: Note Default Table Metatable
1 parent 9986c4f commit e32b8f0

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

docs/Compatibility.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
sidebar_position: 10
33
---
44

5-
## How compatible with Lua is Pluto?
6-
Pluto is compatible with most Lua source code. However, it's imperfect. Due to that, Pluto has several compatibility options.
7-
### Where are the incompatibilities?
5+
Pluto aims to be source- and bytecode-compatible with existing Lua code such that it can simply be used as a drop-in replacement for Lua, and Lua modules can simply be used in Pluto codebases. In the vast majority of cases, we do succeed, but there are a few things to be aware of.
6+
7+
## New Keywords
8+
89
Pluto adds the following reserved tokens:
910
- `switch`
1011
- `continue`
@@ -18,14 +19,13 @@ Pluto adds the following reserved tokens:
1819

1920
Which means you can't use them as identifiers. They can still be used with short-hand table indexes and goto labels because Pluto [allows reserved keywords to be used in those contexts](QoL%20Improvements/Reserved%20Identifiers).
2021

21-
### How to fix it?
22-
All of these incompatible keywords can be disabled:
22+
### Mitigations
23+
2324
- **For Integrators:** Check your `luaconf.h` file to find the relevant macros under the "Compatibility" heading.
2425
- **For Scripters:** Use `pluto_use` in the source files. `-- @pluto_use * = false` to simply disable all incompatible keywords.
2526
- **For Users:** Pass the `-c` flag to `pluto` or `plutoc`.
2627

27-
## For Scripters
28-
Scripters are given final say in how Compatibility Mode works within their scripts.
28+
The following sections will go more in-depth on source-level mitigations (for scripters).
2929

3030
### Compile-time Configuration (pluto_use)
3131
You can change the meaning of Pluto's reserved tokens at any point in your scripts using the `--@pluto_use` comment or `pluto_use` statement.
@@ -72,4 +72,32 @@ These are what they look like:
7272
- `pluto_parent`
7373
- `pluto_export`
7474
- `pluto_try`
75-
- `pluto_catch`
75+
- `pluto_catch`
76+
77+
## Default Table Metatable
78+
79+
This is [a feature in Pluto](Runtime%20Environment/Global%20&%20Base#default-metatables) that, by itself, is a benign QoL improvement for developers. However, in combination with our added standard library functions like [table.min](Runtime%20Environment/Table#tablemin), it can be an unexpected semantic change:
80+
81+
```pluto showLineNumbers
82+
local function roll(opts)
83+
return math.random(opts.min or 1, opts.max or 100)
84+
end
85+
print(roll{ max = 10 })
86+
```
87+
```
88+
pluto: test.pluto:2: bad argument #1 to 'random' (number expected, got function)
89+
stack traceback:
90+
[C]: in function 'math.rand'
91+
test.pluto:2: in local 'roll'
92+
test.pluto:4: in main chunk
93+
```
94+
95+
Integrators can disable this feature by defining the `PLUTO_NO_DEFAULT_TABLE_METATABLE` macro in their luaconf.h or build config, to aid in a smooth transition, should scripts in their ecosystem require it.
96+
97+
Scripters are advised to use `rawget` and/or `type` to better codify their expectations. For example, the example above seems to care only about providing fallback values and not at all about type-checking, so `rawget` would be an excellent fit for it:
98+
```pluto
99+
local function roll(opts)
100+
return math.random(rawget(opts, "min") or 1, rawget(opts, "max") or 100)
101+
end
102+
print(roll{ max = 10 })
103+
```

src/theme/pluto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Prism.languages.pluto = {
99
/(?<=\|>)\s[\w.:]+/, // |> func
1010
/\b(function|enum|class)\b/,
1111
/\b(os\.platform|json\.null|json\.withnull|json\.withorder)\b/, // standard library constants
12-
/\b(debug|table|string|number|io|os|coroutine|_VERSION|_PVERSION|_PSOUP)\b/, // standard library + type hints
12+
/\b(debug|table|string|math|number|io|os|coroutine|_VERSION|_PVERSION|_PSOUP)\b/, // standard library + type hints
1313
],
1414
'attr-value': /<(const|close|nodiscard)>/,
1515
'string': {

0 commit comments

Comments
 (0)