Debian Codegraph @

terps/tads/tads3/vmtobj.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/VMTOBJ.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 vmtobj.cpp - TADS object implementation
15 Function
16
17 Notes
18
19 Modified
20 10/30/98 MJRoberts - Creation
21 */
22
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <stdarg.h>
26
27 #include "t3std.h"
28 #include "vmglob.h"
29 #include "vmerr.h"
30 #include "vmerrnum.h"
31 #include "vmobj.h"
32 #include "vmtobj.h"
33 #include "vmundo.h"
34 #include "vmtype.h"
35 #include "vmfile.h"
36 #include "vmstack.h"
37 #include "vmrun.h"
38 #include "vmpredef.h"
39 #include "vmmeta.h"
40 #include "vmlst.h"
41 #include "vmintcls.h"
42 #include "vmbif.h"
43 #include "vmstr.h"
44
45
46 /* ------------------------------------------------------------------------ */
47 /*
48 * Allocate a new object header
49 */
50 vm_tadsobj_hdr *vm_tadsobj_hdr::alloc(VMG_ CVmObjTads *self,
51 unsigned short sc_cnt,
52 unsigned short prop_cnt)
53 {
54 ushort hash_siz;
55 size_t siz;
56 size_t i;
57 vm_tadsobj_hdr *hdr;
58 char *mem;
59 vm_tadsobj_prop **hashp;
60
61 /*
62 * Figure the size of the hash table to allocate. Empirically, a good
63 * size seems to be about one hash slot per property.
64 */
65 if (prop_cnt <= 2)
66 hash_siz = 2;
67 else if (prop_cnt <= 4)
68 hash_siz = 4;
69 else if (prop_cnt <= 8)
70 hash_siz = 8;
71 else if (prop_cnt <= 16)
72 hash_siz = 16;
73 else if (prop_cnt <= 32)
74 hash_siz = 32;
75 else if (prop_cnt <= 64)
76 hash_siz = 64;
77 else if (prop_cnt <= 128)
78 hash_siz = 128;
79 else
80 hash_siz = 256;
81
82 /*
83 * increase the requested property count to the hash size at a minimum
84 * - this will avoid the need to reallocate the object to make room
85 * for more properties until we'd have to resize the hash table, at
86 * which point we have to reallocate the object anyway
87 */
88 if (prop_cnt < hash_siz)
89 prop_cnt = hash_siz;
90
91 /* figure the size of the structure we need */
92 siz = sizeof(vm_tadsobj_hdr)
93 + (sc_cnt - 1) * sizeof(hdr->sc[0])
94 + (hash_siz) * sizeof(hdr->hash_arr[0])
95 + prop_cnt * sizeof(hdr->prop_entry_arr[0]);
96
97 /* allocate the memory */
98 hdr = (vm_tadsobj_hdr *)G_mem->get_var_heap()->alloc_mem(siz, self);
99
100 /*
101 * Set up to suballocate out of this block. Free memory in the block
102 * starts after our structure and the array of superclass entries.
103 */
104 mem = (char *)&hdr->sc[sc_cnt];
105
106 /* clear our flags and load-image flags */
107 hdr->li_obj_flags = 0;
108 hdr->intern_obj_flags = 0;
109
110 /* the object has no precalculated inheritance path yet */
111 hdr->inh_path = 0;
112
113 /* suballocate the hash buckets */
114 hdr->hash_siz = hash_siz;
115 hdr->hash_arr = (vm_tadsobj_prop **)mem;
116
117 /* clear out the hash buckets */
118 for (hashp = hdr->hash_arr, i = hash_siz ; i != 0 ; ++hashp, --i)
119 *hashp = 0;
120
121 /* move past the memory taken by the hash buckets */
122 mem = (char *)(hdr->hash_arr + hash_siz);
123
124 /* suballocate the array of hash entries */
125 hdr->prop_entry_cnt = prop_cnt;
126 hdr->prop_entry_arr = (vm_tadsobj_prop *)mem;
127
128 /* all entries are currently free, so point to the first entry */
129 hdr->prop_entry_free = 0;
130
131 /* remember the superclass count */
132 hdr->sc_cnt = sc_cnt;
133
134 /* return the new object */
135 return hdr;
136 }
137
138 /*
139 * Free
140 */
141 void vm_tadsobj_hdr::free_mem()
142 {
143 /* if I have a precalculated inheritance path, delete it */
144 if (inh_path != 0)
145 t3free(inh_path);
146 }
147
148 /*
149 * Expand an existing object header to make room for more properties
150 */
151 vm_tadsobj_hdr *vm_tadsobj_hdr::expand(VMG_ CVmObjTads *self,
152 vm_tadsobj_hdr *hdr)
153 {
154 unsigned short prop_cnt;
155
156 /*
157 * Move up to the next property count increment. If we're not huge,
158 * simply double the current size. If we're getting large, expand by
159 * 50%.
160 */
161 prop_cnt = hdr->prop_entry_cnt;
162 if (prop_cnt <= 128)
163 prop_cnt *= 2;
164 else
165 prop_cnt += prop_cnt/2;
166
167 /* expand to the new size */
168 return expand_to(vmg_ self, hdr, hdr->sc_cnt, prop_cnt);
169 }
170
171 /*
172 * Expand an existing header to the given minimum property table size
173 */
174 vm_tadsobj_hdr *vm_tadsobj_hdr::expand_to(VMG_ CVmObjTads *self,
175 vm_tadsobj_hdr *hdr,
176 size_t new_sc_cnt,
177 size_t new_prop_cnt)
178 {
179 vm_tadsobj_hdr *new_hdr;
180 size_t i;
181 vm_tadsobj_prop *entryp;
182
183 /* allocate a new object at the expanded property table size */
184 new_hdr = alloc(vmg_ self, (ushort)new_sc_cnt, (ushort)new_prop_cnt);
185
186 /* copy the superclasses from the original object */
187 memcpy(new_hdr->sc, hdr->sc,
188 (hdr->sc_cnt < new_sc_cnt ? hdr->sc_cnt : new_sc_cnt)
189 * sizeof(hdr->sc[0]));
190
191 /* use the same flags from the original object */
192 new_hdr->li_obj_flags = hdr->li_obj_flags;
193 new_hdr->intern_obj_flags = hdr->intern_obj_flags;
194
195 /*
196 * if the superclass count is changing, we're obviously changing the
197 * inheritance structure, in which case the old cached inheritance path
198 * is invalid - delete it if so
199 */
200 if (new_sc_cnt != hdr->sc_cnt)
201 hdr->inval_inh_path();
202
203 /* copy the old inheritance path (if we still have one) */
204 new_hdr->inh_path = hdr->inh_path;
205
206 /*
207 * Run through all of the existing properties and duplicate them in the
208 * new object, to build the new object's hash table. Note that the
209 * free index is inherently equivalent to the count of properties in
210 * use.
211 */
212 for (i = hdr->prop_entry_free, entryp = hdr->prop_entry_arr ; i != 0 ;
213 --i, ++entryp)
214 {
215 /* add this property to the new table */
216 new_hdr->alloc_prop_entry(entryp->prop, &entryp->val, entryp->flags);
217 }
218
219 /* delete the old header */
220 G_mem->get_var_heap()->free_mem(hdr);
221
222 /* return the new header */
223 return new_hdr;
224 }
225
226 /*
227 * Allocate an entry for given property from the free pool. The caller is
228 * responsible for checking that there's space in the free pool. We do
229 * not check for an existing entry with the same caller ID, so the caller
230 * is responsible for making sure the property doesn't already exist in
231 * our table.
232 */
233 vm_tadsobj_prop *vm_tadsobj_hdr::alloc_prop_entry(
234 vm_prop_id_t prop, const vm_val_t *val, unsigned int flags)
235 {
236 /* get the hash code for the property */
237 unsigned int hash = calc_hash(prop);
238
239 /* use the next free entry */
240 vm_tadsobj_prop *entry = &prop_entry_arr[prop_entry_free];
241
242 /* link this entry into the list for its hash bucket */
243 entry->nxt = hash_arr[hash];
244 hash_arr[hash] = entry;
245
246 /* count our use of the free entry */
247 ++prop_entry_free;
248
249 /* set the new entry's property ID */
250 entry->prop = prop;
251
252 /* set the value and flags */
253 entry->val = *val;
254 entry->flags = (unsigned char)flags;
255
256 /* return the entry */
257 return entry;
258 }
259
260 /*
261 * Find an entry
262 */
263 vm_tadsobj_prop *vm_tadsobj_hdr::find_prop_entry(uint prop)
264 {
265 /* scan the list of entries in this bucket */
266 for (vm_tadsobj_prop *entry = hash_arr[calc_hash(prop)] ;
267 entry != 0 ; entry = entry->nxt)
268 {
269 /* if this entry matches, return it */
270 if (entry->prop == prop)
271 return entry;
272 }
273
274 /* didn't find it */
275 return 0;
276 }
277
278 /* ------------------------------------------------------------------------ */
279 /*
280 * statics
281 */
282
283 /* metaclass registration object */
284 static CVmMetaclassTads metaclass_reg_obj;
285 CVmMetaclass *CVmObjTads::metaclass_reg_ = &metaclass_reg_obj;
286
287
288 /* function table */
289 int (CVmObjTads::
290 *CVmObjTads::func_table_[])(VMG_ vm_obj_id_t self,
291 vm_val_t *retval, uint *argc) =
292 {
293 &CVmObjTads::getp_undef,
294 &CVmObjTads::getp_create_instance,
295 &CVmObjTads::getp_create_clone,
296 &CVmObjTads::getp_create_trans_instance,
297 &CVmObjTads::getp_create_instance_of,
298 &CVmObjTads::getp_create_trans_instance_of,
299 &CVmObjTads::getp_set_sc_list,
300 &CVmObjTads::getp_get_method,
301 &CVmObjTads::getp_set_method
302 };
303
304 /*
305 * Function table indices. We only need constant definitions for these
306 * for our static methods, since in other cases we translate through the
307 * function table.
308 */
309 const int PROPIDX_CREATE_INSTANCE = 1;
310 const int PROPIDX_CREATE_CLONE = 2;
311 const int PROPIDX_CREATE_TRANS_INSTANCE = 3;
312 const int PROPIDX_CREATE_INSTANCE_OF = 4;
313 const int PROPIDX_CREATE_TRANS_INSTANCE_OF = 5;
314
315 /* ------------------------------------------------------------------------ */
316 /*
317 * Static class initialization
318 */
319 void CVmObjTads::class_init(VMG0_)
320 {
321 /* allocate the inheritance analysis object */
322 VM_IFELSE_ALLOC_PRE_GLOBAL(
323 G_tadsobj_queue = new CVmObjTadsInhQueue(),
324 G_tadsobj_queue->init());
325 }
326
327 /*
328 * Static class termination
329 */
330 void CVmObjTads::class_term(VMG0_)
331 {
332 /* delete the inheritance analysis object */
333 VM_IF_ALLOC_PRE_GLOBAL(
334 delete G_tadsobj_queue;
335 G_tadsobj_queue = 0;
336 )
337 }
338
339 /* ------------------------------------------------------------------------ */
340 /*
341 * Static creation methods
342 */
343
344 /* create dynamically using stack arguments */
345 vm_obj_id_t CVmObjTads::create_from_stack_intern(
346 VMG_ const uchar **pc_ptr, uint argc, int is_transient)
347 {
348 vm_obj_id_t id;
349 CVmObjTads *obj;
350 vm_val_t val;
351 vm_obj_id_t srcobj;
352
353 /* check arguments */
354 if (argc == 0)
355 {
356 /* no superclass argument - create a base object */
357 val.set_nil();
358 }
359 else
360 {
361 /*
362 * We have arguments. The first is the superclass argument, which
363 * must be an object or nil. Retrieve it and make sure it's
364 * valid.
365 */
366 G_stk->pop(&val);
367 if (val.typ != VM_OBJ && val.typ != VM_NIL)
368 err_throw(VMERR_OBJ_VAL_REQD_SC);
369
370 /* if it's the invalid object, treat it as nil */
371 if (val.typ == VM_OBJ && val.val.obj == VM_INVALID_OBJ)
372 val.set_nil();
373
374 /* we cannot create an instance of a transient object */
375 if (val.typ != VM_NIL
376 && G_obj_table->is_obj_transient(val.val.obj))
377 err_throw(VMERR_BAD_DYNAMIC_NEW);
378
379 /* count the removal of the first argument */
380 --argc;
381 }
382
383 /*
384 * create the object - this type of construction is never used for
385 * root set objects
386 */
387 id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
388
389 /* make the object transient if desired */
390 if (is_transient)
391 G_obj_table->set_obj_transient(id);
392
393 /*
394 * create a TADS object with the appropriate number of superclasses
395 * (0 if no superclass was specified, 1 if one was), and the default
396 * number of initial mutable properties
397 */
398 obj = new (vmg_ id) CVmObjTads(vmg_ (val.typ == VM_NIL ? 0 : 1),
399 VMTOBJ_PROP_INIT);
400
401 /* set the object's superclass */
402 if (val.typ != VM_NIL)
403 obj->set_sc(vmg_ 0, val.val.obj);
404
405 /*
406 * Invoke the object's "construct" method, passing it the arguments
407 * that are still on the stack. If the new object doesn't define or
408 * inherit the "construct" method, simply push the new object
409 * reference onto the stack directly.
410 */
411 if (obj->get_prop(vmg_ G_predef->obj_construct, &val, id, &srcobj, 0))
412 {
413 vm_val_t srcobj_val;
414 vm_val_t id_val;
415 const uchar *dummy_pc_ptr;
416 uint caller_ofs;
417 vm_rcdesc rc, *rcp;
418
419 /* use the null PC pointer if the caller didn't supply one */
420 if (pc_ptr == 0)
421 {
422 /* there's no caller PC pointer - use a dummy value */
423 pc_ptr = &dummy_pc_ptr;
424 caller_ofs = 0;
425
426 /* set up the recursive context */
427 rcp = &rc;
428 rc.init_ret(vmg_ "TadsObject.construct");
429 }
430 else
431 {
432 /* get the caller's offset */
433 caller_ofs = G_interpreter->pc_to_method_ofs(*pc_ptr);
434 rcp = 0;
435 }
436
437 /*
438 * A "construct" method is defined - have the interpreter invoke
439 * it, which will set up the interpreter to start executing its
440 * byte-code. This is all we need to do, since we assume and
441 * require that the constructor will return the new object as
442 * its return value when it's done.
443 */
444 srcobj_val.set_obj(srcobj);
445 id_val.set_obj(id);
446 *pc_ptr = G_interpreter->get_prop(vmg_ caller_ofs, &srcobj_val,
447 G_predef->obj_construct,
448 &id_val, argc, rcp);
449 }
450 else
451 {
452 /*
453 * there's no "construct" method defined - if we have any
454 * arguments, its an error
455 */
456 if (argc != 0)
457 err_throw(VMERR_WRONG_NUM_OF_ARGS);
458
459 /* leave the new object value in R0 */
460 G_interpreter->get_r0()->set_obj(id);
461 }
462
463 /* return the new object */
464 return id;
465 }
466
467 /* create an object with no initial extension */
468 vm_obj_id_t CVmObjTads::create(VMG_ int in_root_set)
469 {
470 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE);
471 new (vmg_ id) CVmObjTads();
472 return id;
473 }
474
475 /*
476 * Create an object with a given number of superclasses, and a given
477 * property table size. Each superclass must be set before the object
478 * can be used, and the property table is initially empty.
479 *
480 * This form is used to create objects dynamically; this call is never
481 * used to load an object from an image file.
482 */
483 vm_obj_id_t CVmObjTads::create(VMG_ int in_root_set,
484 ushort superclass_count, ushort prop_count)
485 {
486 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE);
487 new (vmg_ id) CVmObjTads(vmg_ superclass_count, prop_count);
488 return id;
489 }
490
491 /*
492 * Create an instance based on multiple superclasses, using the
493 * createInstanceOf() interface. Arguments are passed on the stack. Each
494 * argument gives a superclass, and optionally the arguments for its
495 * inherited constructor. If an argument is a simple object/class, then we
496 * won't inherit that object's constructor at all. If an argument is a
497 * list, then the first element of the list gives the class, and the
498 * remaining elements of the list give the arguments to pass to that
499 * class's inherited constructor.
500 */
501 vm_obj_id_t CVmObjTads::create_from_stack_multi(
502 VMG_ uint argc, int is_transient)
503 {
504 /* allocate an object ID */
505 vm_obj_id_t id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
506 if (is_transient)
507 G_obj_table->set_obj_transient(id);
508
509 /* create the new object */
510 CVmObjTads *obj = new (vmg_ id) CVmObjTads(
511 vmg_ (ushort)argc, VMTOBJ_PROP_INIT);
512
513 /* push the new object, for garbage collector protection */
514 G_interpreter->push_obj(vmg_ id);
515
516 /* set the superclasses */
517 for (ushort i = 0 ; i < argc ; ++i)
518 {
519 /*
520 * get this argument (it's at i+1 because of the extra item we
521 * pushed for gc protection)
522 */
523 vm_val_t *arg = G_stk->get(i + 1);
524
525 /*
526 * if it's a list, the superclass is the first element; otherwise,
527 * the argument is the superclass
528 */
529 vm_val_t sc;
530 const char *lstp = arg->get_as_list(vmg0_);
531 if (lstp != 0)
532 {
533 /* it's a list - the first element is the superclass */
534 CVmObjList::index_list(vmg_ &sc, lstp, 1);
535 }
536 else
537 {
538 /* not a list - the argument is the superclass */
539 sc = *arg;
540 }
541
542 /* make sure it's a TadsObject */
543 if (sc.typ != VM_OBJ || !is_tadsobj_obj(vmg_ sc.val.obj))
544 err_throw(VMERR_BAD_TYPE_BIF);
545
546 /* can't create an instance of a transient object */
547 if (G_obj_table->is_obj_transient(sc.val.obj))
548 err_throw(VMERR_BAD_DYNAMIC_NEW);
549
550 /* set this superclass */
551 obj->set_sc(vmg_ i, sc.val.obj);
552 }
553
554 /* set up the resursive invocation context */
555 vm_rcdesc rc("TadsObject.contructMulti");
556
557 /*
558 * The new object is ready to go. All that remains is invoking any
559 * inherited construtors that the caller wants us to invoked.
560 * Constructor invocation is indicated by passing a list argument for
561 * the corresponding superclass, so run through the arguments and
562 * invoke each indicated constructor.
563 */
564 for (ushort i = 0 ; i < argc ; ++i)
565 {
566 /* get the next argument */
567 vm_val_t *arg = G_stk->get(i + 1);
568
569 /* if it's not a list, we don't want to invoke this constructor */
570 const char *lstp = arg->get_as_list(vmg0_);
571 if (lstp == 0)
572 {
573 /* no constructor call is wanted - just keep going */
574 continue;
575 }
576
577 /* get the superclass from the list */
578 vm_val_t sc;
579 CVmObjList::index_list(vmg_ &sc, lstp, 1);
580
581 /* get the number of list elements */
582 uint lst_cnt = vmb_get_len(lstp);
583
584 /* make sure we have room to push the arguments */
585 if (!G_stk->check_space(lst_cnt - 1))
586 err_throw(VMERR_STACK_OVERFLOW);
587
588 /*
589 * push the list elements in reverse order; don't push the first
590 * element, since it's the superclass itself rather than an
591 * argument to the constructor
592 */
593 for (uint j = lst_cnt ; j > 1 ; --j)
594 CVmObjList::index_and_push(vmg_ lstp, j);
595
596 /*
597 * Invoke the constructor via a recursive call into the VM. Note
598 * that we're inheriting the property, so 'self' is the new object,
599 * but the 'target' object is the superclass whose constructor
600 * we're invoking.
601 */
602 vm_val_t new_obj_val;
603 new_obj_val.set_obj(id);
604 G_interpreter->get_prop(vmg_ 0, &sc, G_predef->obj_construct,
605 &new_obj_val, lst_cnt - 1, &rc);
606 }
607
608 /* discard the arguments plus our own gc protection */
609 G_stk->discard(argc + 1);
610
611 /* return the new object */
612 return id;
613 }
614
615 /* ------------------------------------------------------------------------ */
616 /*
617 * Constructors
618 */
619
620 /*
621 * Create an object with a given number of superclasses, and a given
622 * property table size. The superclasses must be individually set
623 * before the object can be used, and the property table is initially
624 * empty.
625 *
626 * This constructor is used only when creating a new object dynamically,
627 * and is never used to load an object from an image file.
628 */
629 CVmObjTads::CVmObjTads(VMG_ ushort superclass_count, ushort prop_count)
630 {
631 /* allocate our header */
632 ext_ = (char *)vm_tadsobj_hdr::alloc(vmg_ this, superclass_count,
633 prop_count);
634 }
635
636
637 /* ------------------------------------------------------------------------ */
638 /*
639 * receive notification of deletion
640 */
641 void CVmObjTads::notify_delete(VMG_ int in_root_set)
642 {
643 /* free our extension */
644 if (ext_ != 0)
645 {
646 /* tell the header to delete its memory */
647 get_hdr()->free_mem();
648
649 /* delete the extension */
650 G_mem->get_var_heap()->free_mem(ext_);
651 }
652 }
653
654 /* ------------------------------------------------------------------------ */
655 /*
656 * Create an instance of this class
657 */
658 void CVmObjTads::create_instance(VMG_ vm_obj_id_t self,
659 const uchar **pc_ptr, uint argc)
660 {
661 /* push myself as the superclass */
662 G_stk->push()->set_obj(self);
663
664 /* use the normal stack creation routine */
665 create_from_stack(vmg_ pc_ptr, argc+1);
666 }
667
668 /* ------------------------------------------------------------------------ */
669 /*
670 * Determine if the object has a finalizer method
671 */
672 int CVmObjTads::has_finalizer(VMG_ vm_obj_id_t self)
673 {
674 vm_val_t val;
675 vm_obj_id_t srcobj;
676
677 /*
678 * look up the finalization method - if it's defined, and it's a
679 * method, invoke it; otherwise do nothing
680 */
681 return (G_predef->obj_destruct != VM_INVALID_PROP
682 && get_prop(vmg_ G_predef->obj_destruct, &val, self, &srcobj, 0)
683 && (val.typ == VM_CODEOFS
684 || val.typ == VM_NATIVE_CODE
685 || val.typ == VM_OBJX
686 || val.typ == VM_BIFPTRX));
687 }
688
689 /* ------------------------------------------------------------------------ */
690 /*
691 * Invoke the object's finalizer
692 */
693 void CVmObjTads::invoke_finalizer(VMG_ vm_obj_id_t self)
694 {
695 vm_val_t val;
696 vm_obj_id_t srcobj;
697
698 /*
699 * look up the finalization method - if it's defined, and it's a
700 * method, invoke it; otherwise do nothing
701 */
702 if (G_predef->obj_destruct != VM_INVALID_PROP
703 && get_prop(vmg_ G_predef->obj_destruct, &val, self, &srcobj, 0)
704 && (val.typ == VM_CODEOFS
705 || val.typ == VM_NATIVE_CODE
706 || val.typ == VM_OBJX
707 || val.typ == VM_BIFPTRX))
708 {
709 /*
710 * invoke the finalizer in a protected frame, to ensure that we
711 * catch any exceptions that are thrown out of the finalizer
712 */
713 err_try
714 {
715 vm_val_t srcobj_val;
716 vm_val_t self_val;
717
718 /*
719 * Invoke the finalizer. Use a recursive VM invocation,
720 * since the VM must return to the garbage collector, not to
721 * what it was doing in the enclosing stack frame.
722 */
723 srcobj_val.set_obj(srcobj);
724 self_val.set_obj(self);
725 vm_rcdesc rc("TadsObject.finalize");
726 G_interpreter->get_prop(vmg_ 0, &srcobj_val,
727 G_predef->obj_destruct, &self_val, 0, &rc);
728 }
729 err_catch_disc
730 {
731 /* silently ignore the error */
732 }
733 err_end;
734 }
735 }
736
737
738 /* ------------------------------------------------------------------------ */
739 /*
740 * Clear the undo flags for all properties
741 */
742 void CVmObjTads::clear_undo_flags()
743 {
744 vm_tadsobj_prop *entry;
745 uint i;
746 vm_tadsobj_hdr *hdr = get_hdr();
747
748 /* scan all property entries and clear their undo flags */
749 for (i = hdr->prop_entry_free, entry = hdr->prop_entry_arr ;
750 i != 0 ; --i, ++entry)
751 {
752 /* clear this entry's undo flag */
753 entry->flags &= ~VMTO_PROP_UNDO;
754 }
755 }
756
757 /* ------------------------------------------------------------------------ */
758 /*
759 * Set a property
760 */
761 void CVmObjTads::set_prop(VMG_ CVmUndo *undo, vm_obj_id_t self,
762 vm_prop_id_t prop, const vm_val_t *val)
763 {
764 /* get my header */
765 vm_tadsobj_hdr *hdr = get_hdr();
766
767 /* look for an existing property entry */
768 vm_tadsobj_prop *entry = hdr->find_prop_entry(prop);
769
770 /* check for an existing entry for the property */
771 vm_val_t oldval;
772 if (entry != 0)
773 {
774 /* found an existing entry - note the old value */
775 oldval = entry->val;
776
777 /* store the new value in the existing entry */
778 entry->val = *val;
779 }
780 else
781 {
782 /*
783 * We didn't find an existing entry for the property, so we have to
784 * add a new one. If we don't have any free property slots left,
785 * expand the object to create some more property slots.
786 */
787 if (!hdr->has_free_entries(1))
788 {
789 /* expand the extension to make room for more properties */
790 ext_ = (char *)vm_tadsobj_hdr::expand(vmg_ this, hdr);
791
792 /* get the reallocated header */
793 hdr = get_hdr();
794 }
795
796 /* allocate a new entry */
797 entry = hdr->alloc_prop_entry(prop, val, 0);
798
799 /*
800 * The old value didn't exist, so mark it emtpy, with an intval of
801 * zero. The zero indicates that this is a newly created property
802 * entry, and thus should be deleted on undo.
803 */
804 oldval.set_empty();
805 oldval.val.intval = 0;
806 }
807
808 /*
809 * If we already have undo for this property for the current
810 * savepoint, as indicated by the undo flag for the property, we don't
811 * need to save undo for this change, since we already have an undo
812 * record in the current savepoint. Otherwise, we need to add an undo
813 * record for this savepoint.
814 */
815 if (undo != 0 && (entry->flags & VMTO_PROP_UNDO) == 0)
816 {
817 /* save the undo record */
818 undo->add_new_record_prop_key(vmg_ self, prop, &oldval);
819
820 /* mark the property as now having undo in this savepoint */
821 entry->flags |= VMTO_PROP_UNDO;
822
823 /*
824 * If the entry wasn't previously marked as modified, remember this
825 * by storing an extra 'empty' undo record with intval 1 after the
826 * record we just saved. When we see an 'empty' undo value with an
827 * intval of 1, we recognize it as this special marker that tells
828 * us to remove the 'modified' flag from the property. Note that
829 * we don't need to bother if the old value was already empty,
830 * since that deletes the whole property on undo, making the
831 * 'modified' flag irrelevant.
832 */
833 if ((entry->flags & VMTO_PROP_MOD) == 0 && oldval.typ != VM_EMPTY)
834 {
835 /* store an empty undo record with intval 1 */
836 oldval.set_empty();
837 oldval.val.intval = 1;
838 undo->add_new_record_prop_key(vmg_ self, prop, &oldval);
839 }
840 }
841
842 /* mark the property entry as modified */
843 entry->flags |= VMTO_PROP_MOD;
844
845 /* mark the overall object as modified */
846 mark_modified(vmg_ undo, self);
847 }
848
849 /* ------------------------------------------------------------------------ */
850 /*
851 * Mark the object as modified
852 */
853 void CVmObjTads::mark_modified(VMG_ CVmUndo *undo, vm_obj_id_t self)
854 {
855 /* get my header */
856 vm_tadsobj_hdr *hdr = get_hdr();
857
858 /* if we're not already marked as modified, set the flag */
859 if ((hdr->intern_obj_flags & VMTO_OBJ_MOD) == 0)
860 {
861 /* add the modified flag */
862 hdr->intern_obj_flags |= VMTO_OBJ_MOD;
863
864 /* add an undo record for it */
865 if (undo != 0)
866 {
867 /*
868 * property == 'invalid' and value == int(1) is our special
869 * record indicating an unmodified-to-modified transition
870 */
871 vm_val_t v;
872 v.set_int(1);
873 undo->add_new_record_prop_key(vmg_ self, VM_INVALID_PROP, &v);
874 }
875 }
876 }
877
878 /* ------------------------------------------------------------------------ */
879 /*
880 * Build a list of my properties
881 */
882 void CVmObjTads::build_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval)
883 {
884 size_t cnt;
885 size_t idx;
886 CVmObjList *lst;
887 vm_tadsobj_prop *entry;
888 vm_tadsobj_hdr *hdr = get_hdr();
889
890 /* the next free index is also the number of properties we have */
891 cnt = hdr->prop_entry_free;
892
893 /* allocate a list big enough for all of our properties */
894 retval->set_obj(CVmObjList::create(vmg_ FALSE, cnt));
895
896 /* get the list object, property cast */
897 lst = (CVmObjList *)vm_objp(vmg_ retval->val.obj);
898
899 /* add our image file properties to the list */
900 for (idx = 0, entry = hdr->prop_entry_arr ; cnt != 0 ;
901 --cnt, ++entry)
902 {
903 /* make a value for this property ID */
904 vm_val_t val;
905 val.set_propid(entry->prop);
906
907 /* add it to the list */
908 lst->cons_set_element(idx++, &val);
909 }
910
911 /* set the final length of the list */
912 lst->cons_set_len(idx);
913 }
914
915
916 /* ------------------------------------------------------------------------ */
917 /*
918 * Call a static method.
919 */
920 int CVmObjTads::call_stat_prop(VMG_ vm_val_t *result,
921 const uchar **pc_ptr, uint *argc,
922 vm_prop_id_t prop)
923 {
924 int idx;
925
926 /* convert the property to an index in our method vector */
927 idx = G_meta_table
928 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop);
929
930 /* check what property they're evaluating */
931 switch(idx)
932 {
933 case PROPIDX_CREATE_INSTANCE:
934 case PROPIDX_CREATE_TRANS_INSTANCE:
935 {
936 static CVmNativeCodeDesc desc(0);
937
938 /* check arguments */
939 if (get_prop_check_argc(result, argc, &desc))
940 return TRUE;
941
942 /*
943 * They want to create an instance of TadsObject, which is
944 * just a plain base object with no superclass. Push null as
945 * the base class and call our from-stack constructor.
946 */
947 result->set_obj(create_from_stack_intern(
948 vmg_ pc_ptr, 0, idx == PROPIDX_CREATE_TRANS_INSTANCE));
949 }
950
951 /* handled */
952 return TRUE;
953
954 case PROPIDX_CREATE_INSTANCE_OF:
955 case PROPIDX_CREATE_TRANS_INSTANCE_OF:
956 {
957 static CVmNativeCodeDesc desc(0, 0, TRUE);
958 uint in_argc = (argc == 0 ? 0 : *argc);
959
960 /* check arguments */
961 if (get_prop_check_argc(result, argc, &desc))
962 return TRUE;
963
964 /*
965 * They want to create an instance of TadsObject, which is just
966 * a plain base object with no superclass. Push null as the
967 * base class and call our from-stack constructor.
968 */
969 result->set_obj(create_from_stack_multi(
970 vmg_ in_argc, idx == PROPIDX_CREATE_TRANS_INSTANCE_OF));
971 }
972
973 /* handled */
974 return TRUE;
975
976 default:
977 /* it's not one of ours; inherit the base class statics */
978 return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
979 }
980 }
981
982 /* ------------------------------------------------------------------------ */
983 /*
984 * Superclass inheritance search context. This keeps track of our position
985 * in searching the inheritance tree of a given class.
986 */
987 struct tadsobj_sc_search_ctx
988 {
989 /* initialize at a given object */
990 tadsobj_sc_search_ctx(VMG_ vm_obj_id_t obj, CVmObjTads *objp)
991 {
992 /* start at the given object */
993 cur = obj;
994 curhdr = objp->get_hdr();
995
996 /* we have no path yet */
997 path_sc = 0;
998 }
999
1000 /* current object ID */
1001 vm_obj_id_t cur;
1002
1003 /* if we're using a multiple-inheritance search path, the path */
1004 tadsobj_objid_and_ptr *path_sc;
1005
1006 /* current object header */
1007 vm_tadsobj_hdr *curhdr;
1008
1009
1010 /*
1011 * Find the given property, searching our superclass list until we find
1012 * an object providing the property. Returns true if found, and fills
1013 * in *val and *source. Returns false if not found.
1014 */
1015 int find_prop(VMG_ uint prop, vm_val_t *val, vm_obj_id_t *source)
1016 {
1017 /* keep going until we find the property */
1018 do
1019 {
1020 /* look for this property in the current object */
1021 vm_tadsobj_prop *entry = curhdr->find_prop_entry(prop);
1022 if (entry != 0)
1023 {
1024 /* we found the property - return it */
1025 *val = entry->val;
1026 *source = cur;
1027 return TRUE;
1028 }
1029 }
1030 while (to_next(vmg0_));
1031
1032 /* we've exhausted the search path - return failure */
1033 return FALSE;
1034 }
1035
1036 /*
1037 * Move to the next superclass. This updates 'cur' to refer to the
1038 * next object in inheritance order. Returns true if there is a next
1039 * element, false if not.
1040 *
1041 * It's legal to call this with 'cur' uninitialized, as we don't need
1042 * the old value of 'cur' to do our work. This is important because it
1043 * allows a search position to be initialized knowing only an object's
1044 * 'this' pointer, not its object ID.
1045 */
1046 int to_next(VMG0_)
1047 {
1048 /* check for a path */
1049 try_again:
1050 if (path_sc == 0)
1051 {
1052 /*
1053 * we're not working on a path at all - this means we're
1054 * working directly on a (so far) single-inheritance superclass
1055 * chain, so simply follow the chain up to the next superclass
1056 */
1057
1058 /* we have no path, so look at our object's superclasses */
1059 switch(curhdr->sc_cnt)
1060 {
1061 case 1:
1062 /* we have exactly one superclass, so traverse to it */
1063 {
1064 const vm_tadsobj_sc *sc = curhdr->sc;
1065 cur = sc->id;
1066 curhdr = sc->objp->get_hdr();
1067 }
1068 return TRUE;
1069
1070 case 0:
1071 /* we have no superclasses, so there's nowhere to go */
1072 return FALSE;
1073
1074 default:
1075 /* we have multiple superclasses, so set up a search path */
1076 path_sc = curhdr->get_inh_search_path(vmg0_);
1077
1078 /* start over with the search path */
1079 goto try_again;
1080 }
1081 }
1082 else if ((cur = path_sc->id) != VM_INVALID_OBJ)
1083 {
1084 /*
1085 * we're working on a path, and we have elements remaining -
1086 * move on to the next element
1087 */
1088 curhdr = path_sc->objp->get_hdr();
1089 ++path_sc;
1090
1091 /* got it */
1092 return TRUE;
1093 }
1094 else
1095 {
1096 /*
1097 * we're working on a path, and we're out of elements - we have
1098 * nowhere else to go
1099 */
1100 return FALSE;
1101 }
1102 }
1103
1104 /*
1105 * Skip to the given object. If we find the object in the path, we'll
1106 * leave the current position set to the given object and return true;
1107 * if we fail to find the object, we'll return false.
1108 */
1109 int skip_to(VMG_ vm_obj_id_t target)
1110 {
1111 /* keep going until the current object matches the target */
1112 while (cur != target)
1113 {
1114 /* move to the next element */
1115 if (!to_next(vmg0_))
1116 {
1117 /* there's nothing left - return failure */
1118 return FALSE;
1119 }
1120 }
1121
1122 /* found it */
1123 return TRUE;
1124 }
1125 };
1126
1127 /*
1128 * Get the inheritance search path for this object
1129 */
1130 tadsobj_objid_and_ptr *vm_tadsobj_hdr::get_inh_search_path(VMG0_)
1131 {
1132 /* if we have a cached path, return it */
1133 if (inh_path != 0)
1134 return inh_path;
1135
1136 /* get the queue builder global */
1137 CVmObjTadsInhQueue *q = G_tadsobj_queue;
1138
1139 /*
1140 * We haven't already cached a search path for this object, so build
1141 * the search path now and save it for future searches. Start by
1142 * clearing the work queue.
1143 */
1144 q->clear();
1145
1146 /* we're not yet processing the first element */
1147 pfq_ele *q_ele = 0;
1148
1149 /* start with self */
1150 vm_tadsobj_hdr *curhdr = this;
1151
1152 /* keep going until we run out of queue elements */
1153 for (;;)
1154 {
1155 /* get the superclass count for this object */
1156 uint cnt = curhdr->sc_cnt;
1157
1158 /* insert my superclasses right after me */
1159 pfq_ele *q_ins = q_ele;
1160
1161 /* enqueue the current object's superclasses */
1162 uint i;
1163 vm_tadsobj_sc *scp;
1164 for (i = 0, scp = curhdr->sc ; i < cnt ; ++i, ++scp)
1165 {
1166 /* get the current superclass */
1167 vm_obj_id_t sc = scp->id;
1168 CVmObjTads *scobj = scp->objp;
1169
1170 /* if it's not a TadsObject, skip it */
1171 if (scobj->get_metaclass_reg() != CVmObjTads::metaclass_reg_)
1172 continue;
1173
1174 /* enqueue this superclass */
1175 q_ins = q->insert_obj(vmg_ sc, scobj, q_ins);
1176 }
1177
1178 /* move to the next valid element */
1179 do
1180 {
1181 /* get the next queue element */
1182 q_ele = (q_ele == 0 ? q->get_head() : q_ele->nxt);
1183
1184 /* if we're out of elements, we're done */
1185 if (q_ele == 0)
1186 goto done;
1187 }
1188 while (q_ele->obj == VM_INVALID_OBJ);
1189
1190 /* get the object header for the queue item */
1191 curhdr = q_ele->objp->get_hdr();
1192 }
1193
1194 done:
1195 /* create and cache a linearized path for the queue, and return it */
1196 return inh_path = q->create_path();
1197 }
1198
1199 /*
1200 * Search for a property via inheritance, starting after the given defining
1201 * object.
1202 */
1203 int CVmObjTads::search_for_prop_from(VMG_ uint prop,
1204 vm_val_t *val,
1205 vm_obj_id_t orig_target_obj,
1206 vm_obj_id_t *source_obj,
1207 vm_obj_id_t defining_obj)
1208 {
1209 /* set up a search position */
1210 tadsobj_sc_search_ctx curpos(vmg_ orig_target_obj,
1211 (CVmObjTads *)vm_objp(vmg_ orig_target_obj));
1212
1213 /* if we have a starting point, skip past it */
1214 if (defining_obj != VM_INVALID_OBJ)
1215 {
1216 /* skip until we're at defining_obj */
1217 if (!curpos.skip_to(vmg_ defining_obj))
1218 return FALSE;
1219
1220 /* skip defining_obj itself */
1221 if (!curpos.to_next(vmg0_))
1222 return FALSE;
1223 }
1224
1225 /* find the property */
1226 return curpos.find_prop(vmg_ prop, val, source_obj);
1227 }
1228
1229 /* ------------------------------------------------------------------------ */
1230 /*
1231 * Get a property. We first look in this object; if we can't find the
1232 * property here, we look for it in one of our superclasses.
1233 */
1234 int CVmObjTads::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
1235 vm_obj_id_t self, vm_obj_id_t *source_obj,
1236 uint *argc)
1237 {
1238 /*
1239 * try finding the property in my own direct property list or a
1240 * superclass property list
1241 */
1242 tadsobj_sc_search_ctx curpos(vmg_ self, this);
1243 if (curpos.find_prop(vmg_ prop, val, source_obj))
1244 return TRUE;
1245
1246 /*
1247 * we didn't find the property in a property list, so try the
1248 * intrinsic class methods
1249 */
1250 if (get_prop_intrinsic(vmg_ prop, val, self, source_obj, argc))
1251 return TRUE;
1252
1253 /*
1254 * we didn't find the property among our methods, so try inheriting it
1255 * from the base metaclass
1256 */
1257 return CVmObject::get_prop(vmg_ prop, val, self, source_obj, argc);
1258 }
1259
1260 /*
1261 * Inherit a property.
1262 */
1263 int CVmObjTads::inh_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
1264 vm_obj_id_t self,
1265 vm_obj_id_t orig_target_obj,
1266 vm_obj_id_t defining_obj,
1267 vm_obj_id_t *source_obj, uint *argc)
1268 {
1269 /*
1270 * check to see if we're already inheriting from an intrinsic class or
1271 * an intrinsic class modifier
1272 */
1273 if (defining_obj == VM_INVALID_OBJ
1274 || (!CVmObjIntClsMod::is_intcls_mod_obj(vmg_ defining_obj)
1275 && !CVmObjClass::is_intcls_obj(vmg_ defining_obj)))
1276 {
1277 /*
1278 * The previous defining object wasn't itself an intrinsic class or
1279 * modifier object, so continue searching for TadsObject
1280 * superclasses.
1281 */
1282 if (search_for_prop_from(vmg_ prop, val,
1283 orig_target_obj, source_obj, defining_obj))
1284 return TRUE;
1285
1286 /*
1287 * We didn't find the property in a property list. Since we were
1288 * inheriting, we must have originally found it in a property list,
1289 * but we've found no more inherited properties. Next, check the
1290 * intrinsic methods of the intrinsic class.
1291 */
1292 if (get_prop_intrinsic(vmg_ prop, val, self, source_obj, argc))
1293 return TRUE;
1294
1295 /*
1296 * We didn't find it among our TadsObject superclasses or as an
1297 * intrinsic method. There's still one possibility: it could be
1298 * defined in an intrinsic class modifier for TadsObject or one of
1299 * its intrinsic superclasses (aka supermetaclasses).
1300 *
1301 * This represents a new starting point in the search. No longer
1302 * are we looking for TadsObject overrides; we're now looking for
1303 * modifier objects. The modifier objects effectively form a
1304 * separate class hierarchy alongside the intrinsic class hierarchy
1305 * they modify. Since we're starting a new search in this new
1306 * context, forget the previous defining object - it has a
1307 * different meaning in the new context, and we want to start the
1308 * new search from the beginning.
1309 *
1310 * Note that if this search does turn up a modifier object, and
1311 * that modifier object further inherits, we'll come back through
1312 * this method again to find the base class method. At that point,
1313 * however we'll notice that the previous defining object was a
1314 * modifier, so we will not go through this branch again - we'll go
1315 * directly to the base metaclass and continue the inheritance
1316 * search there.
1317 */
1318 defining_obj = VM_INVALID_OBJ;
1319 }
1320
1321 /* continue searching via our base metaclass */
1322 return CVmObject::inh_prop(vmg_ prop, val, self, orig_target_obj,
1323 defining_obj, source_obj, argc);
1324 }
1325
1326 /* ------------------------------------------------------------------------ */
1327 /*
1328 * Get a property from the intrinsic class.
1329 */
1330 int CVmObjTads::get_prop_intrinsic(VMG_ vm_prop_id_t prop, vm_val_t *val,
1331 vm_obj_id_t self, vm_obj_id_t *source_obj,
1332 uint *argc)
1333 {
1334 uint func_idx;
1335
1336 /* translate the property into a function vector index */
1337 func_idx = G_meta_table
1338 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop);
1339
1340 /* call the appropriate function in our function vector */
1341 if ((this->*func_table_[func_idx])(vmg_ self, val, argc))
1342 {
1343 *source_obj = metaclass_reg_->get_class_obj(vmg0_);
1344 return TRUE;
1345 }
1346
1347 /* didn't find it */
1348 return FALSE;
1349 }
1350
1351 /* ------------------------------------------------------------------------ */
1352 /*
1353 * Enumerate properties
1354 */
1355 void CVmObjTads::enum_props(VMG_ vm_obj_id_t self,
1356 void (*cb)(VMG_ void *ctx, vm_obj_id_t self,
1357 vm_prop_id_t prop,
1358 const vm_val_t *val),
1359 void *cbctx)
1360 {
1361 size_t i;
1362 size_t sc_cnt;
1363 vm_tadsobj_prop *entry;
1364 vm_tadsobj_hdr *hdr = get_hdr();
1365
1366 /* run through our properties */
1367 for (i = hdr->prop_entry_free, entry = hdr->prop_entry_arr ;
1368 i != 0 ; --i, ++entry)
1369 {
1370 /* invoke the callback */
1371 (*cb)(vmg_ cbctx, self, entry->prop, &entry->val);
1372 }
1373
1374 /* enumerate properties in each superclass */
1375 sc_cnt = get_sc_count();
1376 for (i = 0 ; i < sc_cnt ; ++i)
1377 {
1378 vm_obj_id_t sc;
1379
1380 /* get this superclass */
1381 sc = get_sc(i);
1382
1383 /* enumerate its properties */
1384 vm_objp(vmg_ sc)->enum_props(vmg_ sc, cb, cbctx);
1385 }
1386 }
1387
1388
1389 /* ------------------------------------------------------------------------ */
1390 /*
1391 * Determine if I'm an instance of the given object
1392 */
1393 int CVmObjTads::is_instance_of(VMG_ vm_obj_id_t obj)
1394 {
1395 /*
1396 * Set up a superclass search position. Since the first thing we'll
1397 * do is call 'to_next', and since 'to_next' doesn't require a valid
1398 * current object ID (only a valid 'this' pointer), we don't need to
1399 * know our own object ID - simply set the initial object ID to the
1400 * invalid ID.
1401 */
1402 tadsobj_sc_search_ctx curpos(vmg_ VM_INVALID_OBJ, this);
1403
1404 /*
1405 * scan through the search list, comparing each superclass to the
1406 * object of interest; if we find it among our superclasses, we're an
1407 * instance of the given object
1408 */
1409 for (;;)
1410 {
1411 /* skip to the next object */
1412 if (!curpos.to_next(vmg0_))
1413 {
1414 /* we've run out of superclasses without finding it */
1415 break;
1416 }
1417
1418 /*
1419 * if the current superclass is the object we're looking for, then
1420 * we're an instance of that object
1421 */
1422 if (curpos.cur == obj)
1423 return TRUE;
1424 }
1425
1426 /*
1427 * None of our superclasses match the given object, and none of the
1428 * superclasses derive from the given object, so we must not derive
1429 * from the given object. Our last recourse is to determine if the
1430 * object represents our metaclass; inherit the default handling to
1431 * make this check.
1432 */
1433 return CVmObject::is_instance_of(vmg_ obj);
1434 }
1435
1436 /* ------------------------------------------------------------------------ */
1437 /*
1438 * Apply undo
1439 */
1440 void CVmObjTads::apply_undo(VMG_ CVmUndoRecord *rec)
1441 {
1442 /* get my header */
1443 vm_tadsobj_hdr *hdr = get_hdr();
1444
1445 /*
1446 * if the property is valid, it's a simple property change record;
1447 * otherwise it's some other object-level change
1448 */
1449 if (rec->id.prop == VM_INVALID_PROP)
1450 {
1451 /*
1452 * Invalid property ID, so it's an object-level change:
1453 *
1454 * - int(1) -> object is newly modified (VMTO_OBJ_MOD flag was
1455 * newly set)
1456 *
1457 * - list -> superclass list change
1458 */
1459 switch (rec->oldval.typ)
1460 {
1461 case VM_INT:
1462 switch (rec->oldval.val.intval)
1463 {
1464 case 1:
1465 /* VMTO_OBJ_MOD flag newly set - clear the flag */
1466 hdr->intern_obj_flags &= ~VMTO_OBJ_MOD;
1467 break;
1468 }
1469 break;
1470
1471 default:
1472 /* check for a list, which indicates a superclass change */
1473 if (rec->oldval.is_listlike(vmg0_))
1474 {
1475 /* set the new superclass list */
1476 change_superclass_list(
1477 vmg_ &rec->oldval, rec->oldval.ll_length(vmg0_));
1478 }
1479 break;
1480 }
1481 }
1482 else
1483 {
1484 /* find the property entry for the property being undone */
1485 vm_tadsobj_prop *entry = hdr->find_prop_entry(rec->id.prop);
1486 if (entry == 0)
1487 {
1488 /* can't find the property - something is out of whack */
1489 assert(FALSE);
1490 return;
1491 }
1492
1493 /* restore the value from the record */
1494 entry->val = rec->oldval;
1495
1496 /* if the old value is 'empty', it requires special handling */
1497 if (rec->oldval.typ == VM_EMPTY)
1498 {
1499 vm_tadsobj_prop *cur, **prv;
1500 unsigned int hash = hdr->calc_hash(rec->id.prop);
1501
1502 /*
1503 * We use 'empty' records for multiple purposes, with the
1504 * specific one indicated by the intval field.
1505 */
1506 switch (rec->oldval.val.intval)
1507 {
1508 case 0:
1509 /*
1510 * Empty with intval 0 indicates a property addition, which
1511 * we undo by deleting the property. First, find it in the
1512 * hash chain.
1513 */
1514 for (prv = &hdr->hash_arr[hash] ;
1515 (cur = *prv) != 0 && cur != entry ;
1516 prv = &cur->nxt) ;
1517
1518 /* make sure we found it */
1519 if (cur == entry)
1520 {
1521 /* unlink it */
1522 *prv = entry->nxt;
1523
1524 /* return it to the free list */
1525 hdr->prop_entry_free -= 1;
1526 assert(entry == &hdr->prop_entry_arr[hdr->prop_entry_free]);
1527 }
1528 else
1529 {
1530 /* this should be impossible */
1531 assert(FALSE);
1532 }
1533 break;
1534
1535 case 1:
1536 /*
1537 * Empty with intval 1 indicates that we marked the slot as
1538 * newly modified, which we undo by clearing the modified
1539 * flag.
1540 */
1541 entry->flags &= ~VMTO_PROP_MOD;
1542 break;
1543 }
1544 }
1545 }
1546 }
1547
1548
1549 /* ------------------------------------------------------------------------ */
1550 /*
1551 * Mark as referenced all of the objects to which we refer
1552 */
1553 void CVmObjTads::mark_refs(VMG_ uint state)
1554 {
1555 /* get my header */
1556 vm_tadsobj_hdr *hdr = get_hdr();
1557
1558 /*
1559 * Go through all of our property slots and mark each object value.
1560 * Note that we only need to worry about the modified properties;
1561 * everything referenced in the load image list is necessarily part of
1562 * the root set, or it couldn't have been in the load image, so we
1563 * don't need to bother marking any of those objects, since they can
1564 * never be deleted by virtue of being in the root set.
1565 */
1566 vm_tadsobj_prop *entry = hdr->prop_entry_arr;
1567 vm_tadsobj_prop *first_free = entry + hdr->prop_entry_free;
1568 for ( ; entry != first_free ; ++entry)
1569 {
1570 /*
1571 * if the slot is marked as modified and contains an object
1572 * reference, mark the reference
1573 */
1574 if ((entry->val.typ == VM_OBJ || entry->val.typ == VM_OBJX)
1575 && (entry->flags & VMTO_PROP_MOD) != 0)
1576 {
1577 /* mark the reference */
1578 G_obj_table->mark_all_refs(entry->val.val.obj, state);
1579 }
1580 }
1581
1582 /* mark our superclasses as referenced */
1583 vm_tadsobj_sc *scp = hdr->sc, *sclast = scp + hdr->sc_cnt;
1584 for ( ; scp != sclast ; ++scp)
1585 G_obj_table->mark_all_refs(scp->id, state);
1586 }
1587
1588
1589 /* ------------------------------------------------------------------------ */
1590 /*
1591 * Mark a reference in an undo record
1592 */
1593 void CVmObjTads::mark_undo_ref(VMG_ CVmUndoRecord *undo)
1594 {
1595 /* if the undo record refers to an object, mark the object */
1596 if (undo->oldval.typ == VM_OBJ || undo->oldval.typ == VM_OBJX)
1597 G_obj_table->mark_all_refs(undo->oldval.val.obj, VMOBJ_REACHABLE);
1598 }
1599
1600 /* ------------------------------------------------------------------------ */
1601 /*
1602 * Determine if the object has been changed since it was loaded from the
1603 * image file. If the object has no properties stored in the modified
1604 * properties table, it is in exactly the same state as is stored in the
1605 * image file.
1606 */
1607 int CVmObjTads::is_changed_since_load() const
1608 {
1609 /* return our 'modified' flag */
1610 return ((get_hdr()->intern_obj_flags & VMTO_OBJ_MOD) != 0);
1611 }
1612
1613 /* ------------------------------------------------------------------------ */
1614 /*
1615 * Save the object's state to a file. We only need to save the modified
1616 * property list, because the load image list never changes.
1617 */
1618 void CVmObjTads::save_to_file(VMG_ CVmFile *fp)
1619 {
1620 size_t i;
1621 vm_tadsobj_prop *entry;
1622 uint cnt;
1623 vm_tadsobj_hdr *hdr = get_hdr();
1624
1625 /* count the number of properties that have actually been modified */
1626 for (cnt = 0, i = hdr->prop_entry_free, entry = hdr->prop_entry_arr ;
1627 i != 0 ; --i, ++entry)
1628 {
1629 /* if the slot is modified, count it */
1630 if ((entry->flags & VMTO_PROP_MOD) != 0)
1631 ++cnt;
1632 }
1633
1634 /* write the number of modified properties */
1635 fp->write_uint2(cnt);
1636
1637 /* write the number of superclasses */
1638 fp->write_uint2(get_sc_count());
1639
1640 /* write the superclasses */
1641 for (i = 0 ; i < get_sc_count() ; ++i)
1642 fp->write_uint4(get_sc(i));
1643
1644 /* write each modified property */
1645 for (cnt = 0, i = hdr->prop_entry_free, entry = hdr->prop_entry_arr ;
1646 i != 0 ; --i, ++entry)
1647 {
1648 /* if the slot is modified, write it out */
1649 if ((entry->flags & VMTO_PROP_MOD) != 0)
1650 {
1651 char slot[16];
1652
1653 /* prepare the slot data */
1654 oswp2(slot, entry->prop);
1655 vmb_put_dh(slot + 2, &entry->val);
1656
1657 /* write the slot */
1658 fp->write_bytes(slot, 2 + VMB_DATAHOLDER);
1659 }
1660 }
1661 }
1662
1663 /* ------------------------------------------------------------------------ */
1664 /*
1665 * Restore the object from a file
1666 */
1667 void CVmObjTads::restore_from_file(VMG_ vm_obj_id_t self,
1668 CVmFile *fp, CVmObjFixup *fixups)
1669 {
1670 /* read number of modified properties */
1671 ushort mod_count = (ushort)fp->read_uint2();
1672
1673 /* read the number of superclasses */
1674 ushort sc_cnt = (ushort)fp->read_uint2();
1675
1676 /*
1677 * If we don't have an extension yet, allocate one. The only way we
1678 * won't have an extension is if we weren't loaded from the image
1679 * file, since we always create the extension upon construction when
1680 * loading from an image file.
1681 */
1682 if (ext_ == 0)
1683 {
1684 /* allocate our extension */
1685 ext_ = (char *)vm_tadsobj_hdr::alloc(vmg_ this, sc_cnt, mod_count);
1686 }
1687 else
1688 {
1689 /*
1690 * We already have an extension, so we must have come from the
1691 * image file. Make sure we have enough memory to hold this many
1692 * properties, and make sure we have space for the superclasses.
1693 */
1694 vm_tadsobj_hdr *hdr = get_hdr();
1695 if (!hdr->has_free_entries(mod_count) || sc_cnt > hdr->sc_cnt)
1696 {
1697 /*
1698 * we need to expand the header to accomodate the modified
1699 * properties and/or the modified superclass list
1700 */
1701 ext_ = (char *)vm_tadsobj_hdr::expand_to(
1702 vmg_ this, hdr, sc_cnt, hdr->prop_entry_cnt + mod_count);
1703 }
1704 }
1705
1706 /* get the extension header */
1707 vm_tadsobj_hdr *hdr = get_hdr();
1708
1709 /* read the superclass list */
1710 hdr->sc_cnt = sc_cnt;
1711 for (ushort i = 0 ; i < sc_cnt ; ++i)
1712 {
1713 /* read the next superclass */
1714 vm_obj_id_t sc = (vm_obj_id_t)fp->read_uint4();
1715
1716 /* fix it up to the new (post-restore) memory numbering system */
1717 sc = fixups->get_new_id(vmg_ sc);
1718
1719 /*
1720 * store it - as when loading from the image file, we can't count
1721 * on the superclass having been loaded yet, so we can only store
1722 * the superclass's ID, not its actual object pointer
1723 */
1724 hdr->sc[i].id = sc;
1725 hdr->sc[i].objp = 0;
1726 }
1727
1728 /* request post-load initialization, to set up the superclass list */
1729 G_obj_table->request_post_load_init(self);
1730
1731 /*
1732 * invalidate any existing inheritance path, in case the superclass
1733 * list changed
1734 */
1735 hdr->inval_inh_path();
1736
1737 /* read the modified properties */
1738 for (ushort i = 0 ; i < mod_count ; ++i)
1739 {
1740 /* read the next slot */
1741 char buf[32];
1742 fp->read_bytes(buf, 2 + VMB_DATAHOLDER);
1743
1744 /* fix up this entry */
1745 fixups->fix_dh(vmg_ buf + 2);
1746
1747 /* decode the entry */
1748 vm_prop_id_t prop = (vm_prop_id_t)osrp2(buf);
1749 vm_val_t val;
1750 vmb_get_dh(buf + 2, &val);
1751
1752 /*
1753 * store the entry (don't save any undo for the operation, as we
1754 * can't undo a load)
1755 */
1756 set_prop(vmg_ 0, self, prop, &val);
1757 }
1758
1759 /* clear all undo information */
1760 clear_undo_flags();
1761
1762 /*
1763 * If we were saved to a file in the first place, it's because we were
1764 * modified or newly created relative to the image file data. So on
1765 * restore, we're necessarily a modified object that will need to be
1766 * saved again, even if we're not modified between now and the next
1767 * save.
1768 */
1769 get_hdr()->intern_obj_flags |= VMTO_OBJ_MOD;
1770 }
1771
1772 /* ------------------------------------------------------------------------ */
1773 /*
1774 * Post-load initialization: cache the object pointers for our
1775 * superclasses.
1776 */
1777 void CVmObjTads::post_load_init(VMG_ vm_obj_id_t self)
1778 {
1779 /* get my header */
1780 vm_tadsobj_hdr *hdr = get_hdr();
1781
1782 /* cache the superclass object pointers */
1783 for (int i = 0 ; i < hdr->sc_cnt ; ++i)
1784 hdr->sc[i].objp = (CVmObjTads *)vm_objp(vmg_ hdr->sc[i].id);
1785 }
1786
1787 /* ------------------------------------------------------------------------ */
1788 /*
1789 * Load the object from an image file
1790 */
1791 void CVmObjTads::load_from_image(VMG_ vm_obj_id_t self,
1792 const char *ptr, size_t siz)
1793 {
1794 /* save our image data pointer for reloading */
1795 G_obj_table->save_image_pointer(self, ptr, siz);
1796
1797 /* if we already have memory allocated, free it */
1798 if (ext_ != 0)
1799 {
1800 G_mem->get_var_heap()->free_mem(ext_);
1801 ext_ = 0;
1802 }
1803
1804 /* get the number of superclasses */
1805 ushort sc_cnt = osrp2(ptr);
1806
1807 /* get the number of load image properties */
1808 ushort li_cnt = osrp2(ptr + 2);
1809
1810 /* allocate our header */
1811 ext_ = (char *)vm_tadsobj_hdr::alloc(vmg_ this, sc_cnt, li_cnt);
1812 vm_tadsobj_hdr *hdr = get_hdr();
1813
1814 /* read the object flags from the image file and store them */
1815 hdr->li_obj_flags = osrp2(ptr + 4);
1816
1817 /*
1818 * set our internal flags - we come from the load image file, and we're
1819 * not yet modified from the load image data
1820 */
1821 hdr->intern_obj_flags |= VMTO_OBJ_IMAGE;
1822 hdr->intern_obj_flags &= ~VMTO_OBJ_MOD;
1823
1824 /* load the image file properties */
1825 load_image_props_and_scs(vmg_ ptr, siz);
1826
1827 /* request post-load initialization, to set up the superclass list */
1828 G_obj_table->request_post_load_init(self);
1829 }
1830
1831 /*
1832 * Reset to image file state. Discards all modified properties, so that
1833 * we have only the image file properties.
1834 */
1835 void CVmObjTads::reload_from_image(VMG_ vm_obj_id_t self,
1836 const char *ptr, size_t siz)
1837 {
1838 /* get my header */
1839 vm_tadsobj_hdr *hdr = get_hdr();
1840
1841 /* get the number of superclasses */
1842 ushort sc_cnt = osrp2(ptr);
1843
1844 /*
1845 * Clear the property table. We don't have to worry about the new
1846 * property table being larger than the existing property table,
1847 * because we can't have shrunk since we were originally loaded. So,
1848 * all we need to do is mark all property entries as free and clear
1849 * out the hash table.
1850 */
1851 hdr->prop_entry_free = 0;
1852 memset(hdr->hash_arr, 0, hdr->hash_siz * sizeof(hdr->hash_arr[0]));
1853
1854 /* if we need space for more superclasses, reallocate the header */
1855 if (sc_cnt > hdr->sc_cnt)
1856 {
1857 /* allocate the new header */
1858 ext_ = (char *)vm_tadsobj_hdr::expand_to(
1859 vmg_ this, hdr, sc_cnt, hdr->prop_entry_cnt);
1860 }
1861
1862 /* reload the image properties and superclasses */
1863 load_image_props_and_scs(vmg_ ptr, siz);
1864
1865 /* we're now unmodified from the image file state */
1866 hdr->intern_obj_flags &= ~VMTO_OBJ_MOD;
1867
1868 /* request post-load initialization, to set up the superclass list */
1869 G_obj_table->request_post_load_init(self);
1870 }
1871
1872 /*
1873 * Load the property list from the image data
1874 */
1875 void CVmObjTads::load_image_props_and_scs(VMG_ const char *ptr, size_t siz)
1876 {
1877 /* get my header */
1878 vm_tadsobj_hdr *hdr = get_hdr();
1879
1880 /* get the number of superclasses */
1881 ushort sc_cnt = osrp2(ptr);
1882
1883 /* get the number of load image properties */
1884 ushort li_cnt = osrp2(ptr + 2);
1885
1886 /* read the superclasses from the load image and store them */
1887 ushort i;
1888 const char *p;
1889 for (i = 0, p = ptr + 6 ; i < sc_cnt ; ++i, p += 4)
1890 {
1891 /* store the object ID */
1892 hdr->sc[i].id = (vm_obj_id_t)t3rp4u(p);
1893
1894 /*
1895 * We can't store the superclass pointer yet, as the superclass
1896 * object might not be loaded yet. Store null for now; we'll fix
1897 * this up in post_load_init() after all the other objects have
1898 * been loaded.
1899 */
1900 hdr->sc[i].objp = 0;
1901 }
1902
1903 /* read the properties from the load image and store them */
1904 for (i = 0 ; i < li_cnt ; ++i, p += 2 + VMB_DATAHOLDER)
1905 {
1906 /* decode the property data */
1907 vm_prop_id_t prop = (vm_prop_id_t)osrp2(p);
1908 vm_val_t val;
1909 vmb_get_dh(p + 2, &val);
1910
1911 /* store the property */
1912 hdr->alloc_prop_entry(prop, &val, 0);
1913 }
1914 }
1915
1916 /* ------------------------------------------------------------------------ */
1917 /*
1918 * Property evaluator - createInstance
1919 */
1920 int CVmObjTads::getp_create_instance(VMG_ vm_obj_id_t self,
1921 vm_val_t *retval, uint *in_argc)
1922 {
1923 /* create a persistent instance */
1924 return getp_create_common(vmg_ self, retval, in_argc, FALSE);
1925 }
1926
1927 /*
1928 * Property evaluator - createTransientInstance
1929 */
1930 int CVmObjTads::getp_create_trans_instance(VMG_ vm_obj_id_t self,
1931 vm_val_t *retval, uint *in_argc)
1932 {
1933 /* create a transient instance */
1934 return getp_create_common(vmg_ self, retval, in_argc, TRUE);
1935 }
1936
1937 /*
1938 * Common handler for createInstance() and createTransientInstance()
1939 */
1940 int CVmObjTads::getp_create_common(VMG_ vm_obj_id_t self,
1941 vm_val_t *retval, uint *in_argc,
1942 int is_transient)
1943 {
1944 uint argc = (in_argc != 0 ? *in_argc : 0);
1945 static CVmNativeCodeDesc desc(0, 0, TRUE);
1946
1947 /* check arguments - any number are allowed */
1948 if (get_prop_check_argc(retval, in_argc, &desc))
1949 return TRUE;
1950
1951 /*
1952 * push myself as the first argument - 'self' is the superclass of the
1953 * object to be created
1954 */
1955 G_interpreter->push_obj(vmg_ self);
1956
1957 /*
1958 * Create an instance - this will recursively execute the new object's
1959 * constructor, if it has one. Note that we have one more argument
1960 * than provided by the caller, because we've pushed the implicit
1961 * argument ('self') that create_from_stack uses to identify the
1962 * superclass.
1963 */
1964 retval->set_obj(create_from_stack_intern(vmg_ 0, argc + 1,
1965 is_transient));
1966
1967 /* handled */
1968 return TRUE;
1969 }
1970
1971 /* ------------------------------------------------------------------------ */
1972 /*
1973 * Property evaluator - createClone
1974 */
1975 int CVmObjTads::getp_create_clone(VMG_ vm_obj_id_t self,
1976 vm_val_t *retval, uint *argc)
1977 {
1978 static CVmNativeCodeDesc desc(0);
1979 vm_obj_id_t new_obj;
1980 CVmObjTads *tobj;
1981 vm_tadsobj_prop *entry;
1982 ushort i;
1983 vm_tadsobj_hdr *hdr = get_hdr();
1984
1985 /* check arguments */
1986 if (get_prop_check_argc(retval, argc, &desc))
1987 return TRUE;
1988
1989 /*
1990 * create a new object with the same number of superclasses as I have,
1991 * and with space for all of my properties
1992 */
1993 new_obj = create(vmg_ FALSE, get_sc_count(), hdr->prop_entry_free);
1994 tobj = (CVmObjTads *)vm_objp(vmg_ new_obj);
1995
1996 /* copy my superclass list to the new object */
1997 for (i = 0 ; i < get_sc_count() ; ++i)
1998 tobj->set_sc(vmg_ i, get_sc(i));
1999
2000 /* copy my properties to the new object */
2001 for (i = hdr->prop_entry_free, entry = hdr->prop_entry_arr ;
2002 i != 0 ; --i, ++entry)
2003 {
2004 /*
2005 * Store the property in the new object. We don't need to store
2006 * undo for the property, as the object is entirely new since the
2007 * last savepoint (as there can't have been a savepoint while we've
2008 * been working, obviously).
2009 */
2010 tobj->set_prop(vmg_ 0, self, entry->prop, &entry->val);
2011 }
2012
2013 /* the return value is the new object ID */
2014 retval->set_obj(new_obj);
2015
2016 /* handled */
2017 return TRUE;
2018 }
2019
2020 /* ------------------------------------------------------------------------ */
2021 /*
2022 * Property evaluator - createInstanceOf
2023 */
2024 int CVmObjTads::getp_create_instance_of(VMG_ vm_obj_id_t self,
2025 vm_val_t *retval, uint *in_argc)
2026 {
2027 /* create a persistent instance */
2028 return getp_create_multi_common(vmg_ self, retval, in_argc, FALSE);
2029 }
2030
2031 /*
2032 * Property evaluator - createTransientInstanceOf
2033 */
2034 int CVmObjTads::getp_create_trans_instance_of(
2035 VMG_ vm_obj_id_t self, vm_val_t *retval, uint *in_argc)
2036 {
2037 /* create a persistent instance */
2038 return getp_create_multi_common(vmg_ self, retval, in_argc, TRUE);
2039 }
2040
2041 /*
2042 * Common handler for createInstanceOf() and createTransientInstanceOf()
2043 */
2044 int CVmObjTads::getp_create_multi_common(VMG_ vm_obj_id_t self,
2045 vm_val_t *retval, uint *in_argc,
2046 int is_transient)
2047 {
2048 uint argc = (in_argc != 0 ? *in_argc : 0);
2049 static CVmNativeCodeDesc desc(0, 0, TRUE);
2050
2051 /* check arguments - any number are allowed */
2052 if (get_prop_check_argc(retval, in_argc, &desc))
2053 return TRUE;
2054
2055 /* create the new instance */
2056 retval->set_obj(create_from_stack_multi(vmg_ argc, is_transient));
2057
2058 /* handled */
2059 return TRUE;
2060 }
2061
2062 /* ------------------------------------------------------------------------ */
2063 /*
2064 * Iteration callback for getp_set_sc_list. On changing an object's
2065 * superclass list, we have to rebuild the inheritance path cache for any
2066 * object that has the modified object anywhere in its path.
2067 */
2068 struct set_sc_cb_ctx
2069 {
2070 set_sc_cb_ctx(vm_obj_id_t obj) { this->obj = obj; }
2071 vm_obj_id_t obj;
2072 };
2073
2074 void CVmObjTads::set_sc_cb(VMG_ vm_obj_id_t obj, void *ctx0)
2075 {
2076 /* if this is a TadsObject instance, update it */
2077 if (CVmObjTads::is_tadsobj_obj(vmg_ obj))
2078 {
2079 /* cast the context to our private structure */
2080 set_sc_cb_ctx *ctx = (set_sc_cb_ctx *)ctx0;
2081
2082 /* get this object's header */
2083 vm_tadsobj_hdr *hdr = ((CVmObjTads *)vm_objp(vmg_ obj))->get_hdr();
2084
2085 /*
2086 * if it has a cached inheritance path, check to see if it contains
2087 * the object being changed
2088 */
2089 tadsobj_objid_and_ptr *path = hdr->inh_path;
2090 if (path != 0)
2091 {
2092 /* scan the path for 'obj' */
2093 for ( ; path->id != VM_INVALID_OBJ ; ++path)
2094 {
2095 /* if this superclass is 'obj', we must delete the path */
2096 if (path->id == ctx->obj)
2097 {
2098 /* we need to drop this path */
2099 hdr->inval_inh_path();
2100
2101 /* no need to look any further */
2102 break;
2103 }
2104 }
2105 }
2106 }
2107 }
2108
2109 /* ------------------------------------------------------------------------ */
2110 /*
2111 * Property evaluator - setSuperclassList
2112 */
2113 int CVmObjTads::getp_set_sc_list(VMG_ vm_obj_id_t self,
2114 vm_val_t *retval, uint *in_argc)
2115 {
2116 static CVmNativeCodeDesc desc(1);
2117 int cnt;
2118 int i;
2119 vm_val_t ele;
2120 int sc_cnt;
2121 vm_tadsobj_hdr *hdr = get_hdr();
2122 vm_val_t *lst;
2123
2124 /* check arguments */
2125 if (get_prop_check_argc(retval, in_argc, &desc))
2126 return TRUE;
2127
2128 /* get the list argument (but leave it on the stack for now) */
2129 lst = G_stk->get(0);
2130 if (!lst->is_listlike(vmg0_) || (cnt = lst->ll_length(vmg0_)) < 0)
2131 err_throw(VMERR_BAD_TYPE_BIF);
2132
2133 /* we need at least one argument - the minimal root is TadsObject */
2134 if (cnt < 1)
2135 err_throw(VMERR_BAD_VAL_BIF);
2136
2137 /*
2138 * Check for a special case: our entire superclass list consists of
2139 * [TadsObject]. In this case, we have nothing in our internal
2140 * superclass list, since our only superclass is our metaclass.
2141 */
2142 lst->ll_index(vmg_ &ele, 1);
2143 if (cnt == 1
2144 && ele.typ == VM_OBJ
2145 && ele.val.obj == metaclass_reg_->get_class_obj(vmg0_))
2146 {
2147 /* use an empty internal superclass list */
2148 sc_cnt = 0;
2149 }
2150 else
2151 {
2152 /*
2153 * Scan the superclasses. Each superclass must be a TadsObject,
2154 * with the one exception that if we have only one superclass, it
2155 * can be the TadsObject intrinsic class itself, signifying that we
2156 * have no superclasses.
2157 */
2158 for (i = 1 ; i <= cnt ; ++i)
2159 {
2160 /* get this element from the list */
2161 lst->ll_index(vmg_ &ele, i);
2162
2163 /* it has to be an object of type TadsObject */
2164 if (ele.typ != VM_OBJ || !is_tadsobj_obj(vmg_ ele.val.obj))
2165 err_throw(VMERR_BAD_VAL_BIF);
2166
2167 /*
2168 * make sure that this superclass doesn't inherit from 'self' -
2169 * if it does, that would create a circular inheritance
2170 * hierarchy, which is illegal
2171 */
2172 if (vm_objp(vmg_ ele.val.obj)->is_instance_of(vmg_ self))
2173 err_throw(VMERR_BAD_VAL_BIF);
2174 }
2175
2176 /* the list is valid - we need one superclass per list element */
2177 sc_cnt = cnt;
2178 }
2179
2180 /* mark the overall object as modified if it's not already */
2181 mark_modified(vmg_ G_undo, self);
2182
2183 /* if there's a system undo object, add undo for the change */
2184 if (G_undo != 0)
2185 {
2186 /* allocate a list for the results */
2187 vm_val_t oldv;
2188 oldv.set_obj(CVmObjList::create(vmg_ FALSE, hdr->sc_cnt));
2189 CVmObjList *oldp = (CVmObjList *)vm_objp(vmg_ oldv.val.obj);
2190
2191 /* build the superclass list */
2192 for (i = 0 ; i < hdr->sc_cnt ; ++i)
2193 {
2194 /* add this superclass to the list */
2195 ele.set_obj(hdr->sc[i].id);
2196 oldp->cons_set_element(i, &ele);
2197 }
2198
2199 /*
2200 * Add an undo record with the original superclass list as the old
2201 * value. Use the 'invalid' property as the property key; when
2202 * this property is used with a list value, it indicates a
2203 * superclass list change.
2204 */
2205 G_undo->add_new_record_prop_key(vmg_ self, VM_INVALID_PROP, &oldv);
2206 }
2207
2208 /* update the superclass list with the given list */
2209 change_superclass_list(vmg_ lst, sc_cnt);
2210
2211 /* discard arguments */
2212 G_stk->discard();
2213
2214 /* no return value */
2215 retval->set_nil();
2216
2217 /* we need to clear all cached superclass path lists involving 'self' */
2218 set_sc_cb_ctx ctx(self);
2219 G_obj_table->for_each(vmg_ &set_sc_cb, &ctx);
2220
2221 /* handled */
2222 return TRUE;
2223 }
2224
2225 /*
2226 * Change the superclass list to the given list. 'lstp' is the new
2227 * superclass list, in constant list format (i.e., a packed array of
2228 * dataholder values).
2229 */
2230 void CVmObjTads::change_superclass_list(VMG_ const vm_val_t *lst, int cnt)
2231 {
2232 vm_tadsobj_hdr *hdr = get_hdr();
2233 int i;
2234
2235 /* keep the count within range */
2236 cnt = (cnt > UINT16MAXVAL ? UINT16MAXVAL : cnt < 0 ? 0 : cnt);
2237
2238 /*
2239 * if we're increasing the number of superclasses, expand our object
2240 * header to make room
2241 */
2242 if (cnt > (int)hdr->sc_cnt)
2243 {
2244 /* expand the header to accomodate the new superclass list */
2245 ext_ = (char *)vm_tadsobj_hdr::expand_to(
2246 vmg_ this, hdr, cnt, hdr->prop_entry_cnt);
2247
2248 /* get the new header */
2249 hdr = get_hdr();
2250 }
2251
2252 /* set the new superclass count */
2253 hdr->sc_cnt = (ushort)cnt;
2254
2255 /* set the new superclasses */
2256 for (i = 0 ; i < cnt ; ++i)
2257 {
2258 vm_val_t ele;
2259
2260 /* get this element from the list */
2261 lst->ll_index(vmg_ &ele, i+1);
2262
2263 /* set this superclass in the header */
2264 hdr->sc[i].id = ele.val.obj;
2265 hdr->sc[i].objp = (CVmObjTads *)vm_objp(vmg_ ele.val.obj);
2266 }
2267
2268 /* invalidate the cached inheritance path */
2269 hdr->inval_inh_path();
2270 }
2271
2272 /* ------------------------------------------------------------------------ */
2273 /*
2274 * Get a method pointer. This is almost like an ordinary getprop, but if
2275 * the property contains a method, returns a function pointer to the method
2276 * rather than evaluating it.
2277 */
2278 int CVmObjTads::getp_get_method(VMG_ vm_obj_id_t self,
2279 vm_val_t *retval, uint *argc)
2280 {
2281 static CVmNativeCodeDesc desc(1);
2282
2283 /* check arguments: getMethod(&propid) */
2284 if (get_prop_check_argc(retval, argc, &desc))
2285 return TRUE;
2286
2287 /* retrieve the property ID */
2288 vm_obj_id_t prop = CVmBif::pop_propid_val(vmg0_);
2289
2290 /* find the property */
2291 vm_obj_id_t source_obj;
2292 tadsobj_sc_search_ctx curpos(vmg_ self, this);
2293 if (curpos.find_prop(vmg_ prop, retval, &source_obj))
2294 {
2295 /* the property is definfed - check its type */
2296 switch (retval->typ)
2297 {
2298 case VM_CODEOFS:
2299 /* it's a direct call to code - convert to a function pointer */
2300 retval->typ = VM_FUNCPTR;
2301 break;
2302
2303 case VM_DSTRING:
2304 /* it's a self-printing constant string - convert to string */
2305 retval->typ = VM_SSTRING;
2306 break;
2307
2308 case VM_OBJX:
2309 /*
2310 * It's an executable object, which is either a self-printing
2311 * non-constant string or an execute-on-invoke anonymous
2312 * function. In either case, simply convert it back to an
2313 * ordinary value.
2314 */
2315 retval->typ = VM_OBJ;
2316 break;
2317
2318 case VM_BIFPTRX:
2319 /*
2320 * executable built-in function pointer - convert back to a
2321 * regular bif pointer
2322 */
2323 retval->typ = VM_BIFPTR;
2324 break;
2325
2326 default:
2327 /* other types aren't executable methods, so return nil */
2328 retval->set_nil();
2329 break;
2330 }
2331 }
2332 else
2333 {
2334 /* this property isn't defined - return nil */
2335 retval->set_nil();
2336 }
2337
2338 /* handled */
2339 return TRUE;
2340 }
2341
2342 /*
2343 * Set a method pointer. This is almost like an ordinary setprop, but if
2344 * the value to assign contains a function pointer, this sets the property
2345 * to a method rather than to a pointer-to-function value.
2346 */
2347 int CVmObjTads::getp_set_method(VMG_ vm_obj_id_t self,
2348 vm_val_t *retval, uint *argc)
2349 {
2350 /* check arguments: setMethod(&propid, val) */
2351 static CVmNativeCodeDesc desc(2);
2352 if (get_prop_check_argc(retval, argc, &desc))
2353 return TRUE;
2354
2355 /*
2356 * retrieve the property ID and the method value to set (leave the
2357 * value on the stack as gc protection)
2358 */
2359 vm_prop_id_t prop = CVmBif::pop_propid_val(vmg0_);
2360 vm_val_t mval = *G_stk->get(0);
2361
2362 /* check the type */
2363 switch (mval.typ)
2364 {
2365 case VM_FUNCPTR:
2366 /*
2367 * a function pointer translates directly to a regular method, by
2368 * directly calling the code body
2369 */
2370 mval.typ = VM_CODEOFS;
2371 break;
2372
2373 case VM_OBJ:
2374 /*
2375 * We can accept string and invokable objects. For either one of
2376 * these, we store the object reference and mark it as an
2377 * execute-on-eval object rather than an ordinary object value.
2378 */
2379 if (CVmObjString::is_string_obj(vmg_ mval.val.obj)
2380 || vm_objp(vmg_ mval.val.obj)->get_invoker(vmg_ 0))
2381 {
2382 /*
2383 * We can handle this object by executing it on evaluation.
2384 * Mark it as a special execute-on-eval object rather than an
2385 * ordinary object.
2386 */
2387 mval.typ = VM_OBJX;
2388 }
2389 else
2390 {
2391 /* it's not an acceptable type of object */
2392 err_throw(VMERR_BAD_TYPE_BIF);
2393 }
2394 break;
2395
2396 case VM_BIFPTR:
2397 /* built-in function pointer - mark it as an execute-on-eval bif */
2398 mval.typ = VM_BIFPTRX;
2399 break;
2400
2401 case VM_SSTRING:
2402 /*
2403 * A constant string translates into a self-printing string.
2404 * SSTRING and DSTRING have the same data representation, so we
2405 * merely need to switch the type to flag it as print-on-eval.
2406 */
2407 mval.typ = VM_DSTRING;
2408 break;
2409
2410 default:
2411 /* other types are not acceptable */
2412 err_throw(VMERR_BAD_TYPE_BIF);
2413 }
2414
2415 /* set the property value */
2416 set_prop(vmg_ G_undo, self, prop, &mval);
2417
2418 /* discard the method value */
2419 G_stk->discard();
2420
2421 /* no return value */
2422 retval->set_nil();
2423
2424 /* handled */
2425 return TRUE;
2426 }
2427
2428 /* ------------------------------------------------------------------------ */
2429 /*
2430 * Intrinsic Class Modifier object implementation
2431 */
2432
2433 /* metaclass registration object */
2434 static CVmMetaclassIntClsMod metaclass_reg_obj_icm;
2435 CVmMetaclass *CVmObjIntClsMod::metaclass_reg_ = &metaclass_reg_obj_icm;
2436
2437 /*
2438 * Get a property. Intrinsic class modifiers do not have intrinsic
2439 * superclasses, because they're effectively mix-in classes. Therefore,
2440 * do not look for intrinsic properties or intrinsic superclass properties
2441 * to resolve the property lookup.
2442 */
2443 int CVmObjIntClsMod::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
2444 vm_obj_id_t self, vm_obj_id_t *source_obj,
2445 uint *argc)
2446 {
2447 /*
2448 * try finding the property in our property list or a superclass
2449 * property list
2450 */
2451 tadsobj_sc_search_ctx curpos(vmg_ self, this);
2452 if (curpos.find_prop(vmg_ prop, val, source_obj))
2453 return TRUE;
2454
2455 /*
2456 * We didn't find it in our list, so we don't have the property.
2457 * Because we're an intrinsic mix-in, we don't look for an intrinsic
2458 * implementation or an intrinsic superclass implementation.
2459 */
2460 return FALSE;
2461 }
2462
2463 /*
2464 * Inherit a property. As with get_prop(), we don't want to inherit from
2465 * any intrinsic superclass if we don't find the property in our property
2466 * list or an inherited property list.
2467 */
2468 int CVmObjIntClsMod::inh_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
2469 vm_obj_id_t self,
2470 vm_obj_id_t orig_target_obj,
2471 vm_obj_id_t defining_obj,
2472 vm_obj_id_t *source_obj, uint *argc)
2473 {
2474 /*
2475 * try finding the property in our property list or a superclass
2476 * property list
2477 */
2478 if (search_for_prop_from(vmg_ prop, val, orig_target_obj,
2479 source_obj, defining_obj))
2480 return TRUE;
2481
2482 /*
2483 * we didn't find it in our list, and we don't want to inherit from any
2484 * intrinsic superclass, so we don't have the property
2485 */
2486 return FALSE;
2487 }
2488
2489 /*
2490 * Build my property list. We build the complete list of methods defined
2491 * in the intrinsic class modifier for all classes, including any modify
2492 * base classes that we further modify.
2493 */
2494 void CVmObjIntClsMod::build_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval)
2495 {
2496 /* push a self-reference for gc protection */
2497 G_stk->push()->set_obj(self);
2498
2499 /* build our own list */
2500 CVmObjTads::build_prop_list(vmg_ self, retval);
2501
2502 /* if we have a base class that we further modify, add its list */
2503 if (get_sc_count() != 0)
2504 {
2505 vm_obj_id_t base_id;
2506 CVmObject *base_obj;
2507
2508 /* get the base class */
2509 base_id = get_sc(0);
2510 base_obj = vm_objp(vmg_ base_id);
2511
2512 /* get its list only if it's of our same metaclass */
2513 if (base_obj->get_metaclass_reg() == get_metaclass_reg())
2514 {
2515 vm_val_t base_val;
2516
2517 /* save our list for gc protection */
2518 G_stk->push(retval);
2519
2520 /* get our base class's list */
2521 base_obj->build_prop_list(vmg_ base_id, &base_val);
2522
2523 /* add this list to our result list */
2524 vm_objp(vmg_ retval->val.obj)->
2525 add_val(vmg_ retval, retval->val.obj, &base_val);
2526
2527 /* discard our gc protection */
2528 G_stk->discard();
2529 }
2530 }
2531
2532 /* discard gc protection */
2533 G_stk->discard();
2534 }