using System.Runtime.InteropServices.Marshalling; using Brutal.Strings; namespace Brutal.SlangApi; /// /// Hand-written ergonomic helpers on top of the generated /// handle struct. The deprecated sp* compile-request flow trafficks in COM /// interfaces as raw nint (because flat-C entry points can't carry /// [GeneratedComInterface] proxies through source-gen marshalling); this layer /// bridges those nints to/from managed COM proxies via /// and . /// public static unsafe class ICompileRequestExtensions { /// /// Returns the the request was created against, wrapping the /// raw COM pointer that the deprecated spCompileRequest_getSession entry point /// produces. Returns null when the call fails or yields a null pointer. /// public static Result GetSession(this ICompileRequest self, out ISession? session) { var hr = self.GetSession(out var raw); session = Slang.WrapComInstance(hr, raw); return hr; } /// /// Returns the linked program assembled from the request's translation units as a /// managed . Returns null when the call fails or /// slang yields a null pointer. /// public static Result GetProgram(this ICompileRequest self, out IComponentType? program) { var hr = self.GetProgram(out var raw); program = Slang.WrapComInstance(hr, raw); return hr; } /// /// Returns the linked program plus its entry points as a single /// . Returns null when the call fails or slang /// yields a null pointer. /// public static Result GetProgramWithEntryPoints(this ICompileRequest self, out IComponentType? program) { var hr = self.GetProgramWithEntryPoints(out var raw); program = Slang.WrapComInstance(hr, raw); return hr; } /// /// Returns the entry point at as a standalone /// . Returns null when the call fails or slang /// yields a null pointer. /// public static Result GetEntryPoint(this ICompileRequest self, Int entryPointIndex, out IComponentType? entryPoint) { var hr = self.GetEntryPoint(entryPointIndex, out var raw); entryPoint = Slang.WrapComInstance(hr, raw); return hr; } /// /// Returns the loaded for the translation unit at /// . Returns null when the call fails /// or slang yields a null pointer. /// public static Result GetModule(this ICompileRequest self, Int translationUnitIndex, out IModule? module) { var hr = self.GetModule(translationUnitIndex, out var raw); module = Slang.WrapComInstance(hr, raw); return hr; } /// /// Sets the used to resolve source includes and other /// file lookups for this request. Pass null to restore slang's default file /// system. The native side AddRefs the supplied object internally. /// public static void SetFileSystem(this ICompileRequest self, ISlangFileSystem? fileSystem) { if (fileSystem is null) { self.SetFileSystem(0); return; } var raw = Slang.GetComInstance(fileSystem); try { self.SetFileSystem(raw); } finally { Marshal.Release(raw); } } /// /// Redirects output on to the supplied /// . Pass null to restore the channel's default /// writer. The native side AddRefs the supplied object internally. /// public static void SetWriter(this ICompileRequest self, WriterChannel channel, ISlangWriter? writer) { if (writer is null) { self.SetWriter(channel, 0); return; } var raw = Slang.GetComInstance(writer); try { self.SetWriter(channel, raw); } finally { Marshal.Release(raw); } } /// /// Returns the currently installed for /// , or null if the channel is unset. Slang's /// getter does not AddRef the returned pointer, so this wrapper performs the /// AddRef itself before handing the pointer to . /// public static ISlangWriter? GetWriter(this ICompileRequest self, WriterChannel channel) { var raw = self.GetWriter(channel); if (raw == 0) return null; Marshal.AddRef(raw); return (ISlangWriter)Slang.ComWrappers.GetOrCreateObjectForComInstance(raw, CreateObjectFlags.None); } /// /// Adds a translation-unit source whose contents are provided as an /// . The native side AddRefs the supplied blob internally. /// public static void AddTranslationUnitSourceBlob( this ICompileRequest self, int translationUnitIndex, RefString8 path, ISlangBlob sourceBlob ) { var raw = Slang.GetComInstance(sourceBlob); try { self.AddTranslationUnitSourceBlob(translationUnitIndex, path, raw); } finally { Marshal.Release(raw); } } }