[Openmcl-devel] More questions on Cocoa bridge & Bosco

Gary Byers gb at clozure.com
Fri Apr 23 19:37:11 PDT 2004



On Fri, 23 Apr 2004, Thom Goodsell wrote:
>
> I've made some progress, and now I have a couple more questions:
>
> Working with attributed strings, I figured out that I can't use
> #$NSForegroundColorAttributeName as a dictionary key (or as anything,
> for that matter), but that using #@"NSColor" does what I want. Is there
> any way to divine the mapping from NS...AttributeName to the NSString?

NSForegoundColorAttributeName is an ObjC global variable, presumably defined
something like:

NSString *NSForegoundColorAttributeName = @"NSColor";

The #$ reader macro works fairly well for constants (defined with
#define or as part of an "enum" form.)

There isn't currently a convenient way to refer to a foreign variable
in OpenMCL's FFI (there's an inconvenient way ...); the standard C
library only defines a handful of global variables, and a mechanism to
deal with them seemed like overkill.  They're more common in Cocoa
(and perhaps in other interesting libraries), so it'd be nice to steal
another reader-macro character and be able to say something like:

#?NSForegroundColorAttributeName

to refer to foreign variables.


The function CCL::FOREIGN-SYMBOL-ADDRESS is documented (and the symbol
should be exported.)  On OSX, the names of all foreign symbols begin
with an underscore:

? (ccl::foreign-symbol-address "_NSForegoundColorAttributeName")

should return a pointer to the address of that particular variable.
Since we happen to know that the value stored at that address is
itself a pointer (to an NSString), we can use %GET-PTR to get that
value:

? (%get-ptr (ccl::foreign-symbol-address "_NSForegoundColorAttributeName"))
#<A MacPTR ...>

If the ObjC-CLOS integration was a little smarter, it would have recognized
that pointer as "an instance of some ObjC string class".  That recognition
is heuristic; it's apparently fooled by the fact that the way that the
string is allocated (or something.)  If we're really sure that that's
an NSString, there are several ways to print/see its value; one way is
to simply pass it to use #_NSLog (this would actually work for any
NSObject pointer):

? (#_NSLog #@"%@" :id (%get-ptr (ccl::foreign-symbol-address "_NSForegoundColorAttributeName")))

That should print a line of debugging info containing the value of the
string.

A reader macro for foreign variables would certainly be simpler.




More information about the Openmcl-devel mailing list