5 /********************************* Copyright **********************************/
9 * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved.
10 * Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved.
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.
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
24 * This program is distributed WITHOUT ANY WARRANTY; without even the
25 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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
35 /********************************** Includes **********************************/
41 /************************************ Code ************************************/
43 int ejsDefineArrayClass(Ejs *ep)
45 if (ejsDefineClass(ep, "Array", "Object", ejsArrayConstructor) == 0) {
46 return MPR_ERR_CANT_INITIALIZE;
51 /******************************************************************************/
53 * Routine to create the base array type
56 EjsVar *ejsCreateArrayInternal(EJS_LOC_DEC(ep, loc), int size)
61 /* MOB -- need to supply hash size -- max(size, 503)); */
63 obj = ejsCreateSimpleObjInternal(EJS_LOC_PASS(ep, loc), "Array");
70 /* MOB -- call constructor here and replace this code */
72 pp = ejsSetPropertyToInteger(ep, obj, "length", size);
73 ejsMakePropertyEnumerable(pp, 0);
75 vp = ejsGetVarPtr(pp);
76 vp->isArrayLength = 1;
81 /******************************************************************************/
83 EjsVar *ejsAddArrayElt(Ejs *ep, EjsVar *op, EjsVar *element,
84 EjsCopyDepth copyDepth)
91 mprAssert(op->isArray);
93 length = ejsGetPropertyAsInteger(ep, op, "length");
95 mprItoa(idx, sizeof(idx), length);
96 pp = ejsCreateProperty(ep, op, idx);
97 vp = ejsGetVarPtr(pp);
99 ejsWriteVar(ep, vp, element, copyDepth);
101 ejsSetPropertyToInteger(ep, op, "length", length + 1);
106 /******************************************************************************/
111 int ejsArrayConstructor(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
118 thisObj->isArray = 1;
122 if (argc == 1 && ejsVarIsNumber(argv[0])) {
124 * x = new Array(size);
126 max = (int) ejsVarToInteger(argv[0]);
130 * x = new Array(element0, element1, ..., elementN):
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);
142 pp = ejsCreateSimpleProperty(ep, thisObj, "length");
143 ejsMakePropertyEnumerable(pp, 0);
144 vp = ejsGetVarPtr(pp);
145 ejsWriteVarAsInteger(ep, vp, max);
146 vp->isArrayLength = 1;
151 /******************************************************************************/
154 void ejsArrayDummy() {}
156 /******************************************************************************/
157 #endif /* BLD_FEATURE_EJS */
165 * vim600: sw=4 ts=4 fdm=marker