I am having an issue whereby I create a RegExp object in a content script and pass it as part of an object back to the main script using self.port.emit().
Somewhere along the way it seems to lose its identity as a RegExp and also its toString abilities. The following returns false in the main script, but true in the content script:
Object.prototype.toString.call(regexp) == '[object RegExp]';
regexp instanceof RegExp;
Interestingly for Arrays passed in the same way the following is true:
Object.prototype.toString.call(array) == '[object Array]';
Am I missing something?
The Add-on SDK doesn’t pass objects around when you send messages, only strings – it essentially calls JSON.stringify() on one side and then JSON.parse() on the other. The result is easy to predict:
This gives you
"{}". In other words,JSON.stringify()treats “custom” objects as normal objects without any properties, object prototypes and such are ignored. What you get in your main code is a plain object, same as if you callnew Object().If you need to pass a regular expression to your main code – send
regexp.source, create an actual regular expression on the other side. Sending actual objects around isn’t possible.