2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2009-02-11 19:48:00 -08:00
|
|
|
* vim: set ts=8 sw=4 et tw=79:
|
2007-03-22 10:30:00 -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 ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS object implementation.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsarena.h" /* Added by JSIFY */
|
|
|
|
#include "jsbit.h"
|
|
|
|
#include "jsutil.h" /* Added by JSIFY */
|
|
|
|
#include "jshash.h" /* Added by JSIFY */
|
|
|
|
#include "jsdhash.h"
|
|
|
|
#include "jsprf.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jsbool.h"
|
2008-10-08 15:08:33 -07:00
|
|
|
#include "jsbuiltins.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-11-29 06:49:42 -08:00
|
|
|
#include "jsemit.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsgc.h"
|
|
|
|
#include "jsinterp.h"
|
2010-06-03 21:41:01 -07:00
|
|
|
#include "jsiter.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsopcode.h"
|
2007-11-29 06:49:42 -08:00
|
|
|
#include "jsparse.h"
|
2010-05-18 19:21:43 -07:00
|
|
|
#include "jsproxy.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsscope.h"
|
|
|
|
#include "jsscript.h"
|
2009-01-30 15:40:05 -08:00
|
|
|
#include "jsstaticcheck.h"
|
2009-09-22 16:46:13 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsstr.h"
|
2009-01-30 15:40:05 -08:00
|
|
|
#include "jstracer.h"
|
2009-02-18 16:31:09 -08:00
|
|
|
#include "jsdbgapi.h"
|
2010-08-24 08:50:54 -07:00
|
|
|
#include "json.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
#include "jsinterpinlines.h"
|
2009-10-26 13:39:39 -07:00
|
|
|
#include "jsscopeinlines.h"
|
|
|
|
#include "jsscriptinlines.h"
|
2010-04-10 16:16:35 -07:00
|
|
|
#include "jsobjinlines.h"
|
2009-10-26 13:39:39 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
#include "jsiter.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
#include "jsxml.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if JS_HAS_XDR
|
|
|
|
#include "jsxdrapi.h"
|
|
|
|
#endif
|
|
|
|
|
2010-09-01 14:09:54 -07:00
|
|
|
#include "jsprobes.h"
|
2009-07-13 14:55:04 -07:00
|
|
|
#include "jsatominlines.h"
|
2009-11-18 12:29:58 -08:00
|
|
|
#include "jsobjinlines.h"
|
2009-09-10 09:50:43 -07:00
|
|
|
#include "jsscriptinlines.h"
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2008-06-20 02:55:49 -07:00
|
|
|
#include "jsautooplen.h"
|
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
using namespace js;
|
|
|
|
|
2010-06-12 09:29:04 -07:00
|
|
|
JS_FRIEND_DATA(const JSObjectMap) JSObjectMap::sharedNonNative(JSObjectMap::SHAPELESS);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class js_ObjectClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_Object_str,
|
2010-08-16 12:35:04 -07:00
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
|
2010-06-12 09:29:04 -07:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
PropertyStub, /* setProperty */
|
|
|
|
EnumerateStub,
|
|
|
|
ResolveStub,
|
|
|
|
ConvertStub
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2010-06-12 09:29:04 -07:00
|
|
|
JS_FRIEND_API(JSObject *)
|
|
|
|
js_ObjectToOuterObject(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
OBJ_TO_OUTER_OBJECT(cx, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_OBJ_PROTO_PROP
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_getProto(JSContext *cx, JSObject *obj, jsid id, Value *vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_setProto(JSContext *cx, JSObject *obj, jsid id, Value *vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static JSPropertySpec object_props[] = {
|
2010-07-14 23:19:36 -07:00
|
|
|
{js_proto_str, 0, JSPROP_PERMANENT|JSPROP_SHARED, Jsvalify(obj_getProto), Jsvalify(obj_setProto)},
|
2007-03-22 10:30:00 -07:00
|
|
|
{0,0,0,0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_getProto(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-17 05:32:26 -07:00
|
|
|
/* Let CheckAccess get the slot's value, based on the access mode. */
|
2010-04-11 13:51:00 -07:00
|
|
|
uintN attrs;
|
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.protoAtom);
|
2010-08-13 06:21:13 -07:00
|
|
|
return CheckAccess(cx, obj, id, JSACC_PROTO, vp, &attrs);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_setProto(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!vp->isObjectOrNull())
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *pobj = vp->toObjectOrNull();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pobj) {
|
|
|
|
/*
|
|
|
|
* Innerize pobj here to avoid sticking unwanted properties on the
|
|
|
|
* outer object. This ensures that any with statements only grant
|
|
|
|
* access to the inner object.
|
|
|
|
*/
|
|
|
|
OBJ_TO_INNER_OBJECT(cx, pobj);
|
|
|
|
if (!pobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-04-11 13:51:00 -07:00
|
|
|
uintN attrs;
|
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.protoAtom);
|
2010-08-13 06:21:13 -07:00
|
|
|
if (!CheckAccess(cx, obj, id, JSAccessMode(JSACC_PROTO|JSACC_WRITE), vp, &attrs))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-06-17 15:23:17 -07:00
|
|
|
return SetProto(cx, obj, pobj, JS_TRUE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !JS_HAS_OBJ_PROTO_PROP */
|
|
|
|
|
|
|
|
#define object_props NULL
|
|
|
|
|
|
|
|
#endif /* !JS_HAS_OBJ_PROTO_PROP */
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static JSHashNumber
|
2007-03-22 10:30:00 -07:00
|
|
|
js_hash_object(const void *key)
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
return JSHashNumber(uintptr_t(key) >> JS_GCTHING_ALIGN);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSHashEntry *
|
|
|
|
MarkSharpObjects(JSContext *cx, JSObject *obj, JSIdArray **idap)
|
|
|
|
{
|
|
|
|
JSSharpObjectMap *map;
|
|
|
|
JSHashTable *table;
|
|
|
|
JSHashNumber hash;
|
|
|
|
JSHashEntry **hep, *he;
|
|
|
|
jsatomid sharpid;
|
|
|
|
JSIdArray *ida;
|
|
|
|
JSBool ok;
|
|
|
|
jsint i, length;
|
|
|
|
jsid id;
|
|
|
|
JSObject *obj2;
|
|
|
|
JSProperty *prop;
|
|
|
|
|
2008-01-31 22:01:17 -08:00
|
|
|
JS_CHECK_RECURSION(cx, return NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
map = &cx->sharpObjectMap;
|
2009-01-16 09:59:46 -08:00
|
|
|
JS_ASSERT(map->depth >= 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
table = map->table;
|
|
|
|
hash = js_hash_object(obj);
|
|
|
|
hep = JS_HashTableRawLookup(table, hash, obj);
|
|
|
|
he = *hep;
|
|
|
|
if (!he) {
|
|
|
|
sharpid = 0;
|
2010-03-16 08:41:28 -07:00
|
|
|
he = JS_HashTableRawAdd(table, hep, hash, obj, (void *) sharpid);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!he) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ida = JS_Enumerate(cx, obj);
|
|
|
|
if (!ida)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ok = JS_TRUE;
|
|
|
|
for (i = 0, length = ida->length; i < length; i++) {
|
|
|
|
id = ida->vector[i];
|
2009-08-11 13:05:44 -07:00
|
|
|
ok = obj->lookupProperty(cx, id, &obj2, &prop);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
break;
|
|
|
|
if (!prop)
|
|
|
|
continue;
|
2010-05-25 03:54:55 -07:00
|
|
|
bool hasGetter, hasSetter;
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoValueRooter v(cx);
|
|
|
|
AutoValueRooter setter(cx);
|
2010-05-28 01:00:07 -07:00
|
|
|
if (obj2->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape = (Shape *) prop;
|
|
|
|
hasGetter = shape->hasGetterValue();
|
|
|
|
hasSetter = shape->hasSetterValue();
|
2010-05-25 03:54:55 -07:00
|
|
|
if (hasGetter)
|
2010-08-29 11:57:08 -07:00
|
|
|
v.set(shape->getterValue());
|
2010-05-25 03:54:55 -07:00
|
|
|
if (hasSetter)
|
2010-08-29 11:57:08 -07:00
|
|
|
setter.set(shape->setterValue());
|
2010-05-25 03:54:55 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj2);
|
|
|
|
} else {
|
|
|
|
hasGetter = hasSetter = false;
|
|
|
|
}
|
|
|
|
if (hasSetter) {
|
|
|
|
/* Mark the getter, then set val to setter. */
|
2010-07-14 23:19:36 -07:00
|
|
|
if (hasGetter && v.value().isObject()) {
|
|
|
|
ok = !!MarkSharpObjects(cx, &v.value().toObject(), NULL);
|
2010-05-25 03:54:55 -07:00
|
|
|
if (!ok)
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-25 03:54:55 -07:00
|
|
|
v.set(setter.value());
|
|
|
|
} else if (!hasGetter) {
|
|
|
|
ok = obj->getProperty(cx, id, v.addr());
|
|
|
|
if (!ok)
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.value().isObject() &&
|
|
|
|
!MarkSharpObjects(cx, &v.value().toObject(), NULL)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
ok = JS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ok || !idap)
|
|
|
|
JS_DestroyIdArray(cx, ida);
|
|
|
|
if (!ok)
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2010-03-16 08:41:28 -07:00
|
|
|
sharpid = uintptr_t(he->value);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sharpid == 0) {
|
|
|
|
sharpid = ++map->sharpgen << SHARP_ID_SHIFT;
|
2010-03-16 08:41:28 -07:00
|
|
|
he->value = (void *) sharpid;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
ida = NULL;
|
|
|
|
}
|
|
|
|
if (idap)
|
|
|
|
*idap = ida;
|
|
|
|
return he;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHashEntry *
|
|
|
|
js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
|
|
|
|
jschar **sp)
|
|
|
|
{
|
|
|
|
JSSharpObjectMap *map;
|
|
|
|
JSHashTable *table;
|
|
|
|
JSIdArray *ida;
|
|
|
|
JSHashNumber hash;
|
|
|
|
JSHashEntry *he, **hep;
|
|
|
|
jsatomid sharpid;
|
|
|
|
char buf[20];
|
|
|
|
size_t len;
|
|
|
|
|
2009-02-10 14:07:01 -08:00
|
|
|
if (!JS_CHECK_OPERATION_LIMIT(cx))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Set to null in case we return an early error. */
|
|
|
|
*sp = NULL;
|
|
|
|
map = &cx->sharpObjectMap;
|
|
|
|
table = map->table;
|
|
|
|
if (!table) {
|
|
|
|
table = JS_NewHashTable(8, js_hash_object, JS_CompareValues,
|
|
|
|
JS_CompareValues, NULL, NULL);
|
|
|
|
if (!table) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
map->table = table;
|
|
|
|
JS_KEEP_ATOMS(cx->runtime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* From this point the control must flow either through out: or bad:. */
|
|
|
|
ida = NULL;
|
|
|
|
if (map->depth == 0) {
|
2009-01-16 09:59:46 -08:00
|
|
|
/*
|
|
|
|
* Although MarkSharpObjects tries to avoid invoking getters,
|
|
|
|
* it ends up doing so anyway under some circumstances; for
|
|
|
|
* example, if a wrapped object has getters, the wrapper will
|
|
|
|
* prevent MarkSharpObjects from recognizing them as such.
|
|
|
|
* This could lead to js_LeaveSharpObject being called while
|
|
|
|
* MarkSharpObjects is still working.
|
|
|
|
*
|
|
|
|
* Increment map->depth while we call MarkSharpObjects, to
|
|
|
|
* ensure that such a call doesn't free the hash table we're
|
|
|
|
* still using.
|
|
|
|
*/
|
|
|
|
++map->depth;
|
2007-03-22 10:30:00 -07:00
|
|
|
he = MarkSharpObjects(cx, obj, &ida);
|
2009-01-16 09:59:46 -08:00
|
|
|
--map->depth;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!he)
|
|
|
|
goto bad;
|
2010-03-16 08:41:28 -07:00
|
|
|
JS_ASSERT((uintptr_t(he->value) & SHARP_BIT) == 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!idap) {
|
|
|
|
JS_DestroyIdArray(cx, ida);
|
|
|
|
ida = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hash = js_hash_object(obj);
|
|
|
|
hep = JS_HashTableRawLookup(table, hash, obj);
|
|
|
|
he = *hep;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's possible that the value of a property has changed from the
|
|
|
|
* first time the object's properties are traversed (when the property
|
|
|
|
* ids are entered into the hash table) to the second (when they are
|
2009-08-11 13:05:44 -07:00
|
|
|
* converted to strings), i.e., the JSObject::getProperty() call is not
|
2007-03-22 10:30:00 -07:00
|
|
|
* idempotent.
|
|
|
|
*/
|
|
|
|
if (!he) {
|
|
|
|
he = JS_HashTableRawAdd(table, hep, hash, obj, NULL);
|
|
|
|
if (!he) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
sharpid = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-16 08:41:28 -07:00
|
|
|
sharpid = uintptr_t(he->value);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (sharpid != 0) {
|
|
|
|
len = JS_snprintf(buf, sizeof buf, "#%u%c",
|
|
|
|
sharpid >> SHARP_ID_SHIFT,
|
|
|
|
(sharpid & SHARP_BIT) ? '#' : '=');
|
|
|
|
*sp = js_InflateString(cx, buf, &len);
|
|
|
|
if (!*sp) {
|
|
|
|
if (ida)
|
|
|
|
JS_DestroyIdArray(cx, ida);
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
JS_ASSERT(he);
|
|
|
|
if ((sharpid & SHARP_BIT) == 0) {
|
|
|
|
if (idap && !ida) {
|
|
|
|
ida = JS_Enumerate(cx, obj);
|
|
|
|
if (!ida) {
|
|
|
|
if (*sp) {
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(*sp);
|
2007-03-22 10:30:00 -07:00
|
|
|
*sp = NULL;
|
|
|
|
}
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
map->depth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idap)
|
|
|
|
*idap = ida;
|
|
|
|
return he;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
/* Clean up the sharpObjectMap table on outermost error. */
|
|
|
|
if (map->depth == 0) {
|
|
|
|
JS_UNKEEP_ATOMS(cx->runtime);
|
|
|
|
map->sharpgen = 0;
|
|
|
|
JS_HashTableDestroy(map->table);
|
|
|
|
map->table = NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_LeaveSharpObject(JSContext *cx, JSIdArray **idap)
|
|
|
|
{
|
|
|
|
JSSharpObjectMap *map;
|
|
|
|
JSIdArray *ida;
|
|
|
|
|
|
|
|
map = &cx->sharpObjectMap;
|
|
|
|
JS_ASSERT(map->depth > 0);
|
|
|
|
if (--map->depth == 0) {
|
|
|
|
JS_UNKEEP_ATOMS(cx->runtime);
|
|
|
|
map->sharpgen = 0;
|
|
|
|
JS_HashTableDestroy(map->table);
|
|
|
|
map->table = NULL;
|
|
|
|
}
|
|
|
|
if (idap) {
|
|
|
|
ida = *idap;
|
|
|
|
if (ida) {
|
|
|
|
JS_DestroyIdArray(cx, ida);
|
|
|
|
*idap = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static intN
|
2007-03-22 10:30:00 -07:00
|
|
|
gc_sharp_table_entry_marker(JSHashEntry *he, intN i, void *arg)
|
|
|
|
{
|
2007-04-16 23:53:37 -07:00
|
|
|
JS_CALL_OBJECT_TRACER((JSTracer *)arg, (JSObject *)he->key,
|
|
|
|
"sharp table entry");
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-04-16 23:53:37 -07:00
|
|
|
js_TraceSharpMap(JSTracer *trc, JSSharpObjectMap *map)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(map->depth > 0);
|
|
|
|
JS_ASSERT(map->table);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* During recursive calls to MarkSharpObjects a non-native object or
|
|
|
|
* object with a custom getProperty method can potentially return an
|
|
|
|
* unrooted value or even cut from the object graph an argument of one of
|
|
|
|
* MarkSharpObjects recursive invocations. So we must protect map->table
|
|
|
|
* entries against GC.
|
|
|
|
*
|
|
|
|
* We can not simply use JSTempValueRooter to mark the obj argument of
|
|
|
|
* MarkSharpObjects during recursion as we have to protect *all* entries
|
|
|
|
* in JSSharpObjectMap including those that contains otherwise unreachable
|
|
|
|
* objects just allocated through custom getProperty. Otherwise newer
|
|
|
|
* allocations can re-use the address of an object stored in the hashtable
|
|
|
|
* confusing js_EnterSharpObject. So to address the problem we simply
|
|
|
|
* mark all objects from map->table.
|
|
|
|
*
|
|
|
|
* An alternative "proper" solution is to use JSTempValueRooter in
|
|
|
|
* MarkSharpObjects with code to remove during finalization entries
|
|
|
|
* with otherwise unreachable objects. But this is way too complex
|
|
|
|
* to justify spending efforts.
|
|
|
|
*/
|
2007-04-16 23:53:37 -07:00
|
|
|
JS_HashTableEnumerateEntries(map->table, gc_sharp_table_entry_marker, trc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_TOSOURCE
|
2007-08-01 21:33:52 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_toSource(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSBool ok, outermost;
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSHashEntry *he;
|
|
|
|
JSIdArray *ida;
|
|
|
|
jschar *chars, *ochars, *vsharp;
|
|
|
|
const jschar *idstrchars, *vchars;
|
|
|
|
size_t nchars, idstrlength, gsoplength, vlength, vsharplength, curlen;
|
2008-07-20 05:53:21 -07:00
|
|
|
const char *comma;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *obj2;
|
|
|
|
JSProperty *prop;
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *val;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSString *gsop[2];
|
|
|
|
JSString *idstr, *valstr, *str;
|
|
|
|
|
2008-01-31 22:01:17 -08:00
|
|
|
JS_CHECK_RECURSION(cx, return JS_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value localroot[4];
|
|
|
|
PodArrayZero(localroot);
|
2010-03-28 20:32:20 -07:00
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(localroot), localroot);
|
2007-10-13 13:09:48 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* If outermost, we need parentheses to be an expression, not a block. */
|
|
|
|
outermost = (cx->sharpObjectMap.depth == 0);
|
2010-07-14 23:19:36 -07:00
|
|
|
obj = ComputeThisFromVp(cx, vp);
|
2008-02-17 16:12:33 -08:00
|
|
|
if (!obj || !(he = js_EnterSharpObject(cx, obj, &ida, &chars))) {
|
2007-10-13 13:09:48 -07:00
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (IS_SHARP(he)) {
|
|
|
|
/*
|
|
|
|
* We didn't enter -- obj is already "sharp", meaning we've visited it
|
|
|
|
* already in our depth first search, and therefore chars contains a
|
|
|
|
* string of the form "#n#".
|
|
|
|
*/
|
|
|
|
JS_ASSERT(!ida);
|
|
|
|
#if JS_HAS_SHARP_VARS
|
|
|
|
nchars = js_strlen(chars);
|
|
|
|
#else
|
|
|
|
chars[0] = '{';
|
|
|
|
chars[1] = '}';
|
|
|
|
chars[2] = 0;
|
|
|
|
nchars = 2;
|
|
|
|
#endif
|
|
|
|
goto make_string;
|
|
|
|
}
|
|
|
|
JS_ASSERT(ida);
|
|
|
|
ok = JS_TRUE;
|
|
|
|
|
|
|
|
if (!chars) {
|
|
|
|
/* If outermost, allocate 4 + 1 for "({})" and the terminator. */
|
2010-09-07 14:08:20 -07:00
|
|
|
chars = (jschar *) cx->runtime->malloc(((outermost ? 4 : 2) + 1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
nchars = 0;
|
|
|
|
if (!chars)
|
|
|
|
goto error;
|
|
|
|
if (outermost)
|
|
|
|
chars[nchars++] = '(';
|
|
|
|
} else {
|
|
|
|
/* js_EnterSharpObject returned a string of the form "#n=" in chars. */
|
|
|
|
MAKE_SHARP(he);
|
|
|
|
nchars = js_strlen(chars);
|
|
|
|
chars = (jschar *)
|
2009-07-27 21:10:12 -07:00
|
|
|
js_realloc((ochars = chars), (nchars + 2 + 1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!chars) {
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(ochars);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (outermost) {
|
|
|
|
/*
|
|
|
|
* No need for parentheses around the whole shebang, because #n=
|
|
|
|
* unambiguously begins an object initializer, and never a block
|
|
|
|
* statement.
|
|
|
|
*/
|
|
|
|
outermost = JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chars[nchars++] = '{';
|
|
|
|
|
|
|
|
comma = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have four local roots for cooked and raw value GC safety. Hoist the
|
2007-10-13 13:09:48 -07:00
|
|
|
* "localroot + 2" out of the loop using the val local, which refers to
|
|
|
|
* the raw (unconverted, "uncooked") values.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2007-10-13 13:09:48 -07:00
|
|
|
val = localroot + 2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
for (jsint i = 0, length = ida->length; i < length; i++) {
|
2007-08-01 21:33:52 -07:00
|
|
|
/* Get strings for id and value and GC-root them via vp. */
|
2009-10-06 12:35:33 -07:00
|
|
|
jsid id = ida->vector[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-11 13:05:44 -07:00
|
|
|
ok = obj->lookupProperty(cx, id, &obj2, &prop);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!ok)
|
|
|
|
goto error;
|
2007-05-01 11:07:44 -07:00
|
|
|
|
|
|
|
/*
|
2010-07-14 23:19:36 -07:00
|
|
|
* Convert id to a value and then to a string. Decide early whether we
|
2007-05-01 11:07:44 -07:00
|
|
|
* prefer get/set or old getter/setter syntax.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
idstr = js_ValueToString(cx, IdToValue(id));
|
2007-05-01 11:07:44 -07:00
|
|
|
if (!idstr) {
|
|
|
|
ok = JS_FALSE;
|
2009-08-11 13:05:44 -07:00
|
|
|
obj2->dropProperty(cx, prop);
|
2007-05-01 11:07:44 -07:00
|
|
|
goto error;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(idstr); /* local root */
|
2007-05-01 11:07:44 -07:00
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
jsint valcnt = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (prop) {
|
2010-05-26 03:56:57 -07:00
|
|
|
bool doGet = true;
|
2010-05-28 01:00:07 -07:00
|
|
|
if (obj2->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape = (Shape *) prop;
|
|
|
|
unsigned attrs = shape->attributes();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (attrs & JSPROP_GETTER) {
|
2010-05-26 03:56:57 -07:00
|
|
|
doGet = false;
|
2010-08-29 11:57:08 -07:00
|
|
|
val[valcnt] = shape->getterValue();
|
2009-10-06 12:35:33 -07:00
|
|
|
gsop[valcnt] = ATOM_TO_STRING(cx->runtime->atomState.getAtom);
|
2007-05-21 11:02:33 -07:00
|
|
|
valcnt++;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (attrs & JSPROP_SETTER) {
|
2010-05-26 03:56:57 -07:00
|
|
|
doGet = false;
|
2010-08-29 11:57:08 -07:00
|
|
|
val[valcnt] = shape->setterValue();
|
2009-10-06 12:35:33 -07:00
|
|
|
gsop[valcnt] = ATOM_TO_STRING(cx->runtime->atomState.setAtom);
|
2007-05-21 11:02:33 -07:00
|
|
|
valcnt++;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-28 01:00:07 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj2);
|
2010-05-26 03:56:57 -07:00
|
|
|
}
|
|
|
|
if (doGet) {
|
2007-03-22 10:30:00 -07:00
|
|
|
valcnt = 1;
|
|
|
|
gsop[0] = NULL;
|
2009-08-11 13:05:44 -07:00
|
|
|
ok = obj->getProperty(cx, id, &val[0]);
|
2010-05-26 03:56:57 -07:00
|
|
|
if (!ok)
|
|
|
|
goto error;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-10-06 12:35:33 -07:00
|
|
|
* If id is a string that's not an identifier, or if it's a negative
|
|
|
|
* integer, then it must be quoted.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2009-10-06 12:35:33 -07:00
|
|
|
bool idIsLexicalIdentifier = !!js_IsIdentifier(idstr);
|
2007-08-11 13:25:16 -07:00
|
|
|
if (JSID_IS_ATOM(id)
|
2007-05-01 11:07:44 -07:00
|
|
|
? !idIsLexicalIdentifier
|
2007-08-11 13:25:16 -07:00
|
|
|
: (!JSID_IS_INT(id) || JSID_TO_INT(id) < 0)) {
|
2009-10-06 12:35:33 -07:00
|
|
|
idstr = js_QuoteString(cx, idstr, jschar('\''));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!idstr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(idstr); /* local root */
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-06-10 18:29:44 -07:00
|
|
|
idstr->getCharsAndLength(idstrchars, idstrlength);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
for (jsint j = 0; j < valcnt; j++) {
|
|
|
|
/*
|
|
|
|
* Censor an accessor descriptor getter or setter part if it's
|
|
|
|
* undefined.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
if (gsop[j] && val[j].isUndefined())
|
2009-10-06 12:35:33 -07:00
|
|
|
continue;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Convert val[j] to its canonical source form. */
|
|
|
|
valstr = js_ValueToSource(cx, val[j]);
|
|
|
|
if (!valstr) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
localroot[j].setString(valstr); /* local root */
|
2009-06-10 18:29:44 -07:00
|
|
|
valstr->getCharsAndLength(vchars, vlength);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
/*
|
|
|
|
* If val[j] is a non-sharp object, and we're not serializing an
|
|
|
|
* accessor (ECMA syntax can't accommodate sharpened accessors),
|
|
|
|
* consider sharpening it.
|
|
|
|
*/
|
2007-06-05 13:20:07 -07:00
|
|
|
vsharp = NULL;
|
|
|
|
vsharplength = 0;
|
|
|
|
#if JS_HAS_SHARP_VARS
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!gsop[j] && val[j].isObject() && vchars[0] != '#') {
|
|
|
|
he = js_EnterSharpObject(cx, &val[j].toObject(), NULL, &vsharp);
|
2007-06-05 13:20:07 -07:00
|
|
|
if (!he) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (IS_SHARP(he)) {
|
|
|
|
vchars = vsharp;
|
|
|
|
vlength = js_strlen(vchars);
|
|
|
|
} else {
|
|
|
|
if (vsharp) {
|
|
|
|
vsharplength = js_strlen(vsharp);
|
|
|
|
MAKE_SHARP(he);
|
|
|
|
}
|
|
|
|
js_LeaveSharpObject(cx, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Remove '(function ' from the beginning of valstr and ')' from the
|
|
|
|
* end so that we can put "get" in front of the function definition.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
if (gsop[j] && IsFunctionObject(val[j])) {
|
2007-05-18 16:12:56 -07:00
|
|
|
const jschar *start = vchars;
|
2007-05-30 13:05:31 -07:00
|
|
|
const jschar *end = vchars + vlength;
|
|
|
|
|
2007-05-29 14:05:09 -07:00
|
|
|
uint8 parenChomp = 0;
|
|
|
|
if (vchars[0] == '(') {
|
2007-05-18 16:12:56 -07:00
|
|
|
vchars++;
|
2007-05-29 14:05:09 -07:00
|
|
|
parenChomp = 1;
|
|
|
|
}
|
2007-05-30 13:05:31 -07:00
|
|
|
|
|
|
|
/* Try to jump "function" keyword. */
|
|
|
|
if (vchars)
|
|
|
|
vchars = js_strchr_limit(vchars, ' ', end);
|
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
/*
|
|
|
|
* Jump over the function's name: it can't be encoded as part
|
|
|
|
* of an ECMA getter or setter.
|
|
|
|
*/
|
|
|
|
if (vchars)
|
|
|
|
vchars = js_strchr_limit(vchars, '(', end);
|
|
|
|
|
2007-05-21 11:01:08 -07:00
|
|
|
if (vchars) {
|
2007-05-30 13:05:31 -07:00
|
|
|
if (*vchars == ' ')
|
|
|
|
vchars++;
|
|
|
|
vlength = end - vchars - parenChomp;
|
2007-05-21 11:01:08 -07:00
|
|
|
} else {
|
|
|
|
gsop[j] = NULL;
|
|
|
|
vchars = start;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#define SAFE_ADD(n) \
|
|
|
|
JS_BEGIN_MACRO \
|
|
|
|
size_t n_ = (n); \
|
|
|
|
curlen += n_; \
|
|
|
|
if (curlen < n_) \
|
|
|
|
goto overflow; \
|
|
|
|
JS_END_MACRO
|
|
|
|
|
|
|
|
curlen = nchars;
|
|
|
|
if (comma)
|
|
|
|
SAFE_ADD(2);
|
|
|
|
SAFE_ADD(idstrlength + 1);
|
|
|
|
if (gsop[j])
|
2009-06-10 18:29:44 -07:00
|
|
|
SAFE_ADD(gsop[j]->length() + 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
SAFE_ADD(vsharplength);
|
|
|
|
SAFE_ADD(vlength);
|
|
|
|
/* Account for the trailing null. */
|
|
|
|
SAFE_ADD((outermost ? 2 : 1) + 1);
|
|
|
|
#undef SAFE_ADD
|
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
if (curlen > size_t(-1) / sizeof(jschar))
|
2007-03-22 10:30:00 -07:00
|
|
|
goto overflow;
|
|
|
|
|
|
|
|
/* Allocate 1 + 1 at end for closing brace and terminating 0. */
|
2009-10-06 12:35:33 -07:00
|
|
|
chars = (jschar *) js_realloc((ochars = chars), curlen * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!chars) {
|
|
|
|
/* Save code space on error: let JS_free ignore null vsharp. */
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(vsharp);
|
|
|
|
js_free(ochars);
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comma) {
|
|
|
|
chars[nchars++] = comma[0];
|
|
|
|
chars[nchars++] = comma[1];
|
|
|
|
}
|
|
|
|
comma = ", ";
|
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
if (gsop[j]) {
|
|
|
|
gsoplength = gsop[j]->length();
|
|
|
|
js_strncpy(&chars[nchars], gsop[j]->chars(),
|
|
|
|
gsoplength);
|
|
|
|
nchars += gsoplength;
|
|
|
|
chars[nchars++] = ' ';
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-10-06 12:35:33 -07:00
|
|
|
js_strncpy(&chars[nchars], idstrchars, idstrlength);
|
|
|
|
nchars += idstrlength;
|
|
|
|
/* Extraneous space after id here will be extracted later */
|
|
|
|
chars[nchars++] = gsop[j] ? ' ' : ':';
|
2007-05-15 15:41:24 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (vsharplength) {
|
|
|
|
js_strncpy(&chars[nchars], vsharp, vsharplength);
|
|
|
|
nchars += vsharplength;
|
|
|
|
}
|
|
|
|
js_strncpy(&chars[nchars], vchars, vlength);
|
|
|
|
nchars += vlength;
|
|
|
|
|
|
|
|
if (vsharp)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(vsharp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chars[nchars++] = '}';
|
|
|
|
if (outermost)
|
|
|
|
chars[nchars++] = ')';
|
|
|
|
chars[nchars] = 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
js_LeaveSharpObject(cx, &ida);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
if (chars)
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(chars);
|
2007-10-13 13:09:48 -07:00
|
|
|
goto out;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!chars) {
|
|
|
|
JS_ReportOutOfMemory(cx);
|
2007-10-13 13:09:48 -07:00
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
make_string:
|
2007-08-15 23:23:06 -07:00
|
|
|
str = js_NewString(cx, chars, nchars);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str) {
|
2009-07-27 21:10:12 -07:00
|
|
|
js_free(chars);
|
2007-10-13 13:09:48 -07:00
|
|
|
ok = JS_FALSE;
|
|
|
|
goto out;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(str);
|
2007-10-13 13:09:48 -07:00
|
|
|
ok = JS_TRUE;
|
|
|
|
out:
|
|
|
|
return ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
overflow:
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(vsharp);
|
|
|
|
js_free(chars);
|
2007-03-22 10:30:00 -07:00
|
|
|
chars = NULL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
#endif /* JS_HAS_TOSOURCE */
|
|
|
|
|
2010-06-23 14:35:10 -07:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
JSString *
|
|
|
|
obj_toStringHelper(JSContext *cx, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-23 14:35:10 -07:00
|
|
|
if (obj->isProxy())
|
|
|
|
return JSProxy::obj_toString(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-23 14:35:10 -07:00
|
|
|
const char *clazz = obj->wrappedObject(cx)->getClass()->name;
|
|
|
|
size_t nchars = 9 + strlen(clazz); /* 9 for "[object ]" */
|
|
|
|
jschar *chars = (jschar *) cx->malloc((nchars + 1) * sizeof(jschar));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!chars)
|
2010-06-23 14:35:10 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-23 14:35:10 -07:00
|
|
|
const char *prefix = "[object ";
|
2007-03-22 10:30:00 -07:00
|
|
|
nchars = 0;
|
|
|
|
while ((chars[nchars] = (jschar)*prefix) != 0)
|
|
|
|
nchars++, prefix++;
|
|
|
|
while ((chars[nchars] = (jschar)*clazz) != 0)
|
|
|
|
nchars++, clazz++;
|
|
|
|
chars[nchars++] = ']';
|
|
|
|
chars[nchars] = 0;
|
|
|
|
|
2010-06-23 14:35:10 -07:00
|
|
|
JSString *str = js_NewString(cx, chars, nchars);
|
|
|
|
if (!str)
|
2009-07-27 21:10:12 -07:00
|
|
|
cx->free(chars);
|
2010-06-23 14:35:10 -07:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_toString(JSContext *cx, uintN argc, Value *vp)
|
2010-06-23 14:35:10 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
2010-06-23 14:35:10 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
JSString *str = js::obj_toStringHelper(cx, obj);
|
|
|
|
if (!str)
|
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(str);
|
2010-06-23 14:35:10 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_toLocaleString(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ComputeThisFromVp(cx, vp))
|
2008-02-17 16:12:33 -08:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSString *str = js_ValueToString(cx, vp[1]);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setString(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_valueOf(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ComputeThisFromVp(cx, vp))
|
|
|
|
return JS_FALSE;
|
|
|
|
*vp = vp[1];
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-08 00:24:50 -08:00
|
|
|
/*
|
|
|
|
* Check if CSP allows new Function() or eval() to run in the current
|
|
|
|
* principals.
|
|
|
|
*/
|
|
|
|
JSBool
|
|
|
|
js_CheckContentSecurityPolicy(JSContext *cx)
|
|
|
|
{
|
2010-05-11 14:05:35 -07:00
|
|
|
JSSecurityCallbacks *callbacks = JS_GetSecurityCallbacks(cx);
|
2010-03-08 00:24:50 -08:00
|
|
|
|
|
|
|
// if there are callbacks, make sure that the CSP callback is installed and
|
|
|
|
// that it permits eval().
|
2010-03-18 17:27:39 -07:00
|
|
|
if (callbacks && callbacks->contentSecurityPolicyAllows)
|
|
|
|
return callbacks->contentSecurityPolicyAllows(cx);
|
2010-03-08 00:24:50 -08:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Check whether principals subsumes scopeobj's principals, and return true
|
|
|
|
* if so (or if scopeobj has no principals, for backward compatibility with
|
|
|
|
* the JS API, which does not require principals), and false otherwise.
|
|
|
|
*/
|
|
|
|
JSBool
|
|
|
|
js_CheckPrincipalsAccess(JSContext *cx, JSObject *scopeobj,
|
|
|
|
JSPrincipals *principals, JSAtom *caller)
|
|
|
|
{
|
2008-09-05 16:24:53 -07:00
|
|
|
JSSecurityCallbacks *callbacks;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSPrincipals *scopePrincipals;
|
|
|
|
const char *callerstr;
|
|
|
|
|
2008-09-05 16:24:53 -07:00
|
|
|
callbacks = JS_GetSecurityCallbacks(cx);
|
|
|
|
if (callbacks && callbacks->findObjectPrincipals) {
|
|
|
|
scopePrincipals = callbacks->findObjectPrincipals(cx, scopeobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!principals || !scopePrincipals ||
|
|
|
|
!principals->subsume(principals, scopePrincipals)) {
|
|
|
|
callerstr = js_AtomToPrintableString(cx, caller);
|
|
|
|
if (!callerstr)
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_INDIRECT_CALL, callerstr);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-29 14:25:06 -07:00
|
|
|
static JSObject *
|
|
|
|
CheckScopeChainValidity(JSContext *cx, JSObject *scopeobj, const char *caller)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSObject *inner;
|
|
|
|
|
|
|
|
if (!scopeobj)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
OBJ_TO_INNER_OBJECT(cx, scopeobj);
|
|
|
|
if (!scopeobj)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* XXX This is an awful gross hack. */
|
2010-06-12 09:29:04 -07:00
|
|
|
inner = scopeobj;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (scopeobj) {
|
2010-06-12 09:29:04 -07:00
|
|
|
JSObjectOp op = scopeobj->getClass()->ext.innerObject;
|
|
|
|
if (op && op(cx, scopeobj) != scopeobj)
|
|
|
|
goto bad;
|
2010-03-04 23:52:52 -08:00
|
|
|
scopeobj = scopeobj->getParent();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return inner;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_INDIRECT_CALL, caller);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-03-06 14:52:58 -08:00
|
|
|
const char *
|
|
|
|
js_ComputeFilename(JSContext *cx, JSStackFrame *caller,
|
|
|
|
JSPrincipals *principals, uintN *linenop)
|
|
|
|
{
|
|
|
|
uint32 flags;
|
2008-09-05 16:24:53 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
JSSecurityCallbacks *callbacks = JS_GetSecurityCallbacks(cx);
|
|
|
|
#endif
|
2008-03-06 14:52:58 -08:00
|
|
|
|
2008-09-05 16:24:53 -07:00
|
|
|
JS_ASSERT(principals || !(callbacks && callbacks->findObjectPrincipals));
|
2010-08-09 22:43:33 -07:00
|
|
|
flags = JS_GetScriptFilenameFlags(caller->script());
|
2008-03-06 14:52:58 -08:00
|
|
|
if ((flags & JSFILENAME_PROTECTED) &&
|
2008-08-27 13:56:34 -07:00
|
|
|
principals &&
|
2008-03-06 14:52:58 -08:00
|
|
|
strcmp(principals->codebase, "[System Principal]")) {
|
|
|
|
*linenop = 0;
|
|
|
|
return principals->codebase;
|
|
|
|
}
|
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
jsbytecode *pc = caller->pc(cx);
|
2010-08-09 22:43:33 -07:00
|
|
|
if (pc && js_GetOpcode(cx, caller->script(), pc) == JSOP_EVAL) {
|
|
|
|
JS_ASSERT(js_GetOpcode(cx, caller->script(), pc + JSOP_EVAL_LENGTH) == JSOP_LINENO);
|
2010-03-03 18:10:13 -08:00
|
|
|
*linenop = GET_UINT16(pc + JSOP_EVAL_LENGTH);
|
2008-03-24 10:00:01 -07:00
|
|
|
} else {
|
2008-11-13 00:30:20 -08:00
|
|
|
*linenop = js_FramePCToLineNumber(cx, caller);
|
2008-03-24 10:00:01 -07:00
|
|
|
}
|
2010-08-09 22:43:33 -07:00
|
|
|
return caller->script()->filename;
|
2008-03-06 14:52:58 -08:00
|
|
|
}
|
|
|
|
|
2009-01-27 16:40:40 -08:00
|
|
|
#ifndef EVAL_CACHE_CHAIN_LIMIT
|
|
|
|
# define EVAL_CACHE_CHAIN_LIMIT 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline JSScript **
|
|
|
|
EvalCacheHash(JSContext *cx, JSString *str)
|
|
|
|
{
|
|
|
|
const jschar *s;
|
|
|
|
size_t n;
|
|
|
|
uint32 h;
|
|
|
|
|
2009-06-10 18:29:44 -07:00
|
|
|
str->getCharsAndLength(s, n);
|
2009-01-27 16:40:40 -08:00
|
|
|
if (n > 100)
|
|
|
|
n = 100;
|
|
|
|
for (h = 0; n; s++, n--)
|
|
|
|
h = JS_ROTATE_LEFT32(h, 4) ^ *s;
|
|
|
|
|
|
|
|
h *= JS_GOLDEN_RATIO;
|
|
|
|
h >>= 32 - JS_EVAL_CACHE_SHIFT;
|
|
|
|
return &JS_SCRIPTS_TO_GC(cx)[h];
|
|
|
|
}
|
|
|
|
|
2008-10-08 15:08:33 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_eval(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-16 17:41:39 -08:00
|
|
|
if (argc < 1) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2010-02-16 17:41:39 -08:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-10-09 15:42:42 -07:00
|
|
|
|
2010-02-17 11:16:11 -08:00
|
|
|
JSStackFrame *caller = js_GetScriptedCaller(cx, NULL);
|
2009-08-17 18:08:20 -07:00
|
|
|
if (!caller) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_INDIRECT_CALL, js_eval_str);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
jsbytecode *callerPC = caller->pc(cx);
|
|
|
|
bool indirectCall = (callerPC && *callerPC != JSOP_EVAL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-03-04 16:27:50 -08:00
|
|
|
/*
|
2010-06-01 15:59:02 -07:00
|
|
|
* This call to JSObject::wrappedObject is safe because of the security
|
|
|
|
* checks we do below. However, the control flow below is confusing, so we
|
|
|
|
* double check. There are two cases:
|
2009-03-04 16:27:50 -08:00
|
|
|
* - Direct call: This object is never used. So unwrapping can't hurt.
|
|
|
|
* - Indirect call: If this object isn't already the scope chain (which
|
|
|
|
* we're guaranteed to be allowed to access) then we do a security
|
|
|
|
* check.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *argv = JS_ARGV(cx, vp);
|
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
2010-02-16 17:41:39 -08:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2010-06-01 15:59:02 -07:00
|
|
|
obj = obj->wrappedObject(cx);
|
2009-03-04 16:27:50 -08:00
|
|
|
|
2010-08-10 15:00:26 -07:00
|
|
|
OBJ_TO_INNER_OBJECT(cx, obj);
|
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2007-07-13 14:23:39 -07:00
|
|
|
/*
|
2010-08-10 15:00:26 -07:00
|
|
|
* Ban indirect uses of eval (nonglobal.eval = eval; nonglobal.eval(....))
|
|
|
|
* that attempt to use a non-global object as the scope object.
|
2007-06-04 16:03:03 -07:00
|
|
|
*/
|
2009-03-04 16:27:50 -08:00
|
|
|
{
|
2010-03-04 23:52:52 -08:00
|
|
|
JSObject *parent = obj->getParent();
|
2009-03-04 16:27:50 -08:00
|
|
|
if (indirectCall || parent) {
|
|
|
|
uintN flags = parent
|
|
|
|
? JSREPORT_ERROR
|
|
|
|
: JSREPORT_STRICT | JSREPORT_WARNING;
|
|
|
|
if (!JS_ReportErrorFlagsAndNumber(cx, flags, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_INDIRECT_CALL,
|
|
|
|
js_eval_str)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2007-06-05 16:42:00 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!argv[0].isString()) {
|
2010-02-16 17:41:39 -08:00
|
|
|
*vp = argv[0];
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-23 09:27:07 -07:00
|
|
|
/*
|
|
|
|
* We once supported a second argument to eval to use as the scope chain
|
|
|
|
* when evaluating the code string. Warn when such uses are seen so that
|
|
|
|
* authors will know that support for eval(s, o) has been removed.
|
|
|
|
*/
|
2010-08-09 22:43:33 -07:00
|
|
|
if (argc > 1 && !caller->script()->warnedAboutTwoArgumentEval) {
|
2010-07-23 09:27:07 -07:00
|
|
|
static const char TWO_ARGUMENT_WARNING[] =
|
|
|
|
"Support for eval(code, scopeObject) has been removed. "
|
|
|
|
"Use |with (scopeObject) eval(code);| instead.";
|
|
|
|
if (!JS_ReportWarning(cx, TWO_ARGUMENT_WARNING))
|
2008-12-12 23:47:23 -08:00
|
|
|
return JS_FALSE;
|
2010-08-09 22:43:33 -07:00
|
|
|
caller->script()->warnedAboutTwoArgumentEval = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-08 17:54:31 -08:00
|
|
|
/* From here on, control must exit through label out with ok set. */
|
2008-09-08 10:51:10 -07:00
|
|
|
MUST_FLOW_THROUGH("out");
|
2010-08-09 22:43:33 -07:00
|
|
|
uintN staticLevel = caller->script()->staticLevel + 1;
|
2009-10-21 14:34:55 -07:00
|
|
|
|
2010-07-23 09:27:07 -07:00
|
|
|
/*
|
|
|
|
* Bring fp->scopeChain up to date. We're either going to use
|
|
|
|
* it (direct call) or save it and restore it (indirect call).
|
|
|
|
*/
|
|
|
|
JSObject *callerScopeChain = js_GetScopeChain(cx, caller);
|
|
|
|
if (!callerScopeChain)
|
|
|
|
return JS_FALSE;
|
2009-08-17 18:08:20 -07:00
|
|
|
|
2010-07-23 09:27:07 -07:00
|
|
|
JSObject *scopeobj = NULL;
|
2007-10-09 15:42:42 -07:00
|
|
|
|
2010-07-23 09:27:07 -07:00
|
|
|
#if JS_HAS_EVAL_THIS_SCOPE
|
|
|
|
/*
|
|
|
|
* If we see an indirect call, then run eval in the global scope. We do
|
|
|
|
* this so the compiler can make assumptions about what bindings may or
|
|
|
|
* may not exist in the current frame if it doesn't see 'eval'.
|
|
|
|
*/
|
|
|
|
if (indirectCall) {
|
|
|
|
/* Pretend that we're top level. */
|
|
|
|
staticLevel = 0;
|
2007-10-09 15:42:42 -07:00
|
|
|
|
2010-07-23 09:27:07 -07:00
|
|
|
if (!js_CheckPrincipalsAccess(cx, obj,
|
2010-08-16 12:35:04 -07:00
|
|
|
js_StackFramePrincipals(cx, caller),
|
2010-07-23 09:27:07 -07:00
|
|
|
cx->runtime->atomState.evalAtom)) {
|
2010-02-16 17:41:39 -08:00
|
|
|
return JS_FALSE;
|
2010-07-23 09:27:07 -07:00
|
|
|
}
|
2008-12-12 23:47:23 -08:00
|
|
|
|
2010-07-23 09:27:07 -07:00
|
|
|
/* NB: We know inner is a global object here. */
|
|
|
|
JS_ASSERT(!obj->getParent());
|
|
|
|
scopeobj = obj;
|
|
|
|
} else {
|
2009-10-30 09:31:15 -07:00
|
|
|
/*
|
2010-07-23 09:27:07 -07:00
|
|
|
* Compile using the caller's current scope object.
|
|
|
|
*
|
|
|
|
* NB: This means that the C API must not be used to call eval.
|
2009-10-30 09:31:15 -07:00
|
|
|
*/
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT_IF(caller->isFunctionFrame(), caller->hasCallObj());
|
2010-07-23 09:27:07 -07:00
|
|
|
scopeobj = callerScopeChain;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-23 09:27:07 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Ensure we compile this eval with the right object in the scope chain. */
|
2010-07-29 14:25:06 -07:00
|
|
|
JSObject *result = CheckScopeChainValidity(cx, scopeobj, js_eval_str);
|
2010-02-16 17:41:39 -08:00
|
|
|
JS_ASSERT_IF(result, result == scopeobj);
|
|
|
|
if (!result)
|
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 00:24:50 -08:00
|
|
|
// CSP check: is eval() allowed at all?
|
|
|
|
// report errors via CSP is done in the script security mgr.
|
|
|
|
if (!js_CheckContentSecurityPolicy(cx)) {
|
|
|
|
JS_ReportError(cx, "call to eval() blocked by CSP");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *callee = &vp[0].toObject();
|
2010-02-16 17:41:39 -08:00
|
|
|
JSPrincipals *principals = js_EvalFramePrincipals(cx, callee, caller);
|
|
|
|
uintN line;
|
|
|
|
const char *file = js_ComputeFilename(cx, caller, principals, &line);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSString *str = argv[0].toString();
|
2010-02-16 17:41:39 -08:00
|
|
|
JSScript *script = NULL;
|
2010-08-31 16:45:34 -07:00
|
|
|
|
2010-08-24 08:50:54 -07:00
|
|
|
const jschar *chars;
|
|
|
|
size_t length;
|
|
|
|
str->getCharsAndLength(chars, length);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the eval string starts with '(' and ends with ')', it may be JSON.
|
|
|
|
* Try the JSON parser first because it's much faster. If the eval string
|
|
|
|
* isn't JSON, JSON parsing will probably fail quickly, so little time
|
|
|
|
* will be lost.
|
|
|
|
*/
|
|
|
|
if (length > 2 && chars[0] == '(' && chars[length-1] == ')') {
|
|
|
|
JSONParser *jp = js_BeginJSONParse(cx, vp, /* suppressErrors = */true);
|
|
|
|
JSBool ok = jp != NULL;
|
|
|
|
if (ok) {
|
|
|
|
/* Run JSON-parser on string inside ( and ). */
|
|
|
|
ok = js_ConsumeJSONText(cx, jp, chars+1, length-2);
|
|
|
|
ok &= js_FinishJSONParse(cx, jp, NullValue());
|
|
|
|
if (ok)
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2009-01-27 16:40:40 -08:00
|
|
|
|
2009-11-19 09:48:59 -08:00
|
|
|
/*
|
|
|
|
* Cache local eval scripts indexed by source qualified by scope.
|
|
|
|
*
|
|
|
|
* An eval cache entry should never be considered a hit unless its
|
|
|
|
* strictness matches that of the new eval code. The existing code takes
|
|
|
|
* care of this, because hits are qualified by the function from which
|
|
|
|
* eval was called, whose strictness doesn't change. Scripts produced by
|
|
|
|
* calls to eval from global code are not cached.
|
|
|
|
*/
|
2010-02-16 17:41:39 -08:00
|
|
|
JSScript **bucket = EvalCacheHash(cx, str);
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!indirectCall && caller->isFunctionFrame()) {
|
2009-01-27 16:40:40 -08:00
|
|
|
uintN count = 0;
|
|
|
|
JSScript **scriptp = bucket;
|
|
|
|
|
|
|
|
EVAL_CACHE_METER(probe);
|
2010-09-13 09:38:22 -07:00
|
|
|
JSVersion version = cx->findVersion();
|
2009-01-27 16:40:40 -08:00
|
|
|
while ((script = *scriptp) != NULL) {
|
2009-10-08 10:29:03 -07:00
|
|
|
if (script->savedCallerFun &&
|
2009-12-03 12:12:39 -08:00
|
|
|
script->staticLevel == staticLevel &&
|
2010-09-13 09:38:22 -07:00
|
|
|
script->version == version &&
|
2009-01-27 16:40:40 -08:00
|
|
|
(script->principals == principals ||
|
|
|
|
(principals->subsume(principals, script->principals) &&
|
|
|
|
script->principals->subsume(script->principals, principals)))) {
|
|
|
|
/*
|
|
|
|
* Get the prior (cache-filling) eval's saved caller function.
|
2010-04-10 21:15:35 -07:00
|
|
|
* See Compiler::compileScript in jsparse.cpp.
|
2009-01-27 16:40:40 -08:00
|
|
|
*/
|
2009-10-18 17:41:24 -07:00
|
|
|
JSFunction *fun = script->getFunction(0);
|
2009-01-27 16:40:40 -08:00
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fun == caller->fun()) {
|
2009-01-27 16:40:40 -08:00
|
|
|
/*
|
|
|
|
* Get the source string passed for safekeeping in the
|
2010-04-10 21:15:35 -07:00
|
|
|
* atom map by the prior eval to Compiler::compileScript.
|
2009-01-27 16:40:40 -08:00
|
|
|
*/
|
|
|
|
JSString *src = ATOM_TO_STRING(script->atomMap.vector[0]);
|
|
|
|
|
|
|
|
if (src == str || js_EqualStrings(src, str)) {
|
|
|
|
/*
|
|
|
|
* Source matches, qualify by comparing scopeobj to the
|
|
|
|
* COMPILE_N_GO-memoized parent of the first literal
|
|
|
|
* function or regexp object if any. If none, then this
|
|
|
|
* script has no compiled-in dependencies on the prior
|
|
|
|
* eval's scopeobj.
|
|
|
|
*/
|
2009-09-09 16:52:55 -07:00
|
|
|
JSObjectArray *objarray = script->objects();
|
2009-01-27 16:40:40 -08:00
|
|
|
int i = 1;
|
2009-10-18 17:41:24 -07:00
|
|
|
|
2009-01-27 16:40:40 -08:00
|
|
|
if (objarray->length == 1) {
|
|
|
|
if (script->regexpsOffset != 0) {
|
2009-09-09 16:52:55 -07:00
|
|
|
objarray = script->regexps();
|
2009-01-27 16:40:40 -08:00
|
|
|
i = 0;
|
|
|
|
} else {
|
|
|
|
EVAL_CACHE_METER(noscope);
|
|
|
|
i = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < 0 ||
|
2010-03-04 23:52:52 -08:00
|
|
|
objarray->vector[i]->getParent() == scopeobj) {
|
2009-11-30 13:01:11 -08:00
|
|
|
JS_ASSERT(staticLevel == script->staticLevel);
|
2009-01-27 16:40:40 -08:00
|
|
|
EVAL_CACHE_METER(hit);
|
|
|
|
*scriptp = script->u.nextToGC;
|
|
|
|
script->u.nextToGC = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++count == EVAL_CACHE_CHAIN_LIMIT) {
|
|
|
|
script = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
EVAL_CACHE_METER(step);
|
|
|
|
scriptp = &script->u.nextToGC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-29 16:38:00 -07:00
|
|
|
/*
|
|
|
|
* We can't have a callerFrame (down in js_Execute's terms) if we're in
|
|
|
|
* global code. This includes indirect eval and direct eval called with a
|
|
|
|
* scope object parameter.
|
|
|
|
*/
|
2010-02-16 17:41:39 -08:00
|
|
|
JSStackFrame *callerFrame = (staticLevel != 0) ? caller : NULL;
|
2007-10-09 15:42:42 -07:00
|
|
|
if (!script) {
|
2010-05-22 15:38:04 -07:00
|
|
|
uint32 tcflags = TCF_COMPILE_N_GO | TCF_NEED_MUTABLE_SCRIPT | TCF_COMPILE_FOR_EVAL;
|
2010-04-10 21:15:35 -07:00
|
|
|
script = Compiler::compileScript(cx, scopeobj, callerFrame,
|
2010-05-22 15:38:04 -07:00
|
|
|
principals, tcflags,
|
2010-08-24 08:50:54 -07:00
|
|
|
chars, length,
|
2010-04-10 21:15:35 -07:00
|
|
|
NULL, file, line, str, staticLevel);
|
2010-02-16 17:41:39 -08:00
|
|
|
if (!script)
|
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Belt-and-braces: check that the lesser of eval's principals and the
|
|
|
|
* caller's principals has access to scopeobj.
|
|
|
|
*/
|
2010-02-16 17:41:39 -08:00
|
|
|
JSBool ok = js_CheckPrincipalsAccess(cx, scopeobj, principals,
|
|
|
|
cx->runtime->atomState.evalAtom) &&
|
2010-07-14 23:19:36 -07:00
|
|
|
Execute(cx, scopeobj, script, callerFrame, JSFRAME_EVAL, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-01-27 16:40:40 -08:00
|
|
|
script->u.nextToGC = *bucket;
|
|
|
|
*bucket = script;
|
2008-08-21 05:04:20 -07:00
|
|
|
#ifdef CHECK_SCRIPT_OWNER
|
|
|
|
script->owner = NULL;
|
|
|
|
#endif
|
2007-10-09 15:42:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_OBJ_WATCHPOINT
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_watch_handler(JSContext *cx, JSObject *obj, jsid id, jsval old,
|
|
|
|
jsval *nvp, void *closure)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSObject *callable;
|
2008-09-05 16:24:53 -07:00
|
|
|
JSSecurityCallbacks *callbacks;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSStackFrame *caller;
|
|
|
|
JSPrincipals *subject, *watcher;
|
|
|
|
JSResolvingKey key;
|
|
|
|
JSResolvingEntry *entry;
|
|
|
|
uint32 generation;
|
2010-07-14 23:19:36 -07:00
|
|
|
Value argv[3];
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool ok;
|
|
|
|
|
|
|
|
callable = (JSObject *) closure;
|
|
|
|
|
2008-09-05 16:24:53 -07:00
|
|
|
callbacks = JS_GetSecurityCallbacks(cx);
|
|
|
|
if (callbacks && callbacks->findObjectPrincipals) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Skip over any obj_watch_* frames between us and the real subject. */
|
2008-12-09 08:38:32 -08:00
|
|
|
caller = js_GetScriptedCaller(cx, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (caller) {
|
|
|
|
/*
|
|
|
|
* Only call the watch handler if the watcher is allowed to watch
|
|
|
|
* the currently executing script.
|
|
|
|
*/
|
2008-09-05 16:24:53 -07:00
|
|
|
watcher = callbacks->findObjectPrincipals(cx, callable);
|
2010-08-16 12:35:04 -07:00
|
|
|
subject = js_StackFramePrincipals(cx, caller);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (watcher && subject && !watcher->subsume(watcher, subject)) {
|
|
|
|
/* Silently don't call the watch handler. */
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Avoid recursion on (obj, id) already being watched on cx. */
|
|
|
|
key.obj = obj;
|
|
|
|
key.id = id;
|
|
|
|
if (!js_StartResolving(cx, &key, JSRESFLAG_WATCH, &entry))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!entry)
|
|
|
|
return JS_TRUE;
|
|
|
|
generation = cx->resolvingTable->generation;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
argv[0] = IdToValue(id);
|
|
|
|
argv[1] = Valueify(old);
|
|
|
|
argv[2] = Valueify(*nvp);
|
2010-08-16 12:35:04 -07:00
|
|
|
ok = ExternalInvoke(cx, obj, ObjectOrNullValue(callable), 3, argv, Valueify(nvp));
|
2007-03-22 10:30:00 -07:00
|
|
|
js_StopResolving(cx, &key, JSRESFLAG_WATCH, entry, generation);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_watch(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc <= 1) {
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ReportMissingArg(cx, *vp, 1);
|
2010-08-13 06:21:13 -07:00
|
|
|
return JS_FALSE;
|
2008-08-08 09:02:50 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *callable = js_ValueToCallableObject(cx, &vp[3], 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!callable)
|
2010-08-13 06:21:13 -07:00
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Compute the unique int/atom symbol id needed by js_LookupProperty. */
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid propid;
|
|
|
|
if (!ValueToId(cx, vp[2], &propid))
|
2010-08-13 06:21:13 -07:00
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
|
|
|
Value tmp;
|
|
|
|
uintN attrs;
|
2010-08-13 06:21:13 -07:00
|
|
|
if (!obj || !CheckAccess(cx, obj, propid, JSACC_WATCH, &tmp, &attrs))
|
|
|
|
return JS_FALSE;
|
2010-04-12 17:47:35 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2008-07-01 12:47:09 -07:00
|
|
|
|
2010-04-12 17:47:35 -07:00
|
|
|
if (attrs & JSPROP_READONLY)
|
2010-08-13 06:21:13 -07:00
|
|
|
return JS_TRUE;
|
2010-06-02 23:44:24 -07:00
|
|
|
if (obj->isDenseArray() && !obj->makeDenseArraySlow(cx))
|
2010-08-13 06:21:13 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
return JS_SetWatchPoint(cx, obj, propid, obj_watch_handler, callable);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_unwatch(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
2008-02-17 16:12:33 -08:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
|
|
|
jsid id;
|
|
|
|
if (argc != 0) {
|
|
|
|
if (!ValueToId(cx, vp[2], &id))
|
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
id = JSID_VOID;
|
|
|
|
}
|
|
|
|
return JS_ClearWatchPoint(cx, obj, id, NULL, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* JS_HAS_OBJ_WATCHPOINT */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prototype and property query methods, to complement the 'in' and
|
|
|
|
* 'instanceof' operators.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Proposed ECMA 15.2.4.5. */
|
2008-10-08 15:08:33 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_hasOwnProperty(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
2008-02-17 16:12:33 -08:00
|
|
|
return obj &&
|
2010-06-12 09:29:04 -07:00
|
|
|
js_HasOwnPropertyHelper(cx, obj->getOps()->lookupProperty, argc, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2008-08-08 09:02:50 -07:00
|
|
|
js_HasOwnPropertyHelper(JSContext *cx, JSLookupPropOp lookup, uintN argc,
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsid id;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2009-07-21 19:35:59 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
2009-06-05 12:56:45 -07:00
|
|
|
JSObject *obj2;
|
|
|
|
JSProperty *prop;
|
2010-05-18 19:21:43 -07:00
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
if (obj->isProxy()) {
|
|
|
|
bool has;
|
|
|
|
if (!JSProxy::hasOwn(cx, obj, id, &has))
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean(has);
|
2010-05-18 19:21:43 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!js_HasOwnProperty(cx, lookup, obj, id, &obj2, &prop))
|
2009-07-21 19:35:59 -07:00
|
|
|
return JS_FALSE;
|
2009-06-05 12:56:45 -07:00
|
|
|
if (prop) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean(true);
|
2009-06-05 12:56:45 -07:00
|
|
|
obj2->dropProperty(cx, prop);
|
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean(false);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
2009-07-21 19:35:59 -07:00
|
|
|
return JS_TRUE;
|
2008-08-11 17:47:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_HasOwnProperty(JSContext *cx, JSLookupPropOp lookup, JSObject *obj, jsid id,
|
2009-06-05 12:56:45 -07:00
|
|
|
JSObject **objp, JSProperty **propp)
|
2008-08-11 17:47:05 -07:00
|
|
|
{
|
2010-06-04 14:20:53 -07:00
|
|
|
JSAutoResolveFlags rf(cx, JSRESOLVE_QUALIFIED | JSRESOLVE_DETECTING);
|
2010-06-12 09:29:04 -07:00
|
|
|
if (!(lookup ? lookup : js_LookupProperty)(cx, obj, id, objp, propp))
|
2010-03-22 11:11:44 -07:00
|
|
|
return false;
|
2009-06-05 12:56:45 -07:00
|
|
|
if (!*propp)
|
2010-03-22 11:11:44 -07:00
|
|
|
return true;
|
2009-06-05 12:56:45 -07:00
|
|
|
|
|
|
|
if (*objp == obj)
|
2010-03-22 11:11:44 -07:00
|
|
|
return true;
|
2009-06-05 12:56:45 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = (*objp)->getClass();
|
2010-06-12 09:29:04 -07:00
|
|
|
JSObject *outer = NULL;
|
|
|
|
if (JSObjectOp op = (*objp)->getClass()->ext.outerObject) {
|
|
|
|
outer = op(cx, *objp);
|
2009-06-05 12:56:45 -07:00
|
|
|
if (!outer)
|
2010-03-22 11:11:44 -07:00
|
|
|
return false;
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
2010-03-22 11:11:44 -07:00
|
|
|
|
2009-06-05 12:56:45 -07:00
|
|
|
if (outer != *objp) {
|
2010-03-31 22:13:51 -07:00
|
|
|
if ((*objp)->isNative() && obj->getClass() == clasp) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* The combination of JSPROP_SHARED and JSPROP_PERMANENT in a
|
|
|
|
* delegated property makes that property appear to be direct in
|
|
|
|
* all delegating instances of the same native class. This hack
|
|
|
|
* avoids bloating every function instance with its own 'length'
|
|
|
|
* (AKA 'arity') property. But it must not extend across class
|
|
|
|
* boundaries, to avoid making hasOwnProperty lie (bug 320854).
|
|
|
|
*
|
|
|
|
* It's not really a hack, of course: a permanent property can't
|
|
|
|
* be deleted, and JSPROP_SHARED means "don't allocate a slot in
|
|
|
|
* any instance, prototype or delegating". Without a slot, and
|
|
|
|
* without the ability to remove and recreate (with differences)
|
|
|
|
* the property, there is no way to tell whether it is directly
|
|
|
|
* owned, or indirectly delegated.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
Shape *shape = reinterpret_cast<Shape *>(*propp);
|
|
|
|
if (shape->isSharedPermanent())
|
2010-03-22 11:11:44 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-03-22 11:11:44 -07:00
|
|
|
|
|
|
|
(*objp)->dropProperty(cx, *propp);
|
|
|
|
*propp = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-03-22 11:11:44 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Proposed ECMA 15.2.4.6. */
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_isPrototypeOf(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
2010-04-12 20:05:03 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &v = argc != 0 ? vp[2] : UndefinedValue();
|
|
|
|
vp->setBoolean(js_IsDelegate(cx, obj, v));
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Proposed ECMA 15.2.4.7. */
|
2008-10-08 15:08:33 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_propertyIsEnumerable(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsid id;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
2008-08-11 17:47:05 -07:00
|
|
|
return obj && js_PropertyIsEnumerable(cx, obj, id, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_PropertyIsEnumerable(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2008-08-11 17:47:05 -07:00
|
|
|
{
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj->lookupProperty(cx, id, &pobj, &prop))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (!prop) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX ECMA spec error compatible: return false unless hasOwnProperty.
|
|
|
|
* The ECMA spec really should be fixed so propertyIsEnumerable and the
|
|
|
|
* for..in loop agree on whether prototype properties are enumerable,
|
|
|
|
* obviously by fixing this method (not by breaking the for..in loop!).
|
|
|
|
*
|
|
|
|
* We check here for shared permanent prototype properties, which should
|
|
|
|
* be treated as if they are local to obj. They are an implementation
|
|
|
|
* technique used to satisfy ECMA requirements; users should not be able
|
|
|
|
* to distinguish a shared permanent proto-property from a local one.
|
|
|
|
*/
|
2010-06-05 14:24:54 -07:00
|
|
|
bool shared;
|
|
|
|
uintN attrs;
|
|
|
|
if (pobj->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
Shape *shape = (Shape *) prop;
|
|
|
|
shared = shape->isSharedPermanent();
|
|
|
|
attrs = shape->attributes();
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
|
|
|
} else {
|
|
|
|
shared = false;
|
|
|
|
if (!pobj->getAttributes(cx, id, &attrs))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (pobj != obj && !shared) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean(false);
|
2010-06-05 14:24:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setBoolean((attrs & JSPROP_ENUMERATE) != 0);
|
2010-06-05 14:24:54 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:35:33 -07:00
|
|
|
#if OLD_GETTER_SETTER_METHODS
|
|
|
|
|
|
|
|
const char js_defineGetter_str[] = "__defineGetter__";
|
|
|
|
const char js_defineSetter_str[] = "__defineSetter__";
|
|
|
|
const char js_lookupGetter_str[] = "__lookupGetter__";
|
|
|
|
const char js_lookupSetter_str[] = "__lookupSetter__";
|
|
|
|
|
2009-01-23 15:44:01 -08:00
|
|
|
JS_FRIEND_API(JSBool)
|
2010-07-14 23:19:36 -07:00
|
|
|
js_obj_defineGetter(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-22 22:25:09 -08:00
|
|
|
if (argc <= 1 || !js_IsCallable(vp[3])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_GETTER_OR_SETTER,
|
|
|
|
js_getter_str);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyOp getter = CastAsPropertyOp(&vp[3].toObject());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid id;
|
|
|
|
if (!ValueToId(cx, vp[2], &id))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
|
|
|
if (!obj || !CheckRedeclaration(cx, obj, id, JSPROP_GETTER, NULL, NULL))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-08-13 06:21:13 -07:00
|
|
|
/*
|
|
|
|
* Getters and setters are just like watchpoints from an access
|
|
|
|
* control point of view.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
Value junk;
|
|
|
|
uintN attrs;
|
2010-08-13 06:21:13 -07:00
|
|
|
if (!CheckAccess(cx, obj, id, JSACC_WATCH, &junk, &attrs))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
|
|
|
return obj->defineProperty(cx, id, UndefinedValue(), getter, PropertyStub,
|
2009-08-26 14:28:36 -07:00
|
|
|
JSPROP_ENUMERATE | JSPROP_GETTER | JSPROP_SHARED);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-01-23 15:44:01 -08:00
|
|
|
JS_FRIEND_API(JSBool)
|
2010-07-14 23:19:36 -07:00
|
|
|
js_obj_defineSetter(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-22 22:25:09 -08:00
|
|
|
if (argc <= 1 || !js_IsCallable(vp[3])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_BAD_GETTER_OR_SETTER,
|
|
|
|
js_setter_str);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyOp setter = CastAsPropertyOp(&vp[3].toObject());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid id;
|
|
|
|
if (!ValueToId(cx, vp[2], &id))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
|
|
|
if (!obj || !CheckRedeclaration(cx, obj, id, JSPROP_SETTER, NULL, NULL))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-08-13 06:21:13 -07:00
|
|
|
/*
|
|
|
|
* Getters and setters are just like watchpoints from an access
|
|
|
|
* control point of view.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
Value junk;
|
|
|
|
uintN attrs;
|
2010-08-13 06:21:13 -07:00
|
|
|
if (!CheckAccess(cx, obj, id, JSACC_WATCH, &junk, &attrs))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
|
|
|
return obj->defineProperty(cx, id, UndefinedValue(), PropertyStub, setter,
|
2009-08-26 14:28:36 -07:00
|
|
|
JSPROP_ENUMERATE | JSPROP_SETTER | JSPROP_SHARED);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_lookupGetter(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsid id;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj || !obj->lookupProperty(cx, id, &pobj, &prop))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (prop) {
|
2010-03-31 22:13:51 -07:00
|
|
|
if (pobj->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
Shape *shape = (Shape *) prop;
|
|
|
|
if (shape->hasGetterValue())
|
|
|
|
*vp = shape->getterValue();
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_lookupSetter(JSContext *cx, uintN argc, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsid id;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = ComputeThisFromVp(cx, vp);
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj || !obj->lookupProperty(cx, id, &pobj, &prop))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (prop) {
|
2010-03-31 22:13:51 -07:00
|
|
|
if (pobj->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
Shape *shape = (Shape *) prop;
|
|
|
|
if (shape->hasSetterValue())
|
|
|
|
*vp = shape->setterValue();
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2009-10-06 12:35:33 -07:00
|
|
|
#endif /* OLD_GETTER_SETTER_METHODS */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-04 16:43:12 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_getPrototypeOf(JSContext *cx, uintN argc, Value *vp)
|
2008-08-04 16:43:12 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc == 0) {
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ReportMissingArg(cx, *vp, 0);
|
2008-08-08 09:02:50 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp[2].isPrimitive()) {
|
2010-08-09 14:07:27 -07:00
|
|
|
char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, vp[2], NULL);
|
2009-08-17 14:54:52 -07:00
|
|
|
if (!bytes)
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_UNEXPECTED_TYPE, bytes, "not an object");
|
|
|
|
JS_free(cx, bytes);
|
2008-08-04 16:43:12 -07:00
|
|
|
return JS_FALSE;
|
2009-08-17 14:54:52 -07:00
|
|
|
}
|
2008-08-04 16:43:12 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = &vp[2].toObject();
|
|
|
|
uintN attrs;
|
2010-08-13 06:21:13 -07:00
|
|
|
return CheckAccess(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.protoAtom),
|
|
|
|
JSACC_PROTO, vp, &attrs);
|
2008-08-04 16:43:12 -07:00
|
|
|
}
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
extern JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_NewPropertyDescriptorObject(JSContext *cx, jsid id, uintN attrs,
|
|
|
|
const Value &getter, const Value &setter,
|
|
|
|
const Value &value, Value *vp)
|
2010-05-18 19:21:43 -07:00
|
|
|
{
|
|
|
|
/* We have our own property, so start creating the descriptor. */
|
2010-06-18 17:43:02 -07:00
|
|
|
JSObject *desc = NewBuiltinClassInstance(cx, &js_ObjectClass);
|
2010-05-18 19:21:43 -07:00
|
|
|
if (!desc)
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*desc); /* Root and return. */
|
2010-05-18 19:21:43 -07:00
|
|
|
|
|
|
|
const JSAtomState &atomState = cx->runtime->atomState;
|
|
|
|
if (attrs & (JSPROP_GETTER | JSPROP_SETTER)) {
|
|
|
|
if (!desc->defineProperty(cx, ATOM_TO_JSID(atomState.getAtom), getter,
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyStub, PropertyStub, JSPROP_ENUMERATE) ||
|
2010-05-18 19:21:43 -07:00
|
|
|
!desc->defineProperty(cx, ATOM_TO_JSID(atomState.setAtom), setter,
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyStub, PropertyStub, JSPROP_ENUMERATE)) {
|
2010-05-18 19:21:43 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!desc->defineProperty(cx, ATOM_TO_JSID(atomState.valueAtom), value,
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyStub, PropertyStub, JSPROP_ENUMERATE) ||
|
2010-05-18 19:21:43 -07:00
|
|
|
!desc->defineProperty(cx, ATOM_TO_JSID(atomState.writableAtom),
|
2010-07-14 23:19:36 -07:00
|
|
|
BooleanValue((attrs & JSPROP_READONLY) == 0),
|
|
|
|
PropertyStub, PropertyStub, JSPROP_ENUMERATE)) {
|
2010-05-18 19:21:43 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return desc->defineProperty(cx, ATOM_TO_JSID(atomState.enumerableAtom),
|
2010-07-14 23:19:36 -07:00
|
|
|
BooleanValue((attrs & JSPROP_ENUMERATE) != 0),
|
|
|
|
PropertyStub, PropertyStub, JSPROP_ENUMERATE) &&
|
2010-05-18 19:21:43 -07:00
|
|
|
desc->defineProperty(cx, ATOM_TO_JSID(atomState.configurableAtom),
|
2010-07-14 23:19:36 -07:00
|
|
|
BooleanValue((attrs & JSPROP_PERMANENT) == 0),
|
|
|
|
PropertyStub, PropertyStub, JSPROP_ENUMERATE);
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 12:36:42 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_GetOwnPropertyDescriptor(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2009-07-21 19:35:59 -07:00
|
|
|
{
|
2010-05-18 19:21:43 -07:00
|
|
|
if (obj->isProxy()) {
|
|
|
|
if (!JSProxy::getOwnPropertyDescriptor(cx, obj, id, vp))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-06-05 12:56:45 -07:00
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2010-06-12 09:29:04 -07:00
|
|
|
if (!js_HasOwnProperty(cx, obj->getOps()->lookupProperty, obj, id, &pobj, &prop))
|
2010-03-24 12:36:42 -07:00
|
|
|
return false;
|
2009-06-05 12:56:45 -07:00
|
|
|
if (!prop) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2010-03-24 12:36:42 -07:00
|
|
|
return true;
|
2009-07-21 19:35:59 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value roots[] = { UndefinedValue(), UndefinedValue(), UndefinedValue() };
|
2010-01-11 09:52:21 -08:00
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(roots), roots);
|
2010-06-05 14:24:54 -07:00
|
|
|
unsigned attrs;
|
|
|
|
bool doGet = true;
|
|
|
|
if (pobj->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
Shape *shape = (Shape *) prop;
|
|
|
|
attrs = shape->attributes();
|
2010-06-05 14:24:54 -07:00
|
|
|
if (attrs & (JSPROP_GETTER | JSPROP_SETTER)) {
|
|
|
|
doGet = false;
|
2009-11-09 16:16:28 -08:00
|
|
|
if (attrs & JSPROP_GETTER)
|
2010-08-29 11:57:08 -07:00
|
|
|
roots[0] = shape->getterValue();
|
2009-11-09 16:16:28 -08:00
|
|
|
if (attrs & JSPROP_SETTER)
|
2010-08-29 11:57:08 -07:00
|
|
|
roots[1] = shape->setterValue();
|
2009-11-09 16:16:28 -08:00
|
|
|
}
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
|
|
|
} else if (!pobj->getAttributes(cx, id, &attrs)) {
|
|
|
|
return false;
|
2009-11-09 16:16:28 -08:00
|
|
|
}
|
2009-07-21 19:35:59 -07:00
|
|
|
|
2010-06-05 14:24:54 -07:00
|
|
|
if (doGet && !obj->getProperty(cx, id, &roots[2]))
|
|
|
|
return false;
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
return js_NewPropertyDescriptorObject(cx, id,
|
|
|
|
attrs,
|
|
|
|
roots[0], /* getter */
|
|
|
|
roots[1], /* setter */
|
|
|
|
roots[2], /* value */
|
|
|
|
vp);
|
2009-07-21 19:35:59 -07:00
|
|
|
}
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
static bool
|
2010-07-14 23:19:36 -07:00
|
|
|
GetFirstArgumentAsObject(JSContext *cx, uintN argc, Value *vp, const char *method, JSObject **objp)
|
2010-03-24 12:36:42 -07:00
|
|
|
{
|
2009-09-24 14:33:14 -07:00
|
|
|
if (argc == 0) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
|
|
|
|
method, "0", "s");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &v = vp[2];
|
|
|
|
if (v.isPrimitive()) {
|
|
|
|
char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, NULL);
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!bytes)
|
|
|
|
return false;
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_UNEXPECTED_TYPE,
|
|
|
|
bytes, "not an object");
|
|
|
|
JS_free(cx, bytes);
|
2010-03-24 12:36:42 -07:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-24 14:33:14 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
*objp = &v.toObject();
|
2009-09-24 14:33:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_getOwnPropertyDescriptor(JSContext *cx, uintN argc, Value *vp)
|
2009-09-24 14:33:14 -07:00
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.getOwnPropertyDescriptor", &obj))
|
|
|
|
return JS_FALSE;
|
2010-03-28 20:32:20 -07:00
|
|
|
AutoIdRooter nameidr(cx);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToId(cx, argc >= 2 ? vp[3] : UndefinedValue(), nameidr.addr()))
|
2010-03-24 12:36:42 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
return js_GetOwnPropertyDescriptor(cx, obj, nameidr.id(), vp);
|
|
|
|
}
|
|
|
|
|
2009-09-08 15:59:14 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_keys(JSContext *cx, uintN argc, Value *vp)
|
2009-09-08 15:59:14 -07:00
|
|
|
{
|
2009-09-24 14:33:14 -07:00
|
|
|
JSObject *obj;
|
|
|
|
if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.keys", &obj))
|
2009-09-08 15:59:14 -07:00
|
|
|
return JS_FALSE;
|
2010-05-18 19:21:43 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoIdVector props(cx);
|
2010-06-24 15:03:54 -07:00
|
|
|
if (!GetPropertyNames(cx, obj, JSITER_OWNONLY, props))
|
2009-09-08 15:59:14 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoValueVector vals(cx);
|
|
|
|
vals.resize(props.length());
|
2010-06-24 15:03:54 -07:00
|
|
|
for (size_t i = 0, len = props.length(); i < len; i++) {
|
2010-07-14 23:19:36 -07:00
|
|
|
jsid id = props[i];
|
|
|
|
if (JSID_IS_STRING(id)) {
|
|
|
|
vals[i].setString(JSID_TO_STRING(id));
|
|
|
|
} else {
|
|
|
|
JS_ASSERT(JSID_IS_INT(id));
|
|
|
|
JSString *str = js_IntToString(cx, JSID_TO_INT(id));
|
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
vals[i].setString(str);
|
|
|
|
}
|
2010-06-24 15:03:54 -07:00
|
|
|
}
|
2009-09-08 15:59:14 -07:00
|
|
|
|
2010-06-24 15:03:54 -07:00
|
|
|
JS_ASSERT(props.length() <= UINT32_MAX);
|
2010-07-22 18:45:21 -07:00
|
|
|
JSObject *aobj = js_NewArrayObject(cx, jsuint(vals.length()), vals.begin());
|
2009-09-08 15:59:14 -07:00
|
|
|
if (!aobj)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*aobj);
|
2009-09-08 15:59:14 -07:00
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-06-05 12:56:45 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
HasProperty(JSContext* cx, JSObject* obj, jsid id, Value* vp, JSBool* answerp)
|
2009-06-05 12:56:45 -07:00
|
|
|
{
|
|
|
|
if (!JS_HasPropertyById(cx, obj, id, answerp))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!*answerp) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
return JS_GetPropertyById(cx, obj, id, Jsvalify(vp));
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
PropDesc::PropDesc()
|
|
|
|
: pd(UndefinedValue()),
|
|
|
|
id(INT_TO_JSID(0)),
|
|
|
|
value(UndefinedValue()),
|
|
|
|
get(UndefinedValue()),
|
|
|
|
set(UndefinedValue()),
|
2009-06-05 12:56:45 -07:00
|
|
|
attrs(0),
|
|
|
|
hasGet(false),
|
|
|
|
hasSet(false),
|
|
|
|
hasValue(false),
|
|
|
|
hasWritable(false),
|
|
|
|
hasEnumerable(false),
|
|
|
|
hasConfigurable(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-07-14 23:19:36 -07:00
|
|
|
PropDesc::initialize(JSContext* cx, jsid id, const Value &origval)
|
2009-06-05 12:56:45 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v = origval;
|
2009-06-05 12:56:45 -07:00
|
|
|
this->id = id;
|
|
|
|
|
|
|
|
/* 8.10.5 step 1 */
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isPrimitive()) {
|
2010-01-04 11:48:30 -08:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NOT_NONNULL_OBJECT);
|
2009-06-05 12:56:45 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject* desc = &v.toObject();
|
2009-06-05 12:56:45 -07:00
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
/* Make a copy of the descriptor. We might need it later. */
|
|
|
|
pd = v;
|
|
|
|
|
2009-06-05 12:56:45 -07:00
|
|
|
/* Start with the proper defaults. */
|
|
|
|
attrs = JSPROP_PERMANENT | JSPROP_READONLY;
|
|
|
|
|
|
|
|
JSBool hasProperty;
|
|
|
|
|
|
|
|
/* 8.10.5 step 3 */
|
|
|
|
if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.enumerableAtom), &v,
|
|
|
|
&hasProperty)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (hasProperty) {
|
|
|
|
hasEnumerable = JS_TRUE;
|
|
|
|
if (js_ValueToBoolean(v))
|
|
|
|
attrs |= JSPROP_ENUMERATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.10.5 step 4 */
|
|
|
|
if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.configurableAtom), &v,
|
|
|
|
&hasProperty)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (hasProperty) {
|
|
|
|
hasConfigurable = JS_TRUE;
|
|
|
|
if (js_ValueToBoolean(v))
|
|
|
|
attrs &= ~JSPROP_PERMANENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.10.5 step 5 */
|
|
|
|
if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.valueAtom), &v, &hasProperty))
|
|
|
|
return false;
|
|
|
|
if (hasProperty) {
|
|
|
|
hasValue = true;
|
|
|
|
value = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.10.6 step 6 */
|
|
|
|
if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.writableAtom), &v, &hasProperty))
|
|
|
|
return false;
|
|
|
|
if (hasProperty) {
|
|
|
|
hasWritable = JS_TRUE;
|
|
|
|
if (js_ValueToBoolean(v))
|
|
|
|
attrs &= ~JSPROP_READONLY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.10.7 step 7 */
|
|
|
|
if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.getAtom), &v, &hasProperty))
|
|
|
|
return false;
|
|
|
|
if (hasProperty) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if ((v.isPrimitive() || !js_IsCallable(v)) && !v.isUndefined()) {
|
2010-04-04 21:59:09 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_GET_SET_FIELD,
|
2009-06-05 12:56:45 -07:00
|
|
|
js_getter_str);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hasGet = true;
|
|
|
|
get = v;
|
|
|
|
attrs |= JSPROP_GETTER | JSPROP_SHARED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.10.7 step 8 */
|
|
|
|
if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.setAtom), &v, &hasProperty))
|
|
|
|
return false;
|
|
|
|
if (hasProperty) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if ((v.isPrimitive() || !js_IsCallable(v)) && !v.isUndefined()) {
|
2010-04-04 21:59:09 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_GET_SET_FIELD,
|
2009-06-05 12:56:45 -07:00
|
|
|
js_setter_str);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hasSet = true;
|
|
|
|
set = v;
|
|
|
|
attrs |= JSPROP_SETTER | JSPROP_SHARED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.10.7 step 9 */
|
|
|
|
if ((hasGet || hasSet) && (hasValue || hasWritable)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_INVALID_DESCRIPTOR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Reject(JSContext *cx, uintN errorNumber, bool throwError, jsid id, bool *rval)
|
|
|
|
{
|
|
|
|
if (throwError) {
|
|
|
|
jsid idstr;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!js_ValueToStringId(cx, IdToValue(id), &idstr))
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, errorNumber,
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_GetStringBytes(JSID_TO_STRING(idstr)));
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rval = false;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Reject(JSContext *cx, uintN errorNumber, bool throwError, bool *rval)
|
|
|
|
{
|
|
|
|
if (throwError) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, errorNumber);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*rval = false;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Reject(JSContext *cx, JSObject *obj, JSProperty *prop, uintN errorNumber, bool throwError,
|
|
|
|
jsid id, bool *rval)
|
|
|
|
{
|
|
|
|
obj->dropProperty(cx, prop);
|
|
|
|
return Reject(cx, errorNumber, throwError, id, rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
DefinePropertyOnObject(JSContext *cx, JSObject *obj, const PropDesc &desc,
|
2010-04-30 16:03:37 -07:00
|
|
|
bool throwError, bool *rval)
|
2009-06-05 12:56:45 -07:00
|
|
|
{
|
|
|
|
/* 8.12.9 step 1. */
|
|
|
|
JSProperty *current;
|
|
|
|
JSObject *obj2;
|
2010-06-12 09:29:04 -07:00
|
|
|
JS_ASSERT(!obj->getOps()->lookupProperty);
|
|
|
|
if (!js_HasOwnProperty(cx, NULL, obj, desc.id, &obj2, ¤t))
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-06-12 09:29:04 -07:00
|
|
|
JS_ASSERT(!obj->getOps()->defineProperty);
|
2009-06-05 12:56:45 -07:00
|
|
|
|
|
|
|
/* 8.12.9 steps 2-4. */
|
|
|
|
if (!current) {
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->sealed())
|
2009-06-05 12:56:45 -07:00
|
|
|
return Reject(cx, JSMSG_OBJECT_NOT_EXTENSIBLE, throwError, rval);
|
|
|
|
|
|
|
|
*rval = true;
|
|
|
|
|
|
|
|
if (desc.isGenericDescriptor() || desc.isDataDescriptor()) {
|
2010-06-12 09:29:04 -07:00
|
|
|
JS_ASSERT(!obj->getOps()->defineProperty);
|
2010-07-14 23:19:36 -07:00
|
|
|
return js_DefineProperty(cx, obj, desc.id, &desc.value,
|
|
|
|
PropertyStub, PropertyStub, desc.attrs);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_ASSERT(desc.isAccessorDescriptor());
|
|
|
|
|
2010-08-13 06:21:13 -07:00
|
|
|
/*
|
|
|
|
* Getters and setters are just like watchpoints from an access
|
|
|
|
* control point of view.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
Value dummy;
|
2009-06-05 12:56:45 -07:00
|
|
|
uintN dummyAttrs;
|
2010-08-13 06:21:13 -07:00
|
|
|
if (!CheckAccess(cx, obj, desc.id, JSACC_WATCH, &dummy, &dummyAttrs))
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value tmp = UndefinedValue();
|
|
|
|
return js_DefineProperty(cx, obj, desc.id, &tmp,
|
2010-04-30 16:03:37 -07:00
|
|
|
desc.getter(), desc.setter(), desc.attrs);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.12.9 steps 5-6 (note 5 is merely a special case of 6). */
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v = UndefinedValue();
|
2009-06-05 12:56:45 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In the special case of shared permanent properties, the "own" property
|
|
|
|
* can be found on a different object. In that case the returned property
|
|
|
|
* might not be native, except: the shared permanent property optimization
|
|
|
|
* is not applied if the objects have different classes (bug 320854), as
|
2010-08-29 11:57:08 -07:00
|
|
|
* must be enforced by js_HasOwnProperty for the Shape cast below to be
|
|
|
|
* safe.
|
2009-06-05 12:56:45 -07:00
|
|
|
*/
|
|
|
|
JS_ASSERT(obj->getClass() == obj2->getClass());
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape = reinterpret_cast<Shape *>(current);
|
2009-06-05 12:56:45 -07:00
|
|
|
do {
|
|
|
|
if (desc.isAccessorDescriptor()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->isAccessorDescriptor())
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (desc.hasGet &&
|
2010-08-29 11:57:08 -07:00
|
|
|
!SameValue(desc.getterValue(), shape->getterOrUndefined(), cx)) {
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.hasSet &&
|
2010-08-29 11:57:08 -07:00
|
|
|
!SameValue(desc.setterValue(), shape->setterOrUndefined(), cx)) {
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Determine the current value of the property once, if the current
|
|
|
|
* value might actually need to be used or preserved later. NB: we
|
|
|
|
* guard on whether the current property is a data descriptor to
|
|
|
|
* avoid calling a getter; we won't need the value if it's not a
|
|
|
|
* data descriptor.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape->isDataDescriptor()) {
|
2009-06-05 12:56:45 -07:00
|
|
|
/*
|
|
|
|
* Non-standard: if the property is non-configurable and is
|
|
|
|
* represented by a native getter or setter, don't permit
|
|
|
|
* redefinition. We expose properties with native getter/setter
|
|
|
|
* as though they were data properties, for the most part, but
|
|
|
|
* in this particular case we must worry about integrity
|
|
|
|
* concerns for JSAPI users who expected that
|
|
|
|
* permanent+getter/setter means precisely controlled behavior.
|
|
|
|
* If we permitted such redefinitions, such a property could be
|
|
|
|
* "fixed" to some specific previous value, no longer varying
|
|
|
|
* according to the intent of the native getter/setter for the
|
|
|
|
* property.
|
|
|
|
*
|
|
|
|
* Other engines expose properties of this nature using ECMA
|
|
|
|
* getter/setter pairs, but we can't because we use them even
|
|
|
|
* for properties which ECMA specifies as being true data
|
|
|
|
* descriptors ([].length, Function.length, /regex/.lastIndex,
|
|
|
|
* &c.). Longer-term perhaps we should convert such properties
|
|
|
|
* to use data descriptors (at which point representing a
|
|
|
|
* descriptor with native getter/setter as an accessor
|
|
|
|
* descriptor would be fine) and take a small memory hit, but
|
|
|
|
* for now we'll simply forbid their redefinition.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->configurable() &&
|
|
|
|
(!shape->hasDefaultGetter() || !shape->hasDefaultSetter())) {
|
2009-06-05 12:56:45 -07:00
|
|
|
return Reject(cx, obj2, current, JSMSG_CANT_REDEFINE_UNCONFIGURABLE_PROP,
|
|
|
|
throwError, desc.id, rval);
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!js_NativeGet(cx, obj, obj2, shape, JSGET_NO_METHOD_BARRIER, &v)) {
|
2009-06-05 12:56:45 -07:00
|
|
|
/* current was dropped when the failure occurred. */
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.isDataDescriptor()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->isDataDescriptor())
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (desc.hasValue && !SameValue(desc.value, v, cx))
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
2010-08-29 11:57:08 -07:00
|
|
|
if (desc.hasWritable && desc.writable() != shape->writable())
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* The only fields in desc will be handled below. */
|
|
|
|
JS_ASSERT(desc.isGenericDescriptor());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (desc.hasConfigurable && desc.configurable() != shape->configurable())
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
2010-08-29 11:57:08 -07:00
|
|
|
if (desc.hasEnumerable && desc.enumerable() != shape->enumerable())
|
2009-06-05 12:56:45 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* The conditions imposed by step 5 or step 6 apply. */
|
|
|
|
obj2->dropProperty(cx, current);
|
|
|
|
*rval = true;
|
|
|
|
return JS_TRUE;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
/* 8.12.9 step 7. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->configurable()) {
|
2009-06-05 12:56:45 -07:00
|
|
|
/*
|
|
|
|
* Since [[Configurable]] defaults to false, we don't need to check
|
|
|
|
* whether it was specified. We can't do likewise for [[Enumerable]]
|
|
|
|
* because its putative value is used in a comparison -- a comparison
|
|
|
|
* whose result must always be false per spec if the [[Enumerable]]
|
|
|
|
* field is not present. Perfectly pellucid logic, eh?
|
|
|
|
*/
|
|
|
|
JS_ASSERT_IF(!desc.hasConfigurable, !desc.configurable());
|
|
|
|
if (desc.configurable() ||
|
2010-08-29 11:57:08 -07:00
|
|
|
(desc.hasEnumerable && desc.enumerable() != shape->enumerable())) {
|
2009-06-05 12:56:45 -07:00
|
|
|
return Reject(cx, obj2, current, JSMSG_CANT_REDEFINE_UNCONFIGURABLE_PROP, throwError,
|
|
|
|
desc.id, rval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.isGenericDescriptor()) {
|
|
|
|
/* 8.12.9 step 8, no validation required */
|
2010-08-29 11:57:08 -07:00
|
|
|
} else if (desc.isDataDescriptor() != shape->isDataDescriptor()) {
|
2009-06-05 12:56:45 -07:00
|
|
|
/* 8.12.9 step 9. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->configurable()) {
|
2009-06-05 12:56:45 -07:00
|
|
|
return Reject(cx, obj2, current, JSMSG_CANT_REDEFINE_UNCONFIGURABLE_PROP,
|
|
|
|
throwError, desc.id, rval);
|
|
|
|
}
|
2010-04-30 16:03:37 -07:00
|
|
|
} else if (desc.isDataDescriptor()) {
|
2009-06-05 12:56:45 -07:00
|
|
|
/* 8.12.9 step 10. */
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(shape->isDataDescriptor());
|
|
|
|
if (!shape->configurable() && !shape->writable()) {
|
2009-06-05 12:56:45 -07:00
|
|
|
if ((desc.hasWritable && desc.writable()) ||
|
2010-07-14 23:19:36 -07:00
|
|
|
(desc.hasValue && !SameValue(desc.value, v, cx))) {
|
2009-06-05 12:56:45 -07:00
|
|
|
return Reject(cx, obj2, current, JSMSG_CANT_REDEFINE_UNCONFIGURABLE_PROP,
|
|
|
|
throwError, desc.id, rval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* 8.12.9 step 11. */
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(desc.isAccessorDescriptor() && shape->isAccessorDescriptor());
|
|
|
|
if (!shape->configurable()) {
|
2009-06-05 12:56:45 -07:00
|
|
|
if ((desc.hasSet &&
|
2010-08-29 11:57:08 -07:00
|
|
|
!SameValue(desc.setterValue(), shape->setterOrUndefined(), cx)) ||
|
2009-06-05 12:56:45 -07:00
|
|
|
(desc.hasGet &&
|
2010-08-29 11:57:08 -07:00
|
|
|
!SameValue(desc.getterValue(), shape->getterOrUndefined(), cx))) {
|
2009-06-05 12:56:45 -07:00
|
|
|
return Reject(cx, obj2, current, JSMSG_CANT_REDEFINE_UNCONFIGURABLE_PROP,
|
|
|
|
throwError, desc.id, rval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 8.12.9 step 12. */
|
|
|
|
uintN attrs;
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyOp getter, setter;
|
2009-06-05 12:56:45 -07:00
|
|
|
if (desc.isGenericDescriptor()) {
|
|
|
|
uintN changed = 0;
|
|
|
|
if (desc.hasConfigurable)
|
|
|
|
changed |= JSPROP_PERMANENT;
|
|
|
|
if (desc.hasEnumerable)
|
|
|
|
changed |= JSPROP_ENUMERATE;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
attrs = (shape->attributes() & ~changed) | (desc.attrs & changed);
|
|
|
|
if (shape->isMethod()) {
|
2010-05-24 10:48:32 -07:00
|
|
|
JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
|
2010-07-14 23:19:36 -07:00
|
|
|
getter = setter = PropertyStub;
|
2010-05-24 10:48:32 -07:00
|
|
|
} else {
|
2010-08-29 11:57:08 -07:00
|
|
|
getter = shape->getter();
|
|
|
|
setter = shape->setter();
|
2010-05-24 10:48:32 -07:00
|
|
|
}
|
2009-06-05 12:56:45 -07:00
|
|
|
} else if (desc.isDataDescriptor()) {
|
|
|
|
uintN unchanged = 0;
|
|
|
|
if (!desc.hasConfigurable)
|
|
|
|
unchanged |= JSPROP_PERMANENT;
|
|
|
|
if (!desc.hasEnumerable)
|
|
|
|
unchanged |= JSPROP_ENUMERATE;
|
|
|
|
if (!desc.hasWritable)
|
|
|
|
unchanged |= JSPROP_READONLY;
|
|
|
|
|
|
|
|
if (desc.hasValue)
|
|
|
|
v = desc.value;
|
2010-08-29 11:57:08 -07:00
|
|
|
attrs = (desc.attrs & ~unchanged) | (shape->attributes() & unchanged);
|
2010-07-14 23:19:36 -07:00
|
|
|
getter = setter = PropertyStub;
|
2009-06-05 12:56:45 -07:00
|
|
|
} else {
|
|
|
|
JS_ASSERT(desc.isAccessorDescriptor());
|
|
|
|
|
2010-08-13 06:21:13 -07:00
|
|
|
/*
|
|
|
|
* Getters and setters are just like watchpoints from an access
|
|
|
|
* control point of view.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
Value dummy;
|
2010-06-17 05:32:26 -07:00
|
|
|
if (!CheckAccess(cx, obj2, desc.id, JSACC_WATCH, &dummy, &attrs)) {
|
2010-08-13 06:21:13 -07:00
|
|
|
obj2->dropProperty(cx, current);
|
|
|
|
return JS_FALSE;
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT_IF(shape->isMethod(), !(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
|
2010-07-19 10:39:57 -07:00
|
|
|
|
2009-06-05 12:56:45 -07:00
|
|
|
/* 8.12.9 step 12. */
|
|
|
|
uintN changed = 0;
|
|
|
|
if (desc.hasConfigurable)
|
|
|
|
changed |= JSPROP_PERMANENT;
|
|
|
|
if (desc.hasEnumerable)
|
|
|
|
changed |= JSPROP_ENUMERATE;
|
|
|
|
if (desc.hasGet)
|
|
|
|
changed |= JSPROP_GETTER | JSPROP_SHARED;
|
|
|
|
if (desc.hasSet)
|
|
|
|
changed |= JSPROP_SETTER | JSPROP_SHARED;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
attrs = (desc.attrs & changed) | (shape->attributes() & ~changed);
|
2010-04-30 16:03:37 -07:00
|
|
|
if (desc.hasGet) {
|
|
|
|
getter = desc.getter();
|
|
|
|
} else {
|
2010-08-29 11:57:08 -07:00
|
|
|
getter = (shape->isMethod() || (shape->hasDefaultGetter() && !shape->hasGetterValue()))
|
2010-07-14 23:19:36 -07:00
|
|
|
? PropertyStub
|
2010-08-29 11:57:08 -07:00
|
|
|
: shape->getter();
|
2010-04-30 16:03:37 -07:00
|
|
|
}
|
|
|
|
if (desc.hasSet) {
|
|
|
|
setter = desc.setter();
|
|
|
|
} else {
|
2010-08-29 11:57:08 -07:00
|
|
|
setter = (shape->hasDefaultSetter() && !shape->hasSetterValue())
|
2010-07-14 23:19:36 -07:00
|
|
|
? PropertyStub
|
2010-08-29 11:57:08 -07:00
|
|
|
: shape->setter();
|
2010-04-30 16:03:37 -07:00
|
|
|
}
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*rval = true;
|
|
|
|
obj2->dropProperty(cx, current);
|
2010-07-14 23:19:36 -07:00
|
|
|
return js_DefineProperty(cx, obj, desc.id, &v, getter, setter, attrs);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
DefinePropertyOnArray(JSContext *cx, JSObject *obj, const PropDesc &desc,
|
2010-04-30 16:03:37 -07:00
|
|
|
bool throwError, bool *rval)
|
2009-06-05 12:56:45 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We probably should optimize dense array property definitions where
|
|
|
|
* the descriptor describes a traditional array property (enumerable,
|
|
|
|
* configurable, writable, numeric index or length without altering its
|
|
|
|
* attributes). Such definitions are probably unlikely, so we don't bother
|
|
|
|
* for now.
|
|
|
|
*/
|
2010-06-02 23:44:24 -07:00
|
|
|
if (obj->isDenseArray() && !obj->makeDenseArraySlow(cx))
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-04-05 18:32:16 -07:00
|
|
|
jsuint oldLen = obj->getArrayLength();
|
2009-06-05 12:56:45 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_ATOM(desc.id, cx->runtime->atomState.lengthAtom)) {
|
2009-06-05 12:56:45 -07:00
|
|
|
/*
|
|
|
|
* Our optimization of storage of the length property of arrays makes
|
|
|
|
* it very difficult to properly implement defining the property. For
|
|
|
|
* now simply throw an exception (NB: not merely Reject) on any attempt
|
|
|
|
* to define the "length" property, rather than attempting to implement
|
|
|
|
* some difficult-for-authors-to-grasp subset of that functionality.
|
|
|
|
*/
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_DEFINE_ARRAY_LENGTH_UNSUPPORTED);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 index;
|
|
|
|
if (js_IdIsIndex(desc.id, &index)) {
|
|
|
|
/*
|
|
|
|
// Disabled until we support defining "length":
|
|
|
|
if (index >= oldLen && lengthPropertyNotWritable())
|
|
|
|
return ThrowTypeError(cx, JSMSG_CANT_APPEND_PROPERTIES_TO_UNWRITABLE_LENGTH_ARRAY);
|
|
|
|
*/
|
2010-04-30 16:03:37 -07:00
|
|
|
if (!DefinePropertyOnObject(cx, obj, desc, false, rval))
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
if (!*rval)
|
|
|
|
return Reject(cx, JSMSG_CANT_DEFINE_ARRAY_INDEX, throwError, rval);
|
|
|
|
|
|
|
|
if (index >= oldLen) {
|
|
|
|
JS_ASSERT(index != UINT32_MAX);
|
2010-07-22 18:45:21 -07:00
|
|
|
obj->setArrayLength(index + 1);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*rval = true;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-04-30 16:03:37 -07:00
|
|
|
return DefinePropertyOnObject(cx, obj, desc, throwError, rval);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
DefineProperty(JSContext *cx, JSObject *obj, const PropDesc &desc, bool throwError,
|
2009-06-05 12:56:45 -07:00
|
|
|
bool *rval)
|
|
|
|
{
|
2010-03-04 20:44:09 -08:00
|
|
|
if (obj->isArray())
|
2010-04-30 16:03:37 -07:00
|
|
|
return DefinePropertyOnArray(cx, obj, desc, throwError, rval);
|
2009-06-05 12:56:45 -07:00
|
|
|
|
2010-06-12 09:29:04 -07:00
|
|
|
if (obj->getOps()->lookupProperty) {
|
2010-05-18 19:21:43 -07:00
|
|
|
if (obj->isProxy())
|
|
|
|
return JSProxy::defineProperty(cx, obj, desc.id, desc.pd);
|
2009-06-05 12:56:45 -07:00
|
|
|
return Reject(cx, JSMSG_OBJECT_NOT_EXTENSIBLE, throwError, rval);
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
2009-06-05 12:56:45 -07:00
|
|
|
|
2010-04-30 16:03:37 -07:00
|
|
|
return DefinePropertyOnObject(cx, obj, desc, throwError, rval);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 12:36:42 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_DefineOwnProperty(JSContext *cx, JSObject *obj, jsid id,
|
|
|
|
const Value &descriptor, JSBool *bp)
|
2010-03-24 12:36:42 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoPropDescArrayRooter descs(cx);
|
|
|
|
PropDesc *desc = descs.append();
|
2010-03-24 12:36:42 -07:00
|
|
|
if (!desc || !desc->initialize(cx, id, descriptor))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool rval;
|
|
|
|
if (!DefineProperty(cx, obj, *desc, true, &rval))
|
|
|
|
return false;
|
|
|
|
*bp = !!rval;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-05 12:56:45 -07:00
|
|
|
/* ES5 15.2.3.6: Object.defineProperty(O, P, Attributes) */
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_defineProperty(JSContext* cx, uintN argc, Value* vp)
|
2009-06-05 12:56:45 -07:00
|
|
|
{
|
|
|
|
/* 15.2.3.6 steps 1 and 5. */
|
2009-09-24 14:33:14 -07:00
|
|
|
JSObject *obj;
|
|
|
|
if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.defineProperty", &obj))
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*obj);
|
2009-06-05 12:56:45 -07:00
|
|
|
|
|
|
|
/* 15.2.3.6 step 2. */
|
2010-01-11 09:52:21 -08:00
|
|
|
AutoIdRooter nameidr(cx);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!ValueToId(cx, argc >= 2 ? vp[3] : UndefinedValue(), nameidr.addr()))
|
2009-06-05 12:56:45 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
/* 15.2.3.6 step 3. */
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &descval = argc >= 3 ? vp[4] : UndefinedValue();
|
2009-06-05 12:56:45 -07:00
|
|
|
|
|
|
|
/* 15.2.3.6 step 4 */
|
2010-03-24 12:36:42 -07:00
|
|
|
JSBool junk;
|
|
|
|
return js_DefineOwnProperty(cx, obj, nameidr.id(), descval, &junk);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
static bool
|
|
|
|
DefineProperties(JSContext *cx, JSObject *obj, JSObject *props)
|
|
|
|
{
|
|
|
|
AutoIdArray ida(cx, JS_Enumerate(cx, props));
|
|
|
|
if (!ida)
|
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoPropDescArrayRooter descs(cx);
|
2010-05-18 19:21:43 -07:00
|
|
|
size_t len = ida.length();
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
jsid id = ida[i];
|
2010-07-14 23:19:36 -07:00
|
|
|
PropDesc* desc = descs.append();
|
2010-05-18 19:21:43 -07:00
|
|
|
AutoValueRooter tvr(cx);
|
|
|
|
if (!desc ||
|
2010-07-14 23:19:36 -07:00
|
|
|
!JS_GetPropertyById(cx, props, id, tvr.jsval_addr()) ||
|
2010-05-18 19:21:43 -07:00
|
|
|
!desc->initialize(cx, id, tvr.value())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
if (!DefineProperty(cx, obj, descs[i], true, &dummy))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern JSBool
|
|
|
|
js_PopulateObject(JSContext *cx, JSObject *newborn, JSObject *props)
|
|
|
|
{
|
|
|
|
return DefineProperties(cx, newborn, props);
|
|
|
|
}
|
|
|
|
|
2009-06-05 12:56:45 -07:00
|
|
|
/* ES5 15.2.3.7: Object.defineProperties(O, Properties) */
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_defineProperties(JSContext* cx, uintN argc, Value* vp)
|
2009-06-05 12:56:45 -07:00
|
|
|
{
|
|
|
|
/* 15.2.3.6 steps 1 and 5. */
|
2009-09-24 14:33:14 -07:00
|
|
|
JSObject *obj;
|
|
|
|
if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.defineProperties", &obj))
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*obj);
|
2009-09-24 14:33:14 -07:00
|
|
|
|
2010-01-04 11:48:30 -08:00
|
|
|
if (argc < 2) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
|
|
|
|
"Object.defineProperties", "0", "s");
|
2010-05-18 19:21:43 -07:00
|
|
|
return false;
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
2010-01-04 11:48:30 -08:00
|
|
|
JSObject* props = js_ValueToNonNullObject(cx, vp[3]);
|
|
|
|
if (!props)
|
2010-05-18 19:21:43 -07:00
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp[3].setObject(*props);
|
2010-01-04 11:48:30 -08:00
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
return DefineProperties(cx, obj, props);
|
2009-06-05 12:56:45 -07:00
|
|
|
}
|
|
|
|
|
2009-09-08 15:59:14 -07:00
|
|
|
/* ES5 15.2.3.5: Object.create(O [, Properties]) */
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_create(JSContext *cx, uintN argc, Value *vp)
|
2009-09-08 15:59:14 -07:00
|
|
|
{
|
|
|
|
if (argc == 0) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
|
|
|
|
"Object.create", "0", "s");
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &v = vp[2];
|
|
|
|
if (!v.isObjectOrNull()) {
|
|
|
|
char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, NULL);
|
2009-09-08 15:59:14 -07:00
|
|
|
if (!bytes)
|
|
|
|
return JS_FALSE;
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_UNEXPECTED_TYPE,
|
|
|
|
bytes, "not an object or null");
|
|
|
|
JS_free(cx, bytes);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-06-18 17:43:02 -07:00
|
|
|
* Use the callee's global as the parent of the new object to avoid dynamic
|
|
|
|
* scoping (i.e., using the caller's global).
|
2009-09-08 15:59:14 -07:00
|
|
|
*/
|
2010-08-09 09:11:22 -07:00
|
|
|
JSObject *obj = NewNonFunction<WithProto::Given>(cx, &js_ObjectClass, v.toObjectOrNull(),
|
|
|
|
vp->toObject().getGlobal());
|
2009-09-08 15:59:14 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*obj); /* Root and prepare for eventual return. */
|
2009-09-08 15:59:14 -07:00
|
|
|
|
|
|
|
/* 15.2.3.5 step 4. */
|
2010-07-14 23:19:36 -07:00
|
|
|
if (argc > 1 && !vp[3].isUndefined()) {
|
|
|
|
if (vp[3].isPrimitive()) {
|
2010-01-04 11:48:30 -08:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NOT_NONNULL_OBJECT);
|
2009-09-08 15:59:14 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *props = &vp[3].toObject();
|
2010-03-28 20:32:20 -07:00
|
|
|
AutoIdArray ida(cx, JS_Enumerate(cx, props));
|
2009-09-08 15:59:14 -07:00
|
|
|
if (!ida)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoPropDescArrayRooter descs(cx);
|
2009-09-08 15:59:14 -07:00
|
|
|
size_t len = ida.length();
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
jsid id = ida[i];
|
2010-07-14 23:19:36 -07:00
|
|
|
PropDesc *desc = descs.append();
|
|
|
|
if (!desc || !JS_GetPropertyById(cx, props, id, Jsvalify(&vp[1])) ||
|
2009-12-30 13:20:21 -08:00
|
|
|
!desc->initialize(cx, id, vp[1])) {
|
2009-09-08 15:59:14 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
if (!DefineProperty(cx, obj, descs[i], true, &dummy))
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 5. Return obj. */
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
obj_getOwnPropertyNames(JSContext *cx, uintN argc, Value *vp)
|
2009-09-24 14:33:14 -07:00
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.getOwnPropertyNames", &obj))
|
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoIdVector keys(cx);
|
|
|
|
if (!GetPropertyNames(cx, obj, JSITER_OWNONLY | JSITER_HIDDEN, keys))
|
2009-09-24 14:33:14 -07:00
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoValueVector vals(cx);
|
|
|
|
if (!vals.resize(keys.length()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0, len = keys.length(); i < len; i++) {
|
|
|
|
jsid id = keys[i];
|
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
JSString *str = js_ValueToString(cx, Int32Value(JSID_TO_INT(id)));
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!str)
|
|
|
|
return false;
|
2010-07-14 23:19:36 -07:00
|
|
|
vals[i].setString(str);
|
|
|
|
} else if (JSID_IS_ATOM(id)) {
|
|
|
|
vals[i].setString(JSID_TO_STRING(id));
|
|
|
|
} else {
|
|
|
|
vals[i].setObject(*JSID_TO_OBJECT(id));
|
2009-09-24 14:33:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *aobj = js_NewArrayObject(cx, vals.length(), vals.begin());
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!aobj)
|
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*aobj);
|
2009-09-24 14:33:14 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-21 19:35:59 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_OBJ_WATCHPOINT
|
|
|
|
const char js_watch_str[] = "watch";
|
|
|
|
const char js_unwatch_str[] = "unwatch";
|
|
|
|
#endif
|
|
|
|
const char js_hasOwnProperty_str[] = "hasOwnProperty";
|
|
|
|
const char js_isPrototypeOf_str[] = "isPrototypeOf";
|
|
|
|
const char js_propertyIsEnumerable_str[] = "propertyIsEnumerable";
|
|
|
|
|
|
|
|
static JSFunctionSpec object_methods[] = {
|
|
|
|
#if JS_HAS_TOSOURCE
|
2008-08-11 17:47:05 -07:00
|
|
|
JS_FN(js_toSource_str, obj_toSource, 0,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2008-08-11 17:47:05 -07:00
|
|
|
JS_FN(js_toString_str, obj_toString, 0,0),
|
|
|
|
JS_FN(js_toLocaleString_str, obj_toLocaleString, 0,0),
|
2010-04-30 14:40:25 -07:00
|
|
|
JS_FN(js_valueOf_str, obj_valueOf, 0,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_OBJ_WATCHPOINT
|
2008-08-11 17:47:05 -07:00
|
|
|
JS_FN(js_watch_str, obj_watch, 2,0),
|
|
|
|
JS_FN(js_unwatch_str, obj_unwatch, 1,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2010-04-30 14:40:25 -07:00
|
|
|
JS_FN(js_hasOwnProperty_str, obj_hasOwnProperty, 1,0),
|
2008-08-11 17:47:05 -07:00
|
|
|
JS_FN(js_isPrototypeOf_str, obj_isPrototypeOf, 1,0),
|
2010-04-30 14:40:25 -07:00
|
|
|
JS_FN(js_propertyIsEnumerable_str, obj_propertyIsEnumerable, 1,0),
|
2009-10-06 12:35:33 -07:00
|
|
|
#if OLD_GETTER_SETTER_METHODS
|
2009-01-23 15:44:01 -08:00
|
|
|
JS_FN(js_defineGetter_str, js_obj_defineGetter, 2,0),
|
|
|
|
JS_FN(js_defineSetter_str, js_obj_defineSetter, 2,0),
|
2008-08-11 17:47:05 -07:00
|
|
|
JS_FN(js_lookupGetter_str, obj_lookupGetter, 1,0),
|
|
|
|
JS_FN(js_lookupSetter_str, obj_lookupSetter, 1,0),
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2008-08-04 16:43:12 -07:00
|
|
|
static JSFunctionSpec object_static_methods[] = {
|
2008-10-08 15:08:33 -07:00
|
|
|
JS_FN("getPrototypeOf", obj_getPrototypeOf, 1,0),
|
2009-07-21 19:35:59 -07:00
|
|
|
JS_FN("getOwnPropertyDescriptor", obj_getOwnPropertyDescriptor,2,0),
|
2009-09-08 15:59:14 -07:00
|
|
|
JS_FN("keys", obj_keys, 1,0),
|
2009-06-05 12:56:45 -07:00
|
|
|
JS_FN("defineProperty", obj_defineProperty, 3,0),
|
|
|
|
JS_FN("defineProperties", obj_defineProperties, 2,0),
|
2009-09-08 15:59:14 -07:00
|
|
|
JS_FN("create", obj_create, 2,0),
|
2009-09-24 14:33:14 -07:00
|
|
|
JS_FN("getOwnPropertyNames", obj_getOwnPropertyNames, 1,0),
|
2008-08-04 16:43:12 -07:00
|
|
|
JS_FS_END
|
|
|
|
};
|
|
|
|
|
2009-07-14 17:06:09 -07:00
|
|
|
JSBool
|
2010-08-03 12:20:48 -07:00
|
|
|
js_Object(JSContext *cx, uintN argc, Value *vp)
|
2009-07-14 17:06:09 -07:00
|
|
|
{
|
2010-08-03 12:20:48 -07:00
|
|
|
JSObject *obj;
|
2009-07-14 17:06:09 -07:00
|
|
|
if (argc == 0) {
|
|
|
|
/* Trigger logic below to construct a blank object. */
|
|
|
|
obj = NULL;
|
|
|
|
} else {
|
|
|
|
/* If argv[0] is null or undefined, obj comes back null. */
|
2010-08-03 12:20:48 -07:00
|
|
|
if (!js_ValueToObjectOrNull(cx, vp[2], &obj))
|
2009-07-14 17:06:09 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
if (!obj) {
|
2010-08-03 12:20:48 -07:00
|
|
|
/* Make an object whether this was called with 'new' or not. */
|
|
|
|
JS_ASSERT(!argc || vp[2].isNull() || vp[2].isUndefined());
|
2010-06-18 17:43:02 -07:00
|
|
|
obj = NewBuiltinClassInstance(cx, &js_ObjectClass);
|
2009-07-14 17:06:09 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2010-08-03 12:20:48 -07:00
|
|
|
vp->setObject(*obj);
|
2009-07-14 17:06:09 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-08-16 12:35:04 -07:00
|
|
|
JSObject*
|
|
|
|
js_NewInstance(JSContext *cx, JSObject *callee)
|
|
|
|
{
|
|
|
|
Class *clasp = callee->getClass();
|
|
|
|
|
|
|
|
Class *newclasp = &js_ObjectClass;
|
|
|
|
if (clasp == &js_FunctionClass) {
|
|
|
|
JSFunction *fun = callee->getFunctionPrivate();
|
|
|
|
if (fun->isNative() && fun->u.n.clasp)
|
|
|
|
newclasp = fun->u.n.clasp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value protov;
|
|
|
|
if (!callee->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom), &protov))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
JSObject *proto = protov.isObjectOrNull() ? protov.toObjectOrNull() : NULL;
|
|
|
|
JSObject *parent = callee->getParent();
|
|
|
|
return NewObject<WithProto::Class>(cx, newclasp, proto, parent);
|
|
|
|
}
|
|
|
|
|
2009-08-29 18:46:27 -07:00
|
|
|
#ifdef JS_TRACER
|
|
|
|
|
2010-09-10 17:06:30 -07:00
|
|
|
static JS_ALWAYS_INLINE JSObject*
|
|
|
|
NewObjectWithClassProto(JSContext *cx, Class *clasp, JSObject *proto,
|
|
|
|
const Value &privateSlotValue)
|
2009-08-29 18:46:27 -07:00
|
|
|
{
|
2010-06-12 09:29:04 -07:00
|
|
|
JS_ASSERT(clasp->isNative());
|
2009-11-18 12:29:58 -08:00
|
|
|
|
2009-10-02 01:21:19 -07:00
|
|
|
JSObject* obj = js_NewGCObject(cx);
|
2009-08-29 18:46:27 -07:00
|
|
|
if (!obj)
|
|
|
|
return NULL;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
obj->initSharingEmptyShape(clasp, proto, proto->getParent(), privateSlotValue, cx);
|
2009-11-18 12:29:58 -08:00
|
|
|
return obj;
|
2009-08-29 18:46:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* FASTCALL
|
|
|
|
js_Object_tn(JSContext* cx, JSObject* proto)
|
|
|
|
{
|
2009-09-05 08:59:11 -07:00
|
|
|
JS_ASSERT(!(js_ObjectClass.flags & JSCLASS_HAS_PRIVATE));
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2010-09-10 17:06:30 -07:00
|
|
|
return NewObjectWithClassProto(cx, &js_ObjectClass, proto, UndefinedValue());
|
2009-08-29 18:46:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_DEFINE_TRCINFO_1(js_Object,
|
2010-03-01 18:47:17 -08:00
|
|
|
(2, (extern, CONSTRUCTOR_RETRY, js_Object_tn, CONTEXT, CALLEE_PROTOTYPE, 0,
|
2010-07-27 22:33:42 -07:00
|
|
|
nanojit::ACCSET_STORE_ANY)))
|
2009-08-29 18:46:27 -07:00
|
|
|
|
2010-01-07 11:47:16 -08:00
|
|
|
JSObject* FASTCALL
|
|
|
|
js_NonEmptyObject(JSContext* cx, JSObject* proto)
|
|
|
|
{
|
|
|
|
JS_ASSERT(!(js_ObjectClass.flags & JSCLASS_HAS_PRIVATE));
|
2010-02-04 23:41:54 -08:00
|
|
|
|
2010-09-10 17:06:30 -07:00
|
|
|
JSObject *obj = NewObjectWithClassProto(cx, &js_ObjectClass, proto, UndefinedValue());
|
2010-08-29 11:57:08 -07:00
|
|
|
return (obj && obj->ensureClassReservedSlotsForEmptyObject(cx)) ? obj : NULL;
|
2010-01-07 11:47:16 -08:00
|
|
|
}
|
|
|
|
|
2010-03-01 18:47:17 -08:00
|
|
|
JS_DEFINE_CALLINFO_2(extern, CONSTRUCTOR_RETRY, js_NonEmptyObject, CONTEXT, CALLEE_PROTOTYPE, 0,
|
2010-07-27 22:33:42 -07:00
|
|
|
nanojit::ACCSET_STORE_ANY)
|
2010-01-07 11:47:16 -08:00
|
|
|
|
2010-09-10 17:06:30 -07:00
|
|
|
JSObject* FASTCALL
|
|
|
|
js_String_tn(JSContext* cx, JSObject* proto, JSString* str)
|
|
|
|
{
|
|
|
|
JS_ASSERT(JS_ON_TRACE(cx));
|
|
|
|
return NewObjectWithClassProto(cx, &js_StringClass, proto, StringValue(str));
|
|
|
|
}
|
|
|
|
JS_DEFINE_CALLINFO_3(extern, OBJECT, js_String_tn, CONTEXT, CALLEE_PROTOTYPE, STRING, 0,
|
|
|
|
nanojit::ACCSET_STORE_ANY)
|
|
|
|
|
2009-08-29 18:46:27 -07:00
|
|
|
JSObject* FASTCALL
|
2010-08-16 12:35:04 -07:00
|
|
|
js_NewInstanceFromTrace(JSContext *cx, Class *clasp, JSObject *ctor)
|
2009-08-29 18:46:27 -07:00
|
|
|
{
|
2010-06-18 17:43:02 -07:00
|
|
|
JS_ASSERT(JS_ON_TRACE(cx));
|
2010-03-31 22:13:51 -07:00
|
|
|
JS_ASSERT(ctor->isFunction());
|
2009-08-29 18:46:27 -07:00
|
|
|
#ifdef JS_THREADSAFE
|
2010-08-29 11:57:08 -07:00
|
|
|
if (ctor->title.ownercx != cx)
|
2009-08-29 18:46:27 -07:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!ctor->ensureClassReservedSlots(cx))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
jsid classPrototypeId = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
|
|
|
|
const Shape *shape = ctor->nativeLookup(classPrototypeId);
|
|
|
|
Value pval = shape ? ctor->getSlot(shape->slot) : MagicValue(JS_GENERIC_MAGIC);
|
2009-08-29 18:46:27 -07:00
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
JSObject *parent = ctor->getParent();
|
2009-08-29 18:46:27 -07:00
|
|
|
JSObject *proto;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (pval.isObject()) {
|
2009-08-29 18:46:27 -07:00
|
|
|
/* An object in ctor.prototype, let's use it as the new instance's proto. */
|
2010-07-14 23:19:36 -07:00
|
|
|
proto = &pval.toObject();
|
2009-08-29 18:46:27 -07:00
|
|
|
} else {
|
2010-06-18 17:43:02 -07:00
|
|
|
/* A hole or a primitive: either way, we need to get Object.prototype. */
|
|
|
|
if (!js_GetClassPrototype(cx, parent, JSProto_Object, &proto))
|
2009-08-29 18:46:27 -07:00
|
|
|
return NULL;
|
2010-06-18 17:43:02 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (pval.isMagic(JS_GENERIC_MAGIC)) {
|
2010-06-18 17:43:02 -07:00
|
|
|
/*
|
|
|
|
* No ctor.prototype was set, so we inline-expand and optimize
|
|
|
|
* fun_resolve's prototype creation code.
|
|
|
|
*/
|
|
|
|
proto = NewNativeClassInstance(cx, clasp, proto, parent);
|
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
if (!js_SetClassPrototype(cx, ctor, proto, JSPROP_ENUMERATE | JSPROP_PERMANENT))
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* A primitive value in .prototype means to use Object.prototype
|
|
|
|
* for proto. See ES5 13.2.2 step 7.
|
|
|
|
*/
|
2009-08-29 18:46:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-24 10:10:04 -07:00
|
|
|
/*
|
|
|
|
* FIXME: 561785 at least. Quasi-natives including XML objects prevent us
|
|
|
|
* from easily or unconditionally calling NewNativeClassInstance here.
|
|
|
|
*/
|
2010-08-09 09:11:22 -07:00
|
|
|
return NewNonFunction<WithProto::Given>(cx, clasp, proto, parent);
|
2009-08-29 18:46:27 -07:00
|
|
|
}
|
|
|
|
|
2010-08-16 12:35:04 -07:00
|
|
|
JS_DEFINE_CALLINFO_3(extern, CONSTRUCTOR_RETRY, js_NewInstanceFromTrace, CONTEXT, CLASS, OBJECT, 0,
|
2010-07-27 22:33:42 -07:00
|
|
|
nanojit::ACCSET_STORE_ANY)
|
2009-08-29 18:46:27 -07:00
|
|
|
|
|
|
|
#else /* !JS_TRACER */
|
|
|
|
|
|
|
|
# define js_Object_trcinfo NULL
|
|
|
|
|
|
|
|
#endif /* !JS_TRACER */
|
|
|
|
|
2009-01-02 13:11:46 -08:00
|
|
|
/*
|
|
|
|
* Given pc pointing after a property accessing bytecode, return true if the
|
|
|
|
* access is "object-detecting" in the sense used by web scripts, e.g., when
|
|
|
|
* checking whether document.all is defined.
|
|
|
|
*/
|
2009-07-05 21:42:13 -07:00
|
|
|
JS_REQUIRES_STACK JSBool
|
2009-01-02 13:11:46 -08:00
|
|
|
Detecting(JSContext *cx, jsbytecode *pc)
|
|
|
|
{
|
|
|
|
JSScript *script;
|
2009-02-20 16:22:51 -08:00
|
|
|
jsbytecode *endpc;
|
2009-01-02 13:11:46 -08:00
|
|
|
JSOp op;
|
|
|
|
JSAtom *atom;
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
script = cx->fp()->script();
|
2009-02-20 16:22:51 -08:00
|
|
|
endpc = script->code + script->length;
|
2009-02-18 16:31:09 -08:00
|
|
|
for (;; pc += js_CodeSpec[op].length) {
|
2010-08-09 22:43:33 -07:00
|
|
|
JS_ASSERT_IF(!cx->fp()->hasImacropc(), script->code <= pc && pc < endpc);
|
2009-02-20 16:22:51 -08:00
|
|
|
|
2009-01-02 13:11:46 -08:00
|
|
|
/* General case: a branch or equality op follows the access. */
|
2009-02-18 16:31:09 -08:00
|
|
|
op = js_GetOpcode(cx, script, pc);
|
2009-01-02 13:11:46 -08:00
|
|
|
if (js_CodeSpec[op].format & JOF_DETECTING)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case JSOP_NULL:
|
|
|
|
/*
|
|
|
|
* Special case #1: handle (document.all == null). Don't sweat
|
|
|
|
* about JS1.2's revision of the equality operators here.
|
|
|
|
*/
|
2009-02-20 16:22:51 -08:00
|
|
|
if (++pc < endpc) {
|
|
|
|
op = js_GetOpcode(cx, script, pc);
|
|
|
|
return *pc == JSOP_EQ || *pc == JSOP_NE;
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
2009-01-02 13:11:46 -08:00
|
|
|
|
2010-06-12 19:00:27 -07:00
|
|
|
case JSOP_GETGNAME:
|
2009-01-02 13:11:46 -08:00
|
|
|
case JSOP_NAME:
|
|
|
|
/*
|
|
|
|
* Special case #2: handle (document.all == undefined). Don't
|
|
|
|
* worry about someone redefining undefined, which was added by
|
|
|
|
* Edition 3, so is read/write for backward compatibility.
|
|
|
|
*/
|
|
|
|
GET_ATOM_FROM_BYTECODE(script, pc, 0, atom);
|
2009-02-20 16:22:51 -08:00
|
|
|
if (atom == cx->runtime->atomState.typeAtoms[JSTYPE_VOID] &&
|
|
|
|
(pc += js_CodeSpec[op].length) < endpc) {
|
2009-02-18 16:31:09 -08:00
|
|
|
op = js_GetOpcode(cx, script, pc);
|
2009-01-02 13:11:46 -08:00
|
|
|
return op == JSOP_EQ || op == JSOP_NE ||
|
|
|
|
op == JSOP_STRICTEQ || op == JSOP_STRICTNE;
|
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/*
|
|
|
|
* At this point, anything but an extended atom index prefix means
|
|
|
|
* we're not detecting.
|
|
|
|
*/
|
|
|
|
if (!(js_CodeSpec[op].format & JOF_INDEXBASE))
|
|
|
|
return JS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Infer lookup flags from the currently executing bytecode. This does
|
|
|
|
* not attempt to infer JSRESOLVE_WITH, because the current bytecode
|
|
|
|
* does not indicate whether we are in a with statement. Return defaultFlags
|
|
|
|
* if a currently executing bytecode cannot be determined.
|
|
|
|
*/
|
2009-07-05 21:42:13 -07:00
|
|
|
uintN
|
|
|
|
js_InferFlags(JSContext *cx, uintN defaultFlags)
|
2009-01-02 13:11:46 -08:00
|
|
|
{
|
2009-07-05 22:01:21 -07:00
|
|
|
#ifdef JS_TRACER
|
2009-07-05 21:42:13 -07:00
|
|
|
if (JS_ON_TRACE(cx))
|
|
|
|
return cx->bailExit->lookupFlags;
|
2009-07-05 22:01:21 -07:00
|
|
|
#endif
|
2009-07-05 21:42:13 -07:00
|
|
|
|
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
|
|
|
|
2009-01-02 13:11:46 -08:00
|
|
|
jsbytecode *pc;
|
|
|
|
const JSCodeSpec *cs;
|
|
|
|
uint32 format;
|
|
|
|
uintN flags = 0;
|
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
JSStackFrame *const fp = js_GetTopStackFrame(cx);
|
|
|
|
if (!fp || !(pc = cx->regs->pc))
|
2009-01-02 13:11:46 -08:00
|
|
|
return defaultFlags;
|
2010-08-09 22:43:33 -07:00
|
|
|
cs = &js_CodeSpec[js_GetOpcode(cx, fp->script(), pc)];
|
2009-01-02 13:11:46 -08:00
|
|
|
format = cs->format;
|
|
|
|
if (JOF_MODE(format) != JOF_NAME)
|
|
|
|
flags |= JSRESOLVE_QUALIFIED;
|
2010-08-09 22:43:33 -07:00
|
|
|
if ((format & (JOF_SET | JOF_FOR)) || fp->isAssigning()) {
|
2009-01-02 13:11:46 -08:00
|
|
|
flags |= JSRESOLVE_ASSIGNING;
|
2009-09-04 17:47:23 -07:00
|
|
|
} else if (cs->length >= 0) {
|
2009-01-02 13:11:46 -08:00
|
|
|
pc += cs->length;
|
2010-08-09 22:43:33 -07:00
|
|
|
JSScript *script = cx->fp()->script();
|
2010-08-22 16:00:20 -07:00
|
|
|
if (pc < script->code + script->length && Detecting(cx, pc))
|
2009-01-02 13:11:46 -08:00
|
|
|
flags |= JSRESOLVE_DETECTING;
|
|
|
|
}
|
|
|
|
if (format & JOF_DECLARING)
|
|
|
|
flags |= JSRESOLVE_DECLARING;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* ObjectOps and Class for with-statement stack objects.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
with_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
|
|
|
|
JSProperty **propp)
|
|
|
|
{
|
2009-01-02 13:11:46 -08:00
|
|
|
/* Fixes bug 463997 */
|
|
|
|
uintN flags = cx->resolveFlags;
|
|
|
|
if (flags == JSRESOLVE_INFER)
|
2009-07-05 21:42:13 -07:00
|
|
|
flags = js_InferFlags(cx, flags);
|
2009-01-02 13:11:46 -08:00
|
|
|
flags |= JSRESOLVE_WITH;
|
|
|
|
JSAutoResolveFlags rf(cx, flags);
|
2010-06-08 14:09:40 -07:00
|
|
|
return obj->getProto()->lookupProperty(cx, id, objp, propp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
with_GetProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-08 14:09:40 -07:00
|
|
|
return obj->getProto()->getProperty(cx, id, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-09-15 13:43:55 -07:00
|
|
|
with_SetProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp, JSBool strict)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-15 13:43:55 -07:00
|
|
|
return obj->getProto()->setProperty(cx, id, vp, strict);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-06-05 14:24:54 -07:00
|
|
|
with_GetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-08 14:09:40 -07:00
|
|
|
return obj->getProto()->getAttributes(cx, id, attrsp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-06-05 14:24:54 -07:00
|
|
|
with_SetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-08 14:09:40 -07:00
|
|
|
return obj->getProto()->setAttributes(cx, id, attrsp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-09-15 13:43:55 -07:00
|
|
|
with_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, Value *rval, JSBool strict)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-15 13:43:55 -07:00
|
|
|
return obj->getProto()->deleteProperty(cx, id, rval, strict);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
with_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *statep, jsid *idp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-08 14:09:40 -07:00
|
|
|
return obj->getProto()->enumerate(cx, enum_op, statep, idp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-02-22 16:30:22 -08:00
|
|
|
static JSType
|
|
|
|
with_TypeOf(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
return JSTYPE_OBJECT;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSObject *
|
2010-05-12 07:34:03 -07:00
|
|
|
with_ThisObject(JSContext *cx, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-08 14:09:40 -07:00
|
|
|
return obj->getWithThis();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class js_WithClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
"With",
|
2010-06-08 14:09:40 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(2) | JSCLASS_IS_ANONYMOUS,
|
2010-06-12 09:29:04 -07:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
PropertyStub, /* setProperty */
|
|
|
|
EnumerateStub,
|
|
|
|
ResolveStub,
|
|
|
|
ConvertStub,
|
|
|
|
NULL, /* finalize */
|
|
|
|
NULL, /* reserved */
|
|
|
|
NULL, /* checkAccess */
|
|
|
|
NULL, /* call */
|
|
|
|
NULL, /* construct */
|
|
|
|
NULL, /* xdrObject */
|
|
|
|
NULL, /* hasInstance */
|
|
|
|
NULL, /* mark */
|
|
|
|
JS_NULL_CLASS_EXT,
|
|
|
|
{
|
|
|
|
with_LookupProperty,
|
|
|
|
NULL, /* defineProperty */
|
|
|
|
with_GetProperty,
|
|
|
|
with_SetProperty,
|
|
|
|
with_GetAttributes,
|
|
|
|
with_SetAttributes,
|
|
|
|
with_DeleteProperty,
|
|
|
|
with_Enumerate,
|
|
|
|
with_TypeOf,
|
|
|
|
NULL, /* trace */
|
|
|
|
with_ThisObject,
|
|
|
|
NULL, /* clear */
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2008-12-09 08:38:32 -08:00
|
|
|
JS_REQUIRES_STACK JSObject *
|
2007-03-22 10:30:00 -07:00
|
|
|
js_NewWithObject(JSContext *cx, JSObject *proto, JSObject *parent, jsint depth)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
obj = js_NewGCObject(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
|
|
|
return NULL;
|
2010-08-29 11:57:08 -07:00
|
|
|
|
2010-09-10 17:06:30 -07:00
|
|
|
obj->init(&js_WithClass, proto, parent, js_FloatingFrameIfGenerator(cx, cx->fp()), cx);
|
2010-08-29 11:57:08 -07:00
|
|
|
obj->setMap(cx->runtime->emptyWithShape);
|
2007-03-22 10:30:00 -07:00
|
|
|
OBJ_SET_BLOCK_DEPTH(cx, obj, depth);
|
2010-06-08 14:09:40 -07:00
|
|
|
|
|
|
|
AutoObjectRooter tvr(cx, obj);
|
|
|
|
JSObject *thisp = proto->thisObject(cx);
|
|
|
|
if (!thisp)
|
|
|
|
return NULL;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
obj->setWithThis(thisp);
|
2007-03-22 10:30:00 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_NewBlockObject(JSContext *cx)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Null obj's proto slot so that Object.prototype.* does not pollute block
|
2009-05-06 16:03:10 -07:00
|
|
|
* scopes and to give the block object its own scope.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-06-18 17:43:02 -07:00
|
|
|
JSObject *blockObj = js_NewGCObject(cx);
|
|
|
|
if (!blockObj)
|
|
|
|
return NULL;
|
2010-08-29 11:57:08 -07:00
|
|
|
|
|
|
|
blockObj->init(&js_BlockClass, NULL, NULL, NullValue(), cx);
|
|
|
|
blockObj->setMap(cx->runtime->emptyBlockShape);
|
2009-05-06 16:03:10 -07:00
|
|
|
return blockObj;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2009-07-21 14:25:11 -07:00
|
|
|
js_CloneBlockObject(JSContext *cx, JSObject *proto, JSStackFrame *fp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(proto->isStaticBlock());
|
2009-07-21 14:25:11 -07:00
|
|
|
|
2009-10-02 01:21:19 -07:00
|
|
|
JSObject *clone = js_NewGCObject(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!clone)
|
|
|
|
return NULL;
|
2009-07-21 14:25:11 -07:00
|
|
|
|
2010-09-10 17:06:30 -07:00
|
|
|
JSStackFrame *priv = js_FloatingFrameIfGenerator(cx, fp);
|
2010-07-14 23:19:36 -07:00
|
|
|
|
2010-01-26 22:50:17 -08:00
|
|
|
/* The caller sets parent on its own. */
|
2010-09-10 17:06:30 -07:00
|
|
|
clone->init(&js_BlockClass, proto, NULL, priv, cx);
|
2009-07-21 14:25:11 -07:00
|
|
|
clone->fslots[JSSLOT_BLOCK_DEPTH] = proto->fslots[JSSLOT_BLOCK_DEPTH];
|
2010-01-26 22:50:17 -08:00
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
clone->setMap(proto->map);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!clone->ensureInstanceReservedSlots(cx, OBJ_BLOCK_COUNT(cx, proto)))
|
2010-06-18 17:43:02 -07:00
|
|
|
return NULL;
|
2010-09-02 14:50:44 -07:00
|
|
|
|
|
|
|
JS_ASSERT(clone->isClonedBlock());
|
2007-03-22 10:30:00 -07:00
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2008-12-09 08:38:32 -08:00
|
|
|
JS_REQUIRES_STACK JSBool
|
2008-02-19 04:16:26 -08:00
|
|
|
js_PutBlockObject(JSContext *cx, JSBool normalUnwind)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-05-01 14:59:52 -07:00
|
|
|
/* Blocks have one fixed slot available for the first local.*/
|
|
|
|
JS_STATIC_ASSERT(JS_INITIAL_NSLOTS == JSSLOT_BLOCK_DEPTH + 2);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-22 16:00:20 -07:00
|
|
|
JSStackFrame *const fp = cx->fp();
|
2010-08-09 22:43:33 -07:00
|
|
|
JSObject *obj = &fp->scopeChain();
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(obj->isClonedBlock());
|
2010-08-22 16:00:20 -07:00
|
|
|
JS_ASSERT(obj->getPrivate() == js_FloatingFrameIfGenerator(cx, cx->fp()));
|
2008-02-20 01:24:02 -08:00
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
/* Block objects should have all reserved slots allocated early. */
|
|
|
|
uintN count = OBJ_BLOCK_COUNT(cx, obj);
|
|
|
|
JS_ASSERT(obj->numSlots() == JSSLOT_BLOCK_DEPTH + 1 + count);
|
2008-05-01 14:59:52 -07:00
|
|
|
|
|
|
|
/* The block and its locals must be on the current stack for GC safety. */
|
2010-03-03 18:10:13 -08:00
|
|
|
uintN depth = OBJ_BLOCK_DEPTH(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(depth <= size_t(cx->regs->sp - fp->base()));
|
|
|
|
JS_ASSERT(count <= size_t(cx->regs->sp - fp->base() - depth));
|
2008-05-01 14:59:52 -07:00
|
|
|
|
2009-09-09 17:23:42 -07:00
|
|
|
/* See comments in CheckDestructuring from jsparse.cpp. */
|
2008-05-01 14:59:52 -07:00
|
|
|
JS_ASSERT(count >= 1);
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (normalUnwind) {
|
|
|
|
uintN slot = JSSLOT_BLOCK_DEPTH + 1;
|
|
|
|
uintN flen = JS_MIN(count, JS_INITIAL_NSLOTS - slot);
|
|
|
|
uintN stop = slot + flen;
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
depth += fp->numFixed();
|
2010-08-29 11:57:08 -07:00
|
|
|
while (slot < stop)
|
|
|
|
obj->fslots[slot++] = fp->slots()[depth++];
|
|
|
|
count -= flen;
|
|
|
|
if (count != 0)
|
|
|
|
memcpy(obj->dslots, fp->slots() + depth, count * sizeof(Value));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-15 03:38:40 -08:00
|
|
|
/* We must clear the private slot even with errors. */
|
2009-08-04 14:06:55 -07:00
|
|
|
obj->setPrivate(NULL);
|
2010-08-09 22:43:33 -07:00
|
|
|
fp->setScopeChainNoCallObj(*obj->getParent());
|
2008-02-15 03:38:40 -08:00
|
|
|
return normalUnwind;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
block_getProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-09 13:53:37 -07:00
|
|
|
/*
|
|
|
|
* Block objects are never exposed to script, and the engine handles them
|
|
|
|
* with care. So unlike other getters, this one can assert (rather than
|
|
|
|
* check) certain invariants about obj.
|
|
|
|
*/
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(obj->isClonedBlock());
|
2010-07-14 23:19:36 -07:00
|
|
|
uintN index = (uintN) JSID_TO_INT(id);
|
2009-09-09 13:53:37 -07:00
|
|
|
JS_ASSERT(index < OBJ_BLOCK_COUNT(cx, obj));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
|
2008-05-01 14:59:52 -07:00
|
|
|
if (fp) {
|
2010-03-03 17:52:26 -08:00
|
|
|
fp = js_LiveFrameIfGenerator(fp);
|
2010-08-09 22:43:33 -07:00
|
|
|
index += fp->numFixed() + OBJ_BLOCK_DEPTH(cx, obj);
|
|
|
|
JS_ASSERT(index < fp->numSlots());
|
2010-03-03 17:52:26 -08:00
|
|
|
*vp = fp->slots()[index];
|
2009-09-09 13:53:37 -07:00
|
|
|
return true;
|
2008-05-01 14:59:52 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
/* Values are in slots immediately following the class-reserved ones. */
|
|
|
|
JS_ASSERT(obj->getSlot(JSSLOT_FREE(&js_BlockClass) + index) == *vp);
|
2009-09-09 13:53:37 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
block_setProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(obj->isClonedBlock());
|
2010-07-14 23:19:36 -07:00
|
|
|
uintN index = (uintN) JSID_TO_INT(id);
|
2009-09-09 13:53:37 -07:00
|
|
|
JS_ASSERT(index < OBJ_BLOCK_COUNT(cx, obj));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
|
2008-05-01 14:59:52 -07:00
|
|
|
if (fp) {
|
2010-03-03 17:52:26 -08:00
|
|
|
fp = js_LiveFrameIfGenerator(fp);
|
2010-08-09 22:43:33 -07:00
|
|
|
index += fp->numFixed() + OBJ_BLOCK_DEPTH(cx, obj);
|
|
|
|
JS_ASSERT(index < fp->numSlots());
|
2010-03-03 17:52:26 -08:00
|
|
|
fp->slots()[index] = *vp;
|
2009-09-09 13:53:37 -07:00
|
|
|
return true;
|
2008-05-01 14:59:52 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
/*
|
|
|
|
* The value in *vp will be written back to the slot in obj that was
|
|
|
|
* allocated when this let binding was defined.
|
|
|
|
*/
|
2009-09-09 13:53:37 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
const Shape *
|
|
|
|
JSObject::defineBlockVariable(JSContext *cx, jsid id, intN index)
|
2009-09-09 13:53:37 -07:00
|
|
|
{
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(isStaticBlock());
|
2009-09-09 13:53:37 -07:00
|
|
|
|
|
|
|
/* Use JSPROP_ENUMERATE to aid the disassembler. */
|
2010-09-02 14:50:44 -07:00
|
|
|
uint32 slot = JSSLOT_FREE(&js_BlockClass) + index;
|
|
|
|
const Shape *shape = addProperty(cx, id,
|
|
|
|
block_getProperty, block_setProperty,
|
|
|
|
slot, JSPROP_ENUMERATE | JSPROP_PERMANENT,
|
|
|
|
Shape::HAS_SHORTID, index);
|
|
|
|
if (!shape)
|
|
|
|
return NULL;
|
|
|
|
if (slot >= numSlots() && !growSlots(cx, slot + 1))
|
|
|
|
return NULL;
|
|
|
|
return shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-24 14:33:03 -07:00
|
|
|
static size_t
|
|
|
|
GetObjectSize(JSObject *obj)
|
|
|
|
{
|
|
|
|
return (obj->isFunction() && !obj->getPrivate())
|
|
|
|
? sizeof(JSFunction)
|
|
|
|
: sizeof(JSObject);
|
|
|
|
}
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
/*
|
|
|
|
* Use this method with extreme caution. It trades the guts of two objects and updates
|
|
|
|
* scope ownership. This operation is not thread-safe, just as fast array to slow array
|
|
|
|
* transitions are inherently not thread-safe. Don't perform a swap operation on objects
|
|
|
|
* shared across threads or, or bad things will happen. You have been warned.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
JSObject::swap(JSObject *other)
|
|
|
|
{
|
2010-05-24 14:33:03 -07:00
|
|
|
size_t size = GetObjectSize(this);
|
|
|
|
JS_ASSERT(size == GetObjectSize(other));
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
/* Trade the guts of the objects. */
|
2010-07-14 23:19:36 -07:00
|
|
|
char tmp[tl::Max<sizeof(JSFunction), sizeof(JSObject)>::result];
|
|
|
|
memcpy(tmp, this, size);
|
2010-05-24 14:33:03 -07:00
|
|
|
memcpy(this, other, size);
|
2010-07-14 23:19:36 -07:00
|
|
|
memcpy(other, tmp, size);
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if JS_HAS_XDR
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
#define NO_PARENT_INDEX ((uint32)-1)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
uint32
|
|
|
|
FindObjectIndex(JSObjectArray *array, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2007-07-08 02:03:34 -07:00
|
|
|
if (array) {
|
|
|
|
i = array->length;
|
|
|
|
do {
|
|
|
|
|
|
|
|
if (array->vector[--i] == obj)
|
|
|
|
return i;
|
|
|
|
} while (i != 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NO_PARENT_INDEX;
|
|
|
|
}
|
|
|
|
|
2009-05-06 16:03:10 -07:00
|
|
|
JSBool
|
|
|
|
js_XDRBlockObject(JSXDRState *xdr, JSObject **objp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSContext *cx;
|
2007-07-08 02:03:34 -07:00
|
|
|
uint32 parentId;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *obj, *parent;
|
2010-09-02 14:50:44 -07:00
|
|
|
uintN depth, count;
|
|
|
|
uint32 depthAndCount;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
cx = xdr->cx;
|
|
|
|
#ifdef __GNUC__
|
|
|
|
obj = NULL; /* quell GCC overwarning */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
|
|
|
obj = *objp;
|
2010-03-04 23:52:52 -08:00
|
|
|
parent = obj->getParent();
|
2007-07-08 02:03:34 -07:00
|
|
|
parentId = (xdr->script->objectsOffset == 0)
|
|
|
|
? NO_PARENT_INDEX
|
2009-09-09 16:52:55 -07:00
|
|
|
: FindObjectIndex(xdr->script->objects(), parent);
|
2007-06-18 12:33:21 -07:00
|
|
|
depth = (uint16)OBJ_BLOCK_DEPTH(cx, obj);
|
|
|
|
count = (uint16)OBJ_BLOCK_COUNT(cx, obj);
|
2010-09-02 14:50:44 -07:00
|
|
|
depthAndCount = (uint32)(depth << 16) | count;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#ifdef __GNUC__ /* suppress bogus gcc warnings */
|
|
|
|
else count = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* First, XDR the parent atomid. */
|
|
|
|
if (!JS_XDRUint32(xdr, &parentId))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
|
|
|
obj = js_NewBlockObject(cx);
|
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
*objp = obj;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's a parent id, then get the parent out of our script's
|
2007-07-08 02:03:34 -07:00
|
|
|
* object array. We know that we XDR block object in outer-to-inner
|
|
|
|
* order, which means that getting the parent now will work.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2007-07-08 02:03:34 -07:00
|
|
|
if (parentId == NO_PARENT_INDEX)
|
2007-03-22 10:30:00 -07:00
|
|
|
parent = NULL;
|
2007-07-08 02:03:34 -07:00
|
|
|
else
|
2009-09-09 16:52:55 -07:00
|
|
|
parent = xdr->script->getObject(parentId);
|
2010-03-04 23:52:52 -08:00
|
|
|
obj->setParent(parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoObjectRooter tvr(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
if (!JS_XDRUint32(xdr, &depthAndCount))
|
2009-09-25 05:30:11 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
2010-09-02 14:50:44 -07:00
|
|
|
depth = (uint16)(depthAndCount >> 16);
|
|
|
|
count = (uint16)depthAndCount;
|
2010-07-14 23:19:36 -07:00
|
|
|
obj->setSlot(JSSLOT_BLOCK_DEPTH, Value(Int32Value(depth)));
|
2010-09-12 08:53:18 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XDR the block object's properties. We know that there are 'count'
|
|
|
|
* properties to XDR, stored as id/shortid pairs.
|
|
|
|
*/
|
|
|
|
for (uintN i = 0; i < count; i++) {
|
|
|
|
JSAtom *atom;
|
|
|
|
uint16 shortid;
|
|
|
|
|
|
|
|
/* XDR the real id, then the shortid. */
|
|
|
|
if (!js_XDRAtom(xdr, &atom) || !JS_XDRUint16(xdr, &shortid))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!obj->defineBlockVariable(cx, ATOM_TO_JSID(atom), shortid))
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-02 14:50:44 -07:00
|
|
|
} else {
|
2010-09-12 08:53:18 -07:00
|
|
|
Vector<const Shape *, 8> shapes(cx);
|
|
|
|
shapes.growByUninitialized(count);
|
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
for (Shape::Range r(obj->lastProperty()); !r.empty(); r.popFront()) {
|
|
|
|
shape = &r.front();
|
|
|
|
shapes[shape->shortid] = shape;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-12 08:53:18 -07:00
|
|
|
/*
|
|
|
|
* XDR the block object's properties. We know that there are 'count'
|
|
|
|
* properties to XDR, stored as id/shortid pairs.
|
|
|
|
*/
|
|
|
|
for (uintN i = 0; i < count; i++) {
|
2010-09-02 14:50:44 -07:00
|
|
|
shape = shapes[i];
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(shape->getter() == block_getProperty);
|
2010-09-02 14:50:44 -07:00
|
|
|
|
|
|
|
jsid propid = shape->id;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(JSID_IS_ATOM(propid));
|
2010-09-12 08:53:18 -07:00
|
|
|
JSAtom *atom = JSID_TO_ATOM(propid);
|
2010-09-02 14:50:44 -07:00
|
|
|
|
2010-09-12 08:53:18 -07:00
|
|
|
uint16 shortid = uint16(shape->shortid);
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(shortid == i);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-12 08:53:18 -07:00
|
|
|
/* XDR the real id, then the shortid. */
|
|
|
|
if (!js_XDRAtom(xdr, &atom) || !JS_XDRUint16(xdr, &shortid))
|
2009-09-25 05:30:11 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 05:30:11 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class js_BlockClass = {
|
2007-03-22 10:30:00 -07:00
|
|
|
"Block",
|
2009-05-06 16:03:10 -07:00
|
|
|
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_IS_ANONYMOUS,
|
2010-06-12 09:29:04 -07:00
|
|
|
PropertyStub, /* addProperty */
|
|
|
|
PropertyStub, /* delProperty */
|
|
|
|
PropertyStub, /* getProperty */
|
|
|
|
PropertyStub, /* setProperty */
|
|
|
|
EnumerateStub,
|
|
|
|
ResolveStub,
|
|
|
|
ConvertStub
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
JSObject *
|
2010-06-02 01:18:50 -07:00
|
|
|
js_InitObjectClass(JSContext *cx, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-08-16 12:35:04 -07:00
|
|
|
JSObject *proto = js_InitClass(cx, obj, NULL, &js_ObjectClass, js_Object, 1,
|
2010-06-02 01:18:50 -07:00
|
|
|
object_props, object_methods, NULL, object_static_methods);
|
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
|
2007-06-04 16:03:03 -07:00
|
|
|
/* ECMA (15.1.2.1) says 'eval' is a property of the global object. */
|
2010-08-16 12:35:04 -07:00
|
|
|
if (!js_DefineFunction(cx, obj, cx->runtime->atomState.evalAtom, obj_eval, 1,
|
|
|
|
JSFUN_STUB_GSOPS)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-02 01:18:50 -07:00
|
|
|
return proto;
|
2009-03-04 19:26:16 -08:00
|
|
|
}
|
|
|
|
|
2010-05-23 17:12:08 -07:00
|
|
|
static bool
|
|
|
|
DefineStandardSlot(JSContext *cx, JSObject *obj, JSProtoKey key, JSAtom *atom,
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &v, uint32 attrs, bool &named)
|
2010-05-23 17:12:08 -07:00
|
|
|
{
|
|
|
|
jsid id = ATOM_TO_JSID(atom);
|
|
|
|
|
|
|
|
if (key != JSProto_Null) {
|
|
|
|
/*
|
|
|
|
* Initializing an actual standard class on a global object. If the
|
|
|
|
* property is not yet present, force it into a new one bound to a
|
|
|
|
* reserved slot. Otherwise, go through the normal property path.
|
|
|
|
*/
|
|
|
|
JS_ASSERT(obj->getClass()->flags & JSCLASS_IS_GLOBAL);
|
|
|
|
JS_ASSERT(obj->isNative());
|
|
|
|
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->ensureClassReservedSlots(cx)) {
|
2010-05-23 17:12:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape = obj->nativeLookup(id);
|
|
|
|
if (!shape) {
|
2010-06-02 20:30:45 -07:00
|
|
|
uint32 index = 2 * JSProto_LIMIT + key;
|
2010-05-23 17:12:08 -07:00
|
|
|
if (!js_SetReservedSlot(cx, obj, index, v)) {
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-05-23 17:12:08 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 slot = JSSLOT_START(obj->getClass()) + index;
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = obj->addProperty(cx, id, PropertyStub, PropertyStub, slot, attrs, 0, 0);
|
2010-05-23 17:12:08 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
if (!shape)
|
2010-05-23 17:12:08 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
named = true;
|
|
|
|
return true;
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-05-23 17:12:08 -07:00
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
named = obj->defineProperty(cx, id, v, PropertyStub, PropertyStub, attrs);
|
2010-05-23 17:12:08 -07:00
|
|
|
return named;
|
|
|
|
}
|
|
|
|
|
2009-03-04 19:26:16 -08:00
|
|
|
JSObject *
|
|
|
|
js_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp, Native constructor, uintN nargs,
|
2009-03-04 19:26:16 -08:00
|
|
|
JSPropertySpec *ps, JSFunctionSpec *fs,
|
2009-05-05 14:26:06 -07:00
|
|
|
JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
|
2009-03-04 19:26:16 -08:00
|
|
|
{
|
|
|
|
JSAtom *atom;
|
|
|
|
JSProtoKey key;
|
|
|
|
JSFunction *fun;
|
2010-05-23 17:12:08 -07:00
|
|
|
bool named = false;
|
2009-03-04 19:26:16 -08:00
|
|
|
|
|
|
|
atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0);
|
|
|
|
if (!atom)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When initializing a standard class, if no parent_proto (grand-proto of
|
|
|
|
* instances of the class, parent-proto of the class's prototype object)
|
|
|
|
* is given, we must use Object.prototype if it is available. Otherwise,
|
|
|
|
* we could look up the wrong binding for a class name in obj. Example:
|
|
|
|
*
|
|
|
|
* String = Array;
|
|
|
|
* print("hi there".join);
|
|
|
|
*
|
|
|
|
* should print undefined, not Array.prototype.join. This is required by
|
|
|
|
* ECMA-262, alas. It might have been better to make String readonly and
|
|
|
|
* permanent in the global object, instead -- but that's too big a change
|
|
|
|
* to swallow at this point.
|
|
|
|
*/
|
|
|
|
key = JSCLASS_CACHED_PROTO_KEY(clasp);
|
|
|
|
if (key != JSProto_Null &&
|
|
|
|
!parent_proto &&
|
2010-02-19 09:44:23 -08:00
|
|
|
!js_GetClassPrototype(cx, obj, JSProto_Object, &parent_proto)) {
|
2009-03-04 19:26:16 -08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
/*
|
|
|
|
* Create a prototype object for this class.
|
|
|
|
*
|
|
|
|
* FIXME: lazy standard (built-in) class initialization and even older
|
|
|
|
* eager boostrapping code rely on all of these properties:
|
|
|
|
*
|
|
|
|
* 1. NewObject attempting to compute a default prototype object when
|
|
|
|
* passed null for proto; and
|
|
|
|
*
|
|
|
|
* 2. NewObject tolerating no default prototype (null proto slot value)
|
|
|
|
* due to this js_InitClass call coming from js_InitFunctionClass on an
|
|
|
|
* otherwise-uninitialized global.
|
|
|
|
*
|
|
|
|
* 3. NewObject allocating a JSFunction-sized GC-thing when clasp is
|
|
|
|
* &js_FunctionClass, not a JSObject-sized (smaller) GC-thing.
|
|
|
|
*
|
|
|
|
* The JS_NewObjectForGivenProto and JS_NewObject APIs also allow clasp to
|
|
|
|
* be &js_FunctionClass (we could break compatibility easily). But fixing
|
|
|
|
* (3) is not enough without addressing the bootstrapping dependency on (1)
|
|
|
|
* and (2).
|
|
|
|
*/
|
2010-08-09 09:11:22 -07:00
|
|
|
JSObject *proto = NewObject<WithProto::Class>(cx, clasp, parent_proto, obj);
|
2009-03-04 19:26:16 -08:00
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
|
2010-03-28 20:32:20 -07:00
|
|
|
/* After this point, control must exit via label bad or out. */
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoObjectRooter tvr(cx, proto);
|
2009-03-04 19:26:16 -08:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *ctor;
|
2009-03-04 19:26:16 -08:00
|
|
|
if (!constructor) {
|
|
|
|
/*
|
|
|
|
* Lacking a constructor, name the prototype (e.g., Math) unless this
|
|
|
|
* class (a) is anonymous, i.e. for internal use only; (b) the class
|
|
|
|
* of obj (the global object) is has a reserved slot indexed by key;
|
|
|
|
* and (c) key is not the null key.
|
|
|
|
*/
|
2010-05-23 17:12:08 -07:00
|
|
|
if (!(clasp->flags & JSCLASS_IS_ANONYMOUS) ||
|
|
|
|
!(obj->getClass()->flags & JSCLASS_IS_GLOBAL) ||
|
|
|
|
key == JSProto_Null)
|
|
|
|
{
|
|
|
|
uint32 attrs = (clasp->flags & JSCLASS_IS_ANONYMOUS)
|
|
|
|
? JSPROP_READONLY | JSPROP_PERMANENT
|
|
|
|
: 0;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!DefineStandardSlot(cx, obj, key, atom, ObjectValue(*proto), attrs, named))
|
2009-03-04 19:26:16 -08:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctor = proto;
|
|
|
|
} else {
|
2010-08-16 12:35:04 -07:00
|
|
|
fun = js_NewFunction(cx, NULL, constructor, nargs, JSFUN_CONSTRUCTOR, obj, atom);
|
2009-03-04 19:26:16 -08:00
|
|
|
if (!fun)
|
|
|
|
goto bad;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoValueRooter tvr2(cx, ObjectValue(*fun));
|
2010-05-23 17:12:08 -07:00
|
|
|
if (!DefineStandardSlot(cx, obj, key, atom, tvr2.value(), 0, named))
|
|
|
|
goto bad;
|
|
|
|
|
2009-03-04 19:26:16 -08:00
|
|
|
/*
|
|
|
|
* Remember the class this function is a constructor for so that
|
|
|
|
* we know to create an object of this class when we call the
|
2010-07-30 18:24:03 -07:00
|
|
|
* constructor.
|
2009-03-04 19:26:16 -08:00
|
|
|
*/
|
2010-07-30 18:24:03 -07:00
|
|
|
FUN_CLASP(fun) = clasp;
|
2009-03-04 19:26:16 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Optionally construct the prototype object, before the class has
|
|
|
|
* been fully initialized. Allow the ctor to replace proto with a
|
|
|
|
* different object, as is done for operator new -- and as at least
|
|
|
|
* XML support requires.
|
|
|
|
*/
|
|
|
|
ctor = FUN_OBJECT(fun);
|
|
|
|
if (clasp->flags & JSCLASS_CONSTRUCT_PROTOTYPE) {
|
2010-07-14 23:19:36 -07:00
|
|
|
Value rval;
|
2010-08-16 12:35:04 -07:00
|
|
|
if (!InvokeConstructorWithGivenThis(cx, proto, ObjectOrNullValue(ctor),
|
|
|
|
0, NULL, &rval)) {
|
2009-03-04 19:26:16 -08:00
|
|
|
goto bad;
|
2010-08-16 12:35:04 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
if (rval.isObject() && &rval.toObject() != proto)
|
|
|
|
proto = &rval.toObject();
|
2009-03-04 19:26:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect constructor and prototype by named properties. */
|
|
|
|
if (!js_SetClassPrototype(cx, ctor, proto,
|
|
|
|
JSPROP_READONLY | JSPROP_PERMANENT)) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bootstrap Function.prototype (see also JS_InitStandardClasses). */
|
2010-04-07 16:09:49 -07:00
|
|
|
if (ctor->getClass() == clasp)
|
2010-03-04 20:44:09 -08:00
|
|
|
ctor->setProto(proto);
|
2009-03-04 19:26:16 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add properties and methods to the prototype and the constructor. */
|
|
|
|
if ((ps && !JS_DefineProperties(cx, proto, ps)) ||
|
|
|
|
(fs && !JS_DefineFunctions(cx, proto, fs)) ||
|
|
|
|
(static_ps && !JS_DefineProperties(cx, ctor, static_ps)) ||
|
|
|
|
(static_fs && !JS_DefineFunctions(cx, ctor, static_fs))) {
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
|
2009-11-18 12:29:58 -08:00
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* Make sure proto's emptyShape is available to be shared by objects of
|
|
|
|
* this class. JSObject::emptyShape is a one-slot cache. If we omit this,
|
|
|
|
* some other class could snap it up. (The risk is particularly great for
|
|
|
|
* Object.prototype.)
|
2009-11-18 12:29:58 -08:00
|
|
|
*
|
2010-08-29 11:57:08 -07:00
|
|
|
* All callers of JSObject::initSharingEmptyShape depend on this.
|
2010-08-31 07:33:25 -07:00
|
|
|
*
|
|
|
|
* FIXME: bug 592296 -- js_InitArrayClass should pass &js_SlowArrayClass
|
|
|
|
* and make the Array.prototype slow from the start.
|
2009-11-18 12:29:58 -08:00
|
|
|
*/
|
2010-08-31 07:33:25 -07:00
|
|
|
JS_ASSERT_IF(proto->clasp != clasp,
|
|
|
|
clasp == &js_ArrayClass && proto->clasp == &js_SlowArrayClass);
|
|
|
|
if (!proto->getEmptyShape(cx, proto->clasp))
|
2010-01-24 03:15:38 -08:00
|
|
|
goto bad;
|
2009-11-18 12:29:58 -08:00
|
|
|
|
2009-03-04 19:26:16 -08:00
|
|
|
/* If this is a standard class, cache its prototype. */
|
2010-06-02 20:30:45 -07:00
|
|
|
if (key != JSProto_Null && !js_SetClassObject(cx, obj, key, ctor, proto))
|
2009-03-04 19:26:16 -08:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
return proto;
|
|
|
|
|
|
|
|
bad:
|
2010-07-14 23:19:36 -07:00
|
|
|
if (named) {
|
|
|
|
Value rval;
|
2010-09-15 13:43:55 -07:00
|
|
|
obj->deleteProperty(cx, ATOM_TO_JSID(atom), &rval, false);
|
2010-07-14 23:19:36 -07:00
|
|
|
}
|
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-04-10 16:16:35 -07:00
|
|
|
bool
|
2010-05-04 20:28:38 -07:00
|
|
|
JSObject::allocSlots(JSContext *cx, size_t nslots)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-04 20:28:38 -07:00
|
|
|
JS_ASSERT(!dslots);
|
2009-07-16 18:42:54 -07:00
|
|
|
JS_ASSERT(nslots > JS_INITIAL_NSLOTS);
|
|
|
|
|
2010-05-04 20:28:38 -07:00
|
|
|
size_t nwords = slotsToDynamicWords(nslots);
|
2010-07-14 23:19:36 -07:00
|
|
|
dslots = (Value*) cx->malloc(nwords * sizeof(Value));
|
2010-05-04 20:28:38 -07:00
|
|
|
if (!dslots)
|
2010-04-19 14:42:04 -07:00
|
|
|
return false;
|
2009-07-16 18:42:54 -07:00
|
|
|
|
2010-05-04 20:28:38 -07:00
|
|
|
dslots++;
|
2010-07-14 23:19:36 -07:00
|
|
|
dslots[-1].setPrivateUint32(nslots);
|
|
|
|
SetValueRangeToUndefined(dslots, nslots - JS_INITIAL_NSLOTS);
|
2009-07-16 18:42:54 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-16 18:42:54 -07:00
|
|
|
bool
|
2010-05-04 20:28:38 -07:00
|
|
|
JSObject::growSlots(JSContext *cx, size_t nslots)
|
2009-07-16 18:42:54 -07:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Minimal number of dynamic slots to allocate.
|
|
|
|
*/
|
2009-07-16 18:42:54 -07:00
|
|
|
const size_t MIN_DYNAMIC_WORDS = 4;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The limit to switch to linear allocation strategy from the power of 2
|
|
|
|
* growth no to waste too much memory.
|
|
|
|
*/
|
2009-07-16 18:42:54 -07:00
|
|
|
const size_t LINEAR_GROWTH_STEP = JS_BIT(16);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-16 18:42:54 -07:00
|
|
|
/* If we are allocating fslots, there is nothing to do. */
|
|
|
|
if (nslots <= JS_INITIAL_NSLOTS)
|
2010-08-29 11:57:08 -07:00
|
|
|
return true;
|
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
/* Don't let nslots get close to wrapping around uint32. */
|
|
|
|
if (nslots >= NSLOTS_LIMIT) {
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-04 20:28:38 -07:00
|
|
|
size_t nwords = slotsToDynamicWords(nslots);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-16 18:42:54 -07:00
|
|
|
/*
|
|
|
|
* Round up nslots so the number of bytes in dslots array is power
|
|
|
|
* of 2 to ensure exponential grouth.
|
|
|
|
*/
|
|
|
|
uintN log;
|
|
|
|
if (nwords <= MIN_DYNAMIC_WORDS) {
|
|
|
|
nwords = MIN_DYNAMIC_WORDS;
|
|
|
|
} else if (nwords < LINEAR_GROWTH_STEP) {
|
|
|
|
JS_CEILING_LOG2(log, nwords);
|
|
|
|
nwords = JS_BIT(log);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2009-07-16 18:42:54 -07:00
|
|
|
nwords = JS_ROUNDUP(nwords, LINEAR_GROWTH_STEP);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-04 20:28:38 -07:00
|
|
|
nslots = dynamicWordsToSlots(nwords);
|
2009-07-16 18:42:54 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If nothing was allocated yet, treat it as initial allocation (but with
|
|
|
|
* the exponential growth algorithm applied).
|
|
|
|
*/
|
2010-05-04 20:28:38 -07:00
|
|
|
if (!dslots)
|
|
|
|
return allocSlots(cx, nslots);
|
2009-07-16 18:42:54 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
size_t oldnslots = dslots[-1].toPrivateUint32();
|
2009-07-16 18:42:54 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *tmpdslots = (Value*) cx->realloc(dslots - 1, nwords * sizeof(Value));
|
2010-05-04 20:28:38 -07:00
|
|
|
if (!tmpdslots)
|
2010-08-29 11:57:08 -07:00
|
|
|
return false; /* leave dslots at its old size */
|
2010-04-19 14:42:04 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
dslots = tmpdslots;
|
2010-05-04 20:28:38 -07:00
|
|
|
dslots++;
|
2010-07-14 23:19:36 -07:00
|
|
|
dslots[-1].setPrivateUint32(nslots);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-16 18:42:54 -07:00
|
|
|
/* Initialize the additional slots we added. */
|
2010-05-04 20:28:38 -07:00
|
|
|
JS_ASSERT(nslots > oldnslots);
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *beg = dslots + (oldnslots - JS_INITIAL_NSLOTS);
|
|
|
|
Value *end = dslots + (nslots - JS_INITIAL_NSLOTS);
|
|
|
|
SetValueRangeToUndefined(beg, end);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-16 18:42:54 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-05-04 20:28:38 -07:00
|
|
|
JSObject::shrinkSlots(JSContext *cx, size_t nslots)
|
2009-07-16 18:42:54 -07:00
|
|
|
{
|
|
|
|
/* Nothing to shrink? */
|
2010-05-04 20:28:38 -07:00
|
|
|
if (!dslots)
|
2009-07-16 18:42:54 -07:00
|
|
|
return;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(dslots[-1].toPrivateUint32() > JS_INITIAL_NSLOTS);
|
|
|
|
JS_ASSERT(nslots <= dslots[-1].toPrivateUint32());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-16 18:42:54 -07:00
|
|
|
if (nslots <= JS_INITIAL_NSLOTS) {
|
2010-05-04 20:28:38 -07:00
|
|
|
freeSlotsArray(cx);
|
|
|
|
dslots = NULL;
|
2009-07-16 18:42:54 -07:00
|
|
|
} else {
|
2010-05-04 20:28:38 -07:00
|
|
|
size_t nwords = slotsToDynamicWords(nslots);
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *tmpdslots = (Value*) cx->realloc(dslots - 1, nwords * sizeof(Value));
|
2010-05-04 20:28:38 -07:00
|
|
|
if (!tmpdslots)
|
2010-08-29 11:57:08 -07:00
|
|
|
return; /* leave dslots at its old size */
|
2010-05-04 20:28:38 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
dslots = tmpdslots;
|
2010-05-04 20:28:38 -07:00
|
|
|
dslots++;
|
2010-07-14 23:19:36 -07:00
|
|
|
dslots[-1].setPrivateUint32(nslots);
|
2009-07-16 18:42:54 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-07-24 03:01:37 -07:00
|
|
|
bool
|
2010-08-29 11:57:08 -07:00
|
|
|
JSObject::ensureInstanceReservedSlots(JSContext *cx, size_t nreserved)
|
2009-07-24 03:01:37 -07:00
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT_IF(isNative(),
|
2010-08-29 14:24:23 -07:00
|
|
|
isBlock() || isCall() || (isFunction() && getFunctionPrivate()->isBound()));
|
2010-08-29 11:57:08 -07:00
|
|
|
|
|
|
|
uintN nslots = JSSLOT_FREE(clasp) + nreserved;
|
2010-09-02 14:50:44 -07:00
|
|
|
return nslots <= numSlots() || allocSlots(cx, nslots);
|
2009-07-24 03:01:37 -07:00
|
|
|
}
|
|
|
|
|
2008-09-06 15:21:43 -07:00
|
|
|
static JSObject *
|
2007-03-22 10:30:00 -07:00
|
|
|
js_InitNullClass(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JS_ASSERT(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define JS_PROTO(name,code,init) extern JSObject *init(JSContext *, JSObject *);
|
|
|
|
#include "jsproto.tbl"
|
|
|
|
#undef JS_PROTO
|
|
|
|
|
|
|
|
static JSObjectOp lazy_prototype_init[JSProto_LIMIT] = {
|
|
|
|
#define JS_PROTO(name,code,init) init,
|
|
|
|
#include "jsproto.tbl"
|
|
|
|
#undef JS_PROTO
|
|
|
|
};
|
|
|
|
|
2010-06-17 15:23:17 -07:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
bool
|
|
|
|
SetProto(JSContext *cx, JSObject *obj, JSObject *proto, bool checkForCycles)
|
|
|
|
{
|
|
|
|
JS_ASSERT_IF(!checkForCycles, obj != proto);
|
|
|
|
|
|
|
|
if (obj->isNative()) {
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
bool ok = obj->ensureClassReservedSlots(cx);
|
2010-06-17 15:23:17 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
if (!ok)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Regenerate property cache shape ids for all of the scopes along the
|
|
|
|
* old prototype chain to invalidate their property cache entries, in
|
|
|
|
* case any entries were filled by looking up through obj.
|
|
|
|
*/
|
|
|
|
JSObject *oldproto = obj;
|
|
|
|
while (oldproto && oldproto->isNative()) {
|
|
|
|
JS_LOCK_OBJ(cx, oldproto);
|
2010-08-29 11:57:08 -07:00
|
|
|
oldproto->protoShapeChange(cx);
|
2010-06-17 15:23:17 -07:00
|
|
|
JSObject *tmp = oldproto->getProto();
|
|
|
|
JS_UNLOCK_OBJ(cx, oldproto);
|
|
|
|
oldproto = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!proto || !checkForCycles) {
|
|
|
|
obj->setProto(proto);
|
|
|
|
} else if (!SetProtoCheckingForCycles(cx, obj, proto)) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CYCLIC_VALUE, js_proto_str);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
|
|
|
js_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
|
|
|
|
JSObject **objp)
|
|
|
|
{
|
|
|
|
JSObject *tmp, *cobj;
|
|
|
|
JSResolvingKey rkey;
|
|
|
|
JSResolvingEntry *rentry;
|
|
|
|
uint32 generation;
|
|
|
|
JSObjectOp init;
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-04 23:52:52 -08:00
|
|
|
while ((tmp = obj->getParent()) != NULL)
|
2007-03-22 10:30:00 -07:00
|
|
|
obj = tmp;
|
2010-04-07 16:09:49 -07:00
|
|
|
if (!(obj->getClass()->flags & JSCLASS_IS_GLOBAL)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*objp = NULL;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-07 18:35:58 -07:00
|
|
|
v = obj->getReservedSlot(key);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isObject()) {
|
|
|
|
*objp = &v.toObject();
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rkey.obj = obj;
|
|
|
|
rkey.id = ATOM_TO_JSID(cx->runtime->atomState.classAtoms[key]);
|
|
|
|
if (!js_StartResolving(cx, &rkey, JSRESFLAG_LOOKUP, &rentry))
|
|
|
|
return JS_FALSE;
|
|
|
|
if (!rentry) {
|
|
|
|
/* Already caching key in obj -- suppress recursion. */
|
|
|
|
*objp = NULL;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
generation = cx->resolvingTable->generation;
|
|
|
|
|
2010-06-02 20:30:45 -07:00
|
|
|
JSBool ok = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
cobj = NULL;
|
|
|
|
init = lazy_prototype_init[key];
|
|
|
|
if (init) {
|
|
|
|
if (!init(cx, obj)) {
|
|
|
|
ok = JS_FALSE;
|
|
|
|
} else {
|
2010-06-07 18:35:58 -07:00
|
|
|
v = obj->getReservedSlot(key);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isObject())
|
|
|
|
cobj = &v.toObject();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
js_StopResolving(cx, &rkey, JSRESFLAG_LOOKUP, rentry, generation);
|
|
|
|
*objp = cobj;
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-06-02 20:30:45 -07:00
|
|
|
js_SetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key, JSObject *cobj, JSObject *proto)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-04 23:52:52 -08:00
|
|
|
JS_ASSERT(!obj->getParent());
|
2010-04-07 16:09:49 -07:00
|
|
|
if (!(obj->getClass()->flags & JSCLASS_IS_GLOBAL))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
return js_SetReservedSlot(cx, obj, key, ObjectOrNullValue(cobj)) &&
|
|
|
|
js_SetReservedSlot(cx, obj, JSProto_LIMIT + key, ObjectOrNullValue(proto));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-02-19 09:44:23 -08:00
|
|
|
js_FindClassObject(JSContext *cx, JSObject *start, JSProtoKey protoKey,
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *vp, Class *clasp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-12-09 08:38:32 -08:00
|
|
|
JSStackFrame *fp;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *obj, *cobj, *pobj;
|
2010-02-19 09:44:23 -08:00
|
|
|
jsid id;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSProperty *prop;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-12-09 08:38:32 -08:00
|
|
|
/*
|
2010-08-22 16:00:20 -07:00
|
|
|
* Find the global object. Use cx->fp() directly to avoid falling off
|
2008-12-09 08:38:32 -08:00
|
|
|
* trace; all JIT-elided stack frames have the same global object as
|
2010-08-22 16:00:20 -07:00
|
|
|
* cx->fp().
|
2008-12-09 08:38:32 -08:00
|
|
|
*/
|
|
|
|
VOUCH_DOES_NOT_REQUIRE_STACK();
|
2010-08-22 16:00:20 -07:00
|
|
|
if (!start && (fp = cx->maybefp()) != NULL)
|
2010-08-09 22:43:33 -07:00
|
|
|
start = &fp->scopeChain();
|
2008-12-09 08:38:32 -08:00
|
|
|
|
|
|
|
if (start) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Find the topmost object in the scope chain. */
|
|
|
|
do {
|
|
|
|
obj = start;
|
2010-03-04 23:52:52 -08:00
|
|
|
start = obj->getParent();
|
2007-03-22 10:30:00 -07:00
|
|
|
} while (start);
|
|
|
|
} else {
|
|
|
|
obj = cx->globalObject;
|
|
|
|
if (!obj) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OBJ_TO_INNER_OBJECT(cx, obj);
|
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2010-02-19 09:44:23 -08:00
|
|
|
if (protoKey != JSProto_Null) {
|
|
|
|
JS_ASSERT(JSProto_Null < protoKey);
|
|
|
|
JS_ASSERT(protoKey < JSProto_LIMIT);
|
|
|
|
if (!js_GetClassObject(cx, obj, protoKey, &cobj))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
if (cobj) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*cobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2010-02-19 09:44:23 -08:00
|
|
|
id = ATOM_TO_JSID(cx->runtime->atomState.classAtoms[protoKey]);
|
|
|
|
} else {
|
|
|
|
JSAtom *atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0);
|
|
|
|
if (!atom)
|
|
|
|
return false;
|
|
|
|
id = ATOM_TO_JSID(atom);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-31 22:13:51 -07:00
|
|
|
JS_ASSERT(obj->isNative());
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
if (js_LookupPropertyWithFlags(cx, obj, id, JSRESOLVE_CLASSNAME,
|
|
|
|
&pobj, &prop) < 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v = UndefinedValue();
|
2010-06-05 14:24:54 -07:00
|
|
|
if (prop && pobj->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = (Shape *) prop;
|
|
|
|
if (pobj->containsSlot(shape->slot)) {
|
|
|
|
v = pobj->lockedGetSlot(shape->slot);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isPrimitive())
|
|
|
|
v.setUndefined();
|
2007-12-11 02:09:58 -08:00
|
|
|
}
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-12-11 02:09:58 -08:00
|
|
|
*vp = v;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ConstructObject(JSContext *cx, Class *clasp, JSObject *proto, JSObject *parent,
|
|
|
|
uintN argc, Value *argv)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-28 20:32:20 -07:00
|
|
|
AutoArrayRooter argtvr(cx, argc, argv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-19 09:44:23 -08:00
|
|
|
JSProtoKey protoKey = GetClassProtoKey(clasp);
|
2010-07-14 23:19:36 -07:00
|
|
|
|
|
|
|
/* Protect constructor in case a crazy getter for .prototype uproots it. */
|
|
|
|
AutoValueRooter tvr(cx);
|
|
|
|
if (!js_FindClassObject(cx, parent, protoKey, tvr.addr(), clasp))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &cval = tvr.value();
|
|
|
|
if (tvr.value().isPrimitive()) {
|
|
|
|
js_ReportIsNotFunction(cx, tvr.addr(), JSV2F_CONSTRUCT | JSV2F_SEARCH_STACK);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-04-11 13:51:00 -07:00
|
|
|
* If proto is NULL, set it to Constructor.prototype, just like JSOP_NEW
|
|
|
|
* does, likewise for the new object's parent.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *ctor = &cval.toObject();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!parent)
|
2010-03-04 23:52:52 -08:00
|
|
|
parent = ctor->getParent();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!proto) {
|
2010-07-14 23:19:36 -07:00
|
|
|
Value rval;
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!ctor->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom),
|
|
|
|
&rval)) {
|
2009-09-25 05:30:11 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
if (rval.isObjectOrNull())
|
|
|
|
proto = rval.toObjectOrNull();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-09 09:11:22 -07:00
|
|
|
JSObject *obj = NewObject<WithProto::Class>(cx, clasp, proto, parent);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
2009-09-25 05:30:11 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value rval;
|
2010-08-16 12:35:04 -07:00
|
|
|
if (!InvokeConstructorWithGivenThis(cx, obj, cval, argc, argv, &rval))
|
2009-09-25 05:30:11 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (rval.isPrimitive())
|
2009-09-25 05:30:11 -07:00
|
|
|
return obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the instance's class differs from what was requested, throw a type
|
|
|
|
* error. If the given class has both the JSCLASS_HAS_PRIVATE and the
|
|
|
|
* JSCLASS_CONSTRUCT_PROTOTYPE flags, and the instance does not have its
|
|
|
|
* private data set at this point, then the constructor was replaced and
|
|
|
|
* we should throw a type error.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
obj = &rval.toObject();
|
2010-04-07 16:09:49 -07:00
|
|
|
if (obj->getClass() != clasp ||
|
2007-03-22 10:30:00 -07:00
|
|
|
(!(~clasp->flags & (JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_CONSTRUCT_PROTOTYPE)) &&
|
2009-08-04 14:06:55 -07:00
|
|
|
!obj->getPrivate())) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_WRONG_CONSTRUCTOR, clasp->name);
|
2009-09-25 05:30:11 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
bool
|
|
|
|
JSObject::allocSlot(JSContext *cx, uint32 *slotp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-03 11:05:43 -07:00
|
|
|
uint32 slot = slotSpan();
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(slot >= JSSLOT_FREE(clasp));
|
2009-12-16 16:11:19 -08:00
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
/*
|
|
|
|
* If this object is in dictionary mode and it has a property table, try to
|
|
|
|
* pull a free slot from the property table's slot-number freelist.
|
|
|
|
*/
|
2010-08-18 11:34:13 -07:00
|
|
|
if (inDictionaryMode() && lastProp->table) {
|
2010-09-03 11:05:43 -07:00
|
|
|
uint32 &last = lastProp->table->freelist;
|
2010-08-18 11:34:13 -07:00
|
|
|
if (last != SHAPE_INVALID_SLOT) {
|
2010-09-02 14:50:44 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
JS_ASSERT(last < slot);
|
|
|
|
uint32 next = getSlot(last).toPrivateUint32();
|
|
|
|
JS_ASSERT_IF(next != SHAPE_INVALID_SLOT, next < slot);
|
|
|
|
#endif
|
|
|
|
|
2010-08-18 11:34:13 -07:00
|
|
|
*slotp = last;
|
|
|
|
|
|
|
|
Value &vref = getSlotRef(last);
|
|
|
|
last = vref.toPrivateUint32();
|
|
|
|
vref.setUndefined();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
if (slot >= numSlots() && !growSlots(cx, slot + 1))
|
2010-08-29 11:57:08 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
/* JSObject::growSlots or JSObject::freeSlot should set the free slots to void. */
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(getSlot(slot).isUndefined());
|
|
|
|
*slotp = slot;
|
2010-08-29 11:57:08 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-29 11:57:08 -07:00
|
|
|
JSObject::freeSlot(JSContext *cx, uint32 slot)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-03 11:05:43 -07:00
|
|
|
uint32 limit = slotSpan();
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(slot < limit);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-18 11:34:13 -07:00
|
|
|
Value &vref = getSlotRef(slot);
|
2010-09-02 14:50:44 -07:00
|
|
|
if (inDictionaryMode() && lastProp->table) {
|
2010-09-03 11:05:43 -07:00
|
|
|
uint32 &last = lastProp->table->freelist;
|
2010-08-18 11:34:13 -07:00
|
|
|
|
2010-09-02 14:50:44 -07:00
|
|
|
/* Can't afford to check the whole freelist, but let's check the head. */
|
|
|
|
JS_ASSERT_IF(last != SHAPE_INVALID_SLOT, last < limit && last != slot);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Freeing a slot other than the last one mapped by this object's
|
|
|
|
* shape: push the slot onto the dictionary table's freelist. We want
|
|
|
|
* to let the last slot be freed by shrinking the dslots vector; see
|
|
|
|
* js_TraceObject.
|
|
|
|
*/
|
|
|
|
if (slot + 1 < limit) {
|
2010-09-03 11:05:43 -07:00
|
|
|
JS_ASSERT_IF(last != SHAPE_INVALID_SLOT, last < slotSpan());
|
2010-08-18 11:34:13 -07:00
|
|
|
vref.setPrivateUint32(last);
|
|
|
|
last = slot;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vref.setUndefined();
|
2010-08-29 11:57:08 -07:00
|
|
|
}
|
2009-07-11 15:41:32 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
/* JSBOXEDWORD_INT_MAX as a string */
|
|
|
|
#define JSBOXEDWORD_INT_MAX_STRING "1073741823"
|
2009-07-11 15:41:32 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert string indexes that convert to int jsvals as ints to save memory.
|
|
|
|
* Care must be taken to use this macro every time a property name is used, or
|
|
|
|
* else double-sets, incorrect property cache misses, or other mistakes could
|
|
|
|
* occur.
|
|
|
|
*/
|
2008-02-10 17:12:29 -08:00
|
|
|
jsid
|
2009-07-11 15:41:32 -07:00
|
|
|
js_CheckForStringIndex(jsid id)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-07-11 15:41:32 -07:00
|
|
|
if (!JSID_IS_ATOM(id))
|
|
|
|
return id;
|
|
|
|
|
|
|
|
JSAtom *atom = JSID_TO_ATOM(id);
|
|
|
|
JSString *str = ATOM_TO_STRING(atom);
|
|
|
|
const jschar *s = str->flatChars();
|
|
|
|
jschar ch = *s;
|
|
|
|
|
|
|
|
JSBool negative = (ch == '-');
|
|
|
|
if (negative)
|
|
|
|
ch = *++s;
|
|
|
|
|
|
|
|
if (!JS7_ISDEC(ch))
|
|
|
|
return id;
|
|
|
|
|
|
|
|
size_t n = str->flatLength() - negative;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (n > sizeof(JSBOXEDWORD_INT_MAX_STRING) - 1)
|
2009-07-11 15:41:32 -07:00
|
|
|
return id;
|
|
|
|
|
|
|
|
const jschar *cp = s;
|
|
|
|
const jschar *end = s + n;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
jsuint index = JS7_UNDEC(*cp++);
|
|
|
|
jsuint oldIndex = 0;
|
|
|
|
jsuint c = 0;
|
|
|
|
|
|
|
|
if (index != 0) {
|
|
|
|
while (JS7_ISDEC(*cp)) {
|
|
|
|
oldIndex = index;
|
|
|
|
c = JS7_UNDEC(*cp);
|
|
|
|
index = 10 * index + c;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
}
|
2008-11-21 11:47:35 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-integer indexes can't be represented as integers. Also, distinguish
|
2010-07-14 23:19:36 -07:00
|
|
|
* index "-0" from "0", because JSBOXEDWORD_INT cannot.
|
2008-11-21 11:47:35 -08:00
|
|
|
*/
|
|
|
|
if (cp != end || (negative && index == 0))
|
|
|
|
return id;
|
2009-07-11 15:41:32 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (oldIndex < JSID_INT_MAX / 10 ||
|
|
|
|
(oldIndex == JSID_INT_MAX / 10 && c <= (JSID_INT_MAX % 10))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (negative)
|
|
|
|
index = 0 - index;
|
|
|
|
id = INT_TO_JSID((jsint)index);
|
|
|
|
}
|
2009-07-11 15:41:32 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
static JSBool
|
|
|
|
PurgeProtoChain(JSContext *cx, JSObject *obj, jsid id)
|
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2008-02-07 15:18:45 -08:00
|
|
|
|
|
|
|
while (obj) {
|
2010-03-31 22:13:51 -07:00
|
|
|
if (!obj->isNative()) {
|
2010-03-04 20:44:09 -08:00
|
|
|
obj = obj->getProto();
|
2008-02-07 15:18:45 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = obj->nativeLookup(id);
|
|
|
|
if (shape) {
|
2008-08-21 03:47:33 -07:00
|
|
|
PCMETER(JS_PROPERTY_CACHE(cx).pcpurges++);
|
2010-08-29 11:57:08 -07:00
|
|
|
obj->shadowingShapeChange(cx, *shape);
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-04-06 13:24:34 -07:00
|
|
|
|
2010-03-04 23:52:52 -08:00
|
|
|
if (!obj->getParent()) {
|
2009-04-06 13:24:34 -07:00
|
|
|
/*
|
|
|
|
* All scope chains end in a global object, so this will change
|
|
|
|
* the global shape. jstracer.cpp assumes that the global shape
|
|
|
|
* never changes on trace, so we must deep-bail here.
|
|
|
|
*/
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTrace(cx);
|
2009-04-06 13:24:34 -07:00
|
|
|
}
|
2008-02-07 15:18:45 -08:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
#ifdef JS_THREADSAFE
|
|
|
|
JSObject *pobj = obj;
|
|
|
|
#endif
|
2009-08-27 22:53:26 -07:00
|
|
|
obj = obj->getProto();
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-03-06 16:34:19 -08:00
|
|
|
void
|
|
|
|
js_PurgeScopeChainHelper(JSContext *cx, JSObject *obj, jsid id)
|
2008-02-07 15:18:45 -08:00
|
|
|
{
|
2009-08-27 22:53:26 -07:00
|
|
|
JS_ASSERT(obj->isDelegate());
|
|
|
|
PurgeProtoChain(cx, obj->getProto(), id);
|
2009-05-15 02:43:19 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We must purge the scope chain only for Call objects as they are the only
|
|
|
|
* kind of cacheable non-global object that can gain properties after outer
|
|
|
|
* properties with the same names have been cached or traced. Call objects
|
|
|
|
* may gain such properties via eval introducing new vars; see bug 490364.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->isCall()) {
|
2010-03-04 23:52:52 -08:00
|
|
|
while ((obj = obj->getParent()) != NULL) {
|
2009-05-15 02:43:19 -07:00
|
|
|
if (PurgeProtoChain(cx, obj, id))
|
|
|
|
break;
|
|
|
|
}
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *
|
2007-03-22 10:30:00 -07:00
|
|
|
js_AddNativeProperty(JSContext *cx, JSObject *obj, jsid id,
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyOp getter, PropertyOp setter, uint32 slot,
|
2007-03-22 10:30:00 -07:00
|
|
|
uintN attrs, uintN flags, intN shortid)
|
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(!(flags & Shape::METHOD));
|
2009-09-03 14:41:19 -07:00
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
/*
|
|
|
|
* Purge the property cache of now-shadowed id in obj's scope chain. Do
|
|
|
|
* this optimistically (assuming no failure below) before locking obj, so
|
|
|
|
* we can lock the shadowed scope.
|
|
|
|
*/
|
2009-03-06 16:34:19 -08:00
|
|
|
js_PurgeScopeChain(cx, obj, id);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->ensureClassReservedSlots(cx)) {
|
|
|
|
shape = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2008-02-07 15:18:45 -08:00
|
|
|
/* Convert string indices to integers if appropriate. */
|
2009-07-11 15:41:32 -07:00
|
|
|
id = js_CheckForStringIndex(id);
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = obj->putProperty(cx, id, getter, setter, slot, attrs, flags, shortid);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
return shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *
|
2007-03-22 10:30:00 -07:00
|
|
|
js_ChangeNativePropertyAttrs(JSContext *cx, JSObject *obj,
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape, uintN attrs, uintN mask,
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyOp getter, PropertyOp setter)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = obj->ensureClassReservedSlots(cx)
|
|
|
|
? obj->changeProperty(cx, shape, attrs, mask, getter, setter)
|
|
|
|
: NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
return shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, const Value *value,
|
|
|
|
PropertyOp getter, PropertyOp setter, uintN attrs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
return js_DefineNativeProperty(cx, obj, id, *value, getter, setter, attrs,
|
2009-08-26 14:28:36 -07:00
|
|
|
0, 0, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Backward compatibility requires allowing addProperty hooks to mutate the
|
2010-09-02 14:50:44 -07:00
|
|
|
* nominal initial value of a slotful property, while GC safety wants that
|
2007-03-22 10:30:00 -07:00
|
|
|
* value to be stored before the call-out through the hook. Optimize to do
|
|
|
|
* both while saving cycles for classes that stub their addProperty hook.
|
|
|
|
*/
|
2009-09-03 14:41:19 -07:00
|
|
|
static inline bool
|
2010-08-29 11:57:08 -07:00
|
|
|
CallAddPropertyHook(JSContext *cx, Class *clasp, JSObject *obj, const Shape *shape, Value *vp)
|
2009-09-03 14:41:19 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (clasp->addProperty != PropertyStub) {
|
|
|
|
Value nominal = *vp;
|
2009-09-03 14:41:19 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!CallJSPropertyOp(cx, clasp->addProperty, obj, SHAPE_USERID(shape), vp))
|
2009-09-03 14:41:19 -07:00
|
|
|
return false;
|
|
|
|
if (*vp != nominal) {
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->containsSlot(shape->slot))
|
|
|
|
obj->lockedSetSlot(shape->slot, *vp);
|
2009-09-03 14:41:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-30 15:52:13 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, const Value &value,
|
|
|
|
PropertyOp getter, PropertyOp setter, uintN attrs,
|
2009-02-03 23:14:36 -08:00
|
|
|
uintN flags, intN shortid, JSProperty **propp,
|
2009-05-15 02:43:19 -07:00
|
|
|
uintN defineHow /* = 0 */)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2009-07-21 14:25:11 -07:00
|
|
|
JSBool added;
|
2010-07-14 23:19:36 -07:00
|
|
|
Value valueCopy;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-03 14:41:19 -07:00
|
|
|
JS_ASSERT((defineHow & ~(JSDNP_CACHE_RESULT | JSDNP_DONT_PURGE | JSDNP_SET_METHOD)) == 0);
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTraceIfGlobalObject(cx, obj);
|
2009-04-17 15:25:25 -07:00
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
/* Convert string indices to integers if appropriate. */
|
2009-07-11 15:41:32 -07:00
|
|
|
id = js_CheckForStringIndex(id);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If defining a getter or setter, we must check for its counterpart and
|
|
|
|
* update the attributes and property ops. A getter or setter is really
|
|
|
|
* only half of a property.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (attrs & (JSPROP_GETTER | JSPROP_SETTER)) {
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If JS_THREADSAFE and id is found, js_LookupProperty returns with
|
2010-08-29 11:57:08 -07:00
|
|
|
* shape non-null and pobj locked. If pobj == obj, the property is
|
2007-03-22 10:30:00 -07:00
|
|
|
* already in obj and obj has its own (mutable) scope. So if we are
|
|
|
|
* defining a getter whose setter was already defined, or vice versa,
|
2010-08-29 11:57:08 -07:00
|
|
|
* finish the job via obj->changeProperty, and refresh the property
|
|
|
|
* cache line for (obj, id) to map shape.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
if (!js_LookupProperty(cx, obj, id, &pobj, &prop))
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_FALSE;
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = (Shape *) prop;
|
|
|
|
if (shape && pobj == obj && shape->isAccessorDescriptor()) {
|
|
|
|
shape = obj->changeProperty(cx, shape, attrs,
|
|
|
|
JSPROP_GETTER | JSPROP_SETTER,
|
|
|
|
(attrs & JSPROP_GETTER)
|
|
|
|
? getter
|
|
|
|
: shape->getter(),
|
|
|
|
(attrs & JSPROP_SETTER)
|
|
|
|
? setter
|
|
|
|
: shape->setter());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* NB: obj == pobj, so we can share unlock code at the bottom. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape)
|
2009-04-30 17:54:20 -07:00
|
|
|
goto error;
|
2008-04-23 16:17:45 -07:00
|
|
|
} else if (prop) {
|
2009-08-11 13:05:44 -07:00
|
|
|
pobj->dropProperty(cx, prop);
|
2007-03-22 10:30:00 -07:00
|
|
|
prop = NULL;
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
/*
|
2009-05-15 02:43:19 -07:00
|
|
|
* Purge the property cache of any properties named by id that are about
|
|
|
|
* to be shadowed in obj's scope chain unless it is known a priori that it
|
|
|
|
* is not possible. We do this before locking obj to avoid nesting locks.
|
2009-05-13 17:11:23 -07:00
|
|
|
*/
|
2009-05-15 02:43:19 -07:00
|
|
|
if (!(defineHow & JSDNP_DONT_PURGE))
|
|
|
|
js_PurgeScopeChain(cx, obj, id);
|
2009-05-13 17:11:23 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether a readonly property or setter is being defined on a known
|
|
|
|
* prototype object. See the comment in jscntxt.h before protoHazardShape's
|
|
|
|
* member declaration.
|
2008-02-07 15:18:45 -08:00
|
|
|
*/
|
2009-08-27 22:53:26 -07:00
|
|
|
if (obj->isDelegate() && (attrs & (JSPROP_READONLY | JSPROP_SETTER)))
|
2009-05-13 17:11:23 -07:00
|
|
|
cx->runtime->protoHazardShape = js_GenerateShape(cx, false);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Lock if object locking is required by this implementation. */
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
|
|
|
|
|
|
|
/* Use the object's class getter and setter by default. */
|
2009-08-27 22:53:26 -07:00
|
|
|
clasp = obj->getClass();
|
2009-09-03 14:41:19 -07:00
|
|
|
if (!(defineHow & JSDNP_SET_METHOD)) {
|
2010-04-30 16:03:37 -07:00
|
|
|
if (!getter && !(attrs & JSPROP_GETTER))
|
2009-09-03 14:41:19 -07:00
|
|
|
getter = clasp->getProperty;
|
2010-04-30 16:03:37 -07:00
|
|
|
if (!setter && !(attrs & JSPROP_SETTER))
|
2009-09-03 14:41:19 -07:00
|
|
|
setter = clasp->setProperty;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Get obj's own scope if it has one, or create a new one for obj. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->ensureClassReservedSlots(cx))
|
2009-04-30 17:54:20 -07:00
|
|
|
goto error;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-29 07:07:21 -07:00
|
|
|
added = false;
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape) {
|
2008-09-03 13:30:13 -07:00
|
|
|
/* Add a new property, or replace an existing one of the same id. */
|
2009-09-03 14:41:19 -07:00
|
|
|
if (defineHow & JSDNP_SET_METHOD) {
|
|
|
|
JS_ASSERT(clasp == &js_ObjectClass);
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(IsFunctionObject(value));
|
2009-09-03 14:41:19 -07:00
|
|
|
JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
|
|
|
|
JS_ASSERT(!getter && !setter);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *funobj = &value.toObject();
|
2009-09-03 14:41:19 -07:00
|
|
|
if (FUN_OBJECT(GET_FUNCTION_PRIVATE(cx, funobj)) == funobj) {
|
2010-08-29 11:57:08 -07:00
|
|
|
flags |= Shape::METHOD;
|
2010-04-30 16:03:37 -07:00
|
|
|
getter = CastAsPropertyOp(funobj);
|
2009-09-03 14:41:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
added = !obj->nativeContains(id);
|
|
|
|
uint32 oldShape = obj->shape();
|
|
|
|
shape = obj->putProperty(cx, id, getter, setter, SHAPE_INVALID_SLOT,
|
|
|
|
attrs, flags, shortid);
|
|
|
|
if (!shape)
|
2009-04-30 17:54:20 -07:00
|
|
|
goto error;
|
2010-04-30 06:29:04 -07:00
|
|
|
|
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* If shape is a method, the above call to putProperty suffices to
|
2010-04-30 06:29:04 -07:00
|
|
|
* update the shape if necessary. But if scope->branded(), the shape
|
|
|
|
* may not have changed and we may be overwriting a function-valued
|
|
|
|
* property. See bug 560998.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->shape() == oldShape && obj->branded() && shape->slot != SHAPE_INVALID_SLOT)
|
|
|
|
obj->methodWriteBarrier(cx, shape->slot, value);
|
2008-04-23 16:17:45 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Store value before calling addProperty, in case the latter GC's. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->containsSlot(shape->slot))
|
|
|
|
obj->lockedSetSlot(shape->slot, value);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* XXXbe called with lock held */
|
2010-07-14 23:19:36 -07:00
|
|
|
valueCopy = value;
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!CallAddPropertyHook(cx, clasp, obj, shape, &valueCopy)) {
|
|
|
|
obj->removeProperty(cx, id);
|
2009-09-03 14:41:19 -07:00
|
|
|
goto error;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-05-15 02:43:19 -07:00
|
|
|
if (defineHow & JSDNP_CACHE_RESULT) {
|
2010-07-14 23:19:36 -07:00
|
|
|
#ifdef JS_TRACER
|
2009-02-03 23:14:36 -08:00
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
2010-06-02 16:01:36 -07:00
|
|
|
PropertyCacheEntry *entry =
|
2010-07-14 23:19:36 -07:00
|
|
|
#endif
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_PROPERTY_CACHE(cx).fill(cx, obj, 0, 0, obj, shape, added);
|
|
|
|
TRACE_2(SetPropHit, entry, shape);
|
2009-02-03 23:14:36 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (propp)
|
2010-08-29 11:57:08 -07:00
|
|
|
*propp = (JSProperty *) shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-30 17:54:20 -07:00
|
|
|
error: // TRACE_2 jumps here on error, as well.
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(JSBool)
|
|
|
|
js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
|
|
|
|
JSProperty **propp)
|
|
|
|
{
|
2008-09-25 09:13:31 -07:00
|
|
|
return js_LookupPropertyWithFlags(cx, obj, id, cx->resolveFlags,
|
|
|
|
objp, propp) >= 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-20 13:13:17 -07:00
|
|
|
#define SCOPE_DEPTH_ACCUM(bs,val) \
|
|
|
|
JS_SCOPE_DEPTH_METERING(JS_BASIC_STATS_ACCUM(bs, val))
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
|
2010-06-02 23:11:31 -07:00
|
|
|
/*
|
|
|
|
* Call obj's resolve hook. obj is a native object and the caller holds its
|
|
|
|
* scope lock.
|
|
|
|
*
|
|
|
|
* cx, start, id, and flags are the parameters initially passed to the ongoing
|
|
|
|
* lookup; objp and propp are its out parameters. obj is an object along
|
|
|
|
* start's prototype chain.
|
|
|
|
*
|
|
|
|
* There are four possible outcomes:
|
|
|
|
*
|
|
|
|
* - On failure, report an error or exception, unlock obj, and return false.
|
|
|
|
*
|
|
|
|
* - If we are alrady resolving a property of *curobjp, set *recursedp = true,
|
|
|
|
* unlock obj, and return true.
|
|
|
|
*
|
|
|
|
* - If the resolve hook finds or defines the sought property, set *objp and
|
|
|
|
* *propp appropriately, set *recursedp = false, and return true with *objp's
|
|
|
|
* lock held.
|
|
|
|
*
|
|
|
|
* - Otherwise no property was resolved. Set *propp = NULL and *recursedp = false
|
|
|
|
* and return true.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
CallResolveOp(JSContext *cx, JSObject *start, JSObject *obj, jsid id, uintN flags,
|
|
|
|
JSObject **objp, JSProperty **propp, bool *recursedp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = obj->getClass();
|
2010-06-02 23:11:31 -07:00
|
|
|
JSResolveOp resolve = clasp->resolve;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid recursion on (obj, id) already being resolved on cx.
|
|
|
|
*
|
|
|
|
* Once we have successfully added an entry for (obj, key) to
|
|
|
|
* cx->resolvingTable, control must go through cleanup: before
|
|
|
|
* returning. But note that JS_DHASH_ADD may find an existing
|
|
|
|
* entry, in which case we bail to suppress runaway recursion.
|
|
|
|
*/
|
|
|
|
JSResolvingKey key = {obj, id};
|
2007-03-22 10:30:00 -07:00
|
|
|
JSResolvingEntry *entry;
|
2010-06-02 23:11:31 -07:00
|
|
|
if (!js_StartResolving(cx, &key, JSRESFLAG_LOOKUP, &entry)) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!entry) {
|
|
|
|
/* Already resolving id in obj -- suppress recursion. */
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
*recursedp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
uint32 generation = cx->resolvingTable->generation;
|
|
|
|
*recursedp = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-02 23:11:31 -07:00
|
|
|
*propp = NULL;
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2010-06-02 23:11:31 -07:00
|
|
|
JSBool ok;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape = NULL;
|
2010-06-02 23:11:31 -07:00
|
|
|
if (clasp->flags & JSCLASS_NEW_RESOLVE) {
|
|
|
|
JSNewResolveOp newresolve = (JSNewResolveOp)resolve;
|
|
|
|
if (flags == JSRESOLVE_INFER)
|
2010-08-29 11:57:08 -07:00
|
|
|
flags = js_InferFlags(cx, 0);
|
2010-06-02 23:11:31 -07:00
|
|
|
JSObject *obj2 = (clasp->flags & JSCLASS_NEW_RESOLVE_GETS_START) ? start : NULL;
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-02 23:11:31 -07:00
|
|
|
{
|
|
|
|
/* Protect id and all atoms from a GC nested in resolve. */
|
|
|
|
AutoKeepAtoms keep(cx->runtime);
|
2010-07-14 23:19:36 -07:00
|
|
|
ok = newresolve(cx, obj, id, flags, &obj2);
|
2010-06-02 23:11:31 -07:00
|
|
|
}
|
|
|
|
if (!ok)
|
|
|
|
goto cleanup;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-02 23:11:31 -07:00
|
|
|
JS_LOCK_OBJ(cx, obj);
|
|
|
|
if (obj2) {
|
|
|
|
/* Resolved: juggle locks and lookup id again. */
|
|
|
|
if (obj2 != obj) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
if (obj2->isNative())
|
|
|
|
JS_LOCK_OBJ(cx, obj2);
|
|
|
|
}
|
|
|
|
if (!obj2->isNative()) {
|
|
|
|
/* Whoops, newresolve handed back a foreign obj2. */
|
|
|
|
JS_ASSERT(obj2 != obj);
|
|
|
|
ok = obj2->lookupProperty(cx, id, objp, propp);
|
|
|
|
if (!ok || *propp)
|
|
|
|
goto cleanup;
|
|
|
|
JS_LOCK_OBJ(cx, obj2);
|
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* Require that obj2 not be empty now, as we do for old-style
|
|
|
|
* resolve. If it doesn't, then id was not truly resolved, and
|
|
|
|
* we'll find it in the proto chain, or miss it if obj2's proto
|
|
|
|
* is not on obj's proto chain. That last case is a "too bad!"
|
|
|
|
* case.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj2->nativeEmpty())
|
|
|
|
shape = obj2->nativeLookup(id);
|
2010-06-02 23:11:31 -07:00
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape) {
|
|
|
|
JS_ASSERT(!obj2->nativeEmpty());
|
2010-06-02 23:11:31 -07:00
|
|
|
obj = obj2;
|
|
|
|
} else if (obj2 != obj) {
|
|
|
|
if (obj2->isNative())
|
|
|
|
JS_UNLOCK_OBJ(cx, obj2);
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2010-06-02 23:11:31 -07:00
|
|
|
} else {
|
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* Old resolve always requires id re-lookup if obj is not empty after
|
|
|
|
* resolve returns.
|
2010-06-02 23:11:31 -07:00
|
|
|
*/
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-07-14 23:19:36 -07:00
|
|
|
ok = resolve(cx, obj, id);
|
2010-06-02 23:11:31 -07:00
|
|
|
if (!ok)
|
|
|
|
goto cleanup;
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
|
|
|
JS_ASSERT(obj->isNative());
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->nativeEmpty())
|
|
|
|
shape = obj->nativeLookup(id);
|
2010-06-02 23:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2010-08-29 11:57:08 -07:00
|
|
|
if (ok && shape) {
|
2010-06-02 23:11:31 -07:00
|
|
|
*objp = obj;
|
2010-08-29 11:57:08 -07:00
|
|
|
*propp = (JSProperty *) shape;
|
2010-06-02 23:11:31 -07:00
|
|
|
}
|
|
|
|
js_StopResolving(cx, &key, JSRESFLAG_LOOKUP, entry, generation);
|
|
|
|
return ok;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-02 23:11:31 -07:00
|
|
|
int
|
|
|
|
js_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, jsid id, uintN flags,
|
|
|
|
JSObject **objp, JSProperty **propp)
|
|
|
|
{
|
|
|
|
/* Convert string indices to integers if appropriate. */
|
|
|
|
id = js_CheckForStringIndex(id);
|
|
|
|
|
|
|
|
/* Search scopes starting with obj and following the prototype link. */
|
|
|
|
JSObject *start = obj;
|
|
|
|
int protoIndex;
|
|
|
|
for (protoIndex = 0; ; protoIndex++) {
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape = obj->nativeLookup(id);
|
|
|
|
if (shape) {
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
SCOPE_DEPTH_ACCUM(&cx->runtime->protoLookupDepthStats, protoIndex);
|
2009-07-21 14:25:11 -07:00
|
|
|
*objp = obj;
|
2010-08-29 11:57:08 -07:00
|
|
|
*propp = (JSProperty *) shape;
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
return protoIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-02 23:11:31 -07:00
|
|
|
/* Try obj's class resolve hook if id was not found in obj's scope. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape && obj->getClass()->resolve != JS_ResolveStub) {
|
2010-06-02 23:11:31 -07:00
|
|
|
bool recursed;
|
|
|
|
if (!CallResolveOp(cx, start, obj, id, flags, objp, propp, &recursed))
|
|
|
|
return -1;
|
|
|
|
if (recursed)
|
|
|
|
break;
|
|
|
|
if (*propp) {
|
|
|
|
/* Recalculate protoIndex in case it was resolved on some other object. */
|
|
|
|
protoIndex = 0;
|
|
|
|
for (JSObject *proto = start; proto && proto != *objp; proto = proto->getProto())
|
|
|
|
protoIndex++;
|
|
|
|
SCOPE_DEPTH_ACCUM(&cx->runtime->protoLookupDepthStats, protoIndex);
|
|
|
|
return protoIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *proto = obj->getProto();
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
if (!proto)
|
|
|
|
break;
|
2010-03-31 22:13:51 -07:00
|
|
|
if (!proto->isNative()) {
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!proto->lookupProperty(cx, id, objp, propp))
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
return -1;
|
2008-03-02 23:02:53 -08:00
|
|
|
return protoIndex + 1;
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
}
|
2009-07-21 14:25:11 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
obj = proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
*objp = NULL;
|
|
|
|
*propp = NULL;
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
return protoIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 19:20:44 -07:00
|
|
|
PropertyCacheEntry *
|
2009-04-20 17:00:59 -07:00
|
|
|
js_FindPropertyHelper(JSContext *cx, jsid id, JSBool cacheResult,
|
|
|
|
JSObject **objp, JSObject **pobjp, JSProperty **propp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-04-18 11:10:59 -07:00
|
|
|
JSObject *scopeChain, *obj, *parent, *pobj;
|
2010-03-24 19:20:44 -07:00
|
|
|
PropertyCacheEntry *entry;
|
2008-02-07 15:18:45 -08:00
|
|
|
int scopeIndex, protoIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSProperty *prop;
|
|
|
|
|
2009-04-20 17:00:59 -07:00
|
|
|
JS_ASSERT_IF(cacheResult, !JS_ON_TRACE(cx));
|
2010-08-09 22:43:33 -07:00
|
|
|
scopeChain = &js_GetTopStackFrame(cx)->scopeChain();
|
2009-04-18 11:10:59 -07:00
|
|
|
|
|
|
|
/* Scan entries on the scope chain that we can cache across. */
|
2009-04-20 17:00:59 -07:00
|
|
|
entry = JS_NO_PROP_CACHE_FILL;
|
2009-04-18 11:10:59 -07:00
|
|
|
obj = scopeChain;
|
2010-03-04 23:52:52 -08:00
|
|
|
parent = obj->getParent();
|
2009-04-18 11:10:59 -07:00
|
|
|
for (scopeIndex = 0;
|
|
|
|
parent
|
2009-08-06 12:40:51 -07:00
|
|
|
? js_IsCacheableNonGlobalScope(obj)
|
2010-06-12 09:29:04 -07:00
|
|
|
: !obj->getOps()->lookupProperty;
|
2009-04-18 11:10:59 -07:00
|
|
|
++scopeIndex) {
|
|
|
|
protoIndex =
|
|
|
|
js_LookupPropertyWithFlags(cx, obj, id, cx->resolveFlags,
|
|
|
|
&pobj, &prop);
|
|
|
|
if (protoIndex < 0)
|
2009-04-20 17:00:59 -07:00
|
|
|
return NULL;
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (prop) {
|
2009-04-18 11:10:59 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (parent) {
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = obj->getClass();
|
2010-03-31 22:13:51 -07:00
|
|
|
JS_ASSERT(pobj->isNative());
|
2010-04-07 16:09:49 -07:00
|
|
|
JS_ASSERT(pobj->getClass() == clasp);
|
2009-04-18 11:10:59 -07:00
|
|
|
if (clasp == &js_BlockClass) {
|
|
|
|
/*
|
2010-09-02 14:50:44 -07:00
|
|
|
* A block instance on the scope chain is immutable and it
|
|
|
|
* shares its shapes with its compile-time prototype.
|
2009-04-18 11:10:59 -07:00
|
|
|
*/
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(pobj == obj);
|
|
|
|
JS_ASSERT(pobj->isClonedBlock());
|
|
|
|
JS_ASSERT(protoIndex == 0);
|
2008-02-07 15:18:45 -08:00
|
|
|
} else {
|
2009-04-18 11:10:59 -07:00
|
|
|
/* Call and DeclEnvClass objects have no prototypes. */
|
2010-03-04 20:44:09 -08:00
|
|
|
JS_ASSERT(!obj->getProto());
|
2009-04-18 11:10:59 -07:00
|
|
|
JS_ASSERT(protoIndex == 0);
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
2010-05-18 07:51:11 -07:00
|
|
|
} else {
|
|
|
|
JS_ASSERT(obj->isNative());
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
2009-04-18 11:10:59 -07:00
|
|
|
#endif
|
2010-05-18 07:51:11 -07:00
|
|
|
/*
|
|
|
|
* We must check if pobj is native as a global object can have
|
|
|
|
* non-native prototype.
|
|
|
|
*/
|
|
|
|
if (cacheResult && pobj->isNative()) {
|
|
|
|
entry = JS_PROPERTY_CACHE(cx).fill(cx, scopeChain, scopeIndex,
|
|
|
|
protoIndex, pobj,
|
2010-08-29 11:57:08 -07:00
|
|
|
(Shape *) prop);
|
2009-04-18 11:10:59 -07:00
|
|
|
}
|
2009-12-14 18:36:58 -08:00
|
|
|
SCOPE_DEPTH_ACCUM(&cx->runtime->scopeSearchDepthStats, scopeIndex);
|
2009-04-18 11:10:59 -07:00
|
|
|
goto out;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2009-04-18 11:10:59 -07:00
|
|
|
if (!parent) {
|
|
|
|
pobj = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
obj = parent;
|
2010-03-04 23:52:52 -08:00
|
|
|
parent = obj->getParent();
|
2009-04-18 11:10:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj->lookupProperty(cx, id, &pobj, &prop))
|
2009-04-20 17:00:59 -07:00
|
|
|
return NULL;
|
2009-04-18 11:10:59 -07:00
|
|
|
if (prop) {
|
|
|
|
PCMETER(JS_PROPERTY_CACHE(cx).nofills++);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We conservatively assume that a resolve hook could mutate the scope
|
2009-08-11 13:05:44 -07:00
|
|
|
* chain during JSObject::lookupProperty. So we read parent here again.
|
2009-04-18 11:10:59 -07:00
|
|
|
*/
|
2010-03-04 23:52:52 -08:00
|
|
|
parent = obj->getParent();
|
2009-04-18 11:10:59 -07:00
|
|
|
if (!parent) {
|
|
|
|
pobj = NULL;
|
2008-02-07 15:18:45 -08:00
|
|
|
break;
|
2009-04-18 11:10:59 -07:00
|
|
|
}
|
|
|
|
obj = parent;
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-18 11:10:59 -07:00
|
|
|
out:
|
|
|
|
JS_ASSERT(!!pobj == !!prop);
|
|
|
|
*objp = obj;
|
|
|
|
*pobjp = pobj;
|
|
|
|
*propp = prop;
|
2009-04-20 17:00:59 -07:00
|
|
|
return entry;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
JS_FRIEND_API(JSBool)
|
|
|
|
js_FindProperty(JSContext *cx, jsid id, JSObject **objp, JSObject **pobjp,
|
|
|
|
JSProperty **propp)
|
|
|
|
{
|
2009-04-20 17:00:59 -07:00
|
|
|
return !!js_FindPropertyHelper(cx, id, false, objp, pobjp, propp);
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
|
|
|
|
2009-04-14 03:54:37 -07:00
|
|
|
JSObject *
|
2009-04-20 17:00:59 -07:00
|
|
|
js_FindIdentifierBase(JSContext *cx, JSObject *scopeChain, jsid id)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/*
|
2009-04-11 01:11:06 -07:00
|
|
|
* This function should not be called for a global object or from the
|
|
|
|
* trace and should have a valid cache entry for native scopeChain.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-03-04 23:52:52 -08:00
|
|
|
JS_ASSERT(scopeChain->getParent());
|
2009-04-11 01:11:06 -07:00
|
|
|
JS_ASSERT(!JS_ON_TRACE(cx));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-11 01:11:06 -07:00
|
|
|
JSObject *obj = scopeChain;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-14 03:54:37 -07:00
|
|
|
/*
|
|
|
|
* Loop over cacheable objects on the scope chain until we find a
|
|
|
|
* property. We also stop when we reach the global object skipping any
|
|
|
|
* farther checks or lookups. For details see the JSOP_BINDNAME case of
|
|
|
|
* js_Interpret.
|
2010-04-26 07:06:30 -07:00
|
|
|
*
|
|
|
|
* The test order here matters because js_IsCacheableNonGlobalScope
|
|
|
|
* must not be passed a global object (i.e. one with null parent).
|
2009-04-14 03:54:37 -07:00
|
|
|
*/
|
2010-04-26 07:06:30 -07:00
|
|
|
for (int scopeIndex = 0;
|
|
|
|
!obj->getParent() || js_IsCacheableNonGlobalScope(obj);
|
|
|
|
scopeIndex++) {
|
2009-04-11 01:11:06 -07:00
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2009-04-14 03:54:37 -07:00
|
|
|
int protoIndex = js_LookupPropertyWithFlags(cx, obj, id,
|
|
|
|
cx->resolveFlags,
|
2009-04-11 01:11:06 -07:00
|
|
|
&pobj, &prop);
|
|
|
|
if (protoIndex < 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2009-04-11 01:11:06 -07:00
|
|
|
if (prop) {
|
2010-05-18 07:51:11 -07:00
|
|
|
if (!pobj->isNative()) {
|
|
|
|
JS_ASSERT(!obj->getParent());
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
JS_ASSERT_IF(obj->getParent(), pobj->getClass() == obj->getClass());
|
2009-04-20 17:00:59 -07:00
|
|
|
#ifdef DEBUG
|
2010-03-24 19:20:44 -07:00
|
|
|
PropertyCacheEntry *entry =
|
2009-04-20 17:00:59 -07:00
|
|
|
#endif
|
2010-03-24 19:20:44 -07:00
|
|
|
JS_PROPERTY_CACHE(cx).fill(cx, scopeChain, scopeIndex, protoIndex, pobj,
|
2010-08-29 11:57:08 -07:00
|
|
|
(Shape *) prop);
|
2009-04-20 17:00:59 -07:00
|
|
|
JS_ASSERT(entry);
|
2009-04-11 01:11:06 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-04-26 07:06:30 -07:00
|
|
|
JSObject *parent = obj->getParent();
|
|
|
|
if (!parent)
|
2009-04-11 01:11:06 -07:00
|
|
|
return obj;
|
2010-04-26 07:06:30 -07:00
|
|
|
obj = parent;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2009-04-14 03:54:37 -07:00
|
|
|
/* Loop until we find a property or reach the global object. */
|
2009-04-11 01:11:06 -07:00
|
|
|
do {
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj->lookupProperty(cx, id, &pobj, &prop))
|
2009-04-11 01:11:06 -07:00
|
|
|
return NULL;
|
|
|
|
if (prop) {
|
2009-08-11 13:05:44 -07:00
|
|
|
pobj->dropProperty(cx, prop);
|
2009-04-11 01:11:06 -07:00
|
|
|
break;
|
|
|
|
}
|
2009-04-14 03:54:37 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We conservatively assume that a resolve hook could mutate the scope
|
2009-08-11 13:05:44 -07:00
|
|
|
* chain during JSObject::lookupProperty. So we must check if parent is
|
|
|
|
* not null here even if it wasn't before the lookup.
|
2009-04-14 03:54:37 -07:00
|
|
|
*/
|
2010-03-04 23:52:52 -08:00
|
|
|
JSObject *parent = obj->getParent();
|
2009-04-14 03:54:37 -07:00
|
|
|
if (!parent)
|
|
|
|
break;
|
2009-04-11 01:11:06 -07:00
|
|
|
obj = parent;
|
2010-03-04 23:52:52 -08:00
|
|
|
} while (obj->getParent());
|
2007-03-22 10:30:00 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-08-29 11:57:08 -07:00
|
|
|
js_NativeGet(JSContext *cx, JSObject *obj, JSObject *pobj, const Shape *shape, uintN getHow,
|
|
|
|
Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTraceIfGlobalObject(cx, pobj);
|
2009-03-17 10:39:51 -07:00
|
|
|
|
2009-04-17 12:41:00 -07:00
|
|
|
uint32 slot;
|
|
|
|
int32 sample;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-31 22:13:51 -07:00
|
|
|
JS_ASSERT(pobj->isNative());
|
2009-04-17 12:41:00 -07:00
|
|
|
JS_ASSERT(JS_IS_OBJ_LOCKED(cx, pobj));
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
slot = shape->slot;
|
|
|
|
if (slot != SHAPE_INVALID_SLOT)
|
2010-07-14 23:19:36 -07:00
|
|
|
*vp = pobj->lockedGetSlot(slot);
|
|
|
|
else
|
|
|
|
vp->setUndefined();
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape->hasDefaultGetter())
|
2009-09-03 14:41:19 -07:00
|
|
|
return true;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (JS_UNLIKELY(shape->isMethod()) && (getHow & JSGET_NO_METHOD_BARRIER)) {
|
|
|
|
JS_ASSERT(&shape->methodObject() == &vp->toObject());
|
2009-09-03 14:41:19 -07:00
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-04-17 12:41:00 -07:00
|
|
|
sample = cx->runtime->propertyRemovals;
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2010-01-11 09:52:21 -08:00
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
AutoShapeRooter tvr(cx, shape);
|
2010-07-14 23:19:36 -07:00
|
|
|
AutoObjectRooter tvr2(cx, pobj);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->get(cx, obj, pobj, vp))
|
2010-01-11 09:52:21 -08:00
|
|
|
return false;
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_LOCK_OBJ(cx, pobj);
|
2010-01-11 09:52:21 -08:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (pobj->containsSlot(slot) &&
|
2009-04-17 12:41:00 -07:00
|
|
|
(JS_LIKELY(cx->runtime->propertyRemovals == sample) ||
|
2010-08-29 11:57:08 -07:00
|
|
|
pobj->nativeContains(*shape))) {
|
|
|
|
if (!pobj->methodWriteBarrier(cx, *shape, *vp)) {
|
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2009-09-03 14:41:19 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
pobj->lockedSetSlot(slot, *vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-03 14:41:19 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-08-29 11:57:08 -07:00
|
|
|
js_NativeSet(JSContext *cx, JSObject *obj, const Shape *shape, bool added, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTraceIfGlobalObject(cx, obj);
|
2009-04-17 12:41:00 -07:00
|
|
|
|
|
|
|
uint32 slot;
|
|
|
|
int32 sample;
|
|
|
|
|
2010-03-31 22:13:51 -07:00
|
|
|
JS_ASSERT(obj->isNative());
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(JS_IS_OBJ_LOCKED(cx, obj));
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
slot = shape->slot;
|
|
|
|
if (slot != SHAPE_INVALID_SLOT) {
|
2009-04-17 12:41:00 -07:00
|
|
|
OBJ_CHECK_SLOT(obj, slot);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
/* If shape has a stub setter, keep obj locked and just store *vp. */
|
|
|
|
if (shape->hasDefaultSetter()) {
|
|
|
|
if (!added && !obj->methodWriteBarrier(cx, *shape, *vp)) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-09-03 14:41:19 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-04-08 11:22:04 -07:00
|
|
|
obj->lockedSetSlot(slot, *vp);
|
2009-09-03 14:41:19 -07:00
|
|
|
return true;
|
|
|
|
}
|
2009-04-17 12:41:00 -07:00
|
|
|
} else {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Allow API consumers to create shared properties with stub setters.
|
2009-12-08 17:00:42 -08:00
|
|
|
* Such properties effectively function as data descriptors which are
|
|
|
|
* not writable, so attempting to set such a property should do nothing
|
|
|
|
* or throw if we're in strict mode.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->hasGetterValue() && shape->hasDefaultSetter())
|
2009-12-08 17:00:42 -08:00
|
|
|
return js_ReportGetterOnlyAssignment(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-17 12:41:00 -07:00
|
|
|
sample = cx->runtime->propertyRemovals;
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-01-11 09:52:21 -08:00
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
AutoShapeRooter tvr(cx, shape);
|
|
|
|
if (!shape->set(cx, obj, vp))
|
2010-01-11 09:52:21 -08:00
|
|
|
return false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_LOCK_OBJ(cx, obj);
|
|
|
|
if (obj->containsSlot(slot) &&
|
2009-04-17 12:41:00 -07:00
|
|
|
(JS_LIKELY(cx->runtime->propertyRemovals == sample) ||
|
2010-08-29 11:57:08 -07:00
|
|
|
obj->nativeContains(*shape))) {
|
|
|
|
if (!added && !obj->methodWriteBarrier(cx, *shape, *vp)) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-09-03 14:41:19 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
obj->lockedSetSlot(slot, *vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-03 14:41:19 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-09-15 21:33:17 -07:00
|
|
|
JS_ALWAYS_INLINE bool
|
|
|
|
js_GetPropertyHelperWithShapeInline(JSContext *cx, JSObject *obj, jsid id,
|
|
|
|
uintN getHow, Value *vp,
|
|
|
|
const Shape **shapeOut, JSObject **holderOut)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-03-09 11:25:43 -07:00
|
|
|
JSObject *aobj, *obj2;
|
2008-02-07 15:18:45 -08:00
|
|
|
int protoIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSProperty *prop;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-03 14:41:19 -07:00
|
|
|
JS_ASSERT_IF(getHow & JSGET_CACHE_RESULT, !JS_ON_TRACE(cx));
|
|
|
|
|
2010-09-15 21:33:17 -07:00
|
|
|
*shapeOut = NULL;
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
/* Convert string indices to integers if appropriate. */
|
2009-07-11 15:41:32 -07:00
|
|
|
id = js_CheckForStringIndex(id);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2010-03-04 20:44:09 -08:00
|
|
|
aobj = js_GetProtoIfDenseArray(obj);
|
2009-03-09 11:25:43 -07:00
|
|
|
protoIndex = js_LookupPropertyWithFlags(cx, aobj, id, cx->resolveFlags,
|
2008-09-25 09:13:31 -07:00
|
|
|
&obj2, &prop);
|
2008-02-07 15:18:45 -08:00
|
|
|
if (protoIndex < 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-09-15 21:33:17 -07:00
|
|
|
|
|
|
|
*holderOut = obj2;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!prop) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!CallJSPropertyOp(cx, obj->getClass()->getProperty, obj, id, vp))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-09-03 14:41:19 -07:00
|
|
|
PCMETER(getHow & JSGET_CACHE_RESULT && JS_PROPERTY_CACHE(cx).nofills++);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Give a strict warning if foo.bar is evaluated by a script for an
|
|
|
|
* object foo with no property named 'bar'.
|
|
|
|
*/
|
2009-02-02 17:25:59 -08:00
|
|
|
jsbytecode *pc;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (vp->isUndefined() && ((pc = js_GetCurrentBytecodePC(cx)) != NULL)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JSOp op;
|
|
|
|
uintN flags;
|
|
|
|
|
2007-07-05 13:37:47 -07:00
|
|
|
op = (JSOp) *pc;
|
2009-02-18 16:31:09 -08:00
|
|
|
if (op == JSOP_TRAP) {
|
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
2010-08-09 22:43:33 -07:00
|
|
|
op = JS_GetTrapOpcode(cx, cx->fp()->script(), pc);
|
2009-02-18 16:31:09 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if (op == JSOP_GETXPROP) {
|
|
|
|
flags = JSREPORT_ERROR;
|
|
|
|
} else {
|
|
|
|
if (!JS_HAS_STRICT_OPTION(cx) ||
|
2009-08-13 14:07:13 -07:00
|
|
|
(op != JSOP_GETPROP && op != JSOP_GETELEM) ||
|
2009-08-20 12:13:21 -07:00
|
|
|
js_CurrentPCIsInImacro(cx)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX do not warn about missing __iterator__ as the function
|
|
|
|
* may be called from JS_GetMethodById. See bug 355145.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_ATOM(id, cx->runtime->atomState.iteratorAtom))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
|
2009-02-18 11:14:41 -08:00
|
|
|
/* Do not warn about tests like (obj[prop] == undefined). */
|
|
|
|
if (cx->resolveFlags == JSRESOLVE_INFER) {
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTrace(cx);
|
2009-02-18 11:14:41 -08:00
|
|
|
pc += js_CodeSpec[op].length;
|
|
|
|
if (Detecting(cx, pc))
|
|
|
|
return JS_TRUE;
|
|
|
|
} else if (cx->resolveFlags & JSRESOLVE_DETECTING) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
2009-02-18 11:14:41 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
flags = JSREPORT_WARNING | JSREPORT_STRICT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ok, bad undefined property reference: whine about it. */
|
|
|
|
if (!js_ReportValueErrorFlags(cx, flags, JSMSG_UNDEFINED_PROP,
|
2010-07-14 23:19:36 -07:00
|
|
|
JSDVG_IGNORE_STACK, IdToValue(id),
|
2007-03-22 10:30:00 -07:00
|
|
|
NULL, NULL, NULL)) {
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-05 14:24:54 -07:00
|
|
|
if (!obj2->isNative())
|
2009-08-11 13:05:44 -07:00
|
|
|
return obj2->getProperty(cx, id, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = (Shape *) prop;
|
2010-09-15 21:33:17 -07:00
|
|
|
*shapeOut = shape;
|
2009-04-17 12:41:00 -07:00
|
|
|
|
2009-09-03 14:41:19 -07:00
|
|
|
if (getHow & JSGET_CACHE_RESULT) {
|
2009-01-30 15:40:05 -08:00
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_PROPERTY_CACHE(cx).fill(cx, aobj, 0, protoIndex, obj2, shape);
|
2008-12-09 08:38:32 -08:00
|
|
|
}
|
2009-04-30 15:52:13 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!js_NativeGet(cx, obj, obj2, shape, getHow, vp))
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-04-17 12:41:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj2);
|
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-09-15 21:33:17 -07:00
|
|
|
bool
|
|
|
|
js_GetPropertyHelperWithShape(JSContext *cx, JSObject *obj, jsid id,
|
|
|
|
uintN getHow, Value *vp,
|
|
|
|
const Shape **shapeOut, JSObject **holderOut)
|
|
|
|
{
|
|
|
|
return js_GetPropertyHelperWithShapeInline(cx, obj, id, getHow, vp, shapeOut, holderOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_GetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uint32 getHow, Value *vp)
|
|
|
|
{
|
|
|
|
const Shape *shape;
|
|
|
|
JSObject *holder;
|
|
|
|
return js_GetPropertyHelperWithShapeInline(cx, obj, id, getHow, vp, &shape, &holder);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_GetProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
2008-02-07 15:18:45 -08:00
|
|
|
{
|
2009-09-03 14:41:19 -07:00
|
|
|
return js_GetPropertyHelper(cx, obj, id, JSGET_METHOD_BARRIER, vp);
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
|
|
|
|
2009-03-14 09:41:47 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_GetMethod(JSContext *cx, JSObject *obj, jsid id, uintN getHow, Value *vp)
|
2009-03-14 09:41:47 -07:00
|
|
|
{
|
2009-06-29 15:19:37 -07:00
|
|
|
JSAutoResolveFlags rf(cx, JSRESOLVE_QUALIFIED);
|
|
|
|
|
2010-06-12 09:29:04 -07:00
|
|
|
PropertyIdOp op = obj->getOps()->getProperty;
|
|
|
|
if (!op) {
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
JS_ASSERT(!obj->isXML());
|
|
|
|
#endif
|
2009-09-03 14:41:19 -07:00
|
|
|
return js_GetPropertyHelper(cx, obj, id, getHow, vp);
|
2009-03-14 09:41:47 -07:00
|
|
|
}
|
2010-03-04 20:44:09 -08:00
|
|
|
JS_ASSERT_IF(getHow & JSGET_CACHE_RESULT, obj->isDenseArray());
|
2009-03-14 09:41:47 -07:00
|
|
|
#if JS_HAS_XML_SUPPORT
|
2010-05-12 18:57:36 -07:00
|
|
|
if (obj->isXML())
|
2009-03-14 09:41:47 -07:00
|
|
|
return js_GetXMLMethod(cx, obj, id, vp);
|
|
|
|
#endif
|
2010-06-12 09:29:04 -07:00
|
|
|
return op(cx, obj, id, vp);
|
2009-03-14 09:41:47 -07:00
|
|
|
}
|
|
|
|
|
2009-11-19 12:35:55 -08:00
|
|
|
JS_FRIEND_API(bool)
|
2010-07-14 23:19:36 -07:00
|
|
|
js_CheckUndeclaredVarAssignment(JSContext *cx, JSString *propname)
|
2009-04-15 07:09:58 -07:00
|
|
|
{
|
2010-03-03 18:10:13 -08:00
|
|
|
JSStackFrame *const fp = js_GetTopStackFrame(cx);
|
2009-11-19 12:35:55 -08:00
|
|
|
if (!fp)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* If neither cx nor the code is strict, then no check is needed. */
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!(fp->isScriptFrame() && fp->script()->strictModeCode) &&
|
2009-11-19 12:35:55 -08:00
|
|
|
!JS_HAS_STRICT_OPTION(cx)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
const char *bytes = js_GetStringBytes(cx, propname);
|
2009-04-15 07:09:58 -07:00
|
|
|
return bytes &&
|
2009-11-19 12:35:55 -08:00
|
|
|
JS_ReportErrorFlagsAndNumber(cx,
|
|
|
|
(JSREPORT_WARNING | JSREPORT_STRICT
|
|
|
|
| JSREPORT_STRICT_MODE_ERROR),
|
2009-04-15 07:09:58 -07:00
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_UNDECLARED_VAR, bytes);
|
|
|
|
}
|
|
|
|
|
2010-04-01 18:11:14 -07:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
ReportReadOnly(JSContext* cx, jsid id, uintN flags)
|
|
|
|
{
|
|
|
|
return js_ReportValueErrorFlags(cx, flags, JSMSG_READ_ONLY,
|
2010-07-14 23:19:36 -07:00
|
|
|
JSDVG_IGNORE_STACK, IdToValue(id), NULL,
|
2010-04-01 18:11:14 -07:00
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2010-09-15 13:43:55 -07:00
|
|
|
JSBool
|
|
|
|
ReportNotConfigurable(JSContext* cx, jsid id, uintN flags)
|
|
|
|
{
|
|
|
|
return js_ReportValueErrorFlags(cx, flags, JSMSG_CANT_DELETE,
|
|
|
|
JSDVG_IGNORE_STACK, IdToValue(id), NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2010-04-01 18:11:14 -07:00
|
|
|
}
|
|
|
|
|
2010-06-02 16:01:36 -07:00
|
|
|
/*
|
|
|
|
* Note: all non-error exits in this function must notify the tracer using
|
|
|
|
* SetPropHit when called from the interpreter, which is detected by testing
|
|
|
|
* (defineHow & JSDNP_CACHE_RESULT).
|
|
|
|
*/
|
2009-04-30 15:52:13 -07:00
|
|
|
JSBool
|
2009-09-03 14:41:19 -07:00
|
|
|
js_SetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN defineHow,
|
2010-09-15 13:43:55 -07:00
|
|
|
Value *vp, JSBool strict)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-07 15:18:45 -08:00
|
|
|
int protoIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
uintN attrs, flags;
|
|
|
|
intN shortid;
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp;
|
|
|
|
PropertyOp getter, setter;
|
2009-04-29 07:07:21 -07:00
|
|
|
bool added;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-26 08:15:33 -07:00
|
|
|
JS_ASSERT((defineHow &
|
|
|
|
~(JSDNP_CACHE_RESULT | JSDNP_SET_METHOD | JSDNP_UNQUALIFIED)) == 0);
|
2009-09-03 14:41:19 -07:00
|
|
|
if (defineHow & JSDNP_CACHE_RESULT)
|
2009-05-15 06:37:37 -07:00
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
/* Convert string indices to integers if appropriate. */
|
2009-07-11 15:41:32 -07:00
|
|
|
id = js_CheckForStringIndex(id);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
/* Check for a sealed object first (now that id has been normalized). */
|
|
|
|
if (obj->sealed())
|
2010-04-01 18:11:14 -07:00
|
|
|
return ReportReadOnly(cx, id, JSREPORT_ERROR);
|
2008-12-07 00:03:12 -08:00
|
|
|
|
2008-09-25 09:13:31 -07:00
|
|
|
protoIndex = js_LookupPropertyWithFlags(cx, obj, id, cx->resolveFlags,
|
|
|
|
&pobj, &prop);
|
2008-02-07 15:18:45 -08:00
|
|
|
if (protoIndex < 0)
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_FALSE;
|
2009-04-11 01:11:06 -07:00
|
|
|
if (prop) {
|
2010-06-05 14:24:54 -07:00
|
|
|
if (!pobj->isNative())
|
2009-04-11 01:11:06 -07:00
|
|
|
prop = NULL;
|
|
|
|
} else {
|
|
|
|
/* We should never add properties to lexical blocks. */
|
2010-09-02 14:50:44 -07:00
|
|
|
JS_ASSERT(!obj->isBlock());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-26 07:06:25 -07:00
|
|
|
if (!obj->getParent() &&
|
|
|
|
(defineHow & JSDNP_UNQUALIFIED) &&
|
2010-07-14 23:19:36 -07:00
|
|
|
!js_CheckUndeclaredVarAssignment(cx, JSID_TO_STRING(id))) {
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_FALSE;
|
2010-04-26 07:06:25 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = (Shape *) prop;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* Now either shape is null, meaning id was not found in obj or one of its
|
|
|
|
* prototypes; or shape is non-null, meaning id was found directly in pobj.
|
|
|
|
* If JS_THREADSAFE and shape is non-null, then pobj is locked, and shape
|
|
|
|
* is held: we must JSObject::dropProperty or else JS_UNLOCK_OBJ before we
|
|
|
|
* return (the two are equivalent for native objects; we use JS_UNLOCK_OBJ
|
|
|
|
* because it is cheaper).
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
attrs = JSPROP_ENUMERATE;
|
|
|
|
flags = 0;
|
|
|
|
shortid = 0;
|
2010-04-07 16:09:49 -07:00
|
|
|
clasp = obj->getClass();
|
2007-03-22 10:30:00 -07:00
|
|
|
getter = clasp->getProperty;
|
|
|
|
setter = clasp->setProperty;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape) {
|
2010-04-01 18:11:14 -07:00
|
|
|
/* ES5 8.12.4 [[Put]] step 2. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape->isAccessorDescriptor()) {
|
|
|
|
if (shape->hasDefaultSetter()) {
|
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2010-06-02 16:01:36 -07:00
|
|
|
if (defineHow & JSDNP_CACHE_RESULT)
|
2010-08-29 11:57:08 -07:00
|
|
|
TRACE_2(SetPropHit, JS_NO_PROP_CACHE_FILL, shape);
|
2010-04-01 18:11:14 -07:00
|
|
|
return js_ReportGetterOnlyAssignment(cx);
|
|
|
|
}
|
|
|
|
} else {
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_ASSERT(shape->isDataDescriptor());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->writable()) {
|
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-01 18:11:14 -07:00
|
|
|
PCMETER((defineHow & JSDNP_CACHE_RESULT) && JS_PROPERTY_CACHE(cx).rofills++);
|
2010-06-02 16:01:36 -07:00
|
|
|
if (defineHow & JSDNP_CACHE_RESULT) {
|
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
2010-08-29 11:57:08 -07:00
|
|
|
TRACE_2(SetPropHit, JS_NO_PROP_CACHE_FILL, shape);
|
2010-06-02 16:01:36 -07:00
|
|
|
}
|
2010-04-13 13:34:31 -07:00
|
|
|
|
2010-09-15 13:43:55 -07:00
|
|
|
/* Error in strict mode code, warn with strict option, otherwise do nothing. */
|
|
|
|
if (strict)
|
|
|
|
return ReportReadOnly(cx, id, 0);
|
2010-04-13 13:34:31 -07:00
|
|
|
if (JS_HAS_STRICT_OPTION(cx))
|
|
|
|
return ReportReadOnly(cx, id, JSREPORT_STRICT | JSREPORT_WARNING);
|
2010-04-01 18:11:14 -07:00
|
|
|
return JS_TRUE;
|
2010-06-02 16:01:36 -07:00
|
|
|
|
|
|
|
#ifdef JS_TRACER
|
|
|
|
error: // TRACE_2 jumps here in case of error.
|
|
|
|
return JS_FALSE;
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-04-01 18:11:14 -07:00
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
if (pobj->sealed() && !shape->hasSlot()) {
|
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2010-04-01 18:11:14 -07:00
|
|
|
return ReportReadOnly(cx, id, JSREPORT_ERROR);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
attrs = shape->attributes();
|
2009-08-26 09:34:41 -07:00
|
|
|
if (pobj != obj) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* We found id in a prototype object: prepare to share or shadow.
|
2008-02-07 15:18:45 -08:00
|
|
|
*
|
2007-03-22 10:30:00 -07:00
|
|
|
* NB: Thanks to the immutable, garbage-collected property tree
|
|
|
|
* maintained by jsscope.c in cx->runtime, we needn't worry about
|
2010-08-29 11:57:08 -07:00
|
|
|
* shape going away behind our back after we've unlocked pobj.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-08 14:44:59 -08:00
|
|
|
/* Don't clone a prototype property that doesn't have a slot. */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape->hasSlot()) {
|
2009-09-03 14:41:19 -07:00
|
|
|
if (defineHow & JSDNP_CACHE_RESULT) {
|
2010-07-14 23:19:36 -07:00
|
|
|
#ifdef JS_TRACER
|
2009-09-18 06:36:51 -07:00
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
2010-06-02 16:01:36 -07:00
|
|
|
PropertyCacheEntry *entry =
|
2010-07-14 23:19:36 -07:00
|
|
|
#endif
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_PROPERTY_CACHE(cx).fill(cx, obj, 0, protoIndex, pobj, shape);
|
|
|
|
TRACE_2(SetPropHit, entry, shape);
|
2009-04-30 15:52:13 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape->hasDefaultSetter() && !shape->hasGetterValue())
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_TRUE;
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
return shape->set(cx, obj, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore attrs to the ECMA default for new properties. */
|
|
|
|
attrs = JSPROP_ENUMERATE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Preserve the shortid, getter, and setter when shadowing any
|
|
|
|
* property that has a shortid. An old API convention requires
|
|
|
|
* that the property's getter and setter functions receive the
|
|
|
|
* shortid, not id, when they are called on the shadow we are
|
2010-08-29 11:57:08 -07:00
|
|
|
* about to create in obj.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape->hasShortID()) {
|
|
|
|
flags = Shape::HAS_SHORTID;
|
|
|
|
shortid = shape->shortid;
|
|
|
|
getter = shape->getter();
|
|
|
|
setter = shape->setter();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Forget we found the proto-property now that we've copied any
|
|
|
|
* needed member values.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-08-31 16:45:34 -07:00
|
|
|
|
|
|
|
if (shape) {
|
|
|
|
if (shape->isMethod()) {
|
|
|
|
JS_ASSERT(pobj->hasMethodBarrier());
|
|
|
|
} else if ((defineHow & JSDNP_SET_METHOD) && obj->canHaveMethodBarrier()) {
|
|
|
|
JS_ASSERT(IsFunctionObject(*vp));
|
|
|
|
JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
|
|
|
|
|
|
|
|
JSObject *funobj = &vp->toObject();
|
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
|
|
|
|
if (fun == funobj) {
|
|
|
|
funobj = CloneFunctionObject(cx, fun, fun->parent);
|
|
|
|
if (!funobj)
|
|
|
|
return JS_FALSE;
|
|
|
|
vp->setObject(*funobj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-04-29 07:07:21 -07:00
|
|
|
added = false;
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape) {
|
2008-02-07 15:18:45 -08:00
|
|
|
/*
|
|
|
|
* Purge the property cache of now-shadowed id in obj's scope chain.
|
|
|
|
* Do this early, before locking obj to avoid nesting locks.
|
|
|
|
*/
|
2009-03-06 16:34:19 -08:00
|
|
|
js_PurgeScopeChain(cx, obj, id);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Find or make a property descriptor with the right heritage. */
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->ensureClassReservedSlots(cx)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-09-03 14:41:19 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for Object class here to avoid defining a method on a class
|
|
|
|
* with magic resolve, addProperty, getProperty, etc. hooks.
|
|
|
|
*/
|
2010-07-23 14:41:56 -07:00
|
|
|
if ((defineHow & JSDNP_SET_METHOD) && obj->canHaveMethodBarrier()) {
|
2010-07-14 23:19:36 -07:00
|
|
|
JS_ASSERT(IsFunctionObject(*vp));
|
2009-09-03 14:41:19 -07:00
|
|
|
JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *funobj = &vp->toObject();
|
2010-07-23 14:41:56 -07:00
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
|
|
|
|
if (fun == funobj) {
|
2010-08-29 11:57:08 -07:00
|
|
|
flags |= Shape::METHOD;
|
2010-04-30 16:03:37 -07:00
|
|
|
getter = CastAsPropertyOp(funobj);
|
2009-09-03 14:41:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = obj->putProperty(cx, id, getter, setter, SHAPE_INVALID_SLOT,
|
|
|
|
attrs, flags, shortid);
|
|
|
|
if (!shape) {
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the new property value (passed to setter) to undefined.
|
|
|
|
* Note that we store before calling addProperty, to match the order
|
|
|
|
* in js_DefineNativeProperty.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->containsSlot(shape->slot))
|
|
|
|
obj->lockedSetSlot(shape->slot, UndefinedValue());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* XXXbe called with obj locked */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!CallAddPropertyHook(cx, clasp, obj, shape, vp)) {
|
|
|
|
obj->removeProperty(cx, id);
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-09-03 14:41:19 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2009-04-29 07:07:21 -07:00
|
|
|
added = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-03 14:41:19 -07:00
|
|
|
if (defineHow & JSDNP_CACHE_RESULT) {
|
2010-07-14 23:19:36 -07:00
|
|
|
#ifdef JS_TRACER
|
2009-09-18 06:36:51 -07:00
|
|
|
JS_ASSERT_NOT_ON_TRACE(cx);
|
2010-07-14 23:19:36 -07:00
|
|
|
PropertyCacheEntry *entry =
|
|
|
|
#endif
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_PROPERTY_CACHE(cx).fill(cx, obj, 0, 0, obj, shape, added);
|
|
|
|
TRACE_2(SetPropHit, entry, shape);
|
2008-04-27 13:31:24 -07:00
|
|
|
}
|
2009-04-30 15:52:13 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!js_NativeSet(cx, obj, shape, added, vp))
|
2009-04-30 15:52:13 -07:00
|
|
|
return NULL;
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-04-30 15:52:13 -07:00
|
|
|
return JS_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
JSBool
|
2010-09-15 13:43:55 -07:00
|
|
|
js_SetProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp, JSBool strict)
|
2008-02-07 15:18:45 -08:00
|
|
|
{
|
2010-09-15 13:43:55 -07:00
|
|
|
return js_SetPropertyHelper(cx, obj, id, 0, vp, strict);
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-06-05 14:24:54 -07:00
|
|
|
js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-05 14:24:54 -07:00
|
|
|
JSProperty *prop;
|
|
|
|
if (!js_LookupProperty(cx, obj, id, &obj, &prop))
|
|
|
|
return false;
|
|
|
|
if (!prop) {
|
|
|
|
*attrsp = 0;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-06-05 14:24:54 -07:00
|
|
|
if (!obj->isNative())
|
|
|
|
return obj->getAttributes(cx, id, attrsp);
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape = (Shape *)prop;
|
|
|
|
*attrsp = shape->attributes();
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-08-29 11:57:08 -07:00
|
|
|
js_SetNativeAttributes(JSContext *cx, JSObject *obj, Shape *shape, uintN attrs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_ASSERT(obj->isNative());
|
2010-08-29 11:57:08 -07:00
|
|
|
bool ok = !!js_ChangeNativePropertyAttrs(cx, obj, shape, attrs, 0,
|
|
|
|
shape->getter(), shape->setter());
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
return ok;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-05 14:24:54 -07:00
|
|
|
JSBool
|
|
|
|
js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
|
|
|
|
{
|
|
|
|
JSProperty *prop;
|
|
|
|
if (!js_LookupProperty(cx, obj, id, &obj, &prop))
|
|
|
|
return false;
|
|
|
|
if (!prop)
|
|
|
|
return true;
|
|
|
|
return obj->isNative()
|
2010-08-29 11:57:08 -07:00
|
|
|
? js_SetNativeAttributes(cx, obj, (Shape *) prop, *attrsp)
|
2010-06-05 14:24:54 -07:00
|
|
|
: obj->setAttributes(cx, id, attrsp);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-09-15 13:43:55 -07:00
|
|
|
js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, Value *rval, JSBool strict)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSObject *proto;
|
|
|
|
JSProperty *prop;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool ok;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
rval->setBoolean(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
/* Convert string indices to integers if appropriate. */
|
2009-07-11 15:41:32 -07:00
|
|
|
id = js_CheckForStringIndex(id);
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!js_LookupProperty(cx, obj, id, &proto, &prop))
|
2010-09-15 13:43:55 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!prop || proto != obj) {
|
|
|
|
/*
|
|
|
|
* If the property was found in a native prototype, check whether it's
|
|
|
|
* shared and permanent. Such a property stands for direct properties
|
|
|
|
* in all delegating objects, matching ECMA semantics without bloating
|
|
|
|
* each delegating object.
|
|
|
|
*/
|
2010-09-15 13:43:55 -07:00
|
|
|
if (prop && proto->isNative()) {
|
|
|
|
shape = (Shape *)prop;
|
|
|
|
if (shape->isSharedPermanent()) {
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, proto);
|
2010-09-15 13:43:55 -07:00
|
|
|
if (strict)
|
|
|
|
return ReportNotConfigurable(cx, id, 0);
|
|
|
|
rval->setBoolean(false);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-09-15 13:43:55 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, proto);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If no property, or the property comes unshared or impermanent from
|
|
|
|
* a prototype, call the class's delProperty hook, passing rval as the
|
|
|
|
* result parameter.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
return CallJSPropertyOp(cx, obj->getClass()->delProperty, obj, id, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = (Shape *)prop;
|
|
|
|
if (!shape->configurable()) {
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-09-15 13:43:55 -07:00
|
|
|
if (strict)
|
|
|
|
return ReportNotConfigurable(cx, id, 0);
|
2010-07-14 23:19:36 -07:00
|
|
|
rval->setBoolean(false);
|
2010-09-15 13:43:55 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXXbe called with obj locked */
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!CallJSPropertyOp(cx, obj->getClass()->delProperty, obj, SHAPE_USERID(shape), rval)) {
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-09-15 13:43:55 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->containsSlot(shape->slot)) {
|
|
|
|
const Value &v = obj->lockedGetSlot(shape->slot);
|
2010-07-23 14:41:56 -07:00
|
|
|
GC_POKE(cx, v);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete is rare enough that we can take the hit of checking for an
|
|
|
|
* active cloned method function object that must be homed to a callee
|
|
|
|
* slot on the active stack frame before this delete completes, in case
|
|
|
|
* someone saved the clone and checks it against foo.caller for a foo
|
|
|
|
* called from the active method.
|
|
|
|
*
|
|
|
|
* We do not check suspended frames. They can't be reached via caller,
|
|
|
|
* so the only way they could have the method's joined function object
|
|
|
|
* as callee is through an API abusage. We break any such edge case.
|
|
|
|
*/
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->hasMethodBarrier()) {
|
2010-07-23 14:41:56 -07:00
|
|
|
JSObject *funobj;
|
|
|
|
|
|
|
|
if (IsFunctionObject(v, &funobj)) {
|
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
|
|
|
|
|
|
|
|
if (fun != funobj) {
|
2010-08-09 22:43:33 -07:00
|
|
|
for (JSStackFrame *fp = cx->maybefp(); fp; fp = fp->prev()) {
|
|
|
|
if (fp->isFunctionFrame() &&
|
|
|
|
&fp->callee() == &fun->compiledFunObj() &&
|
|
|
|
fp->thisValue().isObject() &&
|
|
|
|
&fp->thisValue().toObject() == obj) {
|
|
|
|
fp->calleeValue().setObject(*funobj);
|
2010-07-23 14:41:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
ok = obj->removeProperty(cx, id);
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2010-06-03 21:41:01 -07:00
|
|
|
|
|
|
|
return ok && js_SuppressDeletedProperty(cx, obj, id);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-07 00:53:47 -07:00
|
|
|
namespace js {
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
DefaultValue(JSContext *cx, JSObject *obj, JSType hint, Value *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-04-11 20:55:22 -07:00
|
|
|
JS_ASSERT(hint != JSTYPE_OBJECT && hint != JSTYPE_FUNCTION);
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v = ObjectValue(*obj);
|
|
|
|
if (hint == JSTYPE_STRING) {
|
2009-07-15 17:36:24 -07:00
|
|
|
/*
|
|
|
|
* Optimize for String objects with standard toString methods. Support
|
|
|
|
* new String(...) instances whether mutated to have their own scope or
|
|
|
|
* not, as well as direct String.prototype references.
|
|
|
|
*/
|
2010-04-07 16:09:49 -07:00
|
|
|
if (obj->getClass() == &js_StringClass) {
|
2009-07-15 17:36:24 -07:00
|
|
|
jsid toStringId = ATOM_TO_JSID(cx->runtime->atomState.toStringAtom);
|
|
|
|
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
JSObject *lockedobj = obj;
|
|
|
|
const Shape *shape = obj->nativeLookup(toStringId);
|
2009-07-21 14:25:11 -07:00
|
|
|
JSObject *pobj = obj;
|
2009-07-15 17:36:24 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape) {
|
2009-08-27 22:53:26 -07:00
|
|
|
pobj = obj->getProto();
|
2009-07-15 17:36:24 -07:00
|
|
|
|
2010-04-07 16:09:49 -07:00
|
|
|
if (pobj && pobj->getClass() == &js_StringClass) {
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-07-21 14:25:11 -07:00
|
|
|
JS_LOCK_OBJ(cx, pobj);
|
2010-08-29 11:57:08 -07:00
|
|
|
lockedobj = pobj;
|
|
|
|
shape = pobj->nativeLookup(toStringId);
|
2009-07-15 17:36:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape && shape->hasDefaultGetter() && pobj->containsSlot(shape->slot)) {
|
|
|
|
const Value &fval = pobj->lockedGetSlot(shape->slot);
|
2009-07-15 17:36:24 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *funobj;
|
|
|
|
if (IsFunctionObject(fval, &funobj)) {
|
2010-08-16 12:35:04 -07:00
|
|
|
JSFunction *fun = funobj->getFunctionPrivate();
|
|
|
|
if (fun->maybeNative() == js_str_toString) {
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, lockedobj);
|
2010-04-14 18:57:30 -07:00
|
|
|
*vp = obj->getPrimitiveThis();
|
2009-07-15 17:36:24 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, lockedobj);
|
2009-07-15 17:36:24 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Propagate the exception if js_TryMethod finds an appropriate
|
|
|
|
* method, and calling that method returned failure.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!js_TryMethod(cx, obj, cx->runtime->atomState.toStringAtom,
|
|
|
|
0, NULL, &v)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!v.isPrimitive()) {
|
2010-04-07 16:09:49 -07:00
|
|
|
if (!obj->getClass()->convert(cx, obj, hint, &v))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
} else {
|
2010-04-07 16:09:49 -07:00
|
|
|
if (!obj->getClass()->convert(cx, obj, hint, &v))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isObject()) {
|
|
|
|
JS_ASSERT(hint != TypeOfValue(cx, v));
|
2010-04-11 20:55:22 -07:00
|
|
|
if (!js_TryMethod(cx, obj, cx->runtime->atomState.toStringAtom, 0, NULL, &v))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isObject()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Avoid recursive death when decompiling in js_ReportValueError. */
|
2010-07-14 23:19:36 -07:00
|
|
|
JSString *str;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (hint == JSTYPE_STRING) {
|
2010-04-07 16:09:49 -07:00
|
|
|
str = JS_InternString(cx, obj->getClass()->name);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!str)
|
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
|
|
|
str = NULL;
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
js_ReportValueError2(cx, JSMSG_CANT_CONVERT_TO,
|
2010-07-14 23:19:36 -07:00
|
|
|
JSDVG_SEARCH_STACK, *vp, str,
|
2007-03-22 10:30:00 -07:00
|
|
|
(hint == JSTYPE_VOID)
|
|
|
|
? "primitive type"
|
2007-08-07 00:29:32 -07:00
|
|
|
: JS_TYPE_STR(hint));
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
*vp = v;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-07 00:53:47 -07:00
|
|
|
} /* namespace js */
|
|
|
|
|
2010-06-12 09:29:04 -07:00
|
|
|
JS_FRIEND_API(JSBool)
|
2010-07-14 23:19:36 -07:00
|
|
|
js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op, Value *statep, jsid *idp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
/* If the class has a custom JSCLASS_NEW_ENUMERATE hook, call it. */
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = obj->getClass();
|
2009-09-19 02:40:43 -07:00
|
|
|
JSEnumerateOp enumerate = clasp->enumerate;
|
2008-02-18 13:01:47 -08:00
|
|
|
if (clasp->flags & JSCLASS_NEW_ENUMERATE) {
|
|
|
|
JS_ASSERT(enumerate != JS_EnumerateStub);
|
2010-07-14 23:19:36 -07:00
|
|
|
return ((NewEnumerateOp) enumerate)(cx, obj, enum_op, statep, idp);
|
2008-02-18 13:01:47 -08:00
|
|
|
}
|
2010-05-12 16:00:23 -07:00
|
|
|
|
|
|
|
if (!enumerate(cx, obj))
|
|
|
|
return false;
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Tell InitNativeIterator to treat us like a native object. */
|
2009-09-24 14:33:14 -07:00
|
|
|
JS_ASSERT(enum_op == JSENUMERATE_INIT || enum_op == JSENUMERATE_INIT_ALL);
|
2010-07-14 23:19:36 -07:00
|
|
|
statep->setMagic(JS_NATIVE_ENUMERATE);
|
2009-09-19 02:40:43 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-17 05:32:26 -07:00
|
|
|
namespace js {
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-06-17 05:32:26 -07:00
|
|
|
CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *vp, uintN *attrsp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSBool writing;
|
|
|
|
JSObject *pobj;
|
|
|
|
JSProperty *prop;
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp;
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2008-09-05 16:24:53 -07:00
|
|
|
JSSecurityCallbacks *callbacks;
|
2010-07-14 23:19:36 -07:00
|
|
|
CheckAccessOp check;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-17 05:32:26 -07:00
|
|
|
while (JS_UNLIKELY(obj->getClass() == &js_WithClass))
|
|
|
|
obj = obj->getProto();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
writing = (mode & JSACC_WRITE) != 0;
|
|
|
|
switch (mode & JSACC_TYPEMASK) {
|
|
|
|
case JSACC_PROTO:
|
|
|
|
pobj = obj;
|
|
|
|
if (!writing)
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObjectOrNull(obj->getProto());
|
2007-03-22 10:30:00 -07:00
|
|
|
*attrsp = JSPROP_PERMANENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSACC_PARENT:
|
|
|
|
JS_ASSERT(!writing);
|
|
|
|
pobj = obj;
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setObject(*obj->getParent());
|
2007-03-22 10:30:00 -07:00
|
|
|
*attrsp = JSPROP_READONLY | JSPROP_PERMANENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!obj->lookupProperty(cx, id, &pobj, &prop))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
if (!prop) {
|
|
|
|
if (!writing)
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
*attrsp = 0;
|
2008-02-18 18:27:44 -08:00
|
|
|
pobj = obj;
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-02-18 18:27:44 -08:00
|
|
|
|
2010-03-31 22:13:51 -07:00
|
|
|
if (!pobj->isNative()) {
|
2010-06-17 05:32:26 -07:00
|
|
|
if (!writing) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2010-06-17 05:32:26 -07:00
|
|
|
*attrsp = 0;
|
2008-02-20 12:01:14 -08:00
|
|
|
}
|
2010-06-17 05:32:26 -07:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = (Shape *)prop;
|
|
|
|
*attrsp = shape->attributes();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!writing) {
|
2010-08-29 11:57:08 -07:00
|
|
|
if (pobj->containsSlot(shape->slot))
|
|
|
|
*vp = pobj->lockedGetSlot(shape->slot);
|
2010-07-14 23:19:36 -07:00
|
|
|
else
|
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-06-05 14:24:54 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, pobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If obj's class has a stub (null) checkAccess hook, use the per-runtime
|
|
|
|
* checkObjectAccess callback, if configured.
|
|
|
|
*
|
|
|
|
* We don't want to require all classes to supply a checkAccess hook; we
|
|
|
|
* need that hook only for certain classes used when precompiling scripts
|
|
|
|
* and functions ("brutal sharing"). But for general safety of built-in
|
2010-04-11 13:51:00 -07:00
|
|
|
* magic properties like __proto__, we route all access checks, even for
|
|
|
|
* classes that stub out checkAccess, through the global checkObjectAccess
|
|
|
|
* hook. This covers precompilation-based sharing and (possibly
|
|
|
|
* unintended) runtime sharing across trust boundaries.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-04-07 16:09:49 -07:00
|
|
|
clasp = pobj->getClass();
|
2007-03-22 10:30:00 -07:00
|
|
|
check = clasp->checkAccess;
|
2008-09-05 16:24:53 -07:00
|
|
|
if (!check) {
|
|
|
|
callbacks = JS_GetSecurityCallbacks(cx);
|
2010-07-14 23:19:36 -07:00
|
|
|
check = callbacks ? Valueify(callbacks->checkObjectAccess) : NULL;
|
2008-09-05 16:24:53 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
return !check || check(cx, pobj, id, mode, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-17 05:32:26 -07:00
|
|
|
}
|
|
|
|
|
2010-02-22 16:30:22 -08:00
|
|
|
JSType
|
|
|
|
js_TypeOf(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
/*
|
2010-02-22 19:30:22 -08:00
|
|
|
* Unfortunately we have wrappers that are native objects and thus don't
|
|
|
|
* overwrite js_TypeOf (i.e. XPCCrossOriginWrapper), so we have to
|
|
|
|
* unwrap here.
|
2010-02-22 16:30:22 -08:00
|
|
|
*/
|
2010-06-01 15:59:02 -07:00
|
|
|
obj = obj->wrappedObject(cx);
|
2010-02-22 16:30:22 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ECMA 262, 11.4.3 says that any native object that implements
|
|
|
|
* [[Call]] should be of type "function". However, RegExp is of
|
|
|
|
* type "object", not "function", for Web compatibility.
|
|
|
|
*/
|
2010-02-22 22:25:09 -08:00
|
|
|
if (obj->isCallable()) {
|
2010-02-22 16:30:22 -08:00
|
|
|
return (obj->getClass() != &js_RegExpClass)
|
|
|
|
? JSTYPE_FUNCTION
|
|
|
|
: JSTYPE_OBJECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSTYPE_OBJECT;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef JS_THREADSAFE
|
|
|
|
void
|
|
|
|
js_DropProperty(JSContext *cx, JSObject *obj, JSProperty *prop)
|
|
|
|
{
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
bool
|
|
|
|
js_IsDelegate(JSContext *cx, JSObject *obj, const Value &v)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isPrimitive())
|
|
|
|
return false;
|
|
|
|
JSObject *obj2 = v.toObject().wrappedObject(cx);
|
2010-03-04 20:44:09 -08:00
|
|
|
while ((obj2 = obj2->getProto()) != NULL) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if (obj2 == obj)
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
bool
|
2010-08-29 11:57:08 -07:00
|
|
|
js::FindClassPrototype(JSContext *cx, JSObject *scopeobj, JSProtoKey protoKey,
|
|
|
|
JSObject **protop, Class *clasp)
|
2010-06-18 17:43:02 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v;
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!js_FindClassObject(cx, scopeobj, protoKey, &v, clasp))
|
2010-06-18 17:43:02 -07:00
|
|
|
return false;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (IsFunctionObject(v)) {
|
|
|
|
JSObject *ctor = &v.toObject();
|
2010-06-18 17:43:02 -07:00
|
|
|
if (!ctor->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom), &v))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
*protop = v.isObject() ? &v.toObject() : NULL;
|
2010-06-18 17:43:02 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The first part of this function has been hand-expanded and optimized into
|
|
|
|
* NewBuiltinClassInstance in jsobjinlines.h.
|
|
|
|
*/
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-08-29 11:57:08 -07:00
|
|
|
js_GetClassPrototype(JSContext *cx, JSObject *scopeobj, JSProtoKey protoKey,
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject **protop, Class *clasp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-02-19 09:44:23 -08:00
|
|
|
VOUCH_DOES_NOT_REQUIRE_STACK();
|
|
|
|
JS_ASSERT(JSProto_Null <= protoKey);
|
|
|
|
JS_ASSERT(protoKey < JSProto_LIMIT);
|
|
|
|
|
|
|
|
if (protoKey != JSProto_Null) {
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!scopeobj) {
|
2010-08-22 16:00:20 -07:00
|
|
|
if (cx->hasfp())
|
2010-08-09 22:43:33 -07:00
|
|
|
scopeobj = &cx->fp()->scopeChain();
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!scopeobj) {
|
|
|
|
scopeobj = cx->globalObject;
|
|
|
|
if (!scopeobj) {
|
2010-02-19 09:44:23 -08:00
|
|
|
*protop = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
scopeobj = scopeobj->getGlobal();
|
|
|
|
if (scopeobj->getClass()->flags & JSCLASS_IS_GLOBAL) {
|
|
|
|
const Value &v = scopeobj->getReservedSlot(JSProto_LIMIT + protoKey);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isObject()) {
|
|
|
|
*protop = &v.toObject();
|
2010-06-02 20:30:45 -07:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-19 09:44:23 -08:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
return FindClassPrototype(cx, scopeobj, protoKey, protop, clasp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-04 13:44:48 -07:00
|
|
|
/*
|
|
|
|
* For shared precompilation of function objects, we support cloning on entry
|
|
|
|
* to an execution context in which the function declaration or expression
|
|
|
|
* should be processed as if it were not precompiled, where the precompiled
|
|
|
|
* function's scope chain does not match the execution context's. The cloned
|
|
|
|
* function object carries its execution-context scope in its parent slot; it
|
|
|
|
* links to the precompiled function (the "clone-parent") via its proto slot.
|
|
|
|
*
|
|
|
|
* Note that this prototype-based delegation leaves an unchecked access path
|
|
|
|
* from the clone to the clone-parent's 'constructor' property. If the clone
|
|
|
|
* lives in a less privileged or shared scope than the clone-parent, this is
|
|
|
|
* a security hole, a sharing hazard, or both. Therefore we check all such
|
|
|
|
* accesses with the following getter/setter pair, which we use when defining
|
|
|
|
* 'constructor' in f.prototype for all function objects f.
|
|
|
|
*/
|
|
|
|
static JSBool
|
|
|
|
CheckCtorGetAccess(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
|
|
|
{
|
|
|
|
JSAtom *atom = cx->runtime->atomState.constructorAtom;
|
|
|
|
JS_ASSERT(id == ATOM_TO_JSID(atom));
|
|
|
|
uintN attrs;
|
|
|
|
return CheckAccess(cx, obj, ATOM_TO_JSID(atom), JSACC_READ, vp, &attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
CheckCtorSetAccess(JSContext *cx, JSObject *obj, jsid id, Value *vp)
|
|
|
|
{
|
|
|
|
JSAtom *atom = cx->runtime->atomState.constructorAtom;
|
|
|
|
JS_ASSERT(id == ATOM_TO_JSID(atom));
|
|
|
|
uintN attrs;
|
|
|
|
return CheckAccess(cx, obj, ATOM_TO_JSID(atom), JSACC_WRITE, vp, &attrs);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_SetClassPrototype(JSContext *cx, JSObject *ctor, JSObject *proto, uintN attrs)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Use the given attributes for the prototype property of the constructor,
|
|
|
|
* as user-defined constructors have a DontDelete prototype (which may be
|
|
|
|
* reset), while native or "system" constructors have DontEnum | ReadOnly |
|
|
|
|
* DontDelete.
|
|
|
|
*/
|
2009-08-11 13:05:44 -07:00
|
|
|
if (!ctor->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom),
|
2010-07-14 23:19:36 -07:00
|
|
|
ObjectOrNullValue(proto), PropertyStub, PropertyStub, attrs)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ECMA says that Object.prototype.constructor, or f.prototype.constructor
|
|
|
|
* for a user-defined function f, is DontEnum.
|
|
|
|
*/
|
2009-08-11 13:05:44 -07:00
|
|
|
return proto->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.constructorAtom),
|
2010-08-04 13:44:48 -07:00
|
|
|
ObjectOrNullValue(ctor), CheckCtorGetAccess, CheckCtorSetAccess, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-14 00:07:01 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_PrimitiveToObject(JSContext *cx, Value *vp)
|
2007-06-14 00:07:01 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value v = *vp;
|
|
|
|
JS_ASSERT(v.isPrimitive());
|
|
|
|
|
|
|
|
Class *clasp;
|
|
|
|
if (v.isNumber())
|
|
|
|
clasp = &js_NumberClass;
|
|
|
|
else if (v.isString())
|
|
|
|
clasp = &js_StringClass;
|
|
|
|
else
|
|
|
|
clasp = &js_BooleanClass;
|
2007-06-14 00:07:01 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, clasp);
|
2007-06-14 00:07:01 -07:00
|
|
|
if (!obj)
|
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
|
|
|
|
obj->setPrimitiveThis(v);
|
|
|
|
vp->setObject(*obj);
|
2007-06-14 00:07:01 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ValueToObjectOrNull(JSContext *cx, const Value &v, JSObject **objp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isObjectOrNull()) {
|
|
|
|
obj = v.toObjectOrNull();
|
|
|
|
} else if (v.isUndefined()) {
|
2010-04-11 20:55:22 -07:00
|
|
|
obj = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
Value tmp = v;
|
|
|
|
if (!js_PrimitiveToObject(cx, &tmp))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-07-14 23:19:36 -07:00
|
|
|
obj = &tmp.toObject();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
*objp = obj;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject *
|
2010-07-14 23:19:36 -07:00
|
|
|
js_ValueToNonNullObject(JSContext *cx, const Value &v)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!js_ValueToObjectOrNull(cx, v, &obj))
|
2007-03-22 10:30:00 -07:00
|
|
|
return NULL;
|
2007-11-12 21:23:22 -08:00
|
|
|
if (!obj)
|
|
|
|
js_ReportIsNullOrUndefined(cx, JSDVG_SEARCH_STACK, v, NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, Value *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
Value argv[1];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
argv[0].setString(ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[type]));
|
|
|
|
return js_TryMethod(cx, obj, cx->runtime->atomState.valueOfAtom,
|
|
|
|
1, argv, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
|
2010-07-14 23:19:36 -07:00
|
|
|
uintN argc, Value *argv, Value *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-31 22:01:17 -08:00
|
|
|
JS_CHECK_RECURSION(cx, return JS_FALSE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Report failure only if an appropriate method was found, and calling it
|
|
|
|
* returned failure. We propagate failure in this case to make exceptions
|
|
|
|
* behave properly.
|
|
|
|
*/
|
2010-07-14 23:19:36 -07:00
|
|
|
JSErrorReporter older = JS_SetErrorReporter(cx, NULL);
|
|
|
|
jsid id = ATOM_TO_JSID(atom);
|
|
|
|
Value fval;
|
|
|
|
JSBool ok = js_GetMethod(cx, obj, id, JSGET_NO_METHOD_BARRIER, &fval);
|
2010-02-20 07:54:48 -08:00
|
|
|
JS_SetErrorReporter(cx, older);
|
2010-02-21 00:11:54 -08:00
|
|
|
if (!ok)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
if (fval.isPrimitive())
|
2009-01-30 15:40:05 -08:00
|
|
|
return JS_TRUE;
|
2010-08-16 12:35:04 -07:00
|
|
|
return ExternalInvoke(cx, obj, fval, argc, argv, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#if JS_HAS_XDR
|
|
|
|
|
|
|
|
JSBool
|
|
|
|
js_XDRObject(JSXDRState *xdr, JSObject **objp)
|
|
|
|
{
|
|
|
|
JSContext *cx;
|
|
|
|
JSAtom *atom;
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp;
|
2007-03-22 10:30:00 -07:00
|
|
|
uint32 classId, classDef;
|
|
|
|
JSProtoKey protoKey;
|
|
|
|
JSObject *proto;
|
|
|
|
|
|
|
|
cx = xdr->cx;
|
|
|
|
atom = NULL;
|
|
|
|
if (xdr->mode == JSXDR_ENCODE) {
|
2010-04-07 16:09:49 -07:00
|
|
|
clasp = (*objp)->getClass();
|
2007-03-22 10:30:00 -07:00
|
|
|
classId = JS_XDRFindClassIdByName(xdr, clasp->name);
|
|
|
|
classDef = !classId;
|
|
|
|
if (classDef) {
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JS_XDRRegisterClass(xdr, Jsvalify(clasp), &classId))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
protoKey = JSCLASS_CACHED_PROTO_KEY(clasp);
|
|
|
|
if (protoKey != JSProto_Null) {
|
|
|
|
classDef |= (protoKey << 1);
|
|
|
|
} else {
|
|
|
|
atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0);
|
|
|
|
if (!atom)
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clasp = NULL; /* quell GCC overwarning */
|
|
|
|
classDef = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XDR a flag word, which could be 0 for a class use, in which case no
|
|
|
|
* name follows, only the id in xdr's class registry; 1 for a class def,
|
|
|
|
* in which case the flag word is followed by the class name transferred
|
|
|
|
* from or to atom; or a value greater than 1, an odd number that when
|
|
|
|
* divided by two yields the JSProtoKey for class. In the last case, as
|
|
|
|
* in the 0 classDef case, no name is transferred via atom.
|
|
|
|
*/
|
|
|
|
if (!JS_XDRUint32(xdr, &classDef))
|
|
|
|
return JS_FALSE;
|
2010-07-15 18:19:50 -07:00
|
|
|
if (classDef == 1 && !js_XDRAtom(xdr, &atom))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (!JS_XDRUint32(xdr, &classId))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
if (xdr->mode == JSXDR_DECODE) {
|
|
|
|
if (classDef) {
|
|
|
|
/* NB: we know that JSProto_Null is 0 here, for backward compat. */
|
2007-07-05 13:37:47 -07:00
|
|
|
protoKey = (JSProtoKey) (classDef >> 1);
|
2010-02-19 09:44:23 -08:00
|
|
|
if (!js_GetClassPrototype(cx, NULL, protoKey, &proto, clasp))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-04-07 16:09:49 -07:00
|
|
|
clasp = proto->getClass();
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!JS_XDRRegisterClass(xdr, Jsvalify(clasp), &classId))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
clasp = Valueify(JS_XDRFindClassById(xdr, classId));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!clasp) {
|
|
|
|
char numBuf[12];
|
|
|
|
JS_snprintf(numBuf, sizeof numBuf, "%ld", (long)classId);
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_CANT_FIND_CLASS, numBuf);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!clasp->xdrObject) {
|
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_CANT_XDR_CLASS, clasp->name);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
return clasp->xdrObject(xdr, objp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* JS_HAS_XDR */
|
|
|
|
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
#ifdef JS_DUMP_SCOPE_METERS
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
JSBasicStats js_entry_count_bs = JS_INIT_STATIC_BASIC_STATS;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static void
|
|
|
|
MeterEntryCount(uintN count)
|
|
|
|
{
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
JS_BASIC_STATS_ACCUM(&js_entry_count_bs, count);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
js_DumpScopeMeters(JSRuntime *rt)
|
|
|
|
{
|
|
|
|
static FILE *logfp;
|
|
|
|
if (!logfp)
|
|
|
|
logfp = fopen("/tmp/scope.stats", "a");
|
|
|
|
|
|
|
|
{
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
double mean, sigma;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
mean = JS_MeanAndStdDevBS(&js_entry_count_bs, &sigma);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
fprintf(logfp, "scopes %u entries %g mean %g sigma %g max %u",
|
|
|
|
js_entry_count_bs.num, js_entry_count_bs.sum, mean, sigma,
|
|
|
|
js_entry_count_bs.max);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
JS_DumpHistogram(&js_entry_count_bs, logfp);
|
|
|
|
JS_BASIC_STATS_INIT(&js_entry_count_bs);
|
2007-03-22 10:30:00 -07:00
|
|
|
fflush(logfp);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
#ifdef DEBUG
|
2008-02-18 13:01:47 -08:00
|
|
|
void
|
|
|
|
js_PrintObjectSlotName(JSTracer *trc, char *buf, size_t bufsize)
|
2007-04-16 23:53:37 -07:00
|
|
|
{
|
2008-02-18 13:01:47 -08:00
|
|
|
JS_ASSERT(trc->debugPrinter == js_PrintObjectSlotName);
|
2007-04-16 23:53:37 -07:00
|
|
|
|
2009-09-05 08:59:11 -07:00
|
|
|
JSObject *obj = (JSObject *)trc->debugPrintArg;
|
|
|
|
uint32 slot = (uint32)trc->debugPrintIndex;
|
|
|
|
JS_ASSERT(slot >= JSSLOT_START(obj->getClass()));
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
const Shape *shape;
|
2010-03-31 22:13:51 -07:00
|
|
|
if (obj->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = obj->lastProperty();
|
|
|
|
while (shape->previous() && shape->slot != slot)
|
|
|
|
shape = shape->previous();
|
2008-02-18 13:01:47 -08:00
|
|
|
} else {
|
2010-08-29 11:57:08 -07:00
|
|
|
shape = NULL;
|
2008-02-18 13:01:47 -08:00
|
|
|
}
|
2007-04-16 23:53:37 -07:00
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!shape) {
|
2009-09-05 08:59:11 -07:00
|
|
|
const char *slotname = NULL;
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = obj->getClass();
|
2009-08-27 22:53:26 -07:00
|
|
|
if (clasp->flags & JSCLASS_IS_GLOBAL) {
|
2009-09-05 08:59:11 -07:00
|
|
|
uint32 key = slot - JSSLOT_START(clasp);
|
2009-08-27 22:53:26 -07:00
|
|
|
#define JS_PROTO(name,code,init) \
|
2007-04-16 23:53:37 -07:00
|
|
|
if ((code) == key) { slotname = js_##name##_str; goto found; }
|
|
|
|
#include "jsproto.tbl"
|
|
|
|
#undef JS_PROTO
|
|
|
|
}
|
2009-08-27 22:53:26 -07:00
|
|
|
found:
|
|
|
|
if (slotname)
|
|
|
|
JS_snprintf(buf, bufsize, "CLASS_OBJECT(%s)", slotname);
|
|
|
|
else
|
|
|
|
JS_snprintf(buf, bufsize, "**UNKNOWN SLOT %ld**", (long)slot);
|
2007-04-16 23:53:37 -07:00
|
|
|
} else {
|
2010-08-29 11:57:08 -07:00
|
|
|
jsid id = shape->id;
|
2010-07-14 23:19:36 -07:00
|
|
|
if (JSID_IS_INT(id)) {
|
|
|
|
JS_snprintf(buf, bufsize, "%ld", (long)JSID_TO_INT(id));
|
|
|
|
} else if (JSID_IS_ATOM(id)) {
|
|
|
|
js_PutEscapedString(buf, bufsize, JSID_TO_STRING(id), 0);
|
2007-04-16 23:53:37 -07:00
|
|
|
} else {
|
|
|
|
JS_snprintf(buf, bufsize, "**FINALIZED ATOM KEY**");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
js_TraceObject(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-31 22:13:51 -07:00
|
|
|
JS_ASSERT(obj->isNative());
|
2008-02-07 15:18:45 -08:00
|
|
|
|
2009-09-05 08:59:11 -07:00
|
|
|
JSContext *cx = trc->context;
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->nativeEmpty() && IS_GC_MARKING_TRACER(trc)) {
|
2008-02-07 15:18:45 -08:00
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* Trim overlong dslots allocations from the GC, to avoid thrashing in
|
|
|
|
* case of delete-happy code that settles down at a given population.
|
|
|
|
* The !obj->nativeEmpty() guard above is due to the bug described by
|
|
|
|
* the FIXME comment below.
|
2008-02-07 15:18:45 -08:00
|
|
|
*/
|
2010-09-03 11:05:43 -07:00
|
|
|
size_t slots = obj->slotSpan();
|
2010-03-30 14:42:48 -07:00
|
|
|
if (obj->numSlots() != slots)
|
2010-05-04 20:28:38 -07:00
|
|
|
obj->shrinkSlots(cx, slots);
|
2008-02-07 15:18:45 -08:00
|
|
|
}
|
|
|
|
|
* Menu of -D flags for enabling instrumentation, as a commented-out CFLAGS += setting for convenient testing. * js_FindProperty and js_LookupPropertyWithFlags return indexes into the scope and prototype chains, respectively, to support internal instrumentation, and to pave the way for the return of the property cache (bug 365851).. * jsutil.[ch] JSBasicStats struct and functions for computing mean/sigma/max and auto-scaling histogram. * JS_SCOPE_DEPTH_METER instrumentation for compile- and run-time scope chain length instrumentation: + At compile time, rt->hostenvScopeDepthStats and rt->lexicalScopeDepthStats meter scope chains passed into the compile and evaluate APIs. + At runtime, rt->protoLookupDepthStats and rt->scopeSearchDepthStats track steps along the prototype and scope chains until the sought-after property is found. * JS_ARENAMETER uses JSBasicStats now. * Added rt->liveScopePropsPreSweep to fix the property tree stats code that rotted when property tree sweeping moved to after the finalization phase. * Un-bitrotted some DEBUG_brendan code, turned some off for myself via XXX. * Mac OS X toolchain requires initialized data shared across dynamic library member files, outlaws common data, so initialize extern metering vars. * Old HASHMETER code in jshash.[ch] is now JS_HASHMETER-controlled and based on JSBasicStats. * DEBUG_scopemeters macro renamed JS_DUMP_SCOPE_METERS; uses JSBasicStats now. * Disentangle DEBUG and DUMP_SCOPE_STATS (now JS_DUMP_PROPTREE_STATS) and fix inconsistent thread safety for liveScopeProps (sometimes atomic-incremented, sometimes runtime-locked). * Compiler-modeled maxScopeDepth will propagate via JSScript to runtime for capability-based, interpreter-inlined cache hit qualifier bits, to bypass scope and prototype chain lookup by optimizing for common monomorphic get, set, and call site referencing a prototype property in a well-named object (no shadowing or mutation in 99.9% of the cases).
2008-01-12 16:31:31 -08:00
|
|
|
#ifdef JS_DUMP_SCOPE_METERS
|
2010-08-29 11:57:08 -07:00
|
|
|
MeterEntryCount(obj->propertyCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
obj->trace(trc);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-07 14:02:20 -07:00
|
|
|
if (!JS_CLIST_IS_EMPTY(&cx->runtime->watchPointList))
|
|
|
|
js_TraceWatchPoints(trc, obj);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* No one runs while the GC is running, so we can use LOCKED_... here. */
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = obj->getClass();
|
2007-04-16 23:53:37 -07:00
|
|
|
if (clasp->mark) {
|
|
|
|
if (clasp->flags & JSCLASS_MARK_IS_TRACE)
|
2008-02-07 15:18:45 -08:00
|
|
|
((JSTraceOp) clasp->mark)(trc, obj);
|
2007-04-16 23:53:37 -07:00
|
|
|
else if (IS_GC_MARKING_TRACER(trc))
|
|
|
|
(void) clasp->mark(cx, obj, trc);
|
|
|
|
}
|
2010-06-23 14:35:10 -07:00
|
|
|
if (clasp->flags & JSCLASS_IS_GLOBAL) {
|
|
|
|
JSCompartment *compartment = obj->getCompartment(cx);
|
|
|
|
compartment->marked = true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-07 15:18:45 -08:00
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* NB: In case clasp->mark mutates something (which would be a bug, but we
|
|
|
|
* want to be defensive), leave this code here -- don't move it up and
|
|
|
|
* unify it with the |if (!traceScope)| section above.
|
2008-02-07 15:18:45 -08:00
|
|
|
*
|
2010-09-03 11:05:43 -07:00
|
|
|
* FIXME: We minimize nslots against obj->slotSpan because native objects
|
|
|
|
* such as Date instances may have failed to advance slotSpan to cover all
|
2010-08-29 11:57:08 -07:00
|
|
|
* reserved slots (this Date issue may be a bug in JSObject::growSlots, but
|
|
|
|
* the general problem occurs in other built-in class implementations).
|
2008-02-07 15:18:45 -08:00
|
|
|
*/
|
2010-03-30 14:42:48 -07:00
|
|
|
uint32 nslots = obj->numSlots();
|
2010-09-03 11:05:43 -07:00
|
|
|
if (!obj->nativeEmpty() && obj->slotSpan() < nslots)
|
|
|
|
nslots = obj->slotSpan();
|
2009-09-05 08:59:11 -07:00
|
|
|
JS_ASSERT(nslots >= JSSLOT_START(clasp));
|
2007-04-16 23:53:37 -07:00
|
|
|
|
2009-09-05 08:59:11 -07:00
|
|
|
for (uint32 i = JSSLOT_START(clasp); i != nslots; ++i) {
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &v = obj->getSlot(i);
|
|
|
|
JS_SET_TRACING_DETAILS(trc, js_PrintObjectSlotName, obj, i);
|
|
|
|
MarkValueRaw(trc, v);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-12 09:29:04 -07:00
|
|
|
js_ClearNative(JSContext *cx, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/*
|
2010-08-29 11:57:08 -07:00
|
|
|
* Clear obj of all obj's properties. FIXME: we do not clear reserved slots
|
|
|
|
* lying below JSSLOT_FREE(clasp). JS_ClearScope does that.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-08-29 11:57:08 -07:00
|
|
|
if (!obj->nativeEmpty()) {
|
|
|
|
/* Now that we're done using real properties, clear obj. */
|
|
|
|
obj->clear(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-09-03 11:05:43 -07:00
|
|
|
/* Clear slot values since obj->clear reset our shape to empty. */
|
2010-07-31 02:42:28 -07:00
|
|
|
uint32 freeslot = JSSLOT_FREE(obj->getClass());
|
|
|
|
uint32 n = obj->numSlots();
|
|
|
|
for (uint32 i = freeslot; i < n; ++i)
|
2010-07-14 23:19:36 -07:00
|
|
|
obj->setSlot(i, UndefinedValue());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
}
|
|
|
|
|
2009-08-27 22:53:26 -07:00
|
|
|
bool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, Value *vp)
|
2009-08-27 22:53:26 -07:00
|
|
|
{
|
2010-03-31 22:13:51 -07:00
|
|
|
if (!obj->isNative()) {
|
2010-07-14 23:19:36 -07:00
|
|
|
vp->setUndefined();
|
2009-08-27 22:53:26 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
uint32 slot = JSSLOT_START(obj->getClass()) + index;
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-07-14 23:19:36 -07:00
|
|
|
if (slot < obj->numSlots())
|
|
|
|
*vp = obj->getSlot(slot);
|
|
|
|
else
|
|
|
|
vp->setUndefined();
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-08-27 22:53:26 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-08-27 22:53:26 -07:00
|
|
|
bool
|
2010-07-14 23:19:36 -07:00
|
|
|
js_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, const Value &v)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-03-31 22:13:51 -07:00
|
|
|
if (!obj->isNative())
|
2009-08-27 22:53:26 -07:00
|
|
|
return true;
|
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = obj->getClass();
|
2010-06-18 17:43:02 -07:00
|
|
|
uint32 slot = JSSLOT_START(clasp) + index;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
JS_LOCK_OBJ(cx, obj);
|
2010-05-04 20:28:38 -07:00
|
|
|
if (slot >= obj->numSlots()) {
|
2009-08-27 22:53:26 -07:00
|
|
|
uint32 nslots = JSSLOT_FREE(clasp);
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(slot < nslots);
|
2010-05-04 20:28:38 -07:00
|
|
|
if (!obj->allocSlots(cx, nslots)) {
|
2009-08-27 22:53:26 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-30 14:42:48 -07:00
|
|
|
obj->setSlot(slot, v);
|
2009-03-07 01:32:22 -08:00
|
|
|
GC_POKE(cx, JS_NULL);
|
2010-08-29 11:57:08 -07:00
|
|
|
JS_UNLOCK_OBJ(cx, obj);
|
2009-08-27 22:53:26 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-03-06 12:05:18 -08:00
|
|
|
JSObject *
|
2010-06-01 15:59:02 -07:00
|
|
|
JSObject::wrappedObject(JSContext *cx) const
|
2008-03-06 12:05:18 -08:00
|
|
|
{
|
2010-06-12 09:29:04 -07:00
|
|
|
if (JSObjectOp op = getClass()->ext.wrappedObject) {
|
|
|
|
if (JSObject *obj = op(cx, const_cast<JSObject *>(this)))
|
|
|
|
return obj;
|
2008-03-06 12:05:18 -08:00
|
|
|
}
|
2010-06-01 15:59:02 -07:00
|
|
|
return const_cast<JSObject *>(this);
|
2008-03-06 12:05:18 -08:00
|
|
|
}
|
|
|
|
|
2010-03-31 15:57:03 -07:00
|
|
|
JSObject *
|
2010-08-02 23:52:12 -07:00
|
|
|
JSObject::getGlobal() const
|
2010-03-31 15:57:03 -07:00
|
|
|
{
|
2010-08-02 23:52:12 -07:00
|
|
|
JSObject *obj = const_cast<JSObject *>(this);
|
2010-03-31 15:57:03 -07:00
|
|
|
while (JSObject *parent = obj->getParent())
|
|
|
|
obj = parent;
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2009-12-08 17:00:42 -08:00
|
|
|
JSBool
|
2009-04-22 03:39:08 -07:00
|
|
|
js_ReportGetterOnlyAssignment(JSContext *cx)
|
|
|
|
{
|
2009-12-08 17:00:42 -08:00
|
|
|
return JS_ReportErrorFlagsAndNumber(cx,
|
|
|
|
JSREPORT_WARNING | JSREPORT_STRICT |
|
|
|
|
JSREPORT_STRICT_MODE_ERROR,
|
|
|
|
js_GetErrorMessage, NULL,
|
|
|
|
JSMSG_GETTER_ONLY);
|
2009-04-22 03:39:08 -07:00
|
|
|
}
|
|
|
|
|
2010-06-04 16:32:10 -07:00
|
|
|
JSCompartment *
|
2010-06-16 14:13:50 -07:00
|
|
|
JSObject::getCompartment(JSContext *cx)
|
|
|
|
{
|
2010-06-04 16:32:10 -07:00
|
|
|
JSObject *obj = getGlobal();
|
2010-06-16 14:13:50 -07:00
|
|
|
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp = obj->getClass();
|
2010-06-16 14:13:50 -07:00
|
|
|
if (!(clasp->flags & JSCLASS_IS_GLOBAL)) {
|
2010-09-15 07:55:35 -07:00
|
|
|
#if JS_HAS_XML_SUPPORT
|
2010-06-16 14:13:50 -07:00
|
|
|
// The magic AnyName object is runtime-wide.
|
|
|
|
if (clasp == &js_AnyNameClass)
|
|
|
|
return cx->runtime->defaultCompartment;
|
|
|
|
|
|
|
|
// The magic function namespace object is runtime-wide.
|
2010-06-12 09:29:04 -07:00
|
|
|
if (clasp == &js_NamespaceClass &&
|
2010-08-31 16:45:34 -07:00
|
|
|
obj->getNameURI() == ATOM_TO_JSVAL(cx->runtime->
|
2010-09-15 07:55:35 -07:00
|
|
|
atomState.functionNamespaceURIAtom)) {
|
2010-06-16 14:13:50 -07:00
|
|
|
return cx->runtime->defaultCompartment;
|
|
|
|
}
|
2010-09-15 07:55:35 -07:00
|
|
|
#endif
|
2010-06-16 14:13:50 -07:00
|
|
|
|
2010-08-31 16:45:34 -07:00
|
|
|
/*
|
2010-08-20 13:11:05 -07:00
|
|
|
* Script objects and compile-time Function, Block, RegExp objects
|
|
|
|
* are not parented.
|
|
|
|
*/
|
2010-08-31 16:45:34 -07:00
|
|
|
if (clasp == &js_FunctionClass || clasp == &js_BlockClass || clasp == &js_RegExpClass ||
|
|
|
|
clasp == &js_ScriptClass) {
|
2010-06-16 14:13:50 -07:00
|
|
|
// This is a bogus answer, but it'll do for now.
|
|
|
|
return cx->runtime->defaultCompartment;
|
|
|
|
}
|
|
|
|
JS_NOT_REACHED("non-global object at end of scope chain");
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
const Value &v = obj->getReservedSlot(JSRESERVED_GLOBAL_COMPARTMENT);
|
|
|
|
return (JSCompartment *)v.toPrivate();
|
2010-06-04 16:32:10 -07:00
|
|
|
}
|
|
|
|
|
2009-04-22 03:39:08 -07:00
|
|
|
JS_FRIEND_API(JSBool)
|
2010-07-14 23:19:36 -07:00
|
|
|
js_GetterOnlyPropertyStub(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
|
2009-04-22 03:39:08 -07:00
|
|
|
{
|
2009-12-08 17:00:42 -08:00
|
|
|
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_GETTER_ONLY);
|
2009-04-22 03:39:08 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-01-23 17:20:14 -08:00
|
|
|
#ifdef DEBUG
|
2008-09-25 09:29:12 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines to print out values during debugging. These are FRIEND_API to help
|
|
|
|
* the debugger find them and to support temporarily hacking js_Dump* calls
|
|
|
|
* into other code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
dumpChars(const jschar *s, size_t n)
|
|
|
|
{
|
|
|
|
size_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
if (n == (size_t) -1) {
|
|
|
|
while (s[++n]) ;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
fputc('"', stderr);
|
2008-09-25 09:29:12 -07:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (s[i] == '\n')
|
|
|
|
fprintf(stderr, "\\n");
|
|
|
|
else if (s[i] == '\t')
|
|
|
|
fprintf(stderr, "\\t");
|
|
|
|
else if (s[i] >= 32 && s[i] < 127)
|
|
|
|
fputc(s[i], stderr);
|
|
|
|
else if (s[i] <= 255)
|
|
|
|
fprintf(stderr, "\\x%02x", (unsigned int) s[i]);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "\\u%04x", (unsigned int) s[i]);
|
|
|
|
}
|
|
|
|
fputc('"', stderr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_DumpChars(const jschar *s, size_t n)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "jschar * (%p) = ", (void *) s);
|
|
|
|
dumpChars(s, n);
|
2007-03-22 10:30:00 -07:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
void
|
|
|
|
dumpString(JSString *str)
|
|
|
|
{
|
2009-06-10 18:29:44 -07:00
|
|
|
dumpChars(str->chars(), str->length());
|
2008-09-25 09:29:12 -07:00
|
|
|
}
|
2008-03-21 10:19:33 -07:00
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_DumpString(JSString *str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "JSString* (%p) = jschar * (%p) = ",
|
2009-06-10 18:29:44 -07:00
|
|
|
(void *) str, (void *) str->chars());
|
2008-09-25 09:29:12 -07:00
|
|
|
dumpString(str);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
2008-03-21 10:19:33 -07:00
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_DumpAtom(JSAtom *atom)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "JSAtom* (%p) = ", (void *) atom);
|
2010-07-14 23:19:36 -07:00
|
|
|
js_DumpString(ATOM_TO_STRING(atom));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
void
|
2010-07-14 23:19:36 -07:00
|
|
|
dumpValue(const Value &v)
|
2008-09-25 09:29:12 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (v.isNull())
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "null");
|
2010-07-14 23:19:36 -07:00
|
|
|
else if (v.isUndefined())
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "undefined");
|
2010-07-14 23:19:36 -07:00
|
|
|
else if (v.isInt32())
|
|
|
|
fprintf(stderr, "%d", v.toInt32());
|
|
|
|
else if (v.isDouble())
|
|
|
|
fprintf(stderr, "%g", v.toDouble());
|
|
|
|
else if (v.isString())
|
|
|
|
dumpString(v.toString());
|
|
|
|
else if (v.isObject() && v.toObject().isFunction()) {
|
|
|
|
JSObject *funobj = &v.toObject();
|
2009-08-04 14:06:55 -07:00
|
|
|
JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
|
2009-03-26 16:07:54 -07:00
|
|
|
fprintf(stderr, "<%s %s at %p (JSFunction at %p)>",
|
|
|
|
fun->atom ? "function" : "unnamed",
|
|
|
|
fun->atom ? JS_GetStringBytes(ATOM_TO_STRING(fun->atom)) : "function",
|
|
|
|
(void *) funobj,
|
|
|
|
(void *) fun);
|
2010-07-14 23:19:36 -07:00
|
|
|
} else if (v.isObject()) {
|
|
|
|
JSObject *obj = &v.toObject();
|
|
|
|
Class *clasp = obj->getClass();
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "<%s%s at %p>",
|
2010-06-16 14:13:50 -07:00
|
|
|
clasp->name,
|
2010-07-23 14:41:56 -07:00
|
|
|
(clasp == &js_ObjectClass) ? "" : " object",
|
2009-02-19 00:33:37 -08:00
|
|
|
(void *) obj);
|
2010-07-14 23:19:36 -07:00
|
|
|
} else if (v.isBoolean()) {
|
|
|
|
if (v.toBoolean())
|
|
|
|
fprintf(stderr, "true");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "false");
|
|
|
|
} else if (v.isMagic()) {
|
|
|
|
fprintf(stderr, "<invalid");
|
|
|
|
#ifdef DEBUG
|
|
|
|
switch (v.whyMagic()) {
|
|
|
|
case JS_ARRAY_HOLE: fprintf(stderr, " array hole"); break;
|
|
|
|
case JS_ARGS_HOLE: fprintf(stderr, " args hole"); break;
|
|
|
|
case JS_NATIVE_ENUMERATE: fprintf(stderr, " native enumeration"); break;
|
|
|
|
case JS_NO_ITER_VALUE: fprintf(stderr, " no iter value"); break;
|
|
|
|
case JS_GENERATOR_CLOSING: fprintf(stderr, " generator closing"); break;
|
|
|
|
default: fprintf(stderr, " ?!"); break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
fprintf(stderr, ">");
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-07-14 23:19:36 -07:00
|
|
|
fprintf(stderr, "unexpected value");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
JS_FRIEND_API(void)
|
2010-07-14 23:19:36 -07:00
|
|
|
js_DumpValue(const Value &val)
|
2008-09-25 09:29:12 -07:00
|
|
|
{
|
|
|
|
dumpValue(val);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_DumpId(jsid id)
|
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
fprintf(stderr, "jsid %p = ", (void *) JSID_BITS(id));
|
|
|
|
dumpValue(IdToValue(id));
|
2008-09-25 09:29:12 -07:00
|
|
|
fputc('\n', stderr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-09 05:30:21 -07:00
|
|
|
static void
|
2010-08-29 11:57:08 -07:00
|
|
|
DumpShape(const Shape &shape)
|
2008-09-25 09:29:12 -07:00
|
|
|
{
|
2010-08-29 11:57:08 -07:00
|
|
|
jsid id = shape.id;
|
|
|
|
uint8 attrs = shape.attributes();
|
2008-09-25 09:29:12 -07:00
|
|
|
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
if (attrs & JSPROP_ENUMERATE) fprintf(stderr, "enumerate ");
|
|
|
|
if (attrs & JSPROP_READONLY) fprintf(stderr, "readonly ");
|
|
|
|
if (attrs & JSPROP_PERMANENT) fprintf(stderr, "permanent ");
|
|
|
|
if (attrs & JSPROP_GETTER) fprintf(stderr, "getter ");
|
|
|
|
if (attrs & JSPROP_SETTER) fprintf(stderr, "setter ");
|
|
|
|
if (attrs & JSPROP_SHARED) fprintf(stderr, "shared ");
|
2010-08-29 11:57:08 -07:00
|
|
|
if (shape.isAlias()) fprintf(stderr, "alias ");
|
2008-09-25 09:29:12 -07:00
|
|
|
if (JSID_IS_ATOM(id))
|
2010-07-14 23:19:36 -07:00
|
|
|
dumpString(JSID_TO_STRING(id));
|
2008-09-25 09:29:12 -07:00
|
|
|
else if (JSID_IS_INT(id))
|
|
|
|
fprintf(stderr, "%d", (int) JSID_TO_INT(id));
|
|
|
|
else
|
2010-07-14 23:19:36 -07:00
|
|
|
fprintf(stderr, "unknown jsid %p", (void *) JSID_BITS(id));
|
2010-08-29 11:57:08 -07:00
|
|
|
fprintf(stderr, ": slot %d", shape.slot);
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(void)
|
|
|
|
js_DumpObject(JSObject *obj)
|
|
|
|
{
|
|
|
|
uint32 i, slots;
|
2010-07-14 23:19:36 -07:00
|
|
|
Class *clasp;
|
2008-09-25 09:29:12 -07:00
|
|
|
jsuint reservedEnd;
|
|
|
|
|
|
|
|
fprintf(stderr, "object %p\n", (void *) obj);
|
2010-03-30 14:42:48 -07:00
|
|
|
clasp = obj->getClass();
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "class %p %s\n", (void *)clasp, clasp->name);
|
|
|
|
|
2010-08-29 11:57:08 -07:00
|
|
|
fprintf(stderr, "flags:");
|
|
|
|
uint32 flags = obj->flags;
|
|
|
|
if (flags & JSObject::DELEGATE) fprintf(stderr, " delegate");
|
|
|
|
if (flags & JSObject::SYSTEM) fprintf(stderr, " system");
|
|
|
|
if (flags & JSObject::SEALED) fprintf(stderr, " sealed");
|
|
|
|
if (flags & JSObject::BRANDED) fprintf(stderr, " branded");
|
|
|
|
if (flags & JSObject::GENERIC) fprintf(stderr, " generic");
|
|
|
|
if (flags & JSObject::METHOD_BARRIER) fprintf(stderr, " method_barrier");
|
|
|
|
if (flags & JSObject::INDEXED) fprintf(stderr, " indexed");
|
|
|
|
if (flags & JSObject::OWN_SHAPE) fprintf(stderr, " own_shape");
|
|
|
|
bool anyFlags = flags != 0;
|
2010-09-02 15:59:19 -07:00
|
|
|
if (obj->isNative()) {
|
|
|
|
if (obj->inDictionaryMode()) {
|
|
|
|
fprintf(stderr, " inDictionaryMode");
|
|
|
|
anyFlags = true;
|
|
|
|
}
|
|
|
|
if (obj->hasPropertyTable()) {
|
|
|
|
fprintf(stderr, " hasPropertyTable");
|
|
|
|
anyFlags = true;
|
|
|
|
}
|
2010-08-29 11:57:08 -07:00
|
|
|
}
|
|
|
|
if (!anyFlags)
|
|
|
|
fprintf(stderr, " none");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2009-12-30 13:49:26 -08:00
|
|
|
if (obj->isDenseArray()) {
|
2010-04-26 18:33:36 -07:00
|
|
|
slots = JS_MIN(obj->getArrayLength(), obj->getDenseArrayCapacity());
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "elements\n");
|
|
|
|
for (i = 0; i < slots; i++) {
|
|
|
|
fprintf(stderr, " %3d: ", i);
|
2010-04-26 18:33:36 -07:00
|
|
|
dumpValue(obj->getDenseArrayElement(i));
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fflush(stderr);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-31 22:13:51 -07:00
|
|
|
if (obj->isNative()) {
|
2010-08-29 11:57:08 -07:00
|
|
|
if (obj->sealed())
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "sealed\n");
|
|
|
|
|
2009-07-21 14:25:11 -07:00
|
|
|
fprintf(stderr, "properties:\n");
|
2010-08-29 11:57:08 -07:00
|
|
|
for (Shape::Range r = obj->lastProperty()->all(); !r.empty(); r.popFront())
|
|
|
|
DumpShape(r.front());
|
2008-09-25 09:29:12 -07:00
|
|
|
} else {
|
2010-03-31 22:13:51 -07:00
|
|
|
if (!obj->isNative())
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "not native\n");
|
|
|
|
}
|
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
fprintf(stderr, "proto ");
|
2010-07-14 23:19:36 -07:00
|
|
|
dumpValue(ObjectOrNullValue(obj->getProto()));
|
2009-08-04 14:06:55 -07:00
|
|
|
fputc('\n', stderr);
|
|
|
|
|
|
|
|
fprintf(stderr, "parent ");
|
2010-07-14 23:19:36 -07:00
|
|
|
dumpValue(ObjectOrNullValue(obj->getParent()));
|
2009-08-04 14:06:55 -07:00
|
|
|
fputc('\n', stderr);
|
|
|
|
|
|
|
|
i = JSSLOT_PRIVATE;
|
|
|
|
if (clasp->flags & JSCLASS_HAS_PRIVATE) {
|
|
|
|
i = JSSLOT_PRIVATE + 1;
|
|
|
|
fprintf(stderr, "private %p\n", obj->getPrivate());
|
|
|
|
}
|
|
|
|
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "slots:\n");
|
2009-08-04 14:06:55 -07:00
|
|
|
reservedEnd = i + JSCLASS_RESERVED_SLOTS(clasp);
|
2010-09-03 11:05:43 -07:00
|
|
|
slots = obj->slotSpan();
|
2009-08-04 14:06:55 -07:00
|
|
|
for (; i < slots; i++) {
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, " %3d ", i);
|
2009-08-04 14:06:55 -07:00
|
|
|
if (i < reservedEnd)
|
2008-09-25 09:29:12 -07:00
|
|
|
fprintf(stderr, "(reserved) ");
|
|
|
|
fprintf(stderr, "= ");
|
2010-03-30 14:42:48 -07:00
|
|
|
dumpValue(obj->getSlot(i));
|
2008-09-25 09:29:12 -07:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
fputc('\n', stderr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-05-20 08:10:03 -07:00
|
|
|
static void
|
|
|
|
MaybeDumpObject(const char *name, JSObject *obj)
|
|
|
|
{
|
|
|
|
if (obj) {
|
|
|
|
fprintf(stderr, " %s: ", name);
|
2010-07-14 23:19:36 -07:00
|
|
|
dumpValue(ObjectValue(*obj));
|
2009-05-20 08:10:03 -07:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-11 19:07:00 -07:00
|
|
|
static void
|
2010-07-14 23:19:36 -07:00
|
|
|
MaybeDumpValue(const char *name, const Value &v)
|
2009-09-11 19:07:00 -07:00
|
|
|
{
|
2010-07-14 23:19:36 -07:00
|
|
|
if (!v.isNull()) {
|
2009-09-11 19:07:00 -07:00
|
|
|
fprintf(stderr, " %s: ", name);
|
|
|
|
dumpValue(v);
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-20 08:10:03 -07:00
|
|
|
JS_FRIEND_API(void)
|
2010-03-03 18:10:13 -08:00
|
|
|
js_DumpStackFrame(JSContext *cx, JSStackFrame *start)
|
2009-05-20 08:10:03 -07:00
|
|
|
{
|
2010-03-03 18:10:13 -08:00
|
|
|
/* This should only called during live debugging. */
|
|
|
|
VOUCH_DOES_NOT_REQUIRE_STACK();
|
|
|
|
|
|
|
|
if (!start)
|
2010-08-22 16:00:20 -07:00
|
|
|
start = cx->maybefp();
|
2010-03-03 18:10:13 -08:00
|
|
|
FrameRegsIter i(cx);
|
|
|
|
while (!i.done() && i.fp() != start)
|
|
|
|
++i;
|
|
|
|
|
|
|
|
if (i.done()) {
|
|
|
|
fprintf(stderr, "fp = %p not found in cx = %p\n", (void *)start, (void *)cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; !i.done(); ++i) {
|
|
|
|
JSStackFrame *const fp = i.fp();
|
2009-05-20 08:10:03 -07:00
|
|
|
|
|
|
|
fprintf(stderr, "JSStackFrame at %p\n", (void *) fp);
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isFunctionFrame()) {
|
|
|
|
fprintf(stderr, "callee fun: ");
|
|
|
|
dumpValue(ObjectValue(fp->callee()));
|
2010-03-03 18:10:13 -08:00
|
|
|
} else {
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, "global frame, no callee");
|
2010-03-03 18:10:13 -08:00
|
|
|
}
|
2009-05-20 08:10:03 -07:00
|
|
|
fputc('\n', stderr);
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isScriptFrame()) {
|
2010-08-15 05:44:51 -07:00
|
|
|
fprintf(stderr, "file %s line %u\n",
|
2010-08-09 22:43:33 -07:00
|
|
|
fp->script()->filename, (unsigned) fp->script()->lineno);
|
2010-08-15 05:44:51 -07:00
|
|
|
}
|
2009-05-20 08:10:03 -07:00
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
if (jsbytecode *pc = i.pc()) {
|
2010-08-09 22:43:33 -07:00
|
|
|
if (!fp->isScriptFrame()) {
|
2010-03-03 18:10:13 -08:00
|
|
|
fprintf(stderr, "*** pc && !script, skipping frame\n\n");
|
2009-05-20 08:10:03 -07:00
|
|
|
continue;
|
|
|
|
}
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->hasImacropc()) {
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " pc in imacro at %p\n called from ", pc);
|
2010-08-09 22:43:33 -07:00
|
|
|
pc = fp->imacropc();
|
2009-05-20 08:10:03 -07:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, " ");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "pc = %p\n", pc);
|
|
|
|
fprintf(stderr, " current op: %s\n", js_CodeName[*pc]);
|
|
|
|
}
|
2010-07-14 23:19:36 -07:00
|
|
|
Value *sp = i.sp();
|
2010-03-03 18:10:13 -08:00
|
|
|
fprintf(stderr, " slots: %p\n", (void *) fp->slots());
|
|
|
|
fprintf(stderr, " sp: %p = slots + %u\n", (void *) sp, (unsigned) (sp - fp->slots()));
|
|
|
|
if (sp - fp->slots() < 10000) { // sanity
|
2010-07-14 23:19:36 -07:00
|
|
|
for (Value *p = fp->slots(); p < sp; p++) {
|
2010-03-03 18:10:13 -08:00
|
|
|
fprintf(stderr, " %p: ", (void *) p);
|
|
|
|
dumpValue(*p);
|
|
|
|
fputc('\n', stderr);
|
2009-05-20 08:10:03 -07:00
|
|
|
}
|
|
|
|
}
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isFunctionFrame() && !fp->isEvalFrame()) {
|
|
|
|
fprintf(stderr, " actuals: %p (%u) ", (void *) fp->actualArgs(), (unsigned) fp->numActualArgs());
|
|
|
|
fprintf(stderr, " formals: %p (%u)\n", (void *) fp->formalArgs(), (unsigned) fp->numFormalArgs());
|
|
|
|
}
|
2010-08-12 15:46:03 -07:00
|
|
|
MaybeDumpObject("callobj", fp->maybeCallObj());
|
|
|
|
MaybeDumpObject("argsobj", fp->maybeArgsObj());
|
2010-08-09 22:43:33 -07:00
|
|
|
MaybeDumpValue("this", fp->thisValue());
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " rval: ");
|
2010-08-09 22:43:33 -07:00
|
|
|
dumpValue(fp->returnValue());
|
2009-05-20 08:10:03 -07:00
|
|
|
fputc('\n', stderr);
|
|
|
|
|
|
|
|
fprintf(stderr, " flags:");
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isConstructing())
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " constructing");
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->hasOverriddenArgs())
|
|
|
|
fprintf(stderr, " overridden_args");
|
|
|
|
if (fp->isAssigning())
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " assigning");
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isDebuggerFrame())
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " debugger");
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isEvalFrame())
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " eval");
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isYielding())
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " yielding");
|
2010-08-09 22:43:33 -07:00
|
|
|
if (fp->isGeneratorFrame())
|
2009-05-20 08:10:03 -07:00
|
|
|
fprintf(stderr, " generator");
|
|
|
|
fputc('\n', stderr);
|
|
|
|
|
2010-08-09 22:43:33 -07:00
|
|
|
fprintf(stderr, " scopeChain: (JSObject *) %p\n", (void *) &fp->scopeChain());
|
2010-08-13 20:36:37 -07:00
|
|
|
if (fp->hasBlockChain())
|
2010-08-09 22:43:33 -07:00
|
|
|
fprintf(stderr, " blockChain: (JSObject *) %p\n", (void *) fp->blockChain());
|
2009-05-20 08:10:03 -07:00
|
|
|
|
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-28 22:24:21 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool
|
|
|
|
IsSaneThisObject(JSObject &obj)
|
|
|
|
{
|
|
|
|
Class *clasp = obj.getClass();
|
|
|
|
return clasp != &js_CallClass &&
|
|
|
|
clasp != &js_BlockClass &&
|
|
|
|
clasp != &js_DeclEnvClass &&
|
|
|
|
clasp != &js_WithClass;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2010-06-28 22:24:21 -07:00
|
|
|
|
2010-06-29 13:29:39 -07:00
|
|
|
#endif /* DEBUG */
|
2010-05-10 22:01:31 -07:00
|
|
|
|