[F-Script-talk] adding Blocks as methods to ObjC classes
Brought to you by:
pmougin
From: Luke N. <l_n...@ya...> - 2005-02-15 05:45:05
|
Hi, I started work on a new software project and decided that I would include F-Script in it from the ground up. The kind of software I'm building has both a end user development/scripting function as well as a deployment function where efficiency is key. Given F-Script's impressive level of integration with ObjC and Cocoa I decided that I would implement all of the scripting and development capability in F-Script. I thought it would be great to give end developers using this software the flexibility to implement methods in F-Script first for prototyping and rapid deployment, and then later recode those methods that were relatively stable and debugged, and needed the additional speed, into ObjC. I therefore added a few methods to the base class in my class hierarchy to support runtime attachment of F-Script Blocks as methods that can be called transparently, using the standard messaging syntax, from either the ObjC or the F-Script side. For example, the following example script adds the method gimmeFive to an object loaded from the framework I'm developing, which is called the Tenth Art Tools. The TATestingDummy object is an ObjC object with no declared methods or member variables. It inherits all of its functionality from TAObject, the base object in the Tenth Art Tools, of which it is a direct descendant. > TATestingDummy attachFSBlock: [5] toSelector:#gimmefive withArgs:''. tadummy := TATestingDummy alloc init. tadummy gimmefive. 5 Although this example is very simplistic the framework supports up to 11 arguments and inheritance and overriding of methods just like ObjC The created Blocks are callable from the ObjC side as well. If an ObjC method of the same name exists it will always override the F-Script Block method, so you can't override compiled-in methods with this code, you can just add new ones. Please note that so far the technology only allows for the addition of instance methods, not class methods. Adding this functionality has really come in handy in situations where I want to script a method for an object. For example, I am using fscripter to create the testbed for the framework I'm developing. Part of the testbed creates an NSOutlineView to view the test results in an organized fashion. Being able to add methods to this dummy object allowed me to create the delegate object that NSOutlineView uses to display its data and script all of its methods as blocks in F-Script. Although it is not in place yet, the scripted methods will have access to member variables via Cocoa Bindings, mainly Key Value Coding. This means that any object that supports KVC could potentially have attachable methods that are modifiable on the fly and have access to all of the functionality that a regular ObjC method has, yet are changeable during runtime without recompile or even program suspension. As the the Tenth Art Tools are ultimately going to be GPLed or some similar open source model I would be happy to share this code or the design with anyone interested and would welcome feedback on this idea. Did I re-invent the wheel? Is there some handy dandy F-Script way to do this and I just missed it on my read-through of the documentation? Is this code already implemented, say in the JGAdditions? Is there any interest in incorporating this functionality in some way to F-Script itself? If so, it would be an honor. Did I completely not get the point of this language and I'm misusing horribly? I hope not. Feel free to let me know what you think. Thanks, Luke Nihlen |