I’ve been at Mozilla for under a year, but I’ve been writing software for a long time, so I am mentally in this weird middle ground where I don’t know a lot about many, many areas of Firefox but feel like I should be able to figure it out.
Last week, I was trying to figure out how to make some C++ code call some already-written Javascript code. I followed my usual first two steps when I’m trying to figure something out and have zero idea where to start:
- Check firefox-source-docs, which is usually pretty complete, but this time it didn’t seem to have anything. (and my searches were of pretty generic terms, which didn’t help)
- Search on searchfox and try to guess the names of relevant classes and methods. This is more of an art than a science, but I’ve gotten surprisingly far this way! In this case I found
jsapi.h
, which seemed to have a bunch of C++ functions to manipulate Javascript classes and such. Score!
Except, after a day and a half of trying to use the JSAPI functions, I just could not figure out how to create an instance of a class. At some point I settled on just trying to call something builtin like window.alert()
, hoping I could use JS_ResolveStandardClass()
, but couldn’t make that work either. What’s more, I couldn’t see anywhere outside of Javascript engine code that was using the functions I was interested in, which given the size of Firefox’s codebase was a pretty good indication that I was not barking up the right tree.
I asked my team if anyone knew how to do this and got a helpful response that turned out to not work in my case.
I tried wracking my brain for other places in Firefox that might want to do something like this in the hopes that I could find some code that did this. This is where my inexperience in Firefox really hurts, because I don’t have a good idea of how a lot of Firefox works. Anyway, I didn’t find anything.
So I went to my last resort – picking a relevant file, looking at its history, finding someone who seems to have made meaningful changes to the file, and asking them 🙂 I explained what I was trying to do, and asked three questions:
- Is what I’m trying to do a bad idea?
- Is there an example of code that does this somewhere that I can follow?
- Is there a better person I should be asking? 🙂
The two sentence reply I got said that no, this was not a good idea, and that I should be defining an XPIDL interface, implementing it in Javascript, and calling that from C++.
And that was all I needed! I’ve implemented XPIDL interfaces in C++ before and didn’t even know you could do that in Javascript. Sadly the tutorial for implementing an XPIDL interface does it in C++ and leaves doing it in Javascript as an exercise for the reader, armed with the knowledge that this was possible it wasn’t hard to find a real example. And it totally worked! And it’s an idiomatic thing to do in Firefox.
Anyway, the lesson to this overly long and detailed story is that two sentences from an expert that they probably knew off the top of their head would have saved me a day and a half of floundering around. One of the things that has bothered me about some managers in the past is the idea of treating developers as “resources” that you can shift around to work on whatever project is the highest priority this month. And sure, people can and do learn, but it’s just so inefficient for people to constantly be starting over in their knowledge about how a product works.