using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace Brutal.SlangApi;
///
/// Hand-written ergonomic overloads on top of the generated ISession COM
/// interface. The generated half (InterfacesExt.gen.cs) mirrors the native
/// vtable shape one-for-one; this partial adds the wrappers needed to keep callers
/// from having to deal with raw nint/nint* at points where the COM
/// ABI requires arrays of unmanaged COM pointers that managed marshalling cannot
/// represent on its own.
///
public static unsafe partial class ISessionExtensions
{
///
/// Creates a composite from the given component types,
/// hiding the nint* array marshalling that the underlying COM ABI requires.
/// Each input proxy is AddRef'd for the duration of the call and released afterwards;
/// slang retains its own references into the returned composite, so the inputs
/// remain usable after this call returns.
///
/// The session creating the composite.
///
/// Components to compose. Accepts arrays, stackalloc spans, collection
/// literals, or .
///
///
/// Diagnostic blob populated by slang. May be non-null even on success.
///
/// The composite component on success.
/// Thrown if slang reports a failing .
public static IComponentType CreateCompositeComponentType(
this ISession self,
ReadOnlySpan componentTypes,
out ISlangBlob outDiagnostics
)
{
var count = componentTypes.Length;
Span pointers = count <= 64 ? stackalloc nint[count] : new nint[count];
pointers.Clear();
try
{
for (var i = 0; i < count; i++)
pointers[i] = Slang.GetComInstance(componentTypes[i]);
fixed (nint* p = pointers)
{
self.CreateCompositeComponentType(p, count, out var composite, out outDiagnostics).ThrowIfFailure();
return composite;
}
}
finally
{
for (var i = 0; i < count; i++)
if (pointers[i] != 0)
Marshal.Release(pointers[i]);
}
}
///
/// Loads a previously-imported module by name. UTF-8 marshalling for
/// is handled here so callers don't have to pin a
/// byte* by hand.
///
public static IModule? LoadModule(this ISession self, string moduleName, out ISlangBlob? outDiagnostics)
{
Span nameBuf = stackalloc byte[ShortStringStackBytes];
EncodeUtf8(moduleName, ref nameBuf);
fixed (byte* nameP = nameBuf)
return self.LoadModule(nameP, out outDiagnostics);
}
///
/// Loads a module from an in-memory source blob, marshalling
/// and from UTF-16 to UTF-8.
///
public static IModule? LoadModuleFromSource(
this ISession self,
string moduleName,
string path,
ISlangBlob source,
out ISlangBlob? outDiagnostics
)
{
Span nameBuf = stackalloc byte[ShortStringStackBytes];
Span pathBuf = stackalloc byte[ShortStringStackBytes];
EncodeUtf8(moduleName, ref nameBuf);
EncodeUtf8(path, ref pathBuf);
fixed (byte* nameP = nameBuf)
fixed (byte* pathP = pathBuf)
return self.LoadModuleFromSource(nameP, pathP, source, out outDiagnostics);
}
///
/// Loads a module from a UTF-16 string source, marshalling all three string
/// parameters to null-terminated UTF-8 for the underlying COM call.
///
public static IModule? LoadModuleFromSourceString(
this ISession self,
string moduleName,
string path,
string source,
out ISlangBlob? outDiagnostics
)
{
Span nameBuf = stackalloc byte[ShortStringStackBytes];
Span pathBuf = stackalloc byte[ShortStringStackBytes];
Span sourceBuf = stackalloc byte[LongStringStackBytes];
EncodeUtf8(moduleName, ref nameBuf);
EncodeUtf8(path, ref pathBuf);
EncodeUtf8(source, ref sourceBuf);
fixed (byte* nameP = nameBuf)
fixed (byte* pathP = pathBuf)
fixed (byte* sourceP = sourceBuf)
return self.LoadModuleFromSourceString(nameP, pathP, sourceP, out outDiagnostics);
}
///
/// Loads a module from a precompiled IR blob, marshalling
/// and to UTF-8.
///
public static IModule? LoadModuleFromIRBlob(
this ISession self,
string moduleName,
string path,
ISlangBlob source,
out ISlangBlob? outDiagnostics
)
{
Span nameBuf = stackalloc byte[ShortStringStackBytes];
Span pathBuf = stackalloc byte[ShortStringStackBytes];
EncodeUtf8(moduleName, ref nameBuf);
EncodeUtf8(path, ref pathBuf);
fixed (byte* nameP = nameBuf)
fixed (byte* pathP = pathBuf)
return self.LoadModuleFromIRBlob(nameP, pathP, source, out outDiagnostics);
}
private const int ShortStringStackBytes = 256;
private const int LongStringStackBytes = 8192;
///
/// Encodes as null-terminated UTF-8 into ,
/// re-pointing the span at a heap-allocated array if the stackalloc'd buffer is too small.
/// The caller pins the resulting span via fixed for a single COM call.
///
private static void EncodeUtf8(string value, ref Span buffer)
{
if (string.IsNullOrEmpty(value))
{
Debug.Assert(buffer.Length >= 1);
buffer[0] = 0;
return;
}
var byteCount = Encoding.UTF8.GetByteCount(value);
var needed = byteCount + 1;
if (needed > buffer.Length)
buffer = new byte[needed];
else
buffer = buffer[..needed];
Encoding.UTF8.GetBytes(value, buffer);
buffer[byteCount] = 0;
}
}