Classlojure Basics

I’m trying to hack around lwjgl’s basic limitations: it *really* wants to be a singleton. I’m hoping I’ll be able to use libgdx as a wrapper on top of it, but they seem to make matters worse because Android seems to be even fussier about the idea that you have 1 Application and 1 Window.

I pretty desperately want multiple windows.

I think that’s on Lwjgl’s roadmap, though I have seen posts from the developers that boil down to “If you want to do something like that, you aren’t building games. Go use something like jogl instead.”

I’m really hoping I’ll be able to work around this by using multiple class loaders to fool the library into believing that I’m running each instance as a singleton.

Classlojure seems like the perfect tool to make that happen, but the documentation leaves a bit to be desired.

The basic idea is simple enough: build a ClassLoader by calling something like

(def cl (classlojure/classlojure "file://${home}/.m2/repository/org/clojure/clojure/1.6.0/clojure-1.6.0.jar"))

and then call

(classlojure/eval-in cl ${something} ${args})

to actually use it.

The docs are a little fuzzy on what that ‘something should be. It really sounded like I wanted to make it a function call. It still seems like that’s probably what I ultimately want, but the unit tests don’t bother.

Based on the unit tests, I almost have a piece working. The ${something} expands out to

'(do (require 'my-project.blah) (my-project.blah/foo))

which has the same problem I ran across when I was providing a function. It can’t find the namespace. (It keeps throwing a FileNotFoundException).

Which is pretty frustrating. Since the file is in the same directory as the one that’s doing the require. (System/getProperty “java.class.path”) tells me that this namespace is chock-full of everything I’m used to dealing with.

The answer to this dilemma is also hidden in the unit tests. Specifically, the require-srcfile test. Though it’s subtle enough that it took me a while to notice (I blame exhaustion brought on by the newborn).

The classlojure function really  takes an ISeq of URLs that are important. I haven’t dug into what the code’s actually doing yet. But the practical upshot is that first “basic idea” form needs to look more like:

(def cl (classlojure/classlojure "file:src/" "file:/path/to/clojure/jar"))

I’m still getting errors about things that my source file is trying to require that should be on the classpath. So I’m guessing that I need to manually add in the rest of the pieces that I know I need, trimming out everything that I’m using as a luxury. This seems like the sort of thing that I’m used to having leiningen handling for me. Messing around with the classpath was one of the main reasons I avoided clojure for so many years.

I’m more than a little curious about whether Chas Emerick’s pomegranate library is the solution to this particular itch.

Either way. I think I see the light at the end of this particular tunnel. Even though it’s still definitely a work in progress.