Esempio n. 1
0
        public async Task <DebugEvaluationResult> EvaluateAsync(string rScript)
        {
            // One eval at a time
            await _sem.WaitAsync();

            try {
                var debugSession = await _debugSessionProvider.GetDebugSessionAsync(Session);

                var frames = await debugSession.GetStackFramesAsync();

                var frame = frames.FirstOrDefault(f => f.Index == 0);

                const DebugEvaluationResultFields fields = DebugEvaluationResultFields.Classes
                                                           | DebugEvaluationResultFields.Expression
                                                           | DebugEvaluationResultFields.TypeName
                                                           | (DebugEvaluationResultFields.Repr | DebugEvaluationResultFields.ReprStr)
                                                           | DebugEvaluationResultFields.Dim
                                                           | DebugEvaluationResultFields.Length;
                var result = await frame.EvaluateAsync(rScript, fields);

                var globalResult = await frame.EvaluateAsync("base::environment()", fields);

                _globalEnv = new EvaluationWrapper(globalResult);

                return(result);
            } finally {
                _sem.Release();
            }
        }
Esempio n. 2
0
        private async Task UpdateList()
        {
            if (_updating)
            {
                return;
            }

            try {
                _updating = true;
                // May be null in tests
                var sessionProvider = EditorShell.Current.ExportProvider.GetExportedValueOrDefault <IRSessionProvider>();
                var session         = sessionProvider.GetOrCreate(GuidList.InteractiveWindowRSessionGuid, null);
                if (session.IsHostRunning)
                {
                    var debugSessionProvider = EditorShell.Current.ExportProvider.GetExportedValueOrDefault <IDebugSessionProvider>();
                    if (debugSessionProvider != null)
                    {
                        var debugSession = await debugSessionProvider.GetDebugSessionAsync(Session);

                        if (debugSession != null)
                        {
                            var stackFrames = await debugSession.GetStackFramesAsync();

                            var globalStackFrame = stackFrames.FirstOrDefault(s => s.IsGlobal);
                            if (globalStackFrame != null)
                            {
                                const DebugEvaluationResultFields fields =
                                    DebugEvaluationResultFields.Expression |
                                    DebugEvaluationResultFields.Kind |
                                    DebugEvaluationResultFields.ReprStr |
                                    DebugEvaluationResultFields.TypeName |
                                    DebugEvaluationResultFields.Classes |
                                    DebugEvaluationResultFields.Length |
                                    DebugEvaluationResultFields.SlotCount |
                                    DebugEvaluationResultFields.AttrCount |
                                    DebugEvaluationResultFields.Dim |
                                    DebugEvaluationResultFields.Flags;
                                DebugEvaluationResult evaluation = await globalStackFrame.EvaluateAsync("base::environment()", "Global Environment", fields);

                                var e = new RSessionDataObject(evaluation);  // root level doesn't truncate children and return every variables

                                _topLevelVariables.Clear();

                                var children = await e.GetChildrenAsync();

                                if (children != null)
                                {
                                    foreach (var x in children)
                                    {
                                        _topLevelVariables[x.Name] = x; // TODO: BUGBUG: this doesn't address removed variables
                                    }
                                }
                            }
                        }
                    }
                }
            } finally {
                _updating = false;
            }
        }
Esempio n. 3
0
        protected virtual async Task <IReadOnlyList <IRSessionDataObject> > GetChildrenAsyncInternal()
        {
            List <IRSessionDataObject> result = null;

            var valueEvaluation = DebugEvaluation as DebugValueEvaluationResult;

            if (valueEvaluation == null)
            {
                Debug.Assert(false, $"{nameof(RSessionDataObject)} result type is not {typeof(DebugValueEvaluationResult)}");
                return(result);
            }

            if (valueEvaluation.HasChildren)
            {
                await TaskUtilities.SwitchToBackgroundThread();

                const DebugEvaluationResultFields fields =
                    DebugEvaluationResultFields.Expression |
                    DebugEvaluationResultFields.Kind |
                    DebugEvaluationResultFields.ReprStr |
                    DebugEvaluationResultFields.TypeName |
                    DebugEvaluationResultFields.Classes |
                    DebugEvaluationResultFields.Length |
                    DebugEvaluationResultFields.SlotCount |
                    DebugEvaluationResultFields.AttrCount |
                    DebugEvaluationResultFields.Dim |
                    DebugEvaluationResultFields.Flags;
                IReadOnlyList <DebugEvaluationResult> children = await valueEvaluation.GetChildrenAsync(fields, MaxChildrenCount, MaxReprLength);

                result = EvaluateChildren(children);
            }

            return(result);
        }
