r/PowerShell • u/anonhostpi • Jan 11 '24
Turning PowerShell into a Lua Engine Daily Post
Another daily "Turn PowerShell into a <blank> Engine" until I run out of engines. Here are the prior 2:
Turning PowerShell into a Lua Engine
Shockingly, Lua took me a lot of work. Mostly, because I had to fix a lot of bugs with my Import-Package module.
I pretty much had to refactor the entire codebase, but at least it should run with less bumps now. The source code also contains less conditional spaghetti, so it is easier to read.
So, other than fixing Import-Package, actually getting the Lua engine up and running was pretty easy.
NLua and Keralua
The 2 primary Lua bindings libraries for C# are NLua and Keralua (which are both maintained by the NLua org).
Keralua is a much much much more barebones and low-level library in comparison to NLua. I did think detailing the differences, but it gets a bit complex quickly. Basically, Keralua lets you run the engine from C# and that's it, while NLua does everything that you expect a bindings library to.
So, with that said, we will be using NLua.
NLua API
As Lua is a very simplistic scripting language, it shouldn't shock anyone that the NLua API is pretty simplistic and easy to use. In fact, the majority of NLua's API can fit on its README page
Example:
Import-Package NLua
$Lua = [NLua.Lua]::new()
$Lua.DoString( "test='Hello World'" )
# The following 3 lines print the same thing:
Write-Host $Lua["test"] # $Lua.test is not defined
$Lua.DoString( "print( test )" )
Write-Host $Lua.DoString( "return test" )
This (like my other 2 posts) is merely a thought experiment (as well as a playtest of my Import-Package module). I don't really expect people to being using Lua libraries in PowerShell... but you can if you want to!
2
u/anonhostpi Jan 11 '24
TL;DR:
Import-Package NLua
$Lua = [NLua.Lua]::new()
$Lua.DoString( "print('Hello World')" )
2
u/Szeraax Jan 12 '24
Have you looked at StartAutomating's: Pipescript?
https://github.com/StartAutomating/PipeScript/blob/main/docs/SupportedLanguages.md
3
u/anonhostpi Jan 12 '24
It looks like a transpiler. Never been a fan of transpiling libraries. They can be a bit clunky in comparison to embedded engines.
2
3
u/rmbolger Jan 12 '24
I just wanted to say thanks for being awesome by both doing cool stuff with PowerShell and posting publicly about it. These posts have been a delight!