0

Experimenting with a bootstrapped extension, I'm trying to understand the scopes and/or persistence of jsm modules by setting a property, called baseUri, on a module object from bootstrap.js and reading it again from javascript in my options.xul (which is opened from the Add-ons Manager).

My current understanding is that JavaScript Code Modules are persisted, once loaded. However, when I try to access baseUri from options.xul, its value is undefined.

install.rdf:

<!-- just the relevant XML (this works as expected, by the way): -->
<em:optionsURL>chrome://test/content/options.xul</em:optionsURL>

/modules/Test.jsm:

var EXPORTED_SYMBOLS = [ 'Test' ];

Test = {
  baseUri: undefined
}

/bootstrap.js:

// this is done in global scope,
// not inside install() or startup() for instance, if that matters
let test = Components.utils.import( 'file:///absolute/path/to/Test.jsm', {} ).Test;
test.baseUri = someBaseUriIExtracted;

/chrome/content/options.js (included in /chrome/content/options.xul):

let test = Components.utils.import( 'file:///absolute/path/to/Test.jsm', {} ).Test;
console.log( test.baseUri ); // undefined

So, I guess what I'm failing to fully understand is what the exact scopes are from which I should be able to access object properties from exported jsm symbols and/or how and when exactly these objects are persisted.

Does my problem have anything to do with sand-boxing, perhaps? Does Firefox consider options.xul, when opened from the Add-ons Manager, to be a different security scope than bootstrap.js, perhaps?

Can you shed a thorough light on the actual scopes of jsm modules and when and where I should be able to access persisted properties on jsm modules?

Codifier
  • 354
  • 1
  • 14

2 Answers2

2

The documentation is pretty straightforward about what and how is shared

Each scope that imports a module receives a by-value copy of the exported symbols in that module. Changes to the symbol's value will not propagate to other scopes (though an object's properties will be manipulated by reference).

I think the accompanying examples are clear.

Maybe you should use getters/setters.

paa
  • 5,048
  • 1
  • 18
  • 22
  • The documentation is pretty straightforward indeed and in my real scenario I'm already using getters and setters, but I just can't seem to get it to work the way I intend. I must be doing something wrong. I'll do some more testing first, before reporting back. Thank you, so far. – Codifier Sep 01 '14 at 08:59
  • OK, I've made an extremely simple test case now (my actual case was more complex than the example in my question) and it is working as expected now. There's something else going on that I need to figure out (probably something to do with the complex way I am instantiating objects in my module). Thank you. – Codifier Sep 01 '14 at 09:32
1

From what I know:

  • Other jsm modules
  • Browser window
  • Content window
  • bootstrap addon scope
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • So, excuse my ignorance, but does that then *include* or *exclude* `options.xul` when accessed from the Add-ons Manager? (Sorry, I'm not entirely comfortable yet with the nomenclature you are using.) – Codifier Aug 31 '14 at 09:13
  • To put it differently: do you think my example *should* be able to work as I intended (but that I'm possibly overlooking something), or is it expected behaviour that it is not working as I intended? – Codifier Aug 31 '14 at 09:15
  • Oh options.xul is its owo browser window context :) No need to be sorry :) So anything outside of that window cant access it. so import to each options.js and bootstrap.js – Noitidart Aug 31 '14 at 09:21
  • 1
    oh i see wat u r doing. making a change in the imported module and looking for it in the options.js, hm weird im not sure if changes go through like that, you might have to re-import after change, im not sure. – Noitidart Aug 31 '14 at 09:25
  • Yes, that's what I'm going for, indeed. Just to be absolutely clear though (I had a little trouble parsing your previous comment): you think my example should work, in principle, right? – Codifier Sep 01 '14 at 09:04
  • Thank you for your help Noitidart (and never mind my previous comment). I've not completely figured out what I'm doing wrong in my actual case yet, but see [my comment to paa](https://stackoverflow.com/questions/25590182/what-are-the-scopes-and-or-persistence-of-javascript-code-modules#comment39993714_25594662) on the current status. It works as expected in a simpler case. – Codifier Sep 01 '14 at 09:35
  • Thanks for that info, I had family over this weekend so was away from the board. – Noitidart Sep 01 '14 at 13:32