On May 1, 2011, at 2:26 PM, David Chisnall wrote:

> On 1 May 2011, at 12:19, Mathieu Suen wrote:
> 
>> Hi All,
>> 
>> In my objective-c-smallltalk bridge now I need to setup a value of an 
>> instance variable.
>> On osx I used the function object_setIvar, but in gnustep there is no such 
>> method.
>> 
>> undefined symbol: object_setIvar
> 
> This function does exist in trunk, just not in the latest stable releases.
> 
>> How do I set a value of an instance variable with the runtime?
> 
> Using this function is almost certainly wrong anyway, for two reasons:
> 
> 1) It only works correctly with objects - if you try to set a char instance 
> variable with it, for example, you will corrupt your object.
> 
> 2) With a non-fragile ABI, ivars can be declared in class extensions, so an 
> object may have two ivars with the same name, coming from different classes.  
> This function does not allow you to disambiguate them.
> 
> The correct way of doing it is to use class_getInstanceVariable(), then 
> ivar_getTypeEncoding() and ivar_getOffset().  Use the result from the first 
> call to box / unbox the value correctly for the corresponding Objective-C 
> type.  Then add the value returned by the second to the address of the object 
> to find the correct memory location.  


Thanks David for the help.

Now I have a running example of the bridge :)
You can find the source here:
https://github.com/mathk/gst-objc

Of course a lot of polishing need to be done

The canonical example is:
--------
Eval [
    PackageLoader fileInPackage: 'Objc'.
]

Objc.ObjcObject subclass: MyNSView [
    <objcSubclass: 'NSView'>

    drawRect: rect [
        "d or f depending on 64 or 32 bit arch (should be simpler using 
Objc.NSRect typeStr...)"
        <objcTypeStr:'v@:{_NSRect={_NSPoint=dd}{_NSSize=dd}}'>
        | nsColor |
        nsColor := Objc.ObjcRuntime at: 'NSColor'.
        nsColor redColor set.
        Objc.ObjcAppKit nsRectFill: self bounds
         
    ]
]

Eval [
  | nsClass nsInstance nsApplication nsApplicationClass nsBundle nsApp nsAuto 
object |

  nsApplication := Objc.ObjcRuntime at: 'NSApplication'.
  nsApp := nsApplication sharedApplication.
  nsRect :=  Objc.NSRect gcOriginX: 0.0
                    y: 0.0
                    width: 100.0
                    height: 100.0.

  nsWindow :=  Objc.ObjcRuntime at: 'NSWindow'.
  nsWindow := nsWindow alloc.
  nsWindow initWithContentRect: nsRect styleMask: 15 backing:Objc.ObjcAppKit 
nsBackingStoreBuffered defer: (Character value:1).

  nsWindow setTitle: 'Test windows' asNSString.

  view := MyNSView noRetainAlloc.
  view init.
  view autorelease.

  nsWindow setContentView: view.
  nsWindow center.
  nsWindow orderFront: 0.
  nsWindow contentView.
  nsApp run.

]
--------

> 
> David
> 
> -- Sent from my Apple II
> 
> 
> _______________________________________________
> Etoile-discuss mailing list
> [email protected]
> https://mail.gna.org/listinfo/etoile-discuss


_______________________________________________
Etoile-discuss mailing list
[email protected]
https://mail.gna.org/listinfo/etoile-discuss

Répondre à