JavaScript:SpiderMonkey:GC Futures: Difference between revisions

Line 142: Line 142:
-->
-->


=== Compartments and wrappers - internal details ===
=== Compartments and wrappers - implementation details ===


A context is always "running in" a given compartment, normally the compartment of JS_GetScopeChain(cx). So, for example, <code>js_Atomize(cx, name, strlen(name), 0)</code> allocates the new string in whatever compartment cx is in.
Each runtime has a ''default compartment'' which contains interned strings (and, in non-compartment-aware embeddings, everything else).


A consequence of this is that the API function <code>JS_SetProperty(cx, obj, name, vp)</code> must put cx into obj's compartment before calling <code>js_Atomize</code>.
Each context has a ''current compartment'', initially the default compartment, and normally the compartment of JS_GetScopeChain(cx). So, for example, <code>js_Atomize(cx, name, strlen(name), 0)</code> allocates the new string from <code>cx->currentCompartment()</code>.
 
A tricky consequence of this is that the API function <code>JS_SetProperty(cx, obj, name, vp)</code> must put cx into obj's compartment before calling <code>js_Atomize</code>. Otherwise obj might end up with property ids in other compartments, a bad scene.
 
When does a context's current compartment change? At API entry points and when a function is called across compartment boundaries (which always happens via a wrapper). So it will be rare and will not ordinarily happen on trace.
638

edits