BREAKING: Replace custom import() with normal Lua require(). (#5014)

* Use require() instead of a custom import()

* Also search relative to the current file

* Update documentation
This commit is contained in:
Mm2PL
2023-12-16 13:16:54 +01:00
committed by GitHub
parent bbf75516ed
commit 5f8c4c6b66
6 changed files with 131 additions and 71 deletions
+14 -7
View File
@@ -158,18 +158,25 @@ It achieves this by forcing all inputs to be encoded with `UTF-8`.
See [official documentation](https://www.lua.org/manual/5.4/manual.html#pdf-load)
#### `import(filename)`
#### `require(modname)`
This function mimics Lua's `dofile` however relative paths are relative to your plugin's directory.
You are restricted to loading files in your plugin's directory. You cannot load files with bytecode inside.
This is Lua's [`require()`](https://www.lua.org/manual/5.3/manual.html#pdf-require) function.
However the searcher and load configuration is notably different than default:
- Lua's built-in dynamic library searcher is removed,
- `package.path` is not used, in its place are two searchers,
- when `require()` is used, first a file relative to the currently executing
file will be checked, then a file relative to the plugin directory,
- binary chunks are never loaded
As in normal Lua, dots are converted to the path separators (`'/'` on Linux and Mac, `'\'` on Windows).
Example:
```lua
import("stuff.lua") -- executes Plugins/name/stuff.lua
import("./stuff.lua") -- executes Plugins/name/stuff.lua
import("../stuff.lua") -- tries to load Plugins/stuff.lua and errors
import("luac.out") -- tried to load Plugins/name/luac.out and errors because it contains non-utf8 data
require("stuff") -- executes Plugins/name/stuff.lua or $(dirname $CURR_FILE)/stuff.lua
require("dir.name") -- executes Plugins/name/dir/name.lua or $(dirname $CURR_FILE)/dir/name.lua
require("binary") -- tried to load Plugins/name/binary.lua and errors because binary is not a text file
```
#### `print(Args...)`