File: array_object.cpp

package info (click to toggle)
kdelibs 4:2.2.2-13.woody.14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 36,832 kB
  • ctags: 40,077
  • sloc: cpp: 313,284; ansic: 20,558; xml: 11,448; sh: 11,318; makefile: 2,426; perl: 2,084; yacc: 1,663; java: 1,538; lex: 629; python: 300
file content (341 lines) | stat: -rw-r--r-- 9,750 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "kjs.h"
#include "operations.h"
#include "types.h"
#include "array_object.h"
#include <stdio.h>

using namespace KJS;

ArrayObject::ArrayObject(const Object &funcProto,
			 const Object &arrayProto)
    : ConstructorImp(funcProto, 1)
{
  // ECMA 15.4.3.1 Array.prototype
  setPrototypeProperty(arrayProto);
}

// ECMA 15.6.1
Completion ArrayObject::execute(const List &args)
{
  // equivalent to 'new Array(....)'
  KJSO result = construct(args);

  return Completion(ReturnValue, result);
}

// ECMA 15.6.2
Object ArrayObject::construct(const List &args)
{
  Object result = Object::create(ArrayClass);

  unsigned int len;
  ListIterator it = args.begin();
  // a single argument might denote the array size
  if (args.size() == 1 && it->isA(NumberType))
    len = it->toUInt32();
  else {
    // initialize array
    len = args.size();
    for (unsigned int u = 0; it != args.end(); it++, u++)
      result.put(UString::from(u), *it);
  }

  // array size
  result.put("length", len, DontEnum | DontDelete);

  return result;
}

// ECMA 15.6.4
ArrayPrototype::ArrayPrototype(const Object& proto)
  : ObjectImp(ArrayClass, Null(), proto)
{
  // The constructor will be added later in ArrayObject's constructor

  put("length", 0u, DontEnum | DontDelete);
}

KJSO ArrayPrototype::get(const UString &p) const
{
  int id;
  if(p == "toString")
    id = ArrayProtoFunc::ToString;
  else if(p == "toLocaleString")
    id = ArrayProtoFunc::ToLocaleString;
  else if(p == "concat")
    id = ArrayProtoFunc::Concat;
  else if (p == "join")
    id = ArrayProtoFunc::Join;
  else if(p == "pop")
    id = ArrayProtoFunc::Pop;
  else if(p == "push")
    id = ArrayProtoFunc::Push;
  else if(p == "reverse")
    id = ArrayProtoFunc::Reverse;
  else if(p == "shift")
    id = ArrayProtoFunc::Shift;
  else if(p == "slice")
    id = ArrayProtoFunc::Slice;
  else if(p == "sort")
    id = ArrayProtoFunc::Sort;
  else if(p == "splice")
    id = ArrayProtoFunc::Splice;
  else if(p == "unshift")
    id = ArrayProtoFunc::UnShift;
  else
    return Imp::get(p);

  return Function(new ArrayProtoFunc(id));
}

