0
|
1
|
|
2 /* Digital Mars DMDScript source code.
|
|
3 * Copyright (c) 2000-2002 by Chromium Communications
|
|
4 * D version Copyright (c) 2004-2005 by Digital Mars
|
|
5 * All Rights Reserved
|
|
6 * written by Walter Bright
|
|
7 * www.digitalmars.com
|
|
8 * Use at your own risk. There is no warranty, express or implied.
|
|
9 * License for redistribution is by the GNU General Public License in gpl.txt.
|
|
10 *
|
|
11 * A binary, non-exclusive license for commercial use can be
|
|
12 * purchased from www.digitalmars.com/dscript/buy.html.
|
|
13 *
|
|
14 * DMDScript is implemented in the D Programming Language,
|
|
15 * www.digitalmars.com/d/
|
|
16 *
|
|
17 * For a C++ implementation of DMDScript, including COM support,
|
|
18 * see www.digitalmars.com/dscript/cppscript.html.
|
|
19 */
|
|
20
|
|
21
|
3
|
22 module dmdscript_tango.darray;
|
0
|
23
|
4
|
24 //import std.string;
|
|
25 //import std.c.stdlib;
|
|
26 import tango.stdc.stdlib;
|
|
27 import tango.text.Text;
|
0
|
28
|
3
|
29 import dmdscript_tango.script;
|
|
30 import dmdscript_tango.value;
|
|
31 import dmdscript_tango.dobject;
|
|
32 import dmdscript_tango.threadcontext;
|
|
33 import dmdscript_tango.identifier;
|
|
34 import dmdscript_tango.dfunction;
|
|
35 import dmdscript_tango.text;
|
|
36 import dmdscript_tango.property;
|
|
37 import dmdscript_tango.textgen.errmsgs;
|
|
38 import dmdscript_tango.dnative;
|
|
39 import dmdscript_tango.program;
|
0
|
40
|
|
41 /* ===================== Darray_constructor ==================== */
|
|
42
|
|
43 class Darray_constructor : Dfunction
|
|
44 {
|
|
45 this(ThreadContext *tc)
|
|
46 {
|
|
47 super(1, tc.Dfunction_prototype);
|
|
48 name = "Array";
|
|
49 }
|
|
50
|
|
51 void* Construct(CallContext *cc, Value *ret, Value[] arglist)
|
|
52 {
|
|
53 // ECMA 15.4.2
|
|
54 Darray a;
|
|
55
|
|
56 a = new Darray();
|
|
57 if (arglist.length == 0)
|
|
58 {
|
|
59 a.ulength = 0;
|
|
60 a.length.number = 0;
|
|
61 }
|
|
62 else if (arglist.length == 1)
|
|
63 {
|
|
64 Value* v = &arglist[0];
|
|
65
|
|
66 if (v.isNumber())
|
|
67 {
|
|
68 d_uint32 len;
|
|
69
|
|
70 len = v.toUint32();
|
|
71 if (cast(double) len != v.number)
|
|
72 { ErrInfo errinfo;
|
|
73
|
|
74 ret.putVundefined();
|
|
75 return RangeError(&errinfo, ERR_ARRAY_LEN_OUT_OF_BOUNDS, v.number);
|
|
76 }
|
|
77 else
|
|
78 { a.ulength = len;
|
|
79 a.length.number = len;
|
|
80 /+
|
|
81 if (len > 16)
|
|
82 {
|
|
83 //writef("setting %p dimension to %d\n", &a.proptable, len);
|
|
84 if (len > 10000)
|
|
85 len = 10000; // cap so we don't run out of memory
|
|
86 a.proptable.roots.setDim(len);
|
|
87 a.proptable.roots.zero();
|
|
88 }
|
|
89 +/
|
|
90 }
|
|
91 }
|
|
92 else
|
|
93 {
|
|
94 a.ulength = 1;
|
|
95 a.length.number = 1;
|
|
96 a.Put(cast(d_uint32) 0, v, 0);
|
|
97 }
|
|
98 }
|
|
99 else
|
|
100 {
|
|
101 //if (arglist.length > 10) writef("Array constructor: arglist.length = %d\n", arglist.length);
|
|
102 /+
|
|
103 if (arglist.length > 16)
|
|
104 {
|
|
105 a.proptable.roots.setDim(arglist.length);
|
|
106 a.proptable.roots.zero();
|
|
107 }
|
|
108 +/
|
|
109 a.ulength = arglist.length;
|
|
110 a.length.number = arglist.length;
|
|
111 for (uint k = 0; k < arglist.length; k++)
|
|
112 {
|
|
113 a.Put(k, &arglist[k], 0);
|
|
114 }
|
|
115 }
|
|
116 Value.copy(ret, &a.value);
|
|
117 //writef("Darray_constructor.Construct(): length = %g\n", a.length.number);
|
|
118 return null;
|
|
119 }
|
|
120
|
|
121 void* Call(CallContext *cc, Dobject othis, Value* ret, Value[] arglist)
|
|
122 {
|
|
123 // ECMA 15.4.1
|
|
124 return Construct(cc, ret, arglist);
|
|
125 }
|
|
126 }
|
|
127
|
|
128
|
|
129 /* ===================== Darray_prototype_toString ================= */
|
|
130
|
|
131 void *Darray_prototype_toString(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
132 {
|
|
133 //writef("Darray_prototype_toString()\n");
|
|
134 array_join(othis, ret, null);
|
|
135 return null;
|
|
136 }
|
|
137
|
|
138 /* ===================== Darray_prototype_toLocaleString ================= */
|
|
139
|
|
140 void *Darray_prototype_toLocaleString(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
141 {
|
|
142 // ECMA v3 15.4.4.3
|
|
143 tchar[] separator;
|
|
144 d_string r;
|
|
145 d_uint32 len;
|
|
146 d_uint32 k;
|
|
147 Value* v;
|
|
148
|
|
149 //writef("array_join(othis = %p)\n", othis);
|
|
150
|
|
151 if (!othis.isClass(TEXT_Array))
|
|
152 {
|
|
153 ret.putVundefined();
|
|
154 ErrInfo errinfo;
|
|
155 return Dobject.RuntimeError(&errinfo, ERR_TLS_NOT_TRANSFERRABLE);
|
|
156 }
|
|
157
|
|
158 v = othis.Get(TEXT_length);
|
|
159 len = v ? v.toUint32() : 0;
|
|
160
|
|
161 Program prog = cc.prog;
|
|
162 if (!prog.slist)
|
|
163 {
|
|
164 // Determine what list separator is only once per thread
|
|
165 //prog.slist = list_separator(prog.lcid);
|
|
166 prog.slist = ",";
|
|
167 }
|
|
168 separator = prog.slist;
|
|
169
|
|
170 for (k = 0; k != len; k++)
|
|
171 {
|
|
172 if (k)
|
|
173 r ~= separator;
|
|
174 v = othis.Get(k);
|
|
175 if (v && !v.isUndefinedOrNull())
|
|
176 { Dobject ot;
|
|
177
|
|
178 ot = v.toObject();
|
|
179 v = ot.Get(TEXT_toLocaleString);
|
|
180 if (v && !v.isPrimitive()) // if it's an Object
|
|
181 { void* a;
|
|
182 Dobject o;
|
|
183 Value rt;
|
|
184
|
|
185 o = v.object;
|
|
186 rt.putVundefined();
|
|
187 a = o.Call(cc, ot, &rt, null);
|
|
188 if (a) // if exception was thrown
|
|
189 return a;
|
|
190 r ~= rt.toString();
|
|
191 }
|
|
192 }
|
|
193 }
|
|
194
|
|
195 ret.putVstring(r);
|
|
196 return null;
|
|
197 }
|
|
198
|
|
199 /* ===================== Darray_prototype_concat ================= */
|
|
200
|
|
201 void *Darray_prototype_concat(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
202 {
|
|
203 // ECMA v3 15.4.4.4
|
|
204 Darray A;
|
|
205 Darray E;
|
|
206 Value* v;
|
|
207 d_uint32 k;
|
|
208 d_uint32 n;
|
|
209 d_uint32 a;
|
|
210
|
|
211 A = new Darray();
|
|
212 n = 0;
|
|
213 v = &othis.value;
|
|
214 for (a = 0; ; a++)
|
|
215 {
|
|
216 if (!v.isPrimitive() && v.object.isDarray())
|
|
217 { d_uint32 len;
|
|
218
|
|
219 E = cast(Darray)v.object;
|
|
220 len = E.ulength;
|
|
221 for (k = 0; k != len; k++)
|
|
222 {
|
|
223 v = E.Get(k);
|
|
224 if (v)
|
|
225 A.Put(n, v, 0);
|
|
226 n++;
|
|
227 }
|
|
228 }
|
|
229 else
|
|
230 {
|
|
231 A.Put(n, v, 0);
|
|
232 n++;
|
|
233 }
|
|
234 if (a == arglist.length)
|
|
235 break;
|
|
236 v = &arglist[a];
|
|
237 }
|
|
238
|
|
239 A.Put(TEXT_length, n, DontDelete | DontEnum);
|
|
240 Value.copy(ret, &A.value);
|
|
241 return null;
|
|
242 }
|
|
243
|
|
244 /* ===================== Darray_prototype_join ================= */
|
|
245
|
|
246 void *Darray_prototype_join(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
247 {
|
|
248 array_join(othis, ret, arglist);
|
|
249 return null;
|
|
250 }
|
|
251
|
|
252 void array_join(Dobject othis, Value* ret, Value[] arglist)
|
|
253 {
|
|
254 // ECMA 15.4.4.3
|
|
255 d_string separator;
|
|
256 d_string r;
|
|
257 d_uint32 len;
|
|
258 d_uint32 k;
|
|
259 Value* v;
|
|
260
|
|
261 //writef("array_join(othis = %p)\n", othis);
|
|
262 v = othis.Get(TEXT_length);
|
|
263 len = v ? v.toUint32() : 0;
|
|
264 if (arglist.length == 0)
|
|
265 separator = TEXT_comma;
|
|
266 else
|
|
267 separator = arglist[0].toString();
|
|
268
|
|
269 for (k = 0; k != len; k++)
|
|
270 {
|
|
271 if (k)
|
|
272 r ~= separator;
|
|
273 v = othis.Get(k);
|
|
274 if (v && !v.isUndefinedOrNull())
|
|
275 r ~= v.toString();
|
|
276 }
|
|
277
|
|
278 ret.putVstring(r);
|
|
279 }
|
|
280
|
|
281 /* ===================== Darray_prototype_toSource ================= */
|
|
282
|
|
283 void *Darray_prototype_toSource(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
284 {
|
|
285 tchar[] separator;
|
|
286 d_string r;
|
|
287 d_uint32 len;
|
|
288 d_uint32 k;
|
|
289 Value* v;
|
|
290
|
|
291 v = othis.Get(TEXT_length);
|
|
292 len = v ? v.toUint32() : 0;
|
|
293 separator = ",";
|
|
294
|
|
295 r = "[";
|
|
296 for (k = 0; k != len; k++)
|
|
297 {
|
|
298 if (k)
|
|
299 r ~= separator;
|
|
300 v = othis.Get(k);
|
|
301 if (v && !v.isUndefinedOrNull())
|
|
302 r ~= v.toSource();
|
|
303 }
|
|
304 r ~= "]";
|
|
305
|
|
306 ret.putVstring(r);
|
|
307 return null;
|
|
308 }
|
|
309
|
|
310
|
|
311 /* ===================== Darray_prototype_pop ================= */
|
|
312
|
|
313 void *Darray_prototype_pop(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
314 {
|
|
315 // ECMA v3 15.4.4.6
|
|
316 Value* v;
|
|
317 d_uint32 u;
|
|
318
|
|
319 // If othis is a Darray, then we can optimize this significantly
|
|
320 v = othis.Get(TEXT_length);
|
|
321 if (!v)
|
|
322 v = &vundefined;
|
|
323 u = v.toUint32();
|
|
324 if (u == 0)
|
|
325 {
|
|
326 othis.Put(TEXT_length, 0.0, DontDelete | DontEnum);
|
|
327 ret.putVundefined();
|
|
328 }
|
|
329 else
|
|
330 {
|
|
331 v = othis.Get(u - 1);
|
|
332 if (!v)
|
|
333 v = &vundefined;
|
|
334 Value.copy(ret, v);
|
|
335 othis.Delete(u - 1);
|
|
336 othis.Put(TEXT_length, u - 1, DontDelete | DontEnum);
|
|
337 }
|
|
338 return null;
|
|
339 }
|
|
340
|
|
341 /* ===================== Darray_prototype_push ================= */
|
|
342
|
|
343 void *Darray_prototype_push(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
344 {
|
|
345 // ECMA v3 15.4.4.7
|
|
346 Value* v;
|
|
347 d_uint32 u;
|
|
348 d_uint32 a;
|
|
349
|
|
350 // If othis is a Darray, then we can optimize this significantly
|
|
351 v = othis.Get(TEXT_length);
|
|
352 if (!v)
|
|
353 v = &vundefined;
|
|
354 u = v.toUint32();
|
|
355 for (a = 0; a < arglist.length; a++)
|
|
356 {
|
|
357 othis.Put(u + a, &arglist[a], 0);
|
|
358 }
|
|
359 othis.Put(TEXT_length, u + a, DontDelete | DontEnum);
|
|
360 ret.putVnumber(u + a);
|
|
361 return null;
|
|
362 }
|
|
363
|
|
364 /* ===================== Darray_prototype_reverse ================= */
|
|
365
|
|
366 void *Darray_prototype_reverse(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
367 {
|
|
368 // ECMA 15.4.4.4
|
|
369 d_uint32 a;
|
|
370 d_uint32 b;
|
|
371 Value* va;
|
|
372 Value* vb;
|
|
373 Value* v;
|
|
374 d_uint32 pivot;
|
|
375 d_uint32 len;
|
|
376 Value tmp;
|
|
377
|
|
378 v = othis.Get(TEXT_length);
|
|
379 len = v ? v.toUint32() : 0;
|
|
380 pivot = len / 2;
|
|
381 for (a = 0; a != pivot; a++)
|
|
382 {
|
|
383 b = len - a - 1;
|
|
384 //writef("a = %d, b = %d\n", a, b);
|
|
385 va = othis.Get(a);
|
|
386 if (va)
|
|
387 Value.copy(&tmp, va);
|
|
388 vb = othis.Get(b);
|
|
389 if (vb)
|
|
390 othis.Put(a, vb, 0);
|
|
391 else
|
|
392 othis.Delete(a);
|
|
393
|
|
394 if (va)
|
|
395 othis.Put(b, &tmp, 0);
|
|
396 else
|
|
397 othis.Delete(b);
|
|
398 }
|
|
399 Value.copy(ret, &othis.value);
|
|
400 return null;
|
|
401 }
|
|
402
|
|
403 /* ===================== Darray_prototype_shift ================= */
|
|
404
|
|
405 void *Darray_prototype_shift(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
406 {
|
|
407 // ECMA v3 15.4.4.9
|
|
408 Value* v;
|
|
409 Value* result;
|
|
410 d_uint32 len;
|
|
411 d_uint32 k;
|
|
412
|
|
413 // If othis is a Darray, then we can optimize this significantly
|
|
414 //writef("shift(othis = %p)\n", othis);
|
|
415 v = othis.Get(TEXT_length);
|
|
416 if (!v)
|
|
417 v = &vundefined;
|
|
418 len = v.toUint32();
|
|
419 if (len)
|
|
420 {
|
|
421 result = othis.Get(0u);
|
|
422 Value.copy(ret, result);
|
|
423 for (k = 1; k != len; k++)
|
|
424 {
|
|
425 v = othis.Get(k);
|
|
426 if (v)
|
|
427 {
|
|
428 othis.Put(k - 1, v, 0);
|
|
429 }
|
|
430 else
|
|
431 {
|
|
432 othis.Delete(k - 1);
|
|
433 }
|
|
434 }
|
|
435 othis.Delete(len - 1);
|
|
436 len--;
|
|
437 }
|
|
438 else
|
|
439 Value.copy(ret, &vundefined);
|
|
440
|
|
441 othis.Put(TEXT_length, len, DontDelete | DontEnum);
|
|
442 return null;
|
|
443 }
|
|
444
|
|
445 /* ===================== Darray_prototype_slice ================= */
|
|
446
|
|
447 void *Darray_prototype_slice(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
448 {
|
|
449 // ECMA v3 15.4.4.10
|
|
450 d_uint32 len;
|
|
451 d_uint32 n;
|
|
452 d_uint32 k;
|
|
453 d_uint32 r8;
|
|
454 d_number start;
|
|
455 d_number end;
|
|
456 Value* v;
|
|
457 Darray A;
|
|
458
|
|
459 v = othis.Get(TEXT_length);
|
|
460 if (!v)
|
|
461 v = &vundefined;
|
|
462 len = v.toUint32();
|
|
463
|
|
464 switch (arglist.length)
|
|
465 {
|
|
466 case 0:
|
|
467 start = vundefined.toUint32();
|
|
468 end = start;
|
|
469 break;
|
|
470
|
|
471 case 1:
|
|
472 start = arglist[0].toInteger();
|
|
473 end = len;
|
|
474 break;
|
|
475
|
|
476 default:
|
|
477 start = arglist[0].toInteger();
|
|
478 end = arglist[1].toInteger();
|
|
479 break;
|
|
480 }
|
|
481
|
|
482 if (start < 0)
|
|
483 {
|
|
484 k = len + cast(d_uint32) start;
|
|
485 if (cast(d_int32)k < 0)
|
|
486 k = 0;
|
|
487 }
|
|
488 else
|
|
489 {
|
|
490 k = cast(d_uint32) start;
|
|
491 if (len < k)
|
|
492 k = len;
|
|
493 }
|
|
494
|
|
495 if (end < 0)
|
|
496 {
|
|
497 r8 = len + cast(d_uint32) end;
|
|
498 if (cast(d_int32)r8 < 0)
|
|
499 r8 = 0;
|
|
500 }
|
|
501 else
|
|
502 {
|
|
503 r8 = cast(d_uint32) end;
|
|
504 if (len < cast(d_uint32) end)
|
|
505 r8 = len;
|
|
506 }
|
|
507
|
|
508 A = new Darray();
|
|
509 for (n = 0; k < r8; k++)
|
|
510 {
|
|
511 v = othis.Get(k);
|
|
512 if (v)
|
|
513 {
|
|
514 A.Put(n, v, 0);
|
|
515 }
|
|
516 n++;
|
|
517 }
|
|
518
|
|
519 A.Put(TEXT_length, n, DontDelete | DontEnum);
|
|
520 Value.copy(ret, &A.value);
|
|
521 return null;
|
|
522 }
|
|
523
|
|
524 /* ===================== Darray_prototype_sort ================= */
|
|
525
|
|
526 static Dobject comparefn;
|
|
527 static CallContext *comparecc;
|
|
528
|
|
529 extern (C) int compare_value(void* x, void* y)
|
|
530 {
|
|
531 Value* vx = cast(Value*)x;
|
|
532 Value* vy = cast(Value*)y;
|
|
533 d_string sx;
|
|
534 d_string sy;
|
|
535 int cmp;
|
|
536
|
|
537 //writef("compare_value()\n");
|
|
538 if (vx.isUndefined())
|
|
539 {
|
|
540 cmp = (vy.isUndefined()) ? 0 : 1;
|
|
541 }
|
|
542 else if (vy.isUndefined())
|
|
543 cmp = -1;
|
|
544 else
|
|
545 {
|
|
546 if (comparefn)
|
|
547 { Value arglist[2];
|
|
548 Value ret;
|
|
549 Value* v;
|
|
550 d_number n;
|
|
551
|
|
552 Value.copy(&arglist[0], vx);
|
|
553 Value.copy(&arglist[1], vy);
|
|
554 ret.putVundefined();
|
|
555 comparefn.Call(comparecc, comparefn, &ret, arglist);
|
|
556 n = ret.toNumber();
|
|
557 if (n < 0)
|
|
558 cmp = -1;
|
|
559 else if (n > 0)
|
|
560 cmp = 1;
|
|
561 else
|
|
562 cmp = 0;
|
|
563 }
|
|
564 else
|
|
565 {
|
|
566 sx = vx.toString();
|
|
567 sy = vy.toString();
|
4
|
568 scope tv = new TextView!(tchar)(sx, false);
|
|
569 cmp = tv.compare(sy);
|
|
570 //cmp = std.string.cmp(sx, sy);
|
0
|
571 if (cmp < 0)
|
|
572 cmp = -1;
|
|
573 else if (cmp > 0)
|
|
574 cmp = 1;
|
|
575 }
|
|
576 }
|
|
577 return cmp;
|
|
578 }
|
|
579
|
|
580 void *Darray_prototype_sort(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
581 {
|
|
582 // ECMA v3 15.4.4.11
|
|
583 Value* v;
|
|
584 d_uint32 len;
|
|
585 uint u;
|
|
586
|
|
587 //writef("Array.prototype.sort()\n");
|
|
588 v = othis.Get(TEXT_length);
|
|
589 len = v ? v.toUint32() : 0;
|
|
590
|
|
591 // This is not optimal, as isArrayIndex is done at least twice
|
|
592 // for every array member. Additionally, the qsort() by index
|
|
593 // can be avoided if we can deduce it is not a sparse array.
|
|
594
|
|
595 Property *p;
|
|
596 Value[] pvalues;
|
|
597 d_uint32[] pindices;
|
|
598 d_uint32 parraydim;
|
|
599 d_uint32 nprops;
|
|
600
|
|
601 // First, size & alloc our temp array
|
|
602 if (len < 100)
|
|
603 { // Probably not too sparse an array
|
|
604 parraydim = len;
|
|
605 }
|
|
606 else
|
|
607 {
|
|
608 parraydim = 0;
|
|
609 foreach (inout Property p; *othis.proptable)
|
|
610 { if (p.attributes == 0) // don't count special properties
|
|
611 parraydim++;
|
|
612 }
|
|
613 if (parraydim > len) // could theoretically happen
|
|
614 parraydim = len;
|
|
615 }
|
|
616
|
|
617 Value[] p1 = null;
|
|
618 Value* v1;
|
|
619 version (Win32) // eh and alloca() not working under linux
|
|
620 {
|
|
621 if (parraydim < 128)
|
|
622 v1 = cast(Value*)alloca(parraydim * Value.sizeof);
|
|
623 }
|
|
624 if (v1)
|
|
625 pvalues = v1[0 .. parraydim];
|
|
626 else
|
|
627 {
|
|
628 p1 = new Value[parraydim];
|
|
629 pvalues = p1;
|
|
630 }
|
|
631
|
|
632 d_uint32[] p2 = null;
|
|
633 d_uint32* p3;
|
|
634 version (Win32)
|
|
635 {
|
|
636 if (parraydim < 128)
|
|
637 p3 = cast(d_uint32*)alloca(parraydim * d_uint32.sizeof);
|
|
638 }
|
|
639 if (p3)
|
|
640 pindices = p3[0 .. parraydim];
|
|
641 else
|
|
642 {
|
|
643 p2 = new d_uint32[parraydim];
|
|
644 pindices = p2;
|
|
645 }
|
|
646
|
|
647 // Now fill it with all the Property's that are array indices
|
|
648 nprops = 0;
|
|
649 foreach (Value key, inout Property p; *othis.proptable)
|
|
650 { d_uint32 index;
|
|
651
|
|
652 if (p.attributes == 0 && key.isArrayIndex(index))
|
|
653 {
|
|
654 pindices[nprops] = index;
|
|
655 Value.copy(&pvalues[nprops], &p.value);
|
|
656 nprops++;
|
|
657 }
|
|
658 }
|
|
659
|
|
660 synchronized
|
|
661 {
|
|
662 comparefn = null;
|
|
663 comparecc = cc;
|
|
664 if (arglist.length)
|
|
665 {
|
|
666 if (!arglist[0].isPrimitive())
|
|
667 comparefn = arglist[0].object;
|
|
668 }
|
|
669
|
|
670 // Sort pvalues[]
|
4
|
671 tango.stdc.stdlib.qsort(pvalues.ptr, nprops, Value.sizeof, &compare_value);
|
|
672 //std.c.stdlib.qsort(pvalues.ptr, nprops, Value.sizeof, &compare_value);
|
0
|
673
|
|
674 comparefn = null;
|
|
675 comparecc = null;
|
|
676 }
|
|
677
|
|
678 // Stuff the sorted value's back into the array
|
|
679 for (u = 0; u < nprops; u++)
|
|
680 { d_uint32 index;
|
|
681
|
|
682 othis.Put(u, &pvalues[u], 0);
|
|
683 index = pindices[u];
|
|
684 if (index >= nprops)
|
|
685 {
|
|
686 othis.Delete(index);
|
|
687 }
|
|
688 }
|
|
689
|
|
690 delete p1;
|
|
691 delete p2;
|
|
692
|
|
693 ret.putVobject(othis);
|
|
694 return null;
|
|
695 }
|
|
696
|
|
697 /* ===================== Darray_prototype_splice ================= */
|
|
698
|
|
699 void *Darray_prototype_splice(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
700 {
|
|
701 // ECMA v3 15.4.4.12
|
|
702 d_uint32 len;
|
|
703 d_uint32 k;
|
|
704 d_number start;
|
|
705 d_number deleteCount;
|
|
706 Value* v;
|
|
707 Darray A;
|
|
708 d_uint32 a;
|
|
709 d_uint32 delcnt;
|
|
710 d_uint32 inscnt;
|
|
711 d_uint32 startidx;
|
|
712
|
|
713 v = othis.Get(TEXT_length);
|
|
714 if (!v)
|
|
715 v = &vundefined;
|
|
716 len = v.toUint32();
|
|
717
|
|
718 switch (arglist.length)
|
|
719 {
|
|
720 case 0:
|
|
721 start = vundefined.toUint32();
|
|
722 deleteCount = start;
|
|
723 break;
|
|
724
|
|
725 case 1:
|
|
726 start = arglist[0].toInteger();
|
|
727 deleteCount = vundefined.toUint32();
|
|
728 break;
|
|
729
|
|
730 default:
|
|
731 start = arglist[0].toInteger();
|
|
732 deleteCount = arglist[1].toInteger();
|
|
733 break;
|
|
734 }
|
|
735
|
|
736 if (start < 0)
|
|
737 {
|
|
738 startidx = len + cast(d_uint32) start;
|
|
739 if (cast(d_int32)startidx < 0)
|
|
740 startidx = 0;
|
|
741 }
|
|
742 else
|
|
743 {
|
|
744 startidx = cast(d_uint32) start;
|
|
745 if (len < startidx)
|
|
746 startidx = len;
|
|
747 }
|
|
748
|
|
749 A = new Darray();
|
|
750
|
|
751 delcnt = (deleteCount > 0) ? cast(d_uint32) deleteCount : 0;
|
|
752 if (delcnt > len - startidx)
|
|
753 delcnt = len - startidx;
|
|
754
|
|
755 // If deleteCount is not specified, ECMA implies it should
|
|
756 // be 0, while "JavaScript The Definitive Guide" says it should
|
|
757 // be delete to end of array. Jscript doesn't implement splice().
|
|
758 // We'll do it the Guide way.
|
|
759 if (arglist.length < 2)
|
|
760 delcnt = len - startidx;
|
|
761
|
|
762 //writef("Darray.splice(startidx = %d, delcnt = %d)\n", startidx, delcnt);
|
|
763 for (k = 0; k != delcnt; k++)
|
|
764 {
|
|
765 v = othis.Get(startidx + k);
|
|
766 if (v)
|
|
767 A.Put(k, v, 0);
|
|
768 }
|
|
769
|
|
770 A.Put(TEXT_length, delcnt, DontDelete | DontEnum);
|
|
771 inscnt = (arglist.length > 2) ? arglist.length - 2 : 0;
|
|
772 if (inscnt != delcnt)
|
|
773 {
|
|
774 if (inscnt <= delcnt)
|
|
775 {
|
|
776 for (k = startidx; k != (len - delcnt); k++)
|
|
777 {
|
|
778 v = othis.Get(k + delcnt);
|
|
779 if (v)
|
|
780 othis.Put(k + inscnt, v, 0);
|
|
781 else
|
|
782 othis.Delete(k + inscnt);
|
|
783 }
|
|
784
|
|
785 for (k = len; k != (len - delcnt + inscnt); k--)
|
|
786 othis.Delete(k - 1);
|
|
787 }
|
|
788 else
|
|
789 {
|
|
790 for (k = len - delcnt; k != startidx; k--)
|
|
791 {
|
|
792 v = othis.Get(k + delcnt - 1);
|
|
793 if (v)
|
|
794 othis.Put(k + inscnt - 1, v, 0);
|
|
795 else
|
|
796 othis.Delete(k + inscnt - 1);
|
|
797 }
|
|
798 }
|
|
799 }
|
|
800 k = startidx;
|
|
801 for (a = 2; a < arglist.length; a++)
|
|
802 {
|
|
803 v = &arglist[a];
|
|
804 othis.Put(k, v, 0);
|
|
805 k++;
|
|
806 }
|
|
807
|
|
808 othis.Put(TEXT_length, len - delcnt + inscnt, DontDelete | DontEnum);
|
|
809 Value.copy(ret, &A.value);
|
|
810 return null;
|
|
811 }
|
|
812
|
|
813 /* ===================== Darray_prototype_unshift ================= */
|
|
814
|
|
815 void *Darray_prototype_unshift(Dobject pthis, CallContext *cc, Dobject othis, Value *ret, Value[] arglist)
|
|
816 {
|
|
817 // ECMA v3 15.4.4.13
|
|
818 Value* v;
|
|
819 d_uint32 len;
|
|
820 d_uint32 k;
|
|
821
|
|
822 v = othis.Get(TEXT_length);
|
|
823 if (!v)
|
|
824 v = &vundefined;
|
|
825 len = v.toUint32();
|
|
826
|
|
827 for (k = len; k; k--)
|
|
828 {
|
|
829 v = othis.Get(k - 1);
|
|
830 if (v)
|
|
831 othis.Put(k + arglist.length - 1, v, 0);
|
|
832 else
|
|
833 othis.Delete(k + arglist.length - 1);
|
|
834 }
|
|
835
|
|
836 for (k = 0; k < arglist.length; k++)
|
|
837 {
|
|
838 othis.Put(k, &arglist[k], 0);
|
|
839 }
|
|
840 othis.Put(TEXT_length, len + arglist.length, DontDelete | DontEnum);
|
|
841 ret.putVnumber(len + arglist.length);
|
|
842 return null;
|
|
843 }
|
|
844
|
|
845 /* =========================== Darray_prototype =================== */
|
|
846
|
|
847 class Darray_prototype : Darray
|
|
848 {
|
|
849 this(ThreadContext *tc)
|
|
850 {
|
|
851 super(tc.Dobject_prototype);
|
|
852 Dobject f = tc.Dfunction_prototype;
|
|
853
|
|
854 Put(TEXT_constructor, tc.Darray_constructor, DontEnum);
|
|
855
|
|
856 static NativeFunctionData nfd[] =
|
|
857 [
|
|
858 { &TEXT_toString, &Darray_prototype_toString, 0 },
|
|
859 { &TEXT_toLocaleString, &Darray_prototype_toLocaleString, 0 },
|
|
860 { &TEXT_toSource, &Darray_prototype_toSource, 0 },
|
|
861 { &TEXT_concat, &Darray_prototype_concat, 1 },
|
|
862 { &TEXT_join, &Darray_prototype_join, 1 },
|
|
863 { &TEXT_pop, &Darray_prototype_pop, 0 },
|
|
864 { &TEXT_push, &Darray_prototype_push, 1 },
|
|
865 { &TEXT_reverse, &Darray_prototype_reverse, 0 },
|
|
866 { &TEXT_shift, &Darray_prototype_shift, 0, },
|
|
867 { &TEXT_slice, &Darray_prototype_slice, 2 },
|
|
868 { &TEXT_sort, &Darray_prototype_sort, 1 },
|
|
869 { &TEXT_splice, &Darray_prototype_splice, 2 },
|
|
870 { &TEXT_unshift, &Darray_prototype_unshift, 1 },
|
|
871 ];
|
|
872
|
|
873 DnativeFunction.init(this, nfd, DontEnum);
|
|
874 }
|
|
875 }
|
|
876
|
|
877
|
|
878 /* =========================== Darray =================== */
|
|
879
|
|
880 class Darray : Dobject
|
|
881 {
|
|
882 Value length; // length property
|
|
883 d_uint32 ulength;
|
|
884
|
|
885 this()
|
|
886 {
|
|
887 this(getPrototype());
|
|
888 }
|
|
889
|
|
890 this(Dobject prototype)
|
|
891 {
|
|
892 super(prototype);
|
|
893 length.putVnumber(0);
|
|
894 ulength = 0;
|
|
895 classname = TEXT_Array;
|
|
896 }
|
|
897
|
|
898 Value* Put(Identifier* key, Value* value, uint attributes)
|
|
899 {
|
|
900 Value* result = proptable.put(&key.value, key.value.hash, value, attributes);
|
|
901 if (!result)
|
|
902 Put(key.value.string, value, attributes);
|
|
903 return null;
|
|
904 }
|
|
905
|
|
906 Value* Put(d_string name, Value* v, uint attributes)
|
|
907 {
|
|
908 d_uint32 i;
|
|
909 uint c;
|
|
910 Value* result;
|
|
911
|
|
912 // ECMA 15.4.5.1
|
|
913 result = proptable.put(name, v, attributes);
|
|
914 if (!result)
|
|
915 {
|
|
916 if (name == TEXT_length)
|
|
917 {
|
|
918 i = v.toUint32();
|
|
919 if (i != v.toInteger())
|
|
920 { ErrInfo errinfo;
|
|
921
|
|
922 return Dobject.RuntimeError(&errinfo, ERR_LENGTH_INT);
|
|
923 }
|
|
924 if (i < ulength)
|
|
925 {
|
|
926 // delete all properties with keys >= i
|
|
927 d_uint32[] todelete;
|
|
928
|
|
929 foreach (Value key, inout Property p; *proptable)
|
|
930 { d_uint32 j;
|
|
931
|
|
932 j = key.toUint32();
|
|
933 if (j >= i)
|
|
934 todelete ~= j;
|
|
935 }
|
|
936 foreach (d_uint32 j; todelete)
|
|
937 {
|
|
938 proptable.del(j);
|
|
939 }
|
|
940 }
|
|
941 ulength = i;
|
|
942 length.number = i;
|
|
943 proptable.put(name, v, attributes | DontDelete | DontEnum);
|
|
944 }
|
|
945
|
|
946 // if (name is an array index i)
|
|
947
|
|
948 i = 0;
|
|
949 for (size_t j = 0; j < name.length; j++)
|
|
950 { ulong k;
|
|
951
|
|
952 c = name[j];
|
|
953 if (c == '0' && i == 0 && name.length > 1)
|
|
954 goto Lret;
|
|
955 if (c >= '0' && c <= '9')
|
|
956 { k = i * cast(ulong)10 + c - '0';
|
|
957 i = cast(d_uint32)k;
|
|
958 if (i != k)
|
|
959 goto Lret; // overflow
|
|
960 }
|
|
961 else
|
|
962 goto Lret;
|
|
963 }
|
|
964 if (i >= ulength)
|
|
965 {
|
|
966 if (i == 0xFFFFFFFF)
|
|
967 goto Lret;
|
|
968 ulength = i + 1;
|
|
969 length.number = ulength;
|
|
970 }
|
|
971 }
|
|
972 Lret:
|
|
973 return null;
|
|
974 }
|
|
975
|
|
976 Value* Put(d_string name, Dobject o, uint attributes)
|
|
977 {
|
|
978 return Put(name, &o.value, attributes);
|
|
979 }
|
|
980
|
|
981 Value* Put(d_string PropertyName, d_number n, uint attributes)
|
|
982 {
|
|
983 Value v;
|
|
984
|
|
985 v.putVnumber(n);
|
|
986 return Put(PropertyName, &v, attributes);
|
|
987 }
|
|
988
|
|
989 Value* Put(d_string PropertyName, d_string string, uint attributes)
|
|
990 {
|
|
991 Value v;
|
|
992
|
|
993 v.putVstring(string);
|
|
994 return Put(PropertyName, &v, attributes);
|
|
995 }
|
|
996
|
|
997 Value* Put(d_uint32 index, Value* vindex, Value* value, uint attributes)
|
|
998 {
|
|
999 if (index >= ulength)
|
|
1000 ulength = index + 1;
|
|
1001
|
|
1002 proptable.put(vindex, index ^ 0x55555555 /*Value.calcHash(index)*/, value, attributes);
|
|
1003 return null;
|
|
1004 }
|
|
1005
|
|
1006 Value* Put(d_uint32 index, Value* value, uint attributes)
|
|
1007 {
|
|
1008 if (index >= ulength)
|
|
1009 { ulength = index + 1;
|
|
1010 length.number = ulength;
|
|
1011 }
|
|
1012
|
|
1013 proptable.put(index, value, attributes);
|
|
1014 return null;
|
|
1015 }
|
|
1016
|
|
1017 Value* Put(d_uint32 index, d_string string, uint attributes)
|
|
1018 {
|
|
1019 if (index >= ulength)
|
|
1020 { ulength = index + 1;
|
|
1021 length.number = ulength;
|
|
1022 }
|
|
1023
|
|
1024 proptable.put(index, string, attributes);
|
|
1025 return null;
|
|
1026 }
|
|
1027
|
|
1028 Value* Get(Identifier* id)
|
|
1029 {
|
|
1030 //writef("Darray.Get(%p, '%s')\n", &proptable, PropertyName);
|
|
1031 if (id.value.string == TEXT_length)
|
|
1032 { length.number = ulength;
|
|
1033 return &length;
|
|
1034 }
|
|
1035 else
|
|
1036 return Dobject.Get(id);
|
|
1037 }
|
|
1038
|
|
1039 Value* Get(d_string PropertyName, uint hash)
|
|
1040 {
|
|
1041 //writef("Darray.Get(%p, '%s')\n", &proptable, PropertyName);
|
|
1042 if (PropertyName == TEXT_length)
|
|
1043 { length.number = ulength;
|
|
1044 return &length;
|
|
1045 }
|
|
1046 else
|
|
1047 return Dobject.Get(PropertyName, hash);
|
|
1048 }
|
|
1049
|
|
1050 Value* Get(d_uint32 index)
|
|
1051 { Value* v;
|
|
1052
|
|
1053 //writef("Darray.Get(%p, %d)\n", &proptable, index);
|
|
1054 v = proptable.get(index);
|
|
1055 return v;
|
|
1056 }
|
|
1057
|
|
1058 Value* Get(d_uint32 index, Value* vindex)
|
|
1059 { Value* v;
|
|
1060
|
|
1061 //writef("Darray.Get(%p, %d)\n", &proptable, index);
|
|
1062 v = proptable.get(vindex, index ^ 0x55555555 /*Value.calcHash(index)*/);
|
|
1063 return v;
|
|
1064 }
|
|
1065
|
|
1066 int Delete(d_string PropertyName)
|
|
1067 {
|
|
1068 // ECMA 8.6.2.5
|
|
1069 //writef("Darray.Delete('%ls')\n", d_string_ptr(PropertyName));
|
|
1070 if (PropertyName == TEXT_length)
|
|
1071 return 0; // can't delete 'length' property
|
|
1072 else
|
|
1073 return proptable.del(PropertyName);
|
|
1074 }
|
|
1075
|
|
1076 int Delete(d_uint32 index)
|
|
1077 {
|
|
1078 // ECMA 8.6.2.5
|
|
1079 return proptable.del(index);
|
|
1080 }
|
|
1081
|
|
1082
|
|
1083 static Dfunction getConstructor()
|
|
1084 {
|
|
1085 ThreadContext *tc = ThreadContext.getThreadContext();
|
|
1086 assert(tc);
|
|
1087 return tc.Darray_constructor;
|
|
1088 }
|
|
1089
|
|
1090 static Dobject getPrototype()
|
|
1091 {
|
|
1092 ThreadContext *tc = ThreadContext.getThreadContext();
|
|
1093 assert(tc);
|
|
1094 return tc.Darray_prototype;
|
|
1095 }
|
|
1096
|
|
1097 static void init(ThreadContext *tc)
|
|
1098 {
|
|
1099 tc.Darray_constructor = new Darray_constructor(tc);
|
|
1100 tc.Darray_prototype = new Darray_prototype(tc);
|
|
1101
|
|
1102 tc.Darray_constructor.Put(TEXT_prototype, tc.Darray_prototype, DontEnum | DontDelete | ReadOnly);
|
|
1103 }
|
|
1104 }
|
|
1105
|