[Openmcl-devel] set-objc-instance-variable and ints?

Gary Byers gb at clozure.com
Mon Jun 30 23:49:53 UTC 2003

On Mon, 30 Jun 2003, Brendan Burns wrote:

> Hello,
> I defined an objc-class as follows:
> (define-objc-class "foo" "NSObject"
> 	(published :int))
> assume *foo* is an instance of this class, I try
> (set-objc-instance-variable *foo* "published" 0)
> and I get an error that 0 is not of type macptr, is that right?
> --brendan

For some (obscure) value of "right", yes.

A comment in front of the definition of SET-OBJC-INSTANCE-VARIABLE
says "This works best when the value is a pointer of some sort ...".

In this case, "best" means "works at all", and "less-than-best" means
"doesn't work at all"; SET-OBJC-INSTANCE-VARIABLE is just a wrapper
around the runtime function #_object_setInstanceVariable, which insists
at the FFI level that its value should be a pointer.

There are a few workarounds, many of them sleazy.

 a) coerce the value to a pointer, if possible.

 b) use %IVAR-OFFSET to find the instance variable's (byte) offset
    within instances of its class, and use memory accessing primitives
    (%GET-LONG, etc.) to access it.

 c) decipher what DEFINE-OBJC-METHOD does: it tries to wrap something
    like WITH-SLOTS around the method body, so that instance variables
    of SELF's class can be referenced/assigned to conveniently.  The
    macro WITH-IVAR-SYMBOL-MACROS looks like it knows how to do this,
    at least for lisp-defined ObjC classes.

    Come the revolution, it'll (hopefully) be possible to just say

    (with-slots (published) *foo* ...)

Openmcl-devel mailing list
Openmcl-devel at clozure.com

More information about the Openmcl-devel mailing list