2023-08-26 22:27:13 +01:00
|
|
|
# Luau
|
|
|
|
|
|
|
|
Built-in library for generating luau bytecode & functions.
|
|
|
|
|
|
|
|
#### Example usage
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local luau = require("@lune/luau")
|
|
|
|
|
|
|
|
local bytecode = luau.compile("print('Hello, World!')")
|
|
|
|
local callableFn = luau.load(bytecode)
|
|
|
|
|
|
|
|
-- Additionally, we can skip the bytecode generation and load a callable function directly from the code itself.
|
|
|
|
-- local callableFn = luau.load("print('Hello, World!')")
|
|
|
|
|
|
|
|
callableFn()
|
|
|
|
```
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
Since luau bytecode is highly compressible, it may also make sense to compress it using the `serde`
|
|
|
|
library while transmitting large amounts of it.
|
|
|
|
|
2023-08-26 22:27:13 +01:00
|
|
|
## Functions
|
|
|
|
|
|
|
|
### compile
|
|
|
|
|
|
|
|
Compiles sourcecode into Luau bytecode
|
|
|
|
|
|
|
|
An error will be thrown if the sourcecode given isn't valid Luau code.
|
|
|
|
|
|
|
|
#### Example usage
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local luau = require("@lune/luau")
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
-- Compile the source to some highly optimized bytecode
|
2023-08-26 22:27:13 +01:00
|
|
|
local bytecode = luau.compile("print('Hello, World!')", {
|
2024-03-14 13:44:52 +00:00
|
|
|
optimizationLevel = 2,
|
|
|
|
coverageLevel = 0,
|
|
|
|
debugLevel = 1,
|
2023-08-26 22:27:13 +01:00
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
- `source` The string that will be compiled into bytecode
|
2023-08-26 22:27:13 +01:00
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
- `compileOptions` The options passed to the luau compiler that will output the bytecode
|
2023-08-26 22:27:13 +01:00
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
- luau bytecode
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### load
|
|
|
|
|
|
|
|
Generates a function from either bytecode or sourcecode
|
|
|
|
|
|
|
|
An error will be thrown if the sourcecode given isn't valid luau code.
|
|
|
|
|
|
|
|
#### Example usage
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local luau = require("@lune/luau")
|
|
|
|
|
|
|
|
local bytecode = luau.compile("print('Hello, World!')")
|
|
|
|
local callableFn = luau.load(bytecode, {
|
|
|
|
debugName = "'Hello, World'"
|
|
|
|
})
|
|
|
|
|
|
|
|
callableFn()
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
- `source` Either luau bytecode or string source code
|
2023-08-26 22:27:13 +01:00
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
- `loadOptions` The options passed to luau for loading the chunk
|
2023-08-26 22:27:13 +01:00
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
- luau chunk
|
2023-08-26 22:27:13 +01:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
## Types
|
|
|
|
|
|
|
|
### CompileOptions
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
The options passed to the luau compiler while compiling bytecode.
|
2023-08-26 22:27:13 +01:00
|
|
|
|
|
|
|
This is a dictionary that may contain one or more of the following values:
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
- `optimizationLevel` - Sets the compiler option "optimizationLevel". Defaults to `1`.
|
|
|
|
- `coverageLevel` - Sets the compiler option "coverageLevel". Defaults to `0`.
|
|
|
|
- `debugLevel` - Sets the compiler option "debugLevel". Defaults to `1`.
|
2023-08-26 22:27:13 +01:00
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
Documentation regarding what these values represent can be found
|
|
|
|
[here](https://github.com/Roblox/luau/blob/bd229816c0a82a8590395416c81c333087f541fd/Compiler/include/luacode.h#L13-L39).
|
2023-08-26 22:27:13 +01:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
### LoadOptions
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
The options passed while loading a luau chunk from an arbitrary string, or bytecode.
|
2023-08-26 22:27:13 +01:00
|
|
|
|
|
|
|
This is a dictionary that may contain one or more of the following values:
|
|
|
|
|
2024-03-14 13:44:52 +00:00
|
|
|
- `debugName` - The debug name of the closure. Defaults to `luau.load(...)`.
|
2024-06-23 13:57:50 +01:00
|
|
|
- `environment` - A custom environment to load the chunk in. Setting a custom environment will
|
|
|
|
deoptimize the chunk and forcefully disable codegen. Defaults to the global environment.
|
|
|
|
- `injectGlobals` - Whether or not to inject globals in the custom environment. Has no effect if
|
|
|
|
no custom environment is provided. Defaults to `true`.
|
|
|
|
- `codegenEnabled` - Whether or not to enable codegen. Defaults to `false`.
|
2023-08-26 22:27:13 +01:00
|
|
|
|
|
|
|
---
|