Skip to content

Instantly share code, notes, and snippets.

@anonhostpi
Last active September 5, 2025 02:25
Show Gist options
  • Save anonhostpi/c82d294d7999d875c820e3b2094998e9 to your computer and use it in GitHub Desktop.
Save anonhostpi/c82d294d7999d875c820e3b2094998e9 to your computer and use it in GitHub Desktop.
Wasm in PowerShell
# iex (iwr 'https://gist.githubusercontent.com/anonhostpi/c82d294d7999d875c820e3b2094998e9/raw/wasm.ps1').Content
$engine = & {
$package = "Wasmtime"
$runtime = & (nmo {iex "using namespace System.Runtime.InteropServices" }) {
$runtime = @{}
$is = { param([OSPlatform] $platform); return ([RuntimeInformation]::IsOSPlatform($platform)) }
$runtime.OS = switch ($true) {
(& $is ([OSPlatform]::Windows)) { "win" }
(& $is ([OSPlatform]::Linux)) { "linux" }
(& $is ([OSPlatform]::OSX)) { "osx" }
}
$runtime.Arch = switch ([RuntimeInformation]::ProcessArchitecture) {
"X64" { "x64" }
"Arm64" { "arm64" }
}
$runtime.Full = @($runtime.OS, $runtime.Arch) -join "-"
return $runtime
}
$package = Get-Package -Name $package -ErrorAction Stop
$directory = $package.Source | Split-Path
$native = "$directory\runtimes\$($runtime.Full)\native\*wasmtime*" | Resolve-Path
switch( $runtime.OS ) {
"win" { $env:PATH += ";$($native | Split-Path)" }
"linux" { $env:LD_LIBRARY_PATH = "$($native | Split-Path):$env:LD_LIBRARY_PATH" }
"osx" { $env:DYLD_LIBRARY_PATH = "$($native | Split-Path):$env:DYLD_LIBRARY_PATH" }
}
Add-Type -Path "$directory\lib\netstandard2.1\Wasmtime.Dotnet.dll"
return [Wasmtime.Engine]::new()
}
$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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment