Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/ScriptEngine/Machine/Contexts/UserScriptContextInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ protected override int GetOwnVariableCount()
return _ownProperties.Count;
}

public int GetOwnPropCount() => GetOwnVariableCount();

protected override void UpdateState()
{
}
Expand Down Expand Up @@ -242,6 +244,18 @@ protected override string GetOwnPropName(int index)

return _ownPropertyIndexes.Where(x => x.Value == index).First().Key;
}

public VariableInfo GetOwnVariableInfo(int index)
{
return new VariableInfo();/* {
Identifier = variable.Identifier,
Type = variable.Type,
Index = variable.Index,
Annotations = HackGetAnnotations(context, variable.Index),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Этот код теперь не нужен? Там вот этот хак с аннотациями кажется зачем-то был полезен

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Уже не нужен, от хака избавились.

А вот здесь же рядом есть несогласованность:

protected override bool IsOwnPropReadable(int index)
{
if (_ownProperties == null)
return false;
- проверяем, возвращаем значение
protected override IValue GetOwnPropValue(int index)
{
return _ownProperties[index];
- вообще не проверяем
protected override string GetOwnPropName(int index)
{
if (_ownProperties == null)
throw new ArgumentException("Unknown property index");
- проверяем и падаем

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я исходил из того, что индексы должны быть заведомо валидные и передавать неверные нельзя. Поэтому не проверяю строго.

IsExport = variable.IsExport
};*/

}

public override int GetMethodsCount()
{
Expand Down
36 changes: 14 additions & 22 deletions src/ScriptEngine/Machine/IRuntimeContextInstance.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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ScriptEngine.Machine.Contexts;

namespace ScriptEngine.Machine
Expand Down Expand Up @@ -58,25 +59,28 @@ public static IEnumerable<VariableInfo> GetProperties(this IRuntimeContextInstan
private static IEnumerable<VariableInfo> GetPropertiesWithPrivate(IRuntimeContextInstance context)
{
if (!(context is UserScriptContextInstance userScript))
return Array.Empty<VariableInfo>();
return GetPropertiesWithoutPrivate(context);

List<VariableInfo> infos = new List<VariableInfo>();
foreach (var variable in userScript.Module.Variables)
var infos = new List<VariableInfo>();
for (int i = 1; i < userScript.GetOwnPropCount(); i++) // skip ThisObject == _ownProperties[0]
{
infos.Add(new VariableInfo() {
Identifier = variable.Identifier,
Type = variable.Type,
Index = variable.Index,
Annotations = HackGetAnnotations(context, variable.Index),
IsExport = variable.IsExport
Identifier = userScript.GetPropName(i),
Type = SymbolType.ContextProperty,
Index = i,
Annotations = Array.Empty<AnnotationDefinition>(),
IsExport = false
});
}

return infos;
return infos.Concat(userScript.Module.Variables);
}

private static IEnumerable<VariableInfo> GetPropertiesWithoutPrivate(IRuntimeContextInstance context)
{
if (context is UserScriptContextInstance userScript)
return userScript.Module.Variables.Where(x => x.IsExport).ToList();

VariableInfo[] infos = new VariableInfo[context.GetPropCount()];
for (int i = 0; i < infos.Length; i++)
{
Expand All @@ -85,26 +89,14 @@ private static IEnumerable<VariableInfo> GetPropertiesWithoutPrivate(IRuntimeCon
Identifier = context.GetPropName(i),
Type = SymbolType.ContextProperty,
Index = i,
Annotations = HackGetAnnotations(context, i),
Annotations = Array.Empty<AnnotationDefinition>(),
IsExport = true
};
}

return infos;
}

private static AnnotationDefinition[] HackGetAnnotations(IRuntimeContextInstance context, int i)
{
if (!(context is UserScriptContextInstance userScript))
return Array.Empty<AnnotationDefinition>();

if (i == 0)
return Array.Empty<AnnotationDefinition>();

var variable = userScript.Module.Variables[i - 1];
return variable.Annotations;
}

public static IValue GetPropValue(this IRuntimeContextInstance context, string propName)
{
int propNum = context.FindProperty(propName);
Expand Down
13 changes: 13 additions & 0 deletions tests/annotations.os
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ВсеТесты.Добавить("ТестДолжен_ПроверитьПолучениеАннотацийПараметров");
ВсеТесты.Добавить("ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарий");
ВсеТесты.Добавить("ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарийИзСтроки");
ВсеТесты.Добавить("ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарийСКонтекстом");

Возврат ВсеТесты;

Expand Down Expand Up @@ -197,4 +198,16 @@

КонецЕсли;

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

Процедура ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарийСКонтекстом() Экспорт

ТекстСценария = "
|Рефлектор = Новый Рефлектор();
|ТаблицаСвойств = Рефлектор.ПолучитьТаблицуСвойств(ЭтотОбъект);
|";
Контекст = Новый Структура( "АргументыКоманднойСтроки", Новый Массив );

Сценарий = ЗагрузитьСценарийИзСтроки(ТекстСценария, Контекст);

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