1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sw=4 et tw=78:
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1 *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Mozilla Communicator client code, released
17  * March 31, 1998.
18  *
19  * The Initial Developer of the Original Code is
20  * Netscape Communications Corporation.
21  * Portions created by the Initial Developer are Copyright (C) 1998
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either of the GNU General Public License Version 2 or later (the "GPL"),
28  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 #ifndef jsiter_h___
41 #define jsiter_h___
42 /*
43  * JavaScript iterators.
44  */
45 #include "jsprvtd.h"
46 #include "jspubtd.h"
47 
48 #define JSITER_ENUMERATE  0x1   /* for-in compatible hidden default iterator */
49 #define JSITER_FOREACH    0x2   /* return [key, value] pair rather than key */
50 #define JSITER_KEYVALUE   0x4   /* destructuring for-in wants [key, value] */
51 
52 extern void
53 js_CloseNativeIterator(JSContext *cx, JSObject *iterobj);
54 
55 extern void
56 js_CloseIteratorState(JSContext *cx, JSObject *iterobj);
57 
58 /*
59  * Convert the value stored in *vp to its iteration object. The flags should
60  * contain JSITER_ENUMERATE if js_ValueToIterator is called when enumerating
61  * for-in semantics are required, and when the caller can guarantee that the
62  * iterator will never be exposed to scripts.
63  */
64 extern JSBool
65 js_ValueToIterator(JSContext *cx, uintN flags, jsval *vp);
66 
67 /*
68  * Given iterobj, call iterobj.next().  If the iterator stopped, set *rval to
69  * JSVAL_HOLE. Otherwise set it to the result of the next call.
70  */
71 extern JSBool
72 js_CallIteratorNext(JSContext *cx, JSObject *iterobj, jsval *rval);
73 
74 #if JS_HAS_GENERATORS
75 
76 /*
77  * Generator state codes.
78  */
79 typedef enum JSGeneratorState {
80     JSGEN_NEWBORN,  /* not yet started */
81     JSGEN_OPEN,     /* started by a .next() or .send(undefined) call */
82     JSGEN_RUNNING,  /* currently executing via .next(), etc., call */
83     JSGEN_CLOSING,  /* close method is doing asynchronous return */
84     JSGEN_CLOSED    /* closed, cannot be started or closed again */
85 } JSGeneratorState;
86 
87 struct JSGenerator {
88     JSGenerator         *next;
89     JSObject            *obj;
90     JSGeneratorState    state;
91     JSStackFrame        frame;
92     JSArena             arena;
93     jsval               stack[1];
94 };
95 
96 #define FRAME_TO_GENERATOR(fp) \
97     ((JSGenerator *) ((uint8 *)(fp) - offsetof(JSGenerator, frame)))
98 
99 extern JSObject *
100 js_NewGenerator(JSContext *cx, JSStackFrame *fp);
101 
102 extern JSBool
103 js_CloseGeneratorObject(JSContext *cx, JSGenerator *gen);
104 
105 #endif
106 
107 extern JSClass          js_GeneratorClass;
108 extern JSClass          js_IteratorClass;
109 extern JSClass          js_StopIterationClass;
110 
111 extern JSObject *
112 js_InitIteratorClasses(JSContext *cx, JSObject *obj);
113 
114 #endif /* jsiter_h___ */
115