Subj : Re: SpiderMonkey: JSScope and JSObectMap To : Sajid Raza From : Brendan Eich Date : Wed Mar 17 2004 03:32 pm Sajid Raza wrote: > The OBJ_SCOPE(obj) macro expands to (JSScope*) obj->map. I see that a > JSObjectMap is the first element of a JSScope. What I'm trying to ask is > what about the other elemnts of a JSScope (lastprop etc.)? I guess they > might not be used in certain situations. I'd appreciate it if I'm > directed to any place in the source that differentiates JSScope's that > are really JSObjectMap's and JSScopes that have been constructed > separately. I guess what I'm looking for is any documentation or > information on scope resolution with respect to the internals of the > interpreter. Any direction appreciated. First, why do you need to know? If you're using the JS API and implementing JSClass, you shouldn't. And you really, *really* should be sticking to the frozen API and using JSClass. If you are implementing JSObjectOps, you're using not-quite-frozen parts of the API, and in fact you'll need to include the unfrozen, private jsobj.h. In that case, you may want to implement your own subtype of JSObjectMap, instead of using JSScope. Here, you're not guaranteed of binary or source compatibility; we may and probably will break you at some point. Anyway, on to the type relations: all JSScopes are JSObjectMaps, but not every JSObjectMap is a JSScope. OBJ_IS_NATIVE (MAP_IS_NATIVE if you're dealing with a JSObjectMap * instead of JSObject *) evaluates to true if the object (map) is native, which means it (a) either has a map->ops that points to &js_ObjectOps, or a map->ops->newObjectMap that points to js_ObjectOps.newObjectMap; and (b) all obj->map members for such objects point to JSScopes. If (!OBJ_IS_NATIVE(obj)), then you can't safely do (JSScope *)obj->map (a.k.a. OBJ_SCOPE(obj)). The core engine code takes care not to do so, and in general, only the js_ObjectOps initializers (the native object operations) assume that OBJ_IS_NATIVE(obj) and deal in JSScopes, but there are some abstraction breaks where parts of the code above the JSObjectOps "vtable" also know about scopes. Those places must guard such scope usage with OBJ_IS_NATIVE or MAP_IS_NATIVE tests. I hope this helps, but it would be better if it didn't ;-). See first paragraph. /be .