2 changed files with 12 additions and 26 deletions
+9 -17
View File
@@ -89,9 +89,9 @@ And include the Haskell stub header:
## Linking
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
a static library is also possible, but currently requires
[building your own GHC][pic-ghc].
as a dynamic library and add it to our Xcode project. Building a
static library is also possible, but currently requires
[rebuilding the GHC standard libraries][pic-ghc].
[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
Before using functions from Haskell, we'll need to start its
runtime. `hs_init` takes pointers to C's argc and argv, however
since we aren't using command line arguments, we can just pass a
constant array.
runtime. `hs_init` takes pointers to C's argc and argv. In
Swift, these are available as `CommandLine.argc` and
`CommandLine.unsafeArgv`.
Add this to your app delegate's `applicationDidFinishLaunching`
method:
func applicationDidFinishLaunching(_ aNotification: Notification) {
var argv0 = Array("SwiftHaskell".utf8CString)
argv0.withUnsafeMutableBufferPointer { argv0bp in
var argv = [argv0bp.baseAddress];
var argc = CInt(argv.count)
argv.withUnsafeMutableBufferPointer { argvbp in
var argvp = argvbp.baseAddress
hs_init(&argc, &argvp)
}
}
var argc = CommandLine.argc
var argv = Optional.some(CommandLine.unsafeArgv)
hs_init(&argc, &argv)
}
And the corresponding `hs_exit` to `applicationWillTerminate`:
@@ -187,8 +181,6 @@ And the corresponding `hs_exit` to `applicationWillTerminate`:
hs_exit()
}
TODO: Explain swift pointer bits.
## Calling Haskell from Swift
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!
func applicationDidFinishLaunching(_ aNotification: Notification) {
var argv0 = Array("SwiftHaskell".utf8CString)
argv0.withUnsafeMutableBufferPointer { argv0bp in
var argv = [argv0bp.baseAddress];
var argc = CInt(argv.count)
argv.withUnsafeMutableBufferPointer { argvbp in
var argvp = argvbp.baseAddress
hs_init(&argc, &argvp)
}
}
var argc = CommandLine.argc
var argv = Optional.some(CommandLine.unsafeArgv)
hs_init(&argc, &argv)
label.stringValue = "\(square(5))"
}