2009-06-10 18:29:44 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2007-03-22 10:30:00 -07:00
|
|
|
* vim: set ts=8 sw=4 et tw=78:
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JavaScript iterators.
|
|
|
|
*/
|
|
|
|
#include <string.h> /* for memcpy */
|
|
|
|
#include "jstypes.h"
|
2009-03-18 11:38:16 -07:00
|
|
|
#include "jsstdint.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsutil.h"
|
|
|
|
#include "jsarena.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "jsarray.h"
|
|
|
|
#include "jsatom.h"
|
|
|
|
#include "jsbool.h"
|
2009-05-05 17:49:29 -07:00
|
|
|
#include "jsbuiltins.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jscntxt.h"
|
2008-09-05 10:19:17 -07:00
|
|
|
#include "jsversion.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsexn.h"
|
|
|
|
#include "jsfun.h"
|
|
|
|
#include "jsgc.h"
|
2010-05-07 17:52:52 -07:00
|
|
|
#include "jshashtable.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsinterp.h"
|
|
|
|
#include "jsiter.h"
|
|
|
|
#include "jslock.h"
|
|
|
|
#include "jsnum.h"
|
|
|
|
#include "jsobj.h"
|
|
|
|
#include "jsopcode.h"
|
2010-05-18 19:21:43 -07:00
|
|
|
#include "jsproxy.h"
|
2007-07-05 13:37:47 -07:00
|
|
|
#include "jsscan.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsscope.h"
|
|
|
|
#include "jsscript.h"
|
2009-01-30 15:40:05 -08:00
|
|
|
#include "jsstaticcheck.h"
|
|
|
|
#include "jstracer.h"
|
2010-05-07 17:52:52 -07:00
|
|
|
#include "jsvector.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if JS_HAS_XML_SUPPORT
|
|
|
|
#include "jsxml.h"
|
|
|
|
#endif
|
|
|
|
|
2010-03-03 18:10:13 -08:00
|
|
|
#include "jscntxtinlines.h"
|
2010-04-10 16:16:35 -07:00
|
|
|
#include "jsobjinlines.h"
|
2010-05-07 17:52:52 -07:00
|
|
|
#include "jsstrinlines.h"
|
2010-04-10 16:16:35 -07:00
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
using namespace js;
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
static void iterator_finalize(JSContext *cx, JSObject *obj);
|
|
|
|
static void iterator_trace(JSTracer *trc, JSObject *obj);
|
|
|
|
static JSObject *iterator_iterator(JSContext *cx, JSObject *obj, JSBool keysonly);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
JSExtendedClass js_IteratorClass = {
|
|
|
|
{ "Iterator",
|
|
|
|
JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Iterator) |
|
|
|
|
JSCLASS_MARK_IS_TRACE |
|
|
|
|
JSCLASS_IS_EXTENDED,
|
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, iterator_finalize,
|
|
|
|
NULL, NULL, NULL, NULL,
|
|
|
|
NULL, NULL, JS_CLASS_TRACE(iterator_trace), NULL },
|
|
|
|
NULL, NULL, NULL, iterator_iterator,
|
|
|
|
NULL,
|
|
|
|
JSCLASS_NO_RESERVED_MEMBERS
|
|
|
|
};
|
2007-07-13 00:28:47 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
void
|
|
|
|
NativeIterator::mark(JSTracer *trc)
|
|
|
|
{
|
|
|
|
for (jsval *vp = props_array; vp < props_end; ++vp) {
|
|
|
|
JS_SET_TRACING_INDEX(trc, "props", (vp - props_array));
|
|
|
|
js_CallValueTracerIfGCThing(trc, *vp);
|
|
|
|
}
|
2010-06-03 21:41:01 -07:00
|
|
|
JS_CALL_OBJECT_TRACER(trc, obj, "obj");
|
2010-05-07 17:52:52 -07:00
|
|
|
}
|
2007-07-13 00:28:47 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
|
|
|
* Shared code to close iterator's state either through an explicit call or
|
|
|
|
* when GC detects that the iterator is no longer reachable.
|
|
|
|
*/
|
2010-04-10 16:08:14 -07:00
|
|
|
static void
|
2010-05-07 17:52:52 -07:00
|
|
|
iterator_finalize(JSContext *cx, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
JS_ASSERT(obj->getClass() == &js_IteratorClass.base);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Avoid double work if the iterator was closed by JSOP_ENDITER. */
|
|
|
|
NativeIterator *ni = obj->getNativeIterator();
|
|
|
|
if (ni) {
|
|
|
|
cx->free(ni);
|
|
|
|
obj->setNativeIterator(NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-10 16:08:14 -07:00
|
|
|
static void
|
2010-05-07 17:52:52 -07:00
|
|
|
iterator_trace(JSTracer *trc, JSObject *obj)
|
2010-04-10 16:08:14 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
NativeIterator *ni = obj->getNativeIterator();
|
|
|
|
|
|
|
|
if (ni)
|
|
|
|
ni->mark(trc);
|
2010-04-10 16:08:14 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
static bool
|
|
|
|
NewKeyValuePair(JSContext *cx, jsid key, jsval val, jsval *rval)
|
2009-09-19 02:40:43 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
jsval vec[2] = { ID_TO_VALUE(key), val };
|
|
|
|
AutoArrayRooter tvr(cx, JS_ARRAY_LENGTH(vec), vec);
|
|
|
|
|
|
|
|
JSObject *aobj = js_NewArrayObject(cx, 2, vec);
|
|
|
|
if (!aobj)
|
|
|
|
return false;
|
|
|
|
*rval = OBJECT_TO_JSVAL(aobj);
|
|
|
|
return true;
|
2009-09-19 02:40:43 -07:00
|
|
|
}
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
static bool
|
|
|
|
IdToIteratorValue(JSContext *cx, JSObject *obj, jsid id, uintN flags, jsval *vp)
|
|
|
|
{
|
|
|
|
if (!(flags & JSITER_FOREACH)) {
|
|
|
|
*vp = ID_TO_VALUE(id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the lookup on the original object instead of the prototype. */
|
|
|
|
if (!obj->getProperty(cx, id, vp))
|
|
|
|
return false;
|
|
|
|
if ((flags & JSITER_KEYVALUE) && !NewKeyValuePair(cx, id, *vp, vp))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
static inline bool
|
2010-05-12 16:37:54 -07:00
|
|
|
Enumerate(JSContext *cx, JSObject *obj, JSObject *pobj, jsid id,
|
2009-09-24 14:33:14 -07:00
|
|
|
bool enumerable, bool sharedPermanent, uintN flags, HashSet<jsid>& ht,
|
2010-05-12 16:37:54 -07:00
|
|
|
AutoValueVector& vec)
|
2010-05-07 17:52:52 -07:00
|
|
|
{
|
|
|
|
JS_ASSERT(JSVAL_IS_INT(id) || JSVAL_IS_STRING(id));
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
HashSet<jsid>::AddPtr p = ht.lookupForAdd(id);
|
|
|
|
JS_ASSERT_IF(obj == pobj, !p);
|
|
|
|
|
|
|
|
/* If we've already seen this, we definitely won't add it. */
|
|
|
|
if (JS_UNLIKELY(!!p))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's not necessary to add properties to the hash table at the end of the
|
|
|
|
* prototype chain.
|
|
|
|
*/
|
|
|
|
if (pobj->getProto() && !ht.add(p, id))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (JS_UNLIKELY(flags & JSITER_OWNONLY)) {
|
|
|
|
/*
|
|
|
|
* Shared-permanent hack: If this property is shared permanent
|
|
|
|
* and pobj and obj have the same class, then treat it as an own
|
|
|
|
* property of obj, even if pobj != obj. (But see bug 575997.)
|
|
|
|
*
|
|
|
|
* Omit the magic __proto__ property so that JS code can use
|
|
|
|
* Object.getOwnPropertyNames without worrying about it.
|
|
|
|
*/
|
|
|
|
if (!pobj->getProto() && id == ATOM_TO_JSID(cx->runtime->atomState.protoAtom))
|
|
|
|
return true;
|
|
|
|
if (pobj != obj && !(sharedPermanent && pobj->getClass() == obj->getClass()))
|
2010-05-07 17:52:52 -07:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-24 14:33:14 -07:00
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
if (enumerable || (flags & JSITER_HIDDEN)) {
|
2010-06-25 15:58:09 -07:00
|
|
|
if (!vec.append(JSVAL_VOID))
|
|
|
|
return false;
|
|
|
|
if (!IdToIteratorValue(cx, obj, id, flags, vec.end() - 1))
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
static bool
|
2010-05-12 16:37:54 -07:00
|
|
|
EnumerateNativeProperties(JSContext *cx, JSObject *obj, JSObject *pobj, uintN flags,
|
|
|
|
HashSet<jsid> &ht, AutoValueVector& props)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
AutoValueVector sprops(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-12 16:37:54 -07:00
|
|
|
JS_LOCK_OBJ(cx, pobj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Collect all unique properties from this object's scope. */
|
2010-05-12 16:37:54 -07:00
|
|
|
JSScope *scope = pobj->scope();
|
2010-05-07 17:52:52 -07:00
|
|
|
for (JSScopeProperty *sprop = scope->lastProperty(); sprop; sprop = sprop->parent) {
|
|
|
|
if (sprop->id != JSVAL_VOID &&
|
|
|
|
!sprop->isAlias() &&
|
2009-09-24 14:33:14 -07:00
|
|
|
!Enumerate(cx, obj, pobj, sprop->id, sprop->enumerable(), sprop->isSharedPermanent(),
|
|
|
|
flags, ht, sprops))
|
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
while (sprops.length() > 0) {
|
2010-06-02 14:17:52 -07:00
|
|
|
if (!props.append(sprops.back()))
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
|
|
|
sprops.popBack();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
JS_UNLOCK_SCOPE(cx, scope);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2010-05-12 16:37:54 -07:00
|
|
|
EnumerateDenseArrayProperties(JSContext *cx, JSObject *obj, JSObject *pobj, uintN flags,
|
|
|
|
HashSet<jsid> &ht, AutoValueVector& props)
|
2010-05-07 17:52:52 -07:00
|
|
|
{
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!Enumerate(cx, obj, pobj, ATOM_TO_JSID(cx->runtime->atomState.lengthAtom), false, true,
|
|
|
|
flags, ht, props)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-07 17:52:52 -07:00
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
if (pobj->getDenseArrayCount() > 0) {
|
2010-05-12 16:37:54 -07:00
|
|
|
size_t capacity = pobj->getDenseArrayCapacity();
|
|
|
|
jsval *vp = pobj->dslots;
|
2010-05-07 17:52:52 -07:00
|
|
|
for (size_t i = 0; i < capacity; ++i, ++vp) {
|
|
|
|
if (*vp != JSVAL_HOLE) {
|
|
|
|
/* Dense arrays never get so large that i would not fit into an integer id. */
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!Enumerate(cx, obj, pobj, INT_TO_JSVAL(i), true, false, flags, ht, props))
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-09-24 14:33:14 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-27 12:03:25 -07:00
|
|
|
NativeIterator *
|
2010-06-03 21:41:01 -07:00
|
|
|
NativeIterator::allocate(JSContext *cx, JSObject *obj, uintN flags, uint32 *sarray, uint32 slength,
|
2010-06-16 16:17:45 -07:00
|
|
|
uint32 key, AutoValueVector &props)
|
2010-05-18 19:21:43 -07:00
|
|
|
{
|
2010-06-16 16:17:45 -07:00
|
|
|
size_t plength = props.length();
|
2010-05-18 19:21:43 -07:00
|
|
|
NativeIterator *ni = (NativeIterator *)
|
|
|
|
cx->malloc(sizeof(NativeIterator) + plength * sizeof(jsval) + slength * sizeof(uint32));
|
2010-06-02 14:17:52 -07:00
|
|
|
if (!ni)
|
2010-05-27 12:03:25 -07:00
|
|
|
return NULL;
|
2010-06-03 21:41:01 -07:00
|
|
|
ni->obj = obj;
|
2010-05-18 19:21:43 -07:00
|
|
|
ni->props_array = ni->props_cursor = (jsval *) (ni + 1);
|
|
|
|
ni->props_end = ni->props_array + plength;
|
|
|
|
if (plength)
|
2010-06-16 16:17:45 -07:00
|
|
|
memcpy(ni->props_array, props.begin(), plength * sizeof(jsval));
|
2010-05-18 19:21:43 -07:00
|
|
|
ni->shapes_array = (uint32 *) ni->props_end;
|
|
|
|
ni->shapes_length = slength;
|
|
|
|
ni->shapes_key = key;
|
|
|
|
ni->flags = flags;
|
|
|
|
if (slength)
|
|
|
|
memcpy(ni->shapes_array, sarray, slength * sizeof(uint32));
|
2010-05-27 12:03:25 -07:00
|
|
|
return ni;
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
|
|
|
|
2010-06-16 14:12:21 -07:00
|
|
|
static bool
|
|
|
|
Snapshot(JSContext *cx, JSObject *obj, uintN flags, AutoValueVector &props)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-24 14:33:14 -07:00
|
|
|
/*
|
|
|
|
* FIXME: Bug 575997 - We won't need to initialize this hash table if
|
|
|
|
* (flags & JSITER_OWNONLY) when we eliminate inheritance of
|
|
|
|
* shared-permanent properties as own properties.
|
|
|
|
*/
|
2010-05-07 17:52:52 -07:00
|
|
|
HashSet<jsid> ht(cx);
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!ht.init(32))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-12 16:37:54 -07:00
|
|
|
JSObject *pobj = obj;
|
2010-06-16 14:12:21 -07:00
|
|
|
do {
|
2010-05-12 16:37:54 -07:00
|
|
|
JSClass *clasp = pobj->getClass();
|
|
|
|
if (pobj->isNative() &&
|
|
|
|
pobj->map->ops->enumerate == js_Enumerate &&
|
2010-05-07 17:52:52 -07:00
|
|
|
!(clasp->flags & JSCLASS_NEW_ENUMERATE)) {
|
2010-05-12 16:37:54 -07:00
|
|
|
if (!clasp->enumerate(cx, pobj))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-12 16:37:54 -07:00
|
|
|
if (!EnumerateNativeProperties(cx, obj, pobj, flags, ht, props))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-12 16:37:54 -07:00
|
|
|
} else if (pobj->isDenseArray()) {
|
|
|
|
if (!EnumerateDenseArrayProperties(cx, obj, pobj, flags, ht, props))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-07 17:52:52 -07:00
|
|
|
} else {
|
2010-05-18 19:21:43 -07:00
|
|
|
if (pobj->isProxy()) {
|
2010-06-16 16:11:13 -07:00
|
|
|
AutoValueVector proxyProps(cx);
|
2010-05-18 19:21:43 -07:00
|
|
|
if (flags & JSITER_OWNONLY) {
|
2010-06-16 16:11:13 -07:00
|
|
|
if (!JSProxy::enumerateOwn(cx, pobj, proxyProps))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-18 19:21:43 -07:00
|
|
|
} else {
|
2010-06-16 16:11:13 -07:00
|
|
|
if (!JSProxy::enumerate(cx, pobj, proxyProps))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
2010-06-16 16:11:13 -07:00
|
|
|
for (size_t n = 0, len = proxyProps.length(); n < len; n++) {
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!Enumerate(cx, obj, pobj, (jsid) proxyProps[n], true, false, flags, ht, props))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
|
|
|
/* Proxy objects enumerate the prototype on their own, so we are done here. */
|
|
|
|
break;
|
|
|
|
}
|
2010-05-07 17:52:52 -07:00
|
|
|
jsval state;
|
2009-09-24 14:33:14 -07:00
|
|
|
JSIterateOp op = (flags & JSITER_HIDDEN) ? JSENUMERATE_INIT_ALL : JSENUMERATE_INIT;
|
|
|
|
if (!pobj->enumerate(cx, op, &state, NULL))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-07 17:52:52 -07:00
|
|
|
if (state == JSVAL_NATIVE_ENUMERATE_COOKIE) {
|
2010-05-12 16:37:54 -07:00
|
|
|
if (!EnumerateNativeProperties(cx, obj, pobj, flags, ht, props))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-07 17:52:52 -07:00
|
|
|
} else {
|
|
|
|
while (true) {
|
|
|
|
jsid id;
|
2010-05-12 16:37:54 -07:00
|
|
|
if (!pobj->enumerate(cx, JSENUMERATE_NEXT, &state, &id))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-07 17:52:52 -07:00
|
|
|
if (state == JSVAL_NULL)
|
|
|
|
break;
|
2009-09-24 14:33:14 -07:00
|
|
|
if (!Enumerate(cx, obj, pobj, id, true, false, flags, ht, props))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
2010-05-07 17:52:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-24 14:33:14 -07:00
|
|
|
if (JS_UNLIKELY(pobj->isXML()))
|
2010-05-07 17:52:52 -07:00
|
|
|
break;
|
2010-06-16 14:12:21 -07:00
|
|
|
} while ((pobj = pobj->getProto()) != NULL);
|
2010-05-07 17:52:52 -07:00
|
|
|
|
2010-06-16 14:12:21 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
bool
|
2010-06-16 14:12:21 -07:00
|
|
|
VectorToIdArray(JSContext *cx, AutoValueVector &props, JSIdArray **idap)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-16 14:12:21 -07:00
|
|
|
JS_STATIC_ASSERT(sizeof(JSIdArray) > sizeof(jsid));
|
|
|
|
size_t len = props.length();
|
|
|
|
size_t idsz = len * sizeof(jsid);
|
|
|
|
size_t sz = (sizeof(JSIdArray) - sizeof(jsid)) + idsz;
|
|
|
|
JSIdArray *ida = static_cast<JSIdArray *>(cx->malloc(sz));
|
|
|
|
if (!ida)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ida->length = static_cast<jsint>(len);
|
|
|
|
memcpy(ida->vector, props.begin(), idsz);
|
2010-05-07 17:52:52 -07:00
|
|
|
*idap = ida;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-18 19:21:43 -07:00
|
|
|
bool
|
2010-06-16 16:11:13 -07:00
|
|
|
GetPropertyNames(JSContext *cx, JSObject *obj, uintN flags, AutoValueVector &props)
|
2010-05-18 19:21:43 -07:00
|
|
|
{
|
2010-06-16 16:11:13 -07:00
|
|
|
return Snapshot(cx, obj, flags & (JSITER_OWNONLY | JSITER_HIDDEN), props);
|
2010-05-18 19:21:43 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
static inline bool
|
|
|
|
GetCustomIterator(JSContext *cx, JSObject *obj, uintN flags, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Check whether we have a valid __iterator__ method. */
|
|
|
|
JSAtom *atom = cx->runtime->atomState.iteratorAtom;
|
|
|
|
if (!js_GetMethod(cx, obj, ATOM_TO_JSID(atom), JSGET_NO_METHOD_BARRIER, vp))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
/* If there is no custom __iterator__ method, we are done here. */
|
|
|
|
if (*vp == JSVAL_VOID)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Otherwise call it and return that object. */
|
|
|
|
LeaveTrace(cx);
|
|
|
|
jsval arg = BOOLEAN_TO_JSVAL((flags & JSITER_FOREACH) == 0);
|
2010-05-29 19:04:01 -07:00
|
|
|
if (!js_InternalCall(cx, obj, *vp, 1, &arg, vp))
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
|
|
|
if (JSVAL_IS_PRIMITIVE(*vp)) {
|
2010-05-29 19:04:01 -07:00
|
|
|
/*
|
|
|
|
* We are always coming from js_ValueToIterator, and we are no longer on
|
|
|
|
* trace, so the object we are iterating over is on top of the stack (-1).
|
|
|
|
*/
|
|
|
|
js_ReportValueError2(cx, JSMSG_BAD_TRAP_RETURN_VALUE,
|
|
|
|
-1, OBJECT_TO_JSVAL(obj), NULL,
|
|
|
|
js_AtomToPrintableString(cx, atom));
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
template <typename T>
|
|
|
|
static inline bool
|
|
|
|
Compare(T *a, T *b, size_t c)
|
|
|
|
{
|
|
|
|
size_t n = (c + size_t(7)) / size_t(8);
|
|
|
|
switch (c % 8) {
|
|
|
|
case 0: do { if (*a++ != *b++) return false;
|
|
|
|
case 7: if (*a++ != *b++) return false;
|
|
|
|
case 6: if (*a++ != *b++) return false;
|
|
|
|
case 5: if (*a++ != *b++) return false;
|
|
|
|
case 4: if (*a++ != *b++) return false;
|
|
|
|
case 3: if (*a++ != *b++) return false;
|
|
|
|
case 2: if (*a++ != *b++) return false;
|
|
|
|
case 1: if (*a++ != *b++) return false;
|
|
|
|
} while (--n > 0);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-18 17:43:02 -07:00
|
|
|
static inline JSObject *
|
2010-05-27 12:03:25 -07:00
|
|
|
NewIteratorObject(JSContext *cx, uintN flags)
|
|
|
|
{
|
2010-06-18 17:43:02 -07:00
|
|
|
if (flags & JSITER_ENUMERATE) {
|
|
|
|
/*
|
|
|
|
* Non-escaping native enumerator objects do not need map, proto, or
|
|
|
|
* parent. However, code in jstracer.cpp and elsewhere may find such a
|
|
|
|
* native enumerator object via the stack and (as for all objects that
|
|
|
|
* are not stillborn, with the exception of "NoSuchMethod" internal
|
|
|
|
* helper objects) expect it to have a non-null map pointer, so we
|
|
|
|
* share an empty Enumerator scope in the runtime.
|
|
|
|
*/
|
|
|
|
JSObject *obj = js_NewGCObject(cx);
|
|
|
|
if (!obj)
|
|
|
|
return false;
|
|
|
|
obj->map = cx->runtime->emptyEnumeratorScope->hold();
|
|
|
|
obj->init(&js_IteratorClass.base, NULL, NULL, JSVAL_NULL);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewBuiltinClassInstance(cx, &js_IteratorClass.base);
|
2010-05-27 12:03:25 -07:00
|
|
|
}
|
|
|
|
|
2010-06-03 21:41:01 -07:00
|
|
|
static inline void
|
|
|
|
RegisterEnumerator(JSContext *cx, JSObject *iterobj, NativeIterator *ni)
|
|
|
|
{
|
|
|
|
/* Register non-escaping native enumerators (for-in) with the current context. */
|
|
|
|
if (ni->flags & JSITER_ENUMERATE) {
|
|
|
|
ni->next = cx->enumerators;
|
|
|
|
cx->enumerators = iterobj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 12:03:25 -07:00
|
|
|
bool
|
2010-06-16 16:11:13 -07:00
|
|
|
IdVectorToIterator(JSContext *cx, JSObject *obj, uintN flags, AutoValueVector &props, jsval *vp)
|
2010-05-27 12:03:25 -07:00
|
|
|
{
|
|
|
|
JSObject *iterobj = NewIteratorObject(cx, flags);
|
|
|
|
if (!iterobj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*vp = OBJECT_TO_JSVAL(iterobj);
|
|
|
|
|
2010-06-16 16:17:45 -07:00
|
|
|
NativeIterator *ni = NativeIterator::allocate(cx, obj, flags, NULL, 0, 0, props);
|
2010-05-27 12:03:25 -07:00
|
|
|
if (!ni)
|
|
|
|
return false;
|
|
|
|
|
2010-06-25 15:58:09 -07:00
|
|
|
/* If this is a for-each iteration, fetch the values or key/value pairs. */
|
|
|
|
if (flags & JSITER_FOREACH) {
|
|
|
|
size_t length = props.length();
|
|
|
|
for (size_t n = 0; n < length; ++n) {
|
|
|
|
jsval *vp = &ni->begin()[n];
|
|
|
|
if (!IdToIteratorValue(cx, obj, *vp, flags, vp))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-27 12:03:25 -07:00
|
|
|
iterobj->setNativeIterator(ni);
|
2010-06-03 21:41:01 -07:00
|
|
|
|
|
|
|
RegisterEnumerator(cx, iterobj, ni);
|
2010-05-27 12:03:25 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2010-05-07 17:52:52 -07:00
|
|
|
GetIterator(JSContext *cx, JSObject *obj, uintN flags, jsval *vp)
|
|
|
|
{
|
|
|
|
uint32 hash;
|
|
|
|
JSObject **hp;
|
|
|
|
Vector<uint32, 8> shapes(cx);
|
|
|
|
uint32 key = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
bool keysOnly = (flags == JSITER_ENUMERATE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
if (obj) {
|
|
|
|
if (keysOnly) {
|
|
|
|
/*
|
|
|
|
* The iterator object for JSITER_ENUMERATE never escapes, so we
|
|
|
|
* don't care for the proper parent/proto to be set. This also
|
|
|
|
* allows us to re-use a previous iterator object that was freed
|
|
|
|
* by JSOP_ENDITER.
|
|
|
|
*/
|
|
|
|
JSObject *pobj = obj;
|
|
|
|
do {
|
|
|
|
if (!pobj->isNative() ||
|
|
|
|
obj->map->ops->enumerate != js_Enumerate ||
|
|
|
|
pobj->getClass()->enumerate != JS_EnumerateStub) {
|
|
|
|
shapes.clear();
|
|
|
|
goto miss;
|
|
|
|
}
|
|
|
|
uint32 shape = pobj->shape();
|
|
|
|
key = (key + (key << 16)) ^ shape;
|
|
|
|
if (!shapes.append(shape))
|
|
|
|
return false;
|
|
|
|
pobj = pobj->getProto();
|
|
|
|
} while (pobj);
|
|
|
|
|
|
|
|
hash = key % JS_ARRAY_LENGTH(JS_THREAD_DATA(cx)->cachedNativeIterators);
|
|
|
|
hp = &JS_THREAD_DATA(cx)->cachedNativeIterators[hash];
|
|
|
|
JSObject *iterobj = *hp;
|
|
|
|
if (iterobj) {
|
2010-05-27 12:03:25 -07:00
|
|
|
NativeIterator *ni = iterobj->getNativeIterator();
|
2010-05-07 17:52:52 -07:00
|
|
|
if (ni->shapes_key == key &&
|
|
|
|
ni->shapes_length == shapes.length() &&
|
|
|
|
Compare(ni->shapes_array, shapes.begin(), ni->shapes_length)) {
|
|
|
|
*vp = OBJECT_TO_JSVAL(iterobj);
|
|
|
|
*hp = ni->next;
|
2010-06-03 21:41:01 -07:00
|
|
|
|
|
|
|
RegisterEnumerator(cx, iterobj, ni);
|
2010-05-07 17:52:52 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-07 17:52:52 -07:00
|
|
|
|
|
|
|
miss:
|
2010-05-27 12:03:25 -07:00
|
|
|
if (obj->isProxy())
|
|
|
|
return JSProxy::iterate(cx, obj, flags, vp);
|
|
|
|
if (!GetCustomIterator(cx, obj, flags, vp))
|
|
|
|
return false;
|
|
|
|
if (*vp != JSVAL_VOID)
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-27 12:03:25 -07:00
|
|
|
JSObject *iterobj = NewIteratorObject(cx, flags);
|
2010-05-07 17:52:52 -07:00
|
|
|
if (!iterobj)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Store in *vp to protect it from GC (callers must root vp). */
|
|
|
|
*vp = OBJECT_TO_JSVAL(iterobj);
|
|
|
|
|
2010-06-17 14:45:55 -07:00
|
|
|
/* NB: for (var p in null) succeeds by iterating over no properties. */
|
2010-06-16 14:12:21 -07:00
|
|
|
AutoValueVector props(cx);
|
2010-06-17 15:02:38 -07:00
|
|
|
if (JS_LIKELY(obj != NULL) && !Snapshot(cx, obj, flags, props))
|
2010-06-16 14:12:21 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
NativeIterator *ni =
|
2010-06-16 16:17:45 -07:00
|
|
|
NativeIterator::allocate(cx, obj, flags, shapes.begin(), shapes.length(), key, props);
|
2010-05-27 12:03:25 -07:00
|
|
|
if (!ni)
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
|
|
|
|
2010-05-27 12:03:25 -07:00
|
|
|
iterobj->setNativeIterator(ni);
|
2010-06-03 21:41:01 -07:00
|
|
|
|
|
|
|
RegisterEnumerator(cx, iterobj, ni);
|
2010-05-07 17:52:52 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject *
|
|
|
|
iterator_iterator(JSContext *cx, JSObject *obj, JSBool keysonly)
|
|
|
|
{
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
Iterator(JSContext *cx, JSObject *iterobj, uintN argc, jsval *argv, jsval *rval)
|
|
|
|
{
|
|
|
|
JSBool keyonly;
|
|
|
|
uintN flags;
|
|
|
|
|
|
|
|
keyonly = js_ValueToBoolean(argv[1]);
|
2010-06-02 16:21:58 -07:00
|
|
|
flags = JSITER_OWNONLY | (keyonly ? 0 : (JSITER_FOREACH | JSITER_KEYVALUE));
|
2010-05-07 17:52:52 -07:00
|
|
|
*rval = argv[0];
|
|
|
|
return js_ValueToIterator(cx, flags, rval);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-30 17:31:22 -07:00
|
|
|
JSBool
|
|
|
|
js_ThrowStopIteration(JSContext *cx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
jsval v;
|
|
|
|
|
|
|
|
JS_ASSERT(!JS_IsExceptionPending(cx));
|
2010-02-19 09:44:23 -08:00
|
|
|
if (js_FindClassObject(cx, NULL, JSProto_StopIteration, &v))
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_SetPendingException(cx, v);
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
iterator_next(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
2010-05-07 17:52:52 -07:00
|
|
|
if (!JS_InstanceOf(cx, obj, &js_IteratorClass.base, vp + 2))
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
if (!js_IteratorMore(cx, obj, vp))
|
|
|
|
return false;
|
|
|
|
if (*vp == JSVAL_FALSE) {
|
2007-09-30 17:31:22 -07:00
|
|
|
js_ThrowStopIteration(cx);
|
2010-05-07 17:52:52 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-07 17:52:52 -07:00
|
|
|
JS_ASSERT(*vp == JSVAL_TRUE);
|
|
|
|
return js_IteratorNext(cx, obj, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-01 21:33:52 -07:00
|
|
|
#define JSPROP_ROPERM (JSPROP_READONLY | JSPROP_PERMANENT)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static JSFunctionSpec iterator_methods[] = {
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_next_str, iterator_next, 0,JSPROP_ROPERM),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call ToObject(v).__iterator__(keyonly) if ToObject(v).__iterator__ exists.
|
2008-02-18 13:01:47 -08:00
|
|
|
* Otherwise construct the default iterator.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2007-12-27 13:34:03 -08:00
|
|
|
JS_FRIEND_API(JSBool)
|
2007-03-22 10:30:00 -07:00
|
|
|
js_ValueToIterator(JSContext *cx, uintN flags, jsval *vp)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
2007-09-15 09:58:45 -07:00
|
|
|
JSClass *clasp;
|
|
|
|
JSExtendedClass *xclasp;
|
2007-03-22 10:30:00 -07:00
|
|
|
JSObject *iterobj;
|
|
|
|
|
|
|
|
/* JSITER_KEYVALUE must always come with JSITER_FOREACH */
|
2010-05-07 17:52:52 -07:00
|
|
|
JS_ASSERT_IF(flags & JSITER_KEYVALUE, flags & JSITER_FOREACH);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the more/next state machine doesn't get stuck. A value might be
|
|
|
|
* left in iterValue when a trace is left due to an operation time-out after
|
|
|
|
* JSOP_MOREITER but before the value is picked up by FOR*.
|
|
|
|
*/
|
|
|
|
cx->iterValue = JSVAL_HOLE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-29 11:36:33 -07:00
|
|
|
AutoValueRooter tvr(cx);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!JSVAL_IS_PRIMITIVE(*vp)) {
|
2010-04-11 20:55:22 -07:00
|
|
|
/* Common case. */
|
2007-03-22 10:30:00 -07:00
|
|
|
obj = JSVAL_TO_OBJECT(*vp);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Enumerating over null and undefined gives an empty enumerator.
|
|
|
|
* This is contrary to ECMA-262 9.9 ToObject, invoked from step 3 of
|
|
|
|
* the first production in 12.6.4 and step 4 of the second production,
|
2010-04-11 20:55:22 -07:00
|
|
|
* but it's "web JS" compatible. ES5 fixed for-in to match this de-facto
|
|
|
|
* standard.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
if ((flags & JSITER_ENUMERATE)) {
|
|
|
|
if (!js_ValueToObject(cx, *vp, &obj))
|
2010-03-29 11:36:33 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
2010-05-07 17:52:52 -07:00
|
|
|
return GetIterator(cx, obj, flags, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
obj = js_ValueToNonNullObject(cx, *vp);
|
|
|
|
if (!obj)
|
2010-03-29 11:36:33 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-29 11:36:33 -07:00
|
|
|
tvr.setObject(obj);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-07 16:09:49 -07:00
|
|
|
clasp = obj->getClass();
|
2007-09-15 09:58:45 -07:00
|
|
|
if ((clasp->flags & JSCLASS_IS_EXTENDED) &&
|
|
|
|
(xclasp = (JSExtendedClass *) clasp)->iteratorObject) {
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Enumerate Iterator.prototype directly. */
|
|
|
|
if (clasp != &js_IteratorClass.base || obj->getNativeIterator()) {
|
|
|
|
iterobj = xclasp->iteratorObject(cx, obj, !(flags & JSITER_FOREACH));
|
|
|
|
if (!iterobj)
|
2010-03-29 11:36:33 -07:00
|
|
|
return false;
|
2010-05-07 17:52:52 -07:00
|
|
|
*vp = OBJECT_TO_JSVAL(iterobj);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
return GetIterator(cx, obj, flags, vp);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
static JS_REQUIRES_STACK JSBool
|
|
|
|
CloseGenerator(JSContext *cx, JSObject *genobj);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JS_FRIEND_API(JSBool)
|
2007-07-02 05:13:23 -07:00
|
|
|
js_CloseIterator(JSContext *cx, jsval v)
|
|
|
|
{
|
|
|
|
JSObject *obj;
|
|
|
|
JSClass *clasp;
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
cx->iterValue = JSVAL_HOLE;
|
|
|
|
|
2007-07-02 05:13:23 -07:00
|
|
|
JS_ASSERT(!JSVAL_IS_PRIMITIVE(v));
|
|
|
|
obj = JSVAL_TO_OBJECT(v);
|
2010-04-07 16:09:49 -07:00
|
|
|
clasp = obj->getClass();
|
2007-07-02 05:13:23 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
if (clasp == &js_IteratorClass.base) {
|
2010-06-03 21:41:01 -07:00
|
|
|
/* Remove enumerators from the active list, which is a stack. */
|
2010-06-02 23:48:22 -07:00
|
|
|
NativeIterator *ni = obj->getNativeIterator();
|
2010-06-03 21:41:01 -07:00
|
|
|
if (ni->flags & JSITER_ENUMERATE) {
|
|
|
|
JS_ASSERT(cx->enumerators == obj);
|
|
|
|
cx->enumerators = ni->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cache the iterator object if possible. */
|
2010-05-07 17:52:52 -07:00
|
|
|
if (ni->shapes_length) {
|
2010-05-27 12:03:25 -07:00
|
|
|
uint32 hash = ni->shapes_key % NATIVE_ITER_CACHE_SIZE;
|
2010-05-07 17:52:52 -07:00
|
|
|
JSObject **hp = &JS_THREAD_DATA(cx)->cachedNativeIterators[hash];
|
|
|
|
ni->props_cursor = ni->props_array;
|
|
|
|
ni->next = *hp;
|
|
|
|
*hp = obj;
|
|
|
|
} else {
|
|
|
|
iterator_finalize(cx, obj);
|
|
|
|
}
|
2007-07-02 05:13:23 -07:00
|
|
|
}
|
|
|
|
#if JS_HAS_GENERATORS
|
2010-05-07 17:52:52 -07:00
|
|
|
else if (clasp == &js_GeneratorClass.base) {
|
|
|
|
return CloseGenerator(cx, obj);
|
2007-07-02 05:13:23 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:41:01 -07:00
|
|
|
/*
|
|
|
|
* Suppress enumeration of deleted properties. We maintain a list of all active
|
|
|
|
* non-escaping for-in enumerators. Whenever a property is deleted, we check
|
|
|
|
* whether any active enumerator contains the (obj, id) pair and has not
|
|
|
|
* enumerated id yet. If so, we delete the id from the list (or advance the
|
|
|
|
* cursor if it is the next id to be enumerated).
|
|
|
|
*
|
|
|
|
* We do not suppress enumeration of a property deleted along an object's
|
|
|
|
* prototype chain. Only direct deletions on the object are handled.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
js_SuppressDeletedProperty(JSContext *cx, JSObject *obj, jsid id)
|
|
|
|
{
|
|
|
|
JSObject *iterobj = cx->enumerators;
|
|
|
|
while (iterobj) {
|
2010-06-06 13:23:48 -07:00
|
|
|
again:
|
2010-06-03 21:41:01 -07:00
|
|
|
NativeIterator *ni = iterobj->getNativeIterator();
|
|
|
|
if (ni->obj == obj && ni->props_cursor < ni->props_end) {
|
|
|
|
/* Check whether id is still to come. */
|
2010-06-06 13:23:48 -07:00
|
|
|
jsid *props_cursor = ni->props_cursor;
|
|
|
|
jsid *props_end = ni->props_end;
|
|
|
|
for (jsid *idp = props_cursor; idp < props_end; ++idp) {
|
2010-06-03 21:41:01 -07:00
|
|
|
if (*idp == id) {
|
|
|
|
/*
|
|
|
|
* Check whether another property along the prototype chain
|
|
|
|
* became visible as a result of this deletion.
|
|
|
|
*/
|
|
|
|
if (obj->getProto()) {
|
|
|
|
AutoObjectRooter proto(cx, obj->getProto());
|
|
|
|
AutoObjectRooter obj2(cx);
|
|
|
|
JSProperty *prop;
|
|
|
|
if (!proto.object()->lookupProperty(cx, id, obj2.addr(), &prop))
|
|
|
|
return false;
|
2010-06-05 14:24:54 -07:00
|
|
|
if (prop) {
|
2010-06-03 21:41:01 -07:00
|
|
|
uintN attrs;
|
2010-06-05 14:24:54 -07:00
|
|
|
if (obj2.object()->isNative()) {
|
|
|
|
attrs = ((JSScopeProperty *) prop)->attributes();
|
|
|
|
JS_UNLOCK_OBJ(cx, obj2.object());
|
|
|
|
} else if (!obj2.object()->getAttributes(cx, id, &attrs)) {
|
2010-06-03 21:41:01 -07:00
|
|
|
return false;
|
2010-06-05 14:24:54 -07:00
|
|
|
}
|
2010-06-03 21:41:01 -07:00
|
|
|
if (attrs & JSPROP_ENUMERATE)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-06 13:23:48 -07:00
|
|
|
/*
|
|
|
|
* If lookupProperty or getAttributes above removed a property from
|
|
|
|
* ni, start over.
|
|
|
|
*/
|
|
|
|
if (props_end != ni->props_end || props_cursor != ni->props_cursor)
|
|
|
|
goto again;
|
|
|
|
|
2010-06-03 21:41:01 -07:00
|
|
|
/*
|
|
|
|
* No property along the prototype chain steppeded in to take the
|
|
|
|
* property's place, so go ahead and delete id from the list.
|
|
|
|
* If it is the next property to be enumerated, just skip it.
|
|
|
|
*/
|
2010-06-06 13:23:48 -07:00
|
|
|
if (idp == props_cursor) {
|
2010-06-03 21:41:01 -07:00
|
|
|
ni->props_cursor++;
|
|
|
|
} else {
|
2010-06-06 13:23:48 -07:00
|
|
|
memmove(idp, idp + 1, (props_end - (idp + 1)) * sizeof(jsid));
|
2010-06-03 21:41:01 -07:00
|
|
|
ni->props_end--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iterobj = ni->next;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
JSBool
|
|
|
|
js_IteratorMore(JSContext *cx, JSObject *iterobj, jsval *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Fast path for native iterators */
|
|
|
|
if (iterobj->getClass() == &js_IteratorClass.base) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2010-05-07 17:52:52 -07:00
|
|
|
* Implement next directly as all the methods of native iterator are
|
|
|
|
* read-only and permanent.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2010-05-07 17:52:52 -07:00
|
|
|
NativeIterator *ni = iterobj->getNativeIterator();
|
|
|
|
*rval = BOOLEAN_TO_JSVAL(ni->props_cursor < ni->props_end);
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
/* We might still have a pending value. */
|
|
|
|
if (cx->iterValue != JSVAL_HOLE) {
|
|
|
|
*rval = JSVAL_TRUE;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Fetch and cache the next value from the iterator. */
|
|
|
|
jsid id = ATOM_TO_JSID(cx->runtime->atomState.nextAtom);
|
|
|
|
if (!JS_GetMethodById(cx, iterobj, id, &iterobj, rval))
|
|
|
|
return false;
|
|
|
|
if (!js_InternalCall(cx, iterobj, *rval, 0, NULL, rval)) {
|
|
|
|
/* Check for StopIteration. */
|
|
|
|
if (!cx->throwing || !js_ValueIsStopIteration(cx->exception))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Inline JS_ClearPendingException(cx). */
|
|
|
|
cx->throwing = JS_FALSE;
|
|
|
|
cx->exception = JSVAL_VOID;
|
|
|
|
cx->iterValue = JSVAL_HOLE;
|
|
|
|
*rval = JSVAL_FALSE;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
/* Cache the value returned by iterobj.next() so js_IteratorNext() can find it. */
|
|
|
|
JS_ASSERT(*rval != JSVAL_HOLE);
|
|
|
|
cx->iterValue = *rval;
|
|
|
|
*rval = JSVAL_TRUE;
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
JSBool
|
|
|
|
js_IteratorNext(JSContext *cx, JSObject *iterobj, jsval *rval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/* Fast path for native iterators */
|
2010-05-07 17:52:52 -07:00
|
|
|
if (iterobj->getClass() == &js_IteratorClass.base) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/*
|
2010-05-07 17:52:52 -07:00
|
|
|
* Implement next directly as all the methods of the native iterator are
|
2007-03-22 10:30:00 -07:00
|
|
|
* read-only and permanent.
|
|
|
|
*/
|
2010-05-07 17:52:52 -07:00
|
|
|
NativeIterator *ni = iterobj->getNativeIterator();
|
|
|
|
JS_ASSERT(ni->props_cursor < ni->props_end);
|
|
|
|
*rval = *ni->props_cursor++;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
if (JSVAL_IS_STRING(*rval) || (ni->flags & JSITER_FOREACH))
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
JSString *str;
|
|
|
|
jsint i;
|
|
|
|
if (JSVAL_IS_INT(*rval) && (jsuint(i = JSVAL_TO_INT(*rval)) < INT_STRING_LIMIT)) {
|
|
|
|
str = JSString::intString(i);
|
|
|
|
} else {
|
|
|
|
str = js_ValueToString(cx, *rval);
|
|
|
|
if (!str)
|
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-05-07 17:52:52 -07:00
|
|
|
|
|
|
|
*rval = STRING_TO_JSVAL(str);
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
JS_ASSERT(cx->iterValue != JSVAL_HOLE);
|
|
|
|
*rval = cx->iterValue;
|
|
|
|
cx->iterValue = JSVAL_HOLE;
|
|
|
|
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
|
|
|
stopiter_hasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp)
|
|
|
|
{
|
2008-11-13 00:30:20 -08:00
|
|
|
*bp = js_ValueIsStopIteration(v);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSClass js_StopIterationClass = {
|
|
|
|
js_StopIteration_str,
|
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_StopIteration),
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub,
|
2009-07-22 09:23:21 -07:00
|
|
|
JS_ConvertStub, NULL,
|
2007-03-22 10:30:00 -07:00
|
|
|
NULL, NULL,
|
|
|
|
NULL, NULL,
|
|
|
|
NULL, stopiter_hasInstance,
|
|
|
|
NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
|
|
|
|
static void
|
|
|
|
generator_finalize(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
2009-08-04 14:06:55 -07:00
|
|
|
JSGenerator *gen = (JSGenerator *) obj->getPrivate();
|
|
|
|
if (!gen)
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
/*
|
|
|
|
* gen is open when a script has not called its close method while
|
|
|
|
* explicitly manipulating it.
|
|
|
|
*/
|
|
|
|
JS_ASSERT(gen->state == JSGEN_NEWBORN ||
|
|
|
|
gen->state == JSGEN_CLOSED ||
|
|
|
|
gen->state == JSGEN_OPEN);
|
|
|
|
cx->free(gen);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-04-16 23:53:37 -07:00
|
|
|
static void
|
|
|
|
generator_trace(JSTracer *trc, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-04 14:06:55 -07:00
|
|
|
JSGenerator *gen = (JSGenerator *) obj->getPrivate();
|
2007-08-04 21:54:34 -07:00
|
|
|
if (!gen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
2010-03-03 17:52:26 -08:00
|
|
|
* Do not mark if the generator is running; the contents may be trash and
|
|
|
|
* will be replaced when the generator stops.
|
2007-08-04 21:54:34 -07:00
|
|
|
*/
|
2010-03-03 17:52:26 -08:00
|
|
|
if (gen->state == JSGEN_RUNNING || gen->state == JSGEN_CLOSING)
|
|
|
|
return;
|
2007-08-04 21:54:34 -07:00
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
JSStackFrame *fp = gen->getFloatingFrame();
|
|
|
|
JS_ASSERT(gen->getLiveFrame() == fp);
|
|
|
|
TraceValues(trc, gen->floatingStack, fp->argEnd(), "generator slots");
|
|
|
|
js_TraceStackFrame(trc, fp);
|
|
|
|
TraceValues(trc, fp->slots(), gen->savedRegs.sp, "generator slots");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
JSExtendedClass js_GeneratorClass = {
|
|
|
|
{ js_Generator_str,
|
|
|
|
JSCLASS_HAS_PRIVATE |
|
|
|
|
JSCLASS_HAS_CACHED_PROTO(JSProto_Generator) |
|
|
|
|
JSCLASS_IS_ANONYMOUS |
|
|
|
|
JSCLASS_MARK_IS_TRACE |
|
|
|
|
JSCLASS_IS_EXTENDED,
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
|
|
|
|
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, generator_finalize,
|
|
|
|
NULL, NULL, NULL, NULL,
|
2010-05-07 17:52:52 -07:00
|
|
|
NULL, NULL, JS_CLASS_TRACE(generator_trace), NULL },
|
|
|
|
NULL, NULL, NULL, iterator_iterator,
|
|
|
|
NULL,
|
|
|
|
JSCLASS_NO_RESERVED_MEMBERS
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called from the JSOP_GENERATOR case in the interpreter, with fp referring
|
|
|
|
* to the frame by which the generator function was activated. Create a new
|
|
|
|
* JSGenerator object, which contains its own JSStackFrame that we populate
|
|
|
|
* from *fp. We know that upon return, the JSOP_GENERATOR opcode will return
|
|
|
|
* from the activation in fp, so we can steal away fp->callobj and fp->argsobj
|
|
|
|
* if they are non-null.
|
|
|
|
*/
|
2010-01-29 18:25:16 -08:00
|
|
|
JS_REQUIRES_STACK JSObject *
|
|
|
|
js_NewGenerator(JSContext *cx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-18 17:43:02 -07:00
|
|
|
JSObject *obj = NewBuiltinClassInstance(cx, &js_GeneratorClass.base);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!obj)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Load and compute stack slot counts. */
|
2010-01-29 18:25:16 -08:00
|
|
|
JSStackFrame *fp = cx->fp;
|
2010-03-03 17:52:26 -08:00
|
|
|
uintN argc = fp->argc;
|
|
|
|
uintN nargs = JS_MAX(argc, fp->fun->nargs);
|
|
|
|
uintN vplen = 2 + nargs;
|
|
|
|
|
|
|
|
/* Compute JSGenerator size. */
|
|
|
|
uintN nbytes = sizeof(JSGenerator) +
|
|
|
|
(-1 + /* one jsval included in JSGenerator */
|
|
|
|
vplen +
|
|
|
|
VALUES_PER_STACK_FRAME +
|
|
|
|
fp->script->nslots) * sizeof(jsval);
|
|
|
|
|
|
|
|
JSGenerator *gen = (JSGenerator *) cx->malloc(nbytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!gen)
|
2009-08-04 14:06:55 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
/* Cut up floatingStack space. */
|
|
|
|
jsval *vp = gen->floatingStack;
|
|
|
|
JSStackFrame *newfp = reinterpret_cast<JSStackFrame *>(vp + vplen);
|
|
|
|
jsval *slots = newfp->slots();
|
2010-05-13 02:21:27 -07:00
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
/* Initialize JSGenerator. */
|
|
|
|
gen->obj = obj;
|
|
|
|
gen->state = JSGEN_NEWBORN;
|
2010-03-03 18:10:13 -08:00
|
|
|
gen->savedRegs.pc = cx->regs->pc;
|
|
|
|
JS_ASSERT(cx->regs->sp == fp->slots() + fp->script->nfixed);
|
2010-03-03 17:52:26 -08:00
|
|
|
gen->savedRegs.sp = slots + fp->script->nfixed;
|
|
|
|
gen->vplen = vplen;
|
2010-06-03 21:41:01 -07:00
|
|
|
gen->enumerators = NULL;
|
2010-03-03 17:52:26 -08:00
|
|
|
gen->liveFrame = newfp;
|
|
|
|
|
|
|
|
/* Copy generator's stack frame copy in from |cx->fp|. */
|
|
|
|
newfp->imacpc = NULL;
|
|
|
|
newfp->callobj = fp->callobj;
|
|
|
|
if (fp->callobj) { /* Steal call object. */
|
|
|
|
fp->callobj->setPrivate(newfp);
|
2007-03-22 10:30:00 -07:00
|
|
|
fp->callobj = NULL;
|
|
|
|
}
|
2010-03-03 17:52:26 -08:00
|
|
|
newfp->argsobj = fp->argsobj;
|
|
|
|
if (fp->argsobj) { /* Steal args object. */
|
|
|
|
JSVAL_TO_OBJECT(fp->argsobj)->setPrivate(newfp);
|
2007-03-22 10:30:00 -07:00
|
|
|
fp->argsobj = NULL;
|
|
|
|
}
|
2010-03-03 17:52:26 -08:00
|
|
|
newfp->script = fp->script;
|
|
|
|
newfp->fun = fp->fun;
|
|
|
|
newfp->thisv = fp->thisv;
|
|
|
|
newfp->argc = fp->argc;
|
|
|
|
newfp->argv = vp + 2;
|
|
|
|
newfp->rval = fp->rval;
|
|
|
|
newfp->annotation = NULL;
|
|
|
|
newfp->scopeChain = fp->scopeChain;
|
Bug 480132: Clone lexical blocks only when needed. r=igor
Terminology:
A "script block" is an object of class Block allocated by the byte
compiler and associated with a script. Script blocks are never
modified, and may be used as a prototype for a "closure block":
A "closure block" is an object of class Block that holds variables
that have been closed over (although we actually leave the variables
on the stack until we leave their dynamic scope). A closure block is
a clone of a script block (its prototype is a script block).
Adjust the meanings of fp->blockChain and fp->scopeChain:
fp->blockChain is always the innermost script block in whose static
scope we're executing.
fp->scopeChain is the current scope chain, including 'call' objects
and closure blocks for those function calls and blocks in whose
static scope we are currently executing, and 'with' objects for with
statements; the chain is typically terminated by a global object.
However, as an optimization, the young end of the chain omits block
objects we have not yet needed to clone.
Closures need fully reified scope chains, so have js_GetScopeChain
reify any closure blocks missing from the young end of fp->scopeChain
by cloning script blocks as needed from fp->blockChain. Thus, if we
never actually close over a particular block, we never place a closure
block for it on fp->scopeChain.
Have JSOP_ENTERBLOCK and JSOP_LEAVEBLOCK always keep fp->blockChain
current. When JSOP_LEAVEBLOCK pops a block from fp->blockChain that
has been cloned on fp->scopeChain, pop fp->scopeChain as well.
Remove the JSFRAME_POP_BLOCKS flag, as it is no longer needed.
Ensure that the JIT won't have to create closure blocks or call
js_PutBlockObject; it can't handle those things yet. Note our current
script block when we begin recording. Abort recording if we leave
that block; we can't tell in advance whether it will need to be "put"
in future trace invocations. Leave trace if we call js_GetScopeChain
while in the static scope of lexical blocks. Remove JIT tests based
on JSFRAME_POP_BLOCKS.
Verify that generators capture the correct value for blockChain.
Add a constructor to JSAutoTempValueRooter for rooting JSObject
pointers.
2009-03-16 09:55:06 -07:00
|
|
|
JS_ASSERT(!fp->blockChain);
|
2010-03-03 17:52:26 -08:00
|
|
|
newfp->blockChain = NULL;
|
|
|
|
newfp->flags = fp->flags | JSFRAME_GENERATOR | JSFRAME_FLOATING_GENERATOR;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
/* Copy in arguments and slots. */
|
|
|
|
memcpy(vp, fp->argv - 2, vplen * sizeof(jsval));
|
|
|
|
memcpy(slots, fp->slots(), fp->script->nfixed * sizeof(jsval));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
obj->setPrivate(gen);
|
2007-03-22 10:30:00 -07:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
JSGenerator *
|
|
|
|
js_FloatingFrameToGenerator(JSStackFrame *fp)
|
|
|
|
{
|
|
|
|
JS_ASSERT(fp->isGenerator() && fp->isFloatingGenerator());
|
|
|
|
char *floatingStackp = (char *)(fp->argv - 2);
|
|
|
|
char *p = floatingStackp - offsetof(JSGenerator, floatingStack);
|
|
|
|
return reinterpret_cast<JSGenerator *>(p);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
typedef enum JSGeneratorOp {
|
|
|
|
JSGENOP_NEXT,
|
|
|
|
JSGENOP_SEND,
|
|
|
|
JSGENOP_THROW,
|
|
|
|
JSGENOP_CLOSE
|
|
|
|
} JSGeneratorOp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start newborn or restart yielding generator and perform the requested
|
|
|
|
* operation inside its frame.
|
|
|
|
*/
|
2009-01-30 15:40:05 -08:00
|
|
|
static JS_REQUIRES_STACK JSBool
|
2007-03-22 10:30:00 -07:00
|
|
|
SendToGenerator(JSContext *cx, JSGeneratorOp op, JSObject *obj,
|
2008-03-06 14:47:46 -08:00
|
|
|
JSGenerator *gen, jsval arg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-07-16 23:41:20 -07:00
|
|
|
if (gen->state == JSGEN_RUNNING || gen->state == JSGEN_CLOSING) {
|
|
|
|
js_ReportValueError(cx, JSMSG_NESTING_GENERATOR,
|
|
|
|
JSDVG_SEARCH_STACK, OBJECT_TO_JSVAL(obj),
|
2010-03-03 17:52:26 -08:00
|
|
|
JS_GetFunctionId(gen->getFloatingFrame()->fun));
|
2007-07-16 23:41:20 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
/* Check for OOM errors here, where we can fail easily. */
|
|
|
|
if (!cx->ensureGeneratorStackSpace())
|
|
|
|
return JS_FALSE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
JS_ASSERT(gen->state == JSGEN_NEWBORN || gen->state == JSGEN_OPEN);
|
|
|
|
switch (op) {
|
|
|
|
case JSGENOP_NEXT:
|
|
|
|
case JSGENOP_SEND:
|
|
|
|
if (gen->state == JSGEN_OPEN) {
|
|
|
|
/*
|
|
|
|
* Store the argument to send as the result of the yield
|
|
|
|
* expression.
|
|
|
|
*/
|
2008-03-17 01:58:28 -07:00
|
|
|
gen->savedRegs.sp[-1] = arg;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
gen->state = JSGEN_RUNNING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSGENOP_THROW:
|
|
|
|
JS_SetPendingException(cx, arg);
|
|
|
|
gen->state = JSGEN_RUNNING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
JS_ASSERT(op == JSGENOP_CLOSE);
|
|
|
|
JS_SetPendingException(cx, JSVAL_ARETURN);
|
|
|
|
gen->state = JSGEN_CLOSING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
JSStackFrame *genfp = gen->getFloatingFrame();
|
|
|
|
JSBool ok;
|
|
|
|
{
|
|
|
|
jsval *genVp = gen->floatingStack;
|
|
|
|
uintN vplen = gen->vplen;
|
|
|
|
uintN nfixed = genfp->script->nslots;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a pointer to new frame/slots. This memory is not "claimed", so
|
|
|
|
* the code before pushExecuteFrame must not reenter the interpreter.
|
|
|
|
*/
|
|
|
|
ExecuteFrameGuard frame;
|
|
|
|
if (!cx->stack().getExecuteFrame(cx, cx->fp, vplen, nfixed, frame)) {
|
|
|
|
gen->state = JSGEN_CLOSED;
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval *vp = frame.getvp();
|
|
|
|
JSStackFrame *fp = frame.getFrame();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy and rebase stack frame/args/slots. The "floating" flag must
|
|
|
|
* only be set on the generator's frame. See args_or_call_trace.
|
|
|
|
*/
|
|
|
|
uintN usedBefore = gen->savedRegs.sp - genVp;
|
|
|
|
memcpy(vp, genVp, usedBefore * sizeof(jsval));
|
|
|
|
fp->flags &= ~JSFRAME_FLOATING_GENERATOR;
|
|
|
|
fp->argv = vp + 2;
|
|
|
|
gen->savedRegs.sp = fp->slots() + (gen->savedRegs.sp - genfp->slots());
|
|
|
|
JS_ASSERT(uintN(gen->savedRegs.sp - fp->slots()) <= fp->script->nslots);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
JSObject *callobjBefore = fp->callobj;
|
|
|
|
jsval argsobjBefore = fp->argsobj;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Repoint Call, Arguments, Block and With objects to the new live
|
|
|
|
* frame. Call and Arguments are done directly because we have
|
|
|
|
* pointers to them. Block and With objects are done indirectly through
|
|
|
|
* 'liveFrame'. See js_LiveFrameToFloating comment in jsiter.h.
|
|
|
|
*/
|
|
|
|
if (genfp->callobj)
|
|
|
|
fp->callobj->setPrivate(fp);
|
|
|
|
if (genfp->argsobj)
|
|
|
|
JSVAL_TO_OBJECT(fp->argsobj)->setPrivate(fp);
|
|
|
|
gen->liveFrame = fp;
|
|
|
|
(void)cx->enterGenerator(gen); /* OOM check above. */
|
|
|
|
|
|
|
|
/* Officially push |fp|. |frame|'s destructor pops. */
|
2010-03-03 18:10:13 -08:00
|
|
|
cx->stack().pushExecuteFrame(cx, frame, gen->savedRegs, NULL);
|
2010-03-03 17:52:26 -08:00
|
|
|
|
2010-06-03 21:41:01 -07:00
|
|
|
/* Swap the enumerators stack for the generator's stack. */
|
|
|
|
JSObject *enumerators = cx->enumerators;
|
|
|
|
cx->enumerators = gen->enumerators;
|
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
ok = js_Interpret(cx);
|
|
|
|
|
2010-06-03 21:41:01 -07:00
|
|
|
/* Restore the original enumerators stack. */
|
|
|
|
gen->enumerators = cx->enumerators;
|
|
|
|
cx->enumerators = enumerators;
|
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
/* Restore call/args/block objects. */
|
|
|
|
cx->leaveGenerator(gen);
|
|
|
|
gen->liveFrame = genfp;
|
|
|
|
if (fp->argsobj)
|
|
|
|
JSVAL_TO_OBJECT(fp->argsobj)->setPrivate(genfp);
|
|
|
|
if (fp->callobj)
|
|
|
|
fp->callobj->setPrivate(genfp);
|
|
|
|
|
|
|
|
JS_ASSERT_IF(argsobjBefore, argsobjBefore == fp->argsobj);
|
|
|
|
JS_ASSERT_IF(callobjBefore, callobjBefore == fp->callobj);
|
|
|
|
|
|
|
|
/* Copy and rebase stack frame/args/slots. Restore "floating" flag. */
|
|
|
|
JS_ASSERT(uintN(gen->savedRegs.sp - fp->slots()) <= fp->script->nslots);
|
|
|
|
uintN usedAfter = gen->savedRegs.sp - vp;
|
|
|
|
memcpy(genVp, vp, usedAfter * sizeof(jsval));
|
|
|
|
genfp->flags |= JSFRAME_FLOATING_GENERATOR;
|
|
|
|
genfp->argv = genVp + 2;
|
|
|
|
gen->savedRegs.sp = genfp->slots() + (gen->savedRegs.sp - fp->slots());
|
|
|
|
JS_ASSERT(uintN(gen->savedRegs.sp - genfp->slots()) <= genfp->script->nslots);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gen->getFloatingFrame()->flags & JSFRAME_YIELDING) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Yield cannot fail, throw or be called on closing. */
|
|
|
|
JS_ASSERT(ok);
|
|
|
|
JS_ASSERT(!cx->throwing);
|
|
|
|
JS_ASSERT(gen->state == JSGEN_RUNNING);
|
|
|
|
JS_ASSERT(op != JSGENOP_CLOSE);
|
2010-03-03 17:52:26 -08:00
|
|
|
genfp->flags &= ~JSFRAME_YIELDING;
|
2007-03-22 10:30:00 -07:00
|
|
|
gen->state = JSGEN_OPEN;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-03 17:52:26 -08:00
|
|
|
genfp->rval = JSVAL_VOID;
|
2007-03-22 10:30:00 -07:00
|
|
|
gen->state = JSGEN_CLOSED;
|
|
|
|
if (ok) {
|
|
|
|
/* Returned, explicitly or by falling off the end. */
|
2008-03-06 14:47:46 -08:00
|
|
|
if (op == JSGENOP_CLOSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
2007-09-30 17:31:22 -07:00
|
|
|
return js_ThrowStopIteration(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-01-03 01:28:40 -08:00
|
|
|
* An error, silent termination by operation callback or an exception.
|
2007-03-22 10:30:00 -07:00
|
|
|
* Propagate the condition to the caller.
|
|
|
|
*/
|
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
|
2009-01-30 15:40:05 -08:00
|
|
|
static JS_REQUIRES_STACK JSBool
|
2007-07-13 00:28:47 -07:00
|
|
|
CloseGenerator(JSContext *cx, JSObject *obj)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-05-07 17:52:52 -07:00
|
|
|
JS_ASSERT(obj->getClass() == &js_GeneratorClass.base);
|
2009-08-04 14:06:55 -07:00
|
|
|
|
|
|
|
JSGenerator *gen = (JSGenerator *) obj->getPrivate();
|
2007-07-02 05:13:23 -07:00
|
|
|
if (!gen) {
|
|
|
|
/* Generator prototype object. */
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gen->state == JSGEN_CLOSED)
|
|
|
|
return JS_TRUE;
|
|
|
|
|
2008-03-06 14:47:46 -08:00
|
|
|
return SendToGenerator(cx, JSGENOP_CLOSE, obj, gen, JSVAL_VOID);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common subroutine of generator_(next|send|throw|close) methods.
|
|
|
|
*/
|
|
|
|
static JSBool
|
2008-08-08 09:02:50 -07:00
|
|
|
generator_op(JSContext *cx, JSGeneratorOp op, jsval *vp, uintN argc)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-01 21:33:52 -07:00
|
|
|
JSObject *obj;
|
2007-03-22 10:30:00 -07:00
|
|
|
jsval arg;
|
|
|
|
|
2010-01-22 14:49:18 -08:00
|
|
|
LeaveTrace(cx);
|
2009-01-30 15:40:05 -08:00
|
|
|
|
2008-02-17 16:12:33 -08:00
|
|
|
obj = JS_THIS_OBJECT(cx, vp);
|
2010-05-07 17:52:52 -07:00
|
|
|
if (!JS_InstanceOf(cx, obj, &js_GeneratorClass.base, vp + 2))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
|
2009-08-04 14:06:55 -07:00
|
|
|
JSGenerator *gen = (JSGenerator *) obj->getPrivate();
|
|
|
|
if (!gen) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* This happens when obj is the generator prototype. See bug 352885. */
|
|
|
|
goto closed_generator;
|
|
|
|
}
|
|
|
|
|
2007-07-16 23:41:20 -07:00
|
|
|
if (gen->state == JSGEN_NEWBORN) {
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (op) {
|
|
|
|
case JSGENOP_NEXT:
|
|
|
|
case JSGENOP_THROW:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JSGENOP_SEND:
|
2008-08-08 09:02:50 -07:00
|
|
|
if (argc >= 1 && !JSVAL_IS_VOID(vp[2])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
js_ReportValueError(cx, JSMSG_BAD_GENERATOR_SEND,
|
2007-08-01 21:33:52 -07:00
|
|
|
JSDVG_SEARCH_STACK, vp[2], NULL);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
JS_ASSERT(op == JSGENOP_CLOSE);
|
|
|
|
gen->state = JSGEN_CLOSED;
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
2007-07-16 23:41:20 -07:00
|
|
|
} else if (gen->state == JSGEN_CLOSED) {
|
2007-03-22 10:30:00 -07:00
|
|
|
closed_generator:
|
|
|
|
switch (op) {
|
|
|
|
case JSGENOP_NEXT:
|
|
|
|
case JSGENOP_SEND:
|
2007-09-30 17:31:22 -07:00
|
|
|
return js_ThrowStopIteration(cx);
|
2007-03-22 10:30:00 -07:00
|
|
|
case JSGENOP_THROW:
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_SetPendingException(cx, argc >= 1 ? vp[2] : JSVAL_VOID);
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
|
|
|
default:
|
|
|
|
JS_ASSERT(op == JSGENOP_CLOSE);
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-08 09:02:50 -07:00
|
|
|
arg = ((op == JSGENOP_SEND || op == JSGENOP_THROW) && argc != 0)
|
2007-08-01 21:33:52 -07:00
|
|
|
? vp[2]
|
2007-03-22 10:30:00 -07:00
|
|
|
: JSVAL_VOID;
|
2008-03-06 14:47:46 -08:00
|
|
|
if (!SendToGenerator(cx, op, obj, gen, arg))
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_FALSE;
|
2010-03-03 17:52:26 -08:00
|
|
|
*vp = gen->getFloatingFrame()->rval;
|
2007-03-22 10:30:00 -07:00
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
generator_send(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
return generator_op(cx, JSGENOP_SEND, vp, argc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
generator_next(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
return generator_op(cx, JSGENOP_NEXT, vp, argc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
generator_throw(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
return generator_op(cx, JSGENOP_THROW, vp, argc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSBool
|
2007-08-01 21:33:52 -07:00
|
|
|
generator_close(JSContext *cx, uintN argc, jsval *vp)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-08 09:02:50 -07:00
|
|
|
return generator_op(cx, JSGENOP_CLOSE, vp, argc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSFunctionSpec generator_methods[] = {
|
2008-08-08 09:02:50 -07:00
|
|
|
JS_FN(js_next_str, generator_next, 0,JSPROP_ROPERM),
|
|
|
|
JS_FN(js_send_str, generator_send, 1,JSPROP_ROPERM),
|
|
|
|
JS_FN(js_throw_str, generator_throw, 1,JSPROP_ROPERM),
|
|
|
|
JS_FN(js_close_str, generator_close, 0,JSPROP_ROPERM),
|
2007-08-01 21:33:52 -07:00
|
|
|
JS_FS_END
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* JS_HAS_GENERATORS */
|
|
|
|
|
|
|
|
JSObject *
|
|
|
|
js_InitIteratorClasses(JSContext *cx, JSObject *obj)
|
|
|
|
{
|
|
|
|
JSObject *proto, *stop;
|
|
|
|
|
|
|
|
/* Idempotency required: we initialize several things, possibly lazily. */
|
|
|
|
if (!js_GetClassObject(cx, obj, JSProto_StopIteration, &stop))
|
|
|
|
return NULL;
|
|
|
|
if (stop)
|
|
|
|
return stop;
|
|
|
|
|
2010-05-07 17:52:52 -07:00
|
|
|
proto = JS_InitClass(cx, obj, NULL, &js_IteratorClass.base, Iterator, 2,
|
2007-03-22 10:30:00 -07:00
|
|
|
NULL, iterator_methods, NULL, NULL);
|
|
|
|
if (!proto)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#if JS_HAS_GENERATORS
|
|
|
|
/* Initialize the generator internals if configured. */
|
2010-05-07 17:52:52 -07:00
|
|
|
if (!JS_InitClass(cx, obj, NULL, &js_GeneratorClass.base, NULL, 0,
|
2007-03-22 10:30:00 -07:00
|
|
|
NULL, generator_methods, NULL, NULL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return JS_InitClass(cx, obj, NULL, &js_StopIterationClass, NULL, 0,
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
}
|