Skip to content

Commit 9b3f73d

Browse files
authored
Merge pull request #1504 from Mr-Rm/fix/1424
v1 fix #1424: рефлексия свойств у загруженного с контекстом сценария, рефлексия приватных +тест
2 parents ca04f06 + fbaf88e commit 9b3f73d

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/ScriptEngine/Machine/Contexts/UserScriptContextInstance.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ protected override int GetOwnVariableCount()
215215
return _ownProperties.Count;
216216
}
217217

218+
public int GetOwnPropCount() => GetOwnVariableCount();
219+
218220
protected override void UpdateState()
219221
{
220222
}
@@ -242,7 +244,7 @@ protected override string GetOwnPropName(int index)
242244

243245
return _ownPropertyIndexes.Where(x => x.Value == index).First().Key;
244246
}
245-
247+
246248
public override int GetMethodsCount()
247249
{
248250
return GetOwnMethodCount() + _module.Methods.Length;

src/ScriptEngine/Machine/IRuntimeContextInstance.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This Source Code Form is subject to the terms of the
88
using System;
99
using System.Collections.Generic;
1010
using System.Diagnostics;
11+
using System.Linq;
1112
using ScriptEngine.Machine.Contexts;
1213

1314
namespace ScriptEngine.Machine
@@ -58,25 +59,28 @@ public static IEnumerable<VariableInfo> GetProperties(this IRuntimeContextInstan
5859
private static IEnumerable<VariableInfo> GetPropertiesWithPrivate(IRuntimeContextInstance context)
5960
{
6061
if (!(context is UserScriptContextInstance userScript))
61-
return Array.Empty<VariableInfo>();
62+
return GetPropertiesWithoutPrivate(context);
6263

63-
List<VariableInfo> infos = new List<VariableInfo>();
64-
foreach (var variable in userScript.Module.Variables)
64+
var infos = new List<VariableInfo>();
65+
for (int i = 1; i < userScript.GetOwnPropCount(); i++) // skip ThisObject == _ownProperties[0]
6566
{
6667
infos.Add(new VariableInfo() {
67-
Identifier = variable.Identifier,
68-
Type = variable.Type,
69-
Index = variable.Index,
70-
Annotations = HackGetAnnotations(context, variable.Index),
71-
IsExport = variable.IsExport
68+
Identifier = userScript.GetPropName(i),
69+
Type = SymbolType.ContextProperty,
70+
Index = i,
71+
Annotations = Array.Empty<AnnotationDefinition>(),
72+
IsExport = false
7273
});
7374
}
7475

75-
return infos;
76+
return infos.Concat(userScript.Module.Variables);
7677
}
7778

7879
private static IEnumerable<VariableInfo> GetPropertiesWithoutPrivate(IRuntimeContextInstance context)
7980
{
81+
if (context is UserScriptContextInstance userScript)
82+
return userScript.Module.Variables.Where(x => x.IsExport).ToList();
83+
8084
VariableInfo[] infos = new VariableInfo[context.GetPropCount()];
8185
for (int i = 0; i < infos.Length; i++)
8286
{
@@ -85,26 +89,14 @@ private static IEnumerable<VariableInfo> GetPropertiesWithoutPrivate(IRuntimeCon
8589
Identifier = context.GetPropName(i),
8690
Type = SymbolType.ContextProperty,
8791
Index = i,
88-
Annotations = HackGetAnnotations(context, i),
92+
Annotations = Array.Empty<AnnotationDefinition>(),
8993
IsExport = true
9094
};
9195
}
9296

9397
return infos;
9498
}
9599

96-
private static AnnotationDefinition[] HackGetAnnotations(IRuntimeContextInstance context, int i)
97-
{
98-
if (!(context is UserScriptContextInstance userScript))
99-
return Array.Empty<AnnotationDefinition>();
100-
101-
if (i == 0)
102-
return Array.Empty<AnnotationDefinition>();
103-
104-
var variable = userScript.Module.Variables[i - 1];
105-
return variable.Annotations;
106-
}
107-
108100
public static IValue GetPropValue(this IRuntimeContextInstance context, string propName)
109101
{
110102
int propNum = context.FindProperty(propName);

tests/annotations.os

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ВсеТесты.Добавить("ТестДолжен_ПроверитьПолучениеАннотацийПараметров");
1414
ВсеТесты.Добавить("ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарий");
1515
ВсеТесты.Добавить("ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарийИзСтроки");
16+
ВсеТесты.Добавить("ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарийСКонтекстом");
1617

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

@@ -197,4 +198,16 @@
197198

198199
КонецЕсли;
199200

201+
КонецПроцедуры
202+
203+
Процедура ТестДолжен_ПроверитьАннотацииПолейЗагрузитьСценарийСКонтекстом() Экспорт
204+
205+
ТекстСценария = "
206+
|Рефлектор = Новый Рефлектор();
207+
|ТаблицаСвойств = Рефлектор.ПолучитьТаблицуСвойств(ЭтотОбъект);
208+
|";
209+
Контекст = Новый Структура( "АргументыКоманднойСтроки", Новый Массив );
210+
211+
Сценарий = ЗагрузитьСценарийИзСтроки(ТекстСценария, Контекст);
212+
200213
КонецПроцедуры

0 commit comments

Comments
 (0)