Bluish Coder

Programming Languages, Martials Arts and Computers. The Weblog of Chris Double.


Dojo and Spidermonkey

I attended the sessions on Dojo at the Ajax Experience conference to learn a bit about it.

Previously I'd considered Dojo to be a large, hard to understand library but I came away quite impressed with it after attending the seminars related to it. You can use the included package mechanism to make the base package fairly small and have components automatically loaded when required. One other neat factor is that Dojo can be used in a server side or command line Javascript environment.

I used Spidermonkey, the Mozilla C based implementation of Javascript, to try it out. I downloaded a nightly tarball of Dojo to get the source so I could set up a customised Dojo build for Spidermonkey.

Dojo has a 'profile' system that allows you to create a customised 'dojo.js' which contains Dojo packages that you want preloaded. Anything not preloaded will be loaded automatically when required by the package system. One file that is needed by Dojo is 'hostenv' which needs to be tailored for a specific Javascript implementation. There is a hostenv_browser for web browsers, hotenv_spidermonkey for spidermonkey, hostenv_rhino, etc.

To set up a customised dojo.js, which does nothing but makes it specific for Spidermonkey, I created a 'buildscripts/profiles/spidermonkey.profile.js' file with the following contents:

var hostenvType="spidermonkey";
var dependencies=[];

This is exactly the same as the 'minimal.profile.js' but with the hostenvType set to 'spidermonkey'. Once this is done a customised dojo.js system for Spidermonkey can be created with the following Ant command:

cd buildscripts
ant -Dprofile=spidermonkey -Ddocless=true release intern-strings

The custom Dojo build for Spidermonkey is created at 'release/dojo'. You can test it with Spidermonkey with something like:

cd release/dojo
js> load("dojo.js");
js> dojo.version
0.2.2+ (3802)

Many of Dojo's libraries are usable from Spidermonkey, including the package system and events. Here's an example demonstrating a usage of Dojo events from Spidermonkey:

cd release/dojo
js> load("dojo.js");
js> dojo.require("dojo.event.*");
js> var scheme = {}
js> scheme.jit = false
js> scheme.setJit = function(value) { this.jit = value }
js> var monitor = {}
js> monitor.watchJit = function() { print("Jit: " + scheme.jit) }
js> dojo.event.connect(scheme,"setJit",monitor,"watchJit")
[object Object]
js> scheme.setJit(true)
Jit: true
js> scheme.setJit(false)
Jit: false

The intent of this is to test decoupling some of the 'Scheme in Javascript' code from the browser DOM so it can be run from the command line. I don't want the code to explicitly check DOM objects so instead it keeps internal variables and browser specific code can watch these for changes to set checkboxes on the page.

In this case I use 'dojo.event.connect' so that when 'setJit' is called then 'watchJit' is called afterwards. As can be seen from above this works.


This site is accessable over tor as hidden service 6vp5u25g4izec5c37wv52skvecikld6kysvsivnl6sdg6q7wy25lixad.onion, or Freenet using key: