Repository navigation
Description
This is slightly premature, given that it assumes #3169 , but I want to lay out a possible roadmap for improving the DSL/DynamicKeyword functionality in that PR.
#3169 provides C# syntax for specifying a dynamic keyword syntax, but achieves the actual keyword functionality by invoking PowerShell functions of the form DslModuleName\DslKeywordName. This works by rewriting the AST at compile time so that what the user has written as keyword invocations get turned into function calls, where the "bodies" of keywords (scriptblocks or hashtables) get passed in as arguments to the function (essentially DSLs are syntactic sugar for nested function calls). This works well, but some users may prefer to write everything within the same file, just like Cmdlets. There are a few steps to make here.
Work out just how much we can change the public, but relatively unused, DynamicKeyword API
At the moment, this looks like:
$kw = [System.Management.Automation.DynamicKeyword]::new()
$kw.Keyword = "MyKeyword"
$kw.PostParse = { ... }
...
[System.Management.Automation.DynamicKeyword]::AddKeyword($kw)This API is currently available through and stored by a thread-static variable (as seen above). However, because keywords are specific to a parser session, they are likely better stored as parser-local. This would be a breaking change, but with a good cause -- especially since PowerShell currently keeps the parser and the REPL in different threads. While this API might always have a use, it might be nice to prefer a more fluent way to define DSL keywords within PowerShell (such as a keyword-defining DSL).
Update DynamicKeywordStatementRule
Currently this is relatively specific for DSC keywords, and in particular has special logic for names in keywords, and does not allow keywords to use parameters -- parameters passed to a scriptblock- or hashtable-bodied keyword will be ignored. For a suggestion on how an update to this might look, see a rough prototype I wrote.
Separate DSC keywords from DynamicKeywords
This class is currently relatively specific to DSC keywords, and might be better as a base type that DSC keywords can inherit from. It contains assumptions such as ResourceName, as well as an extension class (DynamicKeywordExtension) which would be a good candidate for features on a DscKeyword class.
Break up the DynamicKeyword class
Currently DynamicKeyword holds information like keyword name and parameter type constraints, that can be read in without loading an assembly, as well as delegates like SemanticCheck that do require an assembly load. Moving to a pure C# implementation without changing DynamicKeyword might result in it becoming a catch-all monolith. Instead, breaking it up might pay some dividends:
- A
DynamicKeywordMetadataclass could hold any information that can be obtained without loading the keyword's containing assembly. Ideally this class would be immutable after construction like other elements in the AST. This could inherit fromCommandMetadata, containing things likeParameterMetadata. - A
DynamicKeywordInfoclass, just likeCmdletInfowould contain information evaluated from loading the user-defined type that defines a keyword, and would inherit fromCommandInfo. This would similarly be immutable, but more aimed at storing parse-time delegates and being used at compile-time. For a rough prototype, see here. DynamicKeyworditself would perhaps be better repurposed in the namespace in the same way asCmdlet, so as the abstract base class that user-defined keywords inherit from, with virtual methods to provide useful abstractions over the complexity of the PowerShell internals. A benefit here is that DSC keywords could be defined by inheriting fromDscKeyword. Again, see here.- Finally, the state managed in the static part of
DynamicKeywordshould be taken out and managed with structures likeDynamicKeywordScopeandDynamicKeywordRuntimeStateand moved to be parser-local. This is already in Dynamic keyword metadata reader #3169 here and here,
Compiling the C# keyword
This seemed originally to be a hard problem (and may still be), since users must be able to specify when the expressions they pass to their keywords get evaluated -- in case they are side-effectful. Meaning that arbitrary PowerShell expressions that may reference runtime state (like $y + Get-Result -Param ($z+1)) must be passed into the keyword functions written in C# without executing them, and then executed at a time when the C# code prescribes.
(Consider in Pester: I can put a BeforeAll block after all other keywords, but if the keywords above it are executed before it, they might not have the state set up that they need.)
This is solved in the current implementation by passing the body as an expression to the function that implements the keyword -- essentially canning the keyword body like a thunk so that the implementing user can choose when (of if) to evaluate it.
I think a similar method of creating a C# ScriptBlock object for keywords to take in as an argument might work well, which would effectively manage the problem of how state set up by outer keywords is communicated to inner keywords. But it certainly bodes discussion.
Passing parameters to keywords
This is the actual hard problem. In order to use the parameter binding algorithm to set parameters on keywords implemented as C#/.NET objects, we need to use the parameter binder logic to make sure that parameters passed to keywords work and are resolved in exactly the same way as parameters passed to any other PowerShell command -- that users' expectations are met.
From what I've read in the code so far, much of the existing logic is spread across ParameterBinderBase, ParameterBinderController, CmdletParameterBinderController and several others. My understanding is that there are two parameter binders in use. ReflectionParameterBinder and ScriptParameterBinder do most of the heavy lifting with some common code in the base class, and PseudoParameterBinder uses a combination of the two internally, while NativeCommandParameterBinder does its best to just pass arguments through as strings. The difficulty arises when CommandProcess assumes that the command invoking it could only be a ScriptBlock or a Cmdlet. However, this might be easier to fix than I think, or more likely I am wrong about how this all works.
This is complicated by the fact that the parameters on keywords (not their values) are known at compile time -- so it's less complicated to perform parameter and parameter type checks than for Cmdlets.
For what it's worth, I have a small start on a C#-keyword parameter binder that doesn't work, but similarly doesn't crash PowerShell (I consider this an achievement...). It seems like the alternatives are:
- Rewrite some of the current parameter binder logic so that base classes make less assumptions about subclasses and also provide more of the parameter binding logic in a generic way.
- Partially re-implement the parameter binding algorithm in a new subclass of
ParameterBinderBaseand only touch what needs to be changed in the existing parts of code likeCommandProcessor.
This is as far as I've gotten with moving dynamic keywords to a pure C# implementation -- and I wanted to record it for discussion.
Activity
rjmholt commentedon Mar 3, 2017
I might reformat this later to make it more readable...
rjmholt commentedon Apr 13, 2018
Closing this in favour of further discussion of how we implement DSLs in PowerShell.