Daniel's Stuff

I write code

Category: Cocoa

The first stage of my port of pegged to Swift is successful :)

I have managed to get the Objective-C version of pegged to spit out a parser using Swift rather than Objective-C 🙂

It’s getting late, so I really should get some sleep.

I finally figured out what was causing the compiler to crash.  The following code will crash the Swift compiler unless the inout parameter to the block type is removed.

typealias MathFunction = (a: Double, b:Double, inout x:Int) -> Double
var counter = 0
class Executor {
    var fn: MathFunction
    init(fn: MathFunction) {
        self.fn = fn
    func execute(a: Double, b: Double) -> Double {
        return fn(a: a, b: b, x: &counter)

Objective-C to Java and back again via JNI and Unix pipes

I’ve been working on a piece of software for some time now that is almost ready for a public release.  For part of it I needed to connect to various databases, some of which are only accessible via JDBC.  My problem is that my app is written in Objective-C.  For the first version of my app this wasn’t a problem as Apple was promoting their brand new Objective-C to Java bridge and everything worked really nicely.  That was until a couple of years ago when Apple decided to deprecate the Java bridge.  I rewrote that code to use JNI and all was good.  Then Apple brought out the Mac App store but announced that it would not accept any apps that link directly to the JVM.  This causes a problem for me as without the JDBC drivers the usefulness of my app is greatly reduced.

I looked into various ways of embedding the JVM into my app, but the problem with doing that was that it increased my app from less than 10Mb to around 100Mb.  I wasn’t too happy about that to say the least, so I looked into providing a way to make my app optionally use the system JVM allowing the Java support to be turned off.  This worked pretty well unless the machine didn’t have a JVM installed in which case my app would refuse to load due to linkage failures.  So, I had the bright idea to use Apples “new” XPC feature.  I reorganised my JNI code to allow it to be called from an XPC process and found that the security settings meant that the JVM could not access any of the JDBC driver files in my app bundle. I got around that by using a folder shared with a process group.  This allows the JDBC drivers to be loaded, but then I found that for some reason the JNI code that had been running very nicely for a couple of years didn’t like running inside an XPC process and would fail randomly with strange errors.  On top of that I found that the process of going from Java, through JNI to Objective-C, then from the XPC process over to the main app was very slow and was especially noticeable with some of my larger data sets.

In desperation I decided to abandon the JNI code altogether and see if I could communicate from the main app to the java code in some other way.  In the end I created a Java JDBC connector that communicates with the main app via a pair of Unix pipes.  I have found this to amazingly fast and extremely reliable.  Instead of the data going through several transformations from Java, through to JNI and then to Objective-C and so on it now goes directly from the Java code in a special serialised format directly into the Objective-C code via the pipes.  It works really well 🙂

I have just uploaded the next release of i48 for review

The latest version of i48 has been submitted review by Apple.

This new version adds support for running the emulation at full speed (although that has some interesting effects on usability).  I have also added theme support and included a theme based on Power48 provided by Fabrice Roux.  Finally the buttons now highlight when they are selected making it a bit easier to use the keyboard.
I was hoping to add sound support in this release, but have decided to submit what I have now and work on the sound support for the next release.

BlogThing 2.0 has finally been released

After what seems like ages I have finally released BlogThing 2.0.  This new release is totally rewritten from scratch and provides some very nice new features.  The coolest one is the new video upload facility.  Just add a video file to the page and press the Post button.  BlogThing will encode the file and upload a custom Flash based video player.  It just works automagically 🙂

More progress on the HP48GX emulator for the iPhone

I had a bit more of a play with the emulator code tonight and fixed up a few little glitches.  I also changed the color of the emulation screen to look more like the real thing 🙂  Now off to bed!  It also looks like it is OK to distribute the ROM file as apparently HP released the ROMs to the public some time in 2000.  The github repository (http://github.com/dparnell/i48/tree/master) has been updated to include the ROM file.

A HP48GX emulator for the iPhone and iPod Touch

I have just released the source to a HP48GX emulator for the iPhone and iPod Touch.

It’s a very rough port of x48 to the iPhone, but it works 🙂  There is the occasional screen glitch and I’m not too happy with the screen buffer code.  Something else that is a bit annoying is that it appears that compiling in Debug mode to run on the device does not work.  It looks like one of the case statements in the x48 emulation code is too big to be compiled for ARM under gcc.  I had a quick look at using the LLVM compiler but it didn’t seem to like it.
The code can be downloaded from http://github.com/dparnell/i48/tree/master 
Another thing to be aware of is that the code does not include a ROM, but they are fairly easy to come by with a bit of googling 🙂