2010-01-26 22:50:17 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2009-10-26 13:39:39 -07:00
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef jsscopeinlines_h___
|
|
|
|
#define jsscopeinlines_h___
|
|
|
|
|
|
|
|
#include "jscntxt.h"
|
2009-12-01 12:49:15 -08:00
|
|
|
#include "jsdbgapi.h"
|
2009-10-26 13:39:39 -07:00
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsscope.h"
|
|
|
|
|
2010-06-16 14:13:28 -07:00
|
|
|
#include "jscntxtinlines.h"
|
|
|
|
|
2010-01-26 22:50:17 -08:00
|
|
|
inline JSEmptyScope *
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScope::createEmptyScope(JSContext *cx, js::Class *clasp)
|
2010-01-26 22:50:17 -08:00
|
|
|
{
|
2010-07-20 14:57:12 -07:00
|
|
|
JS_ASSERT(!isSharedEmpty());
|
2010-01-26 22:50:17 -08:00
|
|
|
JS_ASSERT(!emptyScope);
|
2010-07-28 05:36:06 -07:00
|
|
|
emptyScope = cx->create<JSEmptyScope>(cx, ops, clasp);
|
2010-01-26 22:50:17 -08:00
|
|
|
return emptyScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSEmptyScope *
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScope::getEmptyScope(JSContext *cx, js::Class *clasp)
|
2010-01-26 22:50:17 -08:00
|
|
|
{
|
|
|
|
if (emptyScope) {
|
|
|
|
JS_ASSERT(clasp == emptyScope->clasp);
|
2010-06-01 15:01:11 -07:00
|
|
|
return emptyScope->hold();
|
2010-01-26 22:50:17 -08:00
|
|
|
}
|
|
|
|
return createEmptyScope(cx, clasp);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScope::ensureEmptyScope(JSContext *cx, js::Class *clasp)
|
2010-01-26 22:50:17 -08:00
|
|
|
{
|
|
|
|
if (emptyScope) {
|
|
|
|
JS_ASSERT(clasp == emptyScope->clasp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!createEmptyScope(cx, clasp))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* We are going to have only single ref to the scope. */
|
|
|
|
JS_ASSERT(emptyScope->nrefs == 2);
|
|
|
|
emptyScope->nrefs = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-20 16:14:42 -08:00
|
|
|
inline void
|
2009-12-01 12:49:15 -08:00
|
|
|
JSScope::updateShape(JSContext *cx)
|
2009-11-20 16:14:42 -08:00
|
|
|
{
|
2009-12-01 12:49:15 -08:00
|
|
|
JS_ASSERT(object);
|
2010-01-22 14:49:18 -08:00
|
|
|
js::LeaveTraceIfGlobalObject(cx, object);
|
2009-12-01 12:49:15 -08:00
|
|
|
shape = (hasOwnShape() || !lastProp) ? js_GenerateShape(cx, false) : lastProp->shape;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2010-05-12 19:34:01 -07:00
|
|
|
JSScope::updateFlags(const JSScopeProperty *sprop, bool isDefinitelyAtom)
|
2009-12-01 12:49:15 -08:00
|
|
|
{
|
2009-10-26 13:39:39 -07:00
|
|
|
jsuint index;
|
2010-05-12 19:34:01 -07:00
|
|
|
if (!isDefinitelyAtom && js_IdIsIndex(sprop->id, &index))
|
2009-10-26 13:39:39 -07:00
|
|
|
setIndexedProperties();
|
|
|
|
|
|
|
|
if (sprop->isMethod())
|
|
|
|
setMethodBarrier();
|
|
|
|
}
|
|
|
|
|
2010-01-07 10:01:01 -08:00
|
|
|
inline void
|
2010-05-12 19:34:01 -07:00
|
|
|
JSScope::extend(JSContext *cx, JSScopeProperty *sprop, bool isDefinitelyAtom)
|
2010-01-07 10:01:01 -08:00
|
|
|
{
|
|
|
|
++entryCount;
|
|
|
|
setLastProperty(sprop);
|
|
|
|
updateShape(cx);
|
2010-05-12 19:34:01 -07:00
|
|
|
updateFlags(sprop, isDefinitelyAtom);
|
2010-01-07 10:01:01 -08:00
|
|
|
}
|
|
|
|
|
2009-10-26 13:39:39 -07:00
|
|
|
/*
|
|
|
|
* Property read barrier for deferred cloning of compiler-created function
|
|
|
|
* objects optimized as typically non-escaping, ad-hoc methods in obj.
|
|
|
|
*/
|
|
|
|
inline bool
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScope::methodReadBarrier(JSContext *cx, JSScopeProperty *sprop, js::Value *vp)
|
2009-10-26 13:39:39 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(hasMethodBarrier());
|
2009-12-01 12:49:15 -08:00
|
|
|
JS_ASSERT(hasProperty(sprop));
|
2009-10-26 13:39:39 -07:00
|
|
|
JS_ASSERT(sprop->isMethod());
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(&vp->toObject() == &sprop->methodObject());
|
2010-07-23 14:41:56 -07:00
|
|
|
JS_ASSERT(object->canHaveMethodBarrier());
|
2009-10-26 13:39:39 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *funobj = &vp->toObject();
|
2009-10-26 13:39:39 -07:00
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
|
2010-07-23 14:41:56 -07:00
|
|
|
JS_ASSERT(fun == funobj && FUN_NULL_CLOSURE(fun));
|
2009-10-26 13:39:39 -07:00
|
|
|
|
2010-03-04 23:52:52 -08:00
|
|
|
funobj = CloneFunctionObject(cx, fun, funobj->getParent());
|
2009-10-26 13:39:39 -07:00
|
|
|
if (!funobj)
|
|
|
|
return false;
|
2010-07-23 14:41:56 -07:00
|
|
|
funobj->setMethodObj(*object);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*funobj);
|
2010-07-23 14:41:56 -07:00
|
|
|
if (!js_SetPropertyHelper(cx, object, sprop->id, 0, vp))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (cx->runtime->functionMeterFilename) {
|
|
|
|
JS_FUNCTION_METER(cx, mreadbarrier);
|
|
|
|
|
|
|
|
typedef JSRuntime::FunctionCountMap HM;
|
|
|
|
HM &h = cx->runtime->methodReadBarrierCountMap;
|
|
|
|
HM::AddPtr p = h.lookupForAdd(fun);
|
|
|
|
if (!p) {
|
|
|
|
h.add(p, fun, 1);
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(p->key == fun);
|
|
|
|
++p->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return true;
|
2009-10-26 13:39:39 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
static JS_ALWAYS_INLINE bool
|
|
|
|
ChangesMethodValue(const js::Value &prev, const js::Value &v)
|
|
|
|
{
|
|
|
|
JSObject *prevObj;
|
|
|
|
return prev.isObject() && (prevObj = &prev.toObject())->isFunction() &&
|
|
|
|
(!v.isObject() || &v.toObject() != prevObj);
|
|
|
|
}
|
|
|
|
|
2009-10-26 13:39:39 -07:00
|
|
|
inline bool
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScope::methodWriteBarrier(JSContext *cx, JSScopeProperty *sprop,
|
|
|
|
const js::Value &v)
|
2009-10-26 13:39:39 -07:00
|
|
|
{
|
2009-11-18 13:41:40 -08:00
|
|
|
if (flags & (BRANDED | METHOD_BARRIER)) {
|
2010-07-14 23:19:36 -07:00
|
|
|
const js::Value &prev = object->lockedGetSlot(sprop->slot);
|
2010-07-23 14:41:56 -07:00
|
|
|
if (ChangesMethodValue(prev, v)) {
|
|
|
|
JS_FUNCTION_METER(cx, mwritebarrier);
|
2010-04-30 06:29:04 -07:00
|
|
|
return methodShapeChange(cx, sprop);
|
2010-07-23 14:41:56 -07:00
|
|
|
}
|
2009-10-26 13:39:39 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScope::methodWriteBarrier(JSContext *cx, uint32 slot, const js::Value &v)
|
2009-10-26 13:39:39 -07:00
|
|
|
{
|
2009-11-18 13:41:40 -08:00
|
|
|
if (flags & (BRANDED | METHOD_BARRIER)) {
|
2010-07-14 23:19:36 -07:00
|
|
|
const js::Value &prev = object->lockedGetSlot(slot);
|
2010-07-23 14:41:56 -07:00
|
|
|
if (ChangesMethodValue(prev, v)) {
|
|
|
|
JS_FUNCTION_METER(cx, mwslotbarrier);
|
2010-04-30 06:29:04 -07:00
|
|
|
return methodShapeChange(cx, slot);
|
2010-07-23 14:41:56 -07:00
|
|
|
}
|
2009-10-26 13:39:39 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
JSScope::trace(JSTracer *trc)
|
|
|
|
{
|
|
|
|
JSContext *cx = trc->context;
|
|
|
|
JSScopeProperty *sprop = lastProp;
|
|
|
|
uint8 regenFlag = cx->runtime->gcRegenShapesScopeFlag;
|
2010-03-22 11:11:44 -07:00
|
|
|
|
2009-10-27 14:00:26 -07:00
|
|
|
if (IS_GC_MARKING_TRACER(trc) && cx->runtime->gcRegenShapes && !hasRegenFlag(regenFlag)) {
|
2009-10-26 13:39:39 -07:00
|
|
|
/*
|
|
|
|
* Either this scope has its own shape, which must be regenerated, or
|
|
|
|
* it must have the same shape as lastProp.
|
|
|
|
*/
|
|
|
|
uint32 newShape;
|
|
|
|
|
|
|
|
if (sprop) {
|
2010-02-05 16:11:13 -08:00
|
|
|
if (!sprop->hasRegenFlag()) {
|
2009-10-26 13:39:39 -07:00
|
|
|
sprop->shape = js_RegenerateShapeForGC(cx);
|
2010-02-05 16:11:13 -08:00
|
|
|
sprop->setRegenFlag();
|
2009-10-26 13:39:39 -07:00
|
|
|
}
|
|
|
|
newShape = sprop->shape;
|
|
|
|
}
|
|
|
|
if (!sprop || hasOwnShape()) {
|
|
|
|
newShape = js_RegenerateShapeForGC(cx);
|
|
|
|
JS_ASSERT_IF(sprop, newShape != sprop->shape);
|
|
|
|
}
|
|
|
|
shape = newShape;
|
|
|
|
flags ^= JSScope::SHAPE_REGEN;
|
|
|
|
|
2010-03-22 11:11:44 -07:00
|
|
|
/* Also regenerate the shapes of this scope's empty scope, if there is one. */
|
|
|
|
JSScope *empty = emptyScope;
|
|
|
|
if (empty) {
|
|
|
|
JS_ASSERT(!empty->emptyScope);
|
|
|
|
if (!empty->hasRegenFlag(regenFlag)) {
|
|
|
|
uint32 newEmptyShape = js_RegenerateShapeForGC(cx);
|
|
|
|
|
|
|
|
JS_PROPERTY_TREE(cx).emptyShapeChange(empty->shape, newEmptyShape);
|
|
|
|
empty->shape = newEmptyShape;
|
|
|
|
empty->flags ^= JSScope::SHAPE_REGEN;
|
|
|
|
}
|
2009-10-26 13:39:39 -07:00
|
|
|
}
|
|
|
|
}
|
2010-03-22 11:11:44 -07:00
|
|
|
|
2009-10-26 13:39:39 -07:00
|
|
|
if (sprop) {
|
2009-12-01 12:49:15 -08:00
|
|
|
JS_ASSERT(hasProperty(sprop));
|
2009-10-26 13:39:39 -07:00
|
|
|
|
|
|
|
/* Trace scope's property tree ancestor line. */
|
|
|
|
do {
|
|
|
|
sprop->trace(trc);
|
|
|
|
} while ((sprop = sprop->parent) != NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
inline
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScopeProperty::JSScopeProperty(jsid id, js::PropertyOp getter, js::PropertyOp setter,
|
2010-05-18 19:21:43 -07:00
|
|
|
uint32 slot, uintN attrs, uintN flags, intN shortid)
|
|
|
|
: id(id), rawGetter(getter), rawSetter(setter), slot(slot), attrs(uint8(attrs)),
|
|
|
|
flags(uint8(flags)), shortid(int16(shortid))
|
|
|
|
{
|
|
|
|
JS_ASSERT_IF(getter && (attrs & JSPROP_GETTER), getterObj->isCallable());
|
|
|
|
JS_ASSERT_IF(setter && (attrs & JSPROP_SETTER), setterObj->isCallable());
|
|
|
|
}
|
|
|
|
|
2010-02-05 16:11:13 -08:00
|
|
|
inline JSDHashNumber
|
|
|
|
JSScopeProperty::hash() const
|
|
|
|
{
|
|
|
|
JSDHashNumber hash = 0;
|
|
|
|
|
|
|
|
/* Accumulate from least to most random so the low bits are most random. */
|
2010-02-24 17:40:28 -08:00
|
|
|
JS_ASSERT_IF(isMethod(), !rawSetter || rawSetter == js_watch_set);
|
|
|
|
if (rawGetter)
|
|
|
|
hash = JS_ROTATE_LEFT32(hash, 4) ^ jsuword(rawGetter);
|
|
|
|
if (rawSetter)
|
|
|
|
hash = JS_ROTATE_LEFT32(hash, 4) ^ jsuword(rawSetter);
|
2010-02-05 16:11:13 -08:00
|
|
|
hash = JS_ROTATE_LEFT32(hash, 4) ^ (flags & PUBLIC_FLAGS);
|
|
|
|
hash = JS_ROTATE_LEFT32(hash, 4) ^ attrs;
|
|
|
|
hash = JS_ROTATE_LEFT32(hash, 4) ^ shortid;
|
|
|
|
hash = JS_ROTATE_LEFT32(hash, 4) ^ slot;
|
2010-07-14 23:19:36 -07:00
|
|
|
hash = JS_ROTATE_LEFT32(hash, 4) ^ JSID_BITS(id);
|
2010-02-05 16:11:13 -08:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
JSScopeProperty::matches(const JSScopeProperty *p) const
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(!JSID_IS_VOID(id));
|
|
|
|
JS_ASSERT(!JSID_IS_VOID(p->id));
|
2010-02-05 16:11:13 -08:00
|
|
|
return id == p->id &&
|
2010-02-24 17:40:28 -08:00
|
|
|
matchesParamsAfterId(p->rawGetter, p->rawSetter, p->slot, p->attrs, p->flags,
|
|
|
|
p->shortid);
|
2010-02-05 16:11:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScopeProperty::matchesParamsAfterId(js::PropertyOp agetter, js::PropertyOp asetter, uint32 aslot,
|
2010-02-05 16:11:13 -08:00
|
|
|
uintN aattrs, uintN aflags, intN ashortid) const
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(!JSID_IS_VOID(id));
|
2010-02-24 17:40:28 -08:00
|
|
|
return rawGetter == agetter &&
|
|
|
|
rawSetter == asetter &&
|
2010-02-05 16:11:13 -08:00
|
|
|
slot == aslot &&
|
|
|
|
attrs == aattrs &&
|
|
|
|
((flags ^ aflags) & PUBLIC_FLAGS) == 0 &&
|
|
|
|
shortid == ashortid;
|
|
|
|
}
|
|
|
|
|
2010-06-16 14:13:01 -07:00
|
|
|
inline bool
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScopeProperty::get(JSContext* cx, JSObject* obj, JSObject *pobj, js::Value* vp)
|
2010-06-16 14:13:01 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(!JSID_IS_VOID(this->id));
|
2010-06-16 14:13:01 -07:00
|
|
|
JS_ASSERT(!hasDefaultGetter());
|
|
|
|
|
|
|
|
if (hasGetterValue()) {
|
|
|
|
JS_ASSERT(!isMethod());
|
2010-07-14 23:19:36 -07:00
|
|
|
js::Value fval = getterValue();
|
|
|
|
return js::InternalGetOrSet(cx, obj, id, fval, JSACC_READ, 0, 0, vp);
|
2010-06-16 14:13:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isMethod()) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(methodObject());
|
2010-06-16 14:13:01 -07:00
|
|
|
|
|
|
|
JSScope *scope = pobj->scope();
|
|
|
|
JS_ASSERT(scope->object == pobj);
|
|
|
|
return scope->methodReadBarrier(cx, this, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* |with (it) color;| ends up here, as do XML filter-expressions.
|
|
|
|
* Avoid exposing the With object to native getters.
|
|
|
|
*/
|
|
|
|
if (obj->getClass() == &js_WithClass)
|
|
|
|
obj = js_UnwrapWithObject(cx, obj);
|
2010-06-16 14:13:28 -07:00
|
|
|
return js::callJSPropertyOp(cx, getterOp(), obj, SPROP_USERID(this), vp);
|
2010-06-16 14:13:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
2010-07-14 23:19:36 -07:00
|
|
|
JSScopeProperty::set(JSContext* cx, JSObject* obj, js::Value* vp)
|
2010-06-16 14:13:01 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT_IF(hasDefaultSetter(), hasGetterValue());
|
|
|
|
|
|
|
|
if (attrs & JSPROP_SETTER) {
|
2010-07-14 23:19:36 -07:00
|
|
|
js::Value fval = setterValue();
|
|
|
|
return js::InternalGetOrSet(cx, obj, id, fval, JSACC_WRITE, 1, vp, vp);
|
2010-06-16 14:13:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (attrs & JSPROP_GETTER)
|
2010-07-14 23:19:36 -07:00
|
|
|
return js_ReportGetterOnlyAssignment(cx);
|
2010-06-16 14:13:01 -07:00
|
|
|
|
|
|
|
/* See the comment in JSScopeProperty::get as to why we check for With. */
|
|
|
|
if (obj->getClass() == &js_WithClass)
|
|
|
|
obj = js_UnwrapWithObject(cx, obj);
|
2010-06-16 14:13:28 -07:00
|
|
|
return js::callJSPropertyOpSetter(cx, setterOp(), obj, SPROP_USERID(this), vp);
|
2010-06-16 14:13:01 -07:00
|
|
|
}
|
|
|
|
|
2009-10-26 13:39:39 -07:00
|
|
|
#endif /* jsscopeinlines_h___ */
|