swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.57k stars 10.35k forks source link

[SR-2995] Support for a main program not written in Swift #45585

Open swift-ci opened 8 years ago

swift-ci commented 8 years ago
Previous ID SR-2995
Radar None
Original Reporter DemiMarie (JIRA User)
Type New Feature
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Standard Library | |Labels | New Feature, Runtime | |Assignee | None | |Priority | Medium | md5: dbf0d6ac2128629b9a736bc6d1d99367

Issue Description:

This is a feature request for having a main program not written in Swift.

Proposed API (based on Haskell):

/**
 * Initializes the Swift runtime system.
 * @return 0 on success, the number of times the runtime has already been initialized
 * (if the runtime _has_ been initialized), or a negative
 * number on failure (presumably because the system ran out of memory).
 *
 * After this function has returned, it is safe to call into Swift from any thread.
 *
 * This function is thread-safe and may be called multiple times, but
 * each call to `swift_init()` must be balanced by one call to `swift_exit()`
 * to avoid leaking resources.
 */
ssize_t swift_init(void);

/**
 * Shuts down the Swift runtime.  If the number of calls to `swift_exit()`
 * matches the number of calls to `swift_init()`, deinitialize the runtime
 * and release associated resources.
 *
 * Returns 0 if the runtime has been shut down.  If the runtime is still running,
 * returns the number of calls to `swift_exit()` needed to shut it down.
 * If the runtime is already shut down, returns a negative number.
 *
 * This function is thread-safe.
 */
ssize_t swift_exit(void);
belkadan commented 8 years ago

FWIW, Swift's runtime has a very tiny initialization cost (two static constructors, plus one more if you use Dispatch on an Apple platform), so there's not much need for a swift_init today. (Swift already works fine when linked with someone else's main function.) swift_exit is a little more interesting, but I'm not sure you actually need it in practice—how often do you shut down part of your program in a way you'd be able to start up again, rather than using a separate process?