fabricedesre/b2gremote — pull request #4 by mykmelez (2013-03-14 22:46:25+00:00)
Title: make adb/debugger modules usable in Simulator; add support for Windows

This changeset makes it possible to use the adb/debugger modules in the Simulator, whose modules follow the CommonJS standard (as implemented by the Add-on SDK). It also adds support for Windows and makes a few other minor changes, as documented below.

I added compatibility with the Simulator by setting a *COMMONJS* const boolean when loading the modules and then branching based on the value of that const. I'm not a huge fan of this method, but I can't think of a better one if we want to use these modules in both places. And despite the volume of changes in this changeset, there aren't actually that many differences between the two module specs, so the branches are fairly trivial and obvious.

([promise.js](http://mxr.mozilla.org/mozilla-central/source/addon-sdk/source/lib/sdk/core/promise.js) does it differently, and I suppose we could look into its approach, although it seems to have no external dependencies, whereas these modules depend on a variety of XPCOM interfaces and external binaries, and that's what causes most of the branching.)

One change whose reasoning is perhaps not obvious is that I converted a few properties into module global variables, specifically *ADB.ready* in adb.jsm and *Debugger._client* and *Debugger._webappsActor* in debugger.jsm. In CommonJS, when a module exports a single symbol like *ADB* that is named after the module, it is a best practice to make that symbol be the *exports* object, such that `require("adb")` will return *ADB*. But CommonJS freezes a module's *exports* object after evaluating the script, so its properties can't be reassigned, and values that will change must be stored elsewhere, like in a module global (which one can then make accessible via getters/setters, as is the case with *ADB.ready*).

A few other changes:

1. Added `use strict`, which helped me find an instance of the variable *dev* being used without being declared.
2. Replaced octal literals, which are forbidden in strict mode (and deprecated otherwise) with `parseInt(…, 8)` calls.
3. Replaced a *Components.Constructor* call that throws *NS_ERROR_XPC_UNEXPECTED* in a CommonJS context with its (IMHO simpler) *createInstance* equivalent.
4. Added logging of *socket.onerror*.
5. Added README/LICENSE files to the binaries/ directory as recommended by Gerv of Mozilla Licensing.