Debian Codegraph @

terps/tads/tads3/vmlst.cpp

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