Skip to content

Commit

Permalink
Merge pull request #1487 from sfaqer/feature/bgTasksCallStack
Browse files Browse the repository at this point in the history
Стек вызовов в информации об ошибке фонового задания
  • Loading branch information
EvilBeaver authored Jan 17, 2025
2 parents 9cf7ebe + 6ae2564 commit eb0a580
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/OneScript.StandardLibrary/Tasks/BackgroundTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void ExecuteOnCurrentThread()
catch (ScriptException exception)
{
State = TaskStateEnum.CompletedWithErrors;
exception.RuntimeSpecificInfo = MachineInstance.Current.GetExecutionFrames();
ExceptionInfo = new ExceptionInfoContext(exception);
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/ScriptEngine/Machine/Contexts/StackTraceCollectionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This Source Code Form is subject to the terms of the
using System.Collections.Generic;
using System.Linq;
using OneScript.Contexts;
using OneScript.Exceptions;

namespace ScriptEngine.Machine.Contexts
{
Expand All @@ -30,6 +31,11 @@ internal StackTraceCollectionContext(IEnumerable<ExecutionFrameInfo> frames)
}).ToList();
}

/// <summary>
/// Возвращает количество кадров в стеке вызовов
/// </summary>
/// <returns>Число - Количество кадров в стеке вызовов</returns>
[ContextMethod("Количество", "Count")]
public override int Count()
{
return _frames.Count;
Expand All @@ -39,5 +45,23 @@ public override IEnumerator<StackTraceItemContext> GetEnumerator()
{
return _frames.GetEnumerator();
}

public override bool IsIndexed => true;

public override IValue GetIndexedValue(IValue index)
{
var idx = (int)index.AsNumber();

if (idx < 0 || idx >= Count())
throw IndexOutOfBoundsException();

return _frames[idx];
}

private static RuntimeException IndexOutOfBoundsException()
{
return new RuntimeException("Значение индекса выходит за пределы диапазона");
}

}
}
1 change: 1 addition & 0 deletions src/ScriptEngine/Machine/MachineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,7 @@ private IValue PopRawValue()

public IList<ExecutionFrameInfo> GetExecutionFrames()
{
CreateFullCallstack();
return _fullCallstackCache;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/tasks.os
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ВсеТесты.Добавить("ТестДолжен_ПроверитьЧтоВозвращаетсяРезультатДелегата");
ВсеТесты.Добавить("ТестДолжен_ПроверитьЧтоРаботаетБлокировка");
ВсеТесты.Добавить("ТестДолжен_ПроверитьЧтоКодМожетОпределитьИДЗадания");
ВсеТесты.Добавить("ТестДолжен_ПроверитьЧтоВИнформацииОбОшибкеЕстьСтекВызовов");
ВсеТесты.Добавить("ТестДолжен_ПроверитьЧтоОбработчикиСобытийВызываютсяВФоновомЗадании");

Возврат ВсеТесты;
Expand Down Expand Up @@ -271,6 +272,23 @@

КонецПроцедуры

Процедура ТестДолжен_ПроверитьЧтоВИнформацииОбОшибкеЕстьСтекВызовов() Экспорт

Задание = ФоновыеЗадания.Выполнить(ЭтотОбъект, "ПроцедураСИсключением");
Задание.ОжидатьЗавершения();

СтекВызовов = Задание.ИнформацияОбОшибке.ПолучитьСтекВызовов();

юТест.ПроверитьТип(СтекВызовов, "КоллекцияКадровСтекаВызовов");

юТест.ПроверитьРавенство(СтекВызовов.Количество(), 1);

юТест.ПроверитьРавенство(СтекВызовов[0].Метод, "ПроцедураСИсключением");
юТест.ПроверитьЗаполненность(СтекВызовов[0].НомерСтроки);
юТест.ПроверитьРавенство(СтекВызовов[0].ИмяМодуля, ТекущийСценарий().Источник);

КонецПроцедуры

Процедура ТестДолжен_ПроверитьЧтоОбработчикиСобытийВызываютсяВФоновомЗадании() Экспорт

СобытиеВызвано = Ложь;
Expand Down

0 comments on commit eb0a580

Please sign in to comment.