mirror of
https://github.com/zap-proto/cs.git
synced 2026-10-30 15:05:32 +00:00
Finishes the in-flight capnp→ZAP rebrand on this branch. The prior commits
renamed identifiers/namespaces/files/projects (Capnp.Net.Runtime→Zap.Net.Runtime,
CapnpC→ZapC, .capnp→.zap) but left the human-readable brand name "Cap'n Proto"
throughout doc-comments, .csproj/.nuspec descriptions, README, MSBuild .xaml
rule display names, and the generated XML doc. This completes brand neutrality:
- Replace 154 "Cap'n Proto" / 4 "Cap'n-Proto" / 1 "Cap'n'p" prose occurrences
with "ZAP" across 32 first-party files (runtime, generator, zapc-csharp,
MSBuild integration, chocolatey nuspecs, README, docs, XML doc).
- Revert the LICENSE provenance URL to the real upstream fork
(c80k/capnproto-dotnetcore) — upstream-fork license headers must stay.
- Canonicalize project/repo URLs to github.com/zap-proto/cs (were fabricated
c80k/zap-dotnetcore) in csproj/nuspec/VERIFICATION/README badges.
- Canonicalize docs URLs zap-protocol(.github.io|/zap-cs|/zap-cpp-core)
→ zap-proto.dev / github.com/zap-proto/{cs,cpp-core}.
- Collapse redundant "zap \"ZAP\"" PackageTags to "zap".
Verified consistent (no toolchain on host): every .sln/.csproj ProjectReference
resolves; all namespaces are Zap*/ZapC*; MSBuild task chain
(GenerateZapFileCodeBehindTask, ZapFiles ItemGroup, _ZapcCsharp_TaskAssembly)
is uniformly renamed; props/targets/xaml/coverage/chocolatey carry no capnp.
Deliberately preserved as upstream-derived test data (not first-party source):
upstream Cap'n Proto schema fixtures (rpc.zap, rpc-twoparty.zap, json.zap,
rpc-csharp.zap), golden compiler-error outputs (errors.txt), and the
machine-generated DO-NOT-EDIT test.zap.{c++,h}. Their "Cap'n Proto" strings are
the literal output of the external schema compiler the compat test matches.
194 lines
7.0 KiB
C#
194 lines
7.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Zap.Rpc
|
|
{
|
|
/// <summary>
|
|
/// A path from an outer ZAP struct to an inner (probably deeply nested) struct member.
|
|
/// </summary>
|
|
public class MemberAccessPath
|
|
{
|
|
/// <summary>
|
|
/// Path to the bootstrap capability (which is an empty path)
|
|
/// </summary>
|
|
public static readonly MemberAccessPath BootstrapAccess = new MemberAccessPath(new List<MemberAccess>());
|
|
|
|
/// <summary>
|
|
/// Deserializes a MemberAccessPath from ZAP representation.
|
|
/// </summary>
|
|
/// <param name="promisedAnswer">ZAP representation</param>
|
|
/// <returns>The MemberAccessPath</returns>
|
|
public static MemberAccessPath Deserialize(PromisedAnswer.READER promisedAnswer)
|
|
{
|
|
var ops = new MemberAccess[promisedAnswer.Transform.Count];
|
|
|
|
int i = 0;
|
|
foreach (var op in promisedAnswer.Transform)
|
|
{
|
|
ops[i++] = MemberAccess.Deserialize(op);
|
|
}
|
|
|
|
return new MemberAccessPath(ops);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a path from <see cref="MemberAccess"/> qualifiers.
|
|
/// </summary>
|
|
/// <param name="path">List of member access elements</param>
|
|
public MemberAccessPath(IReadOnlyList<MemberAccess> path)
|
|
{
|
|
Path = path ?? throw new ArgumentNullException(nameof(path));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructs a path from ZAP struct member offsets.
|
|
/// </summary>
|
|
/// <param name="offsets">Member offsets</param>
|
|
public MemberAccessPath(params uint[] offsets)
|
|
{
|
|
if (offsets == null)
|
|
throw new ArgumentNullException(nameof(offsets));
|
|
|
|
Path = offsets.Select(i => new StructMemberAccess(checked((ushort)i))).ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base class of an individual member access.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This might appear a bit of overengineering, since the only specialization is the <see cref="StructMemberAccess"/>.
|
|
/// But there might be further specializations in the future, the most obvious one being an "ArrayElementAccess".
|
|
/// Now we already have a suitable design pattern, mainly to show the abstract concept behind a member access path.
|
|
/// </remarks>
|
|
public abstract class MemberAccess
|
|
{
|
|
/// <summary>
|
|
/// Deserializes a MemberAccess instance from ZAP representation.
|
|
/// </summary>
|
|
/// <param name="op">ZAP representation</param>
|
|
/// <returns>Deserialized instance</returns>
|
|
public static MemberAccess Deserialize(PromisedAnswer.Op.READER op)
|
|
{
|
|
switch (op.which)
|
|
{
|
|
case PromisedAnswer.Op.WHICH.GetPointerField:
|
|
return new StructMemberAccess(op.GetPointerField);
|
|
|
|
default:
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes this instance to a <see cref="PromisedAnswer.Op"/>.
|
|
/// </summary>
|
|
/// <param name="op">Serialization target</param>
|
|
public abstract void Serialize(PromisedAnswer.Op.WRITER op);
|
|
|
|
/// <summary>
|
|
/// Evaluates the member access on a given struct instance.
|
|
/// </summary>
|
|
/// <param name="state">Input struct instance</param>
|
|
/// <returns>Member value or object</returns>
|
|
public abstract DeserializerState Eval(DeserializerState state);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The one and only member access which is currently supported: Member of a struct.
|
|
/// </summary>
|
|
public class StructMemberAccess: MemberAccess
|
|
{
|
|
/// <summary>
|
|
/// Constructs an instance for given struct member offset.
|
|
/// </summary>
|
|
/// <param name="offset">The ZAP struct member offset</param>
|
|
public StructMemberAccess(ushort offset)
|
|
{
|
|
Offset = offset;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The ZAP struct member offset
|
|
/// </summary>
|
|
public ushort Offset { get; }
|
|
|
|
/// <summary>
|
|
/// Serializes this instance to a <see cref="PromisedAnswer.Op"/>.
|
|
/// </summary>
|
|
/// <param name="op">Serialization target</param>
|
|
public override void Serialize(PromisedAnswer.Op.WRITER op)
|
|
{
|
|
op.which = PromisedAnswer.Op.WHICH.GetPointerField;
|
|
op.GetPointerField = Offset;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Evaluates the member access on a given struct instance.
|
|
/// </summary>
|
|
/// <param name="state">Input struct instance</param>
|
|
/// <returns>Member value or object</returns>
|
|
public override DeserializerState Eval(DeserializerState state)
|
|
{
|
|
if (state.Kind == ObjectKind.Nil)
|
|
{
|
|
return default;
|
|
}
|
|
|
|
if (state.Kind != ObjectKind.Struct)
|
|
{
|
|
throw new ArgumentException("Expected a struct");
|
|
}
|
|
|
|
return state.StructReadPointer(Offset);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The access path is a composition of individual member accesses.
|
|
/// </summary>
|
|
public IReadOnlyList<MemberAccess> Path { get; }
|
|
|
|
/// <summary>
|
|
/// Serializes this path th a <see cref="PromisedAnswer"/>.
|
|
/// </summary>
|
|
/// <param name="promisedAnswer">The serialization target</param>
|
|
public void Serialize(PromisedAnswer.WRITER promisedAnswer)
|
|
{
|
|
promisedAnswer.Transform.Init(Path.Count);
|
|
|
|
for (int i = 0; i < Path.Count; i++)
|
|
{
|
|
Path[i].Serialize(promisedAnswer.Transform[i]);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Evaluates the path on a given object.
|
|
/// </summary>
|
|
/// <param name="rpcState">The object (usually "params struct") on which to evaluate this path.</param>
|
|
/// <returns>Resulting low-level capability</returns>
|
|
/// <exception cref="DeserializationException">Evaluation of this path did not give a capability</exception>
|
|
public ConsumedCapability Eval(DeserializerState rpcState)
|
|
{
|
|
var cur = rpcState;
|
|
|
|
foreach (var op in Path)
|
|
{
|
|
cur = op.Eval(cur);
|
|
}
|
|
|
|
switch (cur.Kind)
|
|
{
|
|
case ObjectKind.Nil:
|
|
return NullCapability.Instance;
|
|
|
|
case ObjectKind.Capability:
|
|
return rpcState.Caps![(int)cur.CapabilityIndex];
|
|
|
|
default:
|
|
throw new DeserializationException("Access path did not result in a capability");
|
|
}
|
|
}
|
|
}
|
|
} |