| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/VMMETA.CPP,v 1.3 1999/07/11 00:46:58 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 | vmmeta.cpp - metaclass dependency table |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 12/01/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "t3std.h" |
| 27 | #include "vmtype.h" |
| 28 | #include "vmerr.h" |
| 29 | #include "vmerrnum.h" |
| 30 | #include "vmglob.h" |
| 31 | #include "vmmeta.h" |
| 32 | #include "vmobj.h" |
| 33 | #include "vmmcreg.h" |
| 34 | #include "vmfile.h" |
| 35 | #include "vmintcls.h" |
| 36 | |
| 37 | |
| 38 | /* ------------------------------------------------------------------------ */ |
| 39 | /* |
| 40 | * Create the metaclass dependency table with a given number of initial |
| 41 | * entries |
| 42 | */ |
| 43 | CVmMetaTable::CVmMetaTable(size_t init_entries) |
| 44 | { |
| 45 | vm_meta_reg_t *entry; |
| 46 | uint cnt; |
| 47 | uint i; |
| 48 | |
| 49 | /* allocate space for our entries */ |
| 50 | if (init_entries != 0) |
| 51 | { |
| 52 | /* allocate the space */ |
| 53 | table_ = (vm_meta_entry_t *) |
| 54 | t3malloc(init_entries * sizeof(table_[0])); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | /* we have no entries */ |
| 59 | table_ = 0; |
| 60 | } |
| 61 | |
| 62 | /* no entries are defined yet */ |
| 63 | count_ = 0; |
| 64 | |
| 65 | /* remember the allocation size */ |
| 66 | alloc_ = init_entries; |
| 67 | |
| 68 | /* count the number of registered metaclasses */ |
| 69 | for (entry = G_meta_reg_table, cnt = 0 ; entry->meta != 0 ; |
| 70 | ++entry, ++cnt) ; |
| 71 | |
| 72 | /* |
| 73 | * allocate the reverse dependency table -- this table lets us get |
| 74 | * the dependency index of a metaclass given its registration table |
| 75 | * index |
| 76 | */ |
| 77 | reverse_map_ = (int *)t3malloc(cnt * sizeof(reverse_map_[0])); |
| 78 | |
| 79 | /* |
| 80 | * set each element of the table to -1, since we have no dependency |
| 81 | * table entries yet |
| 82 | */ |
| 83 | for (i = 0 ; i < cnt ; ++i) |
| 84 | reverse_map_[i] = -1; |
| 85 | } |
| 86 | |
| 87 | /* ------------------------------------------------------------------------ */ |
| 88 | /* |
| 89 | * Delete the table |
| 90 | */ |
| 91 | CVmMetaTable::~CVmMetaTable() |
| 92 | { |
| 93 | /* clear the table */ |
| 94 | clear(); |
| 95 | |
| 96 | /* free the table, if we ever allocated one */ |
| 97 | if (table_ != 0) |
| 98 | t3free(table_); |
| 99 | |
| 100 | /* free the reverse map */ |
| 101 | t3free(reverse_map_); |
| 102 | } |
| 103 | |
| 104 | /* ------------------------------------------------------------------------ */ |
| 105 | /* |
| 106 | * Clear all entries from the table |
| 107 | */ |
| 108 | void CVmMetaTable::clear() |
| 109 | { |
| 110 | size_t i; |
| 111 | |
| 112 | /* delete all of the property tables in the entries */ |
| 113 | for (i = 0 ; i < count_ ; ++i) |
| 114 | table_[i].release_mem(); |
| 115 | |
| 116 | /* |
| 117 | * Reset the entry counter. Note that this doesn't affect any |
| 118 | * allocation; we keep a separate count of the number of table slots |
| 119 | * we have allocated. Table slots don't have any additional |
| 120 | * associated memory, so we don't need to worry about cleaning |
| 121 | * anything up at this point. |
| 122 | */ |
| 123 | count_ = 0; |
| 124 | } |
| 125 | |
| 126 | /* ------------------------------------------------------------------------ */ |
| 127 | /* |
| 128 | * Ensure that we have space for a given number of entries |
| 129 | */ |
| 130 | void CVmMetaTable::ensure_space(size_t entries, size_t increment) |
| 131 | { |
| 132 | /* if we don't have enough space, allocate more */ |
| 133 | if (entries >= alloc_) |
| 134 | { |
| 135 | size_t new_size; |
| 136 | |
| 137 | /* increase the allocation size by the given increment */ |
| 138 | alloc_ += increment; |
| 139 | |
| 140 | /* if it's still too small, bump it up to the required size */ |
| 141 | if (alloc_ < entries) |
| 142 | alloc_ = entries; |
| 143 | |
| 144 | /* compute the new size */ |
| 145 | new_size = alloc_ * sizeof(table_[0]); |
| 146 | |
| 147 | /* |
| 148 | * if we have a table already, reallocate it at the larger size; |
| 149 | * otherwise, allocate a new table |
| 150 | */ |
| 151 | if (table_ != 0) |
| 152 | table_ = (vm_meta_entry_t *)t3realloc(table_, new_size); |
| 153 | else |
| 154 | table_ = (vm_meta_entry_t *)t3malloc(new_size); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* ------------------------------------------------------------------------ */ |
| 159 | /* |
| 160 | * Add an entry to the table |
| 161 | */ |
| 162 | void CVmMetaTable::add_entry(const char *metaclass_id, size_t func_cnt, |
| 163 | vm_prop_id_t min_prop, vm_prop_id_t max_prop) |
| 164 | { |
| 165 | vm_meta_reg_t *entry; |
| 166 | uint idx; |
| 167 | const char *vsn; |
| 168 | size_t name_len; |
| 169 | |
| 170 | /* find the version suffix in the metaclass name, if any */ |
| 171 | vsn = lib_find_vsn_suffix(metaclass_id, '/', "000000", &name_len); |
| 172 | |
| 173 | /* ensure we have space for one more entry */ |
| 174 | ensure_space(count_ + 1, 5); |
| 175 | |
| 176 | /* look up the metaclass by name */ |
| 177 | for (idx = 0, entry = G_meta_reg_table ; entry->meta != 0 ; |
| 178 | ++entry, ++idx) |
| 179 | { |
| 180 | const char *entry_vsn; |
| 181 | size_t entry_name_len; |
| 182 | |
| 183 | /* find the version number in this entry */ |
| 184 | entry_vsn = lib_find_vsn_suffix((*entry->meta)->get_meta_name(), |
| 185 | '/', "000000", &entry_name_len); |
| 186 | |
| 187 | /* see if this is a match */ |
| 188 | if (name_len == entry_name_len |
| 189 | && memcmp(metaclass_id, (*entry->meta)->get_meta_name(), |
| 190 | name_len) == 0) |
| 191 | { |
| 192 | /* |
| 193 | * make sure the version provided in the VM is at least as |
| 194 | * high as the requested version |
| 195 | */ |
| 196 | if (strcmp(vsn, entry_vsn) > 0) |
| 197 | { |
| 198 | /* |
| 199 | * throw the error; this is a version mismatch error, so |
| 200 | * flag it as a version error and include the metaclass |
| 201 | * dependency information |
| 202 | */ |
| 203 | err_throw_a(VMERR_METACLASS_TOO_OLD, 4, |
| 204 | ERR_TYPE_TEXTCHAR, metaclass_id, |
| 205 | ERR_TYPE_TEXTCHAR, entry_vsn, |
| 206 | ERR_TYPE_METACLASS, metaclass_id, |
| 207 | ERR_TYPE_VERSION_FLAG); |
| 208 | } |
| 209 | |
| 210 | /* add this entry */ |
| 211 | add_entry(metaclass_id, idx, func_cnt, min_prop, max_prop); |
| 212 | |
| 213 | /* we're done */ |
| 214 | return; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * We didn't find it. This is probably an out-of-date VM issue, so |
| 220 | * flag it as a version error, and include the metaclass dependency |
| 221 | * information. |
| 222 | */ |
| 223 | err_throw_a(VMERR_UNKNOWN_METACLASS, 3, |
| 224 | ERR_TYPE_TEXTCHAR, metaclass_id, |
| 225 | ERR_TYPE_METACLASS, metaclass_id, |
| 226 | ERR_TYPE_VERSION_FLAG); |
| 227 | } |
| 228 | |
| 229 | /* ------------------------------------------------------------------------ */ |
| 230 | /* |
| 231 | * Add an entry to the table for a given registration table index |
| 232 | */ |
| 233 | void CVmMetaTable::add_entry(const char *metaclass_id, |
| 234 | uint idx, size_t func_cnt, |
| 235 | vm_prop_id_t min_prop, vm_prop_id_t max_prop) |
| 236 | { |
| 237 | /* get the registration table entry from the index */ |
| 238 | vm_meta_reg_t *entry = &G_meta_reg_table[idx]; |
| 239 | |
| 240 | /* remember the defining class object */ |
| 241 | table_[count_].meta_ = *entry->meta; |
| 242 | |
| 243 | /* save the name */ |
| 244 | table_[count_].set_meta_name(metaclass_id); |
| 245 | |
| 246 | /* calculate the number of entries in the table */ |
| 247 | size_t prop_xlat_cnt; |
| 248 | if (min_prop == VM_INVALID_PROP || max_prop == VM_INVALID_PROP) |
| 249 | prop_xlat_cnt = 0; |
| 250 | else |
| 251 | prop_xlat_cnt = max_prop - min_prop + 1; |
| 252 | |
| 253 | /* set the property count */ |
| 254 | table_[count_].min_prop_ = min_prop; |
| 255 | table_[count_].prop_xlat_cnt_ = prop_xlat_cnt; |
| 256 | |
| 257 | /* set the function count */ |
| 258 | table_[count_].func_xlat_cnt_ = func_cnt; |
| 259 | |
| 260 | /* we don't know the intrinsic class's representative object yet */ |
| 261 | table_[count_].class_obj_ = VM_INVALID_OBJ; |
| 262 | |
| 263 | /* allocate space for the property table, if we have any translations */ |
| 264 | if (prop_xlat_cnt != 0) |
| 265 | { |
| 266 | size_t siz; |
| 267 | |
| 268 | /* calculate the table size */ |
| 269 | siz = prop_xlat_cnt * sizeof(table_[count_].prop_xlat_[0]); |
| 270 | |
| 271 | /* allocate the table */ |
| 272 | table_[count_].prop_xlat_ = (unsigned short *)t3malloc(siz); |
| 273 | |
| 274 | /* |
| 275 | * initialize each entry in the table to zero - this will ensure |
| 276 | * that each entry that is never set to a valid function index |
| 277 | * will properly yield function index zero, which is defined as |
| 278 | * the "not found" invalid function index |
| 279 | */ |
| 280 | memset(table_[count_].prop_xlat_, 0, siz); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | /* there are no properties to translate, so we don't need a table */ |
| 285 | table_[count_].prop_xlat_ = 0; |
| 286 | } |
| 287 | |
| 288 | /* allocate the function-to-property translation table */ |
| 289 | if (func_cnt != 0) |
| 290 | { |
| 291 | size_t i; |
| 292 | vm_prop_id_t *prop_ptr; |
| 293 | |
| 294 | /* allocate the table */ |
| 295 | table_[count_].func_xlat_ = |
| 296 | (vm_prop_id_t *)t3malloc(func_cnt * sizeof(vm_prop_id_t)); |
| 297 | |
| 298 | /* clear the table - mark each entry as an invalid property ID */ |
| 299 | for (i = 0, prop_ptr = table_[count_].func_xlat_ ; |
| 300 | i < func_cnt ; ++i, ++prop_ptr) |
| 301 | *prop_ptr = VM_INVALID_PROP; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | /* no properties, so we don't need a table */ |
| 306 | table_[count_].func_xlat_ = 0; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * store the reverse mapping for this entry, so that we can find the |
| 311 | * dependency entry given the registration index |
| 312 | */ |
| 313 | reverse_map_[idx] = count_; |
| 314 | |
| 315 | /* count the new entry */ |
| 316 | ++count_; |
| 317 | } |
| 318 | |
| 319 | /* ------------------------------------------------------------------------ */ |
| 320 | /* |
| 321 | * Add an entry to the dependency table only if it doesn't already |
| 322 | * appear in the dependency table. We add the entry based on its |
| 323 | * registration table index. |
| 324 | */ |
| 325 | void CVmMetaTable::add_entry_if_new(uint reg_table_idx, size_t func_cnt, |
| 326 | vm_prop_id_t min_prop, |
| 327 | vm_prop_id_t max_prop) |
| 328 | { |
| 329 | /* |
| 330 | * if the entry already has a valid dependency table index, as |
| 331 | * obtained from the reverse mapping, we need not add it again |
| 332 | */ |
| 333 | if (reverse_map_[reg_table_idx] != -1) |
| 334 | return; |
| 335 | |
| 336 | /* add the entry */ |
| 337 | add_entry((*G_meta_reg_table[reg_table_idx].meta)->get_meta_name(), |
| 338 | reg_table_idx, func_cnt, min_prop, max_prop); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /* ------------------------------------------------------------------------ */ |
| 343 | /* |
| 344 | * Look up an entry by external ID |
| 345 | */ |
| 346 | vm_meta_entry_t *CVmMetaTable::get_entry_by_id(const char *id) const |
| 347 | { |
| 348 | /* find the version suffix in the metaclass name, if any */ |
| 349 | size_t name_len; |
| 350 | const char *vsn = lib_find_vsn_suffix(id, '/', "000000", &name_len); |
| 351 | |
| 352 | /* look up the metaclass by name */ |
| 353 | size_t i; |
| 354 | vm_meta_entry_t *entry; |
| 355 | for (i = 0, entry = table_ ; i < count_ ; ++entry, ++i) |
| 356 | { |
| 357 | /* find the version number in this entry */ |
| 358 | size_t entry_name_len; |
| 359 | const char *entry_vsn = lib_find_vsn_suffix( |
| 360 | entry->image_meta_name_, '/', "000000", &entry_name_len); |
| 361 | |
| 362 | /* see if this is a match */ |
| 363 | if (name_len == entry_name_len |
| 364 | && memcmp(id, entry->image_meta_name_, name_len) == 0) |
| 365 | { |
| 366 | /* |
| 367 | * we found a match to the name; make sure the version is at |
| 368 | * least as high as requested |
| 369 | */ |
| 370 | if (strcmp(vsn, entry_vsn) <= 0) |
| 371 | { |
| 372 | /* the loaded version is at least as new, so return it */ |
| 373 | return entry; |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | /* it's too old, so it's effectively not available */ |
| 378 | return 0; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /* didn't find a match */ |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | /* ------------------------------------------------------------------------ */ |
| 388 | /* |
| 389 | * Invoke the VM-stack-based constructor for the metaclass at the given |
| 390 | * index |
| 391 | */ |
| 392 | vm_obj_id_t CVmMetaTable::create_from_stack(VMG_ const uchar **pc_ptr, |
| 393 | uint idx, uint argc) |
| 394 | { |
| 395 | /* make sure the entry is defined */ |
| 396 | if (idx >= count_) |
| 397 | err_throw(VMERR_BAD_METACLASS_INDEX); |
| 398 | |
| 399 | /* invoke the appropriate constructor */ |
| 400 | return table_[idx].meta_->create_from_stack(vmg_ pc_ptr, argc); |
| 401 | } |
| 402 | |
| 403 | /* ------------------------------------------------------------------------ */ |
| 404 | /* |
| 405 | * Call a static property in the metaclass at the given index |
| 406 | */ |
| 407 | int CVmMetaTable::call_static_prop(VMG_ vm_val_t *result, |
| 408 | const uchar **pc_ptr, uint idx, |
| 409 | uint *argc, vm_prop_id_t prop) |
| 410 | { |
| 411 | /* make sure the entry is defined */ |
| 412 | if (idx >= count_) |
| 413 | err_throw(VMERR_BAD_METACLASS_INDEX); |
| 414 | |
| 415 | /* invoke the appropriate static property evaluator */ |
| 416 | return table_[idx].meta_->call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 417 | } |
| 418 | |
| 419 | /* ------------------------------------------------------------------------ */ |
| 420 | /* |
| 421 | * Create an object with the given ID and load the object from the image |
| 422 | * file. |
| 423 | */ |
| 424 | void CVmMetaTable::create_from_image(VMG_ uint idx, vm_obj_id_t id, |
| 425 | const char *ptr, size_t siz) |
| 426 | { |
| 427 | /* make sure the entry is defined */ |
| 428 | if (idx >= count_) |
| 429 | err_throw(VMERR_BAD_METACLASS_INDEX); |
| 430 | |
| 431 | /* create the object table entry in the memory manager */ |
| 432 | G_obj_table->alloc_obj_with_id(id, TRUE); |
| 433 | |
| 434 | /* invoke the appropriate constructor */ |
| 435 | table_[idx].meta_->create_for_image_load(vmg_ id); |
| 436 | |
| 437 | /* load the object */ |
| 438 | vm_objp(vmg_ id)->load_from_image(vmg_ id, ptr, siz); |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Create an object of the given metaclass with the given ID, in |
| 443 | * preparation for restoring the object's data from saved state |
| 444 | * information. This doesn't fill in the object with the saved state |
| 445 | * data, but merely creates the object. |
| 446 | * |
| 447 | * The caller is responsible for having allocated the object ID before |
| 448 | * calling this function. |
| 449 | */ |
| 450 | void CVmMetaTable::create_for_restore(VMG_ uint idx, vm_obj_id_t id) |
| 451 | { |
| 452 | /* make sure the entry is defined in our table of metaclasses */ |
| 453 | if (idx >= count_) |
| 454 | err_throw(VMERR_BAD_METACLASS_INDEX); |
| 455 | |
| 456 | /* |
| 457 | * Invoke the appropriate constructor. Note that the caller must |
| 458 | * already have allocated the object ID, so we simply use the given ID |
| 459 | * without further consideration. |
| 460 | */ |
| 461 | table_[idx].meta_->create_for_restore(vmg_ id); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /* ------------------------------------------------------------------------ */ |
| 466 | /* |
| 467 | * Write the table to a file. |
| 468 | */ |
| 469 | void CVmMetaTable::write_to_file(CVmFile *fp) |
| 470 | { |
| 471 | size_t i; |
| 472 | |
| 473 | /* write the number of entries */ |
| 474 | fp->write_uint2(get_count()); |
| 475 | |
| 476 | /* write each entry */ |
| 477 | for (i = 0 ; i < get_count() ; ++i) |
| 478 | { |
| 479 | const char *nm; |
| 480 | const vm_meta_entry_t *entry; |
| 481 | ushort j; |
| 482 | |
| 483 | /* get the entry */ |
| 484 | entry = get_entry(i); |
| 485 | |
| 486 | /* get this entry's name */ |
| 487 | nm = entry->image_meta_name_; |
| 488 | |
| 489 | /* write the length of the name, followed by the name */ |
| 490 | fp->write_uint2(strlen(nm)); |
| 491 | fp->write_bytes(nm, strlen(nm)); |
| 492 | |
| 493 | /* write our associated IntrinsicClass object's ID */ |
| 494 | fp->write_uint4(entry->class_obj_); |
| 495 | |
| 496 | /* |
| 497 | * Write the property table information - write the number of |
| 498 | * function entries, and the minimum and maximum property ID's. |
| 499 | */ |
| 500 | fp->write_uint2(entry->func_xlat_cnt_); |
| 501 | fp->write_uint2(entry->min_prop_); |
| 502 | fp->write_uint2(entry->min_prop_ + entry->prop_xlat_cnt_); |
| 503 | |
| 504 | /* |
| 505 | * Write out the property translation table. The function |
| 506 | * translation table will always be smaller than (at worst, it |
| 507 | * will be the same size as) the property translation table; |
| 508 | * both tables contain the same information, so since we only |
| 509 | * need to write out one or the other, write out the smaller of |
| 510 | * the two. |
| 511 | * |
| 512 | * Note that xlat_func() requires a 1-based function index, so |
| 513 | * run our counter from 1 to the function table count. |
| 514 | */ |
| 515 | for (j = 1 ; j <= entry->func_xlat_cnt_ ; ++j) |
| 516 | fp->write_uint2(entry->xlat_func(j)); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Read the table from a file. |
| 522 | */ |
| 523 | int CVmMetaTable::read_from_file(CVmFile *fp) |
| 524 | { |
| 525 | size_t cnt; |
| 526 | size_t i; |
| 527 | |
| 528 | /* clear the existing table */ |
| 529 | clear(); |
| 530 | |
| 531 | /* read the number of entries */ |
| 532 | cnt = fp->read_uint2(); |
| 533 | |
| 534 | /* read the entries */ |
| 535 | for (i = 0 ; i < cnt ; ++i) |
| 536 | { |
| 537 | char buf[256]; |
| 538 | size_t len; |
| 539 | vm_prop_id_t min_prop; |
| 540 | vm_prop_id_t max_prop; |
| 541 | ushort func_cnt; |
| 542 | ushort j; |
| 543 | vm_meta_entry_t *entry; |
| 544 | vm_obj_id_t class_obj; |
| 545 | |
| 546 | /* read the length of this entry, and make sure it's valid */ |
| 547 | len = fp->read_uint2(); |
| 548 | if (len > sizeof(buf) - 1) |
| 549 | return VMERR_SAVED_META_TOO_LONG; |
| 550 | |
| 551 | /* read the name and null-terminate it */ |
| 552 | fp->read_bytes(buf, len); |
| 553 | buf[len] = '\0'; |
| 554 | |
| 555 | /* read the associated IntrinsicClass object */ |
| 556 | class_obj = (vm_obj_id_t)fp->read_uint4(); |
| 557 | |
| 558 | /* read the property table description */ |
| 559 | func_cnt = (ushort)fp->read_uint2(); |
| 560 | min_prop = (vm_prop_id_t)fp->read_uint2(); |
| 561 | max_prop = (vm_prop_id_t)fp->read_uint2(); |
| 562 | |
| 563 | /* add this entry */ |
| 564 | add_entry(buf, func_cnt, min_prop, max_prop); |
| 565 | |
| 566 | /* get the new entry's record */ |
| 567 | entry = get_entry(i); |
| 568 | |
| 569 | /* set the class ID */ |
| 570 | entry->class_obj_ = class_obj; |
| 571 | |
| 572 | /* |
| 573 | * Read the property mappings. We stored the function table |
| 574 | * entries, so we simply need to load those entries. Note that |
| 575 | * the function table has a 1-based index, so run our counter |
| 576 | * from 1 to the function count. |
| 577 | */ |
| 578 | for (j = 1 ; j <= func_cnt ; ++j) |
| 579 | entry->add_prop_xlat((short)fp->read_int2(), j); |
| 580 | } |
| 581 | |
| 582 | /* success */ |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | #if 0 // moved inline, since it's small and used a lot |
| 587 | /* |
| 588 | * Get the function vector index for a property given the metaclass's |
| 589 | * registration table index. |
| 590 | */ |
| 591 | int CVmMetaTable::prop_to_vector_idx(uint reg_table_idx, vm_prop_id_t prop) |
| 592 | { |
| 593 | /* get the entry for the registration table index */ |
| 594 | vm_meta_entry_t *entry = get_entry_from_reg(reg_table_idx); |
| 595 | |
| 596 | /* |
| 597 | * if there's no entry for this registration index, there's |
| 598 | * obviously no metaclass function mapping for the property |
| 599 | */ |
| 600 | if (entry == 0) |
| 601 | return 0; |
| 602 | |
| 603 | /* return the property translation */ |
| 604 | return entry->xlat_prop(prop); |
| 605 | } |
| 606 | #endif |
| 607 | |
| 608 | /* |
| 609 | * Create an IntrinsicClass object for each metaclass that doesn't |
| 610 | * already have one. |
| 611 | */ |
| 612 | void CVmMetaTable::create_intrinsic_class_instances(VMG0_) |
| 613 | { |
| 614 | size_t idx; |
| 615 | vm_meta_entry_t *entry; |
| 616 | |
| 617 | /* go through our table */ |
| 618 | for (idx = 0, entry = table_ ; idx < count_ ; ++idx, ++entry) |
| 619 | { |
| 620 | /* if this entry has no associated class object, create one for it */ |
| 621 | if (entry->class_obj_ == VM_INVALID_OBJ) |
| 622 | { |
| 623 | /* |
| 624 | * create the class object - it will automatically register |
| 625 | * itself to fill in the entry |
| 626 | */ |
| 627 | CVmObjClass::create_dyn(vmg_ idx); |
| 628 | } |
| 629 | |
| 630 | /* |
| 631 | * Add this object to the machine globals, if it's not a root |
| 632 | * object. Any dynamically-created intrinsic class instance must |
| 633 | * be made a machine global because it will always be reachable as |
| 634 | * the class object for the metaclass, but the metclass won't trace |
| 635 | * the object during garbage collection, so we have to set up a |
| 636 | * global to make sure the gc knows it's always reachable. |
| 637 | */ |
| 638 | G_obj_table->add_to_globals(entry->class_obj_); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Forget the IntrinsicClass objects we created dynamically in |
| 644 | * create_intrinsic_class_instances(). This simply drops our references |
| 645 | * to those objects from the metaclass table. |
| 646 | */ |
| 647 | void CVmMetaTable::forget_intrinsic_class_instances(VMG0_) |
| 648 | { |
| 649 | size_t idx; |
| 650 | vm_meta_entry_t *entry; |
| 651 | |
| 652 | /* go through our table and do class-level initializations */ |
| 653 | for (idx = 0, entry = table_ ; idx < count_ ; ++idx, ++entry) |
| 654 | entry->class_obj_ = VM_INVALID_OBJ; |
| 655 | } |
| 656 | |
| 657 | /* ------------------------------------------------------------------------ */ |
| 658 | /* |
| 659 | * Build a translation table from run-time metaclass index to compiler |
| 660 | * TC_META_xxx ID. |
| 661 | */ |
| 662 | tc_metaclass_t *CVmMetaTable::build_runtime_to_compiler_id_table(VMG0_) |
| 663 | { |
| 664 | vm_meta_reg_t *entry; |
| 665 | int i, cnt; |
| 666 | |
| 667 | /* count the metaclass registration table size */ |
| 668 | for (entry = G_meta_reg_table, cnt = 0 ; entry->meta != 0 ; |
| 669 | ++entry, ++cnt) ; |
| 670 | |
| 671 | /* |
| 672 | * allocate the translation table - this is simply an array indexed by |
| 673 | * metaclass registration index and yielding the corresponding compiler |
| 674 | * metaclass ID |
| 675 | */ |
| 676 | tc_metaclass_t *xlat = (tc_metaclass_t *)t3malloc(cnt * sizeof(xlat[0])); |
| 677 | |
| 678 | /* scan the registration table for compiler-recognized classes */ |
| 679 | for (entry = G_meta_reg_table, i = 0 ; entry->meta != 0 ; ++entry, ++i) |
| 680 | { |
| 681 | /* get its name */ |
| 682 | const char *nm = (*entry->meta)->get_meta_name(); |
| 683 | |
| 684 | /* scan for the version number suffix - we'll ignore it if found */ |
| 685 | const char *p; |
| 686 | for (p = nm ; *p != '\0' && *p != '/' ; ++p) ; |
| 687 | |
| 688 | /* note the length up to the version suffix delimiter */ |
| 689 | size_t len = p - nm; |
| 690 | |
| 691 | /* check for the known names */ |
| 692 | if (len == 11 && memcmp(nm, "tads-object", 11) == 0) |
| 693 | xlat[i] = TC_META_TADSOBJ; |
| 694 | else if (len == 11 && memcmp(nm, "dictionary2", 11) == 0) |
| 695 | xlat[i] = TC_META_DICT; |
| 696 | else if (len == 18 && memcmp(nm, "grammar-production", 18) == 0) |
| 697 | xlat[i] = TC_META_GRAMPROD; |
| 698 | else if (len == 13 && memcmp(nm, "int-class-mod", 13) == 0) |
| 699 | xlat[i] = TC_META_ICMOD; |
| 700 | else |
| 701 | xlat[i] = TC_META_UNKNOWN; |
| 702 | } |
| 703 | |
| 704 | /* return the translation table */ |
| 705 | return xlat; |
| 706 | } |
| 707 |