MathJax

SyntaxHighlighter

Highlight

Custom CSS

Wednesday, May 12, 2010

Storing Numbers in nsIPrefService

Tip of the day: don't use nsIPrefService to store JavaScript Number objects when writing a Firefox extension. Take a look at what happens in XPCShell:

js> var prefService = Components.classes["@mozilla.org/preferences-service;1"].
    getService(Components.interfaces.nsIPrefService);
js> var branch = prefService.getBranch("foo");
js> Date.now();
1273727558711
js> branch.setIntPref("date", Date.now());
js> branch.getIntPref("date");
-1877723439

The reason for this is that while JavaScript Number objects do not overflow, underlying ints in the C++ code that nsIPrefService is implemented in certainly will. I really should've been tipped off by the setIntPref method name =P
Post a Comment