With XPIDL, XPCOM components can be very strict about what they allow.
Simply put, if you try to pass in an argument to a XPCOM component, and the
component requires a type that your argument doesn’t support, it won’t work.
In C++, your program won’t compile; in JavaScript, XPConnect throws
exceptions. This is a good thing.
Many of the basic data structures – nsIArray, nsIPropertyBag, etc. – take
a nsISupports
argument. This covers most objects you construct
and work with. This is all well and good… unless you want to pass in a raw
type such as a number, a true
or false
value, a
string, etc. Then you’re stuck.
Fortunately, all is not lost. There is an interface named
nsIWritableVariant
, which XPCOM provides for wrapping native
types in an nsISupports
object. (There are type-specific
interfaces like nsISupportsPRBool
as well, but
nsIWritableVariant
is an one-size-fits-all solution.) Even more
interesting, its read-only companion, nsIVariant
, is “magical”
to XPConnect: JavaScript receives its values as native types, not as
nsIVariant
objects.
Thus, nsIVariant
turns the strengths of XPIDL barriers so
that they are no longer fighting you, but working with you. This is what I am
calling judo with nsIVariant
. Read on in the extended entry for more details.
Continue reading Back to Basics: Judo with nsIVariant →