Esempio n. 4
0
 public Task <DebugEvaluationResult> GetEnvironmentAsync(
     DebugEvaluationResultFields fields  = DebugEvaluationResultFields.Expression | DebugEvaluationResultFields.Length | DebugEvaluationResultFields.AttrCount,
     CancellationToken cancellationToken = default(CancellationToken)
     )
 {
     return(EvaluateAsync("base::environment()", fields: fields, cancellationToken: cancellationToken));
 }
Esempio n. 5
0
        private async Task SetRootModelAsync(REnvironment env)
        {
            await TaskUtilities.SwitchToBackgroundThread();

            var debugSession = await GetDebugSessionAsync();

            var frames = await debugSession.GetStackFramesAsync();

            var frame = frames.FirstOrDefault(f => f.Index == 0);

            if (frame != null)
            {
                const DebugEvaluationResultFields fields = DebugEvaluationResultFields.Classes
                                                           | DebugEvaluationResultFields.Expression
                                                           | DebugEvaluationResultFields.TypeName
                                                           | (DebugEvaluationResultFields.Repr | DebugEvaluationResultFields.ReprStr)
                                                           | DebugEvaluationResultFields.Dim
                                                           | DebugEvaluationResultFields.Length;
                DebugEvaluationResult result = await frame.EvaluateAsync(
                    GetExpression(env),
                    fields);

                var wrapper       = new EvaluationWrapper(result);
                var rootNodeModel = new VariableNode(_settings, wrapper);

                VsAppShell.Current.DispatchOnUIThread(
                    () => {
                    _rootNode.Model = rootNodeModel;
                });
            }
        }
Esempio n. 6
0
 public Task <DebugEvaluationResult> EvaluateAsync(
     string expression,
     DebugEvaluationResultFields fields,
     CancellationToken cancellationToken = default(CancellationToken)
     )
 {
     return(EvaluateAsync(null, expression, null, null, fields, null, cancellationToken));
 }
Esempio n. 7
0
 public Task <DebugEvaluationResult> EvaluateAsync(
     string expression,
     DebugEvaluationResultFields fields,
     int?reprMaxLength = null,
     CancellationToken cancellationToken = default(CancellationToken)
     )
 {
     return(EvaluateAsync(expression, null, fields, reprMaxLength, cancellationToken));
 }
Esempio n. 8
0
 public Task<DebugEvaluationResult> EvaluateAsync(
     string expression,
     string name = null,
     DebugEvaluationResultFields fields = DebugEvaluationResultFields.All,
     int? reprMaxLength = null,
     CancellationToken cancellationToken = default(CancellationToken)
 ) {
     return Session.EvaluateAsync(this, expression, name, null, fields, reprMaxLength, cancellationToken);
 }
Esempio n. 9
0
 public Task <DebugEvaluationResult> EvaluateAsync(
     string expression,
     string name = null,
     DebugEvaluationResultFields fields = DebugEvaluationResultFields.All,
     int?reprMaxLength = null,
     CancellationToken cancellationToken = default(CancellationToken)
     )
 {
     return(Session.EvaluateAsync(this, expression, name, null, fields, reprMaxLength, cancellationToken));
 }
Esempio n. 10
0
        public static string ToRVector(this DebugEvaluationResultFields fields)
        {
            if (fields == DebugEvaluationResultFields.All)
            {
                return(null);
            }

            var fieldNames = _mapping.Where(kv => fields.HasFlag(kv.Key)).Select(kv => "'" + kv.Value + "'");

            return(Invariant($"base::c({string.Join(", ", fieldNames)})"));
        }
Esempio n. 11
0
        public async Task <IReadOnlyList <DebugEvaluationResult> > GetChildrenAsync(
            DebugEvaluationResultFields fields,
            int?maxLength     = null,
            int?reprMaxLength = null,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            await TaskUtilities.SwitchToBackgroundThread();

            if (StackFrame == null)
            {
                throw new InvalidOperationException("Cannot retrieve children of an evaluation result that is not tied to a frame.");
            }
            if (Expression == null)
            {
                throw new InvalidOperationException("Cannot retrieve children of an evaluation result that does not have an associated expression.");
            }

            var call      = Invariant($@"rtvs:::toJSON(rtvs:::describe_children(
                {Expression.ToRStringLiteral()}, {StackFrame.SysFrame}, 
                {fields.ToRVector()}, {maxLength}, {reprMaxLength}))");
            var jChildren = await StackFrame.Session.InvokeDebugHelperAsync <JArray>(call, cancellationToken);

            Trace.Assert(
                jChildren.Children().All(t => t is JObject),
                Invariant($"rtvs:::describe_children(): object of objects expected.\n\n{jChildren}"));

            var children = new List <DebugEvaluationResult>();

            foreach (var child in jChildren)
            {
                var childObject = (JObject)child;
                Trace.Assert(
                    childObject.Count == 1,
                    Invariant($"rtvs:::describe_children(): each object is expected contain one object\n\n"));
                foreach (var kv in childObject)
                {
                    var name        = kv.Key;
                    var jEvalResult = (JObject)kv.Value;
                    var evalResult  = Parse(StackFrame, name, jEvalResult);
                    children.Add(evalResult);
                }
            }

            return(children);
        }
