Add header.
[vlendec/samba-autobuild/.git] / source4 / lib / appweb / ejs-2.0 / ejs / classes / ejsArray.c
1 /*
2  *      @file   ejsArray.c
3  *      @brief  Array class
4  */
5 /********************************* Copyright **********************************/
6 /*
7  *      @copy   default
8  *      
9  *      Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
10  *      Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
11  *      
12  *      This software is distributed under commercial and open source licenses.
13  *      You may use the GPL open source license described below or you may acquire 
14  *      a commercial license from Mbedthis Software. You agree to be fully bound 
15  *      by the terms of either license. Consult the LICENSE.TXT distributed with 
16  *      this software for full details.
17  *      
18  *      This software is open source; you can redistribute it and/or modify it 
19  *      under the terms of the GNU General Public License as published by the 
20  *      Free Software Foundation; either version 2 of the License, or (at your 
21  *      option) any later version. See the GNU General Public License for more 
22  *      details at: http://www.mbedthis.com/downloads/gplLicense.html
23  *      
24  *      This program is distributed WITHOUT ANY WARRANTY; without even the 
25  *      implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
26  *      
27  *      This GPL license does NOT permit incorporating this software into 
28  *      proprietary programs. If you are unable to comply with the GPL, you must
29  *      acquire a commercial license to use this software. Commercial licenses 
30  *      for this software and support services are available from Mbedthis 
31  *      Software at http://www.mbedthis.com 
32  *      
33  *      @end
34  */
35 /********************************** Includes **********************************/
36
37 #include        "ejs.h"
38
39 #if BLD_FEATURE_EJS
40
41 /************************************ Code ************************************/
42
43 int ejsDefineArrayClass(Ejs *ep)
44 {
45         if (ejsDefineClass(ep, "Array", "Object", ejsArrayConstructor) == 0) {
46                 return MPR_ERR_CANT_INITIALIZE;
47         }
48         return 0;
49 }
50
51 /******************************************************************************/
52 /*
53  *      Routine to create the base array type
54  */
55
56 EjsVar *ejsCreateArrayInternal(EJS_LOC_DEC(ep, loc), int size)
57 {
58         EjsProperty     *pp;
59         EjsVar          *obj, *vp;
60
61         /* MOB -- need to supply hash size -- max(size, 503)); */
62
63         obj = ejsCreateSimpleObjInternal(EJS_LOC_PASS(ep, loc), "Array");
64         if (obj == 0) {
65                 mprAssert(0);
66                 return obj;
67         }
68         obj->isArray = 1;
69
70         /*      MOB -- call constructor here and replace this code */
71
72         pp = ejsSetPropertyToInteger(ep, obj, "length", size);
73         ejsMakePropertyEnumerable(pp, 0);
74
75         vp = ejsGetVarPtr(pp);
76         vp->isArrayLength = 1;
77
78         return obj;
79 }
80
81 /******************************************************************************/
82
83 EjsVar *ejsAddArrayElt(Ejs *ep, EjsVar *op, EjsVar *element, 
84         EjsCopyDepth copyDepth)
85 {
86         EjsProperty             *pp;
87         EjsVar                  *vp;
88         char                    idx[16];
89         int                             length;
90
91         mprAssert(op->isArray);
92
93         length = ejsGetPropertyAsInteger(ep, op, "length");
94
95         mprItoa(idx, sizeof(idx), length);
96         pp = ejsCreateProperty(ep, op, idx);
97         vp = ejsGetVarPtr(pp);
98
99         ejsWriteVar(ep, vp, element, copyDepth);
100
101         ejsSetPropertyToInteger(ep, op, "length", length + 1);
102
103         return vp;
104 }
105
106 /******************************************************************************/
107 /*
108  *      Constructor
109  */
110
111 int ejsArrayConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
112 {
113         EjsProperty             *pp;
114         EjsVar                  *vp;
115         char                    idx[16];
116         int                             i, max;
117
118         thisObj->isArray = 1;
119         max = 0;
120
121         if (argc > 0) {
122                 if (argc == 1 && ejsVarIsNumber(argv[0])) {
123                         /*
124                          *      x = new Array(size);
125                          */
126                         max = (int) ejsVarToInteger(argv[0]);
127
128                 } else {
129                         /*
130                          *      x = new Array(element0, element1, ..., elementN):
131                          */
132                         max = argc;
133                         for (i = 0; i < max; i++) {
134                                 mprItoa(idx, sizeof(idx), i);
135                                 pp = ejsCreateSimpleProperty(ep, thisObj, idx);
136                                 vp = ejsGetVarPtr(pp);
137                                 ejsWriteVar(ep, vp, argv[i], EJS_SHALLOW_COPY);
138                         }
139                 }
140         }
141
142         pp = ejsCreateSimpleProperty(ep, thisObj, "length");
143         ejsMakePropertyEnumerable(pp, 0);
144         vp = ejsGetVarPtr(pp);
145         ejsWriteVarAsInteger(ep, vp, max);
146         vp->isArrayLength = 1;
147
148         return 0;
149 }
150
151 /******************************************************************************/
152
153 #else
154 void ejsArrayDummy() {}
155
156 /******************************************************************************/
157 #endif /* BLD_FEATURE_EJS */
158
159 /*
160  * Local variables:
161  * tab-width: 4
162  * c-basic-offset: 4
163  * End:
164  * vim:tw=78
165  * vim600: sw=4 ts=4 fdm=marker
166  * vim<600: sw=4 ts=4
167  */