// ECMA 15.4.4
Completion ArrayProtoFunc::execute(const List &args)
{
  KJSO result, obj, obj2;
  Object thisObj = Object::dynamicCast(thisValue());
  unsigned int length = thisObj.get("length").toUInt32();
  unsigned int middle;
  UString str = "", str2;
  UString separator = ",";

  switch (id) {
  case ToLocaleString:
    /* TODO */
    // fall trough
  case ToString:
    if (!thisObj.getClass() == ArrayClass) {
      result = Error::create(TypeError);
      break;
    }
    // fall trough
  case Join:
    {
      if (!args[0].isA(UndefinedType))
	separator = args[0].toString().value();
      for (unsigned int k = 0; k < length; k++) {
	if (k >= 1)
	  str += separator;
	obj = thisObj.get(UString::from(k));
	if (!obj.isA(UndefinedType) && !obj.isA(NullType))
	  str += obj.toString().value();
      }
    }
    result = String(str);
    break;
  case Concat: {
    result = Object::create(ArrayClass);
    int n = 0;
    obj = thisObj;
    ListIterator it = args.begin();
    for (;;) {
      if (obj.isA(ObjectType) &&
	  static_cast<Object&>(obj).getClass() == ArrayClass) {
	unsigned int k = 0;
	if (n > 0)
	  length = obj.get("length").toUInt32();
	while (k < length) {
	  UString p = UString::from(k);
	  if (obj.hasProperty(p))
	    result.put(UString::from(n), obj.get(p));
	  n++;
	  k++;
	}
      } else {
	result.put(UString::from(n), obj);
	n++;      
      }
      if (it == args.end())
	break;
      obj = it++;
    }
    result.put("length", Number(n), DontEnum | DontDelete);
  }
    break;
  case Pop:
    if (length == 0) {
      thisObj.put("length", Number(length), DontEnum | DontDelete);
      result = Undefined();
    } else {
      str = UString::from(length - 1);
      result = thisObj.get(str);
      thisObj.deleteProperty(str);
      thisObj.put("length", length - 1, DontEnum | DontDelete);
    }
    break;
  case Push:
    {
      for (int n = 0; n < args.size(); n++)
	thisObj.put(UString::from(length + n), args[n]);
      length += args.size();
      thisObj.put("length", length, DontEnum | DontDelete);
      result = Number(length);
    }
    break;
  case Reverse:
    {
      middle = length / 2;
      for (unsigned int k = 0; k < middle; k++) {
	str = UString::from(k);
	str2 = UString::from(length - k - 1);
	obj = thisObj.get(str);
	obj2 = thisObj.get(str2);
	if (thisObj.hasProperty(str2)) {
	  if (thisObj.hasProperty(str)) {
	    thisObj.put(str, obj2);
	    thisObj.put(str2, obj);
	  } else {
	    thisObj.put(str, obj2);
	    thisObj.deleteProperty(str2);
	  }
	} else {
	  if (thisObj.hasProperty(str)) {
	    thisObj.deleteProperty(str);
	    thisObj.put(str2, obj);
	  } else {
	    // why delete something that's not there ? Strange.
	    thisObj.deleteProperty(str);
	    thisObj.deleteProperty(str2);
	  }
	}
      }
    }
    result = thisObj;
    break;
  case Shift:
    if (length == 0) {
      thisObj.put("length", Number(length), DontEnum | DontDelete);
      result = Undefined();
    } else {
      result = thisObj.get("0");
      for(unsigned int k = 1; k < length; k++) {
	str = UString::from(k);
	str2 = UString::from(k-1);
	if (thisObj.hasProperty(str)) {
	  obj = thisObj.get(str);
	  thisObj.put(str2, obj);
	} else
	  thisObj.deleteProperty(str2);
      }
      thisObj.deleteProperty(UString::from(length - 1));
      thisObj.put("length", length - 1, DontEnum | DontDelete);
    }
    break;
  case Slice: // http://developer.netscape.com/docs/manuals/js/client/jsref/array.htm#1193713
    {
        result = Object::create(ArrayClass); // We return a new array
        int begin = args[0].toUInt32();
        int end = length;
        if (!args[1].isA(UndefinedType))
        {
          end = args[1].toUInt32();
          if ( end < 0 )
            end += length;
        }
        // safety tests
        if ( begin < 0 || end < 0 || begin >= end ) {
            result.put("length", Number(0), DontEnum | DontDelete);
            break;
        }
        //printf( "Slicing from %d to %d \n", begin, end );
        for(unsigned int k = 0; k < (unsigned int) end-begin; k++) {
            str = UString::from(k+begin);
            str2 = UString::from(k);
            if (thisObj.hasProperty(str)) {
                obj = thisObj.get(str);
                result.put(str2, obj);
            }
        }
        result.put("length", end - begin, DontEnum | DontDelete);
        break;
    }
  case Sort:
    {
#if 0
        printf("KJS Array::Sort length=%d\n", length);
        for ( unsigned int i = 0 ; i<length ; ++i )
            printf("KJS Array::Sort: %d: %s\n", i, thisObj.get(UString::from(i)).toString().value().ascii() );
#endif
        Object sortFunction;
        bool useSortFunction = !args[0].isA(UndefinedType);
        if (useSortFunction)
        {
            sortFunction = args[0].toObject();
            if (!sortFunction.implementsCall())
                useSortFunction = false;
        }

        if (length == 0) {
            thisObj.put("length", Number(0), DontEnum | DontDelete);
            result = Undefined();
            break;
        }

        // "Min" sort. Not the fastest, but definitely less code than heapsort
        // or quicksort, and much less swapping than bubblesort/insertionsort.
        for ( unsigned int i = 0 ; i<length-1 ; ++i )
        {
            KJSO iObj = thisObj.get(UString::from(i));
            unsigned int themin = i;
            KJSO minObj = iObj;
            for ( unsigned int j = i+1 ; j<length ; ++j )
            {
                KJSO jObj = thisObj.get(UString::from(j));
                int cmp;
                if ( useSortFunction )
                {
                    List l;
                    l.append(jObj);
                    l.append(minObj);
                    cmp = sortFunction.executeCall( Global::current(), &l ).toInt32();
                }
                else
                    cmp = ( jObj.toString().value() < minObj.toString().value() ) ? -1 : 1;
                if ( cmp < 0 )
                {
                    themin = j;
                    minObj = jObj;
                }
            }
            // Swap themin and i
            if ( themin > i )
            {
                //printf("KJS Array::Sort: swapping %d and %d\n", i, themin );
                thisObj.put( UString::from(i), minObj );
                thisObj.put( UString::from(themin), iObj );
            }
        }
#if 0
        printf("KJS Array::Sort -- Resulting array:\n");
        for ( unsigned int i = 0 ; i<length ; ++i )
            printf("KJS Array::Sort: %d: %s\n", i, thisObj.get(UString::from(i)).toString().value().ascii() );
#endif
        result = thisObj;
        break;
    }
  // TODO Splice
  // TODO Unshift
  default:
    result = Undefined();
  }

  return Completion(ReturnValue, result);
}