Addressing foreign handlers (FFI) to instance methods

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
Simon Knight
Posts: 628
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Addressing foreign handlers (FFI) to instance methods

Post by Simon Knight » Thu Feb 06, 2020 11:41 am

Hi, I apologise for what are some more "dumb" questions but I can't get my LCB code to work, yet I feel I am close to doing so. At the moment I am unable to get calls to instance methods (-) to work. Either I'm getting the syntax wrong or the call is being sent to the class rather than the object. The compiler reports "unknown language" error which is very perceptive of it. In short I'm out of my depth. The following is what I think I understand.

In object programming a "class" defines an object giving it properties and methods. Once an "instance" of the class is created then generally all calls are directed to the instance. However, I am not clear how the FFI in LCB directs method calls to the correct object.

I believe that when a new foreign instance is created an objcID pointer is returned to LCB and this may be stored in a variable. So with this in mind is the first parameter in a foreign handler definition always this local variable i.e. a pointer to the Objective-C object being addressed? Are subsequent parameters in the foreign handler definition the values passed to the method being called, filling in the places after the colons in the Objective-C method name?

The documentation :
The Obj-C binding string
The Obj-C binding string has the following form:

Here library specifies the name of the library or framework to bind to (if no library is specified a symbol from the engine executable or a library it is linked to is assumed).

Here class specifies the name of the class containing the method to bind to. If the method is an instance method, the class can be omitted, creating a 'dynamic binding', i.e. just resolving the selector.

Here method specifies the method name to bind to in standard Obj-C selector form, e.g. addTarget:action:forControlEvents:. If the method is a class method then prefix it with '+', if it is an instance method then prefix it with '-'.
Reading the documentation seems to imply that when calling a instance method that all is required is the -methodName:

Code: Select all

private foreign handler ObjC_IncrementScore(in pID_of_ScoreBoardObject as ObjcID, pIncrementBy as CUInt)  / 
         returns optional CUInt  binds to "objc:-IncrementTotalScore:"
meaning that an object pointed at by "pID_ScoreBoardObject" is being sent the message "IncrementTotalScore" by the value stored in variable "pIncrementBy".

Am I anywhere near to being correct ?

Many thanks for your patience and any light you may shine into the darkness....
best wishes

Post Reply

Return to “LiveCode Builder”