feat(cordis): 完成阶段5访问介导与诊断
This commit is contained in:
@@ -15,14 +15,17 @@ namespace ShrinkContext
|
||||
{
|
||||
private readonly List<ShrinkEffectHandle> _effects = new();
|
||||
private readonly Dictionary<string, string>? _isolateOverlay;
|
||||
private Dictionary<string, IReadOnlyDictionary<string, object?>>? _interceptOverlay;
|
||||
|
||||
internal ShrinkCtx(ShrinkContextRuntime runtime, ShrinkCtx? parent, ShrinkFiber? ownerFiber,
|
||||
Dictionary<string, string>? isolateOverlay)
|
||||
Dictionary<string, string>? isolateOverlay,
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? interceptOverlay = null)
|
||||
{
|
||||
Runtime = runtime;
|
||||
Parent = parent;
|
||||
OwnerFiber = ownerFiber;
|
||||
_isolateOverlay = isolateOverlay;
|
||||
ReplaceIntercept(interceptOverlay);
|
||||
}
|
||||
|
||||
public ShrinkContextRuntime Runtime { get; }
|
||||
@@ -106,7 +109,17 @@ namespace ShrinkContext
|
||||
/// 提供依赖(set 是可逆效应):立即安装绑定并通知依赖者;
|
||||
/// 逆操作撤回绑定并再次通知。句柄同时挂在当前上下文上,随上下文卸载自动撤回。
|
||||
/// </summary>
|
||||
public ShrinkEffectHandle Set<T>(string key, T value) => Runtime.SetBinding(this, key, value);
|
||||
public ShrinkEffectHandle Set<T>(string key, T value) => Runtime.SetBinding(this, key, value, null);
|
||||
|
||||
/// <summary>提供带访问策略的依赖;策略只在介导 Get 时运行,原始编排查找不会运行策略。</summary>
|
||||
public ShrinkEffectHandle Set<T>(string key, T value, IShrinkCoeffectAccessPolicy accessPolicy) =>
|
||||
Runtime.SetBinding(this, key, value, accessPolicy ?? throw new ArgumentNullException(nameof(accessPolicy)));
|
||||
|
||||
public ShrinkEffectHandle Set<T>(ShrinkKey<T> key, T value) =>
|
||||
Set((key ?? throw new ArgumentNullException(nameof(key))).Id, value);
|
||||
|
||||
public ShrinkEffectHandle Set<T>(ShrinkKey<T> key, T value, IShrinkCoeffectAccessPolicy accessPolicy) =>
|
||||
Set((key ?? throw new ArgumentNullException(nameof(key))).Id, value, accessPolicy);
|
||||
|
||||
/// <summary>
|
||||
/// 代理介导的依赖访问(论文算法 6):沿纤程链解析已提交视图;
|
||||
@@ -119,7 +132,7 @@ namespace ShrinkContext
|
||||
while (fiber != null)
|
||||
{
|
||||
if (fiber.Committed != null && fiber.Committed.TryGetValue(key, out var binding))
|
||||
return (T)binding.Value!;
|
||||
return Runtime.ResolveAccess<T>(this, key, binding);
|
||||
if (fiber.InjectSet.Contains(key))
|
||||
throw new ShrinkInactiveAccessException(key, fiber.Name);
|
||||
fiber = fiber.Parent;
|
||||
@@ -128,6 +141,9 @@ namespace ShrinkContext
|
||||
throw new ShrinkUndeclaredAccessException(key);
|
||||
}
|
||||
|
||||
public T Get<T>(ShrinkKey<T> key) => Get<T>(
|
||||
(key ?? throw new ArgumentNullException(nameof(key))).Id);
|
||||
|
||||
/// <summary>与 <see cref="Get{T}"/> 相同的介导规则;未激活返回 false 而不抛异常。</summary>
|
||||
public bool TryGet<T>(string key, out T value)
|
||||
{
|
||||
@@ -136,7 +152,7 @@ namespace ShrinkContext
|
||||
{
|
||||
if (fiber.Committed != null && fiber.Committed.TryGetValue(key, out var binding))
|
||||
{
|
||||
value = (T)binding.Value!;
|
||||
value = Runtime.ResolveAccess<T>(this, key, binding);
|
||||
return true;
|
||||
}
|
||||
if (fiber.InjectSet.Contains(key))
|
||||
@@ -148,6 +164,9 @@ namespace ShrinkContext
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGet<T>(ShrinkKey<T> key, out T value) => TryGet(
|
||||
(key ?? throw new ArgumentNullException(nameof(key))).Id, out value);
|
||||
|
||||
/// <summary>
|
||||
/// 派生隔离子上下文(论文定义 28/29 的派生实现):
|
||||
/// 覆盖 key 的 realm 解析;父上下文不受影响;丢弃子上下文即隐式恢复。
|
||||
@@ -162,6 +181,33 @@ namespace ShrinkContext
|
||||
return new ShrinkCtx(Runtime, this, OwnerFiber, new Dictionary<string, string> { [key] = realm });
|
||||
}
|
||||
|
||||
public ShrinkCtx Isolate<T>(ShrinkKey<T> key, string realm) => Isolate(
|
||||
(key ?? throw new ArgumentNullException(nameof(key))).Id, realm);
|
||||
|
||||
/// <summary>
|
||||
/// 派生带拦截元数据的子上下文。子级同名 metadata 覆盖父级;丢弃子上下文即恢复。
|
||||
/// 该操作不改变 realm、provider 或 target,因此不会触发 fiber 重载。
|
||||
/// </summary>
|
||||
public ShrinkCtx Intercept(string key, IReadOnlyDictionary<string, object?> metadata)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new ArgumentException("Key must not be null or empty.", nameof(key));
|
||||
if (metadata == null)
|
||||
throw new ArgumentNullException(nameof(metadata));
|
||||
|
||||
var metadataCopy = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
foreach (var pair in metadata)
|
||||
metadataCopy[pair.Key] = pair.Value;
|
||||
var overlay = new Dictionary<string, IReadOnlyDictionary<string, object?>>(StringComparer.Ordinal)
|
||||
{
|
||||
[key] = metadataCopy
|
||||
};
|
||||
return new ShrinkCtx(Runtime, this, OwnerFiber, null, overlay);
|
||||
}
|
||||
|
||||
public ShrinkCtx Intercept<T>(ShrinkKey<T> key, IReadOnlyDictionary<string, object?> metadata) =>
|
||||
Intercept((key ?? throw new ArgumentNullException(nameof(key))).Id, metadata);
|
||||
|
||||
internal ShrinkCtx CreateChildForFiber(ShrinkFiber fiber) => new(Runtime, this, fiber, null);
|
||||
|
||||
/// <summary>在派生子上下文中叠加隔离覆盖(同键后者优先),供加载器为条目配置隔离域。</summary>
|
||||
@@ -180,6 +226,38 @@ namespace ShrinkContext
|
||||
return new ShrinkCtx(Runtime, this, OwnerFiber, overlay);
|
||||
}
|
||||
|
||||
/// <summary>在当前纤程上下文上叠加 loader 条目配置的 intercept。</summary>
|
||||
internal ShrinkCtx WithIntercept(
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>> intercept) =>
|
||||
new(Runtime, this, OwnerFiber, null, interceptOverlay: intercept);
|
||||
|
||||
/// <summary>原地替换本层 intercept,供 loader 在不重载 fiber 的情况下更新访问策略。</summary>
|
||||
internal void ReplaceIntercept(
|
||||
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object?>>? intercept)
|
||||
{
|
||||
if (intercept == null || intercept.Count == 0)
|
||||
{
|
||||
_interceptOverlay = null;
|
||||
return;
|
||||
}
|
||||
|
||||
var copy = new Dictionary<string, IReadOnlyDictionary<string, object?>>(StringComparer.Ordinal);
|
||||
foreach (var pair in intercept)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pair.Key))
|
||||
throw new ArgumentException("Intercept key must not be empty.", nameof(intercept));
|
||||
if (pair.Value == null)
|
||||
throw new ArgumentException($"Intercept metadata for '{pair.Key}' must not be null.",
|
||||
nameof(intercept));
|
||||
var metadataCopy = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
foreach (var metadataPair in pair.Value)
|
||||
metadataCopy[metadataPair.Key] = metadataPair.Value;
|
||||
copy[pair.Key] = metadataCopy;
|
||||
}
|
||||
|
||||
_interceptOverlay = copy;
|
||||
}
|
||||
|
||||
/// <summary>解析 key 在本上下文中归属的 realm:沿父链找最近的隔离覆盖,默认 realm 为 key 自身。</summary>
|
||||
internal string ResolveRealm(string key)
|
||||
{
|
||||
@@ -193,5 +271,30 @@ namespace ShrinkContext
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/// <summary>按根到叶顺序合并拦截元数据;越接近访问方的上下文优先。</summary>
|
||||
internal IReadOnlyDictionary<string, object?> ResolveInterceptMetadata(string key)
|
||||
{
|
||||
var chain = new Stack<ShrinkCtx>();
|
||||
for (var current = this; current != null; current = current.Parent)
|
||||
chain.Push(current);
|
||||
|
||||
var merged = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
while (chain.Count > 0)
|
||||
{
|
||||
var current = chain.Pop();
|
||||
if (current._interceptOverlay == null ||
|
||||
!current._interceptOverlay.TryGetValue(key, out var metadata))
|
||||
continue;
|
||||
|
||||
foreach (var pair in metadata)
|
||||
merged[pair.Key] = pair.Value;
|
||||
}
|
||||
|
||||
return merged;
|
||||
}
|
||||
|
||||
internal IReadOnlyCollection<string> GetLocalInterceptKeys() =>
|
||||
_interceptOverlay?.Keys ?? (IReadOnlyCollection<string>)Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user