Skip to content

Conversation

sug44
Copy link
Contributor

@sug44 sug44 commented Jan 17, 2025

This PR adds lua interpreter to kOS. It can be enabled with the "interpreter" field on kOS processors. Here is the documentation for further information. And here is the built release version.

There is the arcane issue #966 discussing lua in kOS. I chose a different approach to implementing lua in kOS than the one discussed in the issue. Instead of using the kerboscript VM I created a higher level binding layer for the functions, bound variables and structures used by it.

With a somewhat unconventional and large PR like this one, as well as the stale state of development, I took the liberty of not following some of the contributing guidelines, mainly not opening a new issue. Feel free to ask any questions, give feedback and discuss the feature here.

sug44 added 30 commits September 8, 2024 15:31
Separated interpreter specific logic with an new IInterpreterLink interface
Created classes KSLink and LuaInterpreter that implement this interface
Added a memeber "InterpreterLink" to SafeSharedObjects to access this new interface
Created a new field "Interpreter" that calls OnInterpreterChanged function that sets Shared.InterpreterLink to the selected interpreter
Atm interpreters can coexist alongside each other
IInterpreter -> ITerminal
Interpreter -> Terminal
SafeSharedObjects.Interpreter -> SafeSharedObjects.Terminal
ConnectivityInterpreter -> ConnectivityTerminal
IInterpreterLink -> IInterpreter
SafeSharedObjects.InterpreterLink -> SafeSharedObjects.Interpreter
KSLink -> KSInterpreter
Make separate boot file fields and boot file selections in editor
Make interpreterLanguage field case insensitive
Swapping is done in one place during kOSProcessor booting. Revert CPU initialization and booting to how it was done before
Userdata type names
Efficient print function
Pairs metamethod for Structures
1 based indexing for Structures in lua
Separate userdata type for kerboscript functions
This is what processor was doing on shutdown before
Couldn't find anything that broke because of this but better safe than sorry
Reverted some changes from a104d58 commit that created a custom stack operator for lua
Now kerboscript and lua use the same stack when calling kerboscript functions and SuffixDelegateResults
Put all types in Types folder
Put function definitions in LuaFunctions.cs file
Support Structure operators
Rename the stage structure getter from "stage" to "stageInfo" to avoid having the same name for the structure and the function. Make loadfile and dofile use the kOS file system. Replace terminal.input.getchar with a function that works for lua. It's a special case because the previous function modified the kerboscript stack after it returned.
"runfileon" function.
Takes two arguments
1) path (works like path in "run")
2) processorId (works like processorId in "processor" function)
Reboots the selected processor with the selected file as bootfile, resets the bootfile after booting. Automatically selects interpreter based on file extension

"runcommandon" function.
Takes two arguments
1) command string
2) processorId (works like processorId in "processor" function)
Calls ProcessCommand(command) on the current interpreter of the selected processor
sug44 added 30 commits December 9, 2024 20:41
Refactor whitelist.lua to make it harder to accidentally expose non whitelisted values
Make kerboscript yields yield all coroutines, not just commandCoroutine.
Remove base `wait` function that uses kerboscript yields.
Replace the `wait` function with a kerboscript-like abstraction using lua `coroutine.yield()`.
Add the `waituntil` function, similar to `wait`.

This fixes update coroutines not being affected by kerboscript yields(`bounds` ship suffix, `stage` function).
Fix some function annotations
Fixes equal priority callbacks changing order after sorting because of the sorting algorithm being unstable
…e instructions hook fields to LuaInterpreter
Because the update function has lower priority than terminal commands no need to reset it.
Move utility functions to Lua.Util class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants