Skip to content

Add interop members from #99's gap list - #491

Merged
jonathanperis merged 4 commits into
mainfrom
fix/interop-gap-members
Nov 24, 2026
Merged

jonathanperis merged 4 commits into
mainfrom
fix/interop-gap-members

Conversation

@jonathanperis

@jonathanperis jonathanperis commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Part of #99.

What changed

Library members #99 lists that csharpc did not provide yet, each written as .NET defines it (corelib, plus one runtime entry point; no codegen change):

  • nint.Add/Subtract, nuint.Add/Subtract (NativeIntMembers.cs)
  • Marshal.GetHRForException and the char overloads of Marshal.WriteInt16 (SecureString.cs's MarshalSecureStringMembers)
  • NativeLibrary.Load(string, Assembly, DllImportSearchPath?) (Interop.cs), with .NET's argument checks and the [DllImport] search: the program's directory first unless searchPath leaves out AssemblyDirectory (csrt_pinvoke_load_library_from in runtime/pinvoke.c; Windows' LoadLibrary starts there itself)
  • OperatingSystem.IsMacOSVersionAtLeast: kern.osproductversion through sysctlbyname on macOS, false on other systems

Tests

tests/samples/1553_interop_gap_members, recorded from .NET. Locally on linux-x86_64: the sample and its neighbours (marshal|nativelibrary|nint|interop|operatingsystem) plus the dllimport/pinvoke samples pass at -O0/-O2 (36/36), via IL (18/18) and on wasm32 (8/8; 1553 is skipped on WASI, which has no dynamic libraries); size.sh is within every budget; slicer.sh passes 10/10.

What remains of #99

The UnsafeAccessor constructor case (next PR). NFloat is in filipnavara's #296. The other items on the list already work on main; #99 gets a comment with each one's status when this merges.


Note

Low Risk
Additive BCL parity with tests; the only behavioral surface is optional native library search order via the new Load overload.

Overview
Adds .NET-compatible interop and platform APIs that were missing from csharpc’s gap list (#99), without codegen changes.

Pointer arithmetic: nint/nuint (and IntPtr/UIntPtr) now expose Add/Subtract with unchecked byte offsets.

Marshal: GetHRForException returns an exception’s HResult (0 for null). WriteInt16 gains char overloads that write the character’s code unit.

NativeLibrary: New Load(libraryName, assembly, searchPath?) validates arguments and calls a new runtime entry csrt_pinvoke_load_library_from, which probes the application directory first when DllImportSearchPath.AssemblyDirectory is included (the default), matching DllImport semantics on Unix; Windows delegates to existing LoadLibrary behavior.

OperatingSystem: IsMacOSVersionAtLeast reads kern.osproductversion via sysctlbyname on macOS and returns false elsewhere.

A sample test 1553_interop_gap_members records behavior against .NET; WASI skips it because dynamic libraries aren’t available.

Reviewed by Cursor Bugbot for commit cf3bb5d. Bugbot is set up for automated code reviews on this repo. Configure here.

nint/nuint.Add and Subtract, Marshal.GetHRForException, Marshal.WriteInt16's
char overloads, NativeLibrary.Load(string, Assembly, DllImportSearchPath?) and
OperatingSystem.IsMacOSVersionAtLeast (kern.osproductversion on macOS, false
elsewhere), each as .NET defines it. Sample 1553 is recorded from .NET.
@jonathanperis jonathanperis added this to the Drop-in for large apps milestone Nov 24, 2026
@jonathanperis jonathanperis added enhancement New feature or request area: bcl Library surface (corelib) labels Nov 24, 2026
@jonathanperis jonathanperis self-assigned this Nov 24, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 86d5417. Configure here.

Comment thread src/Csharpc/corelib/Interop.cs Resolved
…searchPath)

The overload went to the system loader as NativeLibrary.Load(path) does, so a
native library shipped next to the executable was not found. It now takes the
[DllImport] search, the program's directory first, unless searchPath leaves
out DllImportSearchPath.AssemblyDirectory, through the runtime's new
csrt_pinvoke_load_library_from. Windows' LoadLibrary starts in the program's
directory itself. Sample 1553 is skipped on WASI, which has no dynamic
libraries, as 729 and 730 are.
@jonathanperis
jonathanperis merged commit 0037a20 into main Nov 24, 2026
35 checks passed
@jonathanperis
jonathanperis deleted the fix/interop-gap-members branch November 24, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: bcl Library surface (corelib) enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant