Previous Section | Next Section | Table of Contents | Glossary | Index |
Cocoa is one of Apple's APIs for GUI programming; for most purposes, development is considerably faster with Cocoa than with the alternatives. You should have a little familiarity with it, to better understand this section.
A small sample Cocoa program can be invoked by evaluating (REQUIRE 'TINY) and then (CCL::TINY-SETUP). This program provides a simple example of using several of the bridge's capabilities.
The Tiny demo creates Cocoa objects dynamically, at runtime, which is always an option. However, for large applications, it is usually more convenient to create your objects with Apple Interface Builder, and store them in .nib files to be loaded when needed. Both approaches can be freely mixed in a single program.
Clozure CL is ordinarily a command-line application (it doesn't have a connection to the OSX Window server, doesn't have its own menubar or dock icon, etc.) By opening some libraries and jumping through some hoops, it's able to sort of transform itself into a full-fledged GUI application (while retaining its original TTY-based listener.) The general idea is that this hybrid environment can be used to test and protoype UI ideas and the resulting application can eventually be fully transformed into a bundled, double-clickable application. This is to some degree possible, but there needs to be a bit more infrastructure in place before many people would find it easy.
Cocoa applications use the NSLog function to write informational/warning/error messages to the application's standard output stream. When launched by the Finder, a GUI application's standard output is diverted to a logging facility that can be monitored with the Console application (found in /Applications/Utilities/Console.app). In the hybrid environment, the application's standard output stream is usually the initial listener's standard output stream. With two different buffered stream mechanisms trying to write to the same underlying Unix file descriptor, it's not uncommon to see NSLog output mixed with lisp output on the initial listener.
The syntax of the constructs used to define Cocoa classes and methods has changed a bit (it was never documented outside of the source code and never too well documented at all), largely as the result of functionality offered by Randall Beer's bridge; the “standard name-mapping conventions” referenced below are described in his CocoaBridgeDoc.txt file, as are the constructs used to invoke (“send messages to”) Cocoa methods.
All of the symbols described below are currently internal to the CCL package.
ccl::@class |
ccl::@selector |
ccl::define-objc-method |
ccl::define-objc-class-method |
The Cocoa API is broken into several pieces. The Application Kit, affectionately called AppKit, is the one which deals with window management, drawing, and handling events. AppKit really wants all these things to be done by a "distinguished thread". creation, and drawing to take place on a distinguished thread.
Apple has published some guidelines which discuss these issues in some detail; see the Apple Multithreading Documentation, and in particular the guidelines on Using the Application Kit from Multiple Threads. The upshot is that there can sometimes be unexpected behavior when objects are created in threads other than the distinguished event thread; eg, the event thread sometimes starts performing operations on objects that haven't been fully initialized.
It's certainly more convenient to do certain types of exploratory programming by typing things into a listener or evaluating a “defun” in an Emacs buffer; it may sometimes be necessary to be aware of this issue while doing so.
Each thread in the Cocoa runtime system is expected to maintain a current “autorelease pool” (an instance of the NSAutoreleasePool class); newly created objects are often added to the current autorelease pool (via the -autorelease method), and periodically the current autorelease pool is sent a “-release” message, which causes it to send “-release” messages to all of the objects that have been added to it.
If the current thread doesn't have a current autorelease pool, the attempt to autorelease any object will result in a severe-looking warning being written via NSLog. The event thread maintains an autorelease pool (it releases the current pool after each event is processed and creates a new one for the next event), so code that only runs in that thread should never provoke any of these severe-looking NSLog messages.
To try to suppress these messages (and still participate in the Cocoa memory management scheme), each listener thread (the initial listener and any created via the “New Listener” command in the IDE) is given a default autorelease pool; there are REPL colon-commands for manipulating the current listener's “toplevel autorelease pool”.
In the current scheme, every time that Cocoa calls lisp code, a lisp error handler is established which maps any lisp conditions to ObjC exceptions and arranges that this exception is raised when the callback to lisp returns. Whenever lisp code invokes a Cocoa method, it does so with an ObjC exception handler in place; this handler maps ObjC exceptions to lisp conditions and signals those conditions.
Any unhandled lisp error or ObjC exception that occurs during the execution of the distinguished event thread's event loop causes a message to be NSLog'ed and the event loop to (try to) continue execution. Any error that occurs in other threads is handled at the point of the outermost Cocoa method invocation. (Note that the error is not necessarily “handled” in the dynamic context in which it occurs.)
Both of these behaviors could possibly be improved; both of them seem to be substantial improvements over previous behaviors (where, for instance, a misspelled message name typically terminated the application.)
Previous Section | Next Section | Table of Contents | Glossary | Index |