Esempio n. 12
0
        public Task <DebugEvaluationResult> EvaluateAsync(
            DebugEvaluationResultFields fields,
            int?reprMaxLength = null,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            if (StackFrame == null)
            {
                throw new InvalidOperationException("Cannot re-evaluate an evaluation result that is not tied to a frame.");
            }
            if (Expression == null)
            {
                throw new InvalidOperationException("Cannot re-evaluate an evaluation result that does not have an associated expression.");
            }

            return(StackFrame.EvaluateAsync(Expression, Name, fields, reprMaxLength, cancellationToken));
        }
Esempio n. 13
0
        protected override async Task <IReadOnlyList <IRSessionDataObject> > GetChildrenAsyncInternal()
        {
            List <IRSessionDataObject> result = null;

            var valueEvaluation = DebugEvaluation as DebugValueEvaluationResult;

            if (valueEvaluation == null)
            {
                Debug.Assert(false, $"{nameof(EvaluationWrapper)} result type is not {typeof(DebugValueEvaluationResult)}");
                return(result);
            }

            if (valueEvaluation.HasChildren)
            {
                await TaskUtilities.SwitchToBackgroundThread();

                const DebugEvaluationResultFields fields =
                    DebugEvaluationResultFields.Expression |
                    DebugEvaluationResultFields.Kind |
                    DebugEvaluationResultFields.ReprStr |
                    DebugEvaluationResultFields.TypeName |
                    DebugEvaluationResultFields.Classes |
                    DebugEvaluationResultFields.Length |
                    DebugEvaluationResultFields.SlotCount |
                    DebugEvaluationResultFields.AttrCount |
                    DebugEvaluationResultFields.Dim |
                    DebugEvaluationResultFields.Flags;
                IReadOnlyList <DebugEvaluationResult> children = await valueEvaluation.GetChildrenAsync(fields, MaxChildrenCount, 100);

                result = new List <IRSessionDataObject>();
                for (int i = 0; i < children.Count; i++)
                {
                    result.Add(new EvaluationWrapper(children[i], index: i, maxChildrenCount: DefaultMaxGrandChildren));
                }

                // return children can be less than value's length in some cases e.g. missing parameter
                if (valueEvaluation.Length > result.Count &&
                    (valueEvaluation.Length > MaxChildrenCount))
                {
                    result.Add(EvaluationWrapper.Ellipsis); // insert dummy child to indicate truncation in UI
                }
            }

            return(result);
        }
Esempio n. 14
0
        public async Task <DebugEvaluationResult> EvaluateAsync(
            DebugStackFrame stackFrame,
            string expression,
            string name,
            string env,
            DebugEvaluationResultFields fields,
            int?reprMaxLength = null,
            CancellationToken cancellationToken = default(CancellationToken)
            )
        {
            ThrowIfDisposed();

            await TaskUtilities.SwitchToBackgroundThread();

            await InitializeAsync(cancellationToken);

            env = env ?? stackFrame?.SysFrame ?? "NULL";
            var code        = Invariant($"rtvs:::toJSON(rtvs:::eval_and_describe({expression.ToRStringLiteral()}, {env},, {fields.ToRVector()},, {reprMaxLength}))");
            var jEvalResult = await InvokeDebugHelperAsync <JObject>(code, cancellationToken);

            return(DebugEvaluationResult.Parse(stackFrame, name, jEvalResult));
        }
