2023-08-10 20:38:25 +01:00
|
|
|
local luau = require("@lune/luau")
|
|
|
|
|
|
|
|
local RETURN_VALUE = 1
|
|
|
|
|
|
|
|
local EMPTY_LUAU_CODE_BLOCK = "do end"
|
|
|
|
local RETURN_LUAU_CODE_BLOCK = "return " .. tostring(RETURN_VALUE)
|
|
|
|
|
|
|
|
local CUSTOM_SOURCE_BLOCK_NAME = "test"
|
|
|
|
|
|
|
|
assert(type(luau.load) == "function", "expected `luau.compile` to be a function")
|
|
|
|
|
|
|
|
assert(
|
|
|
|
type(luau.load(EMPTY_LUAU_CODE_BLOCK)) == "function",
|
|
|
|
"expected 'luau.load' to return a function"
|
|
|
|
)
|
|
|
|
assert(
|
|
|
|
luau.load(RETURN_LUAU_CODE_BLOCK)() == RETURN_VALUE,
|
|
|
|
"expected 'luau.load' to return a value"
|
|
|
|
)
|
|
|
|
|
|
|
|
local sourceFunction = luau.load(EMPTY_LUAU_CODE_BLOCK, { debugName = CUSTOM_SOURCE_BLOCK_NAME })
|
|
|
|
local sourceFunctionDebugName = debug.info(sourceFunction, "s")
|
|
|
|
|
|
|
|
assert(
|
|
|
|
string.find(sourceFunctionDebugName, CUSTOM_SOURCE_BLOCK_NAME),
|
|
|
|
"expected source block name for 'luau.load' to return a custom debug name"
|
|
|
|
)
|
|
|
|
|
2024-06-05 18:02:48 +01:00
|
|
|
local loadSuccess = pcall(function()
|
2023-08-10 20:38:25 +01:00
|
|
|
luau.load(luau.compile(RETURN_LUAU_CODE_BLOCK))
|
|
|
|
end)
|
|
|
|
|
2024-06-05 18:02:48 +01:00
|
|
|
assert(loadSuccess, "expected `luau.load` to be able to process the result of `luau.compile`")
|
2023-10-04 03:03:47 +01:00
|
|
|
|
|
|
|
local CUSTOM_SOURCE_WITH_FOO_FN = "return foo()"
|
|
|
|
|
|
|
|
-- NOTE: We use newproxy here to make a userdata to ensure
|
|
|
|
-- we get the *exact* same value sent back, not some copy
|
|
|
|
local fooValue = newproxy(false)
|
|
|
|
local fooFn = luau.load(CUSTOM_SOURCE_WITH_FOO_FN, {
|
|
|
|
environment = {
|
|
|
|
foo = function()
|
|
|
|
return fooValue
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
local fooFnRet = fooFn()
|
|
|
|
assert(fooFnRet == fooValue, "expected `luau.load` with custom environment to return proper values")
|
|
|
|
|
2024-06-05 18:02:48 +01:00
|
|
|
local fooValue2 = newproxy(false)
|
|
|
|
local fooFn2 = luau.load(CUSTOM_SOURCE_WITH_FOO_FN, {
|
|
|
|
environment = {
|
|
|
|
foo = function()
|
|
|
|
return fooValue2
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
enableGlobals = false,
|
|
|
|
})
|
|
|
|
|
|
|
|
local fooFn2Ret = fooFn2()
|
|
|
|
assert(
|
|
|
|
fooFn2Ret == fooValue2,
|
|
|
|
"expected `luau.load` with custom environment and no default globals to still return proper values"
|
|
|
|
)
|
|
|
|
|
2023-10-04 03:03:47 +01:00
|
|
|
local CUSTOM_SOURCE_WITH_PRINT_FN = "return print()"
|
|
|
|
|
2024-06-05 18:02:48 +01:00
|
|
|
-- NOTE: Testing overriding the print function
|
|
|
|
local overriddenPrintValue1 = newproxy(false)
|
|
|
|
local overriddenPrintFn1 = luau.load(CUSTOM_SOURCE_WITH_PRINT_FN, {
|
2023-10-04 03:03:47 +01:00
|
|
|
environment = {
|
|
|
|
print = function()
|
2024-06-05 18:02:48 +01:00
|
|
|
return overriddenPrintValue1
|
2023-10-04 03:03:47 +01:00
|
|
|
end,
|
|
|
|
},
|
2024-06-05 18:02:48 +01:00
|
|
|
enableGlobals = true,
|
2023-10-04 03:03:47 +01:00
|
|
|
})
|
|
|
|
|
2024-06-05 18:02:48 +01:00
|
|
|
local overriddenPrintFnRet1 = overriddenPrintFn1()
|
2023-10-04 03:03:47 +01:00
|
|
|
assert(
|
2024-06-05 18:02:48 +01:00
|
|
|
overriddenPrintFnRet1 == overriddenPrintValue1,
|
2023-10-04 03:03:47 +01:00
|
|
|
"expected `luau.load` with overridden environment to return proper values"
|
|
|
|
)
|
|
|
|
|
2024-06-05 18:02:48 +01:00
|
|
|
local overriddenPrintValue2 = newproxy(false)
|
|
|
|
local overriddenPrintFn2 = luau.load(CUSTOM_SOURCE_WITH_PRINT_FN, {
|
2023-10-04 03:03:47 +01:00
|
|
|
environment = {
|
2024-06-05 18:02:48 +01:00
|
|
|
print = function()
|
|
|
|
return overriddenPrintValue2
|
|
|
|
end,
|
2023-10-04 03:03:47 +01:00
|
|
|
},
|
2024-06-05 18:02:48 +01:00
|
|
|
enableGlobals = false,
|
2023-10-04 03:03:47 +01:00
|
|
|
})
|
|
|
|
|
2024-06-05 18:02:48 +01:00
|
|
|
local overriddenPrintFnRet2 = overriddenPrintFn2()
|
|
|
|
assert(
|
|
|
|
overriddenPrintFnRet2 == overriddenPrintValue2,
|
|
|
|
"expected `luau.load` with overridden environment and disabled default globals to return proper values"
|
|
|
|
)
|
|
|
|
|
|
|
|
-- NOTE: Testing whether injectGlobals works
|
|
|
|
local CUSTOM_SOURCE_WITH_DEFAULT_FN = "return string.lower(...)"
|
|
|
|
|
|
|
|
local lowerFn1 = luau.load(CUSTOM_SOURCE_WITH_DEFAULT_FN, {
|
|
|
|
environment = {},
|
|
|
|
injectGlobals = false,
|
|
|
|
})
|
|
|
|
|
|
|
|
local lowerFn1Success = pcall(lowerFn1, "LOWERCASE")
|
|
|
|
|
|
|
|
assert(
|
|
|
|
not lowerFn1Success,
|
|
|
|
"expected `luau.load` with injectGlobals = false and empty custom environment to not contain default globals"
|
|
|
|
)
|
|
|
|
|
|
|
|
local lowerFn2 = luau.load(CUSTOM_SOURCE_WITH_DEFAULT_FN, {
|
|
|
|
environment = { string = string },
|
|
|
|
injectGlobals = false,
|
|
|
|
})
|
|
|
|
|
|
|
|
local lowerFn2Success, lowerFn2Result = pcall(lowerFn2, "LOWERCASE")
|
|
|
|
|
|
|
|
assert(
|
|
|
|
lowerFn2Success and lowerFn2Result == "lowercase",
|
|
|
|
"expected `luau.load` with injectGlobals = false and valid custom environment to return proper values"
|
|
|
|
)
|
|
|
|
|
|
|
|
local lowerFn3 = luau.load(CUSTOM_SOURCE_WITH_DEFAULT_FN, {
|
|
|
|
environment = {},
|
|
|
|
injectGlobals = true,
|
|
|
|
})
|
|
|
|
|
|
|
|
local lowerFn3Success, lowerFn3Result = pcall(lowerFn3, "LOWERCASE")
|
|
|
|
|
2023-10-04 03:03:47 +01:00
|
|
|
assert(
|
2024-06-05 18:02:48 +01:00
|
|
|
lowerFn3Success and lowerFn3Result == "lowercase",
|
|
|
|
"expected `luau.load` with injectGlobals = true and empty custom environment to return proper values"
|
2023-10-04 03:03:47 +01:00
|
|
|
)
|