Skip to content

Commit 2bf95ec

Browse files
committed
- Исправлена ошибка потери значений ХттпКонтекст.Элементы
- Переименовано свойство Элементы - Рефакторинг враппера ХттпКонтекст
1 parent ec2cc63 commit 2bf95ec

File tree

4 files changed

+163
-28
lines changed

4 files changed

+163
-28
lines changed

src/OneScript.StandardLibrary/Collections/MapImpl.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ public MapImpl(IEnumerable<KeyAndValueImpl> source)
3131
}
3232
}
3333

34-
public override bool IsIndexed
35-
{
36-
get
37-
{
38-
return true;
39-
}
40-
}
34+
public override bool IsIndexed => true;
4135

4236
public override IValue GetIndexedValue(IValue index)
4337
{
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using OneScript.Contexts;
12+
using OneScript.Types;
13+
using ScriptEngine.Machine;
14+
using ScriptEngine.Machine.Contexts;
15+
16+
namespace OneScript.StandardLibrary.Collections
17+
{
18+
/// <summary>
19+
/// Класс Соответствие, который оборачивает произвольный Dictionary
20+
/// </summary>
21+
public class MapWrapper<TKey, TValue> : AutoCollectionContext<MapImpl, KeyAndValueImpl>
22+
{
23+
private readonly IDictionary<TKey, TValue> _originalMap;
24+
25+
private static ContextMethodsMapper<MapWrapper<TKey, TValue>> _methods =
26+
new ContextMethodsMapper<MapWrapper<TKey, TValue>>();
27+
28+
public static MapWrapper<TKey, TValue> Create(ITypeManager typeManager, IDictionary<TKey, TValue> originalMap)
29+
{
30+
var type = typeManager.GetTypeByFrameworkType(typeof(MapImpl));
31+
return new MapWrapper<TKey, TValue>(type, originalMap);
32+
}
33+
34+
private MapWrapper(
35+
TypeDescriptor mapType,
36+
IDictionary<TKey, TValue> originalMap) : base(mapType)
37+
{
38+
_originalMap = originalMap;
39+
}
40+
41+
public override bool IsIndexed => true;
42+
43+
[ContextMethod("Количество", "Count")]
44+
public override int Count() => _originalMap.Count;
45+
46+
public override IEnumerator<KeyAndValueImpl> GetEnumerator()
47+
{
48+
return _originalMap.Select(entry => new KeyAndValueImpl(
49+
ContextValuesMarshaller.ConvertReturnValue(entry.Key),
50+
ContextValuesMarshaller.ConvertReturnValue(entry.Value))).GetEnumerator();
51+
}
52+
53+
public override IValue GetIndexedValue(IValue index)
54+
{
55+
if (!_originalMap.TryGetValue(ContextValuesMarshaller.ConvertParam<TKey>(index), out var mapValue))
56+
{
57+
return ValueFactory.Create();
58+
}
59+
60+
return ContextValuesMarshaller.ConvertReturnValue(mapValue);
61+
}
62+
63+
public override void SetIndexedValue(IValue index, IValue val)
64+
{
65+
if (index.SystemType != BasicTypes.Undefined)
66+
{
67+
var mapKey = ContextValuesMarshaller.ConvertParam<TKey>(index);
68+
var mapVal = ContextValuesMarshaller.ConvertParam<TValue>(val);
69+
_originalMap[mapKey] = mapVal;
70+
}
71+
}
72+
73+
public override bool IsPropReadable(int propNum)
74+
{
75+
return false;
76+
}
77+
78+
public override bool IsPropWritable(int propNum)
79+
{
80+
return false;
81+
}
82+
83+
[ContextMethod("Вставить", "Insert")]
84+
public void Insert(IValue key, IValue val = null)
85+
{
86+
SetIndexedValue(key, val ?? ValueFactory.Create());
87+
}
88+
89+
[ContextMethod("Получить", "Get")]
90+
public IValue Retrieve(IValue key)
91+
{
92+
return GetIndexedValue(key);
93+
}
94+
95+
[ContextMethod("Очистить", "Clear")]
96+
public void Clear()
97+
{
98+
_originalMap.Clear();
99+
}
100+
101+
[ContextMethod("Удалить", "Delete")]
102+
public void Delete(IValue key)
103+
{
104+
_originalMap.Remove(ContextValuesMarshaller.ConvertParam<TKey>(key));
105+
}
106+
107+
public override int GetMethodNumber(string name) => _methods.FindMethod(name);
108+
109+
public override int GetMethodsCount() => _methods.Count;
110+
111+
public override BslMethodInfo GetMethodInfo(int methodNumber) => _methods.GetRuntimeMethod(methodNumber);
112+
113+
public override void CallAsProcedure(int methodNumber, IValue[] arguments)
114+
{
115+
var binding = _methods.GetCallableDelegate(methodNumber);
116+
try
117+
{
118+
binding(this, arguments);
119+
}
120+
catch (System.Reflection.TargetInvocationException e)
121+
{
122+
throw e.InnerException!;
123+
}
124+
}
125+
126+
public override void CallAsFunction(int methodNumber, IValue[] arguments, out IValue retValue)
127+
{
128+
var binding = _methods.GetCallableDelegate(methodNumber);
129+
try
130+
{
131+
retValue = binding(this, arguments);
132+
}
133+
catch (System.Reflection.TargetInvocationException e)
134+
{
135+
throw e.InnerException!;
136+
}
137+
}
138+
}
139+
}

src/OneScript.Web.Server/HttpContextWrapper.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,58 @@ This Source Code Form is subject to the terms of the
44
was not distributed with this file, You can obtain one
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
7+
78
using Microsoft.AspNetCore.Http;
89
using OneScript.Contexts;
910
using OneScript.StandardLibrary.Collections;
10-
using OneScript.StandardLibrary.Text;
11-
using OneScript.Values;
1211
using OneScript.Web.Server.WebSockets;
13-
using ScriptEngine.Machine;
1412
using ScriptEngine.Machine.Contexts;
15-
using System;
16-
using System.Collections.Generic;
17-
using System.Runtime.CompilerServices;
18-
using System.Text;
19-
using System.Threading.Tasks;
13+
using OneScript.Types;
2014

2115
namespace OneScript.Web.Server
2216
{
2317
[ContextClass("HTTPКонтекст", "HTTPContext")]
2418
public class HttpContextWrapper : AutoContext<HttpContextWrapper>
2519
{
26-
internal readonly HttpContext _context;
20+
private readonly ITypeManager _typeManager;
21+
private readonly HttpContext _context;
22+
private readonly PropertyWrappersCollection _wrappers = new();
2723

28-
public HttpContextWrapper(HttpContext context)
24+
public HttpContextWrapper(ITypeManager typeManager, HttpContext context)
2925
{
26+
_typeManager = typeManager;
3027
_context = context;
3128
}
3229

3330
[ContextProperty("Запрос", "Request", CanWrite = false)]
34-
public HttpRequestWrapper Request => new HttpRequestWrapper(_context.Request);
31+
public HttpRequestWrapper Request =>
32+
_wrappers.Get(nameof(Request), () => new HttpRequestWrapper(_context.Request));
3533

3634
[ContextProperty("Ответ", "Response", CanWrite = false)]
37-
public HttpResponseWrapper Response => new HttpResponseWrapper(_context.Response);
35+
public HttpResponseWrapper Response =>
36+
_wrappers.Get(nameof(Response), () => new HttpResponseWrapper(_context.Response));
3837

3938
[ContextProperty("Соединение", "Connection", CanWrite = false)]
40-
public ConnectionInfoWrapper Connection => new ConnectionInfoWrapper(_context.Connection);
39+
public ConnectionInfoWrapper Connection =>
40+
_wrappers.Get(nameof(Connection), () => new ConnectionInfoWrapper(_context.Connection));
4141

4242
[ContextProperty("ИдентификаторТрассировки", "TraceIdentifier", CanWrite = false)]
43-
public IValue TraceIdentifier => BslStringValue.Create(_context.TraceIdentifier);
43+
public string TraceIdentifier => _context.TraceIdentifier;
4444

4545
[ContextProperty("ЗапросПрерван", "RequestAborted", CanWrite = false)]
46-
public IValue RequestAborted => BslBooleanValue.Create(_context.RequestAborted.IsCancellationRequested);
46+
public bool RequestAborted => _context.RequestAborted.IsCancellationRequested;
4747

48-
[ContextProperty("Элементы", "Items", CanWrite = false)]
49-
public MapImpl Items { get; } = new MapImpl();
48+
[ContextProperty("Данные", "Data", CanWrite = false)]
49+
public AutoCollectionContext<MapImpl, KeyAndValueImpl> Data => _wrappers.Get(nameof(Data), () =>
50+
MapWrapper<object, object>.Create(_typeManager, _context.Items));
5051

5152
[ContextProperty("ВебСокеты", "WebSockets", CanWrite = false)]
52-
public WebSocketsManagerWrapper WebSockets => new WebSocketsManagerWrapper(_context.WebSockets);
53+
public WebSocketsManagerWrapper WebSockets => _wrappers.Get(nameof(WebSockets), () =>
54+
new WebSocketsManagerWrapper(_context.WebSockets));
5355

5456
[ContextMethod("Прервать", "Abort")]
5557
public void Abort() => _context.Abort();
5658

5759
internal HttpContext GetContext() => _context;
5860
}
59-
}
61+
}

src/OneScript.Web.Server/WebServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void ConfigureApp()
105105
{
106106
var args = new IValue[]
107107
{
108-
new HttpContextWrapper(context),
108+
new HttpContextWrapper(_executionContext.TypeManager, context),
109109
};
110110

111111
var methodNumber = _exceptionHandler?.Target.GetMethodNumber(_exceptionHandler?.MethodName);
@@ -142,7 +142,7 @@ private void ConfigureApp()
142142
{
143143
var args = new IValue[]
144144
{
145-
new HttpContextWrapper(context),
145+
new HttpContextWrapper(_executionContext.TypeManager, context),
146146
new RequestDelegateWrapper(next)
147147
};
148148

0 commit comments

Comments
 (0)