Exemple #1
0
        public static MulticastDelegate CreateNewClosure(ScriptBlock scriptBlock, Type delegateType)
        {
            if (delegateType == null)
            {
                delegateType = typeof(Func <object>);
            }

            SessionState sessionState = scriptBlock.Module == null
                ? GetTopLevelStateFromTLS()
                : scriptBlock.Module.SessionState;

            var writer = new ClosureRewriter(sessionState);
            var newAst = (ScriptBlockAst)scriptBlock.Ast.Rewrite(writer);
            var newSb  = newAst.GetScriptBlock();
            // if (scriptBlock.Module != null)
            // {
            //     newSb = scriptBlock.Module.NewBoundScriptBlock(newSb);
            // }

            var inherits = new PSVariable[writer._inheritsIndex];

            foreach (Tuple <int, PSVariable> inherit in writer._inherits.Values)
            {
                inherits[inherit.Item1] = inherit.Item2;
            }

            var closure = new Closure(
                inherits,
                new object[writer._localsIndex],
                new object[writer._parametersIndex],
                Array.Empty <CommandInfo>(),
                newSb);

            return(CreateDelegate(newSb, closure, delegateType));
        }
Exemple #2
0
        internal static (ScriptBlock, Closure) CreateNewClosure(ScriptBlock scriptBlock)
        {
            SessionState sessionState = scriptBlock.Module == null
                ? GetTopLevelStateFromTLS()
                : scriptBlock.Module.SessionState;

            var writer   = new ClosureRewriter(sessionState);
            var newAst   = (ScriptBlockAst)scriptBlock.Ast.Rewrite(writer);
            var newSb    = newAst.GetScriptBlock();
            var inherits = new PSVariable[writer._inheritsIndex];

            foreach (Tuple <int, PSVariable> inherit in writer._inherits.Values)
            {
                inherits[inherit.Item1] = inherit.Item2;
            }

            var autos = new object[3];

            if (writer._hasThis)
            {
                autos[2] = sessionState.PSVariable.Get("this")?.Value;
            }

            var closure = new Closure(
                inherits,
                new object[writer._localsIndex],
                new object[writer._parametersIndex],
                Array.Empty <CommandInfo>(),
                newSb,
                autos);

            return(newSb, closure);
        }
 public ScriptBlockEnumerable(ScriptBlock scriptBlock, IEnumerable enumerable)
 {
     _enumerable = enumerable;
     (_scriptBlock, _closure) = ClosureRewriter.CreateNewClosure(scriptBlock);
 }