Debian Codegraph @

terps/tads/tads3/vmvec.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /*
7 * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 vmvec.cpp - T3 VM Vector metaclass
15 Function
16
17 Notes
18
19 Modified
20 05/14/00 MJRoberts - Creation
21 */
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #include "t3std.h"
27 #include "vmtype.h"
28 #include "vmmcreg.h"
29 #include "vmmeta.h"
30 #include "vmglob.h"
31 #include "vmobj.h"
32 #include "vmvec.h"
33 #include "vmlst.h"
34 #include "vmerr.h"
35 #include "vmerrnum.h"
36 #include "vmfile.h"
37 #include "vmstack.h"
38 #include "vmbif.h"
39 #include "vmundo.h"
40 #include "vmrun.h"
41 #include "vmiter.h"
42 #include "vmsort.h"
43
44
45 /* ------------------------------------------------------------------------ */
46 /*
47 * The largest number of items we can store is the smaller of the maximum
48 * value for a UINT2 (unsigned 16-bit integer, so 2^16-1) or the maximum
49 * allocation size (OSMALMAX) divided by the element size (DATAHOLDER)
50 * divided minus overhead.
51 */
52 const int32_t VEC_MAX_BY_ALO =
53 (int32_t)(OSMALMAX/(VMB_DATAHOLDER + 1.0/8)) - 4;
54 const int32_t VEC_MAX_ELEMENTS =
55 (VEC_MAX_BY_ALO < 0xFFFF ? VEC_MAX_BY_ALO : 0xFFFF);
56
57
58 /* ------------------------------------------------------------------------ */
59 /*
60 * statics
61 */
62
63 /* metaclass registration object */
64 static CVmMetaclassVector metaclass_reg_obj;
65 CVmMetaclass *CVmObjVector::metaclass_reg_ = &metaclass_reg_obj;
66
67 /* function table */
68 int (CVmObjVector::
69 *CVmObjVector::func_table_[])(VMG_ vm_obj_id_t self,
70 vm_val_t *retval, uint *argc) =
71 {
72 &CVmObjVector::getp_undef, /* 0 */
73 &CVmObjVector::getp_to_list, /* 1 */
74 &CVmObjVector::getp_get_size, /* 2 */
75 &CVmObjVector::getp_copy_from, /* 3 */
76 &CVmObjVector::getp_fill_val, /* 4 */
77 &CVmObjVector::getp_subset, /* 5 */
78 &CVmObjVector::getp_apply_all, /* 6 */
79 &CVmObjVector::getp_index_which, /* 7 */
80 &CVmObjVector::getp_for_each, /* 8 */
81 &CVmObjVector::getp_for_each_assoc, /* 9 */
82 &CVmObjVector::getp_map_all, /* 10 */
83 &CVmObjVector::getp_index_of, /* 11 */
84 &CVmObjVector::getp_val_which, /* 12 */
85 &CVmObjVector::getp_last_index_of, /* 13 */
86 &CVmObjVector::getp_last_index_which, /* 14 */
87 &CVmObjVector::getp_last_val_which, /* 15 */
88 &CVmObjVector::getp_count_of, /* 16 */
89 &CVmObjVector::getp_count_which, /* 17 */
90 &CVmObjVector::getp_get_unique, /* 18 */
91 &CVmObjVector::getp_append_unique, /* 19 */
92 &CVmObjVector::getp_sort, /* 20 */
93 &CVmObjVector::getp_set_length, /* 21 */
94 &CVmObjVector::getp_insert_at, /* 22 */
95 &CVmObjVector::getp_remove_element_at, /* 23 */
96 &CVmObjVector::getp_remove_range, /* 24 */
97 &CVmObjVector::getp_append, /* 25 */
98 &CVmObjVector::getp_prepend, /* 26 */
99 &CVmObjVector::getp_append_all, /* 27 */
100 &CVmObjVector::getp_remove_element, /* 28 */
101 &CVmObjVector::getp_splice, /* 29 */
102 &CVmObjVector::getp_join, /* 30 */
103 &CVmObjVector::getp_generate, /* 31 */
104 &CVmObjVector::getp_indexOfMin, /* 32 */
105 &CVmObjVector::getp_minVal, /* 33 */
106 &CVmObjVector::getp_indexOfMax, /* 34 */
107 &CVmObjVector::getp_maxVal /* 35 */
108 };
109
110 /* static method indices */
111 static const int PROPIDX_generate = 31;
112
113
114 /* ------------------------------------------------------------------------ */
115 /*
116 * create with a given number of elements
117 */
118 CVmObjVector::CVmObjVector(VMG_ size_t element_count)
119 {
120 /* allocate space */
121 alloc_vector(vmg_ element_count);
122
123 /*
124 * a vector intially has no elements in use; the element_count
125 * merely gives the number of slots to be allocated initially (this
126 * won't affect the allocation count, which is stored separately)
127 */
128 set_element_count(0);
129 }
130
131 /* ------------------------------------------------------------------------ */
132 /*
133 * allocate space
134 */
135 void CVmObjVector::alloc_vector(VMG_ size_t cnt)
136 {
137 /* allocate space for the given number of elements */
138 ext_ = (char *)G_mem->get_var_heap()->alloc_mem(calc_alloc(cnt), this);
139
140 /* set the element count and allocated count */
141 set_allocated_count(cnt);
142 set_element_count(cnt);
143
144 /* clear the undo bits */
145 clear_undo_bits();
146 }
147
148 /* ------------------------------------------------------------------------ */
149 /*
150 * notify of deletion
151 */
152 void CVmObjVector::notify_delete(VMG_ int)
153 {
154 /* free our extension */
155 if (ext_ != 0)
156 G_mem->get_var_heap()->free_mem(ext_);
157 }
158
159 /* ------------------------------------------------------------------------ */
160 /*
161 * Check an argument to see if it's a source list or vector
162 */
163 static int get_src_obj(VMG_ const vm_val_t *arg, size_t *copy_cnt)
164 {
165 /* check for a list */
166 int c;
167 if (arg->is_listlike(vmg0_) && (c = arg->ll_length(vmg0_)) >= 0)
168 {
169 /* it's a list-like object */
170 *copy_cnt = c;
171 return TRUE;
172 }
173
174 /* it's not a list */
175 return FALSE;
176 }
177
178 /* ------------------------------------------------------------------------ */
179 /*
180 * create dynamically using stack arguments
181 */
182 vm_obj_id_t CVmObjVector::create_from_stack(VMG_ const uchar **pc_ptr,
183 uint argc)
184 {
185 vm_obj_id_t id;
186 CVmObjVector *vec;
187 size_t cnt = 0;
188 size_t copy_cnt = 0;
189 vm_val_t *lstval = 0;
190 size_t i;
191
192 /* check arguments */
193 if (argc > 2)
194 err_throw(VMERR_WRONG_NUM_OF_ARGS);
195
196 /* if there are no arguments, use a default initial allocation size */
197 if (argc == 0)
198 cnt = 10;
199
200 /* parse the first argument, if present */
201 if (argc >= 1)
202 {
203 /* get the first argument */
204 vm_val_t *arg1 = G_stk->get(0);
205
206 /* check for an initial allocation length or source object */
207 if (arg1->typ == VM_INT)
208 {
209 /* get the number of elements to allocate */
210 cnt = (size_t)arg1->val.intval;
211 }
212 else if (get_src_obj(vmg_ arg1, &copy_cnt))
213 {
214 /* it's a source object */
215 lstval = arg1;
216 }
217 else
218 {
219 /* anything else is invalid */
220 err_throw(VMERR_BAD_TYPE_BIF);
221 }
222 }
223
224 /* parse the second argument, if present */
225 if (argc >= 2)
226 {
227 /* get the second argument */
228 vm_val_t *arg2 = G_stk->get(1);
229
230 /* see what we have */
231 if (get_src_obj(vmg_ arg2, &copy_cnt))
232 {
233 /* it's a source object */
234 lstval = arg2;
235 }
236 else if (arg2->typ == VM_INT)
237 {
238 /*
239 * They want an explicit initial size, with the elements filled
240 * in with nil values. Set the copy size; we'll know to fill
241 * in nil values since there's no source object to copy from.
242 */
243 copy_cnt = (size_t)arg2->val.intval;
244 }
245 else
246 {
247 /* invalid source type */
248 err_throw(VMERR_BAD_TYPE_BIF);
249 }
250 }
251
252 /*
253 * make sure we allocate enough initial space to store the source
254 * object we're copying
255 */
256 if (copy_cnt > cnt)
257 cnt = copy_cnt;
258
259 /* create the vector with the given number of elements */
260 id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
261 vec = new (vmg_ id) CVmObjVector(vmg_ cnt);
262
263 /* copy or initialize the elements */
264 for (i = 0 ; i < copy_cnt ; ++i)
265 {
266 vm_val_t ele;
267
268 /*
269 * get my element at this index: if we have a source list or
270 * vector, take the current element from that list or vector;
271 * otherwise, initialize the element to nil
272 */
273 if (lstval != 0)
274 lstval->ll_index(vmg_ &ele, i + 1);
275 else
276 ele.set_nil();
277
278 /*
279 * set my element at this index - this is construction, so there's
280 * no need to save undo information
281 */
282 vec->set_element(i, &ele);
283 }
284
285 /* set the initial size */
286 vec->set_element_count(copy_cnt);
287
288 /* discard arguments */
289 G_stk->discard(argc);
290
291 /* return the new object */
292 return id;
293 }
294
295 /* ------------------------------------------------------------------------ */
296 /*
297 * save to a file
298 */
299 void CVmObjVector::save_to_file(VMG_ class CVmFile *fp)
300 {
301 size_t ele_cnt;
302 size_t alloc_cnt;
303
304 /* get our element count and full allocation count */
305 alloc_cnt = get_allocated_count();
306 ele_cnt = get_element_count();
307
308 /* write the counts and the elements */
309 fp->write_uint2(alloc_cnt);
310 fp->write_uint2(ele_cnt);
311 fp->write_bytes(get_element_ptr(0), calc_alloc_ele(ele_cnt));
312 }
313
314 /*
315 * restore from a file
316 */
317 void CVmObjVector::restore_from_file(VMG_ vm_obj_id_t self,
318 CVmFile *fp, CVmObjFixup *fixups)
319 {
320 size_t ele_cnt;
321 size_t alloc_cnt;
322
323 /* read the element count and the full allocation count */
324 alloc_cnt = fp->read_uint2();
325 ele_cnt = fp->read_uint2();
326
327 /* free any existing extension */
328 if (ext_ != 0)
329 {
330 G_mem->get_var_heap()->free_mem(ext_);
331 ext_ = 0;
332 }
333
334 /* allocate the space at the allocation count */
335 alloc_vector(vmg_ alloc_cnt);
336
337 /* set the actual in-use element count */
338 set_element_count(ele_cnt);
339
340 /* read the contents */
341 fp->read_bytes(get_element_ptr(0), calc_alloc_ele(ele_cnt));
342
343 /* fix up object references in the values */
344 fixups->fix_dh_array(vmg_ get_element_ptr(0), ele_cnt);
345 }
346
347 /* ------------------------------------------------------------------------ */
348 /*
349 * create with no initial contents
350 */
351 vm_obj_id_t CVmObjVector::create(VMG_ int in_root_set)
352 {
353 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE);
354 new (vmg_ id) CVmObjVector();
355 return id;
356 }
357
358 /*
359 * create with a given number of elements
360 */
361 vm_obj_id_t CVmObjVector::create(VMG_ int in_root_set, size_t element_count)
362 {
363 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE);
364 new (vmg_ id) CVmObjVector(vmg_ element_count);
365 return id;
366 }
367
368
369 /* ------------------------------------------------------------------------ */
370 /*
371 * create with a given list of values, given as varargs vm_val_t values
372 */
373 vm_obj_id_t CVmObjVector::create_fill(VMG_ int in_root_set,
374 size_t element_count, ...)
375 {
376 /* create the object */
377 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE);
378 CVmObjVector *vec = new (vmg_ id) CVmObjVector(vmg_ element_count);
379
380 /* populate the elements */
381 va_list args;
382 va_start(args, element_count);
383 for (size_t i = 0 ; i < element_count ; ++i)
384 {
385 /* get the next value */
386 const vm_val_t *val = va_arg(args, const vm_val_t *);
387
388 /* store it */
389 vec->set_element(i, val);
390 }
391 va_end(args);
392
393 /* set the element count */
394 vec->set_element_count(element_count);
395
396 /* return the new object's ID */
397 return id;
398 }
399
400
401 /* ------------------------------------------------------------------------ */
402 /*
403 * Create a copy of this object
404 */
405 vm_obj_id_t CVmObjVector::create_copy(VMG_ const vm_val_t *self_val)
406 {
407 vm_obj_id_t new_id;
408 CVmObjVector *new_vec;
409
410 /* save the original object on the stack for gc protection */
411 G_stk->push(self_val);
412
413 /* create a new vector with the same parameters as this one */
414 new_id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
415 new_vec = new (vmg_ new_id) CVmObjVector(vmg_ get_allocated_count());
416 new_vec->set_element_count(get_element_count());
417
418 /* copy the elements from the old vector to the new vector */
419 memcpy(new_vec->get_element_ptr(0), get_element_ptr(0),
420 calc_alloc_ele(get_element_count()));
421
422 /* done with the gc protection - discard it */
423 G_stk->discard(1);
424
425 /* return the new vector's object ID */
426 return new_id;
427 }
428
429 /* ------------------------------------------------------------------------ */
430 /*
431 * Set the allocation size - this is the number of elements we've allocated
432 * space for, which might exceed the number of elements we're actually
433 * storing currently.
434 */
435 void CVmObjVector::set_allocated_count(size_t cnt)
436 {
437 /* if the new size exceeds the maximum allowed size, throw an error */
438 if (cnt > 65535)
439 err_throw(VMERR_OUT_OF_RANGE);
440
441 /* set the new allocated size */
442 vmb_put_len(cons_get_vector_ext_ptr(), cnt);
443 }
444
445 /* ------------------------------------------------------------------------ */
446 /*
447 * Create an iterator
448 */
449 void CVmObjVector::new_iterator(VMG_ vm_val_t *retval,
450 const vm_val_t *self_val)
451 {
452 vm_val_t copy_val;
453
454 /*
455 * Create a copy of the vector - since a vector can change, we must
456 * always create an iterator based on a copy, so that the iterator has
457 * a consistent view of the original as of the time the iterator was
458 * created.
459 */
460 copy_val.set_obj(create_copy(vmg_ self_val));
461
462 /* push the copy for gc protection */
463 G_stk->push(&copy_val);
464
465 /*
466 * Set up a new indexed iterator object. The first valid index for a
467 * vector is always 1, and the last valid index is the same as the
468 * number of elements.
469 */
470 retval->set_obj(CVmObjIterIdx::create_for_coll(vmg_ &copy_val,
471 1, get_element_count()));
472
473 /* discard gc protection */
474 G_stk->discard();
475 }
476
477 /*
478 * Create a live iterator
479 */
480 void CVmObjVector::new_live_iterator(VMG_ vm_val_t *retval,
481 const vm_val_t *self_val)
482 {
483 /*
484 * Set up a new indexed iterator object. The first valid index for a
485 * vector is always 1, and the last valid index is the same as the
486 * number of elements. Since we want a "live" iterator, create the
487 * iterator with a direct reference to the vector.
488 */
489 retval->set_obj(CVmObjIterIdx::create_for_coll(vmg_ self_val,
490 1, get_element_count()));
491 }
492
493 /* ------------------------------------------------------------------------ */
494 /*
495 * set a property
496 */
497 void CVmObjVector::set_prop(VMG_ class CVmUndo *,
498 vm_obj_id_t, vm_prop_id_t,
499 const vm_val_t *)
500 {
501 err_throw(VMERR_INVALID_SETPROP);
502 }
503
504 /* ------------------------------------------------------------------------ */
505 /*
506 * call a static property
507 */
508 int CVmObjVector::call_stat_prop(VMG_ vm_val_t *result,
509 const uchar **pc_ptr, uint *argc,
510 vm_prop_id_t prop)
511 {
512 /* get the function table index */
513 int idx = G_meta_table->prop_to_vector_idx(
514 metaclass_reg_->get_reg_idx(), prop);
515
516 /* check for static methods */
517 switch (idx)
518 {
519 case PROPIDX_generate:
520 return static_getp_generate(vmg_ result, argc);
521
522 default:
523 /* inherit the default handling */
524 return CVmObjCollection::call_stat_prop(
525 vmg_ result, pc_ptr, argc, prop);
526 }
527 }
528
529
530 /* ------------------------------------------------------------------------ */
531 /*
532 * get a property
533 */
534 int CVmObjVector::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval,
535 vm_obj_id_t self, vm_obj_id_t *source_obj,
536 uint *argc)
537 {
538 uint func_idx;
539
540 /* translate the property index to an index into our function table */
541 func_idx = G_meta_table
542 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop);
543
544 /* call the appropriate function */
545 if ((this->*func_table_[func_idx])(vmg_ self, retval, argc))
546 {
547 *source_obj = metaclass_reg_->get_class_obj(vmg0_);
548 return TRUE;
549 }
550
551 /* inherit our base class handling */
552 return CVmObjCollection::
553 get_prop(vmg_ prop, retval, self, source_obj, argc);
554 }
555
556 /* ------------------------------------------------------------------------ */
557 /*
558 * Expand the vector to accommodate the given number of new elements.
559 * We'll increase the element count, and we'll save undo for the change.
560 * If necessary, we'll re-allocate our memory extension block at a
561 * larger size.
562 */
563 void CVmObjVector::expand_by(VMG_ vm_obj_id_t self, size_t added_elements)
564 {
565 /* calculate the element count */
566 size_t new_ele_cnt = get_element_count() + added_elements;
567
568 /* remember the new size, saving undo */
569 set_element_count_undo(vmg_ self, new_ele_cnt);
570 }
571
572
573 /* ------------------------------------------------------------------------ */
574 /*
575 * Append an element, with no undo
576 */
577 void CVmObjVector::append_element(VMG_ vm_obj_id_t self, const vm_val_t *val)
578 {
579 /* expand the vector if necessary */
580 size_t cnt = get_element_count();
581 if (cnt >= get_allocated_count())
582 {
583 /* expand by 50% of the current size, or at least 5 new elements */
584 int inc = cnt/2;
585 if (inc < 5)
586 inc = 5;
587
588 /* do the expansion */
589 expand_by(vmg_ self, inc);
590 }
591
592 /* set the element */
593 set_element(cnt, val);
594
595 /* count it in the vector */
596 set_element_count(cnt+1);
597 }
598
599 /* ------------------------------------------------------------------------ */
600 /*
601 * Set the element count, keeping undo for the change
602 */
603 void CVmObjVector::set_element_count_undo(VMG_ vm_obj_id_t self,
604 size_t new_ele_cnt)
605 {
606 /* add undo if necessary */
607 if (G_undo != 0)
608 {
609 size_t old_ele_cnt;
610 vm_val_t old_val;
611 size_t idx;
612 vm_val_t nil_val;
613
614 /* get the old size */
615 old_ele_cnt = get_element_count();
616
617 /*
618 * If we're shrinking the vector, save undo for each element we're
619 * dropping off the end; to do this, simply set each element we're
620 * losing to nil, since this will add an undo record with the
621 * original value of the element.
622 *
623 * Note that we must save undo for dropped elements BEFORE we set
624 * the vector's new sizd. We must perform the steps in this order
625 * because undo is applied in reverse order: when we read back the
626 * undo, we'll first change the vector size to its old size (since
627 * that's the last saved undo instruction), then we'll set the
628 * elements to their old values.
629 */
630 nil_val.set_nil();
631 for (idx = new_ele_cnt ; idx < old_ele_cnt ; ++idx)
632 set_element_undo(vmg_ self, idx, &nil_val);
633
634 /* set up an integer with the pre-modification element count */
635 old_val.set_int(old_ele_cnt);
636
637 /*
638 * add the undo record - use the special index value 0xffffffff
639 * as the key; this will never be a valid index for a real
640 * element, because we could never address an element with an
641 * index this high in a 32-bit address space
642 */
643 G_undo->add_new_record_int_key(vmg_ self, 0xffffffffU, &old_val);
644 }
645
646 /*
647 * if the new size is larger than our current allocation size,
648 * increase the allocated size
649 */
650 if (new_ele_cnt > get_allocated_count())
651 {
652 size_t new_alloc_cnt;
653 size_t new_mem_size;
654
655 /* bump up the allocated size */
656 new_alloc_cnt = get_allocated_count() + get_alloc_count_increment();
657
658 /* if that's still not big enough, go up to the requested size */
659 if (new_alloc_cnt < new_ele_cnt)
660 new_alloc_cnt = new_ele_cnt;
661
662 /* get the new size we need */
663 new_mem_size = calc_alloc(new_alloc_cnt);
664
665 /* reallocate our memory at the new, larger size */
666 ext_ = (char *)G_mem->get_var_heap()
667 ->realloc_mem(new_mem_size, ext_, this);
668
669 /* set the new allocation size */
670 set_allocated_count(new_alloc_cnt);
671
672 /*
673 * clear the undo bits (it's easier than moving them, and the only
674 * cost is that we might generate some redundant undo records)
675 */
676 clear_undo_bits();
677 }
678
679 /*
680 * if we're expanding the in-use size of the vector, set each
681 * newly-added element to nil
682 */
683 if (new_ele_cnt > get_element_count())
684 {
685 size_t i;
686 vm_val_t nil_val;
687
688 /*
689 * set the old value for each new element to nil (we don't have to
690 * save undo for this, since the undo operation for the change in
691 * size will simply discard all of the new elements)
692 */
693 nil_val.set_nil();
694 for (i = get_element_count() ; i < new_ele_cnt ; ++i)
695 set_element(i, &nil_val);
696 }
697
698 /* set the new element count */
699 set_element_count(new_ele_cnt);
700 }
701
702 /* ------------------------------------------------------------------------ */
703 /*
704 * notify of new undo savepoint
705 */
706 void CVmObjVector::notify_new_savept()
707 {
708 /*
709 * clear the undo bits - we obviously have no undo for the new
710 * savepoint yet
711 */
712 clear_undo_bits();
713 }
714
715 /* ------------------------------------------------------------------------ */
716 /*
717 * apply undo
718 */
719 void CVmObjVector::apply_undo(VMG_ struct CVmUndoRecord *rec)
720 {
721 /*
722 * if the record refers to index 0xffffffff, this is a size-change
723 * record; otherwise, it's an ordinary element change record
724 */
725 if ((size_t)rec->id.intval == 0xffffffffU)
726 {
727 /*
728 * it's a size change - apply the new size, which is stored as
729 * an integer in the old value record
730 */
731 set_element_count((size_t)rec->oldval.val.intval);
732 }
733 else
734 {
735 /* it's an ordinary index record - set the element from undo data */
736 set_element((size_t)rec->id.intval, &rec->oldval);
737 }
738 }
739
740 /* ------------------------------------------------------------------------ */
741 /*
742 * mark references from an undo record
743 */
744 void CVmObjVector::mark_undo_ref(VMG_ CVmUndoRecord *rec)
745 {
746 /* if the undo record refers to an object, mark the object */
747 if (rec->oldval.typ == VM_OBJ)
748 G_obj_table->mark_all_refs(rec->oldval.val.obj, VMOBJ_REACHABLE);
749 }
750
751 /* ------------------------------------------------------------------------ */
752 /*
753 * mark references
754 */
755 void CVmObjVector::mark_refs(VMG_ uint state)
756 {
757 /* get my element count */
758 size_t cnt = get_element_count();
759
760 /* mark as referenced each object in the vector */
761 for (char *p = get_element_ptr(0) ; cnt != 0 ;
762 --cnt, inc_element_ptr(&p))
763 {
764 /*
765 * if this is an object, mark it as referenced, and mark its
766 * references as referenced
767 */
768 if (vmb_get_dh_type(p) == VM_OBJ)
769 G_obj_table->mark_all_refs(vmb_get_dh_obj(p), state);
770 }
771 }
772
773 /* ------------------------------------------------------------------------ */
774 /*
775 * load from an image file
776 */
777 void CVmObjVector::load_from_image(VMG_ vm_obj_id_t self,
778 const char *ptr, size_t siz)
779 {
780 /* load from the image data */
781 load_image_data(vmg_ ptr, siz);
782
783 /*
784 * save our image data pointer in the object table, so that we can
785 * access it (without storing it ourselves) during a reload
786 */
787 G_obj_table->save_image_pointer(self, ptr, siz);
788 }
789
790 /*
791 * reload the object from image data
792 */
793 void CVmObjVector::reload_from_image(VMG_ vm_obj_id_t,
794 const char *ptr, size_t siz)
795 {
796 /* load the image data */
797 load_image_data(vmg_ ptr, siz);
798 }
799
800 /*
801 * load the data from an image file
802 */
803 void CVmObjVector::load_image_data(VMG_ const char *ptr, size_t siz)
804 {
805 size_t alloc_cnt;
806 size_t ele_cnt;
807
808 /* if we already have memory allocated, free it */
809 if (ext_ != 0)
810 {
811 G_mem->get_var_heap()->free_mem(ext_);
812 ext_ = 0;
813 }
814
815 /* get the allocation size and the actual in-use size */
816 alloc_cnt = vmb_get_len(ptr);
817 ele_cnt = vmb_get_len(ptr + VMB_LEN);
818
819 /* make sure we allocate at least as much as is actually in use */
820 if (alloc_cnt < ele_cnt)
821 alloc_cnt = ele_cnt;
822
823 /* make sure the size isn't larger than we'd expect */
824 if (siz > VMB_LEN*2 + (VMB_DATAHOLDER * ele_cnt))
825 siz = VMB_LEN*2 + (VMB_DATAHOLDER * ele_cnt);
826
827 /*
828 * allocate memory at the new allocation size as indicated in the
829 * image data
830 */
831 alloc_vector(vmg_ alloc_cnt);
832
833 /* set the in-use element count */
834 set_element_count(ele_cnt);
835
836 /*
837 * if the size is smaller than we'd expect from the initialized
838 * element count, set extra elements to nil
839 */
840 if (siz < VMB_LEN*2 + (VMB_DATAHOLDER * ele_cnt))
841 {
842 size_t i;
843 vm_val_t nil_val;
844
845 /* set all elements to nil */
846 nil_val.set_nil();
847 for (i = 0 ; i < ele_cnt ; ++i)
848 set_element(i, &nil_val);
849 }
850
851 /* copy the initialized elements from the image data */
852 memcpy(get_element_ptr(0), ptr + VMB_LEN*2, siz - VMB_LEN*2);
853
854 /* we're resetting to initial state, so forget all undo */
855 clear_undo_bits();
856 }
857
858 /* ------------------------------------------------------------------------ */
859 /*
860 * index the vector
861 */
862 int CVmObjVector::index_val_q(VMG_ vm_val_t *result, vm_obj_id_t,
863 const vm_val_t *index_val)
864 {
865 /* get the index value as an integer */
866 int32_t idx = index_val->num_to_int(vmg0_);
867
868 /* make sure it's in range - 1 to our element count, inclusive */
869 if (idx < 1 || (uint32_t)idx > get_element_count())
870 err_throw(VMERR_INDEX_OUT_OF_RANGE);
871
872 /*
873 * get the indexed element and store it in the result, adjusting the
874 * index to the C-style 0-based range
875 */
876 get_element(idx - 1, result);
877
878 /* handled */
879 return TRUE;
880 }
881
882 /* ------------------------------------------------------------------------ */
883 /*
884 * set an indexed element of the vector
885 */
886 int CVmObjVector::set_index_val_q(VMG_ vm_val_t *new_container,
887 vm_obj_id_t self,
888 const vm_val_t *index_val,
889 const vm_val_t *new_val)
890 {
891 /* get the index value as an integer */
892 int32_t idx = index_val->num_to_int(vmg0_);
893
894 /* make sure it's at least 1 */
895 if (idx < 1)
896 err_throw(VMERR_INDEX_OUT_OF_RANGE);
897
898 /*
899 * if it's higher than the current length, extend the vector with nil
900 * entries to the requested size
901 */
902 if ((uint32_t)idx > get_element_count())
903 {
904 size_t i;
905 vm_val_t nil_val;
906
907 /* note the first new element index */
908 i = get_element_count();
909
910 /* extend the vector */
911 set_element_count_undo(vmg_ self, idx);
912
913 /*
914 * Fill in entries between the old length and the new length with
915 * nil. Note that we don't have to fill in the very last element,
916 * since we'll explicitly set it to the new value momentarily
917 * anyway. Note also that we don't need to keep undo for the
918 * initializations, since on undo we'll truncate the vector to
919 * remove the newly-added elements and thus won't need to restore
920 * any values for the slots.
921 */
922 for (nil_val.set_nil() ; i < (size_t)idx - 1 ; ++i)
923 set_element(i, &nil_val);
924
925 /*
926 * set the new value - this doesn't require undo since we had to
927 * expand the vector to make room for it
928 */
929 set_element(idx - 1, new_val);
930 }
931 else
932 {
933 /* set the element and record undo, using a zero-based index */
934 set_element_undo(vmg_ self, (size_t)idx - 1, new_val);
935 }
936
937 /* the result is the original vector value */
938 new_container->set_obj(self);
939
940 /* handled */
941 return TRUE;
942 }
943
944 /* ------------------------------------------------------------------------ */
945 /*
946 * Set an element and record undo for the change - takes a C-style 0-based
947 * index.
948 */
949 void CVmObjVector::set_element_undo(VMG_ vm_obj_id_t self,
950 size_t idx, const vm_val_t *new_val)
951 {
952 /* if we don't have undo for this element already, save undo now */
953 if (G_undo != 0 && !get_undo_bit(idx))
954 {
955 vm_val_t old_val;
956
957 /* get the pre-modification value of this element */
958 get_element(idx, &old_val);
959
960 /* add the undo record */
961 G_undo->add_new_record_int_key(vmg_ self, idx, &old_val);
962
963 /*
964 * set the undo bit to indicate that we now have undo for this
965 * element
966 */
967 set_undo_bit(idx, TRUE);
968 }
969
970 /* get the indexed element and store it in the result */
971 set_element(idx, new_val);
972 }
973
974 /* ------------------------------------------------------------------------ */
975 /*
976 * Check a value for equality. We will match any list or vector with the
977 * same number of elements and the same value for each element.
978 */
979 int CVmObjVector::equals(VMG_ vm_obj_id_t self, const vm_val_t *val,
980 int depth) const
981 {
982 int cnt;
983 int cnt2;
984 int idx;
985
986 /* if the recursion depth is excessive, throw an error */
987 if (depth > VM_MAX_TREE_DEPTH_EQ)
988 err_throw(VMERR_TREE_TOO_DEEP_EQ);
989
990 /* if the other value is a reference to myself, we certainly match */
991 if (val->typ == VM_OBJ && val->val.obj == self)
992 {
993 /* no need to look at the contents if this is a reference to me */
994 return TRUE;
995 }
996
997 /* if it's not list-like, it's not a match */
998 if (!val->is_listlike(vmg0_) || (cnt2 = val->ll_length(vmg0_)) < 0)
999 return FALSE;
1000
1001 /* if the sizes don't match, the values are not equal */
1002 cnt = get_element_count();
1003 if (cnt != cnt2)
1004 return FALSE;
1005
1006 /* compare element by element */
1007 for (idx = 0 ; idx < cnt ; ++idx)
1008 {
1009 vm_val_t val1;
1010 vm_val_t val2;
1011
1012 /* get this element of self */
1013 get_element(idx, &val1);
1014
1015 /* get this element of the other value */
1016 val->ll_index(vmg_ &val2, idx + 1);
1017
1018 /* if these elements aren't equal, our values aren't equal */
1019 if (!val1.equals(vmg_ &val2, depth + 1))
1020 return FALSE;
1021 }
1022
1023 /* we didn't find any differences, so the values are equal */
1024 return TRUE;
1025 }
1026
1027 /* ------------------------------------------------------------------------ */
1028 /*
1029 * Hash value calculation
1030 */
1031 uint CVmObjVector::calc_hash(VMG_ vm_obj_id_t self, int depth) const
1032 {
1033 uint hash;
1034 uint i;
1035
1036 /* if the recursion depth is excessive, throw an error */
1037 if (depth > VM_MAX_TREE_DEPTH_EQ)
1038 err_throw(VMERR_TREE_TOO_DEEP_EQ);
1039
1040 /* combine the hash values of the members of the vector */
1041 for (hash = 0, i = 0 ; i < get_element_count() ; ++i)
1042 {
1043 vm_val_t ele;
1044
1045 /* get this element */
1046 get_element(i, &ele);
1047
1048 /*
1049 * Calculate its hash value and add it into the combined hash.
1050 *
1051 * Note that we have to increase the recursion depth, because we're
1052 * recursively calculating the hash value of our contents, and our
1053 * contents can refer (directly or indirectly) back to this object.
1054 * In other words, we can have cycles in the reference tree from
1055 * this object back to itself, so we need to keep track of the
1056 * recursion depth to ensure we don't loop forever.
1057 */
1058 hash += ele.calc_hash(vmg_ depth + 1);
1059 }
1060
1061 /* return the combined hash */
1062 return hash;
1063 }
1064
1065 /* ------------------------------------------------------------------------ */
1066 /*
1067 * add a value to the vector
1068 */
1069 int CVmObjVector::add_val(VMG_ vm_val_t *result,
1070 vm_obj_id_t self, const vm_val_t *val)
1071 {
1072 int idx;
1073 int rhs_cnt, alo_cnt;
1074 int i;
1075 CVmObjVector *new_vec;
1076
1077 /* push the value to append, for gc protection */
1078 G_stk->push(val);
1079
1080 /*
1081 * remember the index of the first new element - this is simply one
1082 * higher than the last valid current index
1083 */
1084 idx = get_element_count();
1085
1086 /* get the number of elements to add */
1087 rhs_cnt = (val->is_listlike(vmg0_) ? val->ll_length(vmg0_) : -1);
1088
1089 /* if it's not list-like, just add it directly as a single element */
1090 alo_cnt = (rhs_cnt < 0 ? 1 : rhs_cnt);
1091
1092 /*
1093 * allocate a copy of the vector for the result - make it big enough
1094 * for my elements plus the elements to be appended
1095 */
1096 result->set_obj(create(vmg_ FALSE, idx + alo_cnt));
1097
1098 /* get the return value as a vector */
1099 new_vec = (CVmObjVector *)vm_objp(vmg_ result->val.obj);
1100
1101 /* push the result for gc protection */
1102 G_stk->push(result);
1103
1104 /* copy my elements into the result */
1105 memcpy(new_vec->get_element_ptr(0), get_element_ptr(0),
1106 calc_alloc_ele(idx));
1107
1108 /*
1109 * set the element count to include the elements copied so far (it's a
1110 * new vector, so there's no need to save undo)
1111 */
1112 new_vec->set_element_count(idx);
1113
1114 /* add the new element or elements */
1115 if (rhs_cnt < 0)
1116 {
1117 /* add a single element */
1118 new_vec->set_element(idx, val);
1119 new_vec->set_element_count(idx + 1);
1120 }
1121 else
1122 {
1123 /* add the contents of the rhs */
1124 for (i = 1 ; i <= rhs_cnt ; ++i, ++idx)
1125 {
1126 /* get this element from the right-hand side */
1127 vm_val_t ele;
1128 val->ll_index(vmg_ &ele, i);
1129
1130 /* store the element in the result */
1131 new_vec->set_element(idx, &ele);
1132 new_vec->set_element_count(idx + 1);
1133 }
1134 }
1135
1136 /* discard the gc protect */
1137 G_stk->discard(2);
1138
1139 /* handled */
1140 return TRUE;
1141 }
1142
1143 /* ------------------------------------------------------------------------ */
1144 /*
1145 * subtract a value from the vector
1146 */
1147 int CVmObjVector::sub_val(VMG_ vm_val_t *result,
1148 vm_obj_id_t self, const vm_val_t *val)
1149 {
1150 int ele_cnt, rhs_cnt;
1151 int i;
1152 CVmObjVector *new_vec;
1153 size_t new_cnt;
1154
1155 /* push the value to append, for gc protection */
1156 G_stk->push(val);
1157
1158 /* note the initial element count */
1159 ele_cnt = get_element_count();
1160
1161 /* get the number of elements to remove */
1162 rhs_cnt = (val->is_listlike(vmg0_) ? val->ll_length(vmg0_) : -1);
1163
1164 /*
1165 * allocate a new vector for the result - the new object will be no
1166 * larger than the current object, so allocate at the same size
1167 */
1168 result->set_obj(create(vmg_ FALSE, ele_cnt));
1169
1170 /* push the result for gc protection */
1171 G_stk->push(result);
1172
1173 /* get the return value as a vector */
1174 new_vec = (CVmObjVector *)vm_objp(vmg_ result->val.obj);
1175
1176 /*
1177 * copy each element from me to the new vector, but don't copy
1178 * elements found in the right-hand side
1179 */
1180 for (i = 0, new_cnt = 0 ; i < ele_cnt ; ++i)
1181 {
1182 vm_val_t ele;
1183 int found = FALSE;
1184
1185 /* get this element of self */
1186 get_element(i, &ele);
1187
1188 /* check the right-hand side for a match */
1189 if (rhs_cnt < 0)
1190 {
1191 /* single element - just check this element */
1192 if (val->equals(vmg_ &ele))
1193 found = TRUE;
1194 }
1195 else
1196 {
1197 /* scan the right side for the element */
1198 for (int j = 1 ; j <= rhs_cnt ; ++j)
1199 {
1200 /* get this right-hand element */
1201 vm_val_t rh_ele;
1202 val->ll_index(vmg_ &rh_ele, j);
1203
1204 /*
1205 * if this right-hand element matches this left-hand
1206 * element, note that we have a match
1207 */
1208 if (rh_ele.equals(vmg_ &ele))
1209 {
1210 /* note that we found it */
1211 found = TRUE;
1212
1213 /* no need to look any further - one match is enough */
1214 break;
1215 }
1216 }
1217 }
1218
1219 /*
1220 * If we didn't find the value, include it in the result vector.
1221 * Note that we don't have to save undo, since we're still
1222 * constructing the new vector.
1223 */
1224 if (!found)
1225 {
1226 /* store the element in the result vector */
1227 new_vec->set_element(new_cnt, &ele);
1228
1229 /* count it */
1230 ++new_cnt;
1231 }
1232 }
1233
1234 /*
1235 * update the new vector's size - we don't have to save undo for this
1236 * change, because we're still constructing the new vector
1237 */
1238 new_vec->set_element_count(new_cnt);
1239
1240 /* discard the gc protection */
1241 G_stk->discard(2);
1242
1243 /* handled */
1244 return TRUE;
1245 }
1246
1247 /* ------------------------------------------------------------------------ */
1248 /*
1249 * property evaluator - convert to a list
1250 */
1251 int CVmObjVector::getp_to_list(VMG_ vm_obj_id_t self, vm_val_t *retval,
1252 uint *argc)
1253 {
1254 int src_cnt;
1255 int dst_cnt;
1256 int start_idx;
1257 CVmObjList *lst;
1258 int idx;
1259 uint orig_argc = (argc != 0 ? *argc : 0);
1260 static CVmNativeCodeDesc desc(0, 2);
1261
1262 /* check arguments */
1263 if (get_prop_check_argc(retval, argc, &desc))
1264 return TRUE;
1265
1266 /* note our size */
1267 src_cnt = get_element_count();
1268
1269 /*
1270 * if there's a starting index, retrieve it; otherwise, start at our
1271 * first element
1272 */
1273 if (orig_argc >= 1)
1274 start_idx = (size_t)CVmBif::pop_int_val(vmg0_);
1275 else
1276 start_idx = 1;
1277
1278 /* negative index values count from the end of the list */
1279 if (start_idx < 0)
1280 start_idx += src_cnt + 1;
1281
1282 /* if the starting index is below 1, force it to 1 */
1283 if (start_idx < 1)
1284 start_idx = 1;
1285
1286 /* adjust the starting index to a 0-based index */
1287 --start_idx;
1288
1289 /*
1290 * if there's a size argument, retrieve it; if it's not specified,
1291 * use our actual size for the output size
1292 */
1293 if (orig_argc >= 2)
1294 dst_cnt = (size_t)CVmBif::pop_int_val(vmg0_);
1295 else
1296 dst_cnt = src_cnt;
1297
1298 /*
1299 * in no case will the result list have more elements than we can
1300 * actually supply
1301 */
1302 if (start_idx >= src_cnt)
1303 {
1304 /* we're starting past our last element - we can't supply anything */
1305 dst_cnt = 0;
1306 }
1307 else if (src_cnt - start_idx < dst_cnt)
1308 {
1309 /* we can't supply as many values as requested - lower the size */
1310 dst_cnt = src_cnt - start_idx;
1311 }
1312
1313 /* push a self-reference for garbage collection protection */
1314 G_stk->push()->set_obj(self);
1315
1316 /* create the new list */
1317 retval->set_obj(CVmObjList::create(vmg_ FALSE, dst_cnt));
1318 lst = (CVmObjList *)vm_objp(vmg_ retval->val.obj);
1319
1320 /* set the list elements */
1321 for (idx = 0 ; idx < dst_cnt ; ++idx)
1322 {
1323 vm_val_t val;
1324
1325 /* get my element at this index */
1326 get_element(idx + start_idx, &val);
1327
1328 /* store the element in the list */
1329 lst->cons_set_element(idx, &val);
1330 }
1331
1332 /* discard the self-reference */
1333 G_stk->discard();
1334
1335 /* handled */
1336 return TRUE;
1337 }
1338
1339 /* ------------------------------------------------------------------------ */
1340 /*
1341 * property evaluator - copy from another vector or list
1342 */
1343 int CVmObjVector::getp_copy_from(VMG_ vm_obj_id_t self, vm_val_t *retval,
1344 uint *argc)
1345 {
1346 /* check arguments */
1347 static CVmNativeCodeDesc desc(4);
1348 if (get_prop_check_argc(retval, argc, &desc))
1349 return TRUE;
1350
1351 /* retrieve the source value */
1352 vm_val_t src_val;
1353 G_stk->pop(&src_val);
1354
1355 /* make sure the source argument is list-like, and get its length */
1356 int src_cnt;
1357 if (!src_val.is_listlike(vmg0_)
1358 || (src_cnt = src_val.ll_length(vmg0_)) < 0)
1359 err_throw(VMERR_BAD_TYPE_BIF);
1360
1361 /* get the source starting index, adjusting negatives to count from end */
1362 int src_start = CVmBif::pop_int_val(vmg0_);
1363 if (src_start < 0)
1364 src_start += src_cnt + 1;
1365
1366 /* note the destination length (i.e., my element count) */
1367 int dst_cnt = get_element_count();
1368
1369 /* get the destination starting index, adjusting negative values */
1370 int dst_start = CVmBif::pop_int_val(vmg0_);
1371 if (dst_start < 0)
1372 dst_start += dst_cnt + 1;
1373
1374 /* get the number of elements to copy */
1375 int copy_cnt = CVmBif::pop_int_val(vmg0_);
1376
1377 /* if either starting index is below 1, force it to 1 */
1378 if (src_start < 1)
1379 src_start = 1;
1380 if (dst_start < 1)
1381 dst_start = 1;
1382
1383 /* adjust the starting indices to 0-based values */
1384 --src_start;
1385 --dst_start;
1386
1387 /* limit our copying to the remaining elements of the source */
1388 if (src_start >= src_cnt)
1389 copy_cnt = 0;
1390 else if (src_start + copy_cnt >= src_cnt)
1391 copy_cnt = src_cnt - src_start;
1392
1393 /* expand the vector if necessary to make room for added elements */
1394 if (dst_start + copy_cnt > dst_cnt)
1395 set_element_count_undo(vmg_ self, dst_start + copy_cnt);
1396
1397 /* set the list elements */
1398 for (int i = 0 ; i < copy_cnt ; ++i)
1399 {
1400 /* get the source element at this index */
1401 vm_val_t val;
1402 src_val.ll_index(vmg_ &val, i + src_start + 1);
1403
1404 /* set my element at this index, recording undo for the change */
1405 set_element_undo(vmg_ self, i + dst_start, &val);
1406 }
1407
1408 /* the return value is 'self' */
1409 retval->set_obj(self);
1410
1411 /* handled */
1412 return TRUE;
1413 }
1414
1415 /* ------------------------------------------------------------------------ */
1416 /*
1417 * property evaluator - fill with a value
1418 */
1419 int CVmObjVector::getp_fill_val(VMG_ vm_obj_id_t self, vm_val_t *retval,
1420 uint *argc)
1421 {
1422 /* check arguments */
1423 uint orig_argc = (argc != 0 ? *argc : 0);
1424 static CVmNativeCodeDesc desc(1, 2);
1425 if (get_prop_check_argc(retval, argc, &desc))
1426 return TRUE;
1427
1428 /* note our size */
1429 int cnt = get_element_count();
1430
1431 /* the return value is 'self' */
1432 retval->set_obj(self);
1433
1434 /* get the fill value */
1435 vm_val_t fill_val;
1436 G_stk->pop(&fill_val);
1437
1438 /* get the starting index; the default is 1 if it's not specified */
1439 int start_idx = (orig_argc >= 2 ? CVmBif::pop_int_val(vmg0_) : 1);
1440
1441 /* a negative index counts from the end of the vector */
1442 if (start_idx < 0)
1443 start_idx += cnt + 1;
1444
1445 /* if the starting index is below 1, force it to 1 */
1446 if (start_idx < 1)
1447 start_idx = 1;
1448
1449 /* adjust the starting index to a 0-based index */
1450 --start_idx;
1451
1452 /*
1453 * if there's a count argument, retrieve it; if it's not specified,
1454 * use our actual size for the count
1455 */
1456 int end_idx = (orig_argc >= 3
1457 ? start_idx + CVmBif::pop_int_val(vmg0_)
1458 : cnt);
1459
1460 /* push self and the fill value for gc protection */
1461 G_stk->push(retval);
1462 G_stk->push(&fill_val);
1463
1464 /* expand the vector to the requested size */
1465 if (end_idx > cnt)
1466 set_element_count_undo(vmg_ self, end_idx);
1467
1468 /* set the elements to the fill value */
1469 for (int idx = start_idx ; idx < end_idx ; ++idx)
1470 {
1471 /* set the element to the fill value, recording undo for the change */
1472 set_element_undo(vmg_ self, idx, &fill_val);
1473 }
1474
1475 /* discard GC protection */
1476 G_stk->discard(2);
1477
1478 /* handled */
1479 return TRUE;
1480 }
1481
1482 /* ------------------------------------------------------------------------ */
1483 /*
1484 * property evaluator - get the number of elements
1485 */
1486 int CVmObjVector::getp_get_size(VMG_ vm_obj_id_t, vm_val_t *retval,
1487 uint *argc)
1488 {
1489 static CVmNativeCodeDesc desc(0);
1490
1491 /* check arguments */
1492 if (get_prop_check_argc(retval, argc, &desc))
1493 return TRUE;
1494
1495 /* return my element count */
1496 retval->set_int(get_element_count());
1497
1498 /* handled */
1499 return TRUE;
1500 }
1501
1502 /* ------------------------------------------------------------------------ */
1503 /*
1504 * property evaluator - select a subset; returns a new vector consisting
1505 * of the subset of the original vector
1506 */
1507 int CVmObjVector::getp_subset(VMG_ vm_obj_id_t self, vm_val_t *retval,
1508 uint *argc)
1509 {
1510 const vm_val_t *func_val;
1511 size_t src;
1512 size_t dst;
1513 size_t cnt;
1514 CVmObjVector *new_vec;
1515 static CVmNativeCodeDesc desc(1);
1516 vm_rcdesc rc(vmg_ "Vector.subset", self, 5, G_stk->get(0), argc);
1517
1518 /* check arguments */
1519 if (get_prop_check_argc(retval, argc, &desc))
1520 return TRUE;
1521
1522 /* get the function pointer argument, but leave it on the stack */
1523 func_val = G_stk->get(0);
1524
1525 /* push a self-reference while allocating to protect from gc */
1526 G_interpreter->push_obj(vmg_ self);
1527
1528 /*
1529 * allocate the new vector that we'll return - all we know at this
1530 * point is that the new vector won't be larger than the current
1531 * vector, so allocate at the current size
1532 */
1533 cnt = get_element_count();
1534 retval->set_obj(create(vmg_ FALSE, cnt));
1535
1536 /* get the return value as an vector */
1537 new_vec = (CVmObjVector *)vm_objp(vmg_ retval->val.obj);
1538
1539 /*
1540 * push a reference to the new list to protect it from the garbage
1541 * collector, which could be invoked in the course of executing the
1542 * user callback
1543 */
1544 G_stk->push(retval);
1545
1546 /*
1547 * Go through each element of our list, and invoke the callback on
1548 * each element. If the element passes, write it to the current
1549 * output location in the list; otherwise, just skip it.
1550 *
1551 * Note that we're using the same list as source and destination,
1552 * which is easy because the list will either shrink or stay the
1553 * same - we'll never need to insert new elements.
1554 */
1555 for (src = dst = 0 ; src < cnt ; ++src)
1556 {
1557 vm_val_t ele;
1558 const vm_val_t *val;
1559
1560 /* get this element from the source vector */
1561 get_element(src, &ele);
1562
1563 /* push the element as the callback's argument */
1564 G_stk->push(&ele);
1565
1566 /* invoke the callback */
1567 G_interpreter->call_func_ptr(vmg_ func_val, 1, &rc, 0);
1568
1569 /* get the result from R0 */
1570 val = G_interpreter->get_r0();
1571
1572 /*
1573 * if the callback returned non-nil and non-zero, include this
1574 * element in the result
1575 */
1576 if (val->typ == VM_NIL
1577 || (val->typ == VM_INT && val->val.intval == 0))
1578 {
1579 /* it's nil or zero - don't include it in the result */
1580 }
1581 else
1582 {
1583 /*
1584 * include this element in the result (there's no need to
1585 * save undo, since the whole vector is new)
1586 */
1587 new_vec->set_element(dst, &ele);
1588
1589 /* advance the output index */
1590 ++dst;
1591 }
1592 }
1593
1594 /* set the actual number of elements in the result */
1595 new_vec->set_element_count(dst);
1596
1597 /* discard our gc protection (self, return value) and our arguments */
1598 G_stk->discard(3);
1599
1600 /* handled */
1601 return TRUE;
1602 }
1603
1604 /* ------------------------------------------------------------------------ */
1605 /*
1606 * property evaluator - apply a callback to each element
1607 */
1608 int CVmObjVector::getp_apply_all(VMG_ vm_obj_id_t self, vm_val_t *retval,
1609 uint *argc)
1610 {
1611 const vm_val_t *func_val;
1612 size_t idx;
1613 static CVmNativeCodeDesc desc(1);
1614 vm_rcdesc rc(vmg_ "Vector.applyAll", self, 6, G_stk->get(0), argc);
1615
1616 /* check arguments */
1617 if (get_prop_check_argc(retval, argc, &desc))
1618 return TRUE;
1619
1620 /* get the function pointer argument, but leave it on the stack */
1621 func_val = G_stk->get(0);
1622
1623 /* push a self-reference while working to protect from gc */
1624 G_interpreter->push_obj(vmg_ self);
1625
1626 /*
1627 * we're going to return the 'self' object, since we update the vector
1628 * in-place
1629 */
1630 retval->set_obj(self);
1631
1632 /*
1633 * Go through each element of our vector, invoking the callback on
1634 * each element. Replace each element with the result of the
1635 * callback. Note that we intentionally re-check the element count on
1636 * each iteration, in case the callback changes the number of
1637 * elements.
1638 */
1639 for (idx = 0 ; idx < get_element_count() ; ++idx)
1640 {
1641 /* push this element as the callback's argument */
1642 push_element(vmg_ idx);
1643
1644 /* invoke the callback */
1645 G_interpreter->call_func_ptr(vmg_ func_val, 1, &rc, 0);
1646
1647 /* replace this element with the result */
1648 set_element_undo(vmg_ self, idx, G_interpreter->get_r0());
1649 }
1650
1651 /* discard our gc protection (self) and our arguments */
1652 G_stk->discard(2);
1653
1654 /* handled */
1655 return TRUE;
1656 }
1657
1658 /* ------------------------------------------------------------------------ */
1659 /*
1660 * property evaluator - find the first element matching a condition
1661 */
1662 int CVmObjVector::getp_index_which(VMG_ vm_obj_id_t self, vm_val_t *retval,
1663 uint *argc)
1664 {
1665 /* use our general handler, working forwards */
1666 vm_rcdesc rc(vmg_ "Vector.indexWhich", self, 7, G_stk->get(0), argc);
1667 return gen_index_which(vmg_ self, retval, argc, TRUE, FALSE, &rc);
1668 }
1669
1670 /*
1671 * property evaluator - lastIndexWhich
1672 */
1673 int CVmObjVector::getp_last_index_which(VMG_ vm_obj_id_t self,
1674 vm_val_t *retval, uint *argc)
1675 {
1676 /* use our general handler, working backwards */
1677 vm_rcdesc rc(vmg_ "Vector.lastIndexWhich", self, 14, G_stk->get(0), argc);
1678 return gen_index_which(vmg_ self, retval, argc, FALSE, FALSE, &rc);
1679 }
1680
1681 /*
1682 * general handler for indexWhich and lastIndexWhich
1683 */
1684 int CVmObjVector::gen_index_which(VMG_ vm_obj_id_t self, vm_val_t *retval,
1685 uint *argc, int forward, int count_only,
1686 vm_rcdesc *rc)
1687 {
1688 const vm_val_t *func_val;
1689 size_t cnt;
1690 size_t idx;
1691 int val_cnt;
1692 static CVmNativeCodeDesc desc(1);
1693
1694 /* check arguments */
1695 if (get_prop_check_argc(retval, argc, &desc))
1696 return TRUE;
1697
1698 /* get the function pointer argument, but leave it on the stack */
1699 func_val = G_stk->get(0);
1700
1701 /* push a self-reference while working to protect from gc */
1702 G_interpreter->push_obj(vmg_ self);
1703
1704 /* presume we won't find a matching element */
1705 retval->set_nil();
1706 val_cnt = 0;
1707
1708 /* get the number of elements to visit */
1709 cnt = get_element_count();
1710
1711 /* start at the first or last element, depending on direction */
1712 idx = (forward ? 0 : cnt);
1713
1714 /*
1715 * Go through each element of our vector, and invoke the callback on
1716 * each element, looking for the first one that returns true.
1717 */
1718 for (;;)
1719 {
1720 /* if we've reached the last element, we can stop looking */
1721 if (forward ? idx >= cnt : idx == 0)
1722 break;
1723
1724 /* if we're going backwards, decrement the index */
1725 if (!forward)
1726 --idx;
1727
1728 /* push the element as the callback's argument */
1729 push_element(vmg_ idx);
1730
1731 /* invoke the callback */
1732 G_interpreter->call_func_ptr(vmg_ func_val, 1, rc, 0);
1733
1734 /*
1735 * if the callback returned true, we've found the element we're
1736 * looking for
1737 */
1738 if (G_interpreter->get_r0()->typ == VM_NIL
1739 || (G_interpreter->get_r0()->typ == VM_INT
1740 && G_interpreter->get_r0()->val.intval == 0))
1741 {
1742 /* nil or zero - this one failed the test, so keep looking */
1743 }
1744 else
1745 {
1746 /* it passed the test - check what we're doing */
1747 if (count_only)
1748 {
1749 /* we only want the count */
1750 ++val_cnt;
1751 }
1752 else
1753 {
1754 /* we want the (1-based) index - return it */
1755 retval->set_int(idx + 1);
1756
1757 /* found it - no need to keep looking */
1758 break;
1759 }
1760 }
1761
1762 /* if we're going forwards, increment the index */
1763 if (forward)
1764 ++idx;
1765 }
1766
1767 /* discard our gc protection (self) and our arguments */
1768 G_stk->discard(2);
1769
1770 /* return the count if desired */
1771 if (count_only)
1772 retval->set_int(val_cnt);
1773
1774 /* handled */
1775 return TRUE;
1776 }
1777
1778 /* ------------------------------------------------------------------------ */
1779 /*
1780 * property evaluator - lastValWhich
1781 */
1782 int CVmObjVector::getp_last_val_which(VMG_ vm_obj_id_t self,
1783 vm_val_t *retval, uint *argc)
1784 {
1785 /* get the index of the value using lastIndexWhich */
1786 getp_last_index_which(vmg_ self, retval, argc);
1787
1788 /* if the return value is a valid index, get the value at the index */
1789 if (retval->typ == VM_INT)
1790 {
1791 int idx;
1792
1793 /* get the element as the return value */
1794 idx = (int)retval->val.intval;
1795 get_element(idx - 1, retval);
1796 }
1797
1798 /* handled */
1799 return TRUE;
1800 }
1801
1802 /* ------------------------------------------------------------------------ */
1803 /*
1804 * property evaluator - valWhich
1805 */
1806 int CVmObjVector::getp_val_which(VMG_ vm_obj_id_t self, vm_val_t *retval,
1807 uint *argc)
1808 {
1809 /* get the index of the value using indexWhich */
1810 getp_index_which(vmg_ self, retval, argc);
1811
1812 /* if the return value is a valid index, get the value at the index */
1813 if (retval->typ == VM_INT)
1814 {
1815 int idx;
1816
1817 /* get the element as the return value */
1818 idx = (int)retval->val.intval;
1819 get_element(idx - 1, retval);
1820 }
1821
1822 /* handled */
1823 return TRUE;
1824 }
1825
1826 /* ------------------------------------------------------------------------ */
1827 /*
1828 * property evaluator - call a callback for each element
1829 */
1830 int CVmObjVector::getp_for_each(VMG_ vm_obj_id_t self, vm_val_t *retval,
1831 uint *argc)
1832 {
1833 /* use the general forEach processor */
1834 vm_rcdesc rc(vmg_ "Vector.forEach", self, 8, G_stk->get(0), argc);
1835 return for_each_gen(vmg_ self, retval, argc, FALSE, &rc);
1836 }
1837
1838 /*
1839 * property evaluator - call a callback for each element
1840 */
1841 int CVmObjVector::getp_for_each_assoc(VMG_ vm_obj_id_t self,
1842 vm_val_t *retval, uint *argc)
1843 {
1844 /* use the general forEach processor */
1845 vm_rcdesc rc(vmg_ "Vector.forEachAssoc", self, 9, G_stk->get(0), argc);
1846 return for_each_gen(vmg_ self, retval, argc, TRUE, &rc);
1847 }
1848
1849
1850 /*
1851 * General forEach/forEachAssoc processor
1852 */
1853 int CVmObjVector::for_each_gen(VMG_ vm_obj_id_t self,
1854 vm_val_t *retval, uint *argc,
1855 int pass_key_to_cb, vm_rcdesc *rc)
1856 {
1857 const vm_val_t *func_val;
1858 size_t idx;
1859 static CVmNativeCodeDesc desc(1);
1860
1861 /* check arguments */
1862 if (get_prop_check_argc(retval, argc, &desc))
1863 return TRUE;
1864
1865 /* get the function pointer argument, but leave it on the stack */
1866 func_val = G_stk->get(0);
1867
1868 /* push a self-reference while working to protect from gc */
1869 G_interpreter->push_obj(vmg_ self);
1870
1871 /* no return value */
1872 retval->set_nil();
1873
1874 /*
1875 * Invoke the callback on each element. Note that we intentionally do
1876 * not cache the element count, since it is possible that a program
1877 * could change the vector size in the course of an iteration; if we
1878 * cached the size, and the actual size were reduced during the
1879 * iteration, we would visit invalid elements past the new end of the
1880 * vector. To avoid this possibility, we re-check the current element
1881 * count on each iteration to make sure we haven't run off the end of
1882 * the vector.
1883 */
1884 for (idx = 0 ; idx < get_element_count() ; ++idx)
1885 {
1886 /* push the element as the callback's argument */
1887 push_element(vmg_ idx);
1888
1889 /* push the index argument if desired */
1890 if (pass_key_to_cb)
1891 G_stk->push()->set_int(idx + 1);
1892
1893 /* invoke the callback */
1894 G_interpreter->call_func_ptr(vmg_ func_val, pass_key_to_cb ? 2 : 1,
1895 rc, 0);
1896 }
1897
1898 /* discard our gc protection (self) and our arguments */
1899 G_stk->discard(2);
1900
1901 /* handled */
1902 return TRUE;
1903 }
1904
1905 /* ------------------------------------------------------------------------ */
1906 /*
1907 * property evaluator - mapAll
1908 */
1909 int CVmObjVector::getp_map_all(VMG_ vm_obj_id_t self, vm_val_t *retval,
1910 uint *argc)
1911 {
1912 const vm_val_t *func_val;
1913 size_t idx;
1914 CVmObjVector *new_vec;
1915 static CVmNativeCodeDesc desc(1);
1916 vm_rcdesc rc(vmg_ "Vector.mapAll", self, 10, G_stk->get(0), argc);
1917
1918 /* check arguments */
1919 if (get_prop_check_argc(retval, argc, &desc))
1920 return TRUE;
1921
1922 /* get the function pointer argument, but leave it on the stack */
1923 func_val = G_stk->get(0);
1924
1925 /* push a self-reference while working to protect from gc */
1926 G_interpreter->push_obj(vmg_ self);
1927
1928 /*
1929 * allocate a new vector for the return value - the new vector will
1930 * have the same size as the original, since we're mapping each
1931 * element of the old vector to the corresponding element of the new
1932 * vector
1933 */
1934 retval->set_obj(create(vmg_ FALSE, get_allocated_count()));
1935
1936 /* get the return value as an vector */
1937 new_vec = (CVmObjVector *)vm_objp(vmg_ retval->val.obj);
1938
1939 /*
1940 * push a reference to the new list to protect it from the garbage
1941 * collector, which could be invoked in the course of executing the
1942 * user callback
1943 */
1944 G_stk->push(retval);
1945
1946 /*
1947 * Go through each element of our vector, and invoke the callback on
1948 * each element, storing the result in the corresponding element of
1949 * the new vector. Note that we re-check the element count on each
1950 * iteration, in case the callback changes it on us.
1951 */
1952 for (idx = 0 ; idx < get_element_count() ; ++idx)
1953 {
1954 /* push the element as the callback's argument */
1955 push_element(vmg_ idx);
1956
1957 /* invoke the callback */
1958 G_interpreter->call_func_ptr(vmg_ func_val, 1, &rc, 0);
1959
1960 /*
1961 * replace this element with the result (there's no need to save
1962 * undo, since the whole vector is new)
1963 */
1964 new_vec->set_element(idx, G_interpreter->get_r0());
1965 new_vec->set_element_count(idx + 1);
1966 }
1967
1968 /* discard our gc protection (self, new vector) and our arguments */
1969 G_stk->discard(3);
1970
1971 /* handled */
1972 return TRUE;
1973 }
1974
1975 /* ------------------------------------------------------------------------ */
1976 /*
1977 * property evaluator - indexOf
1978 */
1979 int CVmObjVector::getp_index_of(VMG_ vm_obj_id_t self, vm_val_t *retval,
1980 uint *argc)
1981 {
1982 /* use our general handler, going forwards */
1983 return gen_index_of(vmg_ self, retval, argc, TRUE, FALSE);
1984 }
1985
1986 /*
1987 * property evaluator - lastIndexOf
1988 */
1989 int CVmObjVector::getp_last_index_of(VMG_ vm_obj_id_t self, vm_val_t *retval,
1990 uint *argc)
1991 {
1992 /* use our general handler, going backwards */
1993 return gen_index_of(vmg_ self, retval, argc, FALSE, FALSE);
1994 }
1995
1996 /*
1997 * general handler for indexOf and lastIndexOf - searches the vector
1998 * either forwards of backwards for a given value
1999 */
2000 int CVmObjVector::gen_index_of(VMG_ vm_obj_id_t self, vm_val_t *retval,
2001 uint *argc, int forward, int count_only)
2002 {
2003 const vm_val_t *val;
2004 size_t cnt;
2005 size_t idx;
2006 int val_cnt;
2007 static CVmNativeCodeDesc desc(1);
2008
2009 /* check arguments */
2010 if (get_prop_check_argc(retval, argc, &desc))
2011 return TRUE;
2012
2013 /* get the value, but leave it on the stack */
2014 val = G_stk->get(0);
2015
2016 /* push a self-reference while working to protect from gc */
2017 G_interpreter->push_obj(vmg_ self);
2018
2019 /* presume we won't find a matching element */
2020 retval->set_nil();
2021 val_cnt = 0;
2022
2023 /* get the number of elements to visit */
2024 cnt = get_element_count();
2025
2026 /* start at the first or last element, depending on the direction */
2027 idx = (forward ? 0 : cnt);
2028
2029 /* scan the list, looking for the element */
2030 for (;;)
2031 {
2032 vm_val_t ele;
2033
2034 /* if we've reached the last element, stop looking */
2035 if (forward ? idx >= cnt : idx == 0)
2036 break;
2037
2038 /* if we're going backwards, move to the next element position */
2039 if (!forward)
2040 --idx;
2041
2042 /* get this element */
2043 get_element(idx, &ele);
2044
2045 /* if the element matches the search value, return its index */
2046 if (ele.equals(vmg_ val))
2047 {
2048 /* it matches - see what we're doing */
2049 if (count_only)
2050 {
2051 /* they only want the count */
2052 ++val_cnt;
2053 }
2054 else
2055 {
2056 /* this is the one - return its 1-based index */
2057 retval->set_int(idx + 1);
2058
2059 /* foind it - no need to keep searching */
2060 break;
2061 }
2062 }
2063
2064 /* if we're going forwards, move to the next element */
2065 if (forward)
2066 ++idx;
2067 }
2068
2069 /* discard our gc protection (self) and our arguments */
2070 G_stk->discard(2);
2071
2072 /* return the count if desired */
2073 if (count_only)
2074 retval->set_int(val_cnt);
2075
2076 /* handled */
2077 return TRUE;
2078 }
2079
2080 /* ------------------------------------------------------------------------ */
2081 /*
2082 * property evaluator - countOf
2083 */
2084 int CVmObjVector::getp_count_of(VMG_ vm_obj_id_t self, vm_val_t *retval,
2085 uint *argc)
2086 {
2087 /* use our general handler to obtain the count */
2088 return gen_index_of(vmg_ self, retval, argc, TRUE, TRUE);
2089 }
2090
2091 /* ------------------------------------------------------------------------ */
2092 /*
2093 * property evaluator - countWhich
2094 */
2095 int CVmObjVector::getp_count_which(VMG_ vm_obj_id_t self, vm_val_t *retval,
2096 uint *argc)
2097 {
2098 /* use our general handler to obtain the count */
2099 vm_rcdesc rc(vmg_ "Vector.countWhich", self, 17, G_stk->get(0), argc);
2100 return gen_index_which(vmg_ self, retval, argc, TRUE, TRUE, &rc);
2101 }
2102
2103 /* ------------------------------------------------------------------------ */
2104 /*
2105 * property evaluator - getUnique; returns a new vector consisting of the
2106 * unique elements of the original vector
2107 */
2108 int CVmObjVector::getp_get_unique(VMG_ vm_obj_id_t self, vm_val_t *retval,
2109 uint *argc)
2110 {
2111 CVmObjVector *new_vec;
2112 size_t cnt;
2113 static CVmNativeCodeDesc desc(0);
2114
2115 /* check arguments */
2116 if (get_prop_check_argc(retval, argc, &desc))
2117 return TRUE;
2118
2119 /* put myself on the stack for GC protection */
2120 G_interpreter->push_obj(vmg_ self);
2121
2122 /*
2123 * allocate a new vector for the return value - the new vector will
2124 * never be any larger than the original
2125 */
2126 cnt = get_element_count();
2127 retval->set_obj(create(vmg_ FALSE, cnt));
2128
2129 /* push a reference to the new list for gc protection */
2130 G_stk->push(retval);
2131
2132 /* get the return value as a vector */
2133 new_vec = (CVmObjVector *)vm_objp(vmg_ retval->val.obj);
2134
2135 /* start out with the result element count the same as my own */
2136 new_vec->set_element_count(cnt);
2137
2138 /* copy my elements to the new vector */
2139 memcpy(new_vec->get_element_ptr(0), get_element_ptr(0),
2140 calc_alloc_ele(cnt));
2141
2142 /* uniquify the result */
2143 new_vec->cons_uniquify(vmg0_);
2144
2145 /* discard the gc protection */
2146 G_stk->discard(2);
2147
2148 /* handled */
2149 return TRUE;
2150 }
2151
2152 /*
2153 * For construction, eliminate repeated elements of the vector, leaving
2154 * only the unique elements. Reduces the size of the vector to the size
2155 * required to accommodate the unique elements.
2156 */
2157 void CVmObjVector::cons_uniquify(VMG0_)
2158 {
2159 size_t cnt;
2160 size_t src, dst;
2161
2162 /* get the number of elements */
2163 cnt = get_element_count();
2164
2165 /* loop through the list and look for repeated values */
2166 for (src = dst = 0 ; src < cnt ; ++src)
2167 {
2168 size_t idx;
2169 vm_val_t src_val;
2170 int found;
2171
2172 /*
2173 * look for a copy of this source value already in the output list
2174 */
2175 get_element(src, &src_val);
2176 for (idx = 0, found = FALSE ; idx < dst ; ++idx)
2177 {
2178 vm_val_t idx_val;
2179
2180 /* get this value */
2181 get_element(idx, &idx_val);
2182
2183 /* if it's equal to the current source value, note it */
2184 if (src_val.equals(vmg_ &idx_val))
2185 {
2186 /* note that we found it */
2187 found = TRUE;
2188
2189 /* no need to look any further */
2190 break;
2191 }
2192 }
2193
2194 /* if we didn't find the value, copy it to the output list */
2195 if (!found)
2196 {
2197 /*
2198 * add it to the output vector - since this is a
2199 * construction-only method, there's no need to save undo (if
2200 * we apply undo, we'll undo the entire construction of the
2201 * vector, hence there's no need to track changes made since
2202 * creation)
2203 */
2204 set_element(dst, &src_val);
2205
2206 /* count it in the output */
2207 ++dst;
2208 }
2209 }
2210
2211 /* adjust the size of the result list */
2212 set_element_count(dst);
2213 }
2214
2215 /* ------------------------------------------------------------------------ */
2216 /*
2217 * property evaluator - appendUnique
2218 */
2219 int CVmObjVector::getp_append_unique(VMG_ vm_obj_id_t self, vm_val_t *retval,
2220 uint *argc)
2221 {
2222 vm_val_t *val2;
2223 int cnt2;
2224 int cnt;
2225 static CVmNativeCodeDesc desc(1);
2226 int i;
2227
2228 /* check arguments */
2229 if (get_prop_check_argc(retval, argc, &desc))
2230 return TRUE;
2231
2232 /* get the other value, but leave it on the stack */
2233 val2 = G_stk->get(0);
2234
2235 /* the return value is 'self' */
2236 retval->set_obj(self);
2237
2238 /* put myself on the stack for GC protection */
2239 G_stk->push(retval);
2240
2241 /* get the number of elements in the original */
2242 cnt = get_element_count();
2243
2244 /* get the number of elements to append */
2245 cnt2 = (val2->is_listlike(vmg0_) ? val2->ll_length(vmg0_) : -1);
2246
2247 /* expand myself to make room for the new elements */
2248 expand_by(vmg_ self, cnt2 < 0 ? 1 : cnt2);
2249
2250 /* append each element of the right-hand side */
2251 if (cnt2 < 0)
2252 {
2253 /* the rhs isn't a list, so just append it directly */
2254 set_element_undo(vmg_ self, cnt, val2);
2255 }
2256 else
2257 {
2258 /* append each element of the rhs list */
2259 for (i = 1 ; i <= cnt2 ; ++i)
2260 {
2261 /* get this element */
2262 vm_val_t ele;
2263 val2->ll_index(vmg_ &ele, i);
2264
2265 /* store it */
2266 set_element_undo(vmg_ self, cnt + i - 1, &ele);
2267 }
2268 }
2269
2270 /* uniquify the result */
2271 uniquify_for_append(vmg_ self);
2272
2273 /* discard the gc protection and arguments */
2274 G_stk->discard(2);
2275
2276 /* handled */
2277 return TRUE;
2278 }
2279
2280 /*
2281 * uniquify after an appendUnique operation
2282 */
2283 void CVmObjVector::uniquify_for_append(VMG_ vm_obj_id_t self)
2284 {
2285 size_t cnt;
2286 size_t src, dst;
2287
2288 /* get the number of elements */
2289 cnt = get_element_count();
2290
2291 /* loop through the list and look for repeated values */
2292 for (src = dst = 0 ; src < cnt ; ++src)
2293 {
2294 size_t idx;
2295 vm_val_t src_val;
2296 int found;
2297
2298 /* look for a copy of this value elsewhere in the vector */
2299 get_element(src, &src_val);
2300 for (idx = 0, found = FALSE ; idx < dst ; ++idx)
2301 {
2302 vm_val_t idx_val;
2303
2304 /* get this value */
2305 get_element(idx, &idx_val);
2306
2307 /* if it's equal to the current source value, note it */
2308 if (src_val.equals(vmg_ &idx_val))
2309 {
2310 /* note that we found it */
2311 found = TRUE;
2312
2313 /* no need to look any further */
2314 break;
2315 }
2316 }
2317
2318 /* if we didn't find the value, copy it to the output list */
2319 if (!found)
2320 {
2321 /*
2322 * Add it to the output vector if we're actually changing this
2323 * element (i.e., the destination and source indices are
2324 * unequal).
2325 */
2326 if (dst != src)
2327 set_element_undo(vmg_ self, dst, &src_val);
2328
2329 /* count it in the output */
2330 ++dst;
2331 }
2332 }
2333
2334 /* adjust the size of the result list */
2335 set_element_count_undo(vmg_ self, dst);
2336 }
2337
2338 /* ------------------------------------------------------------------------ */
2339 /*
2340 * sorter for vector
2341 */
2342 class CVmQSortVector: public CVmQSortVal
2343 {
2344 public:
2345 CVmQSortVector()
2346 {
2347 vec_ = 0;
2348 self_ = VM_INVALID_OBJ;
2349 }
2350
2351 /* get an element from the vector */
2352 void get_ele(VMG_ size_t idx, vm_val_t *val)
2353 { vec_->get_element(idx, val); }
2354
2355 /* store an element */
2356 void set_ele(VMG_ size_t idx, const vm_val_t *val)
2357 { vec_->set_element_undo(vmg_ self_, idx, val); }
2358
2359 /* our vector object */
2360 CVmObjVector *vec_;
2361 vm_obj_id_t self_;
2362 };
2363
2364 /*
2365 * property evaluator - sort
2366 */
2367 int CVmObjVector::getp_sort(VMG_ vm_obj_id_t self, vm_val_t *retval,
2368 uint *in_argc)
2369 {
2370 size_t len;
2371 uint argc = (in_argc == 0 ? 0 : *in_argc);
2372 CVmQSortVector sorter;
2373 static CVmNativeCodeDesc desc(0, 2);
2374
2375 /* check arguments */
2376 if (get_prop_check_argc(retval, in_argc, &desc))
2377 return TRUE;
2378
2379 /* remember the length of my list */
2380 len = get_element_count();
2381
2382 /* set the vector object in the sorter */
2383 sorter.vec_ = this;
2384 sorter.self_ = self;
2385
2386 /* if we have an 'descending' flag, note it */
2387 if (argc >= 1)
2388 sorter.descending_ = (G_stk->get(0)->typ != VM_NIL);
2389
2390 /*
2391 * if we have a comparison function, note it, but leave it on the
2392 * stack for gc protection
2393 */
2394 if (argc >= 2)
2395 {
2396 /* get the function */
2397 sorter.compare_fn_ = *G_stk->get(1);
2398
2399 /* initialize the recursive caller descriptor for it */
2400 sorter.rc.init(vmg_ "Vector.sort", self, 20, G_stk->get(0), argc);
2401 }
2402
2403 /* put myself on the stack for GC protection */
2404 G_interpreter->push_obj(vmg_ self);
2405
2406 /* sort the vector, if we have any elements */
2407 if (len != 0)
2408 sorter.sort(vmg_ 0, len - 1);
2409
2410 /* discard the gc protection and arguments */
2411 G_stk->discard(1 + argc);
2412
2413 /* return myself */
2414 retval->set_obj(self);
2415
2416 /* handled */
2417 return TRUE;
2418 }
2419
2420 /* ------------------------------------------------------------------------ */
2421 /*
2422 * property evaluator - set the length
2423 */
2424 int CVmObjVector::getp_set_length(VMG_ vm_obj_id_t self, vm_val_t *retval,
2425 uint *argc)
2426 {
2427 size_t old_len;
2428 int32_t new_len;
2429 int32_t idx;
2430 vm_val_t nil_val;
2431 static CVmNativeCodeDesc desc(1);
2432
2433 /* check arguments */
2434 if (get_prop_check_argc(retval, argc, &desc))
2435 return TRUE;
2436
2437 /* the return value is 'self' */
2438 retval->set_obj(self);
2439
2440 /* note the old length */
2441 old_len = get_element_count();
2442
2443 /* get the new length */
2444 new_len = CVmBif::pop_long_val(vmg0_);
2445
2446 /* can't go less than zero */
2447 if (new_len < 0 || new_len >= VEC_MAX_ELEMENTS)
2448 err_throw(VMERR_BAD_VAL_BIF);
2449
2450 /* set the vector to its new size */
2451 set_element_count_undo(vmg_ self, (size_t)new_len);
2452
2453 /*
2454 * Set each newly added element to nil. Note that we don't bother
2455 * saving undo for these changes: to undo this change, the only thing
2456 * we'll have to do is reduce the vector size to its previous size,
2457 * and we've already saved undo for the size change.
2458 */
2459 nil_val.set_nil();
2460 for (idx = old_len ; idx < new_len ; ++idx)
2461 set_element((size_t)idx, &nil_val);
2462
2463 /* handled */
2464 return TRUE;
2465 }
2466
2467 /* ------------------------------------------------------------------------ */
2468 /*
2469 * property evaluator - insert one or more elements at a given index
2470 */
2471 int CVmObjVector::getp_insert_at(VMG_ vm_obj_id_t self, vm_val_t *retval,
2472 uint *in_argc)
2473 {
2474 /* we must have at least two arguments */
2475 uint argc = (in_argc != 0 ? *in_argc : 0);
2476 static CVmNativeCodeDesc desc(2, 0, TRUE);
2477 if (get_prop_check_argc(retval, in_argc, &desc))
2478 return TRUE;
2479
2480 /* note the original size of the vector */
2481 int old_cnt = get_element_count();
2482
2483 /*
2484 * get the starting insertion index; negatives count from the end, and
2485 * zero means to insert after the last existing element
2486 */
2487 int ins_idx = CVmBif::pop_int_val(vmg0_);
2488 if (ins_idx <= 0)
2489 ins_idx += old_cnt + 1;
2490
2491 /* the return value is 'self' */
2492 retval->set_obj(self);
2493
2494 /*
2495 * note the number of items we're adding - we're adding each
2496 * argument, other than the first
2497 */
2498 int add_cnt = argc - 1;
2499
2500 /*
2501 * if the starting index is out of range, it's an error - it can
2502 * range from 1 to one higher than the current element count (the
2503 * top end of the range allows us to insert elements after all of
2504 * the current elements)
2505 */
2506 if (ins_idx < 1 || ins_idx > old_cnt + 1)
2507 err_throw(VMERR_INDEX_OUT_OF_RANGE);
2508
2509 /* adjust the starting index to a zero-based value */
2510 --ins_idx;
2511
2512 /* do the insertion */
2513 insert_elements_undo(vmg_ self, ins_idx, add_cnt);
2514
2515 /* set the new element values */
2516 for (int idx = ins_idx ; add_cnt != 0 ; ++idx, --add_cnt)
2517 {
2518 /* pop the next argument value */
2519 vm_val_t val;
2520 G_stk->pop(&val);
2521
2522 /* store the item, keeping undo only if it's in the old size range */
2523 if (idx < old_cnt)
2524 set_element_undo(vmg_ self, idx, &val);
2525 else
2526 set_element(idx, &val);
2527 }
2528
2529 /* handled */
2530 return TRUE;
2531 }
2532
2533 /* ------------------------------------------------------------------------ */
2534 /*
2535 * property evaluator - append an element
2536 */
2537 int CVmObjVector::getp_prepend(VMG_ vm_obj_id_t self, vm_val_t *retval,
2538 uint *argc)
2539 {
2540 /* check arguments */
2541 static CVmNativeCodeDesc desc(1);
2542 if (get_prop_check_argc(retval, argc, &desc))
2543 return TRUE;
2544
2545 /* insert one element at the start of the vector */
2546 insert_elements_undo(vmg_ self, 0, 1);
2547
2548 /*
2549 * set the first element - note that there's no need to save undo,
2550 * since undoing this operation will simply discard this element (in
2551 * other words, there's no previous value for this element since it's
2552 * being created anew here)
2553 */
2554 set_element(0, G_stk->get(0));
2555
2556 /* the return value is 'self' */
2557 retval->set_obj(self);
2558
2559 /* discard arguments */
2560 G_stk->discard(1);
2561
2562 /* handled */
2563 return TRUE;
2564 }
2565
2566 /* ------------------------------------------------------------------------ */
2567 /*
2568 * property evaluator - remove an element at the given position
2569 */
2570 int CVmObjVector::getp_remove_element_at(VMG_ vm_obj_id_t self,
2571 vm_val_t *retval, uint *argc)
2572 {
2573 /* check arguments */
2574 static CVmNativeCodeDesc desc(1);
2575 if (get_prop_check_argc(retval, argc, &desc))
2576 return TRUE;
2577
2578 /* get the deletion index; a negative index counts from the end */
2579 int start_idx = CVmBif::pop_int_val(vmg0_);
2580 if (start_idx < 0)
2581 start_idx += get_element_count() + 1;
2582
2583 /* make sure the index is in range */
2584 if (start_idx < 1 || start_idx > (int)get_element_count())
2585 err_throw(VMERR_INDEX_OUT_OF_RANGE);
2586
2587 /* adjust to a zero-based index */
2588 --start_idx;
2589
2590 /* the return value is 'self' */
2591 retval->set_obj(self);
2592
2593 /* delete one element */
2594 remove_elements_undo(vmg_ self, start_idx, 1);
2595
2596
2597 /* handled */
2598 return TRUE;
2599 }
2600
2601 /* ------------------------------------------------------------------------ */
2602 /*
2603 * property evaluator - remove element(s) matching a given value
2604 */
2605 int CVmObjVector::getp_remove_element(VMG_ vm_obj_id_t self,
2606 vm_val_t *retval, uint *argc)
2607 {
2608 vm_val_t *del_val;
2609 static CVmNativeCodeDesc desc(1);
2610 size_t src;
2611 size_t dst;
2612 size_t old_cnt;
2613
2614 /* check arguments */
2615 if (get_prop_check_argc(retval, argc, &desc))
2616 return TRUE;
2617
2618 /* get the value to be removed */
2619 del_val = G_stk->get(0);
2620
2621 /* the return value is 'self'; leave it on the stack */
2622 retval->set_obj(self);
2623 G_stk->push(retval);
2624
2625 /* get the original number of elements */
2626 old_cnt = get_element_count();
2627
2628 /* delete each element matching the given value */
2629 for (src = dst = 0 ; src < old_cnt ; ++src)
2630 {
2631 vm_val_t ele_val;
2632
2633 /* get this element */
2634 get_element(src, &ele_val);
2635
2636 /*
2637 * if this element doesn't match the value to be deleted, add it
2638 * to the result vector
2639 */
2640 if (!ele_val.equals(vmg_ del_val))
2641 {
2642 /*
2643 * This element is a keeper - if we're moving it to a new
2644 * position (i.e., we've deleted any elements before this
2645 * one), store it at its new position, keeping undo
2646 * information. If we're storing it at its same position,
2647 * there's no need to re-store the value or keep any undo,
2648 * since no change is involved.
2649 */
2650 if (dst != src)
2651 set_element_undo(vmg_ self, dst, &ele_val);
2652
2653 /* increment the write index past this element */
2654 ++dst;
2655 }
2656 }
2657
2658 /* if the element count changed, set the new element count */
2659 if (dst != old_cnt)
2660 set_element_count_undo(vmg_ self, dst);
2661
2662 /* discard gc protection and arguments */
2663 G_stk->discard(2);
2664
2665 /* handled */
2666 return TRUE;
2667 }
2668
2669 /* ------------------------------------------------------------------------ */
2670 /*
2671 * property evaluator - remove a range of elements
2672 */
2673 int CVmObjVector::getp_remove_range(VMG_ vm_obj_id_t self,
2674 vm_val_t *retval, uint *argc)
2675 {
2676 int start_idx;
2677 int end_idx;
2678 static CVmNativeCodeDesc desc(2);
2679
2680 /* check arguments */
2681 if (get_prop_check_argc(retval, argc, &desc))
2682 return TRUE;
2683
2684 /* get the starting index; a negative value counts from the end */
2685 start_idx = CVmBif::pop_int_val(vmg0_);
2686 if (start_idx < 0)
2687 start_idx += get_element_count() + 1;
2688
2689 /*
2690 * make sure the index is in range - it must refer to an existing
2691 * element
2692 */
2693 if (start_idx < 1 || start_idx > (int)get_element_count())
2694 err_throw(VMERR_INDEX_OUT_OF_RANGE);
2695
2696 /* get the ending index, and adjust for negative values */
2697 end_idx = CVmBif::pop_int_val(vmg0_);
2698 if (end_idx < 0)
2699 end_idx += get_element_count() + 1;
2700
2701 /*
2702 * make sure it's in range - it must refer to an existing element,
2703 * and it must be greater than or equal to the starting index
2704 */
2705 if (end_idx < start_idx || end_idx > (int)get_element_count())
2706 err_throw(VMERR_INDEX_OUT_OF_RANGE);
2707
2708 /* adjust to zero-based indices */
2709 --start_idx;
2710 --end_idx;
2711
2712 /* the return value is 'self' */
2713 retval->set_obj(self);
2714
2715 /*
2716 * delete the elements - the number of elements we're deleting is
2717 * one higher than the difference of the starting and ending indices
2718 * (if the two indices are the same, we're deleting just the one
2719 * element)
2720 */
2721 remove_elements_undo(vmg_ self, start_idx, end_idx - start_idx + 1);
2722
2723 /* handled */
2724 return TRUE;
2725 }
2726
2727 /* ------------------------------------------------------------------------ */
2728 /*
2729 * property evaluator - append an element
2730 */
2731 int CVmObjVector::getp_append(VMG_ vm_obj_id_t self, vm_val_t *retval,
2732 uint *argc)
2733 {
2734 vm_val_t *valp;
2735 size_t cnt;
2736 static CVmNativeCodeDesc desc(1);
2737
2738 /* check arguments */
2739 if (get_prop_check_argc(retval, argc, &desc))
2740 return TRUE;
2741
2742 /* get the value to append, but leave it on the stack */
2743 valp = G_stk->get(0);
2744
2745 /* get the old size */
2746 cnt = get_element_count();
2747
2748 /* the result object is 'self' */
2749 retval->set_obj(self);
2750
2751 /* push a self-reference for gc protection */
2752 G_stk->push(retval);
2753
2754 /* expand myself by one element to make room for the addition */
2755 expand_by(vmg_ self, 1);
2756
2757 /* add the new element, saving undo */
2758 set_element_undo(vmg_ self, cnt, valp);
2759
2760 /* discard the argument and gc protection */
2761 G_stk->discard(2);
2762
2763 /* handled */
2764 return TRUE;
2765 }
2766
2767 /* ------------------------------------------------------------------------ */
2768 /*
2769 * property evaluator - append elements from a collection, or append a
2770 * single non-collection element
2771 */
2772 int CVmObjVector::getp_append_all(VMG_ vm_obj_id_t self, vm_val_t *retval,
2773 uint *argc)
2774 {
2775 /* check arguments */
2776 static CVmNativeCodeDesc desc(1);
2777 if (get_prop_check_argc(retval, argc, &desc))
2778 return TRUE;
2779
2780 /* get the value to append, but leave it on the stack */
2781 vm_val_t *valp = G_stk->get(0);
2782
2783 /* the result object is 'self' */
2784 retval->set_obj(self);
2785
2786 /* push a self-reference for gc protection */
2787 G_stk->push(retval);
2788
2789 /* get the old size */
2790 int cnt = get_element_count();
2791
2792 /* get the number of items to add */
2793 int add_cnt = valp->ll_length(vmg0_);
2794
2795 /* expand myself to make room for the new elements */
2796 expand_by(vmg_ self, add_cnt);
2797
2798 /* add the new elements */
2799 for (int i = 1 ; i <= add_cnt ; ++i)
2800 {
2801 /* get the next new element */
2802 vm_val_t ele;
2803 valp->ll_index(vmg_ &ele, i);
2804
2805 /*
2806 * add the new element - note that we don't need to keep undo
2807 * because we created this new element in this same operation, and
2808 * hence undoing the operation will truncate the vector to exclude
2809 * the element, and hence we don't need an old value for the
2810 * element
2811 */
2812 set_element(cnt + i - 1, &ele);
2813 }
2814
2815 /* discard the argument and gc protection */
2816 G_stk->discard(2);
2817
2818 /* handled */
2819 return TRUE;
2820 }
2821
2822 /* ------------------------------------------------------------------------ */
2823 /*
2824 * property evaluator - splice
2825 */
2826 int CVmObjVector::getp_splice(VMG_ vm_obj_id_t self, vm_val_t *retval,
2827 uint *oargc)
2828 {
2829 /* check arguments */
2830 uint argc = (oargc != 0 ? *oargc : 0);
2831 static CVmNativeCodeDesc desc(2, 0, TRUE);
2832 if (get_prop_check_argc(retval, oargc, &desc))
2833 return TRUE;
2834
2835 /* get the current vector size */
2836 int old_cnt = get_element_count();
2837
2838 /* get the starting index, and adjust a zero or negative index */
2839 int start_idx = CVmBif::pop_int_val(vmg0_);
2840 if (start_idx <= 0)
2841 start_idx += old_cnt + 1;
2842
2843 /* adjust to a zero-based starting index */
2844 --start_idx;
2845
2846 /* make sure the starting index is in range */
2847 if (start_idx < 0 || start_idx > old_cnt)
2848 err_throw(VMERR_INDEX_OUT_OF_RANGE);;
2849
2850 /* get the deletion count */
2851 int del_cnt = CVmBif::pop_int_val(vmg0_);
2852
2853 /* adjust the deletion count to the number of elements remaining */
2854 if (del_cnt > old_cnt - start_idx)
2855 del_cnt = old_cnt - start_idx;
2856
2857 /* get a pointer to the first inserted item, and the number of items */
2858 vm_val_t *ins = G_stk->get(0);
2859 int ins_cnt = argc - 2;
2860
2861 /* the result object is 'self'; push self for gc protection */
2862 retval->set_obj(self);
2863 G_stk->push(retval);
2864
2865 /*
2866 * If we're doing a net deletion, contract the vector. If we're doing
2867 * a net expansion, expand it.
2868 */
2869 if (del_cnt > ins_cnt)
2870 {
2871 /* net deletion - remove the excess of deletions over insertions */
2872 remove_elements_undo(
2873 vmg_ self, start_idx + ins_cnt, del_cnt - ins_cnt);
2874 }
2875 else if (del_cnt < ins_cnt)
2876 {
2877 /* net insertion - add room for the excess inserted items */
2878 insert_elements_undo(
2879 vmg_ self, start_idx + del_cnt, ins_cnt - del_cnt);
2880 }
2881
2882 /* set the new elements */
2883 for (int i = 0 ; i < ins_cnt ; ++i, --ins)
2884 {
2885 /*
2886 * If this element is within the old vector's size range, save
2887 * undo, since we're overwriting a pre-existing element. If it's
2888 * beyond the end of the old vector, there's no need to keep undo
2889 * since the overall undo operation will simply shrink the vector
2890 * to eliminate the slot.
2891 */
2892 if (i < old_cnt)
2893 set_element_undo(vmg_ self, start_idx + i, ins);
2894 else
2895 set_element(start_idx + i, ins);
2896 }
2897
2898 /* discard the arguments and gc protection */
2899 G_stk->discard(ins_cnt + 1);
2900
2901 /* handled */
2902 return TRUE;
2903 }
2904
2905 /* ------------------------------------------------------------------------ */
2906 /*
2907 * Insert room for new elements. Inserts the given number of elements at
2908 * the given starting index. Leaves the old elements in the added slots
2909 * unchanged; the caller is responsible for setting the element values.
2910 */
2911 void CVmObjVector::insert_elements_undo(VMG_ vm_obj_id_t self,
2912 int ins_idx, int add_cnt)
2913 {
2914 /* note the new and old sizes */
2915 int old_cnt = get_element_count();
2916 int new_cnt = old_cnt + add_cnt;
2917
2918 /* create the new elements */
2919 set_element_count_undo(vmg_ self, new_cnt);
2920
2921 /*
2922 * Move the existing elements to their new slots. Start with the
2923 * starting index, and move it to its new position, since the starting
2924 * index is the first element that needs to be moved. Note that we
2925 * start at the top and work down, to avoid overwriting any overlapping
2926 * part of the new position block and the old position block.
2927 *
2928 * If we're inserting after the last element, there's no moving that
2929 * needs to be done.
2930 */
2931 if (ins_idx < old_cnt)
2932 {
2933 for (int idx = old_cnt - 1 ; idx >= ins_idx ; --idx)
2934 {
2935 /* get the value at this slot */
2936 vm_val_t ele;
2937 get_element(idx, &ele);
2938
2939 /* calculate the new slot */
2940 int new_idx = idx + add_cnt;
2941
2942 /*
2943 * if the new index is within the old size range, keep undo for
2944 * the change; otherwise, no undo is necessary, since when we
2945 * undo we'll be completely dropping this new slot
2946 */
2947 if (new_idx < old_cnt)
2948 set_element_undo(vmg_ self, new_idx, &ele);
2949 else
2950 set_element(new_idx, &ele);
2951 }
2952 }
2953 }
2954
2955
2956 /*
2957 * Delete a range of elements
2958 */
2959 void CVmObjVector::remove_elements_undo(VMG_ vm_obj_id_t self,
2960 size_t start_idx, size_t del_cnt)
2961 {
2962 /* note the original size of the vector */
2963 size_t cnt = get_element_count();
2964
2965 /* move all of the elements to their new slots, keeping undo */
2966 for (size_t idx = start_idx ; idx < cnt ; ++idx)
2967 {
2968 /*
2969 * calculate the index of the element to replace this one - it's
2970 * past us by the number of items being deleted
2971 */
2972 size_t move_idx = idx + del_cnt;
2973
2974 /*
2975 * if we're moving from a valid index in the old vector, get the
2976 * element; otherwise, replace the value with nil (but still
2977 * replace the value, since we want to keep undo for its original
2978 * value)
2979 */
2980 vm_val_t val;
2981 if (move_idx < cnt)
2982 get_element(move_idx, &val);
2983 else
2984 val.set_nil();
2985
2986 /* store the element at its new position */
2987 set_element_undo(vmg_ self, idx, &val);
2988 }
2989
2990 /*
2991 * Reduce the size, keeping undo. Note that it's important we do
2992 * this last: undo is applied in reverse order, so when applying
2993 * undo, we first want to increase the vector's size to its original
2994 * size, then we want to apply the element value restorations.
2995 */
2996 set_element_count_undo(vmg_ self, cnt - del_cnt);
2997 }
2998
2999 /* ------------------------------------------------------------------------ */
3000 /*
3001 * Property evaluator - join into a string
3002 */
3003 int CVmObjVector::getp_join(VMG_ vm_obj_id_t self, vm_val_t *retval,
3004 uint *argc)
3005 {
3006 /* use the generic list joiner */
3007 vm_val_t vself;
3008 vself.set_obj(self);
3009 return CVmObjList::getp_join(vmg_ retval, &vself, 0, argc);
3010 }
3011
3012 /*
3013 * C++ interface to join()
3014 */
3015 void CVmObjVector::join(VMG_ vm_val_t *retval, vm_obj_id_t self,
3016 const char *sep, size_t sep_len) const
3017 {
3018 vm_val_t vself;
3019 vself.set_obj(self);
3020 return CVmObjList::join(vmg_ retval, &vself, sep, sep_len);
3021 }
3022
3023 /* ------------------------------------------------------------------------ */
3024 /*
3025 * Explicitly convert to a string
3026 */
3027 const char *CVmObjVector::explicit_to_string(
3028 VMG_ vm_obj_id_t self, vm_val_t *new_str, int radix, int flags) const
3029 {
3030 /* use the generic list converter */
3031 vm_val_t vself;
3032 vself.set_obj(self);
3033 return CVmObjList::list_to_string(vmg_ new_str, &vself, radix, flags);
3034 }
3035
3036 /* ------------------------------------------------------------------------ */
3037 /*
3038 * Property evaluator - generate a new vector
3039 */
3040 int CVmObjVector::static_getp_generate(VMG_ vm_val_t *retval, uint *in_argc)
3041 {
3042 /* check arguments */
3043 uint argc = (in_argc == 0 ? 0 : *in_argc);
3044 static CVmNativeCodeDesc desc(2);
3045 if (get_prop_check_argc(retval, in_argc, &desc))
3046 return TRUE;
3047
3048 /* get the callback function argument */
3049 vm_val_t *func = G_stk->get(0);
3050
3051 /* make sure it's a function; set up a header pointer if it is */
3052 CVmFuncPtr funcdesc;
3053 if (!funcdesc.set(vmg_ func))
3054 err_throw(VMERR_BAD_TYPE_BIF);
3055
3056 /* get the count */
3057 int32_t cnt = G_stk->get(1)->num_to_int(vmg0_);
3058
3059 /* make sure it's not negative */
3060 if (cnt < 0)
3061 err_throw(VMERR_BAD_VAL_BIF);
3062
3063 /* create the vector */
3064 retval->set_obj(CVmObjVector::create(vmg_ FALSE, cnt));
3065 CVmObjVector *vec = (CVmObjVector *)vm_objp(vmg_ retval->val.obj);
3066
3067 /* push the new vector for gc protection */
3068 G_stk->push(retval);
3069
3070 /* find out how many arguments the function wants */
3071 int fargc = funcdesc.is_varargs() ? -1 : funcdesc.get_max_argc();
3072
3073 /* set up our recursive call descriptor */
3074 vm_rcdesc rc(vmg_ "Vector.generate",
3075 CVmObjVector::metaclass_reg_->get_class_obj(vmg0_),
3076 PROPIDX_generate, func, argc);
3077
3078 /* generate the elements */
3079 for (int i = 0 ; i < cnt ; ++i)
3080 {
3081 /* push the callback arguments */
3082 const int pushed_argc = 1;
3083 G_stk->push()->set_int(i+1);
3084
3085 /* adjust the arguments for what the callback actually wants */
3086 int sent_argc = fargc < 0 || fargc > pushed_argc ? pushed_argc : fargc;
3087
3088 /* call the callback */
3089 G_interpreter->call_func_ptr(vmg_ func, sent_argc, &rc, 0);
3090
3091 /* discard excess arguments */
3092 G_stk->discard(pushed_argc - sent_argc);
3093
3094 /* add the result to the list */
3095 vec->set_element(i, G_interpreter->get_r0());
3096 vec->set_element_count(i+1);
3097 }
3098
3099 /* discard arguments and gc protection */
3100 G_stk->discard(argc + 1);
3101
3102 /* handled */
3103 return TRUE;
3104 }
3105
3106 /* ------------------------------------------------------------------------ */
3107 /*
3108 * Property evaluator: get the index of the element with the minimum value
3109 */
3110 int CVmObjVector::getp_indexOfMin(VMG_ vm_obj_id_t self,
3111 vm_val_t *retval, uint *argc)
3112 {
3113 /* do the general calculation to select the minimum value ("-1") */
3114 vm_rcdesc rc(vmg_ "Vector.indexOfMin", self, 32, G_stk->get(0),
3115 argc != 0 ? *argc : 0);
3116 return get_minmax(vmg_ self, retval, argc, &rc, -1, TRUE);
3117 }
3118
3119 /*
3120 * Property evaluator: get the value of the element with the minimum value
3121 */
3122 int CVmObjVector::getp_minVal(VMG_ vm_obj_id_t self,
3123 vm_val_t *retval, uint *argc)
3124 {
3125 /* do the general calculation to select the minimum value ("-1") */
3126 vm_rcdesc rc(vmg_ "Vector.minVal", self, 33, G_stk->get(0),
3127 argc != 0 ? *argc : 0);
3128 return get_minmax(vmg_ self, retval, argc, &rc, -1, FALSE);
3129 }
3130
3131 /*
3132 * Property evaluator: get the index of the element with the maximum value
3133 */
3134 int CVmObjVector::getp_indexOfMax(VMG_ vm_obj_id_t self,
3135 vm_val_t *retval, uint *argc)
3136 {
3137 /* do the general calculation to select the maximum value ("+1") */
3138 vm_rcdesc rc(vmg_ "Vector.indexOfMax", self, 34, G_stk->get(0),
3139 argc != 0 ? *argc : 0);
3140 return get_minmax(vmg_ self, retval, argc, &rc, 1, TRUE);
3141 }
3142
3143 /*
3144 * Property evaluator: get the value of the element with the maximum value
3145 */
3146 int CVmObjVector::getp_maxVal(VMG_ vm_obj_id_t self,
3147 vm_val_t *retval, uint *argc)
3148 {
3149 /* do the general calculation to select the maximum value ("+1") */
3150 vm_rcdesc rc(vmg_ "Vector.maxVal", self, 35, G_stk->get(0),
3151 argc != 0 ? *argc : 0);
3152 return get_minmax(vmg_ self, retval, argc, &rc, 1, FALSE);
3153 }
3154
3155 /*
3156 * General handling for minIndex, minVal, maxIndex, and maxVal. This finds
3157 * the highest/lowest value in the list, optionally mapped through a
3158 * callback function. Returns the winning element's index and value.
3159 *
3160 * 'sense' is the sense of the search: -1 for minimum, 1 for maximum.
3161 * 'want_index' is true if 'retval' should be set to the index of the
3162 * winning element, false if 'retval' should be set to the winning
3163 * element's value.
3164 */
3165 int CVmObjVector::get_minmax(VMG_ vm_obj_id_t self, vm_val_t *retval,
3166 uint *in_argc, const vm_rcdesc *rc,
3167 int sense, int want_index)
3168 {
3169 /* check arguments */
3170 uint argc = (in_argc == 0 ? 0 : *in_argc);
3171 static CVmNativeCodeDesc desc(0, 1);
3172 if (get_prop_check_argc(retval, in_argc, &desc))
3173 return TRUE;
3174
3175 /* make sure the 'func' argument is valid */
3176 const vm_val_t *cb = 0;
3177 if (argc >= 1)
3178 {
3179 cb = G_stk->get(0);
3180 if (!cb->is_func_ptr(vmg0_))
3181 err_throw(VMERR_BAD_TYPE_BIF);
3182 }
3183
3184 /* push 'self' for gc protection */
3185 G_stk->push()->set_obj(self);
3186
3187 /* if there are no elements, this is an error */
3188 int cnt = get_element_count();
3189 if (cnt == 0)
3190 err_throw(VMERR_BAD_VAL_BIF);
3191
3192 /* we don't have a winner yet */
3193 int winner_idx = 0;
3194 vm_val_t winner;
3195
3196 /* run through the list */
3197 for (int i = 0 ; i < cnt ; ++i)
3198 {
3199 /* get this element */
3200 vm_val_t ele;
3201 get_element(i, &ele);
3202
3203 /* if there's a callback, invoke it */
3204 if (cb != 0)
3205 {
3206 /* invoke the callback with the element value as the argument */
3207 G_stk->push(&ele);
3208 G_interpreter->call_func_ptr(vmg_ cb, 1, rc, 0);
3209
3210 /* use the result as the comparison value */
3211 ele = *G_interpreter->get_r0();
3212 }
3213
3214 /* compare it to the winner so far, and keep the lower value */
3215 if (i == 0)
3216 {
3217 /* it's the first element, so it's the default winner so far */
3218 winner_idx = i;
3219 winner = ele;
3220 }
3221 else
3222 {
3223 /* compare it to the winner so far */
3224 int cmp = ele.compare_to(vmg_ &winner);
3225
3226 /* keep it if it's the min/max so far */
3227 if ((sense > 0 && cmp > 0) || (sense < 0 && cmp < 0))
3228 {
3229 winner_idx = i;
3230 winner = ele;
3231 }
3232 }
3233 }
3234
3235 /* remove gc protection and arguments */
3236 G_stk->discard(argc + 1);
3237
3238 /* return the winning index (note: 1-based) or value, as desired */
3239 if (want_index)
3240 retval->set_int(winner_idx + 1);
3241 else
3242 *retval = winner;
3243
3244 /* handled */
3245 return TRUE;
3246 }