Skip to content

feat: add more null types for types the runtime accept or returns as null#363

Merged
NathanWalker merged 1 commit intomainfrom
feat/more-nullable-types
May 4, 2026
Merged

feat: add more null types for types the runtime accept or returns as null#363
NathanWalker merged 1 commit intomainfrom
feat/more-nullable-types

Conversation

@edusperoni
Copy link
Copy Markdown
Collaborator

@edusperoni edusperoni commented May 4, 2026

Summary

  • Pointer return types now always include | null in generated .d.ts files, matching the runtime behavior where a native null pointer returns JS null (not an interop.Pointer wrapping address 0)
  • Pointer parameter types include | null by default, except when the native API is annotated _Nonnull — preserving type safety for APIs that contractually require a non-null pointer
  • Adds NonNullableType to the metadata generator's type system to track _Nonnull annotations from clang, mirroring the existing NullableType for _Nullable

Before

// return — no null, but runtime can return null
declare function CFAllocatorAllocate(...): interop.Pointer | interop.Reference<any>;
// param — no null, but runtime accepts null/undefined
declare function fegetenv(p1: interop.Pointer | interop.Reference<fenv_t> | ArrayBufferLike | ArrayBufferView): number;

After

// return — always nullable (runtime returns null for nullptr)
declare function CFAllocatorAllocate(...): interop.Pointer | interop.Reference<any> | null;
// unannotated/nullable param — accepts null
declare function fegetenv(p1: interop.Pointer | interop.Reference<fenv_t> | ArrayBufferLike | ArrayBufferView | null): number;
// _Nonnull param — no null (e.g. NSData.dataWithBytesNoCopyLength)
dataWithBytesNoCopyLength(bytes: interop.Pointer | interop.Reference<any> | ArrayBufferLike | ArrayBufferView, ...): NSData;

Array types (interop.Reference<T> from ConstantArray/ExtVector/IncompleteArray) are not affected — they represent inline data that is never null.

Test plan

  • Built metadata generator for arm64
  • Generated declarations for 65 modules (27,718 declarations) from the iOS 18.5 SDK
  • Verified 20,419 pointer params have | null (nullable/unannotated) and 2,549 do not (_Nonnull)
  • Verified all pointer return types include | null
  • Verified array types (interop.Reference<T> without interop.Pointer) are unaffected
  • Spot-checked real APIs: NSData.dataWithBytesNoCopyLength (nonnull, no null) vs NSData.dataWithBytesLength (nullable, has null), CGBitmapContextCreate(data:) (nullable, has null)

@NathanWalker NathanWalker merged commit fb0a5f4 into main May 4, 2026
8 checks passed
NathanWalker pushed a commit that referenced this pull request May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants