638
edits
Line 144: | Line 144: | ||
=== Compartments and wrappers - implementation details === | === Compartments and wrappers - implementation details === | ||
Each runtime has a ''default compartment'' which contains interned strings | Each runtime has a ''default compartment'' which contains interned strings, the empty string, +Inf, -Inf, NaN—and, in non-compartment-aware embeddings, everything else. | ||
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>. | 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>. | ||
Line 151: | Line 151: | ||
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 we can fall off trace when it happens. | 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 we can fall off trace when it happens. | ||
It might be better to require the application to change the current compartment explicitly: | |||
JSCompartment * | |||
JS_NewCompartment(rt, principals); | |||
JSCompartment * | |||
JS_GetCurrentCompartment(cx); | |||
void | |||
JS_SetCurrentCompartment(cx, compartment); | |||
The only code that would need to do so is (a) code setting up new compartments; (b) wrappers. All other APIs could just assert that cx->currentCompartment() agrees with all the arguments that happen to be gc-things. | |||
The precise agreement rule is: each gc-thing must either be in <code>cx->currentCompartment()</code> or be a string or double in <code>cx->runtime->defaultCompartment</code> (such strings and doubles may be referenced from anywhere). |
edits