Make luau compile options more strict to avoid panics

This commit is contained in:
Filip Tibell 2023-08-11 19:17:48 -05:00
parent 02459483e8
commit 375f1b9334
No known key found for this signature in database
5 changed files with 58 additions and 11 deletions

View file

@ -4,7 +4,7 @@
"luau-lsp.types.roblox": false,
"luau-lsp.require.mode": "relativeToFile",
"luau-lsp.require.directoryAliases": {
"@lune/": "~/.lune/.typedefs/0.7.5/"
"@lune/": "./types/"
},
// Luau - ignore type defs file in docs dir and dev scripts we use
"luau-lsp.ignoreGlobs": [

View file

@ -37,13 +37,23 @@ impl<'lua> FromLua<'lua> for LuauCompileOptions {
LuaValue::Table(t) => {
let mut options = Self::default();
if let Some(optimization_level) = t.get("optimizationLevel")? {
let get_and_check = |name: &'static str| -> LuaResult<Option<u8>> {
match t.get(name)? {
Some(n @ (0 | 1 | 2)) => Ok(Some(n)),
Some(n) => Err(LuaError::runtime(format!(
"'{name}' must be one of: 0, 1, or 2 - got {n}"
))),
None => Ok(None),
}
};
if let Some(optimization_level) = get_and_check("optimizationLevel")? {
options.optimization_level = optimization_level;
}
if let Some(coverage_level) = t.get("coverageLevel")? {
if let Some(coverage_level) = get_and_check("coverageLevel")? {
options.coverage_level = coverage_level;
}
if let Some(debug_level) = t.get("debugLevel")? {
if let Some(debug_level) = get_and_check("debugLevel")? {
options.debug_level = debug_level;
}

View file

@ -44,6 +44,10 @@ create_tests! {
fs_metadata: "fs/metadata",
fs_move: "fs/move",
luau_compile: "luau/compile",
luau_load: "luau/load",
luau_options: "luau/options",
net_request_codes: "net/request/codes",
net_request_compression: "net/request/compression",
net_request_methods: "net/request/methods",
@ -101,9 +105,6 @@ create_tests! {
task_delay: "task/delay",
task_spawn: "task/spawn",
task_wait: "task/wait",
luau_compile: "luau/compile",
luau_load: "luau/load",
}
#[cfg(feature = "roblox")]

38
tests/luau/options.luau Normal file
View file

@ -0,0 +1,38 @@
local luau = require("@lune/luau")
local EMPTY_LUAU_CODE_BLOCK = "do end"
local MIN_OPTION_VALUE = 0
local MAX_OPTION_VALUE = 2
local OPTION_NAMES = {
"optimizationLevel",
"coverageLevel",
"debugLevel",
}
for _, optionName in OPTION_NAMES do
-- In range should work
for optionValue = MIN_OPTION_VALUE, MAX_OPTION_VALUE, 1 do
local options = { [optionName] = optionValue }
local success2 = pcall(function()
luau.compile(EMPTY_LUAU_CODE_BLOCK, options)
end)
assert(success2, "expected `luau.compile` to accept options within range")
end
-- Lower than min range should error
for optionValue = MIN_OPTION_VALUE - 16, MIN_OPTION_VALUE - 1, 1 do
local options = { [optionName] = optionValue }
local success2 = pcall(function()
luau.compile(EMPTY_LUAU_CODE_BLOCK, options)
end)
assert(not success2, "expected `luau.compile` to not accept options outside of range")
end
-- Higher than max range should error
for optionValue = MAX_OPTION_VALUE + 1, MAX_OPTION_VALUE + 16, 1 do
local options = { [optionName] = optionValue }
local success2 = pcall(function()
luau.compile(EMPTY_LUAU_CODE_BLOCK, options)
end)
assert(not success2, "expected `luau.compile` to not accept options outside of range")
end
end

View file

@ -71,8 +71,6 @@ local luau = {}
coverageLevel: 0,
debugLevel: 1,
})
...
```
@param source The string that'll be compiled into bytecode
@ -80,7 +78,7 @@ local luau = {}
@return luau bytecode
]=]
function luau.compile(source: string, CompileOptions: CompileOptions): string
function luau.compile(source: string, CompileOptions: CompileOptions?): string
return nil :: any
end
@ -109,7 +107,7 @@ end
@return luau function
]=]
function luau.load(source: string, loadOptions: LoadOptions): string
function luau.load(source: string, loadOptions: LoadOptions?): (...any) -> ...any
return nil :: any
end