Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsobj.cpp
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sw=4 et tw=79:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is Mozilla Communicator client code, released
18  * March 31, 1998.
19  *
20  * The Initial Developer of the Original Code is
21  * Netscape Communications Corporation.
22  * Portions created by the Initial Developer are Copyright (C) 1998
23  * the Initial Developer. All Rights Reserved.
24  *
25  * Contributor(s):
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either of the GNU General Public License Version 2 or later (the "GPL"),
29  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40
41 /*
42  * JS object implementation.
43  */
44 #include <stdlib.h>
45 #include <string.h>
46 #include "jstypes.h"
47 #include "jsstdint.h"
48 #include "jsarena.h"
49 #include "jsbit.h"
50 #include "jsutil.h"
51 #include "jshash.h"
52 #include "jsdhash.h"
53 #include "jsprf.h"
54 #include "jsapi.h"
55 #include "jsarray.h"
56 #include "jsatom.h"
57 #include "jsbool.h"
58 #include "jsbuiltins.h"
59 #include "jscntxt.h"
60 #include "jsversion.h"
61 #include "jsemit.h"
62 #include "jsfun.h"
63 #include "jsgc.h"
64 #include "jsinterp.h"
65 #include "jsiter.h"
66 #include "jslock.h"
67 #include "jsnum.h"
68 #include "jsobj.h"
69 #include "jsopcode.h"
70 #include "jsparse.h"
71 #include "jsproxy.h"
72 #include "jsscope.h"
73 #include "jsscript.h"
74 #include "jsstaticcheck.h"
75 #include "jsstdint.h"
76 #include "jsstr.h"
77 #include "jstracer.h"
78 #include "jsdbgapi.h"
79 #include "json.h"
80 #include "jswrapper.h"
81
82 #include "jsinterpinlines.h"
83 #include "jsscopeinlines.h"
84 #include "jsscriptinlines.h"
85 #include "jsobjinlines.h"
86
87 #if JS_HAS_GENERATORS
88 #include "jsiter.h"
89 #endif
90
91 #if JS_HAS_XML_SUPPORT
92 #include "jsxml.h"
93 #endif
94
95 #if JS_HAS_XDR
96 #include "jsxdrapi.h"
97 #endif
98
99 #include "jsprobes.h"
100 #include "jsatominlines.h"
101 #include "jsobjinlines.h"
102 #include "jsscriptinlines.h"
103
104 #include "jsautooplen.h"
105
106 using namespace js;
107 using namespace js::gc;
108
109 JS_FRIEND_DATA(const JSObjectMap) JSObjectMap::sharedNonNative(JSObjectMap::SHAPELESS);
110
111 Class js_ObjectClass = {
112     js_Object_str,
113     JSCLASS_HAS_CACHED_PROTO(JSProto_Object),
114     PropertyStub,         /* addProperty */
115     PropertyStub,         /* delProperty */
116     PropertyStub,         /* getProperty */
117     StrictPropertyStub,   /* setProperty */
118     EnumerateStub,
119     ResolveStub,
120     ConvertStub
121 };
122
123 JS_FRIEND_API(JSObject *)
124 js_ObjectToOuterObject(JSContext *cx, JSObject *obj)
125 {
126     OBJ_TO_OUTER_OBJECT(cx, obj);
127     return obj;
128 }
129
130 #if JS_HAS_OBJ_PROTO_PROP
131
132 static JSBool
133 obj_getProto(JSContext *cx, JSObject *obj, jsid id, Value *vp);
134
135 static JSBool
136 obj_setProto(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp);
137
138 static JSPropertySpec object_props[] = {
139     {js_proto_str, 0, JSPROP_PERMANENT|JSPROP_SHARED, Jsvalify(obj_getProto), Jsvalify(obj_setProto)},
140     {0,0,0,0,0}
141 };
142
143 static JSBool
144 obj_getProto(JSContext *cx, JSObject *obj, jsid id, Value *vp)
145 {
146     /* Let CheckAccess get the slot's value, based on the access mode. */
147     uintN attrs;
148     id = ATOM_TO_JSID(cx->runtime->atomState.protoAtom);
149     return CheckAccess(cx, obj, id, JSACC_PROTO, vp, &attrs);
150 }
151
152 static JSBool
153 obj_setProto(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
154 {
155     /* ECMAScript 5 8.6.2 forbids changing [[Prototype]] if not [[Extensible]]. */
156     if (!obj->isExtensible()) {
157         obj->reportNotExtensible(cx);
158         return false;
159     }
160
161     if (!vp->isObjectOrNull())
162         return JS_TRUE;
163
164     JSObject *pobj = vp->toObjectOrNull();
165     if (pobj) {
166         /*
167          * Innerize pobj here to avoid sticking unwanted properties on the
168          * outer object. This ensures that any with statements only grant
169          * access to the inner object.
170          */
171         OBJ_TO_INNER_OBJECT(cx, pobj);
172         if (!pobj)
173             return JS_FALSE;
174     }
175
176     uintN attrs;
177     id = ATOM_TO_JSID(cx->runtime->atomState.protoAtom);
178     if (!CheckAccess(cx, obj, id, JSAccessMode(JSACC_PROTO|JSACC_WRITE), vp, &attrs))
179         return JS_FALSE;
180
181     return SetProto(cx, obj, pobj, JS_TRUE);
182 }
183
184 #else  /* !JS_HAS_OBJ_PROTO_PROP */
185
186 #define object_props NULL
187
188 #endif /* !JS_HAS_OBJ_PROTO_PROP */
189
190 static JSHashNumber
191 js_hash_object(const void *key)
192 {
193     return JSHashNumber(uintptr_t(key) >> JS_GCTHING_ALIGN);
194 }
195
196 static JSHashEntry *
197 MarkSharpObjects(JSContext *cx, JSObject *obj, JSIdArray **idap)
198 {
199     JSSharpObjectMap *map;
200     JSHashTable *table;
201     JSHashNumber hash;
202     JSHashEntry **hep, *he;
203     jsatomid sharpid;
204     JSIdArray *ida;
205     JSBool ok;
206     jsint i, length;
207     jsid id;
208     JSObject *obj2;
209     JSProperty *prop;
210
211     JS_CHECK_RECURSION(cx, return NULL);
212
213     map = &cx->sharpObjectMap;
214     JS_ASSERT(map->depth >= 1);
215     table = map->table;
216     hash = js_hash_object(obj);
217     hep = JS_HashTableRawLookup(table, hash, obj);
218     he = *hep;
219     if (!he) {
220         sharpid = 0;
221         he = JS_HashTableRawAdd(table, hep, hash, obj, (void *) sharpid);
222         if (!he) {
223             JS_ReportOutOfMemory(cx);
224             return NULL;
225         }
226
227         ida = JS_Enumerate(cx, obj);
228         if (!ida)
229             return NULL;
230
231         ok = JS_TRUE;
232         for (i = 0, length = ida->length; i < length; i++) {
233             id = ida->vector[i];
234             ok = obj->lookupProperty(cx, id, &obj2, &prop);
235             if (!ok)
236                 break;
237             if (!prop)
238                 continue;
239             bool hasGetter, hasSetter;
240             AutoValueRooter v(cx);
241             AutoValueRooter setter(cx);
242             if (obj2->isNative()) {
243                 const Shape *shape = (Shape *) prop;
244                 hasGetter = shape->hasGetterValue();
245                 hasSetter = shape->hasSetterValue();
246                 if (hasGetter)
247                     v.set(shape->getterValue());
248                 if (hasSetter)
249                     setter.set(shape->setterValue());
250             } else {
251                 hasGetter = hasSetter = false;
252             }
253             if (hasSetter) {
254                 /* Mark the getter, then set val to setter. */
255                 if (hasGetter && v.value().isObject()) {
256                     ok = !!MarkSharpObjects(cx, &v.value().toObject(), NULL);
257                     if (!ok)
258                         break;
259                 }
260                 v.set(setter.value());
261             } else if (!hasGetter) {
262                 ok = obj->getProperty(cx, id, v.addr());
263                 if (!ok)
264                     break;
265             }
266             if (v.value().isObject() &&
267                 !MarkSharpObjects(cx, &v.value().toObject(), NULL)) {
268                 ok = JS_FALSE;
269                 break;
270             }
271         }
272         if (!ok || !idap)
273             JS_DestroyIdArray(cx, ida);
274         if (!ok)
275             return NULL;
276     } else {
277         sharpid = uintptr_t(he->value);
278         if (sharpid == 0) {
279             sharpid = ++map->sharpgen << SHARP_ID_SHIFT;
280             he->value = (void *) sharpid;
281         }
282         ida = NULL;
283     }
284     if (idap)
285         *idap = ida;
286     return he;
287 }
288
289 JSHashEntry *
290 js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
291                     jschar **sp)
292 {
293     JSSharpObjectMap *map;
294     JSHashTable *table;
295     JSIdArray *ida;
296     JSHashNumber hash;
297     JSHashEntry *he, **hep;
298     jsatomid sharpid;
299     char buf[20];
300     size_t len;
301
302     if (!JS_CHECK_OPERATION_LIMIT(cx))
303         return NULL;
304
305     /* Set to null in case we return an early error. */
306     *sp = NULL;
307     map = &cx->sharpObjectMap;
308     table = map->table;
309     if (!table) {
310         table = JS_NewHashTable(8, js_hash_object, JS_CompareValues,
311                                 JS_CompareValues, NULL, NULL);
312         if (!table) {
313             JS_ReportOutOfMemory(cx);
314             return NULL;
315         }
316         map->table = table;
317         JS_KEEP_ATOMS(cx->runtime);
318     }
319
320     /* From this point the control must flow either through out: or bad:. */
321     ida = NULL;
322     if (map->depth == 0) {
323         /*
324          * Although MarkSharpObjects tries to avoid invoking getters,
325          * it ends up doing so anyway under some circumstances; for
326          * example, if a wrapped object has getters, the wrapper will
327          * prevent MarkSharpObjects from recognizing them as such.
328          * This could lead to js_LeaveSharpObject being called while
329          * MarkSharpObjects is still working.
330          *
331          * Increment map->depth while we call MarkSharpObjects, to
332          * ensure that such a call doesn't free the hash table we're
333          * still using.
334          */
335         ++map->depth;
336         he = MarkSharpObjects(cx, obj, &ida);
337         --map->depth;
338         if (!he)
339             goto bad;
340         JS_ASSERT((uintptr_t(he->value) & SHARP_BIT) == 0);
341         if (!idap) {
342             JS_DestroyIdArray(cx, ida);
343             ida = NULL;
344         }
345     } else {
346         hash = js_hash_object(obj);
347         hep = JS_HashTableRawLookup(table, hash, obj);
348         he = *hep;
349
350         /*
351          * It's possible that the value of a property has changed from the
352          * first time the object's properties are traversed (when the property
353          * ids are entered into the hash table) to the second (when they are
354          * converted to strings), i.e., the JSObject::getProperty() call is not
355          * idempotent.
356          */
357         if (!he) {
358             he = JS_HashTableRawAdd(table, hep, hash, obj, NULL);
359             if (!he) {
360                 JS_ReportOutOfMemory(cx);
361                 goto bad;
362             }
363             sharpid = 0;
364             goto out;
365         }
366     }
367
368     sharpid = uintptr_t(he->value);
369     if (sharpid != 0) {
370         len = JS_snprintf(buf, sizeof buf, "#%u%c",
371                           sharpid >> SHARP_ID_SHIFT,
372                           (sharpid & SHARP_BIT) ? '#' : '=');
373         *sp = js_InflateString(cx, buf, &len);
374         if (!*sp) {
375             if (ida)
376                 JS_DestroyIdArray(cx, ida);
377             goto bad;
378         }
379     }
380
381 out:
382     JS_ASSERT(he);
383     if ((sharpid & SHARP_BIT) == 0) {
384         if (idap && !ida) {
385             ida = JS_Enumerate(cx, obj);
386             if (!ida) {
387                 if (*sp) {
388                     cx->free(*sp);
389                     *sp = NULL;
390                 }
391                 goto bad;
392             }
393         }
394         map->depth++;
395     }
396
397     if (idap)
398         *idap = ida;
399     return he;
400
401 bad:
402     /* Clean up the sharpObjectMap table on outermost error. */
403     if (map->depth == 0) {
404         JS_UNKEEP_ATOMS(cx->runtime);
405         map->sharpgen = 0;
406         JS_HashTableDestroy(map->table);
407         map->table = NULL;
408     }
409     return NULL;
410 }
411
412 void
413 js_LeaveSharpObject(JSContext *cx, JSIdArray **idap)
414 {
415     JSSharpObjectMap *map;
416     JSIdArray *ida;
417
418     map = &cx->sharpObjectMap;
419     JS_ASSERT(map->depth > 0);
420     if (--map->depth == 0) {
421         JS_UNKEEP_ATOMS(cx->runtime);
422         map->sharpgen = 0;
423         JS_HashTableDestroy(map->table);
424         map->table = NULL;
425     }
426     if (idap) {
427         ida = *idap;
428         if (ida) {
429             JS_DestroyIdArray(cx, ida);
430             *idap = NULL;
431         }
432     }
433 }
434
435 static intN
436 gc_sharp_table_entry_marker(JSHashEntry *he, intN i, void *arg)
437 {
438     MarkObject((JSTracer *)arg, *(JSObject *)he->key, "sharp table entry");
439     return JS_DHASH_NEXT;
440 }
441
442 void
443 js_TraceSharpMap(JSTracer *trc, JSSharpObjectMap *map)
444 {
445     JS_ASSERT(map->depth > 0);
446     JS_ASSERT(map->table);
447
448     /*
449      * During recursive calls to MarkSharpObjects a non-native object or
450      * object with a custom getProperty method can potentially return an
451      * unrooted value or even cut from the object graph an argument of one of
452      * MarkSharpObjects recursive invocations. So we must protect map->table
453      * entries against GC.
454      *
455      * We can not simply use JSTempValueRooter to mark the obj argument of
456      * MarkSharpObjects during recursion as we have to protect *all* entries
457      * in JSSharpObjectMap including those that contains otherwise unreachable
458      * objects just allocated through custom getProperty. Otherwise newer
459      * allocations can re-use the address of an object stored in the hashtable
460      * confusing js_EnterSharpObject. So to address the problem we simply
461      * mark all objects from map->table.
462      *
463      * An alternative "proper" solution is to use JSTempValueRooter in
464      * MarkSharpObjects with code to remove during finalization entries
465      * with otherwise unreachable objects. But this is way too complex
466      * to justify spending efforts.
467      */
468     JS_HashTableEnumerateEntries(map->table, gc_sharp_table_entry_marker, trc);
469 }
470
471 #if JS_HAS_TOSOURCE
472 static JSBool
473 obj_toSource(JSContext *cx, uintN argc, Value *vp)
474 {
475     JSBool ok;
476     JSHashEntry *he;
477     JSIdArray *ida;
478     jschar *chars, *ochars, *vsharp;
479     const jschar *idstrchars, *vchars;
480     size_t nchars, idstrlength, gsoplength, vlength, vsharplength, curlen;
481     const char *comma;
482     JSObject *obj2;
483     JSProperty *prop;
484     Value *val;
485     JSString *gsop[2];
486     JSString *valstr, *str;
487     JSLinearString *idstr;
488
489     JS_CHECK_RECURSION(cx, return JS_FALSE);
490
491     Value localroot[4];
492     PodArrayZero(localroot);
493     AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(localroot), localroot);
494
495     /* If outermost, we need parentheses to be an expression, not a block. */
496     JSBool outermost = (cx->sharpObjectMap.depth == 0);
497
498     JSObject *obj = ToObject(cx, &vp[1]);
499     if (!obj)
500         return false;
501
502     if (!(he = js_EnterSharpObject(cx, obj, &ida, &chars))) {
503         ok = JS_FALSE;
504         goto out;
505     }
506     if (IS_SHARP(he)) {
507         /*
508          * We didn't enter -- obj is already "sharp", meaning we've visited it
509          * already in our depth first search, and therefore chars contains a
510          * string of the form "#n#".
511          */
512         JS_ASSERT(!ida);
513 #if JS_HAS_SHARP_VARS
514         nchars = js_strlen(chars);
515 #else
516         chars[0] = '{';
517         chars[1] = '}';
518         chars[2] = 0;
519         nchars = 2;
520 #endif
521         goto make_string;
522     }
523     JS_ASSERT(ida);
524     ok = JS_TRUE;
525
526     if (!chars) {
527         /* If outermost, allocate 4 + 1 for "({})" and the terminator. */
528         chars = (jschar *) cx->runtime->malloc(((outermost ? 4 : 2) + 1) * sizeof(jschar));
529         nchars = 0;
530         if (!chars)
531             goto error;
532         if (outermost)
533             chars[nchars++] = '(';
534     } else {
535         /* js_EnterSharpObject returned a string of the form "#n=" in chars. */
536         MAKE_SHARP(he);
537         nchars = js_strlen(chars);
538         chars = (jschar *)
539             js_realloc((ochars = chars), (nchars + 2 + 1) * sizeof(jschar));
540         if (!chars) {
541             js_free(ochars);
542             goto error;
543         }
544         if (outermost) {
545             /*
546              * No need for parentheses around the whole shebang, because #n=
547              * unambiguously begins an object initializer, and never a block
548              * statement.
549              */
550             outermost = JS_FALSE;
551         }
552     }
553
554     chars[nchars++] = '{';
555
556     comma = NULL;
557
558     /*
559      * We have four local roots for cooked and raw value GC safety.  Hoist the
560      * "localroot + 2" out of the loop using the val local, which refers to
561      * the raw (unconverted, "uncooked") values.
562      */
563     val = localroot + 2;
564
565     for (jsint i = 0, length = ida->length; i < length; i++) {
566         /* Get strings for id and value and GC-root them via vp. */
567         jsid id = ida->vector[i];
568
569         ok = obj->lookupProperty(cx, id, &obj2, &prop);
570         if (!ok)
571             goto error;
572
573         /*
574          * Convert id to a value and then to a string.  Decide early whether we
575          * prefer get/set or old getter/setter syntax.
576          */
577         JSString *s = js_ValueToString(cx, IdToValue(id));
578         if (!s || !(idstr = s->ensureLinear(cx))) {
579             ok = JS_FALSE;
580             goto error;
581         }
582         vp->setString(idstr);                           /* local root */
583
584         jsint valcnt = 0;
585         if (prop) {
586             bool doGet = true;
587             if (obj2->isNative()) {
588                 const Shape *shape = (Shape *) prop;
589                 unsigned attrs = shape->attributes();
590                 if (attrs & JSPROP_GETTER) {
591                     doGet = false;
592                     val[valcnt] = shape->getterValue();
593                     gsop[valcnt] = ATOM_TO_STRING(cx->runtime->atomState.getAtom);
594                     valcnt++;
595                 }
596                 if (attrs & JSPROP_SETTER) {
597                     doGet = false;
598                     val[valcnt] = shape->setterValue();
599                     gsop[valcnt] = ATOM_TO_STRING(cx->runtime->atomState.setAtom);
600                     valcnt++;
601                 }
602             }
603             if (doGet) {
604                 valcnt = 1;
605                 gsop[0] = NULL;
606                 ok = obj->getProperty(cx, id, &val[0]);
607                 if (!ok)
608                     goto error;
609             }
610         }
611
612         /*
613          * If id is a string that's not an identifier, or if it's a negative
614          * integer, then it must be quoted.
615          */
616         bool idIsLexicalIdentifier = js_IsIdentifier(idstr);
617         if (JSID_IS_ATOM(id)
618             ? !idIsLexicalIdentifier
619             : (!JSID_IS_INT(id) || JSID_TO_INT(id) < 0)) {
620             s = js_QuoteString(cx, idstr, jschar('\''));
621             if (!s || !(idstr = s->ensureLinear(cx))) {
622                 ok = JS_FALSE;
623                 goto error;
624             }
625             vp->setString(idstr);                       /* local root */
626         }
627         idstrlength = idstr->length();
628         idstrchars = idstr->getChars(cx);
629         if (!idstrchars) {
630             ok = JS_FALSE;
631             goto error;
632         }
633
634         for (jsint j = 0; j < valcnt; j++) {
635             /*
636              * Censor an accessor descriptor getter or setter part if it's
637              * undefined.
638              */
639             if (gsop[j] && val[j].isUndefined())
640                 continue;
641
642             /* Convert val[j] to its canonical source form. */
643             valstr = js_ValueToSource(cx, val[j]);
644             if (!valstr) {
645                 ok = JS_FALSE;
646                 goto error;
647             }
648             localroot[j].setString(valstr);             /* local root */
649             vchars = valstr->getChars(cx);
650             if (!vchars) {
651                 ok = JS_FALSE;
652                 goto error;
653             }
654             vlength = valstr->length();
655
656             /*
657              * If val[j] is a non-sharp object, and we're not serializing an
658              * accessor (ECMA syntax can't accommodate sharpened accessors),
659              * consider sharpening it.
660              */
661             vsharp = NULL;
662             vsharplength = 0;
663 #if JS_HAS_SHARP_VARS
664             if (!gsop[j] && val[j].isObject() && vchars[0] != '#') {
665                 he = js_EnterSharpObject(cx, &val[j].toObject(), NULL, &vsharp);
666                 if (!he) {
667                     ok = JS_FALSE;
668                     goto error;
669                 }
670                 if (IS_SHARP(he)) {
671                     vchars = vsharp;
672                     vlength = js_strlen(vchars);
673                 } else {
674                     if (vsharp) {
675                         vsharplength = js_strlen(vsharp);
676                         MAKE_SHARP(he);
677                     }
678                     js_LeaveSharpObject(cx, NULL);
679                 }
680             }
681 #endif
682
683             /*
684              * Remove '(function ' from the beginning of valstr and ')' from the
685              * end so that we can put "get" in front of the function definition.
686              */
687             if (gsop[j] && IsFunctionObject(val[j])) {
688                 const jschar *start = vchars;
689                 const jschar *end = vchars + vlength;
690
691                 uint8 parenChomp = 0;
692                 if (vchars[0] == '(') {
693                     vchars++;
694                     parenChomp = 1;
695                 }
696
697                 /* Try to jump "function" keyword. */
698                 if (vchars)
699                     vchars = js_strchr_limit(vchars, ' ', end);
700
701                 /*
702                  * Jump over the function's name: it can't be encoded as part
703                  * of an ECMA getter or setter.
704                  */
705                 if (vchars)
706                     vchars = js_strchr_limit(vchars, '(', end);
707
708                 if (vchars) {
709                     if (*vchars == ' ')
710                         vchars++;
711                     vlength = end - vchars - parenChomp;
712                 } else {
713                     gsop[j] = NULL;
714                     vchars = start;
715                 }
716             }
717
718 #define SAFE_ADD(n)                                                          \
719     JS_BEGIN_MACRO                                                           \
720         size_t n_ = (n);                                                     \
721         curlen += n_;                                                        \
722         if (curlen < n_)                                                     \
723             goto overflow;                                                   \
724     JS_END_MACRO
725
726             curlen = nchars;
727             if (comma)
728                 SAFE_ADD(2);
729             SAFE_ADD(idstrlength + 1);
730             if (gsop[j])
731                 SAFE_ADD(gsop[j]->length() + 1);
732             SAFE_ADD(vsharplength);
733             SAFE_ADD(vlength);
734             /* Account for the trailing null. */
735             SAFE_ADD((outermost ? 2 : 1) + 1);
736 #undef SAFE_ADD
737
738             if (curlen > size_t(-1) / sizeof(jschar))
739                 goto overflow;
740
741             /* Allocate 1 + 1 at end for closing brace and terminating 0. */
742             chars = (jschar *) js_realloc((ochars = chars), curlen * sizeof(jschar));
743             if (!chars) {
744                 chars = ochars;
745                 goto overflow;
746             }
747
748             if (comma) {
749                 chars[nchars++] = comma[0];
750                 chars[nchars++] = comma[1];
751             }
752             comma = ", ";
753
754             if (gsop[j]) {
755                 gsoplength = gsop[j]->length();
756                 const jschar *gsopchars = gsop[j]->getChars(cx);
757                 if (!gsopchars)
758                     goto overflow;
759                 js_strncpy(&chars[nchars], gsopchars, gsoplength);
760                 nchars += gsoplength;
761                 chars[nchars++] = ' ';
762             }
763             js_strncpy(&chars[nchars], idstrchars, idstrlength);
764             nchars += idstrlength;
765             /* Extraneous space after id here will be extracted later */
766             chars[nchars++] = gsop[j] ? ' ' : ':';
767
768             if (vsharplength) {
769                 js_strncpy(&chars[nchars], vsharp, vsharplength);
770                 nchars += vsharplength;
771             }
772             js_strncpy(&chars[nchars], vchars, vlength);
773             nchars += vlength;
774
775             if (vsharp)
776                 cx->free(vsharp);
777         }
778     }
779
780     chars[nchars++] = '}';
781     if (outermost)
782         chars[nchars++] = ')';
783     chars[nchars] = 0;
784
785   error:
786     js_LeaveSharpObject(cx, &ida);
787
788     if (!ok) {
789         if (chars)
790             js_free(chars);
791         goto out;
792     }
793
794     if (!chars) {
795         JS_ReportOutOfMemory(cx);
796         ok = JS_FALSE;
797         goto out;
798     }
799   make_string:
800     str = js_NewString(cx, chars, nchars);
801     if (!str) {
802         js_free(chars);
803         ok = JS_FALSE;
804         goto out;
805     }
806     vp->setString(str);
807     ok = JS_TRUE;
808   out:
809     return ok;
810
811   overflow:
812     cx->free(vsharp);
813     js_free(chars);
814     chars = NULL;
815     goto error;
816 }
817 #endif /* JS_HAS_TOSOURCE */
818
819 namespace js {
820
821 JSString *
822 obj_toStringHelper(JSContext *cx, JSObject *obj)
823 {
824     if (obj->isProxy())
825         return JSProxy::obj_toString(cx, obj);
826
827     const char *clazz = obj->getClass()->name;
828     size_t nchars = 9 + strlen(clazz); /* 9 for "[object ]" */
829     jschar *chars = (jschar *) cx->malloc((nchars + 1) * sizeof(jschar));
830     if (!chars)
831         return NULL;
832
833     const char *prefix = "[object ";
834     nchars = 0;
835     while ((chars[nchars] = (jschar)*prefix) != 0)
836         nchars++, prefix++;
837     while ((chars[nchars] = (jschar)*clazz) != 0)
838         nchars++, clazz++;
839     chars[nchars++] = ']';
840     chars[nchars] = 0;
841
842     JSString *str = js_NewString(cx, chars, nchars);
843     if (!str)
844         cx->free(chars);
845     return str;
846 }
847
848 }
849
850 /* ES5 15.2.4.2.  Note steps 1 and 2 are errata. */
851 static JSBool
852 obj_toString(JSContext *cx, uintN argc, Value *vp)
853 {
854     Value &thisv = vp[1];
855
856     /* Step 1. */
857     if (thisv.isUndefined()) {
858         vp->setString(ATOM_TO_STRING(cx->runtime->atomState.objectUndefinedAtom));
859         return true;
860     }
861
862     /* Step 2. */
863     if (thisv.isNull()) {
864         vp->setString(ATOM_TO_STRING(cx->runtime->atomState.objectNullAtom));
865         return true;
866     }
867
868     /* Step 3. */
869     JSObject *obj = ToObject(cx, &thisv);
870     if (!obj)
871         return false;
872
873     /* Steps 4-5. */
874     JSString *str = js::obj_toStringHelper(cx, obj);
875     if (!str)
876         return false;
877     vp->setString(str);
878     return true;
879 }
880
881 static JSBool
882 obj_toLocaleString(JSContext *cx, uintN argc, Value *vp)
883 {
884     JSObject *obj = ToObject(cx, &vp[1]);
885     if (!obj)
886         return false;
887
888     JSString *str = js_ValueToString(cx, ObjectValue(*obj));
889     if (!str)
890         return JS_FALSE;
891
892     vp->setString(str);
893     return JS_TRUE;
894 }
895
896 static JSBool
897 obj_valueOf(JSContext *cx, uintN argc, Value *vp)
898 {
899     JSObject *obj = ToObject(cx, &vp[1]);
900     if (!obj)
901         return false;
902     vp->setObject(*obj);
903     return true;
904 }
905
906 /*
907  * Check if CSP allows new Function() or eval() to run in the current
908  * principals.
909  */
910 JSBool
911 js_CheckContentSecurityPolicy(JSContext *cx, JSObject *scopeobj)
912 {
913     // CSP is static per document, so if our check said yes before, that
914     // answer is still valid.
915     JSObject *global = scopeobj->getGlobal();
916     Value v = global->getReservedSlot(JSRESERVED_GLOBAL_EVAL_ALLOWED);
917     if (v.isUndefined()) {
918         JSSecurityCallbacks *callbacks = JS_GetSecurityCallbacks(cx);
919
920         // if there are callbacks, make sure that the CSP callback is installed and
921         // that it permits eval().
922         v.setBoolean((!callbacks || !callbacks->contentSecurityPolicyAllows) ||
923                      callbacks->contentSecurityPolicyAllows(cx));
924
925         // update the cache in the global object for the result of the security check
926         js_SetReservedSlot(cx, global, JSRESERVED_GLOBAL_EVAL_ALLOWED, v);
927     }
928     return !v.isFalse();
929 }
930
931 /*
932  * Check whether principals subsumes scopeobj's principals, and return true
933  * if so (or if scopeobj has no principals, for backward compatibility with
934  * the JS API, which does not require principals), and false otherwise.
935  */
936 JSBool
937 js_CheckPrincipalsAccess(JSContext *cx, JSObject *scopeobj,
938                          JSPrincipals *principals, JSAtom *caller)
939 {
940     JSSecurityCallbacks *callbacks;
941     JSPrincipals *scopePrincipals;
942
943     callbacks = JS_GetSecurityCallbacks(cx);
944     if (callbacks && callbacks->findObjectPrincipals) {
945         scopePrincipals = callbacks->findObjectPrincipals(cx, scopeobj);
946         if (!principals || !scopePrincipals ||
947             !principals->subsume(principals, scopePrincipals)) {
948             JSAutoByteString callerstr;
949             if (!js_AtomToPrintableString(cx, caller, &callerstr))
950                 return JS_FALSE;
951             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
952                                  JSMSG_BAD_INDIRECT_CALL, callerstr.ptr());
953             return JS_FALSE;
954         }
955     }
956     return JS_TRUE;
957 }
958
959 static bool
960 CheckScopeChainValidity(JSContext *cx, JSObject *scopeobj)
961 {
962     JSObject *inner = scopeobj;
963     OBJ_TO_INNER_OBJECT(cx, inner);
964     if (!inner)
965         return false;
966     JS_ASSERT(inner == scopeobj);
967
968     /* XXX This is an awful gross hack. */
969     while (scopeobj) {
970         JSObjectOp op = scopeobj->getClass()->ext.innerObject;
971         if (op && op(cx, scopeobj) != scopeobj) {
972             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_INDIRECT_CALL,
973                                  js_eval_str);
974             return false;
975         }
976         scopeobj = scopeobj->getParent();
977     }
978
979     return true;
980 }
981
982 const char *
983 js_ComputeFilename(JSContext *cx, JSStackFrame *caller,
984                    JSPrincipals *principals, uintN *linenop)
985 {
986     uint32 flags;
987 #ifdef DEBUG
988     JSSecurityCallbacks *callbacks = JS_GetSecurityCallbacks(cx);
989 #endif
990
991     JS_ASSERT(principals || !(callbacks  && callbacks->findObjectPrincipals));
992     flags = JS_GetScriptFilenameFlags(caller->script());
993     if ((flags & JSFILENAME_PROTECTED) &&
994         principals &&
995         strcmp(principals->codebase, "[System Principal]")) {
996         *linenop = 0;
997         return principals->codebase;
998     }
999
1000     jsbytecode *pc = caller->pc(cx);
1001     if (pc && js_GetOpcode(cx, caller->script(), pc) == JSOP_EVAL) {
1002         JS_ASSERT(js_GetOpcode(cx, caller->script(), pc + JSOP_EVAL_LENGTH) == JSOP_LINENO);
1003         *linenop = GET_UINT16(pc + JSOP_EVAL_LENGTH);
1004     } else {
1005         *linenop = js_FramePCToLineNumber(cx, caller);
1006     }
1007     return caller->script()->filename;
1008 }
1009
1010 #ifndef EVAL_CACHE_CHAIN_LIMIT
1011 # define EVAL_CACHE_CHAIN_LIMIT 4
1012 #endif
1013
1014 static inline JSScript **
1015 EvalCacheHash(JSContext *cx, JSLinearString *str)
1016 {
1017     const jschar *s = str->chars();
1018     size_t n = str->length();
1019
1020     if (n > 100)
1021         n = 100;
1022     uint32 h;
1023     for (h = 0; n; s++, n--)
1024         h = JS_ROTATE_LEFT32(h, 4) ^ *s;
1025
1026     h *= JS_GOLDEN_RATIO;
1027     h >>= 32 - JS_EVAL_CACHE_SHIFT;
1028     return &JS_SCRIPTS_TO_GC(cx)[h];
1029 }
1030
1031 static JS_ALWAYS_INLINE JSScript *
1032 EvalCacheLookup(JSContext *cx, JSLinearString *str, JSStackFrame *caller, uintN staticLevel,
1033                 JSPrincipals *principals, JSObject *scopeobj, JSScript **bucket)
1034 {
1035     /*
1036      * Cache local eval scripts indexed by source qualified by scope.
1037      *
1038      * An eval cache entry should never be considered a hit unless its
1039      * strictness matches that of the new eval code. The existing code takes
1040      * care of this, because hits are qualified by the function from which
1041      * eval was called, whose strictness doesn't change. (We don't cache evals
1042      * in eval code, so the calling function corresponds to the calling script,
1043      * and its strictness never varies.) Scripts produced by calls to eval from
1044      * global code aren't cached.
1045      *
1046      * FIXME bug 620141: Qualify hits by calling script rather than function.
1047      * Then we wouldn't need the unintuitive !isEvalFrame() hack in EvalKernel
1048      * to avoid caching nested evals in functions (thus potentially mismatching
1049      * on strict mode), and we could cache evals in global code if desired.
1050      */
1051     uintN count = 0;
1052     JSScript **scriptp = bucket;
1053
1054     EVAL_CACHE_METER(probe);
1055     JSVersion version = cx->findVersion();
1056     JSScript *script;
1057     while ((script = *scriptp) != NULL) {
1058         if (script->savedCallerFun &&
1059             script->staticLevel == staticLevel &&
1060             script->getVersion() == version &&
1061             !script->hasSingletons &&
1062             (script->principals == principals ||
1063              (principals->subsume(principals, script->principals) &&
1064               script->principals->subsume(script->principals, principals)))) {
1065             /*
1066              * Get the prior (cache-filling) eval's saved caller function.
1067              * See Compiler::compileScript in jsparse.cpp.
1068              */
1069             JSFunction *fun = script->getFunction(0);
1070
1071             if (fun == caller->fun()) {
1072                 /*
1073                  * Get the source string passed for safekeeping in the
1074                  * atom map by the prior eval to Compiler::compileScript.
1075                  */
1076                 JSAtom *src = script->atomMap.vector[0];
1077
1078                 if (src == str || EqualStrings(src, str)) {
1079                     /*
1080                      * Source matches, qualify by comparing scopeobj to the
1081                      * COMPILE_N_GO-memoized parent of the first literal
1082                      * function or regexp object if any. If none, then this
1083                      * script has no compiled-in dependencies on the prior
1084                      * eval's scopeobj.
1085                      */
1086                     JSObjectArray *objarray = script->objects();
1087                     int i = 1;
1088
1089                     if (objarray->length == 1) {
1090                         if (JSScript::isValidOffset(script->regexpsOffset)) {
1091                             objarray = script->regexps();
1092                             i = 0;
1093                         } else {
1094                             EVAL_CACHE_METER(noscope);
1095                             i = -1;
1096                         }
1097                     }
1098                     if (i < 0 ||
1099                         objarray->vector[i]->getParent() == scopeobj) {
1100                         JS_ASSERT(staticLevel == script->staticLevel);
1101                         EVAL_CACHE_METER(hit);
1102                         *scriptp = script->u.nextToGC;
1103                         script->u.nextToGC = NULL;
1104                         return script;
1105                     }
1106                 }
1107             }
1108         }
1109
1110         if (++count == EVAL_CACHE_CHAIN_LIMIT)
1111             return NULL;
1112         EVAL_CACHE_METER(step);
1113         scriptp = &script->u.nextToGC;
1114     }
1115     return NULL;
1116 }
1117
1118 /* ES5 15.1.2.1. */
1119 static JSBool
1120 eval(JSContext *cx, uintN argc, Value *vp)
1121 {
1122     /*
1123      * NB: This method handles only indirect eval: direct eval is handled by
1124      *     JSOP_EVAL.
1125      */
1126
1127     JSStackFrame *caller = js_GetScriptedCaller(cx, NULL);
1128
1129     /* FIXME Bug 602994: This really should be perfectly cromulent. */
1130     if (!caller) {
1131         /* Eval code needs to inherit principals from the caller. */
1132         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1133                              JSMSG_BAD_INDIRECT_CALL, js_eval_str);
1134         return false;
1135     }
1136
1137     return EvalKernel(cx, argc, vp, INDIRECT_EVAL, caller, vp[0].toObject().getGlobal());
1138 }
1139
1140 namespace js {
1141
1142 bool
1143 EvalKernel(JSContext *cx, uintN argc, Value *vp, EvalType evalType, JSStackFrame *caller,
1144            JSObject *scopeobj)
1145 {
1146     /*
1147      * FIXME Bug 602994: Calls with no scripted caller should be permitted and
1148      *       should be implemented as indirect calls.
1149      */
1150     JS_ASSERT(caller);
1151     JS_ASSERT(scopeobj);
1152
1153     /*
1154      * We once supported a second argument to eval to use as the scope chain
1155      * when evaluating the code string.  Warn when such uses are seen so that
1156      * authors will know that support for eval(s, o) has been removed.
1157      */
1158     JSScript *callerScript = caller->script();
1159     if (argc > 1 && !callerScript->warnedAboutTwoArgumentEval) {
1160         static const char TWO_ARGUMENT_WARNING[] =
1161             "Support for eval(code, scopeObject) has been removed. "
1162             "Use |with (scopeObject) eval(code);| instead.";
1163         if (!JS_ReportWarning(cx, TWO_ARGUMENT_WARNING))
1164             return false;
1165         callerScript->warnedAboutTwoArgumentEval = true;
1166     }
1167
1168     /*
1169      * CSP check: Is eval() allowed at all?
1170      * Report errors via CSP is done in the script security mgr.
1171      */
1172     if (!js_CheckContentSecurityPolicy(cx, scopeobj)) {
1173         JS_ReportError(cx, "call to eval() blocked by CSP");
1174         return false;
1175     }
1176
1177     /* ES5 15.1.2.1 step 1. */
1178     if (argc < 1) {
1179         vp->setUndefined();
1180         return true;
1181     }
1182     if (!vp[2].isString()) {
1183         *vp = vp[2];
1184         return true;
1185     }
1186     JSString *str = vp[2].toString();
1187
1188     /* ES5 15.1.2.1 steps 2-8. */
1189     JSObject *callee = JSVAL_TO_OBJECT(JS_CALLEE(cx, Jsvalify(vp)));
1190     JS_ASSERT(IsBuiltinEvalFunction(callee->getFunctionPrivate()));
1191     JSPrincipals *principals = js_EvalFramePrincipals(cx, callee, caller);
1192
1193     /*
1194      * Per ES5, indirect eval runs in the global scope. (eval is specified this
1195      * way so that the compiler can make assumptions about what bindings may or
1196      * may not exist in the current frame if it doesn't see 'eval'.)
1197      */
1198     uintN staticLevel;
1199     if (evalType == DIRECT_EVAL) {
1200         staticLevel = caller->script()->staticLevel + 1;
1201
1202 #ifdef DEBUG
1203         jsbytecode *callerPC = caller->pc(cx);
1204         JS_ASSERT_IF(caller->isFunctionFrame(), caller->hasCallObj());
1205         JS_ASSERT(callerPC && js_GetOpcode(cx, caller->script(), callerPC) == JSOP_EVAL);
1206 #endif
1207     } else {
1208         /* Pretend that we're top level. */
1209         staticLevel = 0;
1210
1211         JS_ASSERT(scopeobj == scopeobj->getGlobal());
1212         JS_ASSERT(scopeobj->isGlobal());
1213     }
1214
1215     /* Ensure we compile this eval with the right object in the scope chain. */
1216     if (!CheckScopeChainValidity(cx, scopeobj))
1217         return false;
1218
1219     JSLinearString *linearStr = str->ensureLinear(cx);
1220     if (!linearStr)
1221         return false;
1222     const jschar *chars = linearStr->chars();
1223     size_t length = linearStr->length();
1224
1225     /*
1226      * If the eval string starts with '(' and ends with ')', it may be JSON.
1227      * Try the JSON parser first because it's much faster.  If the eval string
1228      * isn't JSON, JSON parsing will probably fail quickly, so little time
1229      * will be lost.
1230      */
1231     if (length > 2 && chars[0] == '(' && chars[length - 1] == ')') {
1232         JSONParser *jp = js_BeginJSONParse(cx, vp, /* suppressErrors = */true);
1233         if (jp != NULL) {
1234             /* Run JSON-parser on string inside ( and ). */
1235             bool ok = js_ConsumeJSONText(cx, jp, chars + 1, length - 2);
1236             ok &= js_FinishJSONParse(cx, jp, NullValue());
1237             if (ok)
1238                 return true;
1239         }
1240     }
1241
1242     /*
1243      * Direct calls to eval are supposed to see the caller's |this|. If we
1244      * haven't wrapped that yet, do so now, before we make a copy of it for
1245      * the eval code to use.
1246      */
1247     if (evalType == DIRECT_EVAL && !caller->computeThis(cx))
1248         return false;
1249
1250     JSScript *script = NULL;
1251     JSScript **bucket = EvalCacheHash(cx, linearStr);
1252     if (evalType == DIRECT_EVAL && caller->isFunctionFrame() && !caller->isEvalFrame()) {
1253         script = EvalCacheLookup(cx, linearStr, caller, staticLevel, principals, scopeobj, bucket);
1254
1255         /*
1256          * Although the eval cache keeps a script alive from the perspective of
1257          * the JS engine, from a jsdbgapi user's perspective each eval()
1258          * creates and destroys a script. This hides implementation details and
1259          * allows jsdbgapi clients to avoid calling JS_GetScriptObject after a
1260          * script has been returned to the eval cache, which is invalid since
1261          * script->u.object aliases script->u.nextToGC.
1262          */
1263         if (script) {
1264             js_CallNewScriptHook(cx, script, NULL);
1265             MUST_FLOW_THROUGH("destroy");
1266         }
1267     }
1268
1269     /*
1270      * We can't have a callerFrame (down in js::Execute's terms) if we're in
1271      * global code (or if we're an indirect eval).
1272      */
1273     JSStackFrame *callerFrame = (staticLevel != 0) ? caller : NULL;
1274     if (!script) {
1275         uintN lineno;
1276         const char *filename = js_ComputeFilename(cx, caller, principals, &lineno);
1277
1278         uint32 tcflags = TCF_COMPILE_N_GO | TCF_NEED_MUTABLE_SCRIPT | TCF_COMPILE_FOR_EVAL;
1279         script = Compiler::compileScript(cx, scopeobj, callerFrame,
1280                                          principals, tcflags, chars, length,
1281                                          filename, lineno, cx->findVersion(),
1282                                          linearStr, staticLevel);
1283         if (!script)
1284             return false;
1285     }
1286
1287     assertSameCompartment(cx, scopeobj, script);
1288
1289     /*
1290      * Belt-and-braces: check that the lesser of eval's principals and the
1291      * caller's principals has access to scopeobj.
1292      */
1293     JSBool ok = js_CheckPrincipalsAccess(cx, scopeobj, principals,
1294                                          cx->runtime->atomState.evalAtom) &&
1295                 Execute(cx, scopeobj, script, callerFrame, JSFRAME_EVAL, vp);
1296
1297     MUST_FLOW_LABEL(destroy);
1298     js_CallDestroyScriptHook(cx, script);
1299
1300     script->u.nextToGC = *bucket;
1301     *bucket = script;
1302 #ifdef CHECK_SCRIPT_OWNER
1303     script->owner = NULL;
1304 #endif
1305
1306     return ok;
1307 }
1308
1309 JS_FRIEND_API(bool)
1310 IsBuiltinEvalFunction(JSFunction *fun)
1311 {
1312     return fun->maybeNative() == eval;
1313 }
1314
1315 }
1316
1317 #if JS_HAS_OBJ_WATCHPOINT
1318
1319 static JSBool
1320 obj_watch_handler(JSContext *cx, JSObject *obj, jsid id, jsval old,
1321                   jsval *nvp, void *closure)
1322 {
1323     JSObject *callable;
1324     JSSecurityCallbacks *callbacks;
1325     JSStackFrame *caller;
1326     JSPrincipals *subject, *watcher;
1327     JSResolvingKey key;
1328     JSResolvingEntry *entry;
1329     uint32 generation;
1330     Value argv[3];
1331     JSBool ok;
1332
1333     callable = (JSObject *) closure;
1334
1335     callbacks = JS_GetSecurityCallbacks(cx);
1336     if (callbacks && callbacks->findObjectPrincipals) {
1337         /* Skip over any obj_watch_* frames between us and the real subject. */
1338         caller = js_GetScriptedCaller(cx, NULL);
1339         if (caller) {
1340             /*
1341              * Only call the watch handler if the watcher is allowed to watch
1342              * the currently executing script.
1343              */
1344             watcher = callbacks->findObjectPrincipals(cx, callable);
1345             subject = js_StackFramePrincipals(cx, caller);
1346
1347             if (watcher && subject && !watcher->subsume(watcher, subject)) {
1348                 /* Silently don't call the watch handler. */
1349                 return JS_TRUE;
1350             }
1351         }
1352     }
1353
1354     /* Avoid recursion on (obj, id) already being watched on cx. */
1355     key.obj = obj;
1356     key.id = id;
1357     if (!js_StartResolving(cx, &key, JSRESFLAG_WATCH, &entry))
1358         return JS_FALSE;
1359     if (!entry)
1360         return JS_TRUE;
1361     generation = cx->resolvingTable->generation;
1362
1363     argv[0] = IdToValue(id);
1364     argv[1] = Valueify(old);
1365     argv[2] = Valueify(*nvp);
1366     ok = ExternalInvoke(cx, ObjectValue(*obj), ObjectOrNullValue(callable), 3, argv,
1367                         Valueify(nvp));
1368     js_StopResolving(cx, &key, JSRESFLAG_WATCH, entry, generation);
1369     return ok;
1370 }
1371
1372 static JSBool
1373 obj_watch(JSContext *cx, uintN argc, Value *vp)
1374 {
1375     if (argc <= 1) {
1376         js_ReportMissingArg(cx, *vp, 1);
1377         return JS_FALSE;
1378     }
1379
1380     JSObject *callable = js_ValueToCallableObject(cx, &vp[3], 0);
1381     if (!callable)
1382         return JS_FALSE;
1383
1384     /* Compute the unique int/atom symbol id needed by js_LookupProperty. */
1385     jsid propid;
1386     if (!ValueToId(cx, vp[2], &propid))
1387         return JS_FALSE;
1388
1389     JSObject *obj = ToObject(cx, &vp[1]);
1390     if (!obj)
1391         return false;
1392
1393     Value tmp;
1394     uintN attrs;
1395     if (!CheckAccess(cx, obj, propid, JSACC_WATCH, &tmp, &attrs))
1396         return JS_FALSE;
1397
1398     vp->setUndefined();
1399
1400     if (attrs & JSPROP_READONLY)
1401         return JS_TRUE;
1402     if (obj->isDenseArray() && !obj->makeDenseArraySlow(cx))
1403         return JS_FALSE;
1404     return JS_SetWatchPoint(cx, obj, propid, obj_watch_handler, callable);
1405 }
1406
1407 static JSBool
1408 obj_unwatch(JSContext *cx, uintN argc, Value *vp)
1409 {
1410     JSObject *obj = ToObject(cx, &vp[1]);
1411     if (!obj)
1412         return false;
1413     vp->setUndefined();
1414     jsid id;
1415     if (argc != 0) {
1416         if (!ValueToId(cx, vp[2], &id))
1417             return JS_FALSE;
1418     } else {
1419         id = JSID_VOID;
1420     }
1421     return JS_ClearWatchPoint(cx, obj, id, NULL, NULL);
1422 }
1423
1424 #endif /* JS_HAS_OBJ_WATCHPOINT */
1425
1426 /*
1427  * Prototype and property query methods, to complement the 'in' and
1428  * 'instanceof' operators.
1429  */
1430
1431 /* Proposed ECMA 15.2.4.5. */
1432 static JSBool
1433 obj_hasOwnProperty(JSContext *cx, uintN argc, Value *vp)
1434 {
1435     JSObject *obj = ToObject(cx, &vp[1]);
1436     if (!obj)
1437         return false;
1438     return js_HasOwnPropertyHelper(cx, obj->getOps()->lookupProperty, argc, vp);
1439 }
1440
1441 JSBool
1442 js_HasOwnPropertyHelper(JSContext *cx, LookupPropOp lookup, uintN argc,
1443                         Value *vp)
1444 {
1445     jsid id;
1446     if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
1447         return JS_FALSE;
1448
1449     JSObject *obj = ToObject(cx, &vp[1]);
1450     if (!obj)
1451         return false;
1452     JSObject *obj2;
1453     JSProperty *prop;
1454     if (obj->isProxy()) {
1455         bool has;
1456         if (!JSProxy::hasOwn(cx, obj, id, &has))
1457             return false;
1458         vp->setBoolean(has);
1459         return true;
1460     }
1461     if (!js_HasOwnProperty(cx, lookup, obj, id, &obj2, &prop))
1462         return JS_FALSE;
1463     vp->setBoolean(!!prop);
1464     return JS_TRUE;
1465 }
1466
1467 JSBool
1468 js_HasOwnProperty(JSContext *cx, LookupPropOp lookup, JSObject *obj, jsid id,
1469                   JSObject **objp, JSProperty **propp)
1470 {
1471     JSAutoResolveFlags rf(cx, JSRESOLVE_QUALIFIED | JSRESOLVE_DETECTING);
1472     if (!(lookup ? lookup : js_LookupProperty)(cx, obj, id, objp, propp))
1473         return false;
1474     if (!*propp)
1475         return true;
1476
1477     if (*objp == obj)
1478         return true;
1479
1480     Class *clasp = (*objp)->getClass();
1481     JSObject *outer = NULL;
1482     if (JSObjectOp op = (*objp)->getClass()->ext.outerObject) {
1483         outer = op(cx, *objp);
1484         if (!outer)
1485             return false;
1486     }
1487
1488     if (outer != *objp) {
1489         if ((*objp)->isNative() && obj->getClass() == clasp) {
1490             /*
1491              * The combination of JSPROP_SHARED and JSPROP_PERMANENT in a
1492              * delegated property makes that property appear to be direct in
1493              * all delegating instances of the same native class.  This hack
1494              * avoids bloating every function instance with its own 'length'
1495              * (AKA 'arity') property.  But it must not extend across class
1496              * boundaries, to avoid making hasOwnProperty lie (bug 320854).
1497              *
1498              * It's not really a hack, of course: a permanent property can't
1499              * be deleted, and JSPROP_SHARED means "don't allocate a slot in
1500              * any instance, prototype or delegating".  Without a slot, and
1501              * without the ability to remove and recreate (with differences)
1502              * the property, there is no way to tell whether it is directly
1503              * owned, or indirectly delegated.
1504              */
1505             Shape *shape = reinterpret_cast<Shape *>(*propp);
1506             if (shape->isSharedPermanent())
1507                 return true;
1508         }
1509
1510         *propp = NULL;
1511     }
1512     return true;
1513 }
1514
1515 /* ES5 15.2.4.6. */
1516 static JSBool
1517 obj_isPrototypeOf(JSContext *cx, uintN argc, Value *vp)
1518 {
1519     /* Step 1. */
1520     if (argc < 1 || !vp[2].isObject()) {
1521         vp->setBoolean(false);
1522         return true;
1523     }
1524
1525     /* Step 2. */
1526     JSObject *obj = ToObject(cx, &vp[1]);
1527     if (!obj)
1528         return false;
1529
1530     /* Step 3. */
1531     vp->setBoolean(js_IsDelegate(cx, obj, vp[2]));
1532     return true;
1533 }
1534
1535 /* ES5 15.2.4.7. */
1536 static JSBool
1537 obj_propertyIsEnumerable(JSContext *cx, uintN argc, Value *vp)
1538 {
1539     /* Step 1. */
1540     jsid id;
1541     if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
1542         return false;
1543
1544     /* Step 2. */
1545     JSObject *obj = ToObject(cx, &vp[1]);
1546     if (!obj)
1547         return false;
1548
1549     /* Steps 3-5. */
1550     return js_PropertyIsEnumerable(cx, obj, id, vp);
1551 }
1552
1553 JSBool
1554 js_PropertyIsEnumerable(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1555 {
1556     JSObject *pobj;
1557     JSProperty *prop;
1558     if (!obj->lookupProperty(cx, id, &pobj, &prop))
1559         return JS_FALSE;
1560
1561     if (!prop) {
1562         vp->setBoolean(false);
1563         return JS_TRUE;
1564     }
1565
1566     /*
1567      * XXX ECMA spec error compatible: return false unless hasOwnProperty.
1568      * The ECMA spec really should be fixed so propertyIsEnumerable and the
1569      * for..in loop agree on whether prototype properties are enumerable,
1570      * obviously by fixing this method (not by breaking the for..in loop!).
1571      *
1572      * We check here for shared permanent prototype properties, which should
1573      * be treated as if they are local to obj.  They are an implementation
1574      * technique used to satisfy ECMA requirements; users should not be able
1575      * to distinguish a shared permanent proto-property from a local one.
1576      */
1577     bool shared;
1578     uintN attrs;
1579     if (pobj->isNative()) {
1580         Shape *shape = (Shape *) prop;
1581         shared = shape->isSharedPermanent();
1582         attrs = shape->attributes();
1583     } else {
1584         shared = false;
1585         if (!pobj->getAttributes(cx, id, &attrs))
1586             return false;
1587     }
1588     if (pobj != obj && !shared) {
1589         vp->setBoolean(false);
1590         return true;
1591     }
1592     vp->setBoolean((attrs & JSPROP_ENUMERATE) != 0);
1593     return true;
1594 }
1595
1596 #if OLD_GETTER_SETTER_METHODS
1597
1598 const char js_defineGetter_str[] = "__defineGetter__";
1599 const char js_defineSetter_str[] = "__defineSetter__";
1600 const char js_lookupGetter_str[] = "__lookupGetter__";
1601 const char js_lookupSetter_str[] = "__lookupSetter__";
1602
1603 JS_FRIEND_API(JSBool)
1604 js_obj_defineGetter(JSContext *cx, uintN argc, Value *vp)
1605 {
1606     if (!BoxThisForVp(cx, vp))
1607         return false;
1608     JSObject *obj = &vp[1].toObject();
1609
1610     if (argc <= 1 || !js_IsCallable(vp[3])) {
1611         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1612                              JSMSG_BAD_GETTER_OR_SETTER,
1613                              js_getter_str);
1614         return JS_FALSE;
1615     }
1616     PropertyOp getter = CastAsPropertyOp(&vp[3].toObject());
1617
1618     jsid id;
1619     if (!ValueToId(cx, vp[2], &id))
1620         return JS_FALSE;
1621     if (!CheckRedeclaration(cx, obj, id, JSPROP_GETTER))
1622         return JS_FALSE;
1623     /*
1624      * Getters and setters are just like watchpoints from an access
1625      * control point of view.
1626      */
1627     Value junk;
1628     uintN attrs;
1629     if (!CheckAccess(cx, obj, id, JSACC_WATCH, &junk, &attrs))
1630         return JS_FALSE;
1631     vp->setUndefined();
1632     return obj->defineProperty(cx, id, UndefinedValue(), getter, StrictPropertyStub,
1633                                JSPROP_ENUMERATE | JSPROP_GETTER | JSPROP_SHARED);
1634 }
1635
1636 JS_FRIEND_API(JSBool)
1637 js_obj_defineSetter(JSContext *cx, uintN argc, Value *vp)
1638 {
1639     if (!BoxThisForVp(cx, vp))
1640         return false;
1641     JSObject *obj = &vp[1].toObject();
1642
1643     if (argc <= 1 || !js_IsCallable(vp[3])) {
1644         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1645                              JSMSG_BAD_GETTER_OR_SETTER,
1646                              js_setter_str);
1647         return JS_FALSE;
1648     }
1649     StrictPropertyOp setter = CastAsStrictPropertyOp(&vp[3].toObject());
1650
1651     jsid id;
1652     if (!ValueToId(cx, vp[2], &id))
1653         return JS_FALSE;
1654     if (!CheckRedeclaration(cx, obj, id, JSPROP_SETTER))
1655         return JS_FALSE;
1656     /*
1657      * Getters and setters are just like watchpoints from an access
1658      * control point of view.
1659      */
1660     Value junk;
1661     uintN attrs;
1662     if (!CheckAccess(cx, obj, id, JSACC_WATCH, &junk, &attrs))
1663         return JS_FALSE;
1664     vp->setUndefined();
1665     return obj->defineProperty(cx, id, UndefinedValue(), PropertyStub, setter,
1666                                JSPROP_ENUMERATE | JSPROP_SETTER | JSPROP_SHARED);
1667 }
1668
1669 static JSBool
1670 obj_lookupGetter(JSContext *cx, uintN argc, Value *vp)
1671 {
1672     jsid id;
1673     if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
1674         return JS_FALSE;
1675     JSObject *obj = ToObject(cx, &vp[1]);
1676     if (!obj)
1677         return JS_FALSE;
1678     JSObject *pobj;
1679     JSProperty *prop;
1680     if (!obj->lookupProperty(cx, id, &pobj, &prop))
1681         return JS_FALSE;
1682     vp->setUndefined();
1683     if (prop) {
1684         if (pobj->isNative()) {
1685             Shape *shape = (Shape *) prop;
1686             if (shape->hasGetterValue())
1687                 *vp = shape->getterValue();
1688         }
1689     }
1690     return JS_TRUE;
1691 }
1692
1693 static JSBool
1694 obj_lookupSetter(JSContext *cx, uintN argc, Value *vp)
1695 {
1696     jsid id;
1697     if (!ValueToId(cx, argc != 0 ? vp[2] : UndefinedValue(), &id))
1698         return JS_FALSE;
1699     JSObject *obj = ToObject(cx, &vp[1]);
1700     if (!obj)
1701         return JS_FALSE;
1702     JSObject *pobj;
1703     JSProperty *prop;
1704     if (!obj->lookupProperty(cx, id, &pobj, &prop))
1705         return JS_FALSE;
1706     vp->setUndefined();
1707     if (prop) {
1708         if (pobj->isNative()) {
1709             Shape *shape = (Shape *) prop;
1710             if (shape->hasSetterValue())
1711                 *vp = shape->setterValue();
1712         }
1713     }
1714     return JS_TRUE;
1715 }
1716 #endif /* OLD_GETTER_SETTER_METHODS */
1717
1718 JSBool
1719 obj_getPrototypeOf(JSContext *cx, uintN argc, Value *vp)
1720 {
1721     if (argc == 0) {
1722         js_ReportMissingArg(cx, *vp, 0);
1723         return JS_FALSE;
1724     }
1725
1726     if (vp[2].isPrimitive()) {
1727         char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, vp[2], NULL);
1728         if (!bytes)
1729             return JS_FALSE;
1730         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
1731                              JSMSG_UNEXPECTED_TYPE, bytes, "not an object");
1732         JS_free(cx, bytes);
1733         return JS_FALSE;
1734     }
1735
1736     JSObject *obj = &vp[2].toObject();
1737     uintN attrs;
1738     return CheckAccess(cx, obj, ATOM_TO_JSID(cx->runtime->atomState.protoAtom),
1739                        JSACC_PROTO, vp, &attrs);
1740 }
1741
1742 extern JSBool
1743 js_NewPropertyDescriptorObject(JSContext *cx, jsid id, uintN attrs,
1744                                const Value &getter, const Value &setter,
1745                                const Value &value, Value *vp)
1746 {
1747     /* We have our own property, so start creating the descriptor. */
1748     JSObject *desc = NewBuiltinClassInstance(cx, &js_ObjectClass);
1749     if (!desc)
1750         return false;
1751     vp->setObject(*desc);    /* Root and return. */
1752
1753     const JSAtomState &atomState = cx->runtime->atomState;
1754     if (attrs & (JSPROP_GETTER | JSPROP_SETTER)) {
1755         if (!desc->defineProperty(cx, ATOM_TO_JSID(atomState.getAtom), getter,
1756                                   PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE) ||
1757             !desc->defineProperty(cx, ATOM_TO_JSID(atomState.setAtom), setter,
1758                                   PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE)) {
1759             return false;
1760         }
1761     } else {
1762         if (!desc->defineProperty(cx, ATOM_TO_JSID(atomState.valueAtom), value,
1763                                   PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE) ||
1764             !desc->defineProperty(cx, ATOM_TO_JSID(atomState.writableAtom),
1765                                   BooleanValue((attrs & JSPROP_READONLY) == 0),
1766                                   PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE)) {
1767             return false;
1768         }
1769     }
1770
1771     return desc->defineProperty(cx, ATOM_TO_JSID(atomState.enumerableAtom),
1772                                 BooleanValue((attrs & JSPROP_ENUMERATE) != 0),
1773                                 PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE) &&
1774            desc->defineProperty(cx, ATOM_TO_JSID(atomState.configurableAtom),
1775                                 BooleanValue((attrs & JSPROP_PERMANENT) == 0),
1776                                 PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE);
1777 }
1778
1779 JSBool
1780 js_GetOwnPropertyDescriptor(JSContext *cx, JSObject *obj, jsid id, Value *vp)
1781 {
1782     if (obj->isProxy())
1783         return JSProxy::getOwnPropertyDescriptor(cx, obj, id, false, vp);
1784
1785     JSObject *pobj;
1786     JSProperty *prop;
1787     if (!js_HasOwnProperty(cx, obj->getOps()->lookupProperty, obj, id, &pobj, &prop))
1788         return false;
1789     if (!prop) {
1790         vp->setUndefined();
1791         return true;
1792     }
1793
1794     Value roots[] = { UndefinedValue(), UndefinedValue(), UndefinedValue() };
1795     AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(roots), roots);
1796     unsigned attrs;
1797     bool doGet = true;
1798     if (pobj->isNative()) {
1799         Shape *shape = (Shape *) prop;
1800         attrs = shape->attributes();
1801         if (attrs & (JSPROP_GETTER | JSPROP_SETTER)) {
1802             doGet = false;
1803             if (attrs & JSPROP_GETTER)
1804                 roots[0] = shape->getterValue();
1805             if (attrs & JSPROP_SETTER)
1806                 roots[1] = shape->setterValue();
1807         }
1808     } else {
1809         if (!pobj->getAttributes(cx, id, &attrs))
1810             return false;
1811     }
1812
1813     if (doGet && !obj->getProperty(cx, id, &roots[2]))
1814         return false;
1815
1816     return js_NewPropertyDescriptorObject(cx, id,
1817                                           attrs,
1818                                           roots[0], /* getter */
1819                                           roots[1], /* setter */
1820                                           roots[2], /* value */
1821                                           vp);
1822 }
1823
1824 static bool
1825 GetFirstArgumentAsObject(JSContext *cx, uintN argc, Value *vp, const char *method, JSObject **objp)
1826 {
1827     if (argc == 0) {
1828         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
1829                              method, "0", "s");
1830         return false;
1831     }
1832
1833     const Value &v = vp[2];
1834     if (!v.isObject()) {
1835         char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, NULL);
1836         if (!bytes)
1837             return false;
1838         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_UNEXPECTED_TYPE,
1839                              bytes, "not an object");
1840         JS_free(cx, bytes);
1841         return false;
1842     }
1843
1844     *objp = &v.toObject();
1845     return true;
1846 }
1847
1848 static JSBool
1849 obj_getOwnPropertyDescriptor(JSContext *cx, uintN argc, Value *vp)
1850 {
1851     JSObject *obj;
1852     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.getOwnPropertyDescriptor", &obj))
1853         return JS_FALSE;
1854     AutoIdRooter nameidr(cx);
1855     if (!ValueToId(cx, argc >= 2 ? vp[3] : UndefinedValue(), nameidr.addr()))
1856         return JS_FALSE;
1857     return js_GetOwnPropertyDescriptor(cx, obj, nameidr.id(), vp);
1858 }
1859
1860 static JSBool
1861 obj_keys(JSContext *cx, uintN argc, Value *vp)
1862 {
1863     JSObject *obj;
1864     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.keys", &obj))
1865         return false;
1866
1867     AutoIdVector props(cx);
1868     if (!GetPropertyNames(cx, obj, JSITER_OWNONLY, &props))
1869         return false;
1870
1871     AutoValueVector vals(cx);
1872     if (!vals.reserve(props.length()))
1873         return false;
1874     for (size_t i = 0, len = props.length(); i < len; i++) {
1875         jsid id = props[i];
1876         if (JSID_IS_STRING(id)) {
1877             JS_ALWAYS_TRUE(vals.append(StringValue(JSID_TO_STRING(id))));
1878         } else if (JSID_IS_INT(id)) {
1879             JSString *str = js_IntToString(cx, JSID_TO_INT(id));
1880             if (!str)
1881                 return false;
1882             JS_ALWAYS_TRUE(vals.append(StringValue(str)));
1883         } else {
1884             JS_ASSERT(JSID_IS_OBJECT(id));
1885         }
1886     }
1887
1888     JS_ASSERT(props.length() <= UINT32_MAX);
1889     JSObject *aobj = NewDenseCopiedArray(cx, jsuint(vals.length()), vals.begin());
1890     if (!aobj)
1891         return false;
1892     vp->setObject(*aobj);
1893
1894     return true;
1895 }
1896
1897 static bool
1898 HasProperty(JSContext* cx, JSObject* obj, jsid id, Value* vp, bool *foundp)
1899 {
1900     if (!obj->hasProperty(cx, id, foundp, JSRESOLVE_QUALIFIED | JSRESOLVE_DETECTING))
1901         return false;
1902     if (!*foundp) {
1903         vp->setUndefined();
1904         return true;
1905     }
1906
1907     /*
1908      * We must go through the method read barrier in case id is 'get' or 'set'.
1909      * There is no obvious way to defer cloning a joined function object whose
1910      * identity will be used by DefinePropertyOnObject, e.g., or reflected via
1911      * js_GetOwnPropertyDescriptor, as the getter or setter callable object.
1912      */
1913     return !!obj->getProperty(cx, id, vp);
1914 }
1915
1916 PropDesc::PropDesc()
1917   : pd(UndefinedValue()),
1918     id(INT_TO_JSID(0)),
1919     value(UndefinedValue()),
1920     get(UndefinedValue()),
1921     set(UndefinedValue()),
1922     attrs(0),
1923     hasGet(false),
1924     hasSet(false),
1925     hasValue(false),
1926     hasWritable(false),
1927     hasEnumerable(false),
1928     hasConfigurable(false)
1929 {
1930 }
1931
1932 bool
1933 PropDesc::initialize(JSContext* cx, jsid id, const Value &origval)
1934 {
1935     Value v = origval;
1936     this->id = id;
1937
1938     /* 8.10.5 step 1 */
1939     if (v.isPrimitive()) {
1940         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NOT_NONNULL_OBJECT);
1941         return false;
1942     }
1943     JSObject* desc = &v.toObject();
1944
1945     /* Make a copy of the descriptor. We might need it later. */
1946     pd = v;
1947
1948     /* Start with the proper defaults. */
1949     attrs = JSPROP_PERMANENT | JSPROP_READONLY;
1950
1951     bool found;
1952
1953     /* 8.10.5 step 3 */
1954 #ifdef __GNUC__ /* quell GCC overwarning */
1955     found = false;
1956 #endif
1957     if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.enumerableAtom), &v, &found))
1958         return false;
1959     if (found) {
1960         hasEnumerable = JS_TRUE;
1961         if (js_ValueToBoolean(v))
1962             attrs |= JSPROP_ENUMERATE;
1963     }
1964
1965     /* 8.10.5 step 4 */
1966     if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.configurableAtom), &v, &found))
1967         return false;
1968     if (found) {
1969         hasConfigurable = JS_TRUE;
1970         if (js_ValueToBoolean(v))
1971             attrs &= ~JSPROP_PERMANENT;
1972     }
1973
1974     /* 8.10.5 step 5 */
1975     if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.valueAtom), &v, &found))
1976         return false;
1977     if (found) {
1978         hasValue = true;
1979         value = v;
1980     }
1981
1982     /* 8.10.6 step 6 */
1983     if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.writableAtom), &v, &found))
1984         return false;
1985     if (found) {
1986         hasWritable = JS_TRUE;
1987         if (js_ValueToBoolean(v))
1988             attrs &= ~JSPROP_READONLY;
1989     }
1990
1991     /* 8.10.7 step 7 */
1992     if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.getAtom), &v, &found))
1993         return false;
1994     if (found) {
1995         if ((v.isPrimitive() || !js_IsCallable(v)) && !v.isUndefined()) {
1996             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_GET_SET_FIELD,
1997                                  js_getter_str);
1998             return false;
1999         }
2000         hasGet = true;
2001         get = v;
2002         attrs |= JSPROP_GETTER | JSPROP_SHARED;
2003     }
2004
2005     /* 8.10.7 step 8 */
2006     if (!HasProperty(cx, desc, ATOM_TO_JSID(cx->runtime->atomState.setAtom), &v, &found))
2007         return false;
2008     if (found) {
2009         if ((v.isPrimitive() || !js_IsCallable(v)) && !v.isUndefined()) {
2010             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_BAD_GET_SET_FIELD,
2011                                  js_setter_str);
2012             return false;
2013         }
2014         hasSet = true;
2015         set = v;
2016         attrs |= JSPROP_SETTER | JSPROP_SHARED;
2017     }
2018
2019     /* 8.10.7 step 9 */
2020     if ((hasGet || hasSet) && (hasValue || hasWritable)) {
2021         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_INVALID_DESCRIPTOR);
2022         return false;
2023     }
2024
2025     return true;
2026 }
2027
2028 static JSBool
2029 Reject(JSContext *cx, uintN errorNumber, bool throwError, jsid id, bool *rval)
2030 {
2031     if (throwError) {
2032         jsid idstr;
2033         if (!js_ValueToStringId(cx, IdToValue(id), &idstr))
2034            return JS_FALSE;
2035         JSAutoByteString bytes(cx, JSID_TO_STRING(idstr));
2036         if (!bytes)
2037             return JS_FALSE;
2038         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, errorNumber, bytes.ptr());
2039         return JS_FALSE;
2040     }
2041
2042     *rval = false;
2043     return JS_TRUE;
2044 }
2045
2046 static JSBool
2047 Reject(JSContext *cx, JSObject *obj, uintN errorNumber, bool throwError, bool *rval)
2048 {
2049     if (throwError) {
2050         if (js_ErrorFormatString[errorNumber].argCount == 1) {
2051             js_ReportValueErrorFlags(cx, JSREPORT_ERROR, errorNumber,
2052                                      JSDVG_IGNORE_STACK, ObjectValue(*obj),
2053                                      NULL, NULL, NULL);
2054         } else {
2055             JS_ASSERT(js_ErrorFormatString[errorNumber].argCount == 0);
2056             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, errorNumber);
2057         }
2058         return JS_FALSE;
2059     }
2060
2061     *rval = false;
2062     return JS_TRUE;
2063 }
2064
2065 static JSBool
2066 DefinePropertyOnObject(JSContext *cx, JSObject *obj, const PropDesc &desc,
2067                        bool throwError, bool *rval)
2068 {
2069     /* 8.12.9 step 1. */
2070     JSProperty *current;
2071     JSObject *obj2;
2072     JS_ASSERT(!obj->getOps()->lookupProperty);
2073     if (!js_HasOwnProperty(cx, NULL, obj, desc.id, &obj2, &current))
2074         return JS_FALSE;
2075
2076     JS_ASSERT(!obj->getOps()->defineProperty);
2077
2078     /*
2079      * If we find a shared permanent property in a different object obj2 from
2080      * obj, then if the property is shared permanent (an old hack to optimize
2081      * per-object properties into one prototype property), ignore that lookup
2082      * result (null current).
2083      *
2084      * FIXME: bug 575997 (see also bug 607863).
2085      */
2086     if (current && obj2 != obj && obj2->isNative()) {
2087         /* See same assertion with comment further below. */
2088         JS_ASSERT(obj2->getClass() == obj->getClass());
2089
2090         Shape *shape = (Shape *) current;
2091         if (shape->isSharedPermanent())
2092             current = NULL;
2093     }
2094
2095     /* 8.12.9 steps 2-4. */
2096     if (!current) {
2097         if (!obj->isExtensible())
2098             return Reject(cx, obj, JSMSG_OBJECT_NOT_EXTENSIBLE, throwError, rval);
2099
2100         *rval = true;
2101
2102         if (desc.isGenericDescriptor() || desc.isDataDescriptor()) {
2103             JS_ASSERT(!obj->getOps()->defineProperty);
2104             return js_DefineProperty(cx, obj, desc.id, &desc.value,
2105                                      PropertyStub, StrictPropertyStub, desc.attrs);
2106         }
2107
2108         JS_ASSERT(desc.isAccessorDescriptor());
2109
2110         /*
2111          * Getters and setters are just like watchpoints from an access
2112          * control point of view.
2113          */
2114         Value dummy;
2115         uintN dummyAttrs;
2116         if (!CheckAccess(cx, obj, desc.id, JSACC_WATCH, &dummy, &dummyAttrs))
2117             return JS_FALSE;
2118
2119         Value tmp = UndefinedValue();
2120         return js_DefineProperty(cx, obj, desc.id, &tmp,
2121                                  desc.getter(), desc.setter(), desc.attrs);
2122     }
2123
2124     /* 8.12.9 steps 5-6 (note 5 is merely a special case of 6). */
2125     Value v = UndefinedValue();
2126
2127     /*
2128      * In the special case of shared permanent properties, the "own" property
2129      * can be found on a different object.  In that case the returned property
2130      * might not be native, except: the shared permanent property optimization
2131      * is not applied if the objects have different classes (bug 320854), as
2132      * must be enforced by js_HasOwnProperty for the Shape cast below to be
2133      * safe.
2134      */
2135     JS_ASSERT(obj->getClass() == obj2->getClass());
2136
2137     const Shape *shape = reinterpret_cast<Shape *>(current);
2138     do {
2139         if (desc.isAccessorDescriptor()) {
2140             if (!shape->isAccessorDescriptor())
2141                 break;
2142
2143             if (desc.hasGet) {
2144                 JSBool same;
2145                 if (!SameValue(cx, desc.getterValue(), shape->getterOrUndefined(), &same))
2146                     return JS_FALSE;
2147                 if (!same)
2148                     break;
2149             }
2150
2151             if (desc.hasSet) {
2152                 JSBool same;
2153                 if (!SameValue(cx, desc.setterValue(), shape->setterOrUndefined(), &same))
2154                     return JS_FALSE;
2155                 if (!same)
2156                     break;
2157             }
2158         } else {
2159             /*
2160              * Determine the current value of the property once, if the current
2161              * value might actually need to be used or preserved later.  NB: we
2162              * guard on whether the current property is a data descriptor to
2163              * avoid calling a getter; we won't need the value if it's not a
2164              * data descriptor.
2165              */
2166             if (shape->isDataDescriptor()) {
2167                 /*
2168                  * We must rule out a non-configurable js::PropertyOp-guarded
2169                  * property becoming a writable unguarded data property, since
2170                  * such a property can have its value changed to one the getter
2171                  * and setter preclude.
2172                  *
2173                  * A desc lacking writable but with value is a data descriptor
2174                  * and we must reject it as if it had writable: true if current
2175                  * is writable.
2176                  */
2177                 if (!shape->configurable() &&
2178                     (!shape->hasDefaultGetter() || !shape->hasDefaultSetter()) &&
2179                     desc.isDataDescriptor() &&
2180                     (desc.hasWritable ? desc.writable() : shape->writable()))
2181                 {
2182                     return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2183                 }
2184
2185                 if (!js_NativeGet(cx, obj, obj2, shape, JSGET_NO_METHOD_BARRIER, &v))
2186                     return JS_FALSE;
2187             }
2188
2189             if (desc.isDataDescriptor()) {
2190                 if (!shape->isDataDescriptor())
2191                     break;
2192
2193                 JSBool same;
2194                 if (desc.hasValue) {
2195                     if (!SameValue(cx, desc.value, v, &same))
2196                         return JS_FALSE;
2197                     if (!same) {
2198                         /*
2199                          * Insist that a non-configurable js::PropertyOp data
2200                          * property is frozen at exactly the last-got value.
2201                          *
2202                          * Duplicate the first part of the big conjunction that
2203                          * we tested above, rather than add a local bool flag.
2204                          * Likewise, don't try to keep shape->writable() in a
2205                          * flag we veto from true to false for non-configurable
2206                          * PropertyOp-based data properties and test before the
2207                          * SameValue check later on in order to re-use that "if
2208                          * (!SameValue) Reject" logic.
2209                          *
2210                          * This function is large and complex enough that it
2211                          * seems best to repeat a small bit of code and return
2212                          * Reject(...) ASAP, instead of being clever.
2213                          */
2214                         if (!shape->configurable() &&
2215                             (!shape->hasDefaultGetter() || !shape->hasDefaultSetter()))
2216                         {
2217                             return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2218                         }
2219                         break;
2220                     }
2221                 }
2222                 if (desc.hasWritable && desc.writable() != shape->writable())
2223                     break;
2224             } else {
2225                 /* The only fields in desc will be handled below. */
2226                 JS_ASSERT(desc.isGenericDescriptor());
2227             }
2228         }
2229
2230         if (desc.hasConfigurable && desc.configurable() != shape->configurable())
2231             break;
2232         if (desc.hasEnumerable && desc.enumerable() != shape->enumerable())
2233             break;
2234
2235         /* The conditions imposed by step 5 or step 6 apply. */
2236         *rval = true;
2237         return JS_TRUE;
2238     } while (0);
2239
2240     /* 8.12.9 step 7. */
2241     if (!shape->configurable()) {
2242         /*
2243          * Since [[Configurable]] defaults to false, we don't need to check
2244          * whether it was specified.  We can't do likewise for [[Enumerable]]
2245          * because its putative value is used in a comparison -- a comparison
2246          * whose result must always be false per spec if the [[Enumerable]]
2247          * field is not present.  Perfectly pellucid logic, eh?
2248          */
2249         JS_ASSERT_IF(!desc.hasConfigurable, !desc.configurable());
2250         if (desc.configurable() ||
2251             (desc.hasEnumerable && desc.enumerable() != shape->enumerable())) {
2252             return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2253         }
2254     }
2255
2256     bool callDelProperty = false;
2257
2258     if (desc.isGenericDescriptor()) {
2259         /* 8.12.9 step 8, no validation required */
2260     } else if (desc.isDataDescriptor() != shape->isDataDescriptor()) {
2261         /* 8.12.9 step 9. */
2262         if (!shape->configurable())
2263             return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2264     } else if (desc.isDataDescriptor()) {
2265         /* 8.12.9 step 10. */
2266         JS_ASSERT(shape->isDataDescriptor());
2267         if (!shape->configurable() && !shape->writable()) {
2268             if (desc.hasWritable && desc.writable())
2269                 return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2270             if (desc.hasValue) {
2271                 JSBool same;
2272                 if (!SameValue(cx, desc.value, v, &same))
2273                     return JS_FALSE;
2274                 if (!same)
2275                     return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2276             }
2277         }
2278
2279         callDelProperty = !shape->hasDefaultGetter() || !shape->hasDefaultSetter();
2280     } else {
2281         /* 8.12.9 step 11. */
2282         JS_ASSERT(desc.isAccessorDescriptor() && shape->isAccessorDescriptor());
2283         if (!shape->configurable()) {
2284             if (desc.hasSet) {
2285                 JSBool same;
2286                 if (!SameValue(cx, desc.setterValue(), shape->setterOrUndefined(), &same))
2287                     return JS_FALSE;
2288                 if (!same)
2289                     return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2290             }
2291
2292             if (desc.hasGet) {
2293                 JSBool same;
2294                 if (!SameValue(cx, desc.getterValue(), shape->getterOrUndefined(), &same))
2295                     return JS_FALSE;
2296                 if (!same)
2297                     return Reject(cx, JSMSG_CANT_REDEFINE_PROP, throwError, desc.id, rval);
2298             }
2299         }
2300     }
2301
2302     /* 8.12.9 step 12. */
2303     uintN attrs;
2304     PropertyOp getter;
2305     StrictPropertyOp setter;
2306     if (desc.isGenericDescriptor()) {
2307         uintN changed = 0;
2308         if (desc.hasConfigurable)
2309             changed |= JSPROP_PERMANENT;
2310         if (desc.hasEnumerable)
2311             changed |= JSPROP_ENUMERATE;
2312
2313         attrs = (shape->attributes() & ~changed) | (desc.attrs & changed);
2314         if (shape->isMethod()) {
2315             JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
2316             getter = PropertyStub;
2317             setter = StrictPropertyStub;
2318         } else {
2319             getter = shape->getter();
2320             setter = shape->setter();
2321         }
2322     } else if (desc.isDataDescriptor()) {
2323         uintN unchanged = 0;
2324         if (!desc.hasConfigurable)
2325             unchanged |= JSPROP_PERMANENT;
2326         if (!desc.hasEnumerable)
2327             unchanged |= JSPROP_ENUMERATE;
2328         if (!desc.hasWritable)
2329             unchanged |= JSPROP_READONLY;
2330
2331         if (desc.hasValue)
2332             v = desc.value;
2333         attrs = (desc.attrs & ~unchanged) | (shape->attributes() & unchanged);
2334         getter = PropertyStub;
2335         setter = StrictPropertyStub;
2336     } else {
2337         JS_ASSERT(desc.isAccessorDescriptor());
2338
2339         /*
2340          * Getters and setters are just like watchpoints from an access
2341          * control point of view.
2342          */
2343         Value dummy;
2344         if (!CheckAccess(cx, obj2, desc.id, JSACC_WATCH, &dummy, &attrs))
2345              return JS_FALSE;
2346
2347         JS_ASSERT_IF(shape->isMethod(), !(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
2348
2349         /* 8.12.9 step 12. */
2350         uintN changed = 0;
2351         if (desc.hasConfigurable)
2352             changed |= JSPROP_PERMANENT;
2353         if (desc.hasEnumerable)
2354             changed |= JSPROP_ENUMERATE;
2355         if (desc.hasGet)
2356             changed |= JSPROP_GETTER | JSPROP_SHARED;
2357         if (desc.hasSet)
2358             changed |= JSPROP_SETTER | JSPROP_SHARED;
2359
2360         attrs = (desc.attrs & changed) | (shape->attributes() & ~changed);
2361         if (desc.hasGet) {
2362             getter = desc.getter();
2363         } else {
2364             getter = (shape->isMethod() || (shape->hasDefaultGetter() && !shape->hasGetterValue()))
2365                      ? PropertyStub
2366                      : shape->getter();
2367         }
2368         if (desc.hasSet) {
2369             setter = desc.setter();
2370         } else {
2371             setter = (shape->hasDefaultSetter() && !shape->hasSetterValue())
2372                      ? StrictPropertyStub
2373                      : shape->setter();
2374         }
2375     }
2376
2377     *rval = true;
2378
2379     /*
2380      * Since "data" properties implemented using native C functions may rely on
2381      * side effects during setting, we must make them aware that they have been
2382      * "assigned"; deleting the property before redefining it does the trick.
2383      * See bug 539766, where we ran into problems when we redefined
2384      * arguments.length without making the property aware that its value had
2385      * been changed (which would have happened if we had deleted it before
2386      * redefining it or we had invoked its setter to change its value).
2387      */
2388     if (callDelProperty) {
2389         Value dummy = UndefinedValue();
2390         if (!CallJSPropertyOp(cx, obj2->getClass()->delProperty, obj2, desc.id, &dummy))
2391             return false;
2392     }
2393
2394     return js_DefineProperty(cx, obj, desc.id, &v, getter, setter, attrs);
2395 }
2396
2397 static JSBool
2398 DefinePropertyOnArray(JSContext *cx, JSObject *obj, const PropDesc &desc,
2399                       bool throwError, bool *rval)
2400 {
2401     /*
2402      * We probably should optimize dense array property definitions where
2403      * the descriptor describes a traditional array property (enumerable,
2404      * configurable, writable, numeric index or length without altering its
2405      * attributes).  Such definitions are probably unlikely, so we don't bother
2406      * for now.
2407      */
2408     if (obj->isDenseArray() && !obj->makeDenseArraySlow(cx))
2409         return JS_FALSE;
2410
2411     jsuint oldLen = obj->getArrayLength();
2412
2413     if (JSID_IS_ATOM(desc.id, cx->runtime->atomState.lengthAtom)) {
2414         /*
2415          * Our optimization of storage of the length property of arrays makes
2416          * it very difficult to properly implement defining the property.  For
2417          * now simply throw an exception (NB: not merely Reject) on any attempt
2418          * to define the "length" property, rather than attempting to implement
2419          * some difficult-for-authors-to-grasp subset of that functionality.
2420          */
2421         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_DEFINE_ARRAY_LENGTH);
2422         return JS_FALSE;
2423     }
2424
2425     uint32 index;
2426     if (js_IdIsIndex(desc.id, &index)) {
2427         /*
2428         // Disabled until we support defining "length":
2429         if (index >= oldLen && lengthPropertyNotWritable())
2430             return ThrowTypeError(cx, JSMSG_CANT_APPEND_TO_ARRAY);
2431          */
2432         if (!DefinePropertyOnObject(cx, obj, desc, false, rval))
2433             return JS_FALSE;
2434         if (!*rval)
2435             return Reject(cx, obj, JSMSG_CANT_DEFINE_ARRAY_INDEX, throwError, rval);
2436
2437         if (index >= oldLen) {
2438             JS_ASSERT(index != UINT32_MAX);
2439             obj->setArrayLength(index + 1);
2440         }
2441
2442         *rval = true;
2443         return JS_TRUE;
2444     }
2445
2446     return DefinePropertyOnObject(cx, obj, desc, throwError, rval);
2447 }
2448
2449 static JSBool
2450 DefineProperty(JSContext *cx, JSObject *obj, const PropDesc &desc, bool throwError,
2451                bool *rval)
2452 {
2453     if (obj->isArray())
2454         return DefinePropertyOnArray(cx, obj, desc, throwError, rval);
2455
2456     if (obj->getOps()->lookupProperty) {
2457         if (obj->isProxy())
2458             return JSProxy::defineProperty(cx, obj, desc.id, desc.pd);
2459         return Reject(cx, obj, JSMSG_OBJECT_NOT_EXTENSIBLE, throwError, rval);
2460     }
2461
2462     return DefinePropertyOnObject(cx, obj, desc, throwError, rval);
2463 }
2464
2465 JSBool
2466 js_DefineOwnProperty(JSContext *cx, JSObject *obj, jsid id,
2467                      const Value &descriptor, JSBool *bp)
2468 {
2469     AutoPropDescArrayRooter descs(cx);
2470     PropDesc *desc = descs.append();
2471     if (!desc || !desc->initialize(cx, id, descriptor))
2472         return false;
2473
2474     bool rval;
2475     if (!DefineProperty(cx, obj, *desc, true, &rval))
2476         return false;
2477     *bp = !!rval;
2478     return true;
2479 }
2480
2481 /* ES5 15.2.3.6: Object.defineProperty(O, P, Attributes) */
2482 static JSBool
2483 obj_defineProperty(JSContext* cx, uintN argc, Value* vp)
2484 {
2485     /* 15.2.3.6 steps 1 and 5. */
2486     JSObject *obj;
2487     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.defineProperty", &obj))
2488         return JS_FALSE;
2489     vp->setObject(*obj);
2490
2491     /* 15.2.3.6 step 2. */
2492     AutoIdRooter nameidr(cx);
2493     if (!ValueToId(cx, argc >= 2 ? vp[3] : UndefinedValue(), nameidr.addr()))
2494         return JS_FALSE;
2495
2496     /* 15.2.3.6 step 3. */
2497     const Value &descval = argc >= 3 ? vp[4] : UndefinedValue();
2498
2499     /* 15.2.3.6 step 4 */
2500     JSBool junk;
2501     return js_DefineOwnProperty(cx, obj, nameidr.id(), descval, &junk);
2502 }
2503
2504 static bool
2505 DefineProperties(JSContext *cx, JSObject *obj, JSObject *props)
2506 {
2507     AutoIdArray ida(cx, JS_Enumerate(cx, props));
2508     if (!ida)
2509         return false;
2510
2511      AutoPropDescArrayRooter descs(cx);
2512      size_t len = ida.length();
2513      for (size_t i = 0; i < len; i++) {
2514          jsid id = ida[i];
2515          PropDesc* desc = descs.append();
2516          AutoValueRooter tvr(cx);
2517          if (!desc ||
2518              !JS_GetPropertyById(cx, props, id, tvr.jsval_addr()) ||
2519              !desc->initialize(cx, id, tvr.value())) {
2520              return false;
2521          }
2522      }
2523
2524      bool dummy;
2525      for (size_t i = 0; i < len; i++) {
2526          if (!DefineProperty(cx, obj, descs[i], true, &dummy))
2527              return false;
2528      }
2529
2530      return true;
2531 }
2532
2533 extern JSBool
2534 js_PopulateObject(JSContext *cx, JSObject *newborn, JSObject *props)
2535 {
2536     return DefineProperties(cx, newborn, props);
2537 }
2538
2539 /* ES5 15.2.3.7: Object.defineProperties(O, Properties) */
2540 static JSBool
2541 obj_defineProperties(JSContext* cx, uintN argc, Value* vp)
2542 {
2543     /* 15.2.3.6 steps 1 and 5. */
2544     JSObject *obj;
2545     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.defineProperties", &obj))
2546         return false;
2547     vp->setObject(*obj);
2548
2549     if (argc < 2) {
2550         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
2551                              "Object.defineProperties", "0", "s");
2552         return false;
2553     }
2554
2555     JSObject* props = js_ValueToNonNullObject(cx, vp[3]);
2556     if (!props)
2557         return false;
2558     vp[3].setObject(*props);
2559
2560     return DefineProperties(cx, obj, props);
2561 }
2562
2563 /* ES5 15.2.3.5: Object.create(O [, Properties]) */
2564 static JSBool
2565 obj_create(JSContext *cx, uintN argc, Value *vp)
2566 {
2567     if (argc == 0) {
2568         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_MORE_ARGS_NEEDED,
2569                              "Object.create", "0", "s");
2570         return JS_FALSE;
2571     }
2572
2573     const Value &v = vp[2];
2574     if (!v.isObjectOrNull()) {
2575         char *bytes = DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, v, NULL);
2576         if (!bytes)
2577             return JS_FALSE;
2578         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_UNEXPECTED_TYPE,
2579                              bytes, "not an object or null");
2580         JS_free(cx, bytes);
2581         return JS_FALSE;
2582     }
2583
2584     /*
2585      * Use the callee's global as the parent of the new object to avoid dynamic
2586      * scoping (i.e., using the caller's global).
2587      */
2588     JSObject *obj = NewNonFunction<WithProto::Given>(cx, &js_ObjectClass, v.toObjectOrNull(),
2589                                                         vp->toObject().getGlobal());
2590     if (!obj)
2591         return JS_FALSE;
2592     vp->setObject(*obj); /* Root and prepare for eventual return. */
2593
2594     /* 15.2.3.5 step 4. */
2595     if (argc > 1 && !vp[3].isUndefined()) {
2596         if (vp[3].isPrimitive()) {
2597             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_NOT_NONNULL_OBJECT);
2598             return JS_FALSE;
2599         }
2600
2601         JSObject *props = &vp[3].toObject();
2602         AutoIdArray ida(cx, JS_Enumerate(cx, props));
2603         if (!ida)
2604             return JS_FALSE;
2605
2606         AutoPropDescArrayRooter descs(cx);
2607         size_t len = ida.length();
2608         for (size_t i = 0; i < len; i++) {
2609             jsid id = ida[i];
2610             PropDesc *desc = descs.append();
2611             if (!desc || !JS_GetPropertyById(cx, props, id, Jsvalify(&vp[1])) ||
2612                 !desc->initialize(cx, id, vp[1])) {
2613                 return JS_FALSE;
2614             }
2615         }
2616
2617         bool dummy;
2618         for (size_t i = 0; i < len; i++) {
2619             if (!DefineProperty(cx, obj, descs[i], true, &dummy))
2620                 return JS_FALSE;
2621         }
2622     }
2623
2624     /* 5. Return obj. */
2625     return JS_TRUE;
2626 }
2627
2628 static JSBool
2629 obj_getOwnPropertyNames(JSContext *cx, uintN argc, Value *vp)
2630 {
2631     JSObject *obj;
2632     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.getOwnPropertyNames", &obj))
2633         return false;
2634
2635     AutoIdVector keys(cx);
2636     if (!GetPropertyNames(cx, obj, JSITER_OWNONLY | JSITER_HIDDEN, &keys))
2637         return false;
2638
2639     AutoValueVector vals(cx);
2640     if (!vals.resize(keys.length()))
2641         return false;
2642
2643     for (size_t i = 0, len = keys.length(); i < len; i++) {
2644          jsid id = keys[i];
2645          if (JSID_IS_INT(id)) {
2646              JSString *str = js_ValueToString(cx, Int32Value(JSID_TO_INT(id)));
2647              if (!str)
2648                  return false;
2649              vals[i].setString(str);
2650          } else if (JSID_IS_ATOM(id)) {
2651              vals[i].setString(JSID_TO_STRING(id));
2652          } else {
2653              vals[i].setObject(*JSID_TO_OBJECT(id));
2654          }
2655     }
2656
2657     JSObject *aobj = NewDenseCopiedArray(cx, vals.length(), vals.begin());
2658     if (!aobj)
2659         return false;
2660
2661     vp->setObject(*aobj);
2662     return true;
2663 }
2664
2665 static JSBool
2666 obj_isExtensible(JSContext *cx, uintN argc, Value *vp)
2667 {
2668     JSObject *obj;
2669     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.isExtensible", &obj))
2670         return false;
2671
2672     vp->setBoolean(obj->isExtensible());
2673     return true;
2674 }
2675
2676 static JSBool
2677 obj_preventExtensions(JSContext *cx, uintN argc, Value *vp)
2678 {
2679     JSObject *obj;
2680     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.preventExtensions", &obj))
2681         return false;
2682
2683     vp->setObject(*obj);
2684     if (!obj->isExtensible())
2685         return true;
2686
2687     AutoIdVector props(cx);
2688     return obj->preventExtensions(cx, &props);
2689 }
2690
2691 bool
2692 JSObject::sealOrFreeze(JSContext *cx, ImmutabilityType it)
2693 {
2694     assertSameCompartment(cx, this);
2695     JS_ASSERT(it == SEAL || it == FREEZE);
2696
2697     AutoIdVector props(cx);
2698     if (isExtensible()) {
2699         if (!preventExtensions(cx, &props))
2700             return false;
2701     } else {
2702         if (!GetPropertyNames(cx, this, JSITER_HIDDEN | JSITER_OWNONLY, &props))
2703             return false;
2704     }
2705
2706     /* preventExtensions must slowify dense arrays, so we can assign to holes without checks. */
2707     JS_ASSERT(!isDenseArray());
2708
2709     for (size_t i = 0, len = props.length(); i < len; i++) {
2710         jsid id = props[i];
2711
2712         uintN attrs;
2713         if (!getAttributes(cx, id, &attrs))
2714             return false;
2715
2716         /* Make all attributes permanent; if freezing, make data attributes read-only. */
2717         uintN new_attrs;
2718         if (it == FREEZE && !(attrs & (JSPROP_GETTER | JSPROP_SETTER)))
2719             new_attrs = JSPROP_PERMANENT | JSPROP_READONLY;
2720         else
2721             new_attrs = JSPROP_PERMANENT;
2722
2723         /* If we already have the attributes we need, skip the setAttributes call. */
2724         if ((attrs | new_attrs) == attrs)
2725             continue;
2726
2727         attrs |= new_attrs;
2728         if (!setAttributes(cx, id, &attrs))
2729             return false;
2730     }
2731
2732     return true;
2733 }
2734
2735 static JSBool
2736 obj_freeze(JSContext *cx, uintN argc, Value *vp)
2737 {
2738     JSObject *obj;
2739     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.freeze", &obj))
2740         return false;
2741
2742     vp->setObject(*obj);
2743
2744     return obj->freeze(cx);
2745 }
2746
2747 static JSBool
2748 obj_isFrozen(JSContext *cx, uintN argc, Value *vp)
2749 {
2750     JSObject *obj;
2751     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.preventExtensions", &obj))
2752         return false;
2753
2754     vp->setBoolean(false);
2755
2756     if (obj->isExtensible())
2757         return true; /* The JavaScript value returned is false. */
2758
2759     AutoIdVector props(cx);
2760     if (!GetPropertyNames(cx, obj, JSITER_HIDDEN | JSITER_OWNONLY, &props))
2761         return false;
2762
2763     for (size_t i = 0, len = props.length(); i < len; i++) {
2764         jsid id = props[i];
2765
2766         uintN attrs = 0;
2767         if (!obj->getAttributes(cx, id, &attrs))
2768             return false;
2769
2770         /* The property must be non-configurable and either read-only or an accessor. */
2771         if (!(attrs & JSPROP_PERMANENT) ||
2772             !(attrs & (JSPROP_READONLY | JSPROP_GETTER | JSPROP_SETTER)))
2773             return true; /* The JavaScript value returned is false. */
2774     }
2775
2776     /* It really was sealed, so return true. */
2777     vp->setBoolean(true);
2778     return true;
2779 }
2780
2781 static JSBool
2782 obj_seal(JSContext *cx, uintN argc, Value *vp)
2783 {
2784     JSObject *obj;
2785     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.seal", &obj))
2786         return false;
2787
2788     vp->setObject(*obj);
2789
2790     return obj->seal(cx);
2791 }
2792
2793 static JSBool
2794 obj_isSealed(JSContext *cx, uintN argc, Value *vp)
2795 {
2796     JSObject *obj;
2797     if (!GetFirstArgumentAsObject(cx, argc, vp, "Object.isSealed", &obj))
2798         return false;
2799
2800     /* Assume not sealed until proven otherwise. */
2801     vp->setBoolean(false);
2802
2803     if (obj->isExtensible())
2804         return true; /* The JavaScript value returned is false. */
2805
2806     AutoIdVector props(cx);
2807     if (!GetPropertyNames(cx, obj, JSITER_HIDDEN | JSITER_OWNONLY, &props))
2808         return false;
2809
2810     for (size_t i = 0, len = props.length(); i < len; i++) {
2811         jsid id = props[i];
2812
2813         uintN attrs;
2814         if (!obj->getAttributes(cx, id, &attrs))
2815             return false;
2816
2817         if (!(attrs & JSPROP_PERMANENT))
2818             return true; /* The JavaScript value returned is false. */
2819     }
2820
2821     /* It really was sealed, so return true. */
2822     vp->setBoolean(true);
2823     return true;
2824 }
2825
2826 #if JS_HAS_OBJ_WATCHPOINT
2827 const char js_watch_str[] = "watch";
2828 const char js_unwatch_str[] = "unwatch";
2829 #endif
2830 const char js_hasOwnProperty_str[] = "hasOwnProperty";
2831 const char js_isPrototypeOf_str[] = "isPrototypeOf";
2832 const char js_propertyIsEnumerable_str[] = "propertyIsEnumerable";
2833
2834 static JSFunctionSpec object_methods[] = {
2835 #if JS_HAS_TOSOURCE
2836     JS_FN(js_toSource_str,             obj_toSource,                0,0),
2837 #endif
2838     JS_FN(js_toString_str,             obj_toString,                0,0),
2839     JS_FN(js_toLocaleString_str,       obj_toLocaleString,          0,0),
2840     JS_FN(js_valueOf_str,              obj_valueOf,                 0,0),
2841 #if JS_HAS_OBJ_WATCHPOINT
2842     JS_FN(js_watch_str,                obj_watch,                   2,0),
2843     JS_FN(js_unwatch_str,              obj_unwatch,                 1,0),
2844 #endif
2845     JS_FN(js_hasOwnProperty_str,       obj_hasOwnProperty,          1,0),
2846     JS_FN(js_isPrototypeOf_str,        obj_isPrototypeOf,           1,0),
2847     JS_FN(js_propertyIsEnumerable_str, obj_propertyIsEnumerable,    1,0),
2848 #if OLD_GETTER_SETTER_METHODS
2849     JS_FN(js_defineGetter_str,         js_obj_defineGetter,         2,0),
2850     JS_FN(js_defineSetter_str,         js_obj_defineSetter,         2,0),
2851     JS_FN(js_lookupGetter_str,         obj_lookupGetter,            1,0),
2852     JS_FN(js_lookupSetter_str,         obj_lookupSetter,            1,0),
2853 #endif
2854     JS_FS_END
2855 };
2856
2857 static JSFunctionSpec object_static_methods[] = {
2858     JS_FN("getPrototypeOf",            obj_getPrototypeOf,          1,0),
2859     JS_FN("getOwnPropertyDescriptor",  obj_getOwnPropertyDescriptor,2,0),
2860     JS_FN("keys",                      obj_keys,                    1,0),
2861     JS_FN("defineProperty",            obj_defineProperty,          3,0),
2862     JS_FN("defineProperties",          obj_defineProperties,        2,0),
2863     JS_FN("create",                    obj_create,                  2,0),
2864     JS_FN("getOwnPropertyNames",       obj_getOwnPropertyNames,     1,0),
2865     JS_FN("isExtensible",              obj_isExtensible,            1,0),
2866     JS_FN("preventExtensions",         obj_preventExtensions,       1,0),
2867     JS_FN("freeze",                    obj_freeze,                  1,0),
2868     JS_FN("isFrozen",                  obj_isFrozen,                1,0),
2869     JS_FN("seal",                      obj_seal,                    1,0),
2870     JS_FN("isSealed",                  obj_isSealed,                1,0),
2871     JS_FS_END
2872 };
2873
2874 JSBool
2875 js_Object(JSContext *cx, uintN argc, Value *vp)
2876 {
2877     JSObject *obj;
2878     if (argc == 0) {
2879         /* Trigger logic below to construct a blank object. */
2880         obj = NULL;
2881     } else {
2882         /* If argv[0] is null or undefined, obj comes back null. */
2883         if (!js_ValueToObjectOrNull(cx, vp[2], &obj))
2884             return JS_FALSE;
2885     }
2886     if (!obj) {
2887         /* Make an object whether this was called with 'new' or not. */
2888         JS_ASSERT(!argc || vp[2].isNull() || vp[2].isUndefined());
2889         gc::FinalizeKind kind = NewObjectGCKind(cx, &js_ObjectClass);
2890         obj = NewBuiltinClassInstance(cx, &js_ObjectClass, kind);
2891         if (!obj)
2892             return JS_FALSE;
2893     }
2894     vp->setObject(*obj);
2895     return JS_TRUE;
2896 }
2897
2898 JSObject*
2899 js_CreateThis(JSContext *cx, JSObject *callee)
2900 {
2901     Class *clasp = callee->getClass();
2902
2903     Class *newclasp = &js_ObjectClass;
2904     if (clasp == &js_FunctionClass) {
2905         JSFunction *fun = callee->getFunctionPrivate();
2906         if (fun->isNative() && fun->u.n.clasp)
2907             newclasp = fun->u.n.clasp;
2908     }
2909
2910     Value protov;
2911     if (!callee->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom), &protov))
2912         return NULL;
2913
2914     JSObject *proto = protov.isObjectOrNull() ? protov.toObjectOrNull() : NULL;
2915     JSObject *parent = callee->getParent();
2916     gc::FinalizeKind kind = NewObjectGCKind(cx, newclasp);
2917     JSObject *obj = NewObject<WithProto::Class>(cx, newclasp, proto, parent, kind);
2918     if (obj)
2919         obj->syncSpecialEquality();
2920     return obj;
2921 }
2922
2923 JSObject *
2924 js_CreateThisForFunctionWithProto(JSContext *cx, JSObject *callee, JSObject *proto)
2925 {
2926     gc::FinalizeKind kind = NewObjectGCKind(cx, &js_ObjectClass);
2927     return NewNonFunction<WithProto::Class>(cx, &js_ObjectClass, proto, callee->getParent(), kind);
2928 }
2929
2930 JSObject *
2931 js_CreateThisForFunction(JSContext *cx, JSObject *callee)
2932 {
2933     Value protov;
2934     if (!callee->getProperty(cx,
2935                              ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom),
2936                              &protov)) {
2937         return NULL;
2938     }
2939     JSObject *proto = protov.isObject() ? &protov.toObject() : NULL;
2940     return js_CreateThisForFunctionWithProto(cx, callee, proto);
2941 }
2942
2943 #ifdef JS_TRACER
2944
2945 static JS_ALWAYS_INLINE JSObject*
2946 NewObjectWithClassProto(JSContext *cx, Class *clasp, JSObject *proto,
2947                         /*gc::FinalizeKind*/ unsigned _kind)
2948 {
2949     JS_ASSERT(clasp->isNative());
2950     gc::FinalizeKind kind = gc::FinalizeKind(_kind);
2951
2952     JSObject* obj = js_NewGCObject(cx, kind);
2953     if (!obj)
2954         return NULL;
2955
2956     if (!obj->initSharingEmptyShape(cx, clasp, proto, proto->getParent(), NULL, kind))
2957         return NULL;
2958     return obj;
2959 }
2960
2961 JSObject* FASTCALL
2962 js_Object_tn(JSContext* cx, JSObject* proto)
2963 {
2964     JS_ASSERT(!(js_ObjectClass.flags & JSCLASS_HAS_PRIVATE));
2965     return NewObjectWithClassProto(cx, &js_ObjectClass, proto, FINALIZE_OBJECT8);
2966 }
2967
2968 JS_DEFINE_TRCINFO_1(js_Object,
2969     (2, (extern, CONSTRUCTOR_RETRY, js_Object_tn, CONTEXT, CALLEE_PROTOTYPE, 0,
2970          nanojit::ACCSET_STORE_ANY)))
2971
2972 JSObject* FASTCALL
2973 js_InitializerObject(JSContext* cx, JSObject *proto, JSObject *baseobj)
2974 {
2975     if (!baseobj) {
2976         gc::FinalizeKind kind = GuessObjectGCKind(0, false);
2977         return NewObjectWithClassProto(cx, &js_ObjectClass, proto, kind);
2978     }
2979
2980     return CopyInitializerObject(cx, baseobj);
2981 }
2982
2983 JS_DEFINE_CALLINFO_3(extern, OBJECT, js_InitializerObject, CONTEXT, OBJECT, OBJECT,
2984                      0, nanojit::ACCSET_STORE_ANY)
2985
2986 JSObject* FASTCALL
2987 js_String_tn(JSContext* cx, JSObject* proto, JSString* str)
2988 {
2989     JS_ASSERT(JS_ON_TRACE(cx));
2990     JSObject *obj = NewObjectWithClassProto(cx, &js_StringClass, proto, FINALIZE_OBJECT2);
2991     if (!obj)
2992         return NULL;
2993     obj->setPrimitiveThis(StringValue(str));
2994     return obj;
2995 }
2996 JS_DEFINE_CALLINFO_3(extern, OBJECT, js_String_tn, CONTEXT, CALLEE_PROTOTYPE, STRING, 0,
2997                      nanojit::ACCSET_STORE_ANY)
2998
2999 JSObject * FASTCALL
3000 js_CreateThisFromTrace(JSContext *cx, JSObject *ctor, uintN protoSlot)
3001 {
3002 #ifdef DEBUG
3003     JS_ASSERT(ctor->isFunction());
3004     JS_ASSERT(ctor->getFunctionPrivate()->isInterpreted());
3005     jsid id = ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom);
3006     const Shape *shape = ctor->nativeLookup(id);
3007     JS_ASSERT(shape->slot == protoSlot);
3008     JS_ASSERT(!shape->configurable());
3009     JS_ASSERT(!shape->isMethod());
3010 #endif
3011
3012     JSObject *parent = ctor->getParent();
3013     JSObject *proto;
3014     const Value &protov = ctor->getSlotRef(protoSlot);
3015     if (protov.isObject()) {
3016         proto = &protov.toObject();
3017     } else {
3018         /*
3019          * GetInterpretedFunctionPrototype found that ctor.prototype is
3020          * primitive. Use Object.prototype for proto, per ES5 13.2.2 step 7.
3021          */
3022         if (!js_GetClassPrototype(cx, parent, JSProto_Object, &proto))
3023             return NULL;
3024     }
3025
3026     gc::FinalizeKind kind = NewObjectGCKind(cx, &js_ObjectClass);
3027     return NewNativeClassInstance(cx, &js_ObjectClass, proto, parent, kind);
3028 }
3029 JS_DEFINE_CALLINFO_3(extern, CONSTRUCTOR_RETRY, js_CreateThisFromTrace, CONTEXT, OBJECT, UINTN, 0,
3030                      nanojit::ACCSET_STORE_ANY)
3031
3032 #else  /* !JS_TRACER */
3033
3034 # define js_Object_trcinfo NULL
3035
3036 #endif /* !JS_TRACER */
3037
3038 /*
3039  * Given pc pointing after a property accessing bytecode, return true if the
3040  * access is "object-detecting" in the sense used by web scripts, e.g., when
3041  * checking whether document.all is defined.
3042  */
3043 JS_REQUIRES_STACK JSBool
3044 Detecting(JSContext *cx, jsbytecode *pc)
3045 {
3046     JSScript *script;
3047     jsbytecode *endpc;
3048     JSOp op;
3049     JSAtom *atom;
3050
3051     script = cx->fp()->script();
3052     endpc = script->code + script->length;
3053     for (;; pc += js_CodeSpec[op].length) {
3054         JS_ASSERT_IF(!cx->fp()->hasImacropc(), script->code <= pc && pc < endpc);
3055
3056         /* General case: a branch or equality op follows the access. */
3057         op = js_GetOpcode(cx, script, pc);
3058         if (js_CodeSpec[op].format & JOF_DETECTING)
3059             return JS_TRUE;
3060
3061         switch (op) {
3062           case JSOP_NULL:
3063             /*
3064              * Special case #1: handle (document.all == null).  Don't sweat
3065              * about JS1.2's revision of the equality operators here.
3066              */
3067             if (++pc < endpc) {
3068                 op = js_GetOpcode(cx, script, pc);
3069                 return *pc == JSOP_EQ || *pc == JSOP_NE;
3070             }
3071             return JS_FALSE;
3072
3073           case JSOP_GETGNAME:
3074           case JSOP_NAME:
3075             /*
3076              * Special case #2: handle (document.all == undefined).  Don't
3077              * worry about someone redefining undefined, which was added by
3078              * Edition 3, so is read/write for backward compatibility.
3079              */
3080             GET_ATOM_FROM_BYTECODE(script, pc, 0, atom);
3081             if (atom == cx->runtime->atomState.typeAtoms[JSTYPE_VOID] &&
3082                 (pc += js_CodeSpec[op].length) < endpc) {
3083                 op = js_GetOpcode(cx, script, pc);
3084                 return op == JSOP_EQ || op == JSOP_NE ||
3085                        op == JSOP_STRICTEQ || op == JSOP_STRICTNE;
3086             }
3087             return JS_FALSE;
3088
3089           default:
3090             /*
3091              * At this point, anything but an extended atom index prefix means
3092              * we're not detecting.
3093              */
3094             if (!(js_CodeSpec[op].format & JOF_INDEXBASE))
3095                 return JS_FALSE;
3096             break;
3097         }
3098     }
3099 }
3100
3101 /*
3102  * Infer lookup flags from the currently executing bytecode. This does
3103  * not attempt to infer JSRESOLVE_WITH, because the current bytecode
3104  * does not indicate whether we are in a with statement. Return defaultFlags
3105  * if a currently executing bytecode cannot be determined.
3106  */
3107 uintN
3108 js_InferFlags(JSContext *cx, uintN defaultFlags)
3109 {
3110 #ifdef JS_TRACER
3111     if (JS_ON_TRACE(cx))
3112         return JS_TRACE_MONITOR_ON_TRACE(cx)->bailExit->lookupFlags;
3113 #endif
3114
3115     JS_ASSERT_NOT_ON_TRACE(cx);
3116
3117     jsbytecode *pc;
3118     const JSCodeSpec *cs;
3119     uint32 format;
3120     uintN flags = 0;
3121
3122     JSStackFrame *const fp = js_GetTopStackFrame(cx);
3123     if (!fp || !(pc = cx->regs->pc))
3124         return defaultFlags;
3125     cs = &js_CodeSpec[js_GetOpcode(cx, fp->script(), pc)];
3126     format = cs->format;
3127     if (JOF_MODE(format) != JOF_NAME)
3128         flags |= JSRESOLVE_QUALIFIED;
3129     if ((format & (JOF_SET | JOF_FOR)) || fp->isAssigning()) {
3130         flags |= JSRESOLVE_ASSIGNING;
3131     } else if (cs->length >= 0) {
3132         pc += cs->length;
3133         JSScript *script = cx->fp()->script();
3134         if (pc < script->code + script->length && Detecting(cx, pc))
3135             flags |= JSRESOLVE_DETECTING;
3136     }
3137     if (format & JOF_DECLARING)
3138         flags |= JSRESOLVE_DECLARING;
3139     return flags;
3140 }
3141
3142 /*
3143  * ObjectOps and Class for with-statement stack objects.
3144  */
3145 static JSBool
3146 with_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
3147                     JSProperty **propp)
3148 {
3149     /* Fixes bug 463997 */
3150     uintN flags = cx->resolveFlags;
3151     if (flags == JSRESOLVE_INFER)
3152         flags = js_InferFlags(cx, flags);
3153     flags |= JSRESOLVE_WITH;
3154     JSAutoResolveFlags rf(cx, flags);
3155     return obj->getProto()->lookupProperty(cx, id, objp, propp);
3156 }
3157
3158 static JSBool
3159 with_GetProperty(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id, Value *vp)
3160 {
3161     return obj->getProto()->getProperty(cx, id, vp);
3162 }
3163
3164 static JSBool
3165 with_SetProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp, JSBool strict)
3166 {
3167     return obj->getProto()->setProperty(cx, id, vp, strict);
3168 }
3169
3170 static JSBool
3171 with_GetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
3172 {
3173     return obj->getProto()->getAttributes(cx, id, attrsp);
3174 }
3175
3176 static JSBool
3177 with_SetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
3178 {
3179     return obj->getProto()->setAttributes(cx, id, attrsp);
3180 }
3181
3182 static JSBool
3183 with_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, Value *rval, JSBool strict)
3184 {
3185     return obj->getProto()->deleteProperty(cx, id, rval, strict);
3186 }
3187
3188 static JSBool
3189 with_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
3190                Value *statep, jsid *idp)
3191 {
3192     return obj->getProto()->enumerate(cx, enum_op, statep, idp);
3193 }
3194
3195 static JSType
3196 with_TypeOf(JSContext *cx, JSObject *obj)
3197 {
3198     return JSTYPE_OBJECT;
3199 }
3200
3201 static JSObject *
3202 with_ThisObject(JSContext *cx, JSObject *obj)
3203 {
3204     return obj->getWithThis();
3205 }
3206
3207 Class js_WithClass = {
3208     "With",
3209     JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(2) | JSCLASS_IS_ANONYMOUS,
3210     PropertyStub,         /* addProperty */
3211     PropertyStub,         /* delProperty */
3212     PropertyStub,         /* getProperty */
3213     StrictPropertyStub,   /* setProperty */
3214     EnumerateStub,
3215     ResolveStub,
3216     ConvertStub,
3217     NULL,                 /* finalize */
3218     NULL,                 /* reserved    */
3219     NULL,                 /* checkAccess */
3220     NULL,                 /* call        */
3221     NULL,                 /* construct   */
3222     NULL,                 /* xdrObject   */
3223     NULL,                 /* hasInstance */
3224     NULL,                 /* mark        */
3225     JS_NULL_CLASS_EXT,
3226     {
3227         with_LookupProperty,
3228         NULL,             /* defineProperty */
3229         with_GetProperty,
3230         with_SetProperty,
3231         with_GetAttributes,
3232         with_SetAttributes,
3233         with_DeleteProperty,
3234         with_Enumerate,
3235         with_TypeOf,
3236         NULL,             /* trace */
3237         NULL,             /* fix   */
3238         with_ThisObject,
3239         NULL,             /* clear */
3240     }
3241 };
3242
3243 JS_REQUIRES_STACK JSObject *
3244 js_NewWithObject(JSContext *cx, JSObject *proto, JSObject *parent, jsint depth)
3245 {
3246     JSObject *obj;
3247
3248     obj = js_NewGCObject(cx, FINALIZE_OBJECT2);
3249     if (!obj)
3250         return NULL;
3251
3252     JSStackFrame *priv = js_FloatingFrameIfGenerator(cx, cx->fp());
3253
3254     obj->init(cx, &js_WithClass, proto, parent, priv, false);
3255     obj->setMap(cx->compartment->emptyWithShape);
3256     OBJ_SET_BLOCK_DEPTH(cx, obj, depth);
3257
3258     AutoObjectRooter tvr(cx, obj);
3259     JSObject *thisp = proto->thisObject(cx);
3260     if (!thisp)
3261         return NULL;
3262
3263     assertSameCompartment(cx, obj, thisp);
3264
3265     obj->setWithThis(thisp);
3266     return obj;
3267 }
3268
3269 JSObject *
3270 js_NewBlockObject(JSContext *cx)
3271 {
3272     /*
3273      * Null obj's proto slot so that Object.prototype.* does not pollute block
3274      * scopes and to give the block object its own scope.
3275      */
3276     JSObject *blockObj = js_NewGCObject(cx, FINALIZE_OBJECT2);
3277     if (!blockObj)
3278         return NULL;
3279
3280     blockObj->init(cx, &js_BlockClass, NULL, NULL, NULL, false);
3281     blockObj->setMap(cx->compartment->emptyBlockShape);
3282     return blockObj;
3283 }
3284
3285 JSObject *
3286 js_CloneBlockObject(JSContext *cx, JSObject *proto, JSStackFrame *fp)
3287 {
3288     JS_ASSERT(proto->isStaticBlock());
3289
3290     size_t count = OBJ_BLOCK_COUNT(cx, proto);
3291     gc::FinalizeKind kind = gc::GetGCObjectKind(count + 1);
3292
3293     JSObject *clone = js_NewGCObject(cx, kind);
3294     if (!clone)
3295         return NULL;
3296
3297     JSStackFrame *priv = js_FloatingFrameIfGenerator(cx, fp);
3298
3299     /* The caller sets parent on its own. */
3300     clone->init(cx, &js_BlockClass, proto, NULL, priv, false);
3301
3302     clone->setMap(proto->map);
3303     if (!clone->ensureInstanceReservedSlots(cx, count + 1))
3304         return NULL;
3305
3306     clone->setSlot(JSSLOT_BLOCK_DEPTH, proto->getSlot(JSSLOT_BLOCK_DEPTH));
3307
3308     JS_ASSERT(clone->isClonedBlock());
3309     return clone;
3310 }
3311
3312 JS_REQUIRES_STACK JSBool
3313 js_PutBlockObject(JSContext *cx, JSBool normalUnwind)
3314 {
3315     JSStackFrame *const fp = cx->fp();
3316     JSObject *obj = &fp->scopeChain();
3317     JS_ASSERT(obj->isClonedBlock());
3318     JS_ASSERT(obj->getPrivate() == js_FloatingFrameIfGenerator(cx, cx->fp()));
3319
3320     /* Block objects should have all reserved slots allocated early. */
3321     uintN count = OBJ_BLOCK_COUNT(cx, obj);
3322     JS_ASSERT(obj->numSlots() >= JSSLOT_BLOCK_DEPTH + 1 + count);
3323
3324     /* The block and its locals must be on the current stack for GC safety. */
3325     uintN depth = OBJ_BLOCK_DEPTH(cx, obj);
3326     JS_ASSERT(depth <= size_t(cx->regs->sp - fp->base()));
3327     JS_ASSERT(count <= size_t(cx->regs->sp - fp->base() - depth));
3328
3329     /* See comments in CheckDestructuring from jsparse.cpp. */
3330     JS_ASSERT(count >= 1);
3331
3332     if (normalUnwind) {
3333         uintN slot = JSSLOT_BLOCK_FIRST_FREE_SLOT;
3334         depth += fp->numFixed();
3335         memcpy(obj->getSlots() + slot, fp->slots() + depth, count * sizeof(Value));
3336     }
3337
3338     /* We must clear the private slot even with errors. */
3339     obj->setPrivate(NULL);
3340     fp->setScopeChainNoCallObj(*obj->getParent());
3341     return normalUnwind;
3342 }
3343
3344 static JSBool
3345 block_getProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp)
3346 {
3347     /*
3348      * Block objects are never exposed to script, and the engine handles them
3349      * with care. So unlike other getters, this one can assert (rather than
3350      * check) certain invariants about obj.
3351      */
3352     JS_ASSERT(obj->isClonedBlock());
3353     uintN index = (uintN) JSID_TO_INT(id);
3354     JS_ASSERT(index < OBJ_BLOCK_COUNT(cx, obj));
3355
3356     JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
3357     if (fp) {
3358         fp = js_LiveFrameIfGenerator(fp);
3359         index += fp->numFixed() + OBJ_BLOCK_DEPTH(cx, obj);
3360         JS_ASSERT(index < fp->numSlots());
3361         *vp = fp->slots()[index];
3362         return true;
3363     }
3364
3365     /* Values are in slots immediately following the class-reserved ones. */
3366     JS_ASSERT(obj->getSlot(JSSLOT_FREE(&js_BlockClass) + index) == *vp);
3367     return true;
3368 }
3369
3370 static JSBool
3371 block_setProperty(JSContext *cx, JSObject *obj, jsid id, JSBool strict, Value *vp)
3372 {
3373     JS_ASSERT(obj->isClonedBlock());
3374     uintN index = (uintN) JSID_TO_INT(id);
3375     JS_ASSERT(index < OBJ_BLOCK_COUNT(cx, obj));
3376
3377     JSStackFrame *fp = (JSStackFrame *) obj->getPrivate();
3378     if (fp) {
3379         fp = js_LiveFrameIfGenerator(fp);
3380         index += fp->numFixed() + OBJ_BLOCK_DEPTH(cx, obj);
3381         JS_ASSERT(index < fp->numSlots());
3382         fp->slots()[index] = *vp;
3383         return true;
3384     }
3385
3386     /*
3387      * The value in *vp will be written back to the slot in obj that was
3388      * allocated when this let binding was defined.
3389      */
3390     return true;
3391 }
3392
3393 const Shape *
3394 JSObject::defineBlockVariable(JSContext *cx, jsid id, intN index)
3395 {
3396     JS_ASSERT(isStaticBlock());
3397
3398     /* Use JSPROP_ENUMERATE to aid the disassembler. */
3399     uint32 slot = JSSLOT_FREE(&js_BlockClass) + index;
3400     const Shape *shape = addProperty(cx, id,
3401                                      block_getProperty, block_setProperty,
3402                                      slot, JSPROP_ENUMERATE | JSPROP_PERMANENT,
3403                                      Shape::HAS_SHORTID, index);
3404     if (!shape)
3405         return NULL;
3406     if (slot >= numSlots() && !growSlots(cx, slot + 1))
3407         return NULL;
3408     return shape;
3409 }
3410
3411 static size_t
3412 GetObjectSize(JSObject *obj)
3413 {
3414     return (obj->isFunction() && !obj->getPrivate())
3415            ? sizeof(JSFunction)
3416            : sizeof(JSObject) + sizeof(js::Value) * obj->numFixedSlots();
3417 }
3418
3419 bool
3420 JSObject::copyPropertiesFrom(JSContext *cx, JSObject *obj)
3421 {
3422     // If we're not native, then we cannot copy properties.
3423     JS_ASSERT(isNative() == obj->isNative());
3424     if (!isNative())
3425         return true;
3426
3427     AutoShapeVector shapes(cx);
3428     for (Shape::Range r(obj->lastProperty()); !r.empty(); r.popFront()) {
3429         if (!shapes.append(&r.front()))
3430             return false;
3431     }
3432
3433     size_t n = shapes.length();
3434     while (n > 0) {
3435         const Shape *shape = shapes[--n];
3436         uintN attrs = shape->attributes();
3437         PropertyOp getter = shape->getter();
3438         if ((attrs & JSPROP_GETTER) && !cx->compartment->wrap(cx, &getter))
3439             return false;
3440         StrictPropertyOp setter = shape->setter();
3441         if ((attrs & JSPROP_SETTER) && !cx->compartment->wrap(cx, &setter))
3442             return false;
3443         Value v = shape->hasSlot() ? obj->getSlot(shape->slot) : UndefinedValue();
3444         if (!cx->compartment->wrap(cx, &v))
3445             return false;
3446         if (!defineProperty(cx, shape->id, v, getter, setter, attrs))
3447             return false;
3448     }
3449     return true;
3450 }
3451
3452 static bool
3453 CopySlots(JSContext *cx, JSObject *from, JSObject *to)
3454 {
3455     JS_ASSERT(!from->isNative() && !to->isNative());
3456     size_t nslots = from->numSlots();
3457     if (to->ensureSlots(cx, nslots))
3458         return false;
3459
3460     size_t n = 0;
3461     if (to->isWrapper() &&
3462         (JSWrapper::wrapperHandler(to)->flags() & JSWrapper::CROSS_COMPARTMENT)) {
3463         to->slots[0] = from->slots[0];
3464         to->slots[1] = from->slots[1];
3465         n = 2;
3466     }
3467
3468     for (; n < nslots; ++n) {
3469         Value v = from->slots[n];
3470         if (!cx->compartment->wrap(cx, &v))
3471             return false;
3472         to->slots[n] = v;
3473     }
3474     return true;
3475 }
3476
3477 JSObject *
3478 JSObject::clone(JSContext *cx, JSObject *proto, JSObject *parent)
3479 {
3480     /*
3481      * We can only clone native objects and proxies. Dense arrays are slowified if
3482      * we try to clone them.
3483      */
3484     if (!isNative()) {
3485         if (isDenseArray()) {
3486             if (!makeDenseArraySlow(cx))
3487                 return NULL;
3488         } else if (!isProxy()) {
3489             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
3490                                  JSMSG_CANT_CLONE_OBJECT);
3491             return NULL;
3492         }
3493     }
3494     JSObject *clone = NewObject<WithProto::Given>(cx, getClass(),
3495                                                   proto, parent,
3496                                                   gc::FinalizeKind(finalizeKind()));
3497     if (!clone)
3498         return NULL;
3499     if (isNative()) {
3500         if (clone->isFunction() && (compartment() != clone->compartment())) {
3501             JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
3502                                  JSMSG_CANT_CLONE_OBJECT);
3503             return NULL;
3504         }
3505
3506         if (getClass()->flags & JSCLASS_HAS_PRIVATE)
3507             clone->setPrivate(getPrivate());
3508     } else {
3509         JS_ASSERT(isProxy());
3510         if (!CopySlots(cx, this, clone))
3511             return NULL;
3512     }
3513     return clone;
3514 }
3515
3516 static void
3517 TradeGuts(JSObject *a, JSObject *b)
3518 {
3519     JS_ASSERT(a->compartment() == b->compartment());
3520     JS_ASSERT(a->isFunction() == b->isFunction());
3521
3522     /*
3523      * Regexp guts are more complicated -- we would need to migrate the
3524      * refcounted JIT code blob for them across compartments instead of just
3525      * swapping guts.
3526      */
3527     JS_ASSERT(!a->isRegExp() && !b->isRegExp());
3528
3529     bool aInline = !a->hasSlotsArray();
3530     bool bInline = !b->hasSlotsArray();
3531
3532     /* Trade the guts of the objects. */
3533     const size_t size = GetObjectSize(a);
3534     if (size == GetObjectSize(b)) {
3535         /*
3536          * If the objects are the same size, then we make no assumptions about
3537          * whether they have dynamically allocated slots and instead just copy
3538          * them over wholesale.
3539          */
3540         char tmp[tl::Max<sizeof(JSFunction), sizeof(JSObject_Slots16)>::result];
3541         JS_ASSERT(size <= sizeof(tmp));
3542
3543         memcpy(tmp, a, size);
3544         memcpy(a, b, size);
3545         memcpy(b, tmp, size);
3546
3547         /* Fixup pointers for inline slots on the objects. */
3548         if (aInline)
3549             b->slots = b->fixedSlots();
3550         if (bInline)
3551             a->slots = a->fixedSlots();
3552     } else {
3553         /*
3554          * If the objects are of differing sizes, then we only copy over the
3555          * JSObject portion (things like class, etc.) and leave it to
3556          * JSObject::clone to copy over the dynamic slots for us.
3557          */
3558         if (a->isFunction()) {
3559             JSFunction tmp;
3560             memcpy(&tmp, a, sizeof tmp);
3561             memcpy(a, b, sizeof tmp);
3562             memcpy(b, &tmp, sizeof tmp);
3563         } else {
3564             JSObject tmp;
3565             memcpy(&tmp, a, sizeof tmp);
3566             memcpy(a, b, sizeof tmp);
3567             memcpy(b, &tmp, sizeof tmp);
3568         }
3569
3570         JS_ASSERT(!aInline);
3571         JS_ASSERT(!bInline);
3572     }
3573 }
3574
3575 /*
3576  * Use this method with extreme caution. It trades the guts of two objects and updates
3577  * scope ownership. This operation is not thread-safe, just as fast array to slow array
3578  * transitions are inherently not thread-safe. Don't perform a swap operation on objects
3579  * shared across threads or, or bad things will happen. You have been warned.
3580  */
3581 bool
3582 JSObject::swap(JSContext *cx, JSObject *other)
3583 {
3584     /*
3585      * If we are swapping objects with a different number of builtin slots, force
3586      * both to not use their inline slots.
3587      */
3588     if (GetObjectSize(this) != GetObjectSize(other)) {
3589         if (!hasSlotsArray()) {
3590             if (!allocSlots(cx, numSlots()))
3591                 return false;
3592         }
3593         if (!other->hasSlotsArray()) {
3594             if (!other->allocSlots(cx, other->numSlots()))
3595                 return false;
3596         }
3597     }
3598
3599     if (this->compartment() == other->compartment()) {
3600         TradeGuts(this, other);
3601         return true;
3602     }
3603
3604     JSObject *thisClone;
3605     JSObject *otherClone;
3606     {
3607         AutoCompartment ac(cx, other);
3608         if (!ac.enter())
3609             return false;
3610         thisClone = this->clone(cx, other->getProto(), other->getParent());
3611         if (!thisClone || !thisClone->copyPropertiesFrom(cx, this))
3612             return false;
3613     }
3614     {
3615         AutoCompartment ac(cx, this);
3616         if (!ac.enter())
3617             return false;
3618         otherClone = other->clone(cx, other->getProto(), other->getParent());
3619         if (!otherClone || !otherClone->copyPropertiesFrom(cx, other))
3620             return false;
3621     }
3622     TradeGuts(this, otherClone);
3623     TradeGuts(other, thisClone);
3624
3625     return true;
3626 }
3627
3628 #if JS_HAS_XDR
3629
3630 #define NO_PARENT_INDEX ((uint32)-1)
3631
3632 uint32
3633 FindObjectIndex(JSObjectArray *array, JSObject *obj)
3634 {
3635     size_t i;
3636
3637     if (array) {
3638         i = array->length;
3639         do {
3640
3641             if (array->vector[--i] == obj)
3642                 return i;
3643         } while (i != 0);
3644     }
3645
3646     return NO_PARENT_INDEX;
3647 }
3648
3649 JSBool
3650 js_XDRBlockObject(JSXDRState *xdr, JSObject **objp)
3651 {
3652     JSContext *cx;
3653     uint32 parentId;
3654     JSObject *obj, *parent;
3655     uintN depth, count;
3656     uint32 depthAndCount;
3657     const Shape *shape;
3658
3659     cx = xdr->cx;
3660 #ifdef __GNUC__
3661     obj = NULL;         /* quell GCC overwarning */
3662 #endif
3663
3664     if (xdr->mode == JSXDR_ENCODE) {
3665         obj = *objp;
3666         parent = obj->getParent();
3667         parentId = JSScript::isValidOffset(xdr->script->objectsOffset)
3668                    ? FindObjectIndex(xdr->script->objects(), parent)
3669                    : NO_PARENT_INDEX;
3670         depth = (uint16)OBJ_BLOCK_DEPTH(cx, obj);
3671         count = (uint16)OBJ_BLOCK_COUNT(cx, obj);
3672         depthAndCount = (uint32)(depth << 16) | count;
3673     }
3674 #ifdef __GNUC__ /* suppress bogus gcc warnings */
3675     else count = 0;
3676 #endif
3677
3678     /* First, XDR the parent atomid. */
3679     if (!JS_XDRUint32(xdr, &parentId))
3680         return JS_FALSE;
3681
3682     if (xdr->mode == JSXDR_DECODE) {
3683         obj = js_NewBlockObject(cx);
3684         if (!obj)
3685             return JS_FALSE;
3686         *objp = obj;
3687
3688         /*
3689          * If there's a parent id, then get the parent out of our script's
3690          * object array. We know that we XDR block object in outer-to-inner
3691          * order, which means that getting the parent now will work.
3692          */
3693         if (parentId == NO_PARENT_INDEX)
3694             parent = NULL;
3695         else
3696             parent = xdr->script->getObject(parentId);
3697         obj->setParent(parent);
3698     }
3699
3700     AutoObjectRooter tvr(cx, obj);
3701
3702     if (!JS_XDRUint32(xdr, &depthAndCount))
3703         return false;
3704
3705     if (xdr->mode == JSXDR_DECODE) {
3706         depth = (uint16)(depthAndCount >> 16);
3707         count = (uint16)depthAndCount;
3708         obj->setSlot(JSSLOT_BLOCK_DEPTH, Value(Int32Value(depth)));
3709
3710         /*
3711          * XDR the block object's properties. We know that there are 'count'
3712          * properties to XDR, stored as id/shortid pairs.
3713          */
3714         for (uintN i = 0; i < count; i++) {
3715             JSAtom *atom;
3716             uint16 shortid;
3717
3718             /* XDR the real id, then the shortid. */
3719             if (!js_XDRAtom(xdr, &atom) || !JS_XDRUint16(xdr, &shortid))
3720                 return false;
3721
3722             if (!obj->defineBlockVariable(cx, ATOM_TO_JSID(atom), shortid))
3723                 return false;
3724         }
3725     } else {
3726         AutoShapeVector shapes(cx);
3727         shapes.growBy(count);
3728
3729         for (Shape::Range r(obj->lastProperty()); !r.empty(); r.popFront()) {
3730             shape = &r.front();
3731             shapes[shape->shortid] = shape;
3732         }
3733
3734         /*
3735          * XDR the block object's properties. We know that there are 'count'
3736          * properties to XDR, stored as id/shortid pairs.
3737          */
3738         for (uintN i = 0; i < count; i++) {
3739             shape = shapes[i];
3740             JS_ASSERT(shape->getter() == block_getProperty);
3741
3742             jsid propid = shape->id;
3743             JS_ASSERT(JSID_IS_ATOM(propid));
3744             JSAtom *atom = JSID_TO_ATOM(propid);
3745
3746             uint16 shortid = uint16(shape->shortid);
3747             JS_ASSERT(shortid == i);
3748
3749             /* XDR the real id, then the shortid. */
3750             if (!js_XDRAtom(xdr, &atom) || !JS_XDRUint16(xdr, &shortid))
3751                 return false;
3752         }
3753     }
3754     return true;
3755 }
3756
3757 #endif
3758
3759 Class js_BlockClass = {
3760     "Block",
3761     JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_IS_ANONYMOUS,
3762     PropertyStub,         /* addProperty */
3763     PropertyStub,         /* delProperty */
3764     PropertyStub,         /* getProperty */
3765     StrictPropertyStub,   /* setProperty */
3766     EnumerateStub,
3767     ResolveStub,
3768     ConvertStub
3769 };
3770
3771 JSObject *
3772 js_InitObjectClass(JSContext *cx, JSObject *obj)
3773 {
3774     JSObject *proto = js_InitClass(cx, obj, NULL, &js_ObjectClass, js_Object, 1,
3775                                    object_props, object_methods, NULL, object_static_methods);
3776     if (!proto)
3777         return NULL;
3778
3779     /* ECMA (15.1.2.1) says 'eval' is a property of the global object. */
3780     jsid id = ATOM_TO_JSID(cx->runtime->atomState.evalAtom);
3781     if (!js_DefineFunction(cx, obj, id, eval, 1, JSFUN_STUB_GSOPS))
3782         return NULL;
3783
3784     return proto;
3785 }
3786
3787 static bool
3788 DefineStandardSlot(JSContext *cx, JSObject *obj, JSProtoKey key, JSAtom *atom,
3789                    const Value &v, uint32 attrs, bool &named)
3790 {
3791     jsid id = ATOM_TO_JSID(atom);
3792
3793     if (key != JSProto_Null) {
3794         /*
3795          * Initializing an actual standard class on a global object. If the
3796          * property is not yet present, force it into a new one bound to a
3797          * reserved slot. Otherwise, go through the normal property path.
3798          */
3799         JS_ASSERT(obj->isGlobal());
3800         JS_ASSERT(obj->isNative());
3801
3802         if (!obj->ensureClassReservedSlots(cx))
3803             return false;
3804
3805         const Shape *shape = obj->nativeLookup(id);
3806         if (!shape) {
3807             uint32 slot = 2 * JSProto_LIMIT + key;
3808             if (!js_SetReservedSlot(cx, obj, slot, v))
3809                 return false;
3810             if (!obj->addProperty(cx, id, PropertyStub, StrictPropertyStub, slot, attrs, 0, 0))
3811                 return false;
3812
3813             named = true;
3814             return true;
3815         }
3816     }
3817
3818     named = obj->defineProperty(cx, id, v, PropertyStub, StrictPropertyStub, attrs);
3819     return named;
3820 }
3821
3822 namespace js {
3823
3824 JSObject *
3825 DefineConstructorAndPrototype(JSContext *cx, JSObject *obj, JSProtoKey key, JSAtom *atom,
3826                               JSObject *protoProto, Class *clasp,
3827                               Native constructor, uintN nargs,
3828                               JSPropertySpec *ps, JSFunctionSpec *fs,
3829                               JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
3830 {
3831     /*
3832      * Create a prototype object for this class.
3833      *
3834      * FIXME: lazy standard (built-in) class initialization and even older
3835      * eager boostrapping code rely on all of these properties:
3836      *
3837      * 1. NewObject attempting to compute a default prototype object when
3838      *    passed null for proto; and
3839      *
3840      * 2. NewObject tolerating no default prototype (null proto slot value)
3841      *    due to this js_InitClass call coming from js_InitFunctionClass on an
3842      *    otherwise-uninitialized global.
3843      *
3844      * 3. NewObject allocating a JSFunction-sized GC-thing when clasp is
3845      *    &js_FunctionClass, not a JSObject-sized (smaller) GC-thing.
3846      *
3847      * The JS_NewObjectForGivenProto and JS_NewObject APIs also allow clasp to
3848      * be &js_FunctionClass (we could break compatibility easily). But fixing
3849      * (3) is not enough without addressing the bootstrapping dependency on (1)
3850      * and (2).
3851      */
3852     JSObject *proto = NewObject<WithProto::Class>(cx, clasp, protoProto, obj);
3853     if (!proto)
3854         return NULL;
3855
3856     proto->syncSpecialEquality();
3857
3858     /* After this point, control must exit via label bad or out. */
3859     AutoObjectRooter tvr(cx, proto);
3860
3861     JSObject *ctor;
3862     bool named = false;
3863     if (!constructor) {
3864         /*
3865          * Lacking a constructor, name the prototype (e.g., Math) unless this
3866          * class (a) is anonymous, i.e. for internal use only; (b) the class
3867          * of obj (the global object) is has a reserved slot indexed by key;
3868          * and (c) key is not the null key.
3869          */
3870         if (!(clasp->flags & JSCLASS_IS_ANONYMOUS) || !obj->isGlobal() || key == JSProto_Null) {
3871             uint32 attrs = (clasp->flags & JSCLASS_IS_ANONYMOUS)
3872                            ? JSPROP_READONLY | JSPROP_PERMANENT
3873                            : 0;
3874             if (!DefineStandardSlot(cx, obj, key, atom, ObjectValue(*proto), attrs, named))
3875                 goto bad;
3876         }
3877
3878         ctor = proto;
3879     } else {
3880         JSFunction *fun = js_NewFunction(cx, NULL, constructor, nargs, JSFUN_CONSTRUCTOR, obj, atom);
3881         if (!fun)
3882             goto bad;
3883
3884         AutoValueRooter tvr2(cx, ObjectValue(*fun));
3885         if (!DefineStandardSlot(cx, obj, key, atom, tvr2.value(), 0, named))
3886             goto bad;
3887
3888         /*
3889          * Remember the class this function is a constructor for so that
3890          * we know to create an object of this class when we call the
3891          * constructor.
3892          */
3893         FUN_CLASP(fun) = clasp;
3894
3895         /*
3896          * Optionally construct the prototype object, before the class has
3897          * been fully initialized.  Allow the ctor to replace proto with a
3898          * different object, as is done for operator new -- and as at least
3899          * XML support requires.
3900          */
3901         ctor = FUN_OBJECT(fun);
3902         if (clasp->flags & JSCLASS_CONSTRUCT_PROTOTYPE) {
3903             Value rval;
3904             if (!InvokeConstructorWithGivenThis(cx, proto, ObjectOrNullValue(ctor),
3905                                                 0, NULL, &rval)) {
3906                 goto bad;
3907             }
3908             if (rval.isObject() && &rval.toObject() != proto)
3909                 proto = &rval.toObject();
3910         }
3911
3912         /* Connect constructor and prototype by named properties. */
3913         if (!js_SetClassPrototype(cx, ctor, proto,
3914                                   JSPROP_READONLY | JSPROP_PERMANENT)) {
3915             goto bad;
3916         }
3917
3918         /* Bootstrap Function.prototype (see also JS_InitStandardClasses). */
3919         if (ctor->getClass() == clasp)
3920             ctor->setProto(proto);
3921     }
3922
3923     /* Add properties and methods to the prototype and the constructor. */
3924     if ((ps && !JS_DefineProperties(cx, proto, ps)) ||
3925         (fs && !JS_DefineFunctions(cx, proto, fs)) ||
3926         (static_ps && !JS_DefineProperties(cx, ctor, static_ps)) ||
3927         (static_fs && !JS_DefineFunctions(cx, ctor, static_fs))) {
3928         goto bad;
3929     }
3930
3931     /*
3932      * Pre-brand the prototype and constructor if they have built-in methods.
3933      * This avoids extra shape guard branch exits in the tracejitted code.
3934      */
3935     if (fs)
3936         proto->brand(cx);
3937     if (ctor != proto && static_fs)
3938         ctor->brand(cx);
3939
3940     /*
3941      * Make sure proto's emptyShape is available to be shared by objects of
3942      * this class.  JSObject::emptyShape is a one-slot cache. If we omit this,
3943      * some other class could snap it up. (The risk is particularly great for
3944      * Object.prototype.)
3945      *
3946      * All callers of JSObject::initSharingEmptyShape depend on this.
3947      *
3948      * FIXME: bug 592296 -- js_InitArrayClass should pass &js_SlowArrayClass
3949      * and make the Array.prototype slow from the start.
3950      */
3951     JS_ASSERT_IF(proto->clasp != clasp,
3952                  clasp == &js_ArrayClass && proto->clasp == &js_SlowArrayClass);
3953     if (!proto->getEmptyShape(cx, proto->clasp, FINALIZE_OBJECT0))
3954         goto bad;
3955
3956     if (clasp->flags & (JSCLASS_FREEZE_PROTO|JSCLASS_FREEZE_CTOR)) {
3957         JS_ASSERT_IF(ctor == proto, !(clasp->flags & JSCLASS_FREEZE_CTOR));
3958         if (proto && (clasp->flags & JSCLASS_FREEZE_PROTO) && !proto->freeze(cx))
3959             goto bad;
3960         if (ctor && (clasp->flags & JSCLASS_FREEZE_CTOR) && !ctor->freeze(cx))
3961             goto bad;
3962     }
3963
3964     /* If this is a standard class, cache its prototype. */
3965     if (key != JSProto_Null && !js_SetClassObject(cx, obj, key, ctor, proto))
3966         goto bad;
3967
3968     return proto;
3969
3970 bad:
3971     if (named) {
3972         Value rval;
3973         obj->deleteProperty(cx, ATOM_TO_JSID(atom), &rval, false);
3974     }
3975     return NULL;
3976 }
3977
3978 }
3979
3980 JSObject *
3981 js_InitClass(JSContext *cx, JSObject *obj, JSObject *protoProto,
3982              Class *clasp, Native constructor, uintN nargs,
3983              JSPropertySpec *ps, JSFunctionSpec *fs,
3984              JSPropertySpec *static_ps, JSFunctionSpec *static_fs)
3985 {
3986     JSAtom *atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0);
3987     if (!atom)
3988         return NULL;
3989
3990     /*
3991      * All instances of the class will inherit properties from the prototype
3992      * object we are about to create (in DefineConstructorAndPrototype), which
3993      * in turn will inherit from protoProto.
3994      *
3995      * When initializing a standard class (other than Object), if protoProto is
3996      * null, default to the Object prototype object. The engine's internal uses
3997      * of js_InitClass depend on this nicety. Note that in
3998      * js_InitFunctionAndObjectClasses, we specially hack the resolving table
3999      * and then depend on js_GetClassPrototype here leaving protoProto NULL and
4000      * returning true.
4001      */
4002     JSProtoKey key = JSCLASS_CACHED_PROTO_KEY(clasp);
4003     if (key != JSProto_Null &&
4004         !protoProto &&
4005         !js_GetClassPrototype(cx, obj, JSProto_Object, &protoProto)) {
4006         return NULL;
4007     }
4008
4009     return DefineConstructorAndPrototype(cx, obj, key, atom, protoProto, clasp, constructor, nargs,
4010                                          ps, fs, static_ps, static_fs);
4011 }
4012
4013 bool
4014 JSObject::allocSlots(JSContext *cx, size_t newcap)
4015 {
4016     uint32 oldcap = numSlots();
4017
4018     JS_ASSERT(newcap >= oldcap && !hasSlotsArray());
4019
4020     if (newcap > NSLOTS_LIMIT) {
4021         if (!JS_ON_TRACE(cx))
4022             js_ReportAllocationOverflow(cx);
4023         return false;
4024     }
4025
4026     Value *tmpslots = (Value*) cx->malloc(newcap * sizeof(Value));
4027     if (!tmpslots)
4028         return false;  /* Leave slots at inline buffer. */
4029     slots = tmpslots;
4030     capacity = newcap;
4031
4032     /* Copy over anything from the inline buffer. */
4033     memcpy(slots, fixedSlots(), oldcap * sizeof(Value));
4034     ClearValueRange(slots + oldcap, newcap - oldcap, isDenseArray());
4035     return true;
4036 }
4037
4038 bool
4039 JSObject::growSlots(JSContext *cx, size_t newcap)
4040 {
4041     /*
4042      * When an object with CAPACITY_DOUBLING_MAX or fewer slots needs to
4043      * grow, double its capacity, to add N elements in amortized O(N) time.
4044      *
4045      * Above this limit, grow by 12.5% each time. Speed is still amortized
4046      * O(N), with a higher constant factor, and we waste less space.
4047      */
4048     static const size_t CAPACITY_DOUBLING_MAX = 1024 * 1024;
4049     static const size_t CAPACITY_CHUNK = CAPACITY_DOUBLING_MAX / sizeof(Value);
4050
4051     uint32 oldcap = numSlots();
4052     JS_ASSERT(oldcap < newcap);
4053
4054     uint32 nextsize = (oldcap <= CAPACITY_DOUBLING_MAX)
4055                     ? oldcap * 2
4056                     : oldcap + (oldcap >> 3);
4057
4058     uint32 actualCapacity = JS_MAX(newcap, nextsize);
4059     if (actualCapacity >= CAPACITY_CHUNK)
4060         actualCapacity = JS_ROUNDUP(actualCapacity, CAPACITY_CHUNK);
4061     else if (actualCapacity < SLOT_CAPACITY_MIN)
4062         actualCapacity = SLOT_CAPACITY_MIN;
4063
4064     /* Don't let nslots get close to wrapping around uint32. */
4065     if (actualCapacity >= NSLOTS_LIMIT) {
4066         JS_ReportOutOfMemory(cx);
4067         return false;
4068     }
4069
4070     /* If nothing was allocated yet, treat it as initial allocation. */
4071     if (!hasSlotsArray())
4072         return allocSlots(cx, actualCapacity);
4073
4074     Value *tmpslots = (Value*) cx->realloc(slots, oldcap * sizeof(Value), actualCapacity * sizeof(Value));
4075     if (!tmpslots)
4076         return false;    /* Leave dslots as its old size. */
4077     slots = tmpslots;
4078     capacity = actualCapacity;
4079
4080     /* Initialize the additional slots we added. */
4081     ClearValueRange(slots + oldcap, actualCapacity - oldcap, isDenseArray());
4082     return true;
4083 }
4084
4085 void
4086 JSObject::shrinkSlots(JSContext *cx, size_t newcap)
4087 {
4088     uint32 oldcap = numSlots();
4089     JS_ASSERT(newcap <= oldcap);
4090     JS_ASSERT(newcap >= slotSpan());
4091
4092     if (oldcap <= SLOT_CAPACITY_MIN || !hasSlotsArray()) {
4093         /* We won't shrink the slots any more.  Clear excess holes. */
4094         ClearValueRange(slots + newcap, oldcap - newcap, isDenseArray());
4095         return;
4096     }
4097
4098     uint32 fill = newcap;
4099     if (newcap < SLOT_CAPACITY_MIN)
4100         newcap = SLOT_CAPACITY_MIN;
4101     if (newcap < numFixedSlots())
4102         newcap = numFixedSlots();
4103
4104     Value *tmpslots = (Value*) cx->realloc(slots, newcap * sizeof(Value));
4105     if (!tmpslots)
4106         return;  /* Leave slots at its old size. */
4107     slots = tmpslots;
4108     capacity = newcap;
4109
4110     if (fill < newcap) {
4111         /* Clear any excess holes if we tried to shrink below SLOT_CAPACITY_MIN. */
4112         ClearValueRange(slots + fill, newcap - fill, isDenseArray());
4113     }
4114 }
4115
4116 bool
4117 JSObject::ensureInstanceReservedSlots(JSContext *cx, size_t nreserved)
4118 {
4119     JS_ASSERT_IF(isNative(),
4120                  isBlock() || isCall() || (isFunction() && isBoundFunction()));
4121
4122     uintN nslots = JSSLOT_FREE(clasp) + nreserved;
4123     return nslots <= numSlots() || allocSlots(cx, nslots);
4124 }
4125
4126 static JSObject *
4127 js_InitNullClass(JSContext *cx, JSObject *obj)
4128 {
4129     JS_ASSERT(0);
4130     return NULL;
4131 }
4132
4133 #define JS_PROTO(name,code,init) extern JSObject *init(JSContext *, JSObject *);
4134 #include "jsproto.tbl"
4135 #undef JS_PROTO
4136
4137 static JSObjectOp lazy_prototype_init[JSProto_LIMIT] = {
4138 #define JS_PROTO(name,code,init) init,
4139 #include "jsproto.tbl"
4140 #undef JS_PROTO
4141 };
4142
4143 namespace js {
4144
4145 bool
4146 SetProto(JSContext *cx, JSObject *obj, JSObject *proto, bool checkForCycles)
4147 {
4148     JS_ASSERT_IF(!checkForCycles, obj != proto);
4149     JS_ASSERT(obj->isExtensible());
4150
4151     if (obj->isNative()) {
4152         if (!obj->ensureClassReservedSlots(cx))
4153             return false;
4154     }
4155
4156     /*
4157      * Regenerate property cache shape ids for all of the scopes along the
4158      * old prototype chain to invalidate their property cache entries, in
4159      * case any entries were filled by looking up through obj.
4160      */
4161     JSObject *oldproto = obj;
4162     while (oldproto && oldproto->isNative()) {
4163         oldproto->protoShapeChange(cx);
4164         oldproto = oldproto->getProto();
4165     }
4166
4167     if (!proto || !checkForCycles) {
4168         obj->setProto(proto);
4169     } else if (!SetProtoCheckingForCycles(cx, obj, proto)) {
4170         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CYCLIC_VALUE, js_proto_str);
4171         return false;
4172     }
4173     return true;
4174 }
4175
4176 }
4177
4178 JSBool
4179 js_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
4180                   JSObject **objp)
4181 {
4182     JSObject *cobj;
4183     JSResolvingKey rkey;
4184     JSResolvingEntry *rentry;
4185     uint32 generation;
4186     JSObjectOp init;
4187     Value v;
4188
4189     obj = obj->getGlobal();
4190     if (!obj->isGlobal()) {
4191         *objp = NULL;
4192         return JS_TRUE;
4193     }
4194
4195     v = obj->getReservedSlot(key);
4196     if (v.isObject()) {
4197         *objp = &v.toObject();
4198         return JS_TRUE;
4199     }
4200
4201     rkey.obj = obj;
4202     rkey.id = ATOM_TO_JSID(cx->runtime->atomState.classAtoms[key]);
4203     if (!js_StartResolving(cx, &rkey, JSRESFLAG_LOOKUP, &rentry))
4204         return JS_FALSE;
4205     if (!rentry) {
4206         /* Already caching key in obj -- suppress recursion. */
4207         *objp = NULL;
4208         return JS_TRUE;
4209     }
4210     generation = cx->resolvingTable->generation;
4211
4212     JSBool ok = true;
4213     cobj = NULL;
4214     init = lazy_prototype_init[key];
4215     if (init) {
4216         if (!init(cx, obj)) {
4217             ok = JS_FALSE;
4218         } else {
4219             v = obj->getReservedSlot(key);
4220             if (v.isObject())
4221                 cobj = &v.toObject();
4222         }
4223     }
4224
4225     js_StopResolving(cx, &rkey, JSRESFLAG_LOOKUP, rentry, generation);
4226     *objp = cobj;
4227     return ok;
4228 }
4229
4230 JSBool
4231 js_SetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key, JSObject *cobj, JSObject *proto)
4232 {
4233     JS_ASSERT(!obj->getParent());
4234     if (!obj->isGlobal())
4235         return JS_TRUE;
4236
4237     return js_SetReservedSlot(cx, obj, key, ObjectOrNullValue(cobj)) &&
4238            js_SetReservedSlot(cx, obj, JSProto_LIMIT + key, ObjectOrNullValue(proto));
4239 }
4240
4241 JSBool
4242 js_FindClassObject(JSContext *cx, JSObject *start, JSProtoKey protoKey,
4243                    Value *vp, Class *clasp)
4244 {
4245     JSStackFrame *fp;
4246     JSObject *obj, *cobj, *pobj;
4247     jsid id;
4248     JSProperty *prop;
4249     const Shape *shape;
4250
4251     /*
4252      * Find the global object. Use cx->fp() directly to avoid falling off
4253      * trace; all JIT-elided stack frames have the same global object as
4254      * cx->fp().
4255      */
4256     VOUCH_DOES_NOT_REQUIRE_STACK();
4257     if (!start && (fp = cx->maybefp()) != NULL)
4258         start = &fp->scopeChain();
4259
4260     if (start) {
4261         /* Find the topmost object in the scope chain. */
4262         do {
4263             obj = start;
4264             start = obj->getParent();
4265         } while (start);
4266     } else {
4267         obj = cx->globalObject;
4268         if (!obj) {
4269             vp->setUndefined();
4270             return JS_TRUE;
4271         }
4272     }
4273
4274     OBJ_TO_INNER_OBJECT(cx, obj);
4275     if (!obj)
4276         return JS_FALSE;
4277
4278     if (protoKey != JSProto_Null) {
4279         JS_ASSERT(JSProto_Null < protoKey);
4280         JS_ASSERT(protoKey < JSProto_LIMIT);
4281         if (!js_GetClassObject(cx, obj, protoKey, &cobj))
4282             return JS_FALSE;
4283         if (cobj) {
4284             vp->setObject(*cobj);
4285             return JS_TRUE;
4286         }
4287         id = ATOM_TO_JSID(cx->runtime->atomState.classAtoms[protoKey]);
4288     } else {
4289         JSAtom *atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0);
4290         if (!atom)
4291             return false;
4292         id = ATOM_TO_JSID(atom);
4293     }
4294
4295     JS_ASSERT(obj->isNative());
4296     if (js_LookupPropertyWithFlags(cx, obj, id, JSRESOLVE_CLASSNAME,
4297                                    &pobj, &prop) < 0) {
4298         return JS_FALSE;
4299     }
4300     Value v = UndefinedValue();
4301     if (prop && pobj->isNative()) {
4302         shape = (Shape *) prop;
4303         if (pobj->containsSlot(shape->slot)) {
4304             v = pobj->nativeGetSlot(shape->slot);
4305             if (v.isPrimitive())
4306                 v.setUndefined();
4307         }
4308     }
4309     *vp = v;
4310     return JS_TRUE;
4311 }
4312
4313 JSObject *
4314 js_ConstructObject(JSContext *cx, Class *clasp, JSObject *proto, JSObject *parent,
4315                    uintN argc, Value *argv)
4316 {
4317     AutoArrayRooter argtvr(cx, argc, argv);
4318
4319     JSProtoKey protoKey = GetClassProtoKey(clasp);
4320
4321     /* Protect constructor in case a crazy getter for .prototype uproots it. */
4322     AutoValueRooter tvr(cx);
4323     if (!js_FindClassObject(cx, parent, protoKey, tvr.addr(), clasp))
4324         return NULL;
4325
4326     const Value &cval = tvr.value();
4327     if (tvr.value().isPrimitive()) {
4328         js_ReportIsNotFunction(cx, tvr.addr(), JSV2F_CONSTRUCT | JSV2F_SEARCH_STACK);
4329         return NULL;
4330     }
4331
4332     /*
4333      * If proto is NULL, set it to Constructor.prototype, just like JSOP_NEW
4334      * does, likewise for the new object's parent.
4335      */
4336     JSObject *ctor = &cval.toObject();
4337     if (!parent)
4338         parent = ctor->getParent();
4339     if (!proto) {
4340         Value rval;
4341         if (!ctor->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom),
4342                                &rval)) {
4343             return NULL;
4344         }
4345         if (rval.isObjectOrNull())
4346             proto = rval.toObjectOrNull();
4347     }
4348
4349     JSObject *obj = NewObject<WithProto::Class>(cx, clasp, proto, parent);
4350     if (!obj)
4351         return NULL;
4352
4353     obj->syncSpecialEquality();
4354
4355     Value rval;
4356     if (!InvokeConstructorWithGivenThis(cx, obj, cval, argc, argv, &rval))
4357         return NULL;
4358
4359     if (rval.isPrimitive())
4360         return obj;
4361
4362     /*
4363      * If the instance's class differs from what was requested, throw a type
4364      * error.  If the given class has both the JSCLASS_HAS_PRIVATE and the
4365      * JSCLASS_CONSTRUCT_PROTOTYPE flags, and the instance does not have its
4366      * private data set at this point, then the constructor was replaced and
4367      * we should throw a type error.
4368      */
4369     obj = &rval.toObject();
4370     if (obj->getClass() != clasp ||
4371         (!(~clasp->flags & (JSCLASS_HAS_PRIVATE |
4372                             JSCLASS_CONSTRUCT_PROTOTYPE)) &&
4373          !obj->getPrivate())) {
4374         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
4375                              JSMSG_WRONG_CONSTRUCTOR, clasp->name);
4376         return NULL;
4377     }
4378     return obj;
4379 }
4380
4381 bool
4382 JSObject::allocSlot(JSContext *cx, uint32 *slotp)
4383 {
4384     uint32 slot = slotSpan();
4385     JS_ASSERT(slot >= JSSLOT_FREE(clasp));
4386
4387     /*
4388      * If this object is in dictionary mode and it has a property table, try to
4389      * pull a free slot from the property table's slot-number freelist.
4390      */
4391     if (inDictionaryMode() && lastProp->hasTable()) {
4392         uint32 &last = lastProp->getTable()->freelist;
4393         if (last != SHAPE_INVALID_SLOT) {
4394 #ifdef DEBUG
4395             JS_ASSERT(last < slot);
4396             uint32 next = getSlot(last).toPrivateUint32();
4397             JS_ASSERT_IF(next != SHAPE_INVALID_SLOT, next < slot);
4398 #endif
4399
4400             *slotp = last;
4401
4402             Value &vref = getSlotRef(last);
4403             last = vref.toPrivateUint32();
4404             vref.setUndefined();
4405             return true;
4406         }
4407     }
4408
4409     if (slot >= numSlots() && !growSlots(cx, slot + 1))
4410         return false;
4411
4412     /* JSObject::growSlots or JSObject::freeSlot should set the free slots to void. */
4413     JS_ASSERT(getSlot(slot).isUndefined());
4414     *slotp = slot;
4415     return true;
4416 }
4417
4418 bool
4419 JSObject::freeSlot(JSContext *cx, uint32 slot)
4420 {
4421     uint32 limit = slotSpan();
4422     JS_ASSERT(slot < limit);
4423
4424     Value &vref = getSlotRef(slot);
4425     if (inDictionaryMode() && lastProp->hasTable()) {
4426         uint32 &last = lastProp->getTable()->freelist;
4427
4428         /* Can't afford to check the whole freelist, but let's check the head. */
4429         JS_ASSERT_IF(last != SHAPE_INVALID_SLOT, last < limit && last != slot);
4430
4431         /*
4432          * Freeing a slot other than the last one mapped by this object's
4433          * shape (and not a reserved slot; see bug 595230): push the slot onto
4434          * the dictionary property table's freelist. We want to let the last
4435          * slot be freed by shrinking the dslots vector; see js_TraceObject.
4436          */
4437         if (JSSLOT_FREE(clasp) <= slot && slot + 1 < limit) {
4438             JS_ASSERT_IF(last != SHAPE_INVALID_SLOT, last < slotSpan());
4439             vref.setPrivateUint32(last);
4440             last = slot;
4441             return true;
4442         }
4443     }
4444     vref.setUndefined();
4445     return false;
4446 }
4447
4448 /* JSBOXEDWORD_INT_MAX as a string */
4449 #define JSBOXEDWORD_INT_MAX_STRING "1073741823"
4450
4451 /*
4452  * Convert string indexes that convert to int jsvals as ints to save memory.
4453  * Care must be taken to use this macro every time a property name is used, or
4454  * else double-sets, incorrect property cache misses, or other mistakes could
4455  * occur.
4456  */
4457 jsid
4458 js_CheckForStringIndex(jsid id)
4459 {
4460     if (!JSID_IS_ATOM(id))
4461         return id;
4462
4463     JSAtom *atom = JSID_TO_ATOM(id);
4464     JSString *str = ATOM_TO_STRING(atom);
4465     const jschar *s = str->flatChars();
4466     jschar ch = *s;
4467
4468     JSBool negative = (ch == '-');
4469     if (negative)
4470         ch = *++s;
4471
4472     if (!JS7_ISDEC(ch))
4473         return id;
4474
4475     size_t n = str->flatLength() - negative;
4476     if (n > sizeof(JSBOXEDWORD_INT_MAX_STRING) - 1)
4477         return id;
4478
4479     const jschar *cp = s;
4480     const jschar *end = s + n;
4481
4482     jsuint index = JS7_UNDEC(*cp++);
4483     jsuint oldIndex = 0;
4484     jsuint c = 0;
4485
4486     if (index != 0) {
4487         while (JS7_ISDEC(*cp)) {
4488             oldIndex = index;
4489             c = JS7_UNDEC(*cp);
4490             index = 10 * index + c;
4491             cp++;
4492         }
4493     }
4494
4495     /*
4496      * Non-integer indexes can't be represented as integers.  Also, distinguish
4497      * index "-0" from "0", because JSBOXEDWORD_INT cannot.
4498      */
4499     if (cp != end || (negative && index == 0))
4500         return id;
4501
4502     if (negative) {
4503         if (oldIndex < -(JSID_INT_MIN / 10) ||
4504             (oldIndex == -(JSID_INT_MIN / 10) && c <= (-JSID_INT_MIN % 10)))
4505         {
4506             id = INT_TO_JSID(-jsint(index));
4507         }
4508     } else {
4509         if (oldIndex < JSID_INT_MAX / 10 ||
4510             (oldIndex == JSID_INT_MAX / 10 && c <= (JSID_INT_MAX % 10)))
4511         {
4512             id = INT_TO_JSID(jsint(index));
4513         }
4514     }
4515
4516     return id;
4517 }
4518
4519 static JSBool
4520 PurgeProtoChain(JSContext *cx, JSObject *obj, jsid id)
4521 {
4522     const Shape *shape;
4523
4524     while (obj) {
4525         if (!obj->isNative()) {
4526             obj = obj->getProto();
4527             continue;
4528         }
4529         shape = obj->nativeLookup(id);
4530         if (shape) {
4531             PCMETER(JS_PROPERTY_CACHE(cx).pcpurges++);
4532             obj->shadowingShapeChange(cx, *shape);
4533
4534             if (!obj->getParent()) {
4535                 /*
4536                  * All scope chains end in a global object, so this will change
4537                  * the global shape. jstracer.cpp assumes that the global shape
4538                  * never changes on trace, so we must deep-bail here.
4539                  */
4540                 LeaveTrace(cx);
4541             }
4542             return JS_TRUE;
4543         }
4544         obj = obj->getProto();
4545     }
4546     return JS_FALSE;
4547 }
4548
4549 void
4550 js_PurgeScopeChainHelper(JSContext *cx, JSObject *obj, jsid id)
4551 {
4552     JS_ASSERT(obj->isDelegate());
4553     PurgeProtoChain(cx, obj->getProto(), id);
4554
4555     /*
4556      * We must purge the scope chain only for Call objects as they are the only
4557      * kind of cacheable non-global object that can gain properties after outer
4558      * properties with the same names have been cached or traced. Call objects
4559      * may gain such properties via eval introducing new vars; see bug 490364.
4560      */
4561     if (obj->isCall()) {
4562         while ((obj = obj->getParent()) != NULL) {
4563             if (PurgeProtoChain(cx, obj, id))
4564                 break;
4565         }
4566     }
4567 }
4568
4569 const Shape *
4570 js_AddNativeProperty(JSContext *cx, JSObject *obj, jsid id,
4571                      PropertyOp getter, StrictPropertyOp setter, uint32 slot,
4572                      uintN attrs, uintN flags, intN shortid)
4573 {
4574     JS_ASSERT(!(flags & Shape::METHOD));
4575
4576     /*
4577      * Purge the property cache of now-shadowed id in obj's scope chain. Do
4578      * this optimistically (assuming no failure below) before locking obj, so
4579      * we can lock the shadowed scope.
4580      */
4581     js_PurgeScopeChain(cx, obj, id);
4582
4583     if (!obj->ensureClassReservedSlots(cx))
4584         return NULL;
4585
4586     /* Convert string indices to integers if appropriate. */
4587     id = js_CheckForStringIndex(id);
4588     return obj->putProperty(cx, id, getter, setter, slot, attrs, flags, shortid);
4589 }
4590
4591 const Shape *
4592 js_ChangeNativePropertyAttrs(JSContext *cx, JSObject *obj,
4593                              const Shape *shape, uintN attrs, uintN mask,
4594                              PropertyOp getter, StrictPropertyOp setter)
4595 {
4596     if (!obj->ensureClassReservedSlots(cx))
4597         return NULL;
4598
4599     /*
4600      * Check for freezing an object with shape-memoized methods here, on a
4601      * shape-by-shape basis. Note that getter may be a pun of the method's
4602      * joined function object value, to indicate "no getter change". In this
4603      * case we must null getter to get the desired PropertyStub behavior.
4604      */
4605     if ((attrs & JSPROP_READONLY) && shape->isMethod()) {
4606         JSObject *funobj = &shape->methodObject();
4607         Value v = ObjectValue(*funobj);
4608
4609         shape = obj->methodReadBarrier(cx, *shape, &v);
4610         if (!shape)
4611             return NULL;
4612
4613         if (CastAsObject(getter) == funobj) {
4614             JS_ASSERT(!(attrs & JSPROP_GETTER));
4615             getter = NULL;
4616         }
4617     }
4618
4619     return obj->changeProperty(cx, shape, attrs, mask, getter, setter);
4620 }
4621
4622 JSBool
4623 js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, const Value *value,
4624                   PropertyOp getter, StrictPropertyOp setter, uintN attrs)
4625 {
4626     return js_DefineNativeProperty(cx, obj, id, *value, getter, setter, attrs,
4627                                    0, 0, NULL);
4628 }
4629
4630 /*
4631  * Backward compatibility requires allowing addProperty hooks to mutate the
4632  * nominal initial value of a slotful property, while GC safety wants that
4633  * value to be stored before the call-out through the hook.  Optimize to do
4634  * both while saving cycles for classes that stub their addProperty hook.
4635  */
4636 static inline bool
4637 CallAddPropertyHook(JSContext *cx, Class *clasp, JSObject *obj, const Shape *shape, Value *vp)
4638 {
4639     if (clasp->addProperty != PropertyStub) {
4640         Value nominal = *vp;
4641
4642         if (!CallJSPropertyOp(cx, clasp->addProperty, obj, shape->id, vp))
4643             return false;
4644         if (*vp != nominal) {
4645             if (obj->containsSlot(shape->slot))
4646                 obj->nativeSetSlot(shape->slot, *vp);
4647         }
4648     }
4649     return true;
4650 }
4651
4652 JSBool
4653 js_DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, const Value &value,
4654                         PropertyOp getter, StrictPropertyOp setter, uintN attrs,
4655                         uintN flags, intN shortid, JSProperty **propp,
4656                         uintN defineHow /* = 0 */)
4657 {
4658     JS_ASSERT((defineHow & ~(JSDNP_CACHE_RESULT | JSDNP_DONT_PURGE | JSDNP_SET_METHOD)) == 0);
4659     LeaveTraceIfGlobalObject(cx, obj);
4660
4661     /* Convert string indices to integers if appropriate. */
4662     id = js_CheckForStringIndex(id);
4663
4664     /*
4665      * If defining a getter or setter, we must check for its counterpart and
4666      * update the attributes and property ops.  A getter or setter is really
4667      * only half of a property.
4668      */
4669     const Shape *shape = NULL;
4670     if (attrs & (JSPROP_GETTER | JSPROP_SETTER)) {
4671         JSObject *pobj;
4672         JSProperty *prop;
4673
4674         /*
4675          * If JS_THREADSAFE and id is found, js_LookupProperty returns with
4676          * shape non-null and pobj locked.  If pobj == obj, the property is
4677          * already in obj and obj has its own (mutable) scope.  So if we are
4678          * defining a getter whose setter was already defined, or vice versa,
4679          * finish the job via obj->changeProperty, and refresh the property
4680          * cache line for (obj, id) to map shape.
4681          */
4682         if (!js_LookupProperty(cx, obj, id, &pobj, &prop))
4683             return JS_FALSE;
4684         shape = (Shape *) prop;
4685         if (shape && pobj == obj && shape->isAccessorDescriptor()) {
4686             shape = obj->changeProperty(cx, shape, attrs,
4687                                         JSPROP_GETTER | JSPROP_SETTER,
4688                                         (attrs & JSPROP_GETTER)
4689                                         ? getter
4690                                         : shape->getter(),
4691                                         (attrs & JSPROP_SETTER)
4692                                         ? setter
4693                                         : shape->setter());
4694
4695             if (!shape)
4696                 return false;
4697         } else if (prop) {
4698             prop = NULL;
4699             shape = NULL;
4700         }
4701     }
4702
4703     /*
4704      * Purge the property cache of any properties named by id that are about
4705      * to be shadowed in obj's scope chain unless it is known a priori that it
4706      * is not possible. We do this before locking obj to avoid nesting locks.
4707      */
4708     if (!(defineHow & JSDNP_DONT_PURGE))
4709         js_PurgeScopeChain(cx, obj, id);
4710
4711     /*
4712      * Check whether a readonly property or setter is being defined on a known
4713      * prototype object. See the comment in jscntxt.h before protoHazardShape's
4714      * member declaration.
4715      */
4716     if (obj->isDelegate() && (attrs & (JSPROP_READONLY | JSPROP_SETTER)))
4717         cx->runtime->protoHazardShape = js_GenerateShape(cx);
4718
4719     /* Use the object's class getter and setter by default. */
4720     Class *clasp = obj->getClass();
4721     if (!(defineHow & JSDNP_SET_METHOD)) {
4722         if (!getter && !(attrs & JSPROP_GETTER))
4723             getter = clasp->getProperty;
4724         if (!setter && !(attrs & JSPROP_SETTER))
4725             setter = clasp->setProperty;
4726     }
4727
4728     /* Get obj's own scope if it has one, or create a new one for obj. */
4729     if (!obj->ensureClassReservedSlots(cx))
4730         return false;
4731
4732     /*
4733      * Make a local copy of value, in case a method barrier needs to update the
4734      * value to define, and just so addProperty can mutate its inout parameter.
4735      */
4736     Value valueCopy = value;
4737     bool adding = false;
4738
4739     if (!shape) {
4740         /* Add a new property, or replace an existing one of the same id. */
4741         if (defineHow & JSDNP_SET_METHOD) {
4742             JS_ASSERT(clasp == &js_ObjectClass);
4743             JS_ASSERT(IsFunctionObject(value));
4744             JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
4745             JS_ASSERT(!getter && !setter);
4746
4747             JSObject *funobj = &value.toObject();
4748             if (FUN_OBJECT(GET_FUNCTION_PRIVATE(cx, funobj)) == funobj) {
4749                 flags |= Shape::METHOD;
4750                 getter = CastAsPropertyOp(funobj);
4751             }
4752         }
4753
4754         if (const Shape *existingShape = obj->nativeLookup(id)) {
4755             if (existingShape->hasSlot())
4756                 AbortRecordingIfUnexpectedGlobalWrite(cx, obj, existingShape->slot);
4757
4758             if (existingShape->isMethod() &&
4759                 ObjectValue(existingShape->methodObject()) == valueCopy)
4760             {
4761                 /*
4762                  * Redefining an existing shape-memoized method object without
4763                  * changing the property's value, perhaps to change attributes.
4764                  * Clone now via the method read barrier.
4765                  *
4766                  * But first, assert that our caller is not trying to preserve
4767                  * the joined function object value as the getter object for
4768                  * the redefined property. The joined function object cannot
4769                  * yet have leaked, so only an internal code path could attempt
4770                  * such a thing. Any such path would be a bug to fix.
4771                  */
4772                 JS_ASSERT(existingShape->getter() != getter);
4773
4774                 if (!obj->methodReadBarrier(cx, *existingShape, &valueCopy))
4775                     return false;
4776             }
4777         } else {
4778             adding = true;
4779         }
4780
4781         uint32 oldShape = obj->shape();
4782         shape = obj->putProperty(cx, id, getter, setter, SHAPE_INVALID_SLOT,
4783                                  attrs, flags, shortid);
4784         if (!shape)
4785             return false;
4786
4787         /*
4788          * If shape is a joined method, the above call to putProperty suffices
4789          * to update the object's shape id if need be (because the shape's hash
4790          * identity includes the method value).
4791          *
4792          * But if scope->branded(), the object's shape id may not have changed
4793          * and we may be overwriting a cached function-valued property (note
4794          * how methodWriteBarrier checks previous vs. would-be current value).
4795          * See bug 560998.
4796          */
4797         if (obj->shape() == oldShape && obj->branded() && shape->slot != SHAPE_INVALID_SLOT) {
4798 #ifdef DEBUG
4799             const Shape *newshape =
4800 #endif
4801                 obj->methodWriteBarrier(cx, *shape, valueCopy);
4802             JS_ASSERT(newshape == shape);
4803         }
4804     }
4805
4806     /* Store valueCopy before calling addProperty, in case the latter GC's. */
4807     if (obj->containsSlot(shape->slot))
4808         obj->nativeSetSlot(shape->slot, valueCopy);
4809
4810     /* XXXbe called with lock held */
4811     if (!CallAddPropertyHook(cx, clasp, obj, shape, &valueCopy)) {
4812         obj->removeProperty(cx, id);
4813         return false;
4814     }
4815
4816     if (defineHow & JSDNP_CACHE_RESULT) {
4817         JS_ASSERT_NOT_ON_TRACE(cx);
4818         if (adding) {
4819             JS_PROPERTY_CACHE(cx).fill(cx, obj, 0, 0, obj, shape, true);
4820             TRACE_1(AddProperty, obj);
4821         }
4822     }
4823     if (propp)
4824         *propp = (JSProperty *) shape;
4825     return true;
4826
4827 #ifdef JS_TRACER
4828   error: // TRACE_1 jumps here on error.
4829 #endif
4830     return false;
4831 }
4832
4833 #define SCOPE_DEPTH_ACCUM(bs,val)                                             \
4834     JS_SCOPE_DEPTH_METERING(JS_BASIC_STATS_ACCUM(bs, val))
4835
4836 /*
4837  * Call obj's resolve hook. obj is a native object and the caller holds its
4838  * scope lock.
4839  *
4840  * cx, start, id, and flags are the parameters initially passed to the ongoing
4841  * lookup; objp and propp are its out parameters. obj is an object along
4842  * start's prototype chain.
4843  *
4844  * There are four possible outcomes:
4845  *
4846  *   - On failure, report an error or exception, unlock obj, and return false.
4847  *
4848  *   - If we are alrady resolving a property of *curobjp, set *recursedp = true,
4849  *     unlock obj, and return true.
4850  *
4851  *   - If the resolve hook finds or defines the sought property, set *objp and
4852  *     *propp appropriately, set *recursedp = false, and return true with *objp's
4853  *     lock held.
4854  *
4855  *   - Otherwise no property was resolved. Set *propp = NULL and *recursedp = false
4856  *     and return true.
4857  */
4858 static JSBool
4859 CallResolveOp(JSContext *cx, JSObject *start, JSObject *obj, jsid id, uintN flags,
4860               JSObject **objp, JSProperty **propp, bool *recursedp)
4861 {
4862     Class *clasp = obj->getClass();
4863     JSResolveOp resolve = clasp->resolve;
4864
4865     /*
4866      * Avoid recursion on (obj, id) already being resolved on cx.
4867      *
4868      * Once we have successfully added an entry for (obj, key) to
4869      * cx->resolvingTable, control must go through cleanup: before
4870      * returning.  But note that JS_DHASH_ADD may find an existing
4871      * entry, in which case we bail to suppress runaway recursion.
4872      */
4873     JSResolvingKey key = {obj, id};
4874     JSResolvingEntry *entry;
4875     if (!js_StartResolving(cx, &key, JSRESFLAG_LOOKUP, &entry))
4876         return false;
4877     if (!entry) {
4878         /* Already resolving id in obj -- suppress recursion. */
4879         *recursedp = true;
4880         return true;
4881     }
4882     uint32 generation = cx->resolvingTable->generation;
4883     *recursedp = false;
4884
4885     *propp = NULL;
4886
4887     JSBool ok;
4888     const Shape *shape = NULL;
4889     if (clasp->flags & JSCLASS_NEW_RESOLVE) {
4890         JSNewResolveOp newresolve = (JSNewResolveOp)resolve;
4891         if (flags == JSRESOLVE_INFER)
4892             flags = js_InferFlags(cx, 0);
4893         JSObject *obj2 = (clasp->flags & JSCLASS_NEW_RESOLVE_GETS_START) ? start : NULL;
4894
4895         {
4896             /* Protect id and all atoms from a GC nested in resolve. */
4897             AutoKeepAtoms keep(cx->runtime);
4898             ok = newresolve(cx, obj, id, flags, &obj2);
4899         }
4900         if (!ok)
4901             goto cleanup;
4902
4903         if (obj2) {
4904             /* Resolved: lookup id again for backward compatibility. */
4905             if (!obj2->isNative()) {
4906                 /* Whoops, newresolve handed back a foreign obj2. */
4907                 JS_ASSERT(obj2 != obj);
4908                 ok = obj2->lookupProperty(cx, id, objp, propp);
4909                 if (!ok || *propp)
4910                     goto cleanup;
4911             } else {
4912                 /*
4913                  * Require that obj2 not be empty now, as we do for old-style
4914                  * resolve.  If it doesn't, then id was not truly resolved, and
4915                  * we'll find it in the proto chain, or miss it if obj2's proto
4916                  * is not on obj's proto chain.  That last case is a "too bad!"
4917                  * case.
4918                  */
4919                 if (!obj2->nativeEmpty())
4920                     shape = obj2->nativeLookup(id);
4921             }
4922             if (shape) {
4923                 JS_ASSERT(!obj2->nativeEmpty());
4924                 obj = obj2;
4925             }
4926         }
4927     } else {
4928         /*
4929          * Old resolve always requires id re-lookup if obj is not empty after
4930          * resolve returns.
4931          */
4932         ok = resolve(cx, obj, id);
4933         if (!ok)
4934             goto cleanup;
4935         JS_ASSERT(obj->isNative());
4936         if (!obj->nativeEmpty())
4937             shape = obj->nativeLookup(id);
4938     }
4939
4940 cleanup:
4941     if (ok && shape) {
4942         *objp = obj;
4943         *propp = (JSProperty *) shape;
4944     }
4945     js_StopResolving(cx, &key, JSRESFLAG_LOOKUP, entry, generation);
4946     return ok;
4947 }
4948
4949 static JS_ALWAYS_INLINE int
4950 js_LookupPropertyWithFlagsInline(JSContext *cx, JSObject *obj, jsid id, uintN flags,
4951                                  JSObject **objp, JSProperty **propp)
4952 {
4953     /* We should not get string indices which aren't already integers here. */
4954     JS_ASSERT(id == js_CheckForStringIndex(id));
4955
4956     /* Search scopes starting with obj and following the prototype link. */
4957     JSObject *start = obj;
4958     int protoIndex;
4959     for (protoIndex = 0; ; protoIndex++) {
4960         const Shape *shape = obj->nativeLookup(id);
4961         if (shape) {
4962             SCOPE_DEPTH_ACCUM(&cx->runtime->protoLookupDepthStats, protoIndex);
4963             *objp = obj;
4964             *propp = (JSProperty *) shape;
4965             return protoIndex;
4966         }
4967
4968         /* Try obj's class resolve hook if id was not found in obj's scope. */
4969         if (!shape && obj->getClass()->resolve != JS_ResolveStub) {
4970             bool recursed;
4971             if (!CallResolveOp(cx, start, obj, id, flags, objp, propp, &recursed))
4972                 return -1;
4973             if (recursed)
4974                 break;
4975             if (*propp) {
4976                 /* Recalculate protoIndex in case it was resolved on some other object. */
4977                 protoIndex = 0;
4978                 for (JSObject *proto = start; proto && proto != *objp; proto = proto->getProto())
4979                     protoIndex++;
4980                 SCOPE_DEPTH_ACCUM(&cx->runtime->protoLookupDepthStats, protoIndex);
4981                 return protoIndex;
4982             }
4983         }
4984
4985         JSObject *proto = obj->getProto();
4986         if (!proto)
4987             break;
4988         if (!proto->isNative()) {
4989             if (!proto->lookupProperty(cx, id, objp, propp))
4990                 return -1;
4991 #ifdef DEBUG
4992             /*
4993              * Non-native objects must have either non-native lookup results,
4994              * or else native results from the non-native's prototype chain.
4995              *
4996              * See JSStackFrame::getValidCalleeObject, where we depend on this
4997              * fact to force a prototype-delegated joined method accessed via
4998              * arguments.callee through the delegating |this| object's method
4999              * read barrier.
5000              */
5001             if (*propp && (*objp)->isNative()) {
5002                 while ((proto = proto->getProto()) != *objp)
5003                     JS_ASSERT(proto);
5004             }
5005 #endif
5006             return protoIndex + 1;
5007         }
5008
5009         obj = proto;
5010     }
5011
5012     *objp = NULL;
5013     *propp = NULL;
5014     return protoIndex;
5015 }
5016
5017 JS_FRIEND_API(JSBool)
5018 js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
5019                   JSProperty **propp)
5020 {
5021     /* Convert string indices to integers if appropriate. */
5022     id = js_CheckForStringIndex(id);
5023
5024     return js_LookupPropertyWithFlagsInline(cx, obj, id, cx->resolveFlags, objp, propp) >= 0;
5025 }
5026
5027 int
5028 js_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, jsid id, uintN flags,
5029                            JSObject **objp, JSProperty **propp)
5030 {
5031     /* Convert string indices to integers if appropriate. */
5032     id = js_CheckForStringIndex(id);
5033
5034     return js_LookupPropertyWithFlagsInline(cx, obj, id, flags, objp, propp);
5035 }
5036
5037 PropertyCacheEntry *
5038 js_FindPropertyHelper(JSContext *cx, jsid id, JSBool cacheResult,
5039                       JSObject **objp, JSObject **pobjp, JSProperty **propp)
5040 {
5041     JSObject *scopeChain, *obj, *parent, *pobj;
5042     PropertyCacheEntry *entry;
5043     int scopeIndex, protoIndex;
5044     JSProperty *prop;
5045
5046     JS_ASSERT_IF(cacheResult, !JS_ON_TRACE(cx));
5047     scopeChain = &js_GetTopStackFrame(cx)->scopeChain();
5048
5049     /* Scan entries on the scope chain that we can cache across. */
5050     entry = JS_NO_PROP_CACHE_FILL;
5051     obj = scopeChain;
5052     parent = obj->getParent();
5053     for (scopeIndex = 0;
5054          parent
5055          ? IsCacheableNonGlobalScope(obj)
5056          : !obj->getOps()->lookupProperty;
5057          ++scopeIndex) {
5058         protoIndex =
5059             js_LookupPropertyWithFlags(cx, obj, id, cx->resolveFlags,
5060                                        &pobj, &prop);
5061         if (protoIndex < 0)
5062             return NULL;
5063
5064         if (prop) {
5065 #ifdef DEBUG
5066             if (parent) {
5067                 Class *clasp = obj->getClass();
5068                 JS_ASSERT(pobj->isNative());
5069                 JS_ASSERT(pobj->getClass() == clasp);
5070                 if (clasp == &js_BlockClass) {
5071                     /*
5072                      * A block instance on the scope chain is immutable and it
5073                      * shares its shapes with its compile-time prototype.
5074                      */
5075                     JS_ASSERT(pobj == obj);
5076                     JS_ASSERT(pobj->isClonedBlock());
5077                     JS_ASSERT(protoIndex == 0);
5078                 } else {
5079                     /* Call and DeclEnvClass objects have no prototypes. */
5080                     JS_ASSERT(!obj->getProto());
5081                     JS_ASSERT(protoIndex == 0);
5082                 }
5083             } else {
5084                 JS_ASSERT(obj->isNative());
5085             }
5086 #endif
5087             /*
5088              * We must check if pobj is native as a global object can have
5089              * non-native prototype.
5090              */
5091             if (cacheResult && pobj->isNative()) {
5092                 entry = JS_PROPERTY_CACHE(cx).fill(cx, scopeChain, scopeIndex,
5093                                                    protoIndex, pobj,
5094                                                    (Shape *) prop);
5095             }
5096             SCOPE_DEPTH_ACCUM(&cx->runtime->scopeSearchDepthStats, scopeIndex);
5097             goto out;
5098         }
5099
5100         if (!parent) {
5101             pobj = NULL;
5102             goto out;
5103         }
5104         obj = parent;
5105         parent = obj->getParent();
5106     }
5107
5108     for (;;) {
5109         if (!obj->lookupProperty(cx, id, &pobj, &prop))
5110             return NULL;
5111         if (prop) {
5112             PCMETER(JS_PROPERTY_CACHE(cx).nofills++);
5113             goto out;
5114         }
5115
5116         /*
5117          * We conservatively assume that a resolve hook could mutate the scope
5118          * chain during JSObject::lookupProperty. So we read parent here again.
5119          */
5120         parent = obj->getParent();
5121         if (!parent) {
5122             pobj = NULL;
5123             break;
5124         }
5125         obj = parent;
5126     }
5127
5128   out:
5129     JS_ASSERT(!!pobj == !!prop);
5130     *objp = obj;
5131     *pobjp = pobj;
5132     *propp = prop;
5133     return entry;
5134 }
5135
5136 /*
5137  * On return, if |*pobjp| is a native object, then |*propp| is a |Shape *|.
5138  * Otherwise, its type and meaning depends on the host object's implementation.
5139  */
5140 JS_FRIEND_API(JSBool)
5141 js_FindProperty(JSContext *cx, jsid id, JSObject **objp, JSObject **pobjp,
5142                 JSProperty **propp)
5143 {
5144     return !!js_FindPropertyHelper(cx, id, false, objp, pobjp, propp);
5145 }
5146
5147 JSObject *
5148 js_FindIdentifierBase(JSContext *cx, JSObject *scopeChain, jsid id)
5149 {
5150     /*
5151      * This function should not be called for a global object or from the
5152      * trace and should have a valid cache entry for native scopeChain.
5153      */
5154     JS_ASSERT(scopeChain->getParent());
5155     JS_ASSERT(!JS_ON_TRACE(cx));
5156
5157     JSObject *obj = scopeChain;
5158
5159     /*
5160      * Loop over cacheable objects on the scope chain until we find a
5161      * property. We also stop when we reach the global object skipping any
5162      * farther checks or lookups. For details see the JSOP_BINDNAME case of
5163      * js_Interpret.
5164      *
5165      * The test order here matters because IsCacheableNonGlobalScope
5166      * must not be passed a global object (i.e. one with null parent).
5167      */
5168     for (int scopeIndex = 0;
5169          !obj->getParent() || IsCacheableNonGlobalScope(obj);
5170          scopeIndex++) {
5171         JSObject *pobj;
5172         JSProperty *prop;
5173         int protoIndex = js_LookupPropertyWithFlags(cx, obj, id,
5174                                                     cx->resolveFlags,
5175                                                     &pobj, &prop);
5176         if (protoIndex < 0)
5177             return NULL;
5178         if (prop) {
5179             if (!pobj->isNative()) {
5180                 JS_ASSERT(!obj->getParent());
5181                 return obj;
5182             }
5183             JS_ASSERT_IF(obj->getParent(), pobj->getClass() == obj->getClass());
5184 #ifdef DEBUG
5185             PropertyCacheEntry *entry =
5186 #endif
5187                 JS_PROPERTY_CACHE(cx).fill(cx, scopeChain, scopeIndex, protoIndex, pobj,
5188                                            (Shape *) prop);
5189             JS_ASSERT(entry);
5190             return obj;
5191         }
5192
5193         JSObject *parent = obj->getParent();
5194         if (!parent)
5195             return obj;
5196         obj = parent;
5197     }
5198
5199     /* Loop until we find a property or reach the global object. */
5200     do {
5201         JSObject *pobj;
5202         JSProperty *prop;
5203         if (!obj->lookupProperty(cx, id, &pobj, &prop))
5204             return NULL;
5205         if (prop)
5206             break;
5207
5208         /*
5209          * We conservatively assume that a resolve hook could mutate the scope
5210          * chain during JSObject::lookupProperty. So we must check if parent is
5211          * not null here even if it wasn't before the lookup.
5212          */
5213         JSObject *parent = obj->getParent();
5214         if (!parent)
5215             break;
5216         obj = parent;
5217     } while (obj->getParent());
5218     return obj;
5219 }
5220
5221 static JS_ALWAYS_INLINE JSBool
5222 js_NativeGetInline(JSContext *cx, JSObject *receiver, JSObject *obj, JSObject *pobj,
5223                    const Shape *shape, uintN getHow, Value *vp)
5224 {
5225     LeaveTraceIfGlobalObject(cx, pobj);
5226
5227     uint32 slot;
5228     int32 sample;
5229
5230     JS_ASSERT(pobj->isNative());
5231
5232     slot = shape->slot;
5233     if (slot != SHAPE_INVALID_SLOT) {
5234         *vp = pobj->nativeGetSlot(slot);
5235         JS_ASSERT(!vp->isMagic());
5236     } else {
5237         vp->setUndefined();
5238     }
5239     if (shape->hasDefaultGetter())
5240         return true;
5241
5242     if (JS_UNLIKELY(shape->isMethod()) && (getHow & JSGET_NO_METHOD_BARRIER)) {
5243         JS_ASSERT(&shape->methodObject() == &vp->toObject());
5244         return true;
5245     }
5246
5247     sample = cx->runtime->propertyRemovals;
5248     {
5249         AutoShapeRooter tvr(cx, shape);
5250         AutoObjectRooter tvr2(cx, pobj);
5251         if (!shape->get(cx, receiver, obj, pobj, vp))
5252             return false;
5253     }
5254
5255     if (pobj->containsSlot(slot) &&
5256         (JS_LIKELY(cx->runtime->propertyRemovals == sample) ||
5257          pobj->nativeContains(*shape))) {
5258         if (!pobj->methodWriteBarrier(cx, *shape, *vp))
5259             return false;
5260         pobj->nativeSetSlot(slot, *vp);
5261     }
5262
5263     return true;
5264 }
5265
5266 JSBool
5267 js_NativeGet(JSContext *cx, JSObject *obj, JSObject *pobj, const Shape *shape, uintN getHow,
5268              Value *vp)
5269 {
5270     return js_NativeGetInline(cx, obj, obj, pobj, shape, getHow, vp);
5271 }
5272
5273 JSBool
5274 js_NativeSet(JSContext *cx, JSObject *obj, const Shape *shape, bool added, bool strict, Value *vp)
5275 {
5276     LeaveTraceIfGlobalObject(cx, obj);
5277
5278     uint32 slot;
5279     int32 sample;
5280
5281     JS_ASSERT(obj->isNative());
5282
5283     slot = shape->slot;
5284     if (slot != SHAPE_INVALID_SLOT) {
5285         JS_ASSERT(obj->containsSlot(slot));
5286
5287         /* If shape has a stub setter, keep obj locked and just store *vp. */
5288         if (shape->hasDefaultSetter()) {
5289             if (!added) {
5290                 AbortRecordingIfUnexpectedGlobalWrite(cx, obj, slot);
5291
5292                 /* FIXME: This should pass *shape, not slot, but see bug 630354. */
5293                 if (!obj->methodWriteBarrier(cx, slot, *vp))
5294                     return false;
5295             }
5296             obj->nativeSetSlot(slot, *vp);
5297             return true;
5298         }
5299     } else {
5300         /*
5301          * Allow API consumers to create shared properties with stub setters.
5302          * Such properties effectively function as data descriptors which are
5303          * not writable, so attempting to set such a property should do nothing
5304          * or throw if we're in strict mode.
5305          */
5306         if (!shape->hasGetterValue() && shape->hasDefaultSetter())
5307             return js_ReportGetterOnlyAssignment(cx);
5308     }
5309
5310     sample = cx->runtime->propertyRemovals;
5311     {
5312         AutoShapeRooter tvr(cx, shape);
5313         if (!shape->set(cx, obj, strict, vp))
5314             return false;
5315
5316         JS_ASSERT_IF(!obj->inDictionaryMode(), shape->slot == slot);
5317         slot = shape->slot;
5318     }
5319
5320     if (obj->containsSlot(slot) &&
5321         (JS_LIKELY(cx->runtime->propertyRemovals == sample) ||
5322          obj->nativeContains(*shape))) {
5323         if (!added) {
5324             AbortRecordingIfUnexpectedGlobalWrite(cx, obj, slot);
5325             if (!obj->methodWriteBarrier(cx, *shape, *vp))
5326                 return false;
5327         }
5328         obj->setSlot(slot, *vp);
5329     }
5330
5331     return true;
5332 }
5333
5334 static JS_ALWAYS_INLINE bool
5335 js_GetPropertyHelperWithShapeInline(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id,
5336                                     uintN getHow, Value *vp,
5337                                     const Shape **shapeOut, JSObject **holderOut)
5338 {
5339     JSObject *aobj, *obj2;
5340     int protoIndex;
5341     JSProperty *prop;
5342     const Shape *shape;
5343
5344     JS_ASSERT_IF(getHow & JSGET_CACHE_RESULT, !JS_ON_TRACE(cx));
5345
5346     *shapeOut = NULL;
5347
5348     /* Convert string indices to integers if appropriate. */
5349     id = js_CheckForStringIndex(id);
5350
5351     aobj = js_GetProtoIfDenseArray(obj);
5352     /* This call site is hot -- use the always-inlined variant of js_LookupPropertyWithFlags(). */
5353     protoIndex = js_LookupPropertyWithFlagsInline(cx, aobj, id, cx->resolveFlags,
5354                                                   &obj2, &prop);
5355     if (protoIndex < 0)
5356         return JS_FALSE;
5357
5358     *holderOut = obj2;
5359
5360     if (!prop) {
5361         vp->setUndefined();
5362
5363         if (!CallJSPropertyOp(cx, obj->getClass()->getProperty, obj, id, vp))
5364             return JS_FALSE;
5365
5366         PCMETER(getHow & JSGET_CACHE_RESULT && JS_PROPERTY_CACHE(cx).nofills++);
5367
5368         /*
5369          * Give a strict warning if foo.bar is evaluated by a script for an
5370          * object foo with no property named 'bar'.
5371          */
5372         jsbytecode *pc;
5373         if (vp->isUndefined() && ((pc = js_GetCurrentBytecodePC(cx)) != NULL)) {
5374             JSOp op;
5375             uintN flags;
5376
5377             op = (JSOp) *pc;
5378             if (op == JSOP_TRAP) {
5379                 JS_ASSERT_NOT_ON_TRACE(cx);
5380                 op = JS_GetTrapOpcode(cx, cx->fp()->script(), pc);
5381             }
5382             if (op == JSOP_GETXPROP) {
5383                 flags = JSREPORT_ERROR;
5384             } else {
5385                 if (!cx->hasStrictOption() ||
5386                     (op != JSOP_GETPROP && op != JSOP_GETELEM) ||
5387                     js_CurrentPCIsInImacro(cx)) {
5388                     return JS_TRUE;
5389                 }
5390
5391                 /*
5392                  * XXX do not warn about missing __iterator__ as the function
5393                  * may be called from JS_GetMethodById. See bug 355145.
5394                  */
5395                 if (JSID_IS_ATOM(id, cx->runtime->atomState.iteratorAtom))
5396                     return JS_TRUE;
5397
5398                 /* Do not warn about tests like (obj[prop] == undefined). */
5399                 if (cx->resolveFlags == JSRESOLVE_INFER) {
5400                     LeaveTrace(cx);
5401                     pc += js_CodeSpec[op].length;
5402                     if (Detecting(cx, pc))
5403                         return JS_TRUE;
5404                 } else if (cx->resolveFlags & JSRESOLVE_DETECTING) {
5405                     return JS_TRUE;
5406                 }
5407
5408                 flags = JSREPORT_WARNING | JSREPORT_STRICT;
5409             }
5410
5411             /* Ok, bad undefined property reference: whine about it. */
5412             if (!js_ReportValueErrorFlags(cx, flags, JSMSG_UNDEFINED_PROP,
5413                                           JSDVG_IGNORE_STACK, IdToValue(id),
5414                                           NULL, NULL, NULL)) {
5415                 return JS_FALSE;
5416             }
5417         }
5418         return JS_TRUE;
5419     }
5420
5421     if (!obj2->isNative()) {
5422         return obj2->isProxy()
5423                ? JSProxy::get(cx, obj2, receiver, id, vp)
5424                : obj2->getProperty(cx, id, vp);
5425     }
5426
5427     shape = (Shape *) prop;
5428     *shapeOut = shape;
5429
5430     if (getHow & JSGET_CACHE_RESULT) {
5431         JS_ASSERT_NOT_ON_TRACE(cx);
5432         JS_PROPERTY_CACHE(cx).fill(cx, aobj, 0, protoIndex, obj2, shape);
5433     }
5434
5435     /* This call site is hot -- use the always-inlined variant of js_NativeGet(). */
5436     if (!js_NativeGetInline(cx, receiver, obj, obj2, shape, getHow, vp))
5437         return JS_FALSE;
5438
5439     return JS_TRUE;
5440 }
5441
5442 bool
5443 js_GetPropertyHelperWithShape(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id,
5444                               uint32 getHow, Value *vp,
5445                               const Shape **shapeOut, JSObject **holderOut)
5446 {
5447     return js_GetPropertyHelperWithShapeInline(cx, obj, receiver, id, getHow, vp,
5448                                                shapeOut, holderOut);
5449 }
5450
5451 static JS_ALWAYS_INLINE JSBool
5452 js_GetPropertyHelperInline(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id,
5453                            uint32 getHow, Value *vp)
5454 {
5455     const Shape *shape;
5456     JSObject *holder;
5457     return js_GetPropertyHelperWithShapeInline(cx, obj, receiver, id, getHow, vp, &shape, &holder);
5458 }
5459
5460 JSBool
5461 js_GetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uint32 getHow, Value *vp)
5462 {
5463     return js_GetPropertyHelperInline(cx, obj, obj, id, getHow, vp);
5464 }
5465
5466 JSBool
5467 js_GetProperty(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id, Value *vp)
5468 {
5469     /* This call site is hot -- use the always-inlined variant of js_GetPropertyHelper(). */
5470     return js_GetPropertyHelperInline(cx, obj, receiver, id, JSGET_METHOD_BARRIER, vp);
5471 }
5472
5473 JSBool
5474 js::GetPropertyDefault(JSContext *cx, JSObject *obj, jsid id, const Value &def, Value *vp)
5475 {
5476     JSProperty *prop;
5477     JSObject *obj2;
5478     if (js_LookupPropertyWithFlags(cx, obj, id, JSRESOLVE_QUALIFIED, &obj2, &prop) < 0)
5479         return false;
5480
5481     if (!prop) {
5482         *vp = def;
5483         return true;
5484     }
5485
5486     return js_GetProperty(cx, obj2, id, vp);
5487 }
5488
5489 JSBool
5490 js_GetMethod(JSContext *cx, JSObject *obj, jsid id, uintN getHow, Value *vp)
5491 {
5492     JSAutoResolveFlags rf(cx, JSRESOLVE_QUALIFIED);
5493
5494     PropertyIdOp op = obj->getOps()->getProperty;
5495     if (!op) {
5496 #if JS_HAS_XML_SUPPORT
5497         JS_ASSERT(!obj->isXML());
5498 #endif
5499         return js_GetPropertyHelper(cx, obj, id, getHow, vp);
5500     }
5501     JS_ASSERT_IF(getHow & JSGET_CACHE_RESULT, obj->isDenseArray());
5502 #if JS_HAS_XML_SUPPORT
5503     if (obj->isXML())
5504         return js_GetXMLMethod(cx, obj, id, vp);
5505 #endif
5506     return op(cx, obj, obj, id, vp);
5507 }
5508
5509 JS_FRIEND_API(bool)
5510 js_CheckUndeclaredVarAssignment(JSContext *cx, JSString *propname)
5511 {
5512     JSStackFrame *const fp = js_GetTopStackFrame(cx);
5513     if (!fp)
5514         return true;
5515
5516     /* If neither cx nor the code is strict, then no check is needed. */
5517     if (!(fp->isScriptFrame() && fp->script()->strictModeCode) &&
5518         !cx->hasStrictOption()) {
5519         return true;
5520     }
5521
5522     JSAutoByteString bytes(cx, propname);
5523     return !!bytes &&
5524            JS_ReportErrorFlagsAndNumber(cx,
5525                                         (JSREPORT_WARNING | JSREPORT_STRICT
5526                                          | JSREPORT_STRICT_MODE_ERROR),
5527                                         js_GetErrorMessage, NULL,
5528                                         JSMSG_UNDECLARED_VAR, bytes.ptr());
5529 }
5530
5531 bool
5532 JSObject::reportReadOnly(JSContext* cx, jsid id, uintN report)
5533 {
5534     return js_ReportValueErrorFlags(cx, report, JSMSG_READ_ONLY,
5535                                     JSDVG_IGNORE_STACK, IdToValue(id), NULL,
5536                                     NULL, NULL);
5537 }
5538
5539 bool
5540 JSObject::reportNotConfigurable(JSContext* cx, jsid id, uintN report)
5541 {
5542     return js_ReportValueErrorFlags(cx, report, JSMSG_CANT_DELETE,
5543                                     JSDVG_IGNORE_STACK, IdToValue(id), NULL,
5544                                     NULL, NULL);
5545 }
5546
5547 bool
5548 JSObject::reportNotExtensible(JSContext *cx, uintN report)
5549 {
5550     return js_ReportValueErrorFlags(cx, report, JSMSG_OBJECT_NOT_EXTENSIBLE,
5551                                     JSDVG_IGNORE_STACK, ObjectValue(*this),
5552                                     NULL, NULL, NULL);
5553 }
5554
5555 JSBool
5556 js_SetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN defineHow,
5557                      Value *vp, JSBool strict)
5558 {
5559     int protoIndex;
5560     JSObject *pobj;
5561     JSProperty *prop;
5562     const Shape *shape;
5563     uintN attrs, flags;
5564     intN shortid;
5565     Class *clasp;
5566     PropertyOp getter;
5567     StrictPropertyOp setter;
5568     bool added;
5569
5570     JS_ASSERT((defineHow &
5571                ~(JSDNP_CACHE_RESULT | JSDNP_SET_METHOD | JSDNP_UNQUALIFIED)) == 0);
5572     if (defineHow & JSDNP_CACHE_RESULT)
5573         JS_ASSERT_NOT_ON_TRACE(cx);
5574
5575     /* Convert string indices to integers if appropriate. */
5576     id = js_CheckForStringIndex(id);
5577
5578     protoIndex = js_LookupPropertyWithFlags(cx, obj, id, cx->resolveFlags,
5579                                             &pobj, &prop);
5580     if (protoIndex < 0)
5581         return JS_FALSE;
5582     if (prop) {
5583         if (!pobj->isNative()) {
5584             if (pobj->isProxy()) {
5585                 AutoPropertyDescriptorRooter pd(cx);
5586                 if (!JSProxy::getPropertyDescriptor(cx, pobj, id, true, &pd))
5587                     return false;
5588
5589                 if (pd.attrs & JSPROP_SHARED)
5590                     return CallSetter(cx, obj, id, pd.setter, pd.attrs, pd.shortid, strict, vp);
5591
5592                 if (pd.attrs & JSPROP_READONLY) {
5593                     if (strict)
5594                         return obj->reportReadOnly(cx, id);
5595                     if (cx->hasStrictOption())
5596                         return obj->reportReadOnly(cx, id, JSREPORT_STRICT | JSREPORT_WARNING);
5597                     return true;
5598                 }
5599             }
5600
5601             prop = NULL;
5602         }
5603     } else {
5604         /* We should never add properties to lexical blocks.  */
5605         JS_ASSERT(!obj->isBlock());
5606
5607         if (!obj->getParent() &&
5608             (defineHow & JSDNP_UNQUALIFIED) &&
5609             !js_CheckUndeclaredVarAssignment(cx, JSID_TO_STRING(id))) {
5610             return JS_FALSE;
5611         }
5612     }
5613     shape = (Shape *) prop;
5614
5615     /*
5616      * Now either shape is null, meaning id was not found in obj or one of its
5617      * prototypes; or shape is non-null, meaning id was found directly in pobj.
5618      */
5619     attrs = JSPROP_ENUMERATE;
5620     flags = 0;
5621     shortid = 0;
5622     clasp = obj->getClass();
5623     getter = clasp->getProperty;
5624     setter = clasp->setProperty;
5625
5626     if (shape) {
5627         /* ES5 8.12.4 [[Put]] step 2. */
5628         if (shape->isAccessorDescriptor()) {
5629             if (shape->hasDefaultSetter())
5630                 return js_ReportGetterOnlyAssignment(cx);
5631         } else {
5632             JS_ASSERT(shape->isDataDescriptor());
5633
5634             if (!shape->writable()) {
5635                 PCMETER((defineHow & JSDNP_CACHE_RESULT) && JS_PROPERTY_CACHE(cx).rofills++);
5636
5637                 /* Error in strict mode code, warn with strict option, otherwise do nothing. */
5638                 if (strict)
5639                     return obj->reportReadOnly(cx, id);
5640                 if (cx->hasStrictOption())
5641                     return obj->reportReadOnly(cx, id, JSREPORT_STRICT | JSREPORT_WARNING);
5642                 return JS_TRUE;
5643             }
5644         }
5645
5646         attrs = shape->attributes();
5647         if (pobj != obj) {
5648             /*
5649              * We found id in a prototype object: prepare to share or shadow.
5650              */
5651             if (!shape->shadowable()) {
5652                 if (defineHow & JSDNP_CACHE_RESULT)
5653                     JS_PROPERTY_CACHE(cx).fill(cx, obj, 0, protoIndex, pobj, shape);
5654
5655                 if (shape->hasDefaultSetter() && !shape->hasGetterValue())
5656                     return JS_TRUE;
5657
5658                 return shape->set(cx, obj, strict, vp);
5659             }
5660
5661             /*
5662              * Preserve attrs except JSPROP_SHARED, getter, and setter when
5663              * shadowing any property that has no slot (is shared). We must
5664              * clear the shared attribute for the shadowing shape so that the
5665              * property in obj that it defines has a slot to retain the value
5666              * being set, in case the setter simply cannot operate on instances
5667              * of obj's class by storing the value in some class-specific
5668              * location.
5669              *
5670              * A subset of slotless shared properties is the set of properties
5671              * with shortids, which must be preserved too. An old API requires
5672              * that the property's getter and setter receive the shortid, not
5673              * id, when they are called on the shadowing property that we are
5674              * about to create in obj.
5675              */
5676             if (!shape->hasSlot()) {
5677                 defineHow &= ~JSDNP_SET_METHOD;
5678                 if (shape->hasShortID()) {
5679                     flags = Shape::HAS_SHORTID;
5680                     shortid = shape->shortid;
5681                 }
5682                 attrs &= ~JSPROP_SHARED;
5683                 getter = shape->getter();
5684                 setter = shape->setter();
5685             } else {
5686                 /* Restore attrs to the ECMA default for new properties. */
5687                 attrs = JSPROP_ENUMERATE;
5688             }
5689
5690             /*
5691              * Forget we found the proto-property now that we've copied any
5692              * needed member values.
5693              */
5694             shape = NULL;
5695         }
5696
5697         JS_ASSERT_IF(shape && shape->isMethod(), pobj->hasMethodBarrier());
5698         JS_ASSERT_IF(shape && shape->isMethod(),
5699                      &pobj->getSlot(shape->slot).toObject() == &shape->methodObject());
5700         if (shape && (defineHow & JSDNP_SET_METHOD)) {
5701             /*
5702              * JSOP_SETMETHOD is assigning to an existing own property. If it
5703              * is an identical method property, do nothing. Otherwise downgrade
5704              * to ordinary assignment. Either way, do not fill the property
5705              * cache, as the interpreter has no fast path for these unusual
5706              * cases.
5707              */
5708             bool identical = shape->isMethod() && &shape->methodObject() == &vp->toObject();
5709             if (!identical) {
5710                 shape = obj->methodShapeChange(cx, *shape);
5711                 if (!shape)
5712                     return false;
5713
5714                 JSObject *funobj = &vp->toObject();
5715                 JSFunction *fun = funobj->getFunctionPrivate();
5716                 if (fun == funobj) {
5717                     funobj = CloneFunctionObject(cx, fun, fun->parent);
5718                     if (!funobj)
5719                         return JS_FALSE;
5720                     vp->setObject(*funobj);
5721                 }
5722             }
5723             return identical || js_NativeSet(cx, obj, shape, false, strict, vp);
5724         }
5725     }
5726
5727     added = false;
5728     if (!shape) {
5729         if (!obj->isExtensible()) {
5730             /* Error in strict mode code, warn with strict option, otherwise do nothing. */
5731             if (strict)
5732                 return obj->reportNotExtensible(cx);
5733             if (cx->hasStrictOption())
5734                 return obj->reportNotExtensible(cx, JSREPORT_STRICT | JSREPORT_WARNING);
5735             return JS_TRUE;
5736         }
5737
5738         /*
5739          * Purge the property cache of now-shadowed id in obj's scope chain.
5740          * Do this early, before locking obj to avoid nesting locks.
5741          */
5742         js_PurgeScopeChain(cx, obj, id);
5743
5744         /* Find or make a property descriptor with the right heritage. */
5745         if (!obj->ensureClassReservedSlots(cx))
5746             return JS_FALSE;
5747
5748         /*
5749          * Check for Object class here to avoid defining a method on a class
5750          * with magic resolve, addProperty, getProperty, etc. hooks.
5751          */
5752         if ((defineHow & JSDNP_SET_METHOD) && obj->canHaveMethodBarrier()) {
5753             JS_ASSERT(IsFunctionObject(*vp));
5754             JS_ASSERT(!(attrs & (JSPROP_GETTER | JSPROP_SETTER)));
5755
5756             JSObject *funobj = &vp->toObject();
5757             JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
5758             if (fun == funobj) {
5759                 flags |= Shape::METHOD;
5760                 getter = CastAsPropertyOp(funobj);
5761             }
5762         }
5763
5764         shape = obj->putProperty(cx, id, getter, setter, SHAPE_INVALID_SLOT,
5765                                  attrs, flags, shortid);
5766         if (!shape)
5767             return JS_FALSE;
5768
5769         if (defineHow & JSDNP_CACHE_RESULT)
5770             TRACE_1(AddProperty, obj);
5771
5772         /*
5773          * Initialize the new property value (passed to setter) to undefined.
5774          * Note that we store before calling addProperty, to match the order
5775          * in js_DefineNativeProperty.
5776          */
5777         if (obj->containsSlot(shape->slot))
5778             obj->nativeSetSlot(shape->slot, UndefinedValue());
5779
5780         /* XXXbe called with obj locked */
5781         if (!CallAddPropertyHook(cx, clasp, obj, shape, vp)) {
5782             obj->removeProperty(cx, id);
5783             return JS_FALSE;
5784         }
5785         added = true;
5786     }
5787
5788     if (defineHow & JSDNP_CACHE_RESULT)
5789         JS_PROPERTY_CACHE(cx).fill(cx, obj, 0, 0, obj, shape, added);
5790
5791     return js_NativeSet(cx, obj, shape, added, strict, vp);
5792
5793 #ifdef JS_TRACER
5794   error: // TRACE_1 jumps here in case of error.
5795     return JS_FALSE;
5796 #endif
5797 }
5798
5799 JSBool
5800 js_SetProperty(JSContext *cx, JSObject *obj, jsid id, Value *vp, JSBool strict)
5801 {
5802     return js_SetPropertyHelper(cx, obj, id, 0, vp, strict);
5803 }
5804
5805 JSBool
5806 js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
5807 {
5808     JSProperty *prop;
5809     if (!js_LookupProperty(cx, obj, id, &obj, &prop))
5810         return false;
5811     if (!prop) {
5812         *attrsp = 0;
5813         return true;
5814     }
5815     if (!obj->isNative())
5816         return obj->getAttributes(cx, id, attrsp);
5817
5818     const Shape *shape = (Shape *)prop;
5819     *attrsp = shape->attributes();
5820     return true;
5821 }
5822
5823 JSBool
5824 js_SetNativeAttributes(JSContext *cx, JSObject *obj, Shape *shape, uintN attrs)
5825 {
5826     JS_ASSERT(obj->isNative());
5827     return !!js_ChangeNativePropertyAttrs(cx, obj, shape, attrs, 0,
5828                                           shape->getter(), shape->setter());
5829 }
5830
5831 JSBool
5832 js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, uintN *attrsp)
5833 {
5834     JSProperty *prop;
5835     if (!js_LookupProperty(cx, obj, id, &obj, &prop))
5836         return false;
5837     if (!prop)
5838         return true;
5839     return obj->isNative()
5840            ? js_SetNativeAttributes(cx, obj, (Shape *) prop, *attrsp)
5841            : obj->setAttributes(cx, id, attrsp);
5842 }
5843
5844 JSBool
5845 js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, Value *rval, JSBool strict)
5846 {
5847     JSObject *proto;
5848     JSProperty *prop;
5849     const Shape *shape;
5850
5851     rval->setBoolean(true);
5852
5853     /* Convert string indices to integers if appropriate. */
5854     id = js_CheckForStringIndex(id);
5855
5856     if (!js_LookupProperty(cx, obj, id, &proto, &prop))
5857         return false;
5858     if (!prop || proto != obj) {
5859         /*
5860          * If the property was found in a native prototype, check whether it's
5861          * shared and permanent.  Such a property stands for direct properties
5862          * in all delegating objects, matching ECMA semantics without bloating
5863          * each delegating object.
5864          */
5865         if (prop && proto->isNative()) {
5866             shape = (Shape *)prop;
5867             if (shape->isSharedPermanent()) {
5868                 if (strict)
5869                     return obj->reportNotConfigurable(cx, id);
5870                 rval->setBoolean(false);
5871                 return true;
5872             }
5873         }
5874
5875         /*
5876          * If no property, or the property comes unshared or impermanent from
5877          * a prototype, call the class's delProperty hook, passing rval as the
5878          * result parameter.
5879          */
5880         return CallJSPropertyOp(cx, obj->getClass()->delProperty, obj, id, rval);
5881     }
5882
5883     shape = (Shape *)prop;
5884     if (!shape->configurable()) {
5885         if (strict)
5886             return obj->reportNotConfigurable(cx, id);
5887         rval->setBoolean(false);
5888         return true;
5889     }
5890
5891     if (!CallJSPropertyOp(cx, obj->getClass()->delProperty, obj, SHAPE_USERID(shape), rval))
5892         return false;
5893
5894     if (obj->containsSlot(shape->slot)) {
5895         const Value &v = obj->nativeGetSlot(shape->slot);
5896         GC_POKE(cx, v);
5897
5898         /*
5899          * Delete is rare enough that we can take the hit of checking for an
5900          * active cloned method function object that must be homed to a callee
5901          * slot on the active stack frame before this delete completes, in case
5902          * someone saved the clone and checks it against foo.caller for a foo
5903          * called from the active method.
5904          *
5905          * We do not check suspended frames. They can't be reached via caller,
5906          * so the only way they could have the method's joined function object
5907          * as callee is through an API abusage. We break any such edge case.
5908          */
5909         if (obj->hasMethodBarrier()) {
5910             JSObject *funobj;
5911
5912             if (IsFunctionObject(v, &funobj)) {
5913                 JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
5914
5915                 if (fun != funobj) {
5916                     for (JSStackFrame *fp = cx->maybefp(); fp; fp = fp->prev()) {
5917                         if (fp->isFunctionFrame() &&
5918                             &fp->callee() == &fun->compiledFunObj() &&
5919                             fp->thisValue().isObject())
5920                         {
5921                             JSObject *tmp = &fp->thisValue().toObject();
5922                             do {
5923                                 if (tmp == obj) {
5924                                     fp->calleeValue().setObject(*funobj);
5925                                     break;
5926                                 }
5927                             } while ((tmp = tmp->getProto()) != NULL);
5928                         }
5929                     }
5930                 }
5931             }
5932         }
5933     }
5934
5935     return obj->removeProperty(cx, id) && js_SuppressDeletedProperty(cx, obj, id);
5936 }
5937
5938 namespace js {
5939
5940 JSObject *
5941 HasNativeMethod(JSObject *obj, jsid methodid, Native native)
5942 {
5943     const Shape *shape = obj->nativeLookup(methodid);
5944     if (!shape || !shape->hasDefaultGetter() || !obj->containsSlot(shape->slot))
5945         return NULL;
5946
5947     const Value &fval = obj->nativeGetSlot(shape->slot);
5948     JSObject *funobj;
5949     if (!IsFunctionObject(fval, &funobj) || funobj->getFunctionPrivate()->maybeNative() != native)
5950         return NULL;
5951
5952     return funobj;
5953 }
5954
5955 bool
5956 DefaultValue(JSContext *cx, JSObject *obj, JSType hint, Value *vp)
5957 {
5958     JS_ASSERT(hint != JSTYPE_OBJECT && hint != JSTYPE_FUNCTION);
5959
5960     Value v = ObjectValue(*obj);
5961     if (hint == JSTYPE_STRING) {
5962         /* Optimize (new String(...)).toString(). */
5963         if (obj->getClass() == &js_StringClass &&
5964             ClassMethodIsNative(cx, obj,
5965                                  &js_StringClass,
5966                                  ATOM_TO_JSID(cx->runtime->atomState.toStringAtom),
5967                                  js_str_toString)) {
5968             *vp = obj->getPrimitiveThis();
5969             return true;
5970         }
5971
5972         if (!js_TryMethod(cx, obj, cx->runtime->atomState.toStringAtom, 0, NULL, &v))
5973             return false;
5974         if (!v.isPrimitive()) {
5975             if (!obj->getClass()->convert(cx, obj, hint, &v))
5976                 return false;
5977         }
5978     } else {
5979         /* Optimize (new String(...)).valueOf(). */
5980         Class *clasp = obj->getClass();
5981         if ((clasp == &js_StringClass &&
5982              ClassMethodIsNative(cx, obj, &js_StringClass,
5983                                  ATOM_TO_JSID(cx->runtime->atomState.valueOfAtom),
5984                                  js_str_toString)) ||
5985             (clasp == &js_NumberClass &&
5986              ClassMethodIsNative(cx, obj, &js_NumberClass,
5987                                  ATOM_TO_JSID(cx->runtime->atomState.valueOfAtom),
5988                                  js_num_valueOf))) {
5989             *vp = obj->getPrimitiveThis();
5990             return true;
5991         }
5992
5993         if (!obj->getClass()->convert(cx, obj, hint, &v))
5994             return false;
5995         if (v.isObject()) {
5996             JS_ASSERT(hint != TypeOfValue(cx, v));
5997             if (!js_TryMethod(cx, obj, cx->runtime->atomState.toStringAtom, 0, NULL, &v))
5998                 return false;
5999         }
6000     }
6001     if (v.isObject()) {
6002         /* Avoid recursive death when decompiling in js_ReportValueError. */
6003         JSString *str;
6004         if (hint == JSTYPE_STRING) {
6005             str = JS_InternString(cx, obj->getClass()->name);
6006             if (!str)
6007                 return false;
6008         } else {
6009             str = NULL;
6010         }
6011         vp->setObject(*obj);
6012         js_ReportValueError2(cx, JSMSG_CANT_CONVERT_TO,
6013                              JSDVG_SEARCH_STACK, *vp, str,
6014                              (hint == JSTYPE_VOID)
6015                              ? "primitive type"
6016                              : JS_TYPE_STR(hint));
6017         return false;
6018     }
6019     *vp = v;
6020     return true;
6021 }
6022
6023 } /* namespace js */
6024
6025 JS_FRIEND_API(JSBool)
6026 js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op, Value *statep, jsid *idp)
6027 {
6028     /* If the class has a custom JSCLASS_NEW_ENUMERATE hook, call it. */
6029     Class *clasp = obj->getClass();
6030     JSEnumerateOp enumerate = clasp->enumerate;
6031     if (clasp->flags & JSCLASS_NEW_ENUMERATE) {
6032         JS_ASSERT(enumerate != JS_EnumerateStub);
6033         return ((NewEnumerateOp) enumerate)(cx, obj, enum_op, statep, idp);
6034     }
6035
6036     if (!enumerate(cx, obj))
6037         return false;
6038
6039     /* Tell InitNativeIterator to treat us like a native object. */
6040     JS_ASSERT(enum_op == JSENUMERATE_INIT || enum_op == JSENUMERATE_INIT_ALL);
6041     statep->setMagic(JS_NATIVE_ENUMERATE);
6042     return true;
6043 }
6044
6045 namespace js {
6046
6047 JSBool
6048 CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
6049             Value *vp, uintN *attrsp)
6050 {
6051     JSBool writing;
6052     JSObject *pobj;
6053     JSProperty *prop;
6054     Class *clasp;
6055     const Shape *shape;
6056     JSSecurityCallbacks *callbacks;
6057     CheckAccessOp check;
6058
6059     while (JS_UNLIKELY(obj->getClass() == &js_WithClass))
6060         obj = obj->getProto();
6061
6062     writing = (mode & JSACC_WRITE) != 0;
6063     switch (mode & JSACC_TYPEMASK) {
6064       case JSACC_PROTO:
6065         pobj = obj;
6066         if (!writing)
6067             vp->setObjectOrNull(obj->getProto());
6068         *attrsp = JSPROP_PERMANENT;
6069         break;
6070
6071       case JSACC_PARENT:
6072         JS_ASSERT(!writing);
6073         pobj = obj;
6074         vp->setObject(*obj->getParent());
6075         *attrsp = JSPROP_READONLY | JSPROP_PERMANENT;
6076         break;
6077
6078       default:
6079         if (!obj->lookupProperty(cx, id, &pobj, &prop))
6080             return JS_FALSE;
6081         if (!prop) {
6082             if (!writing)
6083                 vp->setUndefined();
6084             *attrsp = 0;
6085             pobj = obj;
6086             break;
6087         }
6088
6089         if (!pobj->isNative()) {
6090             if (!writing) {
6091                     vp->setUndefined();
6092                 *attrsp = 0;
6093             }
6094             break;
6095         }
6096
6097         shape = (Shape *)prop;
6098         *attrsp = shape->attributes();
6099         if (!writing) {
6100             if (pobj->containsSlot(shape->slot))
6101                 *vp = pobj->nativeGetSlot(shape->slot);
6102             else
6103                 vp->setUndefined();
6104         }
6105     }
6106
6107     /*
6108      * If obj's class has a stub (null) checkAccess hook, use the per-runtime
6109      * checkObjectAccess callback, if configured.
6110      *
6111      * We don't want to require all classes to supply a checkAccess hook; we
6112      * need that hook only for certain classes used when precompiling scripts
6113      * and functions ("brutal sharing").  But for general safety of built-in
6114      * magic properties like __proto__, we route all access checks, even for
6115      * classes that stub out checkAccess, through the global checkObjectAccess
6116      * hook.  This covers precompilation-based sharing and (possibly
6117      * unintended) runtime sharing across trust boundaries.
6118      */
6119     clasp = pobj->getClass();
6120     check = clasp->checkAccess;
6121     if (!check) {
6122         callbacks = JS_GetSecurityCallbacks(cx);
6123         check = callbacks ? Valueify(callbacks->checkObjectAccess) : NULL;
6124     }
6125     return !check || check(cx, pobj, id, mode, vp);
6126 }
6127
6128 }
6129
6130 JSType
6131 js_TypeOf(JSContext *cx, JSObject *obj)
6132 {
6133     /*
6134      * ECMA 262, 11.4.3 says that any native object that implements
6135      * [[Call]] should be of type "function". However, RegExp is of
6136      * type "object", not "function", for Web compatibility.
6137      */
6138     if (obj->isCallable()) {
6139         return (obj->getClass() != &js_RegExpClass)
6140                ? JSTYPE_FUNCTION
6141                : JSTYPE_OBJECT;
6142     }
6143
6144     return JSTYPE_OBJECT;
6145 }
6146
6147 bool
6148 js_IsDelegate(JSContext *cx, JSObject *obj, const Value &v)
6149 {
6150     if (v.isPrimitive())
6151         return false;
6152     JSObject *obj2 = &v.toObject();
6153     while ((obj2 = obj2->getProto()) != NULL) {
6154         if (obj2 == obj)
6155             return true;
6156     }
6157     return false;
6158 }
6159
6160 bool
6161 js::FindClassPrototype(JSContext *cx, JSObject *scopeobj, JSProtoKey protoKey,
6162                        JSObject **protop, Class *clasp)
6163 {
6164     Value v;
6165     if (!js_FindClassObject(cx, scopeobj, protoKey, &v, clasp))
6166         return false;
6167
6168     if (IsFunctionObject(v)) {
6169         JSObject *ctor = &v.toObject();
6170         if (!ctor->getProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom), &v))
6171             return false;
6172     }
6173
6174     *protop = v.isObject() ? &v.toObject() : NULL;
6175     return true;
6176 }
6177
6178 /*
6179  * The first part of this function has been hand-expanded and optimized into
6180  * NewBuiltinClassInstance in jsobjinlines.h.
6181  */
6182 JSBool
6183 js_GetClassPrototype(JSContext *cx, JSObject *scopeobj, JSProtoKey protoKey,
6184                      JSObject **protop, Class *clasp)
6185 {
6186     VOUCH_DOES_NOT_REQUIRE_STACK();
6187     JS_ASSERT(JSProto_Null <= protoKey);
6188     JS_ASSERT(protoKey < JSProto_LIMIT);
6189
6190     if (protoKey != JSProto_Null) {
6191         if (!scopeobj) {
6192             if (cx->hasfp())
6193                 scopeobj = &cx->fp()->scopeChain();
6194             if (!scopeobj) {
6195                 scopeobj = cx->globalObject;
6196                 if (!scopeobj) {
6197                     *protop = NULL;
6198                     return true;
6199                 }
6200             }
6201         }
6202         scopeobj = scopeobj->getGlobal();
6203         if (scopeobj->isGlobal()) {
6204             const Value &v = scopeobj->getReservedSlot(JSProto_LIMIT + protoKey);
6205             if (v.isObject()) {
6206                 *protop = &v.toObject();
6207                 return true;
6208             }
6209         }
6210     }
6211
6212     return FindClassPrototype(cx, scopeobj, protoKey, protop, clasp);
6213 }
6214
6215 JSBool
6216 js_SetClassPrototype(JSContext *cx, JSObject *ctor, JSObject *proto, uintN attrs)
6217 {
6218     /*
6219      * Use the given attributes for the prototype property of the constructor,
6220      * as user-defined constructors have a DontDelete prototype (which may be
6221      * reset), while native or "system" constructors have DontEnum | ReadOnly |
6222      * DontDelete.
6223      */
6224     if (!ctor->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.classPrototypeAtom),
6225                               ObjectOrNullValue(proto), PropertyStub, StrictPropertyStub, attrs)) {
6226         return JS_FALSE;
6227     }
6228
6229     /*
6230      * ECMA says that Object.prototype.constructor, or f.prototype.constructor
6231      * for a user-defined function f, is DontEnum.
6232      */
6233     return proto->defineProperty(cx, ATOM_TO_JSID(cx->runtime->atomState.constructorAtom),
6234                                  ObjectOrNullValue(ctor), PropertyStub, StrictPropertyStub, 0);
6235 }
6236
6237 JSObject *
6238 PrimitiveToObject(JSContext *cx, const Value &v)
6239 {
6240     JS_ASSERT(v.isPrimitive());
6241
6242     Class *clasp;
6243     if (v.isNumber()) {
6244         clasp = &js_NumberClass;
6245     } else if (v.isString()) {
6246         clasp = &js_StringClass;
6247     } else {
6248         JS_ASSERT(v.isBoolean());
6249         clasp = &js_BooleanClass;
6250     }
6251
6252     JSObject *obj = NewBuiltinClassInstance(cx, clasp);
6253     if (!obj)
6254         return NULL;
6255
6256     obj->setPrimitiveThis(v);
6257     return obj;
6258 }
6259
6260 JSBool
6261 js_PrimitiveToObject(JSContext *cx, Value *vp)
6262 {
6263     JSObject *obj = PrimitiveToObject(cx, *vp);
6264     if (!obj)
6265         return false;
6266
6267     vp->setObject(*obj);
6268     return true;
6269 }
6270
6271 JSBool
6272 js_ValueToObjectOrNull(JSContext *cx, const Value &v, JSObject **objp)
6273 {
6274     JSObject *obj;
6275
6276     if (v.isObjectOrNull()) {
6277         obj = v.toObjectOrNull();
6278     } else if (v.isUndefined()) {
6279         obj = NULL;
6280     } else {
6281         obj = PrimitiveToObject(cx, v);
6282         if (!obj)
6283             return false;
6284     }
6285     *objp = obj;
6286     return true;
6287 }
6288
6289 namespace js {
6290
6291 /* Callers must handle the already-object case . */
6292 JSObject *
6293 ToObjectSlow(JSContext *cx, Value *vp)
6294 {
6295     JS_ASSERT(!vp->isMagic());
6296     JS_ASSERT(!vp->isObject());
6297
6298     if (vp->isNullOrUndefined()) {
6299         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_CANT_CONVERT_TO,
6300                             vp->isNull() ? "null" : "undefined", "object");
6301         return NULL;
6302     }
6303
6304     JSObject *obj = PrimitiveToObject(cx, *vp);
6305     if (obj)
6306         vp->setObject(*obj);
6307     return obj;
6308 }
6309
6310 }
6311
6312 JSObject *
6313 js_ValueToNonNullObject(JSContext *cx, const Value &v)
6314 {
6315     JSObject *obj;
6316
6317     if (!js_ValueToObjectOrNull(cx, v, &obj))
6318         return NULL;
6319     if (!obj)
6320         js_ReportIsNullOrUndefined(cx, JSDVG_SEARCH_STACK, v, NULL);
6321     return obj;
6322 }
6323
6324 JSBool
6325 js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, Value *rval)
6326 {
6327     Value argv[1];
6328
6329     argv[0].setString(ATOM_TO_STRING(cx->runtime->atomState.typeAtoms[type]));
6330     return js_TryMethod(cx, obj, cx->runtime->atomState.valueOfAtom,
6331                         1, argv, rval);
6332 }
6333
6334 JSBool
6335 js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
6336              uintN argc, Value *argv, Value *rval)
6337 {
6338     JS_CHECK_RECURSION(cx, return JS_FALSE);
6339
6340     /*
6341      * Report failure only if an appropriate method was found, and calling it
6342      * returned failure.  We propagate failure in this case to make exceptions
6343      * behave properly.
6344      */
6345     JSErrorReporter older = JS_SetErrorReporter(cx, NULL);
6346     jsid id = ATOM_TO_JSID(atom);
6347     Value fval;
6348     JSBool ok = js_GetMethod(cx, obj, id, JSGET_NO_METHOD_BARRIER, &fval);
6349     JS_SetErrorReporter(cx, older);
6350     if (!ok)
6351         return false;
6352
6353     if (fval.isPrimitive())
6354         return JS_TRUE;
6355     return ExternalInvoke(cx, ObjectValue(*obj), fval, argc, argv, rval);
6356 }
6357
6358 #if JS_HAS_XDR
6359
6360 JSBool
6361 js_XDRObject(JSXDRState *xdr, JSObject **objp)
6362 {
6363     JSContext *cx;
6364     JSAtom *atom;
6365     Class *clasp;
6366     uint32 classId, classDef;
6367     JSProtoKey protoKey;
6368     JSObject *proto;
6369
6370     cx = xdr->cx;
6371     atom = NULL;
6372     if (xdr->mode == JSXDR_ENCODE) {
6373         clasp = (*objp)->getClass();
6374         classId = JS_XDRFindClassIdByName(xdr, clasp->name);
6375         classDef = !classId;
6376         if (classDef) {
6377             if (!JS_XDRRegisterClass(xdr, Jsvalify(clasp), &classId))
6378                 return JS_FALSE;
6379             protoKey = JSCLASS_CACHED_PROTO_KEY(clasp);
6380             if (protoKey != JSProto_Null) {
6381                 classDef |= (protoKey << 1);
6382             } else {
6383                 atom = js_Atomize(cx, clasp->name, strlen(clasp->name), 0);
6384                 if (!atom)
6385                     return JS_FALSE;
6386             }
6387         }
6388     } else {
6389         clasp = NULL;           /* quell GCC overwarning */
6390         classDef = 0;
6391     }
6392
6393     /*
6394      * XDR a flag word, which could be 0 for a class use, in which case no
6395      * name follows, only the id in xdr's class registry; 1 for a class def,
6396      * in which case the flag word is followed by the class name transferred
6397      * from or to atom; or a value greater than 1, an odd number that when
6398      * divided by two yields the JSProtoKey for class.  In the last case, as
6399      * in the 0 classDef case, no name is transferred via atom.
6400      */
6401     if (!JS_XDRUint32(xdr, &classDef))
6402         return JS_FALSE;
6403     if (classDef == 1 && !js_XDRAtom(xdr, &atom))
6404         return JS_FALSE;
6405
6406     if (!JS_XDRUint32(xdr, &classId))
6407         return JS_FALSE;
6408
6409     if (xdr->mode == JSXDR_DECODE) {
6410         if (classDef) {
6411             /* NB: we know that JSProto_Null is 0 here, for backward compat. */
6412             protoKey = (JSProtoKey) (classDef >> 1);
6413             if (!js_GetClassPrototype(cx, NULL, protoKey, &proto, clasp))
6414                 return JS_FALSE;
6415             clasp = proto->getClass();
6416             if (!JS_XDRRegisterClass(xdr, Jsvalify(clasp), &classId))
6417                 return JS_FALSE;
6418         } else {
6419             clasp = Valueify(JS_XDRFindClassById(xdr, classId));
6420             if (!clasp) {
6421                 char numBuf[12];
6422                 JS_snprintf(numBuf, sizeof numBuf, "%ld", (long)classId);
6423                 JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
6424                                      JSMSG_CANT_FIND_CLASS, numBuf);
6425                 return JS_FALSE;
6426             }
6427         }
6428     }
6429
6430     if (!clasp->xdrObject) {
6431         JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
6432                              JSMSG_CANT_XDR_CLASS, clasp->name);
6433         return JS_FALSE;
6434     }
6435     return clasp->xdrObject(xdr, objp);
6436 }
6437
6438 #endif /* JS_HAS_XDR */
6439
6440 #ifdef JS_DUMP_SCOPE_METERS
6441
6442 #include <stdio.h>
6443
6444 JSBasicStats js_entry_count_bs = JS_INIT_STATIC_BASIC_STATS;
6445
6446 static void
6447 MeterEntryCount(uintN count)
6448 {
6449     JS_BASIC_STATS_ACCUM(&js_entry_count_bs, count);
6450 }
6451
6452 void
6453 js_DumpScopeMeters(JSRuntime *rt)
6454 {
6455     static FILE *logfp;
6456     if (!logfp)
6457         logfp = fopen("/tmp/scope.stats", "a");
6458
6459     {
6460         double mean, sigma;
6461
6462         mean = JS_MeanAndStdDevBS(&js_entry_count_bs, &sigma);
6463
6464         fprintf(logfp, "scopes %u entries %g mean %g sigma %g max %u",
6465                 js_entry_count_bs.num, js_entry_count_bs.sum, mean, sigma,
6466                 js_entry_count_bs.max);
6467     }
6468
6469     JS_DumpHistogram(&js_entry_count_bs, logfp);
6470     JS_BASIC_STATS_INIT(&js_entry_count_bs);
6471     fflush(logfp);
6472 }
6473 #endif
6474
6475 #ifdef DEBUG
6476 void
6477 js_PrintObjectSlotName(JSTracer *trc, char *buf, size_t bufsize)
6478 {
6479     JS_ASSERT(trc->debugPrinter == js_PrintObjectSlotName);
6480
6481     JSObject *obj = (JSObject *)trc->debugPrintArg;
6482     uint32 slot = (uint32)trc->debugPrintIndex;
6483
6484     const Shape *shape;
6485     if (obj->isNative()) {
6486         shape = obj->lastProperty();
6487         while (shape->previous() && shape->slot != slot)
6488             shape = shape->previous();
6489         if (shape->slot != slot)
6490             shape = NULL;
6491     } else {
6492         shape = NULL;
6493     }
6494
6495     if (!shape) {
6496         const char *slotname = NULL;
6497         if (obj->isGlobal()) {
6498 #define JS_PROTO(name,code,init)                                              \
6499     if ((code) == slot) { slotname = js_##name##_str; goto found; }
6500 #include "jsproto.tbl"
6501 #undef JS_PROTO
6502         }
6503       found:
6504         if (slotname)
6505             JS_snprintf(buf, bufsize, "CLASS_OBJECT(%s)", slotname);
6506         else
6507             JS_snprintf(buf, bufsize, "**UNKNOWN SLOT %ld**", (long)slot);
6508     } else {
6509         jsid id = shape->id;
6510         if (JSID_IS_INT(id)) {
6511             JS_snprintf(buf, bufsize, "%ld", (long)JSID_TO_INT(id));
6512         } else if (JSID_IS_ATOM(id)) {
6513             PutEscapedString(buf, bufsize, JSID_TO_ATOM(id), 0);
6514         } else {
6515             JS_snprintf(buf, bufsize, "**FINALIZED ATOM KEY**");
6516         }
6517     }
6518 }
6519 #endif
6520
6521 void
6522 js_TraceObject(JSTracer *trc, JSObject *obj)
6523 {
6524     JS_ASSERT(obj->isNative());
6525
6526     JSContext *cx = trc->context;
6527     if (obj->hasSlotsArray() && !obj->nativeEmpty() && IS_GC_MARKING_TRACER(trc)) {
6528         /*
6529          * Trim overlong dslots allocations from the GC, to avoid thrashing in
6530          * case of delete-happy code that settles down at a given population.
6531          * The !obj->nativeEmpty() guard above is due to the bug described by
6532          * the FIXME comment below.
6533          */
6534         size_t slots = obj->slotSpan();
6535         if (obj->numSlots() != slots)
6536             obj->shrinkSlots(cx, slots);
6537     }
6538
6539 #ifdef JS_DUMP_SCOPE_METERS
6540     MeterEntryCount(obj->propertyCount);
6541 #endif
6542
6543     obj->trace(trc);
6544
6545     if (!JS_CLIST_IS_EMPTY(&cx->runtime->watchPointList))
6546         js_TraceWatchPoints(trc, obj);
6547
6548     /* No one runs while the GC is running, so we can use LOCKED_... here. */
6549     Class *clasp = obj->getClass();
6550     if (clasp->mark) {
6551         if (clasp->flags & JSCLASS_MARK_IS_TRACE)
6552             ((JSTraceOp) clasp->mark)(trc, obj);
6553         else if (IS_GC_MARKING_TRACER(trc))
6554             (void) clasp->mark(cx, obj, trc);
6555     }
6556     if (clasp->flags & JSCLASS_IS_GLOBAL) {
6557         JSCompartment *compartment = obj->getCompartment();
6558         compartment->mark(trc);
6559     }
6560
6561     /*
6562      * NB: clasp->mark could mutate something (which would be a bug, but we are
6563      * defensive), so don't hoist this above calling clasp->mark.
6564      */
6565     uint32 nslots = Min(obj->numSlots(), obj->slotSpan());
6566     for (uint32 i = 0; i != nslots; ++i) {
6567         const Value &v = obj->getSlot(i);
6568         JS_SET_TRACING_DETAILS(trc, js_PrintObjectSlotName, obj, i);
6569         MarkValueRaw(trc, v);
6570     }
6571 }
6572
6573 void
6574 js_ClearNative(JSContext *cx, JSObject *obj)
6575 {
6576     /*
6577      * Clear obj of all obj's properties. FIXME: we do not clear reserved slots
6578      * lying below JSSLOT_FREE(clasp). JS_ClearScope does that.
6579      */
6580     if (!obj->nativeEmpty()) {
6581         /* Now that we're done using real properties, clear obj. */
6582         obj->clear(cx);
6583
6584         /* Clear slot values since obj->clear reset our shape to empty. */
6585         uint32 freeslot = JSSLOT_FREE(obj->getClass());
6586         uint32 n = obj->numSlots();
6587         for (uint32 i = freeslot; i < n; ++i)
6588             obj->setSlot(i, UndefinedValue());
6589     }
6590 }
6591
6592 bool
6593 js_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 slot, Value *vp)
6594 {
6595     if (!obj->isNative()) {
6596         vp->setUndefined();
6597         return true;
6598     }
6599
6600     if (slot < obj->numSlots())
6601         *vp = obj->getSlot(slot);
6602     else
6603         vp->setUndefined();
6604     return true;
6605 }
6606
6607 bool
6608 js_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 slot, const Value &v)
6609 {
6610     if (!obj->isNative())
6611         return true;
6612
6613     Class *clasp = obj->getClass();
6614
6615     if (slot >= obj->numSlots()) {
6616         uint32 nslots = JSSLOT_FREE(clasp);
6617         JS_ASSERT(slot < nslots);
6618         if (!obj->allocSlots(cx, nslots))
6619             return false;
6620     }
6621
6622     obj->setSlot(slot, v);
6623     GC_POKE(cx, JS_NULL);
6624     return true;
6625 }
6626
6627 JSObject *
6628 JSObject::getGlobal() const
6629 {
6630     JSObject *obj = const_cast<JSObject *>(this);
6631     while (JSObject *parent = obj->getParent())
6632         obj = parent;
6633     return obj;
6634 }
6635
6636 JSBool
6637 js_ReportGetterOnlyAssignment(JSContext *cx)
6638 {
6639     return JS_ReportErrorFlagsAndNumber(cx,
6640                                         JSREPORT_WARNING | JSREPORT_STRICT |
6641                                         JSREPORT_STRICT_MODE_ERROR,
6642                                         js_GetErrorMessage, NULL,
6643                                         JSMSG_GETTER_ONLY);
6644 }
6645
6646 JS_FRIEND_API(JSBool)
6647 js_GetterOnlyPropertyStub(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
6648 {
6649     JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL, JSMSG_GETTER_ONLY);
6650     return JS_FALSE;
6651 }
6652
6653 #ifdef DEBUG
6654
6655 /*
6656  * Routines to print out values during debugging.  These are FRIEND_API to help
6657  * the debugger find them and to support temporarily hacking js_Dump* calls
6658  * into other code.
6659  */
6660
6661 void
6662 dumpChars(const jschar *s, size_t n)
6663 {
6664     size_t i;
6665
6666     if (n == (size_t) -1) {
6667         while (s[++n]) ;
6668     }
6669
6670     fputc('"', stderr);
6671     for (i = 0; i < n; i++) {
6672         if (s[i] == '\n')
6673             fprintf(stderr, "\\n");
6674         else if (s[i] == '\t')
6675             fprintf(stderr, "\\t");
6676         else if (s[i] >= 32 && s[i] < 127)
6677             fputc(s[i], stderr);
6678         else if (s[i] <= 255)
6679             fprintf(stderr, "\\x%02x", (unsigned int) s[i]);
6680         else
6681             fprintf(stderr, "\\u%04x", (unsigned int) s[i]);
6682     }
6683     fputc('"', stderr);
6684 }
6685
6686 JS_FRIEND_API(void)
6687 js_DumpChars(const jschar *s, size_t n)
6688 {
6689     fprintf(stderr, "jschar * (%p) = ", (void *) s);
6690     dumpChars(s, n);
6691     fputc('\n', stderr);
6692 }
6693
6694 void
6695 dumpString(JSString *str)
6696 {
6697     if (const jschar *chars = str->getChars(NULL))
6698         dumpChars(chars, str->length());
6699     else
6700         fprintf(stderr, "(oom in dumpString)");
6701 }
6702
6703 JS_FRIEND_API(void)
6704 js_DumpString(JSString *str)
6705 {
6706     if (const jschar *chars = str->getChars(NULL)) {
6707         fprintf(stderr, "JSString* (%p) = jschar * (%p) = ",
6708                 (void *) str, (void *) chars);
6709         dumpString(str);
6710     } else {
6711         fprintf(stderr, "(oom in JS_DumpString)");
6712     }
6713     fputc('\n', stderr);
6714 }
6715
6716 JS_FRIEND_API(void)
6717 js_DumpAtom(JSAtom *atom)
6718 {
6719     fprintf(stderr, "JSAtom* (%p) = ", (void *) atom);
6720     js_DumpString(ATOM_TO_STRING(atom));
6721 }
6722
6723 void
6724 dumpValue(const Value &v)
6725 {
6726     if (v.isNull())
6727         fprintf(stderr, "null");
6728     else if (v.isUndefined())
6729         fprintf(stderr, "undefined");
6730     else if (v.isInt32())
6731         fprintf(stderr, "%d", v.toInt32());
6732     else if (v.isDouble())
6733         fprintf(stderr, "%g", v.toDouble());
6734     else if (v.isString())
6735         dumpString(v.toString());
6736     else if (v.isObject() && v.toObject().isFunction()) {
6737         JSObject *funobj = &v.toObject();
6738         JSFunction *fun = GET_FUNCTION_PRIVATE(cx, funobj);
6739         if (fun->atom) {
6740             fputs("<function ", stderr);
6741             FileEscapedString(stderr, ATOM_TO_STRING(fun->atom), 0);
6742         } else {
6743             fputs("<unnamed function", stderr);
6744         }
6745         if (fun->isInterpreted()) {
6746             JSScript *script = fun->script();
6747             fprintf(stderr, " (%s:%u)",
6748                     script->filename ? script->filename : "", script->lineno);
6749         }
6750         fprintf(stderr, " at %p (JSFunction at %p)>", (void *) funobj, (void *) fun);
6751     } else if (v.isObject()) {
6752         JSObject *obj = &v.toObject();
6753         Class *clasp = obj->getClass();
6754         fprintf(stderr, "<%s%s at %p>",
6755                 clasp->name,
6756                 (clasp == &js_ObjectClass) ? "" : " object",
6757                 (void *) obj);
6758     } else if (v.isBoolean()) {
6759         if (v.toBoolean())
6760             fprintf(stderr, "true");
6761         else
6762             fprintf(stderr, "false");
6763     } else if (v.isMagic()) {
6764         fprintf(stderr, "<invalid");
6765 #ifdef DEBUG
6766         switch (v.whyMagic()) {
6767           case JS_ARRAY_HOLE:        fprintf(stderr, " array hole");         break;
6768           case JS_ARGS_HOLE:         fprintf(stderr, " args hole");          break;
6769           case JS_NATIVE_ENUMERATE:  fprintf(stderr, " native enumeration"); break;
6770           case JS_NO_ITER_VALUE:     fprintf(stderr, " no iter value");      break;
6771           case JS_GENERATOR_CLOSING: fprintf(stderr, " generator closing");  break;
6772           default:                   fprintf(stderr, " ?!");                 break;
6773         }
6774 #endif
6775         fprintf(stderr, ">");
6776     } else {
6777         fprintf(stderr, "unexpected value");
6778     }
6779 }
6780
6781 JS_FRIEND_API(void)
6782 js_DumpValue(const Value &val)
6783 {
6784     dumpValue(val);
6785     fputc('\n', stderr);
6786 }
6787
6788 JS_FRIEND_API(void)
6789 js_DumpId(jsid id)
6790 {
6791     fprintf(stderr, "jsid %p = ", (void *) JSID_BITS(id));
6792     dumpValue(IdToValue(id));
6793     fputc('\n', stderr);
6794 }
6795
6796 static void
6797 DumpProperty(JSObject *obj, const Shape &shape)
6798 {
6799     jsid id = shape.id;
6800     uint8 attrs = shape.attributes();
6801
6802     fprintf(stderr, "    ((Shape *) %p) ", (void *) &shape);
6803     if (attrs & JSPROP_ENUMERATE) fprintf(stderr, "enumerate ");
6804     if (attrs & JSPROP_READONLY) fprintf(stderr, "readonly ");
6805     if (attrs & JSPROP_PERMANENT) fprintf(stderr, "permanent ");
6806     if (attrs & JSPROP_SHARED) fprintf(stderr, "shared ");
6807     if (shape.isAlias()) fprintf(stderr, "alias ");
6808     if (shape.isMethod()) fprintf(stderr, "method=%p ", (void *) &shape.methodObject());
6809
6810     if (shape.hasGetterValue())
6811         fprintf(stderr, "getterValue=%p ", (void *) shape.getterObject());
6812     else if (!shape.hasDefaultGetter())
6813         fprintf(stderr, "getterOp=%p ", JS_FUNC_TO_DATA_PTR(void *, shape.getterOp()));
6814
6815     if (shape.hasSetterValue())
6816         fprintf(stderr, "setterValue=%p ", (void *) shape.setterObject());
6817     else if (shape.setterOp() == js_watch_set)
6818         fprintf(stderr, "setterOp=js_watch_set ");
6819     else if (!shape.hasDefaultSetter())
6820         fprintf(stderr, "setterOp=%p ", JS_FUNC_TO_DATA_PTR(void *, shape.setterOp()));
6821
6822     if (JSID_IS_ATOM(id))
6823         dumpString(JSID_TO_STRING(id));
6824     else if (JSID_IS_INT(id))
6825         fprintf(stderr, "%d", (int) JSID_TO_INT(id));
6826     else
6827         fprintf(stderr, "unknown jsid %p", (void *) JSID_BITS(id));
6828     fprintf(stderr, ": slot %d", shape.slot);
6829     if (obj->containsSlot(shape.slot)) {
6830         fprintf(stderr, " = ");
6831         dumpValue(obj->getSlot(shape.slot));
6832     } else if (shape.slot != SHAPE_INVALID_SLOT) {
6833         fprintf(stderr, " (INVALID!)");
6834     }
6835     fprintf(stderr, "\n");
6836 }
6837
6838 JS_FRIEND_API(void)
6839 js_DumpObject(JSObject *obj)
6840 {
6841     fprintf(stderr, "object %p\n", (void *) obj);
6842     Class *clasp = obj->getClass();
6843     fprintf(stderr, "class %p %s\n", (void *)clasp, clasp->name);
6844
6845     fprintf(stderr, "flags:");
6846     uint32 flags = obj->flags;
6847     if (flags & JSObject::DELEGATE) fprintf(stderr, " delegate");
6848     if (flags & JSObject::SYSTEM) fprintf(stderr, " system");
6849     if (flags & JSObject::NOT_EXTENSIBLE) fprintf(stderr, " not_extensible");
6850     if (flags & JSObject::BRANDED) fprintf(stderr, " branded");
6851     if (flags & JSObject::GENERIC) fprintf(stderr, " generic");
6852     if (flags & JSObject::METHOD_BARRIER) fprintf(stderr, " method_barrier");
6853     if (flags & JSObject::INDEXED) fprintf(stderr, " indexed");
6854     if (flags & JSObject::OWN_SHAPE) fprintf(stderr, " own_shape");
6855     if (flags & JSObject::HAS_EQUALITY) fprintf(stderr, " has_equality");
6856
6857     bool anyFlags = flags != 0;
6858     if (obj->isNative()) {
6859         if (obj->inDictionaryMode()) {
6860             fprintf(stderr, " inDictionaryMode");
6861             anyFlags = true;
6862         }
6863         if (obj->hasPropertyTable()) {
6864             fprintf(stderr, " hasPropertyTable");
6865             anyFlags = true;
6866         }
6867     }
6868     if (!anyFlags)
6869         fprintf(stderr, " none");
6870     fprintf(stderr, "\n");
6871
6872     if (obj->isDenseArray()) {
6873         unsigned slots = JS_MIN(obj->getArrayLength(), obj->getDenseArrayCapacity());
6874         fprintf(stderr, "elements\n");
6875         for (unsigned i = 0; i < slots; i++) {
6876             fprintf(stderr, " %3d: ", i);
6877             dumpValue(obj->getDenseArrayElement(i));
6878             fprintf(stderr, "\n");
6879             fflush(stderr);
6880         }
6881         return;
6882     }
6883
6884     fprintf(stderr, "proto ");
6885     dumpValue(ObjectOrNullValue(obj->getProto()));
6886     fputc('\n', stderr);
6887
6888     fprintf(stderr, "parent ");
6889     dumpValue(ObjectOrNullValue(obj->getParent()));
6890     fputc('\n', stderr);
6891
6892     if (clasp->flags & JSCLASS_HAS_PRIVATE)
6893         fprintf(stderr, "private %p\n", obj->getPrivate());
6894
6895     if (!obj->isNative())
6896         fprintf(stderr, "not native\n");
6897
6898     unsigned reservedEnd = JSCLASS_RESERVED_SLOTS(clasp);
6899     unsigned slots = obj->slotSpan();
6900     unsigned stop = obj->isNative() ? reservedEnd : slots;
6901     if (stop > 0)
6902         fprintf(stderr, obj->isNative() ? "reserved slots:\n" : "slots:\n");
6903     for (unsigned i = 0; i < stop; i++) {
6904         fprintf(stderr, " %3d ", i);
6905         if (i < reservedEnd)
6906             fprintf(stderr, "(reserved) ");
6907         fprintf(stderr, "= ");
6908         dumpValue(obj->getSlot(i));
6909         fputc('\n', stderr);
6910     }
6911
6912     if (obj->isNative()) {
6913         fprintf(stderr, "properties:\n");
6914         Vector<const Shape *, 8, SystemAllocPolicy> props;
6915         for (Shape::Range r = obj->lastProperty()->all(); !r.empty(); r.popFront())
6916             props.append(&r.front());
6917         for (size_t i = props.length(); i-- != 0;)
6918             DumpProperty(obj, *props[i]);
6919     }
6920     fputc('\n', stderr);
6921 }
6922
6923 static void
6924 MaybeDumpObject(const char *name, JSObject *obj)
6925 {
6926     if (obj) {
6927         fprintf(stderr, "  %s: ", name);
6928         dumpValue(ObjectValue(*obj));
6929         fputc('\n', stderr);
6930     }
6931 }
6932
6933 static void
6934 MaybeDumpValue(const char *name, const Value &v)
6935 {
6936     if (!v.isNull()) {
6937         fprintf(stderr, "  %s: ", name);
6938         dumpValue(v);
6939         fputc('\n', stderr);
6940     }
6941 }
6942
6943 JS_FRIEND_API(void)
6944 js_DumpStackFrame(JSContext *cx, JSStackFrame *start)
6945 {
6946     /* This should only called during live debugging. */
6947     VOUCH_DOES_NOT_REQUIRE_STACK();
6948
6949     if (!start)
6950         start = cx->maybefp();
6951     FrameRegsIter i(cx);
6952     while (!i.done() && i.fp() != start)
6953         ++i;
6954
6955     if (i.done()) {
6956         fprintf(stderr, "fp = %p not found in cx = %p\n", (void *)start, (void *)cx);
6957         return;
6958     }
6959
6960     for (; !i.done(); ++i) {
6961         JSStackFrame *const fp = i.fp();
6962
6963         fprintf(stderr, "JSStackFrame at %p\n", (void *) fp);
6964         if (fp->isFunctionFrame()) {
6965             fprintf(stderr, "callee fun: ");
6966             dumpValue(ObjectValue(fp->callee()));
6967         } else {
6968             fprintf(stderr, "global frame, no callee");
6969         }
6970         fputc('\n', stderr);
6971
6972         if (fp->isScriptFrame()) {
6973             fprintf(stderr, "file %s line %u\n",
6974                     fp->script()->filename, (unsigned) fp->script()->lineno);
6975         }
6976
6977         if (jsbytecode *pc = i.pc()) {
6978             if (!fp->isScriptFrame()) {
6979                 fprintf(stderr, "*** pc && !script, skipping frame\n\n");
6980                 continue;
6981             }
6982             if (fp->hasImacropc()) {
6983                 fprintf(stderr, "  pc in imacro at %p\n  called from ", pc);
6984                 pc = fp->imacropc();
6985             } else {
6986                 fprintf(stderr, "  ");
6987             }
6988             fprintf(stderr, "pc = %p\n", pc);
6989             fprintf(stderr, "  current op: %s\n", js_CodeName[*pc]);
6990         }
6991         Value *sp = i.sp();
6992         fprintf(stderr, "  slots: %p\n", (void *) fp->slots());
6993         fprintf(stderr, "  sp:    %p = slots + %u\n", (void *) sp, (unsigned) (sp - fp->slots()));
6994         if (sp - fp->slots() < 10000) { // sanity
6995             for (Value *p = fp->slots(); p < sp; p++) {
6996                 fprintf(stderr, "    %p: ", (void *) p);
6997                 dumpValue(*p);
6998                 fputc('\n', stderr);
6999             }
7000         }
7001         if (fp->isFunctionFrame() && !fp->isEvalFrame()) {
7002             fprintf(stderr, "  actuals: %p (%u) ", (void *) fp->actualArgs(), (unsigned) fp->numActualArgs());
7003             fprintf(stderr, "  formals: %p (%u)\n", (void *) fp->formalArgs(), (unsigned) fp->numFormalArgs());
7004         }
7005         MaybeDumpObject("callobj", fp->maybeCallObj());
7006         MaybeDumpObject("argsobj", fp->maybeArgsObj());
7007         if (!fp->isDummyFrame()) {
7008             MaybeDumpValue("this", fp->thisValue());
7009             fprintf(stderr, "  rval: ");
7010             dumpValue(fp->returnValue());
7011         } else {
7012             fprintf(stderr, "dummy frame");
7013         }
7014         fputc('\n', stderr);
7015
7016         fprintf(stderr, "  flags:");
7017         if (fp->isConstructing())
7018             fprintf(stderr, " constructing");
7019         if (fp->hasOverriddenArgs())
7020             fprintf(stderr, " overridden_args");
7021         if (fp->isAssigning())
7022             fprintf(stderr, " assigning");
7023         if (fp->isDebuggerFrame())
7024             fprintf(stderr, " debugger");
7025         if (fp->isEvalFrame())
7026             fprintf(stderr, " eval");
7027         if (fp->isYielding())
7028             fprintf(stderr, " yielding");
7029         if (fp->isGeneratorFrame())
7030             fprintf(stderr, " generator");
7031         fputc('\n', stderr);
7032
7033         fprintf(stderr, "  scopeChain: (JSObject *) %p\n", (void *) &fp->scopeChain());
7034
7035         fputc('\n', stderr);
7036     }
7037 }
7038
7039 #endif /* DEBUG */
7040