Skip to content

feat(apple): expose GC-safe blocks, OnMain and autorelease pools to apps - #2836

Open
houko wants to merge 3 commits into
mainfrom
feat/2828-apple-interop
Open

houko wants to merge 3 commits into
mainfrom
feat/2828-apple-interop

Conversation

@houko

@houko houko commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Summary

Apps that call an Apple framework NSGo does not wrap (ScreenCaptureKit, AVAssetWriter and the like) through purego had no public way to make GC-safe Objective-C blocks, run work on the main thread, or drain autorelease pools off it: the implementations NSGo's own backends use live under internal/, and purego v0.11.3's objc.NewBlock is the one docs/architecture/layers.md documents as unsafe across Go stack moves.

This adds a public apple package that forwards to those implementations instead of copying them:

  • apple.NewBlock(fn any) objc.Block and apple.NewBlockWithCleanup(fn any, cleanup func()) objc.Block forward to internal/platform/objcblock.
  • apple.OnMain(fn func()) forwards to internal/platform/apple.OnMain (main dispatch queue, never waits, submission order). Its functions run only while the main thread's run loop runs: in a running app and in tests on an Apple target such as nsgo test -target macos, never in a headless test, which runs no run loop. The doc comment, the docs and the skill say so.
  • apple.WithAutoreleasePool(fn func()) forwards to internal/platform/apple.WithPool.

The functions build for GOOS=darwin and GOOS=ios (macOS, iOS, tvOS, watchOS); on other targets the package holds only its doc comment, so code calling it goes in Apple-only files. internal/moduletest/layering_test.go gets a rule letting apple import only internal/platform/apple and internal/platform/objcblock; the internal packages and their callers are unchanged.

Docs: the macOS/iOS/tvOS section of docs/public/en/native-code.md and its zh mirror describe the package and when to use it, skills/nsgo/SKILL.md tells agents to use apple.NewBlock, apple.OnMain and apple.WithAutoreleasePool instead of objc.NewBlock, and ARCHITECTURE.md's code map and docs/architecture/layers.md record it. A changelog.d/apple-interop.md fragment records the addition.

TestSkills now checks the skill's apple.X names. Because the package builds only for Apple targets, the test type-checks all of apple's non-test files whatever their build constraints, so the check works on Linux and Windows hosts too.

Tests (apple/apple_darwin_test.go, macOS, with a TestMain that runs the main run loop as an app does):

  • A block from NewBlockWithCleanup is handed to a suspended NSOperationQueue via addOperationWithBlock:, the caller's reference is released, the test goroutine grows its stack and forces GCs, then the queue resumes: the callback runs off the main thread with its captured state, and cleanup runs exactly once after the queue drops its copy.
  • A block from NewBlock receives (id, NSUInteger, BOOL *) from -[NSArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:] after GCs and stack growth, with the right object for every index.
  • OnMain called from a background goroutine runs 32 functions on the main thread (+[NSThread isMainThread]) in submission order.
  • WithAutoreleasePool on a non-main goroutine keeps an autoreleased object alive inside the pool and releases it when the pool pops (observed through a weak reference).

The package name (apple, top-level beside gamepad and cloud) is new public API surface; see the open question below.

Open question for a maintainer: the issue leaves the package name open (native/darwin, platform/apple, ...). This PR uses a top-level apple, matching the flat domain packages and NSGo's own naming of Apple targets (apple.info, "Apple backends"). Renaming before release is cheap; after release it is a breaking change.

Fixes #2828

Targets

  • Shared code (core, base, ui, tsx, the nsgo tool)
  • macOS
  • iOS
  • tvOS
  • watchOS
  • Android
  • Windows
  • Linux
  • Web
  • Mini programs
  • Docs or website only

Android, Windows, Linux, the web and mini programs have no Objective-C runtime; the package is empty there by design.

Screenshots

None

Verification

  • head: 1eec040ca8be95e7b26697f838111dfe5f0dd374
  • base: b84aa6d55419
  • commands:
    • go tool nsgo test ./apple -- -count=1 -race → 0
    • go tool nsgo test -run '^(TestSkills|TestLayering.*)$' ./internal/moduletest → 0
    • go tool nsgo check -target macos_arm64,linux_x64,windows_x64,web -jobs 6 → 0
  • result: pass
  • not covered: the tests ran on an x86_64 Mac with Command Line Tools only (no Xcode, no GUI session), so this head was not vetted for iOS (the build host has no iOS SDK; an earlier head of this branch was) and no iOS, tvOS, watchOS or arm64 macOS runtime test ran; the full go tool nsgo test suite did not run. apple.OnMain is not modelled in headless nsgotest: its functions run only while the app's main run loop runs, which the package's own tests provide through their TestMain.

Checklist

  • The title is a Conventional Commit, such as fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.
  • Docs whose facts change are updated, and a capability users can see has a line in a changelog.d/ fragment, not in CHANGELOG.md.
  • New third-party code, assets and dependencies name their source and license.

@github-actions github-actions Bot added enhancement New feature or request size/M documentation Improvements or additions to documentation local-ci-pass The head commit declares that its local checks passed labels Dec 1, 2026
houko added 3 commits December 1, 2026 13:03
Apps calling Apple frameworks NSGo does not wrap get a public apple package that forwards to internal/platform/objcblock and internal/platform/apple instead of copying them.

Fixes #2828
…pple names

OnMain's functions run only while the main run loop runs, which a headless test never does; say so in the doc comment, native-code.md and the skill. Mirror the apple section in the Chinese native-code.md. TestSkills now type-checks the apple package from all its files so it verifies apple.X names on any host, and the Foundation block test reads its results under the mutex.
@houko
houko force-pushed the feat/2828-apple-interop branch from 1eec040 to 4bf6dc1 Compare December 1, 2026 04:03
@github-actions github-actions Bot added ci local-ci-pass The head commit declares that its local checks passed local-ci-skipped and removed local-ci-pass The head commit declares that its local checks passed labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

This pull request has no valid Local CI declaration for its head commit, so a maintainer audits it before merging:

  • head names 1eec040ca8be95e7b26697f838111dfe5f0dd374, not the pushed head 4bf6dc1efa3a776aebd6462b6347110b52ee74c7

Run the checks on the head commit, fill the block in the template, then push or edit the body: this label and comment update on their own.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci documentation Improvements or additions to documentation enhancement New feature or request local-ci-pass The head commit declares that its local checks passed local-ci-skipped size/M

Projects

None yet

1 participant