Skip to content

Instantly share code, notes, and snippets.

Created February 26, 2015 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/572f42ba0ea7eec4f721 to your computer and use it in GitHub Desktop.
Save anonymous/572f42ba0ea7eec4f721 to your computer and use it in GitHub Desktop.
This PowerShell code works on PowerShell v1 - v4. It does not run in PowerShell v5 (specifically have tested on Win10 Enterprise x64 build 9926). The PS interpreter freezes with high CPU.
function New-PSClass([string] $ClassName, [scriptblock] $definition, $Inherit) {
function property([string]$name, [scriptblock] $get, [scriptblock] $set, [switch]$private, [switch]$override, [switch] $static ) {
$class.Properties[$name] = @{Name=$name;GetScript=$get;SetScript=$set;Private=$private;Override=$override}
}
$class = New-Object Management.Automation.PSObject
Attach-PSNote $class Properties @{}
Attach-PSScriptMethod $class AttachTo {
function AttachAndInit($instance, [array]$params) {
$instance = __PSClass-AttachObject $this $instance
__PSClass-Initialize $this $instance $params # Executing this line - calling this dummy function - is what caused PowerShell to freeze
}
[array]$params = $Args[1]
AttachAndInit $Args[0] $params
}
Attach-PSScriptMethod $class New {
$instance = new-object Management.Automation.PSObject
$this.AttachTo($instance, $Args)
}
Attach-PSScriptMethod $class InvokeProperty {
__PSClass-InvokePropertyMethod $this $Args[0] $Args[1] $Args[2] $Args[3] # I would love to remove this pointless function call, but then I can't repro...
}
$output = &$definition
$class
}
function __PSClass-Initialize($class, $instance, $params) {} # I would love to remove this pointless function, but then I can't repro...
function __PSClass-AttachObject($class, [PSObject] $instance) {
Attach-PSNote $instance Class $class
foreach ($key in $Class.Properties.keys) {
$Property = $Class.Properties[$key]
$targetObject = $instance
$ObjectString = '$this'
$instanceScriptText = $ObjectString + '.Class.InvokeProperty( "GET", "' + $Property.name + '", ' + $ObjectString + ', $Args )'
$getScript = $ExecutionContext.InvokeCommand.NewScriptBlock( $instanceScriptText )
$setScript = $null
Attach-PSProperty $targetObject $Property.Name $getScript $setScript -override:$Property.override
}
$instance
}
function __PSClass-InvokeScript($class, $script, $object, [array]$params) {} # I would love to remove this pointless function, but then I can't repro...
function __PSClass-InvokePropertyMethod($Class, $PropertyType, $PropertyName, $instance, [array]$params) {
__PSClass-InvokeScript $FoundClass $property.SetScript $instance $params # I would love to remove this pointless function call, but then I can't repro...
}
function Attach-PSNote([PSObject]$object, [string]$name, $value) {
$member = new-object management.automation.PSNoteProperty $name, $value
$object.psobject.members.Add($member)
}
function Attach-PSScriptMethod([PSObject]$object, [string]$name, [scriptblock] $script, [switch] $override) {
$member = New-Object management.automation.PSScriptMethod $name, $script
$object.psobject.members.Add($member)
}
function Attach-PSProperty([PSObject]$object, [string]$name, [scriptblock] $get, [scriptblock] $set, [switch] $override) {
$scriptProperty = New-Object management.automation.PsScriptProperty $name, $get
$object.psobject.properties.add($scriptProperty)
}
$AnimalClass = New-PSClass Animal { property Legs {"None"} }
$Animal = $AnimalClass.New()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment