| 1 | /* $Header: d:/cvsroot/tads/tads3/VMTOBJ.H,v 1.2 1999/05/17 02:52:29 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 | vmtobj.h - VM TADS Object implementation |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | This implementation assumes a non-relocating memory manager, both for |
| 16 | the fixed part (the CVmObject part) and the variable part (the |
| 17 | "extension," located in the variable-part heap) of our objects. In the |
| 18 | present implementation, the memory manager satisfies this requirement, and |
| 19 | there are no plans to change this. |
| 20 | |
| 21 | The memory manager is designed *in principal* to allow for object |
| 22 | relocation (specifically as a means to reduce heap fragmentation), so it's |
| 23 | not a foregone conclusion that such a thing will never be implemented. |
| 24 | However, given the large memories of modern machines (especially relative |
| 25 | to the size of a typical tads application), and given that recent academic |
| 26 | research has been calling into question the conventional wisdom that heap |
| 27 | fragmentation is actually a problem in practice, we consider the |
| 28 | probability that we will want to implement a relocation memory manager |
| 29 | low, and thus we feel it's better to exploit the efficiencies of using and |
| 30 | storing direct object pointers in some places in this code. |
| 31 | Modified |
| 32 | 10/30/98 MJRoberts - Creation |
| 33 | */ |
| 34 | |
| 35 | #ifndef VMTOBJ_H |
| 36 | #define VMTOBJ_H |
| 37 | |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | |
| 41 | #include "t3std.h" |
| 42 | #include "vmtype.h" |
| 43 | #include "vmglob.h" |
| 44 | #include "vmobj.h" |
| 45 | #include "vmundo.h" |
| 46 | |
| 47 | /* forward-declare our main class */ |
| 48 | class CVmObjTads; |
| 49 | |
| 50 | /* ------------------------------------------------------------------------ */ |
| 51 | /* |
| 52 | * TADS-Object image file data. The image file state is loaded into an |
| 53 | * image object data block, and we set up our own internal data based on |
| 54 | * it at load time. The image file data block is arranged as follows: |
| 55 | * |
| 56 | *. UINT2 superclass_count |
| 57 | *. UINT2 load_image_property_count |
| 58 | *. UINT2 flags |
| 59 | *. UINT4 superclass_1 |
| 60 | *. ... |
| 61 | *. UINT4 superclass_N |
| 62 | *. UINT2 load_image_property_ID_1 |
| 63 | *. DATAHOLDER load_image_property_value_1 |
| 64 | *. ... |
| 65 | *. UINT2 load_image_property_ID_N |
| 66 | *. DATAHOLDER load_image_property_value_N |
| 67 | */ |
| 68 | |
| 69 | /* superclass structure for object extension */ |
| 70 | struct vm_tadsobj_sc |
| 71 | { |
| 72 | vm_obj_id_t id; |
| 73 | CVmObjTads *objp; |
| 74 | }; |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * For our in-memory object extension, we use a structure that stores the |
| 79 | * object data. We store the properties in a hash table keyed on property |
| 80 | * ID. |
| 81 | */ |
| 82 | struct vm_tadsobj_hdr |
| 83 | { |
| 84 | /* allocate */ |
| 85 | static vm_tadsobj_hdr *alloc(VMG_ class CVmObjTads *self, |
| 86 | unsigned short sc_cnt, |
| 87 | unsigned short prop_cnt); |
| 88 | |
| 89 | /* delete */ |
| 90 | void free_mem(); |
| 91 | |
| 92 | /* reallocate an existing object to expand its property table */ |
| 93 | static vm_tadsobj_hdr *expand(VMG_ class CVmObjTads *self, |
| 94 | vm_tadsobj_hdr *obj); |
| 95 | |
| 96 | /* |
| 97 | * reallocate an existing object to expand its property table to the |
| 98 | * given minimum number of property entries |
| 99 | */ |
| 100 | static vm_tadsobj_hdr *expand_to(VMG_ class CVmObjTads *self, |
| 101 | vm_tadsobj_hdr *obj, |
| 102 | size_t new_sc_cnt, size_t min_prop_cnt); |
| 103 | |
| 104 | /* invalidate the cached inheritance path, if any */ |
| 105 | void inval_inh_path() |
| 106 | { |
| 107 | /* if we have an inheritance path cached, forget it */ |
| 108 | if (inh_path != 0) |
| 109 | { |
| 110 | /* forget the path, so that we recalculate it on demand */ |
| 111 | t3free(inh_path); |
| 112 | inh_path = 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /* find a property entry */ |
| 117 | struct vm_tadsobj_prop *find_prop_entry(uint prop); |
| 118 | |
| 119 | /* allocate a new hash entry */ |
| 120 | vm_tadsobj_prop *alloc_prop_entry(vm_prop_id_t prop, |
| 121 | const vm_val_t *val, |
| 122 | unsigned int flags); |
| 123 | |
| 124 | /* calculate the hash code for a property */ |
| 125 | unsigned int calc_hash(uint prop) const |
| 126 | { |
| 127 | /* simply take the property ID modulo the table size */ |
| 128 | return (unsigned int)(prop & (hash_siz - 1)); |
| 129 | } |
| 130 | |
| 131 | /* check to see if we have the required number of free entries */ |
| 132 | int has_free_entries(size_t cnt) const |
| 133 | { return cnt <= (size_t)(prop_entry_free - prop_entry_cnt); } |
| 134 | |
| 135 | /* cache and return the inheritance search path for this object */ |
| 136 | struct tadsobj_objid_and_ptr *get_inh_search_path(VMG0_); |
| 137 | |
| 138 | /* |
| 139 | * Inheritance search table. We build and save the search path for any |
| 140 | * class with multiple superclasses, because the inheritance path for a |
| 141 | * class with multiple base classes can be somewhat time-consuming to |
| 142 | * determine. For objects with only one base class, we don't bother |
| 143 | * caching a path, since the path is trivial to calculate in these |
| 144 | * cases. |
| 145 | */ |
| 146 | struct tadsobj_objid_and_ptr *inh_path; |
| 147 | |
| 148 | /* load image object flags (a combination of VMTOBJ_OBJF_xxx values) */ |
| 149 | unsigned short li_obj_flags; |
| 150 | |
| 151 | /* internal object flags (a combination of VMTO_OBJ_xxx values) */ |
| 152 | unsigned short intern_obj_flags; |
| 153 | |
| 154 | /* |
| 155 | * Number of hash buckets, and a pointer to the bucket array. (The |
| 156 | * hash bucket array is allocated as part of the same memory block as |
| 157 | * this structure - we suballocate it from the memory block when |
| 158 | * allocating the structure.) 'hash_arr[hash]' points to the head of |
| 159 | * a list of property entries with the given hash value. |
| 160 | */ |
| 161 | unsigned short hash_siz; |
| 162 | struct vm_tadsobj_prop **hash_arr; |
| 163 | |
| 164 | /* |
| 165 | * Pointer to our allocation array of hash buckets. We suballocate |
| 166 | * this out of our allocation block. (Note that this isn't the hash |
| 167 | * table; this is the pool of elements out of which hash table entries |
| 168 | * - not buckets, but the entries in the lists pointed to by the |
| 169 | * buckets - are allocated.) |
| 170 | */ |
| 171 | struct vm_tadsobj_prop *prop_entry_arr; |
| 172 | |
| 173 | /* total number of hash entries allocated */ |
| 174 | unsigned short prop_entry_cnt; |
| 175 | |
| 176 | /* |
| 177 | * Index of next available hash entry. Hash entries are never |
| 178 | * deleted, so we don't have to worry about returning entries to the |
| 179 | * free pool. So, the free pool simply consists of entries from this |
| 180 | * index to the maximum index (prop_entry_cnt - 1). |
| 181 | * |
| 182 | * When we run out of entries, we must reallocate this entire |
| 183 | * structure to make room for more. This means that reallocation is |
| 184 | * fairly expensive, but this is acceptable because we will always |
| 185 | * want to resize the hash table at the same time anyway. We always |
| 186 | * resize the hash table on exhausting our current allocation size |
| 187 | * because we pick the hash table size based on the expected maximum |
| 188 | * number of entries; once we exceed that maximum, we must reconsider |
| 189 | * the hash table size. |
| 190 | */ |
| 191 | unsigned short prop_entry_free; |
| 192 | |
| 193 | /* |
| 194 | * Number of superclasses, and the array of superclasses. We |
| 195 | * overallocate the structure to make room for enough superclasses. |
| 196 | * (Note that this means the 'sc' field must be the last thing in the |
| 197 | * structure.) |
| 198 | */ |
| 199 | unsigned short sc_cnt; |
| 200 | vm_tadsobj_sc sc[1]; |
| 201 | }; |
| 202 | |
| 203 | /* |
| 204 | * Tads-object property entry. Each hash table entry points to a linked |
| 205 | * list of these entries. |
| 206 | */ |
| 207 | struct vm_tadsobj_prop |
| 208 | { |
| 209 | /* my property ID */ |
| 210 | vm_prop_id_t prop; |
| 211 | |
| 212 | /* pointer to the next entry at the same hash value */ |
| 213 | vm_tadsobj_prop *nxt; |
| 214 | |
| 215 | /* flags */ |
| 216 | unsigned char flags; |
| 217 | |
| 218 | /* my value */ |
| 219 | vm_val_t val; |
| 220 | }; |
| 221 | |
| 222 | |
| 223 | /* |
| 224 | * Internal object flags |
| 225 | */ |
| 226 | |
| 227 | /* from load image - object originally came from image file */ |
| 228 | #define VMTO_OBJ_IMAGE 0x0001 |
| 229 | |
| 230 | /* modified - object has been modified since being loaded from image */ |
| 231 | #define VMTO_OBJ_MOD 0x0002 |
| 232 | |
| 233 | |
| 234 | /* |
| 235 | * Property entry flags |
| 236 | */ |
| 237 | |
| 238 | /* modified - this property is not from the load image file */ |
| 239 | #define VMTO_PROP_MOD 0x01 |
| 240 | |
| 241 | /* we've stored undo for this property since the last savepoint */ |
| 242 | #define VMTO_PROP_UNDO 0x02 |
| 243 | |
| 244 | /* ------------------------------------------------------------------------ */ |
| 245 | /* |
| 246 | * Load Image Object Flag Values - these values are stored in the image |
| 247 | * file object header. |
| 248 | */ |
| 249 | |
| 250 | /* class - the object represents a class, not an instance */ |
| 251 | #define VMTOBJ_OBJF_CLASS 0x0001 |
| 252 | |
| 253 | |
| 254 | /* ------------------------------------------------------------------------ */ |
| 255 | /* |
| 256 | * Initial empty property table size. When we initially load an object, |
| 257 | * we'll allocate this many empty slots for modifiable properties. |
| 258 | */ |
| 259 | const ushort VMTOBJ_PROP_INIT = 16; |
| 260 | |
| 261 | |
| 262 | /* ------------------------------------------------------------------------ */ |
| 263 | /* |
| 264 | * TADS object interface. |
| 265 | */ |
| 266 | class CVmObjTads: public CVmObject |
| 267 | { |
| 268 | friend class CVmMetaclassTads; |
| 269 | friend struct tadsobj_sc_search_ctx; |
| 270 | friend struct vm_tadsobj_hdr; |
| 271 | |
| 272 | public: |
| 273 | /* metaclass registration object */ |
| 274 | static class CVmMetaclass *metaclass_reg_; |
| 275 | class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; } |
| 276 | |
| 277 | /* am I of the given metaclass? */ |
| 278 | virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 279 | { |
| 280 | /* try my own metaclass and my base class */ |
| 281 | return (meta == metaclass_reg_ |
| 282 | || CVmObject::is_of_metaclass(meta)); |
| 283 | } |
| 284 | |
| 285 | /* is the given object a TadsObject object? */ |
| 286 | static int is_tadsobj_obj(VMG_ vm_obj_id_t obj) |
| 287 | { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); } |
| 288 | |
| 289 | /* create dynamically using stack arguments */ |
| 290 | static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 291 | uint argc) |
| 292 | { return create_from_stack_intern(vmg_ pc_ptr, argc, FALSE); } |
| 293 | |
| 294 | /* |
| 295 | * call a static property - we don't have any of our own, so simply |
| 296 | * "inherit" the base class handling |
| 297 | */ |
| 298 | static int call_stat_prop(VMG_ vm_val_t *result, |
| 299 | const uchar **pc_ptr, uint *argc, |
| 300 | vm_prop_id_t prop); |
| 301 | |
| 302 | /* create an object with no initial extension */ |
| 303 | static vm_obj_id_t create(VMG_ int in_root_set); |
| 304 | |
| 305 | /* |
| 306 | * Create an object with a given number of superclasses, and a given |
| 307 | * number of property slots. The property slots are all initially |
| 308 | * allocated to modified properties. |
| 309 | */ |
| 310 | static vm_obj_id_t create(VMG_ int in_root_set, |
| 311 | ushort superclass_count, ushort prop_slots); |
| 312 | |
| 313 | /* notify of deletion */ |
| 314 | void notify_delete(VMG_ int in_root_set); |
| 315 | |
| 316 | /* create an instance of this object */ |
| 317 | void create_instance(VMG_ vm_obj_id_t self, |
| 318 | const uchar **pc_ptr, uint argc); |
| 319 | |
| 320 | /* determine if the object has a finalizer method */ |
| 321 | virtual int has_finalizer(VMG_ vm_obj_id_t /*self*/); |
| 322 | |
| 323 | /* invoke the object's finalizer */ |
| 324 | virtual void invoke_finalizer(VMG_ vm_obj_id_t self); |
| 325 | |
| 326 | // $$$ testing |
| 327 | virtual int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, |
| 328 | int depth) const |
| 329 | { |
| 330 | if (((vm_tadsobj_hdr *)ext_)->hash_siz == 0) |
| 331 | return TRUE; |
| 332 | else |
| 333 | return (val->typ == VM_OBJ && val->val.obj == self); |
| 334 | } |
| 335 | |
| 336 | virtual uint calc_hash(VMG_ vm_obj_id_t self, int /*depth*/) const |
| 337 | { |
| 338 | if (((vm_tadsobj_hdr *)ext_)->hash_siz == 0) |
| 339 | return 0; |
| 340 | else |
| 341 | return (uint)(((ulong)self & 0xffff) |
| 342 | ^ (((ulong)self & 0xffff0000) >> 16)); |
| 343 | } |
| 344 | // $$$ end testing |
| 345 | |
| 346 | /* get the number of superclasses of this object */ |
| 347 | virtual int get_superclass_count(VMG_ vm_obj_id_t self) const |
| 348 | { |
| 349 | /* |
| 350 | * if we have no superclass, inherit the default, since we |
| 351 | * inherit from the system TadsObject class; if we have our own |
| 352 | * superclasses, return them |
| 353 | */ |
| 354 | if (get_sc_count() == 0) |
| 355 | return CVmObject::get_superclass_count(vmg_ self); |
| 356 | else |
| 357 | return get_sc_count(); |
| 358 | } |
| 359 | |
| 360 | /* get the nth superclass of this object */ |
| 361 | virtual vm_obj_id_t get_superclass(VMG_ vm_obj_id_t self, int idx) const |
| 362 | { |
| 363 | /* |
| 364 | * if we have no superclass, inherit the default, since we |
| 365 | * inherit from the system TadsObject class; if we have our own |
| 366 | * superclasses, return them |
| 367 | */ |
| 368 | if (get_sc_count() == 0) |
| 369 | return CVmObject::get_superclass(vmg_ self, idx); |
| 370 | else if (idx >= get_sc_count()) |
| 371 | return VM_INVALID_OBJ; |
| 372 | else |
| 373 | return get_sc(idx); |
| 374 | } |
| 375 | |
| 376 | /* determine if I'm a class object */ |
| 377 | virtual int is_class_object(VMG_ vm_obj_id_t /*self*/) const |
| 378 | { return (get_li_obj_flags() & VMTOBJ_OBJF_CLASS) != 0; } |
| 379 | |
| 380 | /* determine if I'm an instance of the given object */ |
| 381 | int is_instance_of(VMG_ vm_obj_id_t obj); |
| 382 | |
| 383 | /* this object type provides properties */ |
| 384 | int provides_props(VMG0_) const { return TRUE; } |
| 385 | |
| 386 | /* enumerate properties */ |
| 387 | void enum_props(VMG_ vm_obj_id_t self, |
| 388 | void (*cb)(VMG_ void *ctx, |
| 389 | vm_obj_id_t self, vm_prop_id_t prop, |
| 390 | const vm_val_t *val), |
| 391 | void *cbctx); |
| 392 | |
| 393 | /* set a property */ |
| 394 | void set_prop(VMG_ class CVmUndo *undo, |
| 395 | vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val); |
| 396 | |
| 397 | /* get a property */ |
| 398 | int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 399 | vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc); |
| 400 | |
| 401 | /* inherit a property */ |
| 402 | int inh_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 403 | vm_obj_id_t self, |
| 404 | vm_obj_id_t orig_target_obj, |
| 405 | vm_obj_id_t defining_obj, |
| 406 | vm_obj_id_t *source_obj, uint *argc); |
| 407 | |
| 408 | /* build my property list */ |
| 409 | void build_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval); |
| 410 | |
| 411 | /* |
| 412 | * Receive notification of a new savepoint. We keep track of |
| 413 | * whether or not we've saved undo information for each modifiable |
| 414 | * property in the current savepoint, so that we can avoid saving |
| 415 | * redundant undo information when repeatedly changing a property |
| 416 | * value (since only the first change in a given savepoint needs to |
| 417 | * be recorded). When we start a new savepoint, we obviously |
| 418 | * haven't yet stored any undo information for the new savepoint, so |
| 419 | * we can simply clear all of the undo records. |
| 420 | */ |
| 421 | void notify_new_savept() |
| 422 | { clear_undo_flags(); } |
| 423 | |
| 424 | /* apply undo */ |
| 425 | void apply_undo(VMG_ struct CVmUndoRecord *rec); |
| 426 | |
| 427 | /* mark a reference in an undo record */ |
| 428 | void mark_undo_ref(VMG_ struct CVmUndoRecord *undo); |
| 429 | |
| 430 | /* |
| 431 | * remove stale weak references from an undo record -- we keep only |
| 432 | * normal strong references, so we don't need to do anything here |
| 433 | */ |
| 434 | void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { } |
| 435 | |
| 436 | /* mark references */ |
| 437 | void mark_refs(VMG_ uint state); |
| 438 | |
| 439 | /* |
| 440 | * remove weak references - we keep only normal (strong) references, |
| 441 | * so this routine doesn't need to do anything |
| 442 | */ |
| 443 | void remove_stale_weak_refs(VMG0_) { } |
| 444 | |
| 445 | /* load from an image file */ |
| 446 | void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz); |
| 447 | |
| 448 | /* restore to image file state */ |
| 449 | void reload_from_image(VMG_ vm_obj_id_t self, |
| 450 | const char *ptr, size_t siz); |
| 451 | |
| 452 | /* perform post-load initialization */ |
| 453 | void post_load_init(VMG_ vm_obj_id_t self); |
| 454 | |
| 455 | /* determine if the object has been changed since it was loaded */ |
| 456 | int is_changed_since_load() const; |
| 457 | |
| 458 | /* save to a file */ |
| 459 | void save_to_file(VMG_ class CVmFile *fp); |
| 460 | |
| 461 | /* restore from a file */ |
| 462 | void restore_from_file(VMG_ vm_obj_id_t self, |
| 463 | class CVmFile *fp, class CVmObjFixup *fixups); |
| 464 | |
| 465 | /* rebuild for image file */ |
| 466 | virtual ulong rebuild_image(VMG_ char *buf, ulong buflen); |
| 467 | |
| 468 | /* convert to constant data */ |
| 469 | virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper, |
| 470 | vm_obj_id_t self); |
| 471 | |
| 472 | /* get the nth superclass */ |
| 473 | vm_obj_id_t get_sc(uint n) const |
| 474 | { return get_hdr()->sc[n].id; } |
| 475 | |
| 476 | /* get a pointer to the object for the nth superclass */ |
| 477 | CVmObjTads *get_sc_objp(VMG_ ushort n) const |
| 478 | { return get_hdr()->sc[n].objp; } |
| 479 | |
| 480 | /* set the nth superclass to the given object */ |
| 481 | void set_sc(VMG_ ushort n, vm_obj_id_t obj) |
| 482 | { |
| 483 | get_hdr()->sc[n].id = obj; |
| 484 | get_hdr()->sc[n].objp = (CVmObjTads *)vm_objp(vmg_ obj); |
| 485 | } |
| 486 | |
| 487 | /* static class initialization/termination */ |
| 488 | static void class_init(VMG0_); |
| 489 | static void class_term(VMG0_); |
| 490 | |
| 491 | protected: |
| 492 | /* create an object with no initial extension */ |
| 493 | CVmObjTads() { ext_ = 0; } |
| 494 | |
| 495 | /* |
| 496 | * Create an object with a given number of superclasses, and a given |
| 497 | * number of property slots. All property slots are initially |
| 498 | * allocated to the modifiable property list. |
| 499 | */ |
| 500 | CVmObjTads(VMG_ ushort superclass_count, ushort prop_count); |
| 501 | |
| 502 | /* internal handler to create from stack arguments */ |
| 503 | static vm_obj_id_t create_from_stack_intern(VMG_ const uchar **pc_ptr, |
| 504 | uint argc, int is_transient); |
| 505 | |
| 506 | /* |
| 507 | * internal handler to create with multiple inheritance from arguments |
| 508 | * passed on the stack |
| 509 | */ |
| 510 | static vm_obj_id_t create_from_stack_multi(VMG_ uint argc, |
| 511 | int is_transient); |
| 512 | |
| 513 | /* get the load image object flags */ |
| 514 | uint get_li_obj_flags() const |
| 515 | { return get_hdr()->li_obj_flags; } |
| 516 | |
| 517 | /* set the object flags */ |
| 518 | void set_li_obj_flags(ushort flags) |
| 519 | { get_hdr()->li_obj_flags = flags; } |
| 520 | |
| 521 | /* |
| 522 | * mark the object as modified; sets the VMTO_OBJ_MOD flag, and adds an |
| 523 | * undo record for the transition if the flag wasn't previously set |
| 524 | */ |
| 525 | void mark_modified(VMG_ class CVmUndo *undo, vm_obj_id_t self); |
| 526 | |
| 527 | /* |
| 528 | * Allocate memory - this replaces any existing extension, so the |
| 529 | * caller must take care to free the extension (if one has already |
| 530 | * been allocated) before calling this routine. |
| 531 | * |
| 532 | * If 'from_image' is true, we're allocating memory for use with an |
| 533 | * object loaded from an image file, so we'll ignore the superclass |
| 534 | * count and leave the image_data pointer in the header unchanged. |
| 535 | * If 'from_image' is false, we're allocating memory for a dynamic |
| 536 | * object that does not have a presence in the image file, so we'll |
| 537 | * allocate space for the superclass list as part of the extension |
| 538 | * and set the image_data pointer in the header to refer the extra |
| 539 | * space after the modifiable property array and undo bit array. |
| 540 | */ |
| 541 | void alloc_mem(VMG_ ushort sc_count, ushort mod_prop_count, |
| 542 | int from_image); |
| 543 | |
| 544 | /* get a property from the intrinsic class */ |
| 545 | int get_prop_intrinsic(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 546 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 547 | uint *argc); |
| 548 | |
| 549 | /* |
| 550 | * Search for a property, continuing a previous search from the given |
| 551 | * point. defining_obj is the starting point for the search: we start |
| 552 | * searching in the target object's inheritance tree after |
| 553 | * defining_obj. This is used to continue an inheritance search from |
| 554 | * a given point, as needed for the 'inherited' operator, for example. |
| 555 | */ |
| 556 | static int search_for_prop_from(VMG_ uint prop, |
| 557 | vm_val_t *val, |
| 558 | vm_obj_id_t orig_target_obj, |
| 559 | vm_obj_id_t *source_obj, |
| 560 | vm_obj_id_t defining_obj); |
| 561 | |
| 562 | /* load the image file properties and superclasses */ |
| 563 | void load_image_props_and_scs(VMG_ const char *ptr, size_t siz); |
| 564 | |
| 565 | /* get/set the superclass count */ |
| 566 | ushort get_sc_count() const |
| 567 | { return get_hdr()->sc_cnt; } |
| 568 | void set_sc_count(ushort cnt) |
| 569 | { get_hdr()->sc_cnt = cnt; } |
| 570 | |
| 571 | /* change the superclass list */ |
| 572 | void change_superclass_list(VMG_ const vm_val_t *lst, int cnt); |
| 573 | |
| 574 | /* clear all undo flags */ |
| 575 | void clear_undo_flags(); |
| 576 | |
| 577 | |
| 578 | /* -------------------------------------------------------------------- */ |
| 579 | /* |
| 580 | * Low-level format management - these routines encapsulate the byte |
| 581 | * layout of the object in memory. This is a bit nasty because we |
| 582 | * keep the object's contents in the portable image format. |
| 583 | */ |
| 584 | |
| 585 | /* get my header */ |
| 586 | inline struct vm_tadsobj_hdr *get_hdr() const |
| 587 | { return (vm_tadsobj_hdr *)ext_; } |
| 588 | |
| 589 | /* property evaluator - undefined property */ |
| 590 | int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; } |
| 591 | |
| 592 | /* property evaluator - createInstance */ |
| 593 | int getp_create_instance(VMG_ vm_obj_id_t, vm_val_t *, uint *); |
| 594 | |
| 595 | /* property evaluator - createClone */ |
| 596 | int getp_create_clone(VMG_ vm_obj_id_t, vm_val_t *, uint *); |
| 597 | |
| 598 | /* property evaluator - createTransientInstance */ |
| 599 | int getp_create_trans_instance(VMG_ vm_obj_id_t, vm_val_t *retval, |
| 600 | uint *argc); |
| 601 | |
| 602 | /* property evaluator - createInstanceOf */ |
| 603 | int getp_create_instance_of(VMG_ vm_obj_id_t self, |
| 604 | vm_val_t *retval, uint *in_argc); |
| 605 | |
| 606 | /* property evaluator - createTransientInstanceOf */ |
| 607 | int getp_create_trans_instance_of(VMG_ vm_obj_id_t self, |
| 608 | vm_val_t *retval, uint *in_argc); |
| 609 | |
| 610 | /* common handler for createInstance and createTransientInstance */ |
| 611 | int getp_create_common(VMG_ vm_obj_id_t, vm_val_t *retval, uint *argc, |
| 612 | int is_transient); |
| 613 | |
| 614 | /* common handler for createInstanceOf and createTransientInstanceOf */ |
| 615 | int getp_create_multi_common(VMG_ vm_obj_id_t, vm_val_t *retval, |
| 616 | uint *argc, int is_transient); |
| 617 | |
| 618 | /* property evaluator - setSuperclassList */ |
| 619 | int getp_set_sc_list(VMG_ vm_obj_id_t self, |
| 620 | vm_val_t *retval, uint *in_argc); |
| 621 | |
| 622 | /* object table iteration callback for setSuperclassList */ |
| 623 | static void set_sc_cb(VMG_ vm_obj_id_t obj, void *ctx); |
| 624 | |
| 625 | /* property evaluator - getMethod */ |
| 626 | int getp_get_method(VMG_ vm_obj_id_t self, |
| 627 | vm_val_t *retval, uint *in_argc); |
| 628 | |
| 629 | /* property evaluator - setMethod */ |
| 630 | int getp_set_method(VMG_ vm_obj_id_t self, |
| 631 | vm_val_t *retval, uint *in_argc); |
| 632 | |
| 633 | /* property evaluation function table */ |
| 634 | static int (CVmObjTads::*func_table_[])(VMG_ vm_obj_id_t self, |
| 635 | vm_val_t *retval, uint *argc); |
| 636 | }; |
| 637 | |
| 638 | /* ------------------------------------------------------------------------ */ |
| 639 | /* |
| 640 | * Registration table object |
| 641 | */ |
| 642 | class CVmMetaclassTads: public CVmMetaclass |
| 643 | { |
| 644 | public: |
| 645 | /* get the global name */ |
| 646 | const char *get_meta_name() const { return "tads-object/030005"; } |
| 647 | |
| 648 | /* create from image file */ |
| 649 | void create_for_image_load(VMG_ vm_obj_id_t id) |
| 650 | { |
| 651 | new (vmg_ id) CVmObjTads(); |
| 652 | G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE); |
| 653 | } |
| 654 | |
| 655 | /* create from restoring from saved state */ |
| 656 | void create_for_restore(VMG_ vm_obj_id_t id) |
| 657 | { |
| 658 | new (vmg_ id) CVmObjTads(); |
| 659 | G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE); |
| 660 | } |
| 661 | |
| 662 | /* create dynamically using stack arguments */ |
| 663 | vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc) |
| 664 | { return CVmObjTads::create_from_stack(vmg_ pc_ptr, argc); } |
| 665 | |
| 666 | /* call a static property */ |
| 667 | int call_stat_prop(VMG_ vm_val_t *result, |
| 668 | const uchar **pc_ptr, uint *argc, |
| 669 | vm_prop_id_t prop) |
| 670 | { |
| 671 | return CVmObjTads::call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 672 | } |
| 673 | }; |
| 674 | |
| 675 | /* ------------------------------------------------------------------------ */ |
| 676 | /* |
| 677 | * Intrinsic class modifier object. This object is for use as a modifier |
| 678 | * object for an intrinsic class. |
| 679 | * |
| 680 | * This is a simple subclass of the regular TADS-Object class. The only |
| 681 | * difference is that we resolve properties a little differently: unlike |
| 682 | * regular TADS Objects, this class is essentially a mix-in, and has no |
| 683 | * intrinsic superclass at all. This means that the only place we look |
| 684 | * for a property in get_prop is in our property list; we specifically do |
| 685 | * not look for an intrinsic property, nor do we look for a superclass |
| 686 | * that provides an intrinsic property. |
| 687 | */ |
| 688 | class CVmObjIntClsMod: public CVmObjTads |
| 689 | { |
| 690 | friend class CVmMetaclassIntClsMod; |
| 691 | |
| 692 | public: |
| 693 | static class CVmMetaclass *metaclass_reg_; |
| 694 | class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; } |
| 695 | |
| 696 | /* am I of the given metaclass? */ |
| 697 | virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 698 | { |
| 699 | /* try my own metaclass and my base class */ |
| 700 | return (meta == metaclass_reg_ |
| 701 | || CVmObjTads::is_of_metaclass(meta)); |
| 702 | } |
| 703 | |
| 704 | /* is the given object an intrinsic class modifier object? */ |
| 705 | static int is_intcls_mod_obj(VMG_ vm_obj_id_t obj) |
| 706 | { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); } |
| 707 | |
| 708 | /* create dynamically using stack arguments */ |
| 709 | static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 710 | uint argc) |
| 711 | { |
| 712 | /* can't create instances of intrinsic class modifiers */ |
| 713 | err_throw(VMERR_ILLEGAL_NEW); |
| 714 | AFTER_ERR_THROW(return VM_INVALID_OBJ;) |
| 715 | } |
| 716 | |
| 717 | /* |
| 718 | * call a static property - we don't have any of our own, so simply |
| 719 | * "inherit" the base class handling |
| 720 | */ |
| 721 | static int call_stat_prop(VMG_ vm_val_t *result, |
| 722 | const uchar **pc_ptr, uint *argc, |
| 723 | vm_prop_id_t prop) |
| 724 | { |
| 725 | return CVmObjTads::call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 726 | } |
| 727 | |
| 728 | /* get a property */ |
| 729 | int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 730 | vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc); |
| 731 | |
| 732 | /* inherit a property */ |
| 733 | int inh_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 734 | vm_obj_id_t self, |
| 735 | vm_obj_id_t orig_target_obj, |
| 736 | vm_obj_id_t defining_obj, |
| 737 | vm_obj_id_t *source_obj, uint *argc); |
| 738 | |
| 739 | /* build my property list */ |
| 740 | void build_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval); |
| 741 | |
| 742 | /* create an object with no initial extension */ |
| 743 | CVmObjIntClsMod() { ext_ = 0; } |
| 744 | |
| 745 | /* |
| 746 | * Create an object with a given number of superclasses, and a given |
| 747 | * number of property slots. All property slots are initially |
| 748 | * allocated to the modifiable property list. |
| 749 | */ |
| 750 | CVmObjIntClsMod(VMG_ ushort superclass_count, ushort prop_count) |
| 751 | : CVmObjTads(vmg_ superclass_count, prop_count) { } |
| 752 | }; |
| 753 | |
| 754 | /* |
| 755 | * Registration table object |
| 756 | */ |
| 757 | class CVmMetaclassIntClsMod: public CVmMetaclass |
| 758 | { |
| 759 | public: |
| 760 | /* get the global name */ |
| 761 | const char *get_meta_name() const { return "int-class-mod/030000"; } |
| 762 | |
| 763 | /* create from image file */ |
| 764 | void create_for_image_load(VMG_ vm_obj_id_t id) |
| 765 | { |
| 766 | new (vmg_ id) CVmObjIntClsMod(); |
| 767 | G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE); |
| 768 | } |
| 769 | |
| 770 | /* create from restoring from saved state */ |
| 771 | void create_for_restore(VMG_ vm_obj_id_t id) |
| 772 | { |
| 773 | new (vmg_ id) CVmObjIntClsMod(); |
| 774 | G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE); |
| 775 | } |
| 776 | |
| 777 | /* create dynamically using stack arguments */ |
| 778 | vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc) |
| 779 | { return CVmObjIntClsMod::create_from_stack(vmg_ pc_ptr, argc); } |
| 780 | |
| 781 | /* call a static property */ |
| 782 | int call_stat_prop(VMG_ vm_val_t *result, |
| 783 | const uchar **pc_ptr, uint *argc, |
| 784 | vm_prop_id_t prop) |
| 785 | { |
| 786 | return CVmObjIntClsMod:: |
| 787 | call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 788 | } |
| 789 | }; |
| 790 | |
| 791 | /* ------------------------------------------------------------------------ */ |
| 792 | /* |
| 793 | * object ID + pointer structure |
| 794 | */ |
| 795 | struct tadsobj_objid_and_ptr |
| 796 | { |
| 797 | vm_obj_id_t id; |
| 798 | CVmObjTads *objp; |
| 799 | }; |
| 800 | |
| 801 | /* ------------------------------------------------------------------------ */ |
| 802 | /* |
| 803 | * Queue element for the inheritance path search queue |
| 804 | */ |
| 805 | struct pfq_ele |
| 806 | { |
| 807 | /* object ID of this element */ |
| 808 | vm_obj_id_t obj; |
| 809 | |
| 810 | /* pointer to the object */ |
| 811 | CVmObjTads *objp; |
| 812 | |
| 813 | /* next queue element */ |
| 814 | pfq_ele *nxt; |
| 815 | }; |
| 816 | |
| 817 | /* allocation page */ |
| 818 | struct pfq_page |
| 819 | { |
| 820 | /* next page in the list */ |
| 821 | pfq_page *nxt; |
| 822 | |
| 823 | /* the elements for this page */ |
| 824 | pfq_ele eles[50]; |
| 825 | }; |
| 826 | |
| 827 | /* ------------------------------------------------------------------------ */ |
| 828 | /* |
| 829 | * Queue for search_for_prop(). This implements a special-purpose work |
| 830 | * queue that we use to keep track of the objects yet to be processed in |
| 831 | * our depth-first search across the inheritance tree. |
| 832 | */ |
| 833 | class CVmObjTadsInhQueue |
| 834 | { |
| 835 | public: |
| 836 | CVmObjTadsInhQueue() |
| 837 | { |
| 838 | init(); |
| 839 | } |
| 840 | |
| 841 | void init() |
| 842 | { |
| 843 | /* there's nothing in the free list or the queue yet */ |
| 844 | head_ = 0; |
| 845 | free_ = 0; |
| 846 | |
| 847 | /* we have no elements yet */ |
| 848 | alloc_ = 0; |
| 849 | } |
| 850 | |
| 851 | ~CVmObjTadsInhQueue() |
| 852 | { |
| 853 | pfq_page *cur; |
| 854 | pfq_page *nxt; |
| 855 | |
| 856 | /* delete all of the allocated pages */ |
| 857 | for (cur = alloc_ ; cur != 0 ; cur = nxt) |
| 858 | { |
| 859 | /* remember the next page */ |
| 860 | nxt = cur->nxt; |
| 861 | |
| 862 | /* free this page */ |
| 863 | t3free(cur); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* get the head of the queue */ |
| 868 | pfq_ele *get_head() const { return head_; } |
| 869 | |
| 870 | /* remove the head of the queue and return the object ID */ |
| 871 | vm_obj_id_t remove_head() |
| 872 | { |
| 873 | /* if there's a head element, remove it */ |
| 874 | if (head_ != 0) |
| 875 | { |
| 876 | pfq_ele *ele; |
| 877 | |
| 878 | /* note the element */ |
| 879 | ele = head_; |
| 880 | |
| 881 | /* unlink it from the list */ |
| 882 | head_ = head_->nxt; |
| 883 | |
| 884 | /* link the element into the free list */ |
| 885 | ele->nxt = free_; |
| 886 | free_ = ele; |
| 887 | |
| 888 | /* return the object ID from the element we removed */ |
| 889 | return ele->obj; |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | /* there's nothing in the queue */ |
| 894 | return VM_INVALID_OBJ; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /* clear the queue */ |
| 899 | void clear() |
| 900 | { |
| 901 | /* move everything from the queue to the free list */ |
| 902 | while (head_ != 0) |
| 903 | { |
| 904 | pfq_ele *cur; |
| 905 | |
| 906 | /* unlink this element from the queue */ |
| 907 | cur = head_; |
| 908 | head_ = cur->nxt; |
| 909 | |
| 910 | /* link it into the free list */ |
| 911 | cur->nxt = free_; |
| 912 | free_ = cur; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* determine if the queue is empty */ |
| 917 | int is_empty() const |
| 918 | { |
| 919 | /* we're empty if there's no head element in the list */ |
| 920 | return (head_ == 0); |
| 921 | } |
| 922 | |
| 923 | /* allocate a path from the contents of the queue */ |
| 924 | tadsobj_objid_and_ptr *create_path() const |
| 925 | { |
| 926 | /* count the elements in the queue */ |
| 927 | int cnt; |
| 928 | pfq_ele *cur; |
| 929 | for (cnt = 0, cur = head_ ; cur != 0 ; cur = cur->nxt) |
| 930 | { |
| 931 | /* only non-nil elements count */ |
| 932 | if (cur->obj != VM_INVALID_OBJ) |
| 933 | ++cnt; |
| 934 | } |
| 935 | |
| 936 | /* allocate the path */ |
| 937 | tadsobj_objid_and_ptr *path = (tadsobj_objid_and_ptr *)t3malloc( |
| 938 | (cnt + 1) * sizeof(tadsobj_objid_and_ptr)); |
| 939 | |
| 940 | /* initialize the path */ |
| 941 | tadsobj_objid_and_ptr *dst; |
| 942 | for (dst = path, cur = head_ ; cur != 0 ; cur = cur->nxt) |
| 943 | { |
| 944 | /* only store non-nil elements */ |
| 945 | if (cur->obj != VM_INVALID_OBJ) |
| 946 | { |
| 947 | dst->id = cur->obj; |
| 948 | dst->objp = cur->objp; |
| 949 | ++dst; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | /* set the null last entry */ |
| 954 | dst->id = VM_INVALID_OBJ; |
| 955 | dst->objp = 0; |
| 956 | |
| 957 | /* return the new path */ |
| 958 | return path; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Insert an object into the queue. We'll insert after the given |
| 963 | * element (null indicates that we insert at the head of the queue). |
| 964 | * Returns a pointer to the newly-inserted element. |
| 965 | */ |
| 966 | pfq_ele *insert_obj(VMG_ vm_obj_id_t obj, CVmObjTads *objp, |
| 967 | pfq_ele *ins_pt) |
| 968 | { |
| 969 | pfq_ele *ele; |
| 970 | |
| 971 | /* |
| 972 | * If the exact same element is already in the queue, delete the |
| 973 | * old copy. This will happen in situations where we have multiple |
| 974 | * superclasses that all inherit from a common base class: we want |
| 975 | * the common base class to come in inheritance order after the |
| 976 | * last superclass that inherits from the common base. By deleting |
| 977 | * previous queue entries that match new queue entries, we ensure |
| 978 | * that the common class will move to follow (in inheritance order) |
| 979 | * the last class that derives from it. |
| 980 | */ |
| 981 | for (ele = head_ ; ele != 0 ; ele = ele->nxt) |
| 982 | { |
| 983 | /* if this is the same thing we're inserting, remove it */ |
| 984 | if (ele->obj == obj) |
| 985 | { |
| 986 | /* |
| 987 | * clear the element (don't unlink it, as this could cause |
| 988 | * confusion for the caller, who's tracking an insertion |
| 989 | * point and traversal point) |
| 990 | */ |
| 991 | ele->obj = VM_INVALID_OBJ; |
| 992 | ele->objp = 0; |
| 993 | |
| 994 | /* |
| 995 | * no need to look any further - we know we can never have |
| 996 | * the same element appear twice in the queue, thanks to |
| 997 | * this very code |
| 998 | */ |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /* allocate our new element */ |
| 1004 | ele = alloc_ele(); |
| 1005 | ele->obj = obj; |
| 1006 | ele->objp = objp; |
| 1007 | |
| 1008 | /* insert it at the insertion point */ |
| 1009 | if (ins_pt == 0) |
| 1010 | { |
| 1011 | /* insert at the head */ |
| 1012 | ele->nxt = head_; |
| 1013 | head_ = ele; |
| 1014 | } |
| 1015 | else |
| 1016 | { |
| 1017 | /* insert after the selected item */ |
| 1018 | ele->nxt = ins_pt->nxt; |
| 1019 | ins_pt->nxt = ele; |
| 1020 | } |
| 1021 | |
| 1022 | /* return the new element */ |
| 1023 | return ele; |
| 1024 | } |
| 1025 | |
| 1026 | protected: |
| 1027 | /* allocate a new element */ |
| 1028 | pfq_ele *alloc_ele() |
| 1029 | { |
| 1030 | pfq_ele *ele; |
| 1031 | |
| 1032 | /* if we have nothing in the free list, allocate more elements */ |
| 1033 | if (free_ == 0) |
| 1034 | { |
| 1035 | pfq_page *pg; |
| 1036 | size_t i; |
| 1037 | |
| 1038 | /* allocate another page */ |
| 1039 | pg = (pfq_page *)t3malloc(sizeof(pfq_page)); |
| 1040 | |
| 1041 | /* link it into our master page list */ |
| 1042 | pg->nxt = alloc_; |
| 1043 | alloc_ = pg; |
| 1044 | |
| 1045 | /* link all of its elements into the free list */ |
| 1046 | for (ele = pg->eles, i = sizeof(pg->eles)/sizeof(pg->eles[0]) ; |
| 1047 | i != 0 ; --i, ++ele) |
| 1048 | { |
| 1049 | /* link this one into the free list */ |
| 1050 | ele->nxt = free_; |
| 1051 | free_ = ele; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | /* take the next element off the free list */ |
| 1056 | ele = free_; |
| 1057 | free_ = free_->nxt; |
| 1058 | |
| 1059 | /* return the element */ |
| 1060 | return ele; |
| 1061 | } |
| 1062 | |
| 1063 | /* head of the active queue */ |
| 1064 | pfq_ele *head_; |
| 1065 | |
| 1066 | /* head of the free element list */ |
| 1067 | pfq_ele *free_; |
| 1068 | |
| 1069 | /* |
| 1070 | * Linked list of element pages. We allocate memory for elements in |
| 1071 | * blocks, to reduce allocation overhead. |
| 1072 | */ |
| 1073 | pfq_page *alloc_; |
| 1074 | }; |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | #endif /* VMTOBJ_H */ |
| 1080 | |
| 1081 | /* |
| 1082 | * Register the classes |
| 1083 | */ |
| 1084 | VM_REGISTER_METACLASS(CVmObjTads) |
| 1085 | VM_REGISTER_METACLASS(CVmObjIntClsMod) |