You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
seems like there is no convenient function to preload entire LUA files into the current State.
There is a ISSUE-119 showing a workaround, but I would really love to see this feature provided by the LState instance inself
For example a look like this wold do the job:
func (ls*LState) PreloadFile(path, namestring) error {
fmod, err:=ls.LoadFile(path)
iferr!=nil {
returnerr
}
preload:=ls.GetField(ls.GetField(ls.Get(EnvironIndex), "package"), "preload")
if_, ok:=preload.(*LTable); !ok {
ls.RaiseError("package.preload must be a table")
}
ls.SetField(preload, name, fmod)
returnnil
}
By leveraging the code above, we could outsource common logic into LUA "utility" files and load them by multiple other ones.
For example:
1) outsource complex computations: utils.lua
localutils= {}
functionutils.addtwo(a, b)
returna+bendreturnutils
2) preload them within the main.go
funcmain() {
// ...// preload some other local lua fileiferr:=L.PreloadFile("./lua_scripts/utils.lua", "utils"); err!=nil {
panic(err)
}
// ...
}
3) Use the functions in other lua files mainlogic.lua
This discussion was converted from issue #435 on June 18, 2023 17:26.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi Gopher-lua team,
seems like there is no convenient function to preload entire LUA files into the current State.
There is a ISSUE-119 showing a workaround, but I would really love to see this feature provided by the LState instance inself
For example a look like this wold do the job:
By leveraging the code above, we could outsource common logic into LUA "utility" files and load them by multiple other ones.
For example:
1) outsource complex computations:
utils.lua
2) preload them within the main.go
3) Use the functions in other lua files
mainlogic.lua
What do you think about this feature request?
Beta Was this translation helpful? Give feedback.
All reactions