Repository navigation
Conversation
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
force-pushed
the
feat/2828-apple-interop
branch
from
December 1, 2026 04:03
1eec040 to
4bf6dc1
Compare
|
This pull request has no valid
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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'sobjc.NewBlockis the onedocs/architecture/layers.mddocuments as unsafe across Go stack moves.This adds a public
applepackage that forwards to those implementations instead of copying them:apple.NewBlock(fn any) objc.Blockandapple.NewBlockWithCleanup(fn any, cleanup func()) objc.Blockforward tointernal/platform/objcblock.apple.OnMain(fn func())forwards tointernal/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 asnsgo 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 tointernal/platform/apple.WithPool.The functions build for
GOOS=darwinandGOOS=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.gogets a rule lettingappleimport onlyinternal/platform/appleandinternal/platform/objcblock; the internal packages and their callers are unchanged.Docs: the macOS/iOS/tvOS section of
docs/public/en/native-code.mdand itszhmirror describe the package and when to use it,skills/nsgo/SKILL.mdtells agents to useapple.NewBlock,apple.OnMainandapple.WithAutoreleasePoolinstead ofobjc.NewBlock, andARCHITECTURE.md's code map anddocs/architecture/layers.mdrecord it. Achangelog.d/apple-interop.mdfragment records the addition.TestSkillsnow checks the skill'sapple.Xnames. Because the package builds only for Apple targets, the test type-checks all ofapple'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 aTestMainthat runs the main run loop as an app does):NewBlockWithCleanupis handed to a suspendedNSOperationQueueviaaddOperationWithBlock:, 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.NewBlockreceives(id, NSUInteger, BOOL *)from-[NSArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:]after GCs and stack growth, with the right object for every index.OnMaincalled from a background goroutine runs 32 functions on the main thread (+[NSThread isMainThread]) in submission order.WithAutoreleasePoolon 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 besidegamepadandcloud) 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-levelapple, 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
core,base,ui,tsx, thensgotool)Android, Windows, Linux, the web and mini programs have no Objective-C runtime; the package is empty there by design.
Screenshots
None
Verification
1eec040ca8be95e7b26697f838111dfe5f0dd374b84aa6d55419go tool nsgo test ./apple -- -count=1 -race→ 0go tool nsgo test -run '^(TestSkills|TestLayering.*)$' ./internal/moduletest→ 0go tool nsgo check -target macos_arm64,linux_x64,windows_x64,web -jobs 6→ 0go tool nsgo testsuite did not run.apple.OnMainis not modelled in headlessnsgotest: its functions run only while the app's main run loop runs, which the package's own tests provide through theirTestMain.Checklist
fix(linux): keep the caret after a paste; a squash merge uses it as the commit subject.changelog.d/fragment, not inCHANGELOG.md.