Debian Codegraph @

terps/tads/tads3/vmlst.h

raw symbols SCIP
1 /* $Header: d:/cvsroot/tads/tads3/VMLST.H,v 1.2 1999/05/17 02:52:28 MJRoberts Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmlst.h - VM dynamic list implementation
12 Function
13
14 Notes
15
16 Modified
17 10/29/98 MJRoberts - Creation
18 */
19
20 #ifndef VMLST_H
21 #define VMLST_H
22
23 #include <stdlib.h>
24 #include "vmtype.h"
25 #include "vmobj.h"
26 #include "vmcoll.h"
27 #include "vmglob.h"
28 #include "vmstack.h"
29 #include "vmrun.h"
30
31
32 class CVmObjList: public CVmObjCollection
33 {
34 friend class CVmMetaclassList;
35
36 public:
37 /* metaclass registration object */
38 static class CVmMetaclass *metaclass_reg_;
39 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
40
41 /* am I of the given metaclass? */
42 virtual int is_of_metaclass(class CVmMetaclass *meta) const
43 {
44 /* try my own metaclass and my base class */
45 return (meta == metaclass_reg_
46 || CVmObjCollection::is_of_metaclass(meta));
47 }
48
49 /* create dynamically using stack arguments */
50 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
51 uint argc);
52
53 /*
54 * Create dynamically from parameters in the stack; we do not remove
55 * the elements from the stack, but simply create a list from the
56 * parameters. 'idx' is the parameter index of the first parameter,
57 * and 'cnt' is the number of parameters to use.
58 */
59 static vm_obj_id_t create_from_params(VMG_ uint idx, uint cnt);
60
61 /* call a static property */
62 static int call_stat_prop(VMG_ vm_val_t *result,
63 const uchar **pc_ptr, uint *argc,
64 vm_prop_id_t prop);
65
66 /* reserve constant data */
67 virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper,
68 vm_obj_id_t self);
69
70 /* convert to constant data */
71 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
72 vm_obj_id_t self);
73
74 /* get my datatype when converted to constant data */
75 virtual vm_datatype_t get_convert_to_const_data_type() const
76 { return VM_LIST; }
77
78 /* create a list with no initial contents */
79 static vm_obj_id_t create(VMG_ int in_root_set);
80
81 /*
82 * create a list with a given number of elements, for construction
83 * of the list element-by-element
84 */
85 static vm_obj_id_t create(VMG_ int in_root_set, size_t element_count);
86
87 /*
88 * create a list from a constant list, for construction of the list
89 * as a modified copy of an original list
90 */
91 static vm_obj_id_t create(VMG_ int in_root_set, const char *lst);
92
93 /* get an element, given a zero-based index */
94 void get_element(size_t idx, vm_val_t *val) const
95 {
96 /* get the data from the data holder in our extension */
97 vmb_get_dh(get_element_ptr(idx), val);
98 }
99
100 /*
101 * List construction: set an element. List contents are immutable, so
102 * they cannot be changed after the list is constructed. However, it
103 * is often convenient to construct a list one element at a time, so a
104 * caller can create the list with the appropriate number of elements,
105 * then use this routine to set each element of the list individually.
106 *
107 * idx is the index of the element in the list; the first element is at
108 * index zero. Note that this routine does *not* allocate memory; the
109 * list must be pre-allocated to its full number of elements. Use
110 * cons_ensure_space to expand the list as needed.
111 */
112 void cons_set_element(size_t idx, const vm_val_t *val);
113
114 /* update the list in place so that each value is unique */
115 void cons_uniquify(VMG0_);
116
117 /*
118 * Copy an existing list into our list, starting at a given index. The
119 * caller must ensure that our list buffer is large enough to
120 * accommodate the new elements. The 'orig_list' value must point to a
121 * standard list constant value: a UINT2 element count prefix followed
122 * by DATAHOLDER elements.
123 */
124 void cons_copy_elements(size_t start_idx, const char *orig_list);
125
126 /*
127 * Copy existing list elements into our list, starting at the given
128 * index. The caller must ensure that our list buffer is large enough
129 * to accommodate the new elements. The 'ele_array' is an array of
130 * DATAHOLDER values.
131 */
132 void cons_copy_data(size_t start_idx, const char *ele_array,
133 size_t ele_count);
134
135 /*
136 * Set the length of the list. This can be used when constructing a
137 * list, and the actual number of elements is unknown before
138 * construction is complete (however, the maximum number of elements
139 * must be known in advance, since this merely sets the length, and
140 * does NOT reallocate the list -- hence, this call can only be used
141 * to shrink the list below its allocated size, never to expand it).
142 */
143 void cons_set_len(size_t len)
144 { vmb_put_len(ext_, len); }
145
146 /*
147 * During construction, ensure there's enough space in the list to hold
148 * an added item at the given index (0-based). If not, expands the
149 * list to make it large enough to hold the given new elements plus the
150 * margin. The margin is to ensure that we don't keep reallocating one
151 * element at a time when the caller is adding items iteratively. The
152 * added elements are automatically set to nil.
153 */
154 void cons_ensure_space(VMG_ size_t idx, size_t margin);
155
156 /*
157 * During construction, clear a range of the list by setting each
158 * element in the range (inclusive of the endpoints) to nil. If the
159 * construction process could trigger garbage collection, it's
160 * important for the caller to clear the list first. Uninitialized
161 * elements could happen to look like pointers to invalid objects, so
162 * if the gc runs, it could try to follow one of these invalid pointers
163 * and cause a crash. The index values are 0-based.
164 */
165 void cons_clear(size_t start_idx, size_t end_idx);
166
167 /* clear the entire list (set the whole list to nil) */
168 void cons_clear()
169 {
170 if (get_ele_count() != 0)
171 cons_clear(0, get_ele_count() - 1);
172 }
173
174 /* notify of deletion */
175 void notify_delete(VMG_ int in_root_set);
176
177 /* set a property */
178 void set_prop(VMG_ class CVmUndo *undo,
179 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
180
181 /* get a property */
182 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
183 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
184
185 /* undo operations - lists are immutable and hence keep no undo */
186 void notify_new_savept() { }
187 void apply_undo(VMG_ struct CVmUndoRecord *) { }
188 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
189 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
190
191 /* mark references */
192 void mark_refs(VMG_ uint state);
193
194 /*
195 * remove weak references - we keep only normal (strong) references,
196 * so this routine doesn't need to do anything
197 */
198 void remove_stale_weak_refs(VMG0_) { }
199
200 /* load from an image file */
201 void load_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t)
202 { ext_ = (char *)ptr; }
203
204 /* rebuild for image file */
205 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
206
207 /* save to a file */
208 void save_to_file(VMG_ class CVmFile *fp);
209
210 /* restore from a file */
211 void restore_from_file(VMG_ vm_obj_id_t self,
212 class CVmFile *fp, class CVmObjFixup *fixups);
213
214 /*
215 * cast to string - returns a string consisting of the list elements
216 * converted to strings and concatenated together with commas
217 * separating elements; the result is the same as self.join(',')
218 */
219 const char *cast_to_string(VMG_ vm_obj_id_t self,
220 vm_val_t *new_str) const
221 {
222 /* set up a 'self' value */
223 vm_val_t self_val;
224 self_val.set_obj(self);
225
226 /* join the list elements into a string, using commas as separators */
227 join(vmg_ new_str, &self_val, ",", 1);
228
229 /* return the string result */
230 return new_str->get_as_string(vmg0_);
231 }
232
233 /* explicitly convert to string using toString() semantics */
234 virtual const char *explicit_to_string(
235 VMG_ vm_obj_id_t self, vm_val_t *new_str, int radix, int flags) const
236 {
237 /* set up a 'self' value */
238 vm_val_t self_val;
239 self_val.set_obj(self);
240
241 /* convert to string */
242 return list_to_string(vmg_ new_str, &self_val, radix, flags);
243 }
244
245 /*
246 * explicitly convert a list or list-like value to a string using
247 * toString() semantics
248 */
249 static const char *list_to_string(
250 VMG_ vm_val_t *result, const vm_val_t *self, int radix, int flags);
251
252
253 /*
254 * Add a value to the list. If the value to add is a list (constant
255 * or object), we'll append each element of the list to this list;
256 * otherwise, we'll just append the value itself to the list. In
257 * any case, we don't modify this list itself, but create a new list
258 * object to hold the result.
259 */
260 int add_val(VMG_ vm_val_t *result,
261 vm_obj_id_t self, const vm_val_t *val);
262
263 /*
264 * Index the list
265 */
266 int index_val_q(VMG_ vm_val_t *result, vm_obj_id_t self,
267 const vm_val_t *index_val);
268
269 /*
270 * Set an indexed element of the list. Since the contents of a list
271 * object cannot be changed, we'll return in *new_container a new
272 * list object that we create with the modified contents.
273 */
274 int set_index_val_q(VMG_ vm_val_t *new_container, vm_obj_id_t self,
275 const vm_val_t *index_val, const vm_val_t *new_val);
276
277 /*
278 * Subtract a value from the list. This creates a new list with the
279 * element matching the given value removed from the original list.
280 * We do not modify the original list; instead, we create a new list
281 * object with the new value.
282 */
283 int sub_val(VMG_ vm_val_t *result,
284 vm_obj_id_t self, const vm_val_t *val);
285
286 /*
287 * get as a list - simply return our extension, which is in the
288 * required portable list format
289 */
290 const char *get_as_list() const { return ext_; }
291
292 /* I'm the original list-like object */
293 int is_listlike(VMG_ vm_obj_id_t /*self*/) { return TRUE; }
294 int ll_length(VMG_ vm_obj_id_t /*self*/) { return get_ele_count(); }
295
296
297 /*
298 * Check a value for equality. We will match any constant list that
299 * contains the same data as our list, and any other list object
300 * with the same underlying data.
301 */
302 int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int depth) const;
303
304 /*
305 * Static list adder. This creates a new list object that results
306 * from appending the given value to the given list constant. This
307 * is defined statically so that this code can be shared for adding
308 * to constant pool lists and adding to CVmObjList objects.
309 *
310 * 'lstval' must point to a constant list. The first two bytes of
311 * the list are stored in portable UINT2 format and give the number
312 * of elements in the list; this is immediately followed by a packed
313 * array of data holders in portable format.
314 */
315 static void add_to_list(VMG_ vm_val_t *result,
316 vm_obj_id_t self, const char *lstval,
317 const vm_val_t *val);
318
319 /*
320 * Static list subtraction routine. This creates a new list object
321 * that results from removing the given value from the list
322 * constant. This is defined statically so that this code can be
323 * shared for subtracting from constant pool lists and subtracting
324 * from CVmObjList objects.
325 *
326 * 'lstmem' must point to a constant list in the same format as
327 * required for add_to_list. If 'lstmem' comes from the constant
328 * pool, then 'lstval' must be provided to give us the constant pool
329 * address; otherwise, 'lstval' should be null.
330 */
331 static void sub_from_list(VMG_ vm_val_t *result,
332 const vm_val_t *lstval, const char *lstmem,
333 const vm_val_t *val);
334
335 /*
336 * Constant list comparison routine. Compares the given list
337 * constant (in portable format, with leading UINT2 element count
338 * prefix followed by the list's elements in portable data holder
339 * format) to the other value. Returns true if the other value is a
340 * list constant or object whose contents match the list constant,
341 * false if not.
342 *
343 * If 'lstmem' comes from the constant pool, then 'lstval' must be
344 * provided to give us the constant pool address; otherwise,
345 * 'lstval' should be null.
346 */
347 static int const_equals(VMG_ const vm_val_t *lstval, const char *lstmem,
348 const vm_val_t *val, int depth);
349
350 /*
351 * Calculate a hash value for the list
352 */
353 uint calc_hash(VMG_ vm_obj_id_t self, int depth) const;
354
355 /*
356 * Constant list hash value calculation
357 */
358 static uint const_calc_hash(VMG_ const vm_val_t *self_val,
359 const char *lst, int depth);
360
361 /*
362 * Constant list indexing routine. Indexes the given constant list
363 * (which must be in portable format, with leading UINT2 element
364 * count followed by the list's elements in portable data holder
365 * format), looking up the value at the index number given by the
366 * index value, and puts the result in *result.
367 */
368 static void index_list(VMG_ vm_val_t *result,
369 const char *lst, const vm_val_t *index_val);
370
371 /* index a list, using a 1-based index */
372 static void index_list(VMG_ vm_val_t *result, const char *lst, uint idx);
373
374 /* push the indexed element, using a 1-based index */
375 static void index_and_push(VMG_ const char *lst, uint idx)
376 {
377 vm_val_t *p;
378
379 /* push a new stack element */
380 p = G_stk->push();
381
382 /* index the list and store the value directly in the stack */
383 index_list(vmg_ p, lst, idx);
384 }
385
386 /*
387 * Constant list set-index routine. Creates a new list object as a
388 * copy of this list, with the element at the given index set to the
389 * given new value.
390 */
391 static void set_index_list(VMG_ vm_val_t *result,
392 const char *lst, const vm_val_t *index_val,
393 const vm_val_t *new_val);
394
395 /*
396 * Find a value within a list. If we find the value, we'll set *idxp
397 * to the index (starting at 1 for the first element) of the item we
398 * found, and we'll return true; if we don't find the value, we'll
399 * return false.
400 */
401 static int find_in_list(VMG_ const vm_val_t *lst,
402 const vm_val_t *val, size_t *idxp);
403
404 /* find the last match for a value */
405 static int find_last_in_list(VMG_ const vm_val_t *lst,
406 const vm_val_t *val, size_t *idxp);
407
408 /*
409 * Evaluate a property of a constant list value. Returns true if we
410 * successfully evaluated the property, false if the property is not
411 * one of the properties that the list class defines.
412 */
413 static int const_get_prop(VMG_ vm_val_t *retval, const vm_val_t *self_val,
414 const char *lst, vm_prop_id_t prop,
415 vm_obj_id_t *srcobj, uint *argc);
416
417 /* property evaluator - undefined property */
418 static int getp_undef(VMG_ vm_val_t *, const vm_val_t *,
419 const char *, uint *)
420 { return FALSE; }
421
422 /* property evaluator - select a subset through a callback */
423 static int getp_subset(VMG_ vm_val_t *retval, const vm_val_t *self_val,
424 const char *lst, uint *argc);
425
426 /* property evaluator - apply a callback to each element */
427 static int getp_map(VMG_ vm_val_t *retval, const vm_val_t *self_val,
428 const char *lst, uint *argc);
429
430 /* get the length */
431 static int getp_len(VMG_ vm_val_t *retval, const vm_val_t *self_val,
432 const char *lst, uint *argc);
433
434 /* sublist */
435 static int getp_sublist(VMG_ vm_val_t *retval, const vm_val_t *self_val,
436 const char *lst, uint *argc);
437
438 /* intersect */
439 static int getp_intersect(VMG_ vm_val_t *retval,
440 const vm_val_t *self_val,
441 const char *lst, uint *argc);
442
443 /* indexOf */
444 static int getp_index_of(VMG_ vm_val_t *retval, const vm_val_t *self_val,
445 const char *lst, uint *argc);
446
447 /* car */
448 static int getp_car(VMG_ vm_val_t *retval, const vm_val_t *self_val,
449 const char *lst, uint *argc);
450
451 /* cdr */
452 static int getp_cdr(VMG_ vm_val_t *retval, const vm_val_t *self_val,
453 const char *lst, uint *argc);
454
455 /* indexWhich */
456 static int getp_index_which(VMG_ vm_val_t *retval,
457 const vm_val_t *self_val,
458 const char *lst, uint *argc);
459
460 /* forEach */
461 static int getp_for_each(VMG_ vm_val_t *retval, const vm_val_t *self_val,
462 const char *lst, uint *argc);
463
464 /* forEachAssoc */
465 static int getp_for_each_assoc(VMG_ vm_val_t *retval,
466 const vm_val_t *self_val,
467 const char *lst, uint *argc);
468
469 /* valWhich */
470 static int getp_val_which(VMG_ vm_val_t *retval,
471 const vm_val_t *self_val,
472 const char *lst, uint *argc);
473
474 /* lastIndexOf */
475 static int getp_last_index_of(VMG_ vm_val_t *retval,
476 const vm_val_t *self_val,
477 const char *lst, uint *argc);
478
479 /* lastIndexWhich */
480 static int getp_last_index_which(VMG_ vm_val_t *retval,
481 const vm_val_t *self_val,
482 const char *lst, uint *argc);
483
484 /* lastValWhich */
485 static int getp_last_val_which(VMG_ vm_val_t *retval,
486 const vm_val_t *self_val,
487 const char *lst, uint *argc);
488
489 /* countOf */
490 static int getp_count_of(VMG_ vm_val_t *retval,
491 const vm_val_t *self_val,
492 const char *lst, uint *argc);
493
494 /* countWhich */
495 static int getp_count_which(VMG_ vm_val_t *retval,
496 const vm_val_t *self_val,
497 const char *lst, uint *argc);
498
499 /* general routine for indexWhich and lastIndexWhich */
500 static int gen_index_which(VMG_ vm_val_t *retval,
501 const vm_val_t *self_val,
502 const char *lst, uint *argc,
503 int forward, vm_rcdesc *rc);
504
505 /* getUnique */
506 static int getp_get_unique(VMG_ vm_val_t *retval,
507 const vm_val_t *self_val,
508 const char *lst, uint *argc);
509
510 /* appendUnique */
511 static int getp_append_unique(VMG_ vm_val_t *retval,
512 const vm_val_t *self_val,
513 const char *lst, uint *argc);
514
515 /* appendUnique - internal interface */
516 static void append_unique(VMG_ vm_val_t *retval,
517 const vm_val_t *self, const vm_val_t *other);
518
519 /* append */
520 static int getp_append(VMG_ vm_val_t *retval,
521 const vm_val_t *self_val,
522 const char *lst, uint *argc);
523
524 /* sort */
525 static int getp_sort(VMG_ vm_val_t *retval,
526 const vm_val_t *self_val,
527 const char *lst, uint *argc);
528
529 /* insertAt */
530 static int getp_insert_at(VMG_ vm_val_t *retval,
531 const vm_val_t *self_val,
532 const char *lst, uint *argc);
533
534 /* prepend */
535 static int getp_prepend(VMG_ vm_val_t *retval,
536 const vm_val_t *self_val,
537 const char *lst, uint *argc);
538
539 /* property evaluator - remove a single element at a given index */
540 static int getp_remove_element_at(VMG_ vm_val_t *retval,
541 const vm_val_t *self_val,
542 const char *lst, uint *argc);
543
544 /* property evaluator - removeRange */
545 static int getp_remove_range(VMG_ vm_val_t *retval,
546 const vm_val_t *self_val,
547 const char *lst, uint *argc);
548
549 /* property evaluator - splice */
550 static int getp_splice(VMG_ vm_val_t *retval,
551 const vm_val_t *self_val,
552 const char *lst, uint *argc);
553
554 /* property evaluator - join */
555 static int getp_join(VMG_ vm_val_t *retval,
556 const vm_val_t *self_val,
557 const char *lst, uint *argc);
558
559 /*
560 * join the elements of a list into a string; this is equivalent to the
561 * bytecode-level join() method, but can be called more conveniently
562 * from C++ code
563 */
564 static void join(VMG_ vm_val_t *retval,
565 const vm_val_t *self_val,
566 const char *sep, size_t sep_len);
567
568 /* property evaluator - generate */
569 static int getp_generate(VMG_ vm_val_t *retval,
570 const vm_val_t * /*self*/,
571 const char * /*lst*/, uint *argc)
572 { return static_getp_generate(vmg_ retval, argc); }
573
574 /* static property evaluator - generate */
575 static int static_getp_generate(VMG_ vm_val_t *retval, uint *argc);
576
577 /* property evaluator - index of minimum value */
578 static int getp_indexOfMin(VMG_ vm_val_t *retval,
579 const vm_val_t *self_val,
580 const char *lst, uint *argc);
581
582 /* property evaluator - minimum value in list */
583 static int getp_minVal(VMG_ vm_val_t *retval,
584 const vm_val_t *self_val,
585 const char *lst, uint *argc);
586
587 /* property evaluator - index of maximum value in list */
588 static int getp_indexOfMax(VMG_ vm_val_t *retval,
589 const vm_val_t *self_val,
590 const char *lst, uint *argc);
591
592 /* property evaluator - maximum value in list */
593 static int getp_maxVal(VMG_ vm_val_t *retval,
594 const vm_val_t *self_val,
595 const char *lst, uint *argc);
596
597
598 protected:
599 /* general processor for forEach and forEachAssoc */
600 static int for_each_gen(VMG_ vm_val_t *retval,
601 const vm_val_t *self_val,
602 const char *lst, uint *argc,
603 int send_idx_to_cb, vm_rcdesc *rc);
604
605 /* general min/max handler, for minIndex, minVal, maxIndex, maxVal */
606 static int get_minmax(VMG_ vm_val_t *retval,
607 const vm_val_t *self_val,
608 const char *lst, uint *in_argc,
609 const vm_rcdesc *rc,
610 int sense, int want_index);
611
612 /*
613 * Compute the intersection of two lists. Returns a new list with the
614 * elements that occur in both lists.
615 */
616 static vm_obj_id_t intersect(VMG_ const vm_val_t *l1,
617 const vm_val_t *l2);
618
619 /* insert the arguments into the list at the given index */
620 static void insert_elements(VMG_ vm_val_t *retval,
621 const vm_val_t *self_val,
622 const char *lst, uint argc, int idx);
623
624 /* remove elements */
625 static void remove_range(VMG_ vm_val_t *retval,
626 const vm_val_t *self_val,
627 const char *lst, int start_idx, int del_cnt);
628
629 /* create an iterator */
630 virtual void new_iterator(VMG_ vm_val_t *retval,
631 const vm_val_t *self_val);
632
633 /*
634 * create a live iterator - for a list, there is no difference
635 * between snapshot and live iterators, since a list is immutable
636 */
637 virtual void new_live_iterator(VMG_ vm_val_t *retval,
638 const vm_val_t *self_val)
639 { new_iterator(vmg_ retval, self_val); }
640
641 /* get the number of elements in the list */
642 size_t get_ele_count() const { return vmb_get_len(ext_); }
643
644 /* get the number of elements in a constant list */
645 static size_t get_ele_count_const(const char *lstval)
646 { return vmb_get_len(lstval); }
647
648 /* get an element from a constant list, given a zero-based index */
649 static void get_element_const(const char *lstval, size_t idx,
650 vm_val_t *val)
651 {
652 /* get the data from the data holder in the constant list */
653 vmb_get_dh(get_element_ptr_const(lstval, idx), val);
654 }
655
656 /* given an index, get a pointer to the element's data in the list */
657 char *get_element_ptr(size_t idx) const
658 {
659 /*
660 * figure out where this element's data holder is by skipping
661 * the count prefix, then skipping past preceding data holders
662 */
663 return ext_ + VMB_LEN + (idx * VMB_DATAHOLDER);
664 }
665
666 /*
667 * given an index, and a pointer to a constant list, get a pointer
668 * to the element's data in the list constant
669 */
670 static const char *get_element_ptr_const(const char *lstval, size_t idx)
671 {
672 /*
673 * figure out where this element's data holder is by skipping
674 * the count prefix, then skipping past preceding data holders
675 */
676 return lstval + VMB_LEN + (idx * VMB_DATAHOLDER);
677 }
678
679 /*
680 * given a pointer to a list element, increment the pointer so that
681 * it points to the next element
682 */
683 static void inc_element_ptr(char **p)
684 {
685 /* add the size of a data holder to the current pointer */
686 *p += VMB_DATAHOLDER;
687 }
688
689 /* increment a constant element pointer */
690 static void inc_const_element_ptr(const char **p)
691 {
692 /* add the size of a data holder to the current pointer */
693 *p += VMB_DATAHOLDER;
694 }
695
696 /* create a list with no initial contents */
697 CVmObjList() { ext_ = 0; }
698
699 /*
700 * create a list with a given number of elements, for construction
701 * of the list element-by-element
702 */
703 CVmObjList(VMG_ size_t element_count);
704
705 /* create a list from a constant list */
706 CVmObjList(VMG_ const char *lst);
707
708 /*
709 * Calculate the amount of space we need to store a list of a given
710 * length. We require two bytes for the length prefix, plus the
711 * space for each element.
712 */
713 static size_t calc_alloc(size_t elecnt)
714 { return (VMB_LEN + (elecnt * VMB_DATAHOLDER)); }
715
716 /* allocate space for the list, given the number of elements */
717 void alloc_list(VMG_ size_t element_count);
718
719 /* property evaluation function table */
720 static int (*func_table_[])(VMG_ vm_val_t *retval,
721 const vm_val_t *self_val,
722 const char *lst, uint *argc);
723 };
724
725
726 /* ------------------------------------------------------------------------ */
727 /*
728 * A constant list is exactly like an ordinary list, except that our
729 * contents come from the constant pool. We store a pointer directly to
730 * our constant pool data rather than making a separate copy. The only
731 * thing we have to do differently from an ordinary list is that we don't
732 * delete our extension when we're deleted, since our extension is really
733 * just a pointer into the constant pool.
734 */
735 class CVmObjListConst: public CVmObjList
736 {
737 public:
738 /* notify of deletion */
739 void notify_delete(VMG_ int /*in_root_set*/)
740 {
741 /*
742 * do nothing, since our extension is just a pointer into the
743 * constant pool
744 */
745 }
746
747 /* create from constant pool data */
748 static vm_obj_id_t create(VMG_ const char *const_ptr);
749
750 protected:
751 /* construct from constant pool data */
752 CVmObjListConst(VMG_ const char *const_ptr)
753 {
754 /* point our extension directly to the constant pool data */
755 ext_ = (char *)const_ptr;
756 }
757 };
758
759 /* ------------------------------------------------------------------------ */
760 /*
761 * Registration table object
762 */
763 class CVmMetaclassList: public CVmMetaclass
764 {
765 public:
766 /* get the global name */
767 const char *get_meta_name() const { return "list/030008"; }
768
769 /* create from image file */
770 void create_for_image_load(VMG_ vm_obj_id_t id)
771 {
772 new (vmg_ id) CVmObjList();
773 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
774 }
775
776 /* create from restoring from saved state */
777 void create_for_restore(VMG_ vm_obj_id_t id)
778 {
779 new (vmg_ id) CVmObjList();
780 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
781 }
782
783 /* create dynamically using stack arguments */
784 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
785 { return CVmObjList::create_from_stack(vmg_ pc_ptr, argc); }
786
787 /* call a static property */
788 int call_stat_prop(VMG_ vm_val_t *result,
789 const uchar **pc_ptr, uint *argc,
790 vm_prop_id_t prop)
791 {
792 return CVmObjList::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
793 }
794
795 /* I'm a Collection object */
796 CVmMetaclass *get_supermeta_reg() const
797 { return CVmObjCollection::metaclass_reg_; }
798 };
799
800 #endif /* VMLST_H */
801
802 /*
803 * Register the class
804 */
805 VM_REGISTER_METACLASS(CVmObjList)
806