Javascript Neural Networks

There are tons of neural network implementations out there, but not many in JavaScript. This is pretty surprising given that JavaScript is awesome and neural networks could really benefit from being in the browser. One partial implementation was used to do some sweet Captcha OCR, and my last post was about using them to determine whether to display black or white text over a given background color.

I ended up creating brain, the missing JavaScript neural network library. I tried to make it easy to use. To use it you don’t have to know what a hidden layer is (but you can specify hidden layers if you want), you can also specify input (and expected output, for training) as hashes instead of arrays – good for sparse or labelled input, and you can pass trained networks around in JSON, which is useful with Worker threads.

If you want to find out more using neural networks from a programmatic perspective, this is a good introduction that just popped up.

Playing Around With js ctypes on Linux

I’m pretty elated about the ctypes module introduced in Firefox 3.6. ctypes.jsm is a module that lets chrome code call functions from shared libraries. This is a big win for a lot of extension developers. The baseline is that you no longer have to create an XPCOM component to call C++ code from javascript.

I had wanted to speed up some calculation-heavy js I was using in my extension by writing it in C++. Writing a C++ XPCOM component would be a painful process, so I decided to ditch the effort…then I found out about ctypes!

Ctypes can help with calling Win API functions and such, and there are some examples of this on the ctypes.jsm wiki page for this. My use case however was loading my own shared library, so I decided to put together a short end-to-end tutorial on how to call your own C code from your extension.

First we write a little C function and put it in add.c:

int add(int a, int b) {
  return a + b;

To get a shared library from this code, compile with these commands:

gcc -fPIC -c add.c
gcc -shared -o add.o

Now you have a file called, which we can load from ctypes. Say you put in your addon’s content directory, then your javascript might look something like this:


function add(a, b) {
  var file = getFile("chrome://myext/content/");
  var lib =; 

  var addc = lib.declare("add",
                           ctypes.int32_t, // return type
                           ctypes.int32_t, // arg1 type
                           ctypes.int32_t // arg2 type
  return addc(a, b);

function getFile(chromeURL) {
  // convert the chrome URL into a file URL
  var cr = Components.classes[';1']
  var io = Components.classes[';1']
  var uri = io.newURI(decodeURI(chromeURL), 'UTF-8', null);
  var fileURL = cr.convertChromeURL(uri);
  // get the nsILocalFile for the file
 return  fileURL.QueryInterface(Components.interfaces.nsIFileURL).file;

Now try calling your add function from js. The getFile function isn’t that pretty, hopefully in the future there will be a way to open a library from a chrome url. Also, you can’t use ctypes from Worker threads, so that is very sad.

Configure Apache To Accept Cross-Site XMLHttpRequests on Ubuntu

Update: check out this for more up-to-date instructions:

1. Make sure you have the mod_headers Apache module installed. to do this check out /etc/apache2/mods-enabled/ and see if there’s a ‘headers.load’ in there. If there isn’t then just sudo ln -s /etc/apache2/mods-available/headers.load /etc/apache2/mods-enabled/headers.load

2. Add the Access-Control-Allow-Origin header to all HTTP responses. You can do this by adding the line Header set Access-Control-Allow-Origin "*" to the desired <Directory> section in your configuration file (like the /etc/apache2/sites-available/default file). Saying "*" will allow cross-site XHR requests from anywhere. You can say "" to only accept requests from that origin.

3. Reload apache server. sudo /etc/init.d/apache2 reload

Maybe this is really obvious to a lot of people, but it wasn’t to me, so there you go.

Bugzilla Web Services fun

So if you try to call a method from a module that doesn’t exist in Bugzilla web services (in my case, I was trying to call ‘bugzilla.version’ but I should have been calling ‘Bugzilla.version’) then it will return a really cryptic and unhelpful fault with the text “Can’t call method “login_exempt” on an undefined value at Bugzilla/WebService/ line 25″. Just in case anyone else runs into this.

Also, you must use, not or it will return a 411 error.


There should be a new standard for colors, rgbal. Where you can specify red, green, blue, alpha, and luminosity values. The luminosity (I’m probably mis-using this word here, but b for brightness was taken) would control the brightness of a particular pixel on the screen (display brightness, not hsb brightness), which right now seems to be controlled in an all-or-nothing way often using special keys on your keyboard. Can you imagine how cool (/inaccessible!) things could get?

Treeview Hang on Dragging

dragging onto a xul tree that has a treeview defined hangs the browser. I’ve defined the nsITreeView function canDrop and it returns true always. /-:

UPDATE: I defined getParentIndex (nsITreeView), but didn’t implement it, this function has to be implemented for a drop operation and must return -1 if the row has no parent. agh.


Linux breaks 1% market share on the client? nice! Still not nearly enough. I can’t believe more people don’t use Linux, especially over Windows. I like Macs fine and enjoy OS X and several aspects of the user experience, but for programming, it’s hands down Linux, there’s nothing more straightforward. I even prefer Linux as a regular user, it’s on my PC (Ubuntu right now). I especially like the super fast and seamless installation of free and open source software. The only encumbrance is the lack of good-quality substitutes for some software products, I’m not totally satisfied with the word processing, vector graphics, and image editing open source software right now, but that could change if Linux keeps on increasing it’s market share. It’s FREE, people! All you need to do is burn it on a regular CD-R, insert the CD in your computer, and hit F11 or whatever when you reboot your computer. Unbelievable.