Esempio n. 15
0
        private async Task PublishAsync(VariableSubscriptionToken token, IList <VariableSubscription> subscriptions)
        {
            if (subscriptions.Count == 0)
            {
                return;
            }

            Debug.Assert(_debugSession != null);

            var stackFrames = await _debugSession.GetStackFramesAsync();

            var stackFrame = stackFrames.FirstOrDefault(f => f.Index == token.FrameIndex);

            if (stackFrame != null)
            {
                const DebugEvaluationResultFields fields = DebugEvaluationResultFields.Classes
                                                           | DebugEvaluationResultFields.Expression
                                                           | DebugEvaluationResultFields.TypeName
                                                           | (DebugEvaluationResultFields.Repr | DebugEvaluationResultFields.ReprStr)
                                                           | DebugEvaluationResultFields.Dim
                                                           | DebugEvaluationResultFields.Length;

                DebugEvaluationResult evaluation = await stackFrame.EvaluateAsync(token.Expression, fields : fields);

                foreach (var sub in subscriptions)
                {
                    try {
                        var action = sub.GetExecuteAction();
                        if (action != null)
                        {
                            action(evaluation);
                        }
                    } catch (Exception e) {
                        Debug.Fail(e.ToString());
                        // swallow exception and continue
                    }
                }
            }
        }
Esempio n. 16
0
        private async Task EvaluateAsync()
        {
            try {
                await TaskUtilities.SwitchToBackgroundThread();

                var debugSession = await VsAppShell.Current.ExportProvider.GetExportedValue <IDebugSessionProvider>().GetDebugSessionAsync(_rSession);

                const DebugEvaluationResultFields fields = DebugEvaluationResultFields.Classes
                                                           | DebugEvaluationResultFields.Expression
                                                           | DebugEvaluationResultFields.TypeName
                                                           | (DebugEvaluationResultFields.Repr | DebugEvaluationResultFields.ReprStr)
                                                           | DebugEvaluationResultFields.Dim
                                                           | DebugEvaluationResultFields.Length;

                var result = await debugSession.EvaluateAsync(_evaluation.Expression, fields);

                var wrapper = new EvaluationWrapper(result);

                VsAppShell.Current.DispatchOnUIThread(() => SetEvaluation(wrapper));
            } catch (Exception ex) {
                VsAppShell.Current.DispatchOnUIThread(() => SetError(ex.Message));
            }
        }
        public Task<DebugEvaluationResult> EvaluateAsync(
            DebugEvaluationResultFields fields = DebugEvaluationResultFields.All,
            int? reprMaxLength = null,
            CancellationToken cancellationToken = default(CancellationToken)
        ) {
            if (StackFrame == null) {
                throw new InvalidOperationException("Cannot re-evaluate an evaluation result that is not tied to a frame.");
            }
            if (Expression == null) {
                throw new InvalidOperationException("Cannot re-evaluate an evaluation result that does not have an associated expression.");
            }

            return StackFrame.EvaluateAsync(Expression, Name, fields, reprMaxLength, cancellationToken);
        }
Esempio n. 18
0
 public Task<DebugEvaluationResult> GetEnvironmentAsync(
     DebugEvaluationResultFields fields = DebugEvaluationResultFields.Expression | DebugEvaluationResultFields.Length | DebugEvaluationResultFields.AttrCount,
     CancellationToken cancellationToken = default(CancellationToken)
 ) {
     return EvaluateAsync("base::environment()", fields: fields, cancellationToken: cancellationToken);
 }
        public async Task<IReadOnlyList<DebugEvaluationResult>> GetChildrenAsync(
            DebugEvaluationResultFields fields = DebugEvaluationResultFields.All,
            int? maxLength = null,
            int? reprMaxLength = null,
            CancellationToken cancellationToken = default(CancellationToken)
        ) {
            await TaskUtilities.SwitchToBackgroundThread();

            if (StackFrame == null) {
                throw new InvalidOperationException("Cannot retrieve children of an evaluation result that is not tied to a frame.");
            }
            if (Expression == null) {
                throw new InvalidOperationException("Cannot retrieve children of an evaluation result that does not have an associated expression.");
            }

            var call = Invariant($@"rtvs:::toJSON(rtvs:::describe_children(
                {Expression.ToRStringLiteral()}, {StackFrame.SysFrame}, 
                {fields.ToRVector()}, {maxLength}, {reprMaxLength}))");
            var jChildren = await StackFrame.Session.InvokeDebugHelperAsync<JArray>(call, cancellationToken);
            Trace.Assert(
                jChildren.Children().All(t => t is JObject),
                Invariant($"rtvs:::describe_children(): object of objects expected.\n\n{jChildren}"));

            var children = new List<DebugEvaluationResult>();
            foreach (var child in jChildren) {
                var childObject = (JObject)child;
                Trace.Assert(
                    childObject.Count == 1,
                    Invariant($"rtvs:::describe_children(): each object is expected contain one object\n\n"));
                foreach (var kv in childObject) {
                    var name = kv.Key;
                    var jEvalResult = (JObject)kv.Value;
                    var evalResult = Parse(StackFrame, name, jEvalResult);
                    children.Add(evalResult);
                }
            }

            return children;
        }