September 8th 2004
Plugin Systems
One of my tasks I spent a lot of time today contemplating is a plugin system. I always enjoyed developing plugin systems. I’ve done two so far, and one more that is unreleased (upcoming product). There’s a joy that I get when I am able to define a set of functions and load up a random shared library that extends my application.
I think it’s part of my enjoyment of the lower-level of things. Sure, loading shared libraries isn’t low-level, but what I’m grasping at is I’m one of those geeks who enjoys understanding how things work.
For example, my first step into assembly/machine code was back in May when I figured out a way to declare into Objective-C frameworks in pure REALbasic code. To do that, I learned a lot about the Objective-C messaging system, as well as a lot of things about how parameters are passed in the PowerPC runtime. However, once you can do that, you realize that there’s just not much that Cocoa has to offer that Carbon doesn’t also provide.
Then an idea clicks, and you remember a few items, like the NSStatusBar. However, there’s a problem with trying to use that from a Carbon based application. The Objective-C framework needs to call back into your application, and it does so through a NSObject subclass of some sort.
So, without hesitation, I created the ability to subclass NSObject in REALbasic. I had it working, but the methods had to be global, because the parameters lined up like this:
Obj-C
objectiveCMethod( id theNSObject, SEL theSelector, … )
REALbasic
objectiveCMethod( self as Object, … )
So, I needed to somehow, at the minimal, put a REALbasic instance into the first parameter. Well, that’s register 3, so with the help of a friend that knew assembly, we constructed 8 opcodes that would basically put r3 in r4, and then put a specified address of a REALbasic object into r3, and then proceed to call the original function.
The address of that chunk of 8 opcodes was then fed to the Objective-C runtime as the real address of the function. After many long hours of debugging, I finally got my feeble MsgBox called from inside of Objective-C. That project was fun.
Fast forward a few months later, and I wrote the beginnings of my own PPC assembler. While I had reasons behind it, I actually put that project on hold. It was mainly a fun thing to do, and I really enjoyed finding out that Motorola gives out the reading material about their processors for free
And this morning. Last night on a mailing list, the topic of OS X screensavers written in REALbasic came up. If no one is familiar with the API on Mac OS X for creating screensavers, it requires Cocoa. Well, there is no way around that, really. At the minimal, there has to be a stub bundle that relays messages to your application.
I wanted to take it a bit further. I haven’t been successful yet, but my idea is to basically perform the equivalent of exec (POSIX call), but not provide a protected memory environment. Why? Because then I can give it the information of what contexts to draw to and so forth. How this is going to work is a bit ugly, but it should work in the end. First, you read in all of the Mach-O REALbasic app. Next, you find the main function offset, and finally, you call that pointer.
It is a hack, in some regards, but when Apple gives you Oranges, what’s a person to do?