2 Commits
2 changed files with 12 additions and 26 deletions
+9 -17
View File
@@ -89,9 +89,9 @@ And include the Haskell stub header:
## Linking ## Linking
To link our Haskell library into our Swift app, we'll build it To link our Haskell library into our Swift app, we'll build it
as a dynamic library and add it to our Xcode project. Building as a dynamic library and add it to our Xcode project. Building a
a static library is also possible, but currently requires static library is also possible, but currently requires
[building your own GHC][pic-ghc]. [rebuilding the GHC standard libraries][pic-ghc].
[pic-ghc]: https://github.com/lyokha/nginx-haskell-module#static-linkage-against-basic-haskell-libraries [pic-ghc]: https://github.com/lyokha/nginx-haskell-module#static-linkage-against-basic-haskell-libraries
@@ -162,23 +162,17 @@ to copy the libraries into the app bundle:
## Runtime Setup ## Runtime Setup
Before using functions from Haskell, we'll need to start its Before using functions from Haskell, we'll need to start its
runtime. `hs_init` takes pointers to C's argc and argv, however runtime. `hs_init` takes pointers to C's argc and argv. In
since we aren't using command line arguments, we can just pass a Swift, these are available as `CommandLine.argc` and
constant array. `CommandLine.unsafeArgv`.
Add this to your app delegate's `applicationDidFinishLaunching` Add this to your app delegate's `applicationDidFinishLaunching`
method: method:
func applicationDidFinishLaunching(_ aNotification: Notification) { func applicationDidFinishLaunching(_ aNotification: Notification) {
var argv0 = Array("SwiftHaskell".utf8CString) var argc = CommandLine.argc
argv0.withUnsafeMutableBufferPointer { argv0bp in var argv = Optional.some(CommandLine.unsafeArgv)
var argv = [argv0bp.baseAddress]; hs_init(&argc, &argv)
var argc = CInt(argv.count)
argv.withUnsafeMutableBufferPointer { argvbp in
var argvp = argvbp.baseAddress
hs_init(&argc, &argvp)
}
}
} }
And the corresponding `hs_exit` to `applicationWillTerminate`: And the corresponding `hs_exit` to `applicationWillTerminate`:
@@ -187,8 +181,6 @@ And the corresponding `hs_exit` to `applicationWillTerminate`:
hs_exit() hs_exit()
} }
TODO: Explain swift pointer bits.
## Calling Haskell from Swift ## Calling Haskell from Swift
Add a new label to the window in `MainMenu.xib` for us to write Add a new label to the window in `MainMenu.xib` for us to write
+3 -9
View File
@@ -15,15 +15,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var label: NSTextField! @IBOutlet weak var label: NSTextField!
func applicationDidFinishLaunching(_ aNotification: Notification) { func applicationDidFinishLaunching(_ aNotification: Notification) {
var argv0 = Array("SwiftHaskell".utf8CString) var argc = CommandLine.argc
argv0.withUnsafeMutableBufferPointer { argv0bp in var argv = Optional.some(CommandLine.unsafeArgv)
var argv = [argv0bp.baseAddress]; hs_init(&argc, &argv)
var argc = CInt(argv.count)
argv.withUnsafeMutableBufferPointer { argvbp in
var argvp = argvbp.baseAddress
hs_init(&argc, &argvp)
}
}
label.stringValue = "\(square(5))" label.stringValue = "\(square(5))"
} }