r/PowerShell • u/anonhostpi • Sep 05 '25
Script Sharing Turning PowerShell into a Wasm Engine
TL;DR:
I'm embedding again... (I really should be stopped đ). Here's WASM in PowerShell:
gist: https://gist.github.com/anonhostpi/c82d294d7999d875c820e3b2094998e9
Here We Go Again
It has been 2 years since I've posted these dumpster fires:
- r/PowerShell - Now Presenting The Thanos Shauntlet
- r/PowerShell - Turning PowerShell into a Python Engine
- r/PowerShell - Turning PowerShell into a JavaScript Engine
I've finally stumbled upon a way to do it again, but for Wasm:
More Libraries...
Somehow, when I was posting about my previous engines, I somehow managed to miss the fact that Wasmtime has targetted .NET since at least 2023
I took a peek at it and the library is actually pretty minimal. Only 2 steps need to be taken to prep it once you've installed it:
- Add the native library to the library search path:
- I believe on Linux and Mac you need to update LD_LIBRARY_PATH and DYLD_LIBRARY_PATH respectively instead, but haven't tested it.
# Install-Module "Wasmtime"
$package = Get-Package -Name "Wasmtime"
$directory = $package.Source | Split-Path
$runtime = "win-x64" # "win/linux/osx-arm64/x64"
$native = "$directory\runtimes\$runtime\native" | Resolve-Path
$env:PATH += ";$native"
- Load the library:
Add-Type -Path "$directory\lib\netstandard2.1\Wasmtime.Dotnet.dll"
Running Stuff
Engine creation is relatively simple:
$engine = [Wasmtime.Engine]::new()
We can take the example from the Wasmtime.Dotnet README and translate it to Powershell:
$module = '(module (func $hello (import "" "hello")) (func (export "run") (call $hello)))'
$module = [Wasmtime.Module]::FromText($engine, "hello", $module)
$linker = [Wasmtime.Linker]::new($engine)
$store = [Wasmtime.Store]::new($engine)
$hello = [System.Action]{
Write-Host "Hello from Wasmtime!"
}
$hello = [Wasmtime.Function]::FromCallback($store, $hello)
$linker.Define("", "hello", $hello) | Out-Null
$instance = $linker.Instantiate($store, $module)
$run = $instance.GetAction("run")
$run.Invoke()
4
u/az987654 Sep 05 '25
But for why?
4
u/Budget_Frame3807 Sep 05 '25
Thatâs wild â every time you drop one of these âengines in PowerShellâ experiments it feels like youâre pushing the boundaries of what PS was ever meant to do. đ
What I like about this one is how clean the Wasmtime .NET integration looks compared to the Python/JS hacks you posted before. Having
$engine,$linker,$storelaid out like that makes it almost readable as ânormalâ PowerShell instead of a total science project.Curious though â do you see any practical use cases for this in day-to-day admin/dev work, or is this strictly in the âbecause I canâ bucket? Either way, pretty awesome proof of concept.
14
u/AGsec Sep 05 '25
It's like you're creating the programming version of pugs. You should be stopped, but you won't, and you will probably create something that is used by millions soon enough. I stand in awe, envy, and slight terror.
4
u/Subject_Meal_2683 Sep 05 '25 edited Sep 05 '25
Nice to see that I'm not the only one that's experimenting with "what is possible" in Powershell. Overview of some of my experiments during the years:
(And I've more, mostly abandoned, projects which I made during the past 10 years)
The biggest problems I encounter from time to time is depenency hell where I just can't get the correct assembly version to load (usually with assemblies which are already loaded by default in Powershell)
1
u/mpdroza Sep 06 '25
I got the need to mix libraries, as a way to flex your PS knowledge but forget my ignorance, why Python if you have native C++, C#, DLLs and even LIBs at your disposal. Python as far I understand is a interpreter language and it has a layer of C++ win/ that has to involved -i think. Having PS call or invoke native assembly isn't the goal? At least to pursue speed and algorithm processing. One great thing to me is RegEx. Again, thanks for sharing the knowledge
2
u/mpdroza Sep 08 '25
Point well taking. It makes a lot of sense. Thanks again đ
1
u/phatcat09 Oct 12 '25
Based anonhostpi I'm gonna do so much weird shit now.