| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2000, 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 | vmdict.cpp - dictionary metaclass |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 01/24/00 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "t3std.h" |
| 27 | #include "vmdict.h" |
| 28 | #include "vmerr.h" |
| 29 | #include "vmerrnum.h" |
| 30 | #include "vmtype.h" |
| 31 | #include "vmglob.h" |
| 32 | #include "vmobj.h" |
| 33 | #include "vmundo.h" |
| 34 | #include "vmmcreg.h" |
| 35 | #include "vmfile.h" |
| 36 | #include "vmhash.h" |
| 37 | #include "vmbif.h" |
| 38 | #include "vmmeta.h" |
| 39 | #include "vmstack.h" |
| 40 | #include "vmstr.h" |
| 41 | #include "vmlst.h" |
| 42 | #include "vmrun.h" |
| 43 | #include "vmstrcmp.h" |
| 44 | #include "vmpredef.h" |
| 45 | |
| 46 | |
| 47 | /* ------------------------------------------------------------------------ */ |
| 48 | /* |
| 49 | * Dictionary undo record. Each time we change the dictionary, we track |
| 50 | * the change with one of these records - we attach this record to the |
| 51 | * standard undo record via the 'ptrval' field. |
| 52 | */ |
| 53 | struct dict_undo_rec |
| 54 | { |
| 55 | /* action type */ |
| 56 | enum dict_undo_action action; |
| 57 | |
| 58 | /* object value stored in dictionary record */ |
| 59 | vm_obj_id_t obj; |
| 60 | |
| 61 | /* vocabulary property for word association */ |
| 62 | vm_prop_id_t prop; |
| 63 | |
| 64 | /* length of the word */ |
| 65 | size_t len; |
| 66 | |
| 67 | /* text of the word */ |
| 68 | char txt[1]; |
| 69 | }; |
| 70 | |
| 71 | /* ------------------------------------------------------------------------ */ |
| 72 | /* |
| 73 | * Generic comparator-based hash |
| 74 | */ |
| 75 | class CVmHashFuncComparator: public CVmHashFunc |
| 76 | { |
| 77 | public: |
| 78 | CVmHashFuncComparator(VMG_ vm_obj_id_t obj) |
| 79 | { |
| 80 | globals_ = VMGLOB_ADDR; |
| 81 | comparator_ = obj; |
| 82 | } |
| 83 | |
| 84 | /* compute the hash value */ |
| 85 | unsigned int compute_hash(const char *str, size_t len) const |
| 86 | { |
| 87 | vm_val_t val; |
| 88 | vm_val_t cobj_val; |
| 89 | VMGLOB_PTR(globals_); |
| 90 | |
| 91 | /* set up a recursive call context */ |
| 92 | vm_rcdesc rc; |
| 93 | rc.init_ret(vmg_ "HashComparator.calcHash"); |
| 94 | |
| 95 | /* create a string object to represent the argument, and push it */ |
| 96 | G_stk->push()->set_obj(CVmObjString::create(vmg_ FALSE, str, len)); |
| 97 | |
| 98 | /* invoke the calcHash method */ |
| 99 | cobj_val.set_obj(comparator_); |
| 100 | G_interpreter->get_prop(vmg_ 0, &cobj_val, G_predef->calc_hash_prop, |
| 101 | &cobj_val, 1, &rc); |
| 102 | |
| 103 | /* retrieve the result from R0 */ |
| 104 | val = *G_interpreter->get_r0(); |
| 105 | |
| 106 | /* we need an integer value from the method */ |
| 107 | if (val.typ == VM_INT) |
| 108 | { |
| 109 | /* return the hash value from the comparator */ |
| 110 | return val.val.intval; |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | /* no or invalid hash value - throw an error */ |
| 115 | err_throw(VMERR_BAD_TYPE_BIF); |
| 116 | AFTER_ERR_THROW(return FALSE;) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | /* my comparator object */ |
| 122 | vm_obj_id_t comparator_; |
| 123 | |
| 124 | /* system globals */ |
| 125 | vm_globals *globals_; |
| 126 | }; |
| 127 | |
| 128 | /* |
| 129 | * StringComparator-based hash |
| 130 | */ |
| 131 | class CVmHashFuncStrComp: public CVmHashFunc |
| 132 | { |
| 133 | public: |
| 134 | CVmHashFuncStrComp(CVmObjStrComp *comp) { comparator_ = comp; } |
| 135 | |
| 136 | /* compute the hash value */ |
| 137 | unsigned int compute_hash(const char *str, size_t len) const |
| 138 | { |
| 139 | /* ask the StringComparator to do the work */ |
| 140 | return comparator_->calc_str_hash(str, len); |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | /* my StringComparator object */ |
| 145 | CVmObjStrComp *comparator_; |
| 146 | }; |
| 147 | |
| 148 | |
| 149 | /* ------------------------------------------------------------------------ */ |
| 150 | /* |
| 151 | * dictionary object statics |
| 152 | */ |
| 153 | |
| 154 | /* metaclass registration object */ |
| 155 | static CVmMetaclassDict metaclass_reg_obj; |
| 156 | CVmMetaclass *CVmObjDict::metaclass_reg_ = &metaclass_reg_obj; |
| 157 | |
| 158 | /* function table */ |
| 159 | int (CVmObjDict:: |
| 160 | *CVmObjDict::func_table_[])(VMG_ vm_obj_id_t self, |
| 161 | vm_val_t *retval, uint *argc) = |
| 162 | { |
| 163 | &CVmObjDict::getp_undef, /* 0 */ |
| 164 | &CVmObjDict::getp_set_comparator, /* 1 */ |
| 165 | &CVmObjDict::getp_find, /* 2 */ |
| 166 | &CVmObjDict::getp_add, /* 3 */ |
| 167 | &CVmObjDict::getp_del, /* 4 */ |
| 168 | &CVmObjDict::getp_is_defined, /* 5 */ |
| 169 | &CVmObjDict::getp_for_each_word, /* 6 */ |
| 170 | &CVmObjDict::getp_correct /* 7 */ |
| 171 | }; |
| 172 | |
| 173 | /* ------------------------------------------------------------------------ */ |
| 174 | /* |
| 175 | * Dictionary object implementation |
| 176 | */ |
| 177 | |
| 178 | /* |
| 179 | * create dynamically using stack arguments |
| 180 | */ |
| 181 | vm_obj_id_t CVmObjDict::create_from_stack(VMG_ const uchar **pc_ptr, |
| 182 | uint argc) |
| 183 | { |
| 184 | vm_obj_id_t id; |
| 185 | vm_obj_id_t comp; |
| 186 | CVmObjDict *obj; |
| 187 | |
| 188 | /* check arguments */ |
| 189 | if (argc != 0 && argc != 1) |
| 190 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 191 | |
| 192 | /* get the Comparator, but leave it on the stack as gc protection */ |
| 193 | if (argc == 0 || G_stk->get(0)->typ == VM_NIL) |
| 194 | comp = VM_INVALID_OBJ; |
| 195 | else if (G_stk->get(0)->typ == VM_OBJ) |
| 196 | comp = G_stk->get(0)->val.obj; |
| 197 | else |
| 198 | err_throw(VMERR_BAD_TYPE_BIF); |
| 199 | |
| 200 | /* |
| 201 | * allocate the object ID - this type of construction never creates |
| 202 | * a root object |
| 203 | */ |
| 204 | id = vm_new_id(vmg_ FALSE, TRUE, TRUE); |
| 205 | |
| 206 | /* create the object */ |
| 207 | obj = new (vmg_ id) CVmObjDict(vmg0_); |
| 208 | |
| 209 | /* set the comparator */ |
| 210 | obj->set_comparator(vmg_ comp); |
| 211 | |
| 212 | /* build an empty initial hash table */ |
| 213 | obj->create_hash_table(vmg0_); |
| 214 | |
| 215 | /* |
| 216 | * mark the object as modified since image load, since it doesn't |
| 217 | * come from the image file at all |
| 218 | */ |
| 219 | obj->get_ext()->modified_ = TRUE; |
| 220 | |
| 221 | /* discard our gc protection */ |
| 222 | G_stk->discard(); |
| 223 | |
| 224 | /* return the new ID */ |
| 225 | return id; |
| 226 | } |
| 227 | |
| 228 | /* ------------------------------------------------------------------------ */ |
| 229 | /* |
| 230 | * constructor |
| 231 | */ |
| 232 | CVmObjDict::CVmObjDict(VMG0_) |
| 233 | { |
| 234 | /* allocate our extension structure from the variable heap */ |
| 235 | ext_ = (char *)G_mem->get_var_heap() |
| 236 | ->alloc_mem(sizeof(vm_dict_ext), this); |
| 237 | |
| 238 | /* we have no image data yet */ |
| 239 | get_ext()->image_data_ = 0; |
| 240 | get_ext()->image_data_size_ = 0; |
| 241 | |
| 242 | /* no hash table yet */ |
| 243 | get_ext()->hashtab_ = 0; |
| 244 | |
| 245 | /* no Trie yet */ |
| 246 | get_ext()->trie_ = 0; |
| 247 | |
| 248 | /* no non-image entries yet */ |
| 249 | get_ext()->modified_ = FALSE; |
| 250 | |
| 251 | /* no comparator yet */ |
| 252 | get_ext()->comparator_ = VM_INVALID_OBJ; |
| 253 | set_comparator_type(vmg_ VM_INVALID_OBJ); |
| 254 | } |
| 255 | |
| 256 | /* ------------------------------------------------------------------------ */ |
| 257 | /* |
| 258 | * notify of deletion |
| 259 | */ |
| 260 | void CVmObjDict::notify_delete(VMG_ int /*in_root_set*/) |
| 261 | { |
| 262 | /* free our additional data */ |
| 263 | if (ext_ != 0) |
| 264 | { |
| 265 | /* free our hash table */ |
| 266 | if (get_ext()->hashtab_ != 0) |
| 267 | delete get_ext()->hashtab_; |
| 268 | |
| 269 | /* free our Trie */ |
| 270 | if (get_ext()->trie_ != 0) |
| 271 | delete get_ext()->trie_; |
| 272 | |
| 273 | /* free the extension */ |
| 274 | G_mem->get_var_heap()->free_mem(ext_); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* ------------------------------------------------------------------------ */ |
| 279 | /* |
| 280 | * Set the comparator object |
| 281 | */ |
| 282 | void CVmObjDict::set_comparator(VMG_ vm_obj_id_t obj) |
| 283 | { |
| 284 | /* if this is the same as the current comparator, there's nothing to do */ |
| 285 | if (obj == get_ext()->comparator_) |
| 286 | return; |
| 287 | |
| 288 | /* remember the new comparator */ |
| 289 | get_ext()->comparator_ = obj; |
| 290 | |
| 291 | /* figure out what kind of compare functions to use */ |
| 292 | set_comparator_type(vmg_ obj); |
| 293 | |
| 294 | /* rebuild the hash tale */ |
| 295 | if (get_ext()->hashtab_ != 0) |
| 296 | create_hash_table(vmg0_); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Set the string-compare function based on the given comparator. |
| 301 | */ |
| 302 | void CVmObjDict::set_comparator_type(VMG_ vm_obj_id_t obj) |
| 303 | { |
| 304 | /* check what kind of object we have */ |
| 305 | if (obj == VM_INVALID_OBJ) |
| 306 | { |
| 307 | /* there's no comparator at all, so simply use an exact comparison */ |
| 308 | get_ext()->comparator_type_ = VMDICT_COMP_NONE; |
| 309 | } |
| 310 | else if (CVmObjStrComp::is_strcmp_obj(vmg_ obj)) |
| 311 | { |
| 312 | /* it's a StringComparator */ |
| 313 | get_ext()->comparator_type_ = VMDICT_COMP_STRCOMP; |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | /* it's a generic comparator object */ |
| 318 | get_ext()->comparator_type_ = VMDICT_COMP_GENERIC; |
| 319 | } |
| 320 | |
| 321 | /* remember the object pointer, if there is one */ |
| 322 | get_ext()->comparator_obj_ = (obj == VM_INVALID_OBJ |
| 323 | ? 0 : vm_objp(vmg_ obj)); |
| 324 | } |
| 325 | |
| 326 | /* ------------------------------------------------------------------------ */ |
| 327 | /* |
| 328 | * Rebuild the hash table. If there's an existing hash table, we'll move |
| 329 | * the entries from the old table to the new table, and delete the old |
| 330 | * table. |
| 331 | */ |
| 332 | void CVmObjDict::create_hash_table(VMG0_) |
| 333 | { |
| 334 | CVmHashTable *new_tab; |
| 335 | CVmHashFunc *hash_func; |
| 336 | |
| 337 | /* |
| 338 | * Create our hash function. If we have a comparator object, base the |
| 339 | * hash function on the comparator; use a special hash function if we |
| 340 | * specifically have a StringComparator, since we can call these |
| 341 | * directly for better efficiency. If we have no comparator, create a |
| 342 | * generic exact string match hash function. |
| 343 | */ |
| 344 | if (get_ext()->comparator_ != VM_INVALID_OBJ) |
| 345 | { |
| 346 | /* |
| 347 | * use our special StringComparator hash function if possible; |
| 348 | * otherwise, use a generic comparator hash function |
| 349 | */ |
| 350 | if (CVmObjStrComp::is_strcmp_obj(vmg_ get_ext()->comparator_)) |
| 351 | { |
| 352 | /* create a StringComparator hash function */ |
| 353 | hash_func = new CVmHashFuncStrComp( |
| 354 | (CVmObjStrComp *)vm_objp(vmg_ get_ext()->comparator_)); |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | /* create a generic comparator hash function */ |
| 359 | hash_func = new CVmHashFuncComparator( |
| 360 | vmg_ get_ext()->comparator_); |
| 361 | } |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | /* create a simple exact-match hash */ |
| 366 | hash_func = new CVmHashFuncCS(); |
| 367 | } |
| 368 | |
| 369 | /* create the hash table */ |
| 370 | new_tab = new CVmHashTable(256, hash_func, TRUE); |
| 371 | |
| 372 | /* if we had a previous hash table, move its contents to the new table */ |
| 373 | if (get_ext()->hashtab_ != 0) |
| 374 | { |
| 375 | /* copy the old hash table to the new one */ |
| 376 | get_ext()->hashtab_->move_entries_to(new_tab); |
| 377 | |
| 378 | /* delete the old hash table */ |
| 379 | delete get_ext()->hashtab_; |
| 380 | } |
| 381 | |
| 382 | /* if we had a previous trie, get rid of it */ |
| 383 | if (get_ext()->trie_ != 0) |
| 384 | { |
| 385 | delete get_ext()->trie_; |
| 386 | get_ext()->trie_ = 0; |
| 387 | } |
| 388 | |
| 389 | /* store the new hash table in the extension */ |
| 390 | get_ext()->hashtab_ = new_tab; |
| 391 | } |
| 392 | |
| 393 | /* ------------------------------------------------------------------------ */ |
| 394 | /* |
| 395 | * Check for a match between two strings, using the current comparator. |
| 396 | * Returns true if the strings match, false if not; fills in *result_val |
| 397 | * with the actual result value from the comparator's matchValues() |
| 398 | * function. |
| 399 | */ |
| 400 | int CVmObjDict::match_strings(VMG_ const vm_val_t *valstrval, |
| 401 | const char *valstr, size_t vallen, |
| 402 | const char *refstr, size_t reflen, |
| 403 | vm_val_t *result_val) |
| 404 | { |
| 405 | vm_val_t cobj_val; |
| 406 | |
| 407 | /* check what kind of comparator we have */ |
| 408 | switch(get_ext()->comparator_type_) |
| 409 | { |
| 410 | case VMDICT_COMP_NONE: |
| 411 | /* no comparator - compare the strings byte-for-byte */ |
| 412 | if (vallen == reflen && memcmp(valstr, refstr, vallen) == 0) |
| 413 | { |
| 414 | /* match - return true */ |
| 415 | result_val->set_int(1); |
| 416 | return TRUE; |
| 417 | } |
| 418 | else |
| 419 | { |
| 420 | /* no match - return false */ |
| 421 | result_val->set_int(0); |
| 422 | return FALSE; |
| 423 | } |
| 424 | |
| 425 | case VMDICT_COMP_STRCOMP: |
| 426 | /* match the value directly with the StringComparator */ |
| 427 | result_val->set_int( |
| 428 | ((CVmObjStrComp *)get_ext()->comparator_obj_)->match_strings( |
| 429 | valstr, vallen, refstr, reflen)); |
| 430 | |
| 431 | /* we matched if the result was non-zero */ |
| 432 | return result_val->val.intval; |
| 433 | |
| 434 | case VMDICT_COMP_GENERIC: |
| 435 | /* 2nd param: push the reference string, as a string object */ |
| 436 | G_stk->push()->set_obj( |
| 437 | CVmObjString::create(vmg_ FALSE, refstr, reflen)); |
| 438 | |
| 439 | /* 1st param: push the value string */ |
| 440 | if (valstrval != 0) |
| 441 | { |
| 442 | /* push the value string as given */ |
| 443 | G_stk->push(valstrval); |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | /* no value string was given - create one and push it */ |
| 448 | G_stk->push()->set_obj( |
| 449 | CVmObjString::create(vmg_ FALSE, valstr, vallen)); |
| 450 | } |
| 451 | |
| 452 | { |
| 453 | /* set up a recursive call context */ |
| 454 | vm_rcdesc rc; |
| 455 | rc.init_ret(vmg_ "HashComparator.matchValues"); |
| 456 | |
| 457 | /* call the comparator's matchValues method */ |
| 458 | cobj_val.set_obj(get_ext()->comparator_); |
| 459 | G_interpreter->get_prop(vmg_ 0, &cobj_val, |
| 460 | G_predef->match_values_prop, &cobj_val, 2, |
| 461 | &rc); |
| 462 | } |
| 463 | |
| 464 | /* get the result from R0 */ |
| 465 | *result_val = *G_interpreter->get_r0(); |
| 466 | |
| 467 | /* we matched if the result isn't 0 or nil */ |
| 468 | return !(result_val->typ == VM_NIL |
| 469 | || (result_val->typ == VM_INT |
| 470 | && result_val->val.intval == 0)); |
| 471 | } |
| 472 | |
| 473 | /* we should never get here, but just in case... */ |
| 474 | return FALSE; |
| 475 | } |
| 476 | |
| 477 | /* ------------------------------------------------------------------------ */ |
| 478 | /* |
| 479 | * Calculate a hash value with the current comparator |
| 480 | */ |
| 481 | unsigned int CVmObjDict::calc_str_hash(VMG_ const vm_val_t *valstrval, |
| 482 | const char *valstr, size_t vallen) |
| 483 | { |
| 484 | vm_val_t result; |
| 485 | vm_val_t cobj_val; |
| 486 | |
| 487 | /* check what kind of comparator we have */ |
| 488 | switch(get_ext()->comparator_type_) |
| 489 | { |
| 490 | case VMDICT_COMP_NONE: |
| 491 | /* no comparator - use the hash table's basic hash calculation */ |
| 492 | return get_ext()->hashtab_->compute_hash(valstr, vallen); |
| 493 | |
| 494 | case VMDICT_COMP_STRCOMP: |
| 495 | /* calculate the hash directly with the StringComparator */ |
| 496 | return ((CVmObjStrComp *)get_ext()->comparator_obj_)->calc_str_hash( |
| 497 | valstr, vallen); |
| 498 | |
| 499 | case VMDICT_COMP_GENERIC: |
| 500 | /* push the value string */ |
| 501 | if (valstrval != 0) |
| 502 | { |
| 503 | /* push the value string as given */ |
| 504 | G_stk->push(valstrval); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | /* no value string was given - create one and push it */ |
| 509 | G_stk->push()->set_obj( |
| 510 | CVmObjString::create(vmg_ FALSE, valstr, vallen)); |
| 511 | } |
| 512 | |
| 513 | { |
| 514 | /* set up a recursive call context */ |
| 515 | vm_rcdesc rc; |
| 516 | rc.init_ret(vmg_ "HashComparator.calcHash"); |
| 517 | |
| 518 | /* call the comparator object's calcHash method */ |
| 519 | cobj_val.set_obj(get_ext()->comparator_); |
| 520 | G_interpreter->get_prop(vmg_ 0, &cobj_val, |
| 521 | G_predef->calc_hash_prop, |
| 522 | &cobj_val, 1, &rc); |
| 523 | } |
| 524 | |
| 525 | /* get the result from R0 */ |
| 526 | result = *G_interpreter->get_r0(); |
| 527 | |
| 528 | /* make sure it's an integer */ |
| 529 | if (result.typ != VM_INT) |
| 530 | err_throw(VMERR_BAD_TYPE_BIF); |
| 531 | |
| 532 | /* return the result */ |
| 533 | return (unsigned int)result.val.intval; |
| 534 | } |
| 535 | |
| 536 | /* we should never get here, but just in case... */ |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | /* ------------------------------------------------------------------------ */ |
| 541 | /* |
| 542 | * Set a property. We have no settable properties, so simply signal an |
| 543 | * error indicating that the set-prop call is invalid. |
| 544 | */ |
| 545 | void CVmObjDict::set_prop(VMG_ CVmUndo *, vm_obj_id_t, |
| 546 | vm_prop_id_t, const vm_val_t *) |
| 547 | { |
| 548 | /* no settable properties - throw an error */ |
| 549 | err_throw(VMERR_INVALID_SETPROP); |
| 550 | } |
| 551 | |
| 552 | /* ------------------------------------------------------------------------ */ |
| 553 | /* |
| 554 | * get a property |
| 555 | */ |
| 556 | int CVmObjDict::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 557 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 558 | uint *argc) |
| 559 | { |
| 560 | uint func_idx; |
| 561 | |
| 562 | /* translate the property into a function vector index */ |
| 563 | func_idx = G_meta_table |
| 564 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 565 | |
| 566 | /* call the appropriate function */ |
| 567 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 568 | { |
| 569 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 570 | return TRUE; |
| 571 | } |
| 572 | |
| 573 | /* inherit default handling from the base object class */ |
| 574 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 575 | } |
| 576 | |
| 577 | /* ------------------------------------------------------------------------ */ |
| 578 | /* |
| 579 | * evaluate property: set the comparator |
| 580 | */ |
| 581 | int CVmObjDict::getp_set_comparator(VMG_ vm_obj_id_t self, vm_val_t *val, |
| 582 | uint *argc) |
| 583 | { |
| 584 | static CVmNativeCodeDesc desc(1); |
| 585 | vm_obj_id_t comp; |
| 586 | |
| 587 | /* check arguments */ |
| 588 | if (get_prop_check_argc(val, argc, &desc)) |
| 589 | return TRUE; |
| 590 | |
| 591 | /* get the comparator object */ |
| 592 | switch(G_stk->get(0)->typ) |
| 593 | { |
| 594 | case VM_NIL: |
| 595 | comp = VM_INVALID_OBJ; |
| 596 | break; |
| 597 | |
| 598 | case VM_OBJ: |
| 599 | comp = G_stk->get(0)->val.obj; |
| 600 | break; |
| 601 | |
| 602 | default: |
| 603 | err_throw(VMERR_BAD_TYPE_BIF); |
| 604 | } |
| 605 | |
| 606 | /* if there's a global undo object, add undo for the change */ |
| 607 | if (G_undo != 0) |
| 608 | { |
| 609 | dict_undo_rec *undo_rec; |
| 610 | |
| 611 | /* create an undo record with the original comparator */ |
| 612 | undo_rec = alloc_undo_rec(DICT_UNDO_COMPARATOR, 0, 0); |
| 613 | undo_rec->obj = get_ext()->comparator_; |
| 614 | |
| 615 | /* add the undo record */ |
| 616 | add_undo_rec(vmg_ self, undo_rec); |
| 617 | } |
| 618 | |
| 619 | /* set our new comparator object */ |
| 620 | set_comparator(vmg_ comp); |
| 621 | |
| 622 | /* mark the object as modified since load */ |
| 623 | get_ext()->modified_ = TRUE; |
| 624 | |
| 625 | /* discard arguments */ |
| 626 | G_stk->discard(); |
| 627 | |
| 628 | /* no return value - just return nil */ |
| 629 | val->set_nil(); |
| 630 | |
| 631 | /* handled */ |
| 632 | return TRUE; |
| 633 | } |
| 634 | |
| 635 | /* ------------------------------------------------------------------------ */ |
| 636 | /* |
| 637 | * result entry for find_ctx |
| 638 | */ |
| 639 | struct find_entry |
| 640 | { |
| 641 | /* matching object */ |
| 642 | vm_obj_id_t obj; |
| 643 | |
| 644 | /* match result code */ |
| 645 | vm_val_t match_result; |
| 646 | }; |
| 647 | |
| 648 | /* |
| 649 | * page of results for find_ctx |
| 650 | */ |
| 651 | const int FIND_ENTRIES_PER_PAGE = 32; |
| 652 | struct find_entry_page |
| 653 | { |
| 654 | /* next in list of pages */ |
| 655 | find_entry_page *nxt; |
| 656 | |
| 657 | /* array of entries */ |
| 658 | find_entry entry[FIND_ENTRIES_PER_PAGE]; |
| 659 | }; |
| 660 | |
| 661 | /* |
| 662 | * enumeration callback context for getp_find |
| 663 | */ |
| 664 | struct find_ctx |
| 665 | { |
| 666 | /* set up a context that adds results to our internal list */ |
| 667 | find_ctx(VMG_ CVmObjDict *dict_obj, const vm_val_t *val, |
| 668 | const char *str, size_t len, vm_prop_id_t prop) |
| 669 | { |
| 670 | /* remember the dictionary object we're searching */ |
| 671 | dict = dict_obj; |
| 672 | |
| 673 | /* remember the globals */ |
| 674 | globals = VMGLOB_ADDR; |
| 675 | |
| 676 | /* keep a pointer to the search string value */ |
| 677 | this->strval = val; |
| 678 | this->strp = str; |
| 679 | this->strl = len; |
| 680 | |
| 681 | /* remember the property */ |
| 682 | voc_prop = prop; |
| 683 | |
| 684 | /* no results yet */ |
| 685 | results_head = 0; |
| 686 | results_tail = 0; |
| 687 | result_cnt = 0; |
| 688 | |
| 689 | /* |
| 690 | * set the free pointer to the end of the non-existent current |
| 691 | * page, so we know we have to allocate a new page before adding |
| 692 | * the first entry |
| 693 | */ |
| 694 | result_free = FIND_ENTRIES_PER_PAGE; |
| 695 | } |
| 696 | |
| 697 | ~find_ctx() |
| 698 | { |
| 699 | /* free our list of results pages */ |
| 700 | while (results_head != 0) |
| 701 | { |
| 702 | /* remember the current page */ |
| 703 | find_entry_page *cur = results_head; |
| 704 | |
| 705 | /* move on to the next page */ |
| 706 | results_head = results_head->nxt; |
| 707 | |
| 708 | /* delete the current page */ |
| 709 | t3free(cur); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /* add a result, expanding the result array if necessary */ |
| 714 | void add_result(vm_obj_id_t obj, const vm_val_t *match_result) |
| 715 | { |
| 716 | /* |
| 717 | * if we have no pages, or we're out of room on the current page, |
| 718 | * allocate a new page |
| 719 | */ |
| 720 | if (result_free == FIND_ENTRIES_PER_PAGE) |
| 721 | { |
| 722 | find_entry_page *pg; |
| 723 | |
| 724 | /* allocate a new page */ |
| 725 | pg = (find_entry_page *)t3malloc(sizeof(find_entry_page)); |
| 726 | |
| 727 | /* link it in at the end of the list */ |
| 728 | pg->nxt = 0; |
| 729 | if (results_tail != 0) |
| 730 | results_tail->nxt = pg; |
| 731 | else |
| 732 | results_head = pg; |
| 733 | results_tail = pg; |
| 734 | |
| 735 | /* reset the free pointer to the start of the new page */ |
| 736 | result_free = 0; |
| 737 | } |
| 738 | |
| 739 | /* add this result */ |
| 740 | results_tail->entry[result_free].obj = obj; |
| 741 | results_tail->entry[result_free].match_result = *match_result; |
| 742 | ++result_free; |
| 743 | ++result_cnt; |
| 744 | } |
| 745 | |
| 746 | /* make our results into a list */ |
| 747 | vm_obj_id_t results_to_list(VMG0_) |
| 748 | { |
| 749 | vm_obj_id_t lst_id; |
| 750 | CVmObjList *lst; |
| 751 | find_entry_page *pg; |
| 752 | int idx; |
| 753 | |
| 754 | /* |
| 755 | * Allocate a list of the appropriate size. We need two list |
| 756 | * entries per result, since we need to store the object and the |
| 757 | * match result code for each result. |
| 758 | */ |
| 759 | lst_id = CVmObjList::create(vmg_ FALSE, result_cnt * 2); |
| 760 | lst = (CVmObjList *)vm_objp(vmg_ lst_id); |
| 761 | |
| 762 | /* add all entries */ |
| 763 | for (idx = 0, pg = results_head ; idx < result_cnt ; pg = pg->nxt) |
| 764 | { |
| 765 | find_entry *ep; |
| 766 | int pg_idx; |
| 767 | |
| 768 | /* add each entry on this page */ |
| 769 | for (ep = pg->entry, pg_idx = 0 ; |
| 770 | idx < result_cnt && pg_idx < FIND_ENTRIES_PER_PAGE ; |
| 771 | ++idx, ++pg_idx, ++ep) |
| 772 | { |
| 773 | vm_val_t ele; |
| 774 | |
| 775 | /* add this entry to the list */ |
| 776 | ele.set_obj(ep->obj); |
| 777 | lst->cons_set_element(idx*2, &ele); |
| 778 | lst->cons_set_element(idx*2 + 1, &ep->match_result); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /* return the list */ |
| 783 | return lst_id; |
| 784 | } |
| 785 | |
| 786 | /* head/tail of list of result pages */ |
| 787 | find_entry_page *results_head; |
| 788 | find_entry_page *results_tail; |
| 789 | |
| 790 | /* next available entry on current results page */ |
| 791 | int result_free; |
| 792 | |
| 793 | /* total number of result */ |
| 794 | int result_cnt; |
| 795 | |
| 796 | /* the string to find */ |
| 797 | const vm_val_t *strval; |
| 798 | const char *strp; |
| 799 | size_t strl; |
| 800 | |
| 801 | /* vocabulary property value to match */ |
| 802 | vm_prop_id_t voc_prop; |
| 803 | |
| 804 | /* VM globals */ |
| 805 | vm_globals *globals; |
| 806 | |
| 807 | /* dictionary object we're searching */ |
| 808 | CVmObjDict *dict; |
| 809 | }; |
| 810 | |
| 811 | /* |
| 812 | * Callback for getp_find hash match enumeration |
| 813 | */ |
| 814 | void CVmObjDict::find_cb(void *ctx0, CVmHashEntry *entry0) |
| 815 | { |
| 816 | find_ctx *ctx = (find_ctx *)ctx0; |
| 817 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 818 | vm_val_t match_val; |
| 819 | VMGLOB_PTR(ctx->globals); |
| 820 | |
| 821 | /* if this entry matches the search string, process it */ |
| 822 | if (ctx->dict->match_strings(vmg_ ctx->strval, ctx->strp, ctx->strl, |
| 823 | entry->getstr(), entry->getlen(), |
| 824 | &match_val)) |
| 825 | { |
| 826 | vm_dict_entry *cur; |
| 827 | |
| 828 | /* process the items under this entry */ |
| 829 | for (cur = entry->get_head() ; cur != 0 ; cur = cur->nxt_) |
| 830 | { |
| 831 | /* |
| 832 | * If it matches the target property, it's a hit. If we're |
| 833 | * searching for any property at all (indicated by the |
| 834 | * context property being set to VM_INVALID_PROP), count |
| 835 | * everything. |
| 836 | */ |
| 837 | if (cur->prop_ == ctx->voc_prop |
| 838 | || ctx->voc_prop == VM_INVALID_PROP) |
| 839 | { |
| 840 | /* process the result */ |
| 841 | ctx->add_result(cur->obj_, &match_val); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | /* ------------------------------------------------------------------------ */ |
| 848 | /* |
| 849 | * property evaluation - look up a word |
| 850 | */ |
| 851 | int CVmObjDict::getp_find(VMG_ vm_obj_id_t, vm_val_t *val, uint *argc) |
| 852 | { |
| 853 | static CVmNativeCodeDesc desc(1, 1); |
| 854 | uint orig_argc = (argc != 0 ? *argc : 0); |
| 855 | vm_val_t *arg0; |
| 856 | vm_val_t *arg1; |
| 857 | const char *strp; |
| 858 | size_t strl; |
| 859 | vm_prop_id_t voc_prop; |
| 860 | unsigned int hash; |
| 861 | |
| 862 | /* check arguments */ |
| 863 | if (get_prop_check_argc(val, argc, &desc)) |
| 864 | return TRUE; |
| 865 | |
| 866 | /* |
| 867 | * make sure the first argument is a string, but leave it on the stack |
| 868 | * for gc protection |
| 869 | */ |
| 870 | arg0 = G_stk->get(0); |
| 871 | if ((strp = arg0->get_as_string(vmg0_)) == 0) |
| 872 | err_throw(VMERR_STRING_VAL_REQD); |
| 873 | |
| 874 | /* scan and skip the string's length prefix */ |
| 875 | strl = vmb_get_len(strp); |
| 876 | strp += VMB_LEN; |
| 877 | |
| 878 | /* |
| 879 | * get the property ID, which can be omitted or specified as nil to |
| 880 | * indicate any property |
| 881 | */ |
| 882 | if (orig_argc < 2) |
| 883 | voc_prop = VM_INVALID_PROP; |
| 884 | else if ((arg1 = G_stk->get(1))->typ == VM_NIL) |
| 885 | voc_prop = VM_INVALID_PROP; |
| 886 | else if (arg1->typ == VM_PROP) |
| 887 | voc_prop = arg1->val.prop; |
| 888 | else |
| 889 | err_throw(VMERR_PROPPTR_VAL_REQD); |
| 890 | |
| 891 | /* calculate the hash value */ |
| 892 | hash = calc_str_hash(vmg_ arg0, strp, strl); |
| 893 | |
| 894 | /* enumerate everything that matches the string's hash code */ |
| 895 | find_ctx ctx(vmg_ this, arg0, strp, strl, voc_prop); |
| 896 | get_ext()->hashtab_->enum_hash_matches(hash, &find_cb, &ctx); |
| 897 | |
| 898 | /* build a list out of the results, and return the list */ |
| 899 | val->set_obj(ctx.results_to_list(vmg0_)); |
| 900 | |
| 901 | /* discard arguments */ |
| 902 | G_stk->discard(orig_argc); |
| 903 | |
| 904 | /* success */ |
| 905 | return TRUE; |
| 906 | } |
| 907 | |
| 908 | /* ------------------------------------------------------------------------ */ |
| 909 | /* |
| 910 | * property evaluation - add a word |
| 911 | */ |
| 912 | int CVmObjDict::getp_add(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc) |
| 913 | { |
| 914 | vm_obj_id_t obj; |
| 915 | vm_val_t str_val; |
| 916 | vm_prop_id_t voc_prop; |
| 917 | CVmObject *objp; |
| 918 | int lstcnt; |
| 919 | const char *str; |
| 920 | static CVmNativeCodeDesc desc(3); |
| 921 | |
| 922 | /* check arguments */ |
| 923 | if (get_prop_check_argc(val, argc, &desc)) |
| 924 | return TRUE; |
| 925 | |
| 926 | /* pop the object, word string, and property ID arguments */ |
| 927 | obj = CVmBif::pop_obj_val(vmg0_); |
| 928 | G_stk->pop(&str_val); |
| 929 | voc_prop = CVmBif::pop_propid_val(vmg0_); |
| 930 | |
| 931 | /* ensure that the object value isn't a string or list */ |
| 932 | objp = vm_objp(vmg_ obj); |
| 933 | if (objp->get_as_list() != 0 || objp->get_as_string(vmg0_) != 0) |
| 934 | err_throw(VMERR_OBJ_VAL_REQD); |
| 935 | |
| 936 | /* if the string argument is a list, add each word from the list */ |
| 937 | if (str_val.is_listlike(vmg0_) |
| 938 | && (lstcnt = str_val.ll_length(vmg0_)) >= 0) |
| 939 | { |
| 940 | /* iterate over the list, using 1-based indices */ |
| 941 | for (int idx = 1 ; idx <= lstcnt ; ++idx) |
| 942 | { |
| 943 | /* get the next value from the list */ |
| 944 | vm_val_t ele_val; |
| 945 | str_val.ll_index(vmg_ &ele_val, idx); |
| 946 | |
| 947 | /* get the next string value */ |
| 948 | if ((str = ele_val.get_as_string(vmg0_)) == 0) |
| 949 | err_throw(VMERR_STRING_VAL_REQD); |
| 950 | |
| 951 | /* set this string */ |
| 952 | add_word(vmg_ self, str, obj, voc_prop); |
| 953 | } |
| 954 | } |
| 955 | else if ((str = str_val.get_as_string(vmg0_)) != 0) |
| 956 | { |
| 957 | /* add the string */ |
| 958 | add_word(vmg_ self, str, obj, voc_prop); |
| 959 | } |
| 960 | else |
| 961 | { |
| 962 | /* string value required */ |
| 963 | err_throw(VMERR_STRING_VAL_REQD); |
| 964 | } |
| 965 | |
| 966 | /* there's no return value */ |
| 967 | val->set_nil(); |
| 968 | |
| 969 | /* success */ |
| 970 | return TRUE; |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * Service routine for getp_add() - add a single string |
| 975 | */ |
| 976 | void CVmObjDict::add_word(VMG_ vm_obj_id_t self, |
| 977 | const char *str, size_t len, |
| 978 | vm_obj_id_t obj, vm_prop_id_t voc_prop) |
| 979 | { |
| 980 | /* add the entry */ |
| 981 | int added = add_hash_entry(vmg_ str, len, TRUE, obj, voc_prop, FALSE); |
| 982 | |
| 983 | /* |
| 984 | * if there's a global undo object, and we actually added a new |
| 985 | * entry, add undo for the change |
| 986 | */ |
| 987 | if (added && G_undo != 0) |
| 988 | { |
| 989 | dict_undo_rec *undo_rec; |
| 990 | |
| 991 | /* create the undo record */ |
| 992 | undo_rec = alloc_undo_rec(DICT_UNDO_ADD, str, len); |
| 993 | undo_rec->obj = obj; |
| 994 | undo_rec->prop = voc_prop; |
| 995 | |
| 996 | /* add the undo record */ |
| 997 | add_undo_rec(vmg_ self, undo_rec); |
| 998 | } |
| 999 | |
| 1000 | /* mark the object as modified since load */ |
| 1001 | get_ext()->modified_ = TRUE; |
| 1002 | } |
| 1003 | |
| 1004 | /* ------------------------------------------------------------------------ */ |
| 1005 | /* |
| 1006 | * property evaluation - delete a word |
| 1007 | */ |
| 1008 | int CVmObjDict::getp_del(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc) |
| 1009 | { |
| 1010 | vm_obj_id_t obj; |
| 1011 | vm_val_t str_val; |
| 1012 | vm_prop_id_t voc_prop; |
| 1013 | int lstcnt; |
| 1014 | const char *str; |
| 1015 | static CVmNativeCodeDesc desc(3); |
| 1016 | |
| 1017 | /* check arguments */ |
| 1018 | if (get_prop_check_argc(val, argc, &desc)) |
| 1019 | return TRUE; |
| 1020 | |
| 1021 | /* pop the object, word string, and property ID arguments */ |
| 1022 | obj = CVmBif::pop_obj_val(vmg0_); |
| 1023 | G_stk->pop(&str_val); |
| 1024 | voc_prop = CVmBif::pop_propid_val(vmg0_); |
| 1025 | |
| 1026 | /* if the string argument is a list, add each word from the list */ |
| 1027 | if (str_val.is_listlike(vmg0_) |
| 1028 | && (lstcnt = str_val.ll_length(vmg0_)) >= 0) |
| 1029 | { |
| 1030 | /* iterate over the list, using 1-based indices */ |
| 1031 | for (int idx = 1 ; idx <= lstcnt ; ++idx) |
| 1032 | { |
| 1033 | /* get the next value from the list */ |
| 1034 | vm_val_t ele_val; |
| 1035 | str_val.ll_index(vmg_ &ele_val, idx); |
| 1036 | |
| 1037 | /* get the next string value */ |
| 1038 | if ((str = ele_val.get_as_string(vmg0_)) == 0) |
| 1039 | err_throw(VMERR_STRING_VAL_REQD); |
| 1040 | |
| 1041 | /* remove this string */ |
| 1042 | del_word(vmg_ self, str, obj, voc_prop); |
| 1043 | } |
| 1044 | } |
| 1045 | else if ((str = str_val.get_as_string(vmg0_)) != 0) |
| 1046 | { |
| 1047 | /* remove the string */ |
| 1048 | del_word(vmg_ self, str, obj, voc_prop); |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | /* string value required */ |
| 1053 | err_throw(VMERR_STRING_VAL_REQD); |
| 1054 | } |
| 1055 | |
| 1056 | /* there's no return value */ |
| 1057 | val->set_nil(); |
| 1058 | |
| 1059 | /* success */ |
| 1060 | return TRUE; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * Service routine for getp_del - delete a string value |
| 1065 | */ |
| 1066 | void CVmObjDict::del_word(VMG_ vm_obj_id_t self, |
| 1067 | const char *str, size_t len, |
| 1068 | vm_obj_id_t obj, vm_prop_id_t voc_prop) |
| 1069 | { |
| 1070 | /* delete this entry */ |
| 1071 | int deleted = del_hash_entry(vmg_ str, len, obj, voc_prop); |
| 1072 | |
| 1073 | /* |
| 1074 | * if there's a global undo object, and we actually deleted an |
| 1075 | * entry, add undo for the change |
| 1076 | */ |
| 1077 | if (deleted && G_undo != 0) |
| 1078 | { |
| 1079 | dict_undo_rec *undo_rec; |
| 1080 | |
| 1081 | /* create the undo record */ |
| 1082 | undo_rec = alloc_undo_rec(DICT_UNDO_DEL, str, len); |
| 1083 | undo_rec->obj = obj; |
| 1084 | undo_rec->prop = voc_prop; |
| 1085 | |
| 1086 | /* add the undo record */ |
| 1087 | add_undo_rec(vmg_ self, undo_rec); |
| 1088 | } |
| 1089 | |
| 1090 | /* mark the object as modified since load */ |
| 1091 | get_ext()->modified_ = TRUE; |
| 1092 | } |
| 1093 | |
| 1094 | /* ------------------------------------------------------------------------ */ |
| 1095 | /* |
| 1096 | * callback context for is_defined enumeration |
| 1097 | */ |
| 1098 | struct isdef_ctx |
| 1099 | { |
| 1100 | isdef_ctx(VMG_ CVmObjDict *dict_obj, |
| 1101 | vm_val_t *filter, const vm_val_t *val, |
| 1102 | const char *str, size_t len) |
| 1103 | { |
| 1104 | /* remember the VM globals and dictionary object */ |
| 1105 | globals = VMGLOB_ADDR; |
| 1106 | dict = dict_obj; |
| 1107 | |
| 1108 | /* we haven't yet found a match */ |
| 1109 | found = FALSE; |
| 1110 | |
| 1111 | /* remember the string */ |
| 1112 | this->strval = *val; |
| 1113 | this->strp = str; |
| 1114 | this->strl = len; |
| 1115 | |
| 1116 | /* remember the filter function */ |
| 1117 | filter_func = filter; |
| 1118 | } |
| 1119 | |
| 1120 | /* flag: we've found a match */ |
| 1121 | int found; |
| 1122 | |
| 1123 | /* string value we're matching */ |
| 1124 | vm_val_t strval; |
| 1125 | const char *strp; |
| 1126 | size_t strl; |
| 1127 | |
| 1128 | /* our filter callback function, if any */ |
| 1129 | const vm_val_t *filter_func; |
| 1130 | |
| 1131 | /* VM globals */ |
| 1132 | vm_globals *globals; |
| 1133 | |
| 1134 | /* dictionary we're searching */ |
| 1135 | CVmObjDict *dict; |
| 1136 | |
| 1137 | /* recursive native caller context */ |
| 1138 | vm_rcdesc rc; |
| 1139 | }; |
| 1140 | |
| 1141 | /* |
| 1142 | * Callback for getp_is_defined hash match enumeration |
| 1143 | */ |
| 1144 | void CVmObjDict::isdef_cb(void *ctx0, CVmHashEntry *entry0) |
| 1145 | { |
| 1146 | isdef_ctx *ctx = (isdef_ctx *)ctx0; |
| 1147 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 1148 | vm_val_t match_val; |
| 1149 | VMGLOB_PTR(ctx->globals); |
| 1150 | |
| 1151 | /* |
| 1152 | * if we've already found a match, don't bother checking anything else |
| 1153 | * - it could be non-trivial to do the string match, so we can save |
| 1154 | * some work by skipping that if we've already found a match |
| 1155 | * previously |
| 1156 | */ |
| 1157 | if (ctx->found) |
| 1158 | return; |
| 1159 | |
| 1160 | /* |
| 1161 | * if this entry matches the search string, and it has at least one |
| 1162 | * defined object/property associated with it, count it as a match |
| 1163 | */ |
| 1164 | if (entry->get_head() != 0 |
| 1165 | && ctx->dict->match_strings(vmg_ &ctx->strval, ctx->strp, ctx->strl, |
| 1166 | entry->getstr(), entry->getlen(), |
| 1167 | &match_val)) |
| 1168 | { |
| 1169 | /* if there's a filter function to invoke, check with it */ |
| 1170 | if (ctx->filter_func->typ != VM_NIL) |
| 1171 | { |
| 1172 | vm_val_t *valp; |
| 1173 | |
| 1174 | /* push the match value parameter */ |
| 1175 | G_stk->push(&match_val); |
| 1176 | |
| 1177 | /* call the filter callback */ |
| 1178 | G_interpreter->call_func_ptr(vmg_ ctx->filter_func, 1, |
| 1179 | &ctx->rc, 0); |
| 1180 | |
| 1181 | /* get the result */ |
| 1182 | valp = G_interpreter->get_r0(); |
| 1183 | |
| 1184 | /* if it's 0 or nil, it's a rejection of the match */ |
| 1185 | if (valp->typ == VM_NIL |
| 1186 | || (valp->typ == VM_INT && valp->val.intval == 0)) |
| 1187 | { |
| 1188 | /* it's a rejection of the value - don't count it */ |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | /* it's an acceptance of the value - count it as found */ |
| 1193 | ctx->found = TRUE; |
| 1194 | } |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | /* there's no filter, so everything matches */ |
| 1199 | ctx->found = TRUE; |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * property evaluation - check to see if a word is defined |
| 1206 | */ |
| 1207 | int CVmObjDict::getp_is_defined(VMG_ vm_obj_id_t self, vm_val_t *val, |
| 1208 | uint *argc) |
| 1209 | { |
| 1210 | static CVmNativeCodeDesc desc(1, 1); |
| 1211 | uint orig_argc = (argc == 0 ? 0 : *argc); |
| 1212 | vm_val_t *arg0; |
| 1213 | const char *strp; |
| 1214 | size_t strl; |
| 1215 | vm_val_t filter; |
| 1216 | unsigned int hash; |
| 1217 | |
| 1218 | /* check arguments */ |
| 1219 | if (get_prop_check_argc(val, argc, &desc)) |
| 1220 | return TRUE; |
| 1221 | |
| 1222 | /* |
| 1223 | * make sure the argument is a string, but leave it on the stack for |
| 1224 | * gc protection |
| 1225 | */ |
| 1226 | arg0 = G_stk->get(0); |
| 1227 | if ((strp = arg0->get_as_string(vmg0_)) == 0) |
| 1228 | err_throw(VMERR_STRING_VAL_REQD); |
| 1229 | |
| 1230 | /* read and skip the string's length prefix */ |
| 1231 | strl = vmb_get_len(strp); |
| 1232 | strp += VMB_LEN; |
| 1233 | |
| 1234 | /* if there's a second argument, it's the filter function */ |
| 1235 | if (orig_argc >= 2) |
| 1236 | { |
| 1237 | /* retrieve the filter */ |
| 1238 | filter = *G_stk->get(1); |
| 1239 | } |
| 1240 | else |
| 1241 | { |
| 1242 | /* there's no filter */ |
| 1243 | filter.set_nil(); |
| 1244 | } |
| 1245 | |
| 1246 | /* calculate the hash code */ |
| 1247 | hash = calc_str_hash(vmg_ arg0, strp, strl); |
| 1248 | |
| 1249 | /* enumerate matches for the string */ |
| 1250 | isdef_ctx ctx(vmg_ this, &filter, arg0, strp, strl); |
| 1251 | ctx.rc.init(vmg_ "Dict.isDefined", self, 5, arg0, orig_argc); |
| 1252 | get_ext()->hashtab_->enum_hash_matches(hash, &isdef_cb, &ctx); |
| 1253 | |
| 1254 | /* if we found any matches, return true; otherwise, return nil */ |
| 1255 | val->set_logical(ctx.found); |
| 1256 | |
| 1257 | /* discard arguments */ |
| 1258 | G_stk->discard(orig_argc); |
| 1259 | |
| 1260 | /* success */ |
| 1261 | return TRUE; |
| 1262 | } |
| 1263 | |
| 1264 | /* ------------------------------------------------------------------------ */ |
| 1265 | /* |
| 1266 | * callback context for forEachWord enumerator |
| 1267 | */ |
| 1268 | struct for_each_word_enum_cb_ctx |
| 1269 | { |
| 1270 | /* callback function value */ |
| 1271 | vm_val_t *cb_val; |
| 1272 | |
| 1273 | /* globals */ |
| 1274 | vm_globals *vmg; |
| 1275 | |
| 1276 | /* recursive native caller context */ |
| 1277 | vm_rcdesc rc; |
| 1278 | }; |
| 1279 | |
| 1280 | /* |
| 1281 | * callback for forEachWord enumerator |
| 1282 | */ |
| 1283 | static void for_each_word_enum_cb(void *ctx0, CVmHashEntry *entry0) |
| 1284 | { |
| 1285 | for_each_word_enum_cb_ctx *ctx = (for_each_word_enum_cb_ctx *)ctx0; |
| 1286 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 1287 | size_t list_idx; |
| 1288 | vm_obj_id_t str_obj; |
| 1289 | |
| 1290 | /* set up access to the VM globals through my stashed context */ |
| 1291 | VMGLOB_PTR(ctx->vmg); |
| 1292 | |
| 1293 | /* create a string object for this entry's word string */ |
| 1294 | str_obj = CVmObjString::create(vmg_ FALSE, |
| 1295 | entry->getstr(), entry->getlen()); |
| 1296 | |
| 1297 | /* scan the entry's list of word associations */ |
| 1298 | for (list_idx = 0 ; ; ++list_idx) |
| 1299 | { |
| 1300 | vm_dict_entry *p; |
| 1301 | size_t i; |
| 1302 | |
| 1303 | /* |
| 1304 | * Find the list entry at the current list index. Note that we |
| 1305 | * scan the list on each iteration for the current index because |
| 1306 | * we don't want to keep track of any pointers between iterations; |
| 1307 | * this insulates us from any changes to the underlying list made |
| 1308 | * in the callback. |
| 1309 | */ |
| 1310 | for (i = 0, p = entry->get_head() ; i < list_idx && p != 0 ; |
| 1311 | p = p->nxt_, ++i) ; |
| 1312 | |
| 1313 | /* |
| 1314 | * if we didn't find a list entry at this index, we're done with |
| 1315 | * this hashtable entry |
| 1316 | */ |
| 1317 | if (p == 0) |
| 1318 | break; |
| 1319 | |
| 1320 | /* |
| 1321 | * Invoke the callback on this list entry. The arguments to the |
| 1322 | * callback are the object, the string, and the vocabulary |
| 1323 | * property of the association: |
| 1324 | * |
| 1325 | * func(obj, str, prop) |
| 1326 | * |
| 1327 | * Note that the order of the arguments is the same as for |
| 1328 | * addWord() and removeWord(). |
| 1329 | */ |
| 1330 | |
| 1331 | /* push the vocabulary property of the association */ |
| 1332 | G_stk->push()->set_propid(p->prop_); |
| 1333 | |
| 1334 | /* push the string of the entry */ |
| 1335 | G_stk->push()->set_obj(str_obj); |
| 1336 | |
| 1337 | /* push the object of the association */ |
| 1338 | G_stk->push()->set_obj(p->obj_); |
| 1339 | |
| 1340 | /* invoke the callback */ |
| 1341 | G_interpreter->call_func_ptr(vmg_ ctx->cb_val, 3, &ctx->rc, 0); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * property evaluation - enumerate each word association |
| 1347 | */ |
| 1348 | int CVmObjDict::getp_for_each_word(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1349 | uint *argc) |
| 1350 | { |
| 1351 | static CVmNativeCodeDesc desc(1); |
| 1352 | vm_val_t *cb_val; |
| 1353 | int orig_argc = (argc != 0 ? *argc : 0); |
| 1354 | for_each_word_enum_cb_ctx ctx; |
| 1355 | |
| 1356 | /* check arguments */ |
| 1357 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1358 | return TRUE; |
| 1359 | |
| 1360 | /* get a pointer to the callback value, but leave it on the stack */ |
| 1361 | cb_val = G_stk->get(0); |
| 1362 | |
| 1363 | /* push a self-reference for gc protection while we're working */ |
| 1364 | G_stk->push()->set_obj(self); |
| 1365 | |
| 1366 | /* |
| 1367 | * enumerate the entries in our underlying hashtable - use the "safe" |
| 1368 | * enumerator to ensure that we're insulated from any changes to the |
| 1369 | * hashtable that the callback wants to make |
| 1370 | */ |
| 1371 | ctx.vmg = VMGLOB_ADDR; |
| 1372 | ctx.cb_val = cb_val; |
| 1373 | ctx.rc.init(vmg_ "Dict.forEachWord", self, 6, cb_val, orig_argc); |
| 1374 | get_ext()->hashtab_->safe_enum_entries(for_each_word_enum_cb, &ctx); |
| 1375 | |
| 1376 | /* discard arguments and gc protection */ |
| 1377 | G_stk->discard(2); |
| 1378 | |
| 1379 | /* there's no return value */ |
| 1380 | retval->set_nil(); |
| 1381 | |
| 1382 | /* handled */ |
| 1383 | return TRUE; |
| 1384 | } |
| 1385 | |
| 1386 | /* ------------------------------------------------------------------------ */ |
| 1387 | /* |
| 1388 | * Correction list entry. Each time we match a word in the spelling |
| 1389 | * correction generator, we'll add one of these entries to our list of |
| 1390 | * matched words. This lets us check for words that we've already found |
| 1391 | * (since it's often possible to find a match via more than one editing |
| 1392 | * route). |
| 1393 | */ |
| 1394 | struct corr_word |
| 1395 | { |
| 1396 | void *operator new(size_t siz, size_t strl) |
| 1397 | { |
| 1398 | return t3mallocnew(siz + (strl-1)*sizeof(wchar_t)); |
| 1399 | } |
| 1400 | void operator delete(void *ptr) { t3free(ptr); } |
| 1401 | |
| 1402 | corr_word(wchar_t *str, size_t strl, int dist, int repl, corr_word *nxt) |
| 1403 | { |
| 1404 | memcpy(this->str, str, strl*sizeof(wchar_t)); |
| 1405 | this->strl = strl; |
| 1406 | this->dist = dist; |
| 1407 | this->repl = repl; |
| 1408 | this->nxt = nxt; |
| 1409 | } |
| 1410 | |
| 1411 | /* next in list */ |
| 1412 | corr_word *nxt; |
| 1413 | |
| 1414 | /* best edit distance found for this word */ |
| 1415 | int dist; |
| 1416 | |
| 1417 | /* number of character replacements */ |
| 1418 | int repl; |
| 1419 | |
| 1420 | /* text of the word (we overallocate the structure to make room) */ |
| 1421 | size_t strl; |
| 1422 | wchar_t str[1]; |
| 1423 | }; |
| 1424 | |
| 1425 | /* correction types */ |
| 1426 | enum corr_type { NoChange, Insertion, Deletion, Replacement, Transposition }; |
| 1427 | |
| 1428 | /* |
| 1429 | * Correction stack element. This represents a state in the correction |
| 1430 | * process. |
| 1431 | */ |
| 1432 | struct corr_state |
| 1433 | { |
| 1434 | /* |
| 1435 | * Allocate space. During the state processing, we need to add one |
| 1436 | * character to the last state to form certain subsequent states, so |
| 1437 | * always overallocate our string by one character. This allows us to |
| 1438 | * build the next-state strings in place in our buffer. |
| 1439 | */ |
| 1440 | void *operator new(size_t siz, size_t strl) |
| 1441 | { |
| 1442 | return t3mallocnew(siz + strl*sizeof(wchar_t)); |
| 1443 | } |
| 1444 | void operator delete(void *ptr) { t3free(ptr); } |
| 1445 | |
| 1446 | corr_state(vmdict_TrieNode *root) |
| 1447 | { |
| 1448 | init(0, 0, 0, 0, 0, |
| 1449 | root, NoChange, 0); |
| 1450 | } |
| 1451 | corr_state(const wchar_t *str, size_t strl, int dist, int repl, |
| 1452 | size_t ipos, vmdict_TrieNode *node, corr_type typ, |
| 1453 | corr_state *nxt) |
| 1454 | { |
| 1455 | init(str, strl, dist, repl, ipos, node, typ, nxt); |
| 1456 | } |
| 1457 | |
| 1458 | void init(const wchar_t *str, size_t strl, int dist, int repl, |
| 1459 | size_t ipos, vmdict_TrieNode *node, corr_type typ, |
| 1460 | corr_state *nxt) |
| 1461 | { |
| 1462 | memcpy(this->str, str, strl*sizeof(wchar_t)); |
| 1463 | this->strl = strl; |
| 1464 | this->dist = dist; |
| 1465 | this->repl = repl; |
| 1466 | this->ipos = ipos; |
| 1467 | this->node = node; |
| 1468 | this->typ = typ; |
| 1469 | this->nxt = nxt; |
| 1470 | } |
| 1471 | |
| 1472 | /* next state in the stack */ |
| 1473 | corr_state *nxt; |
| 1474 | |
| 1475 | /* edit distance so far */ |
| 1476 | int dist; |
| 1477 | |
| 1478 | /* number of replacement transitions */ |
| 1479 | int repl; |
| 1480 | |
| 1481 | /* current character index in the input string */ |
| 1482 | size_t ipos; |
| 1483 | |
| 1484 | /* current Trie node in the dictionary */ |
| 1485 | vmdict_TrieNode *node; |
| 1486 | |
| 1487 | /* type of last transition */ |
| 1488 | corr_type typ; |
| 1489 | |
| 1490 | /* the string (we overallocate the structure to make room) */ |
| 1491 | size_t strl; |
| 1492 | wchar_t str[1]; |
| 1493 | }; |
| 1494 | |
| 1495 | /* correction state stack */ |
| 1496 | struct corr_stack |
| 1497 | { |
| 1498 | corr_stack(vmdict_TrieNode *root) |
| 1499 | { |
| 1500 | /* start the stack with an empty initial state */ |
| 1501 | top = new (0) corr_state(root); |
| 1502 | } |
| 1503 | |
| 1504 | int empty() const { return top == 0; } |
| 1505 | |
| 1506 | void push(const wchar_t *str, size_t strl, int dist, int repl, |
| 1507 | size_t ipos, vmdict_TrieNode *node, corr_type typ) |
| 1508 | { |
| 1509 | top = new (strl) corr_state( |
| 1510 | str, strl, dist, repl, ipos, node, typ, top); |
| 1511 | } |
| 1512 | |
| 1513 | corr_state *pop() |
| 1514 | { |
| 1515 | /* retrieve and unlink the top state */ |
| 1516 | corr_state *ret = top; |
| 1517 | top = top->nxt; |
| 1518 | |
| 1519 | /* return the state we unlinked */ |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
| 1523 | /* top of the stack */ |
| 1524 | corr_state *top; |
| 1525 | }; |
| 1526 | |
| 1527 | |
| 1528 | /* |
| 1529 | * property evaluation - get a list of possible corrections for a |
| 1530 | * misspelled word |
| 1531 | */ |
| 1532 | int CVmObjDict::getp_correct(VMG_ vm_obj_id_t self, |
| 1533 | vm_val_t *retval, uint *argc) |
| 1534 | { |
| 1535 | const char *str; |
| 1536 | size_t strl; |
| 1537 | int max_dist; |
| 1538 | static CVmNativeCodeDesc desc(2); |
| 1539 | |
| 1540 | /* check arguments */ |
| 1541 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1542 | return TRUE; |
| 1543 | |
| 1544 | /* pop the word string and maximum edit distance */ |
| 1545 | str = CVmBif::pop_str_val(vmg0_); |
| 1546 | max_dist = CVmBif::pop_int_val(vmg0_); |
| 1547 | |
| 1548 | /* get the string length and character pointer */ |
| 1549 | strl = vmb_get_len(str); |
| 1550 | str += VMB_LEN; |
| 1551 | |
| 1552 | /* for easier processing, convert the string to a wchar_t array */ |
| 1553 | size_t wcnt = utf8_ptr::to_wcharz(0, 0, str, strl); |
| 1554 | wchar_t *wstr = new wchar_t[wcnt+1]; |
| 1555 | utf8_ptr::to_wcharz(wstr, wcnt+1, str, strl); |
| 1556 | |
| 1557 | /* if we have a StringComparator, fetch it */ |
| 1558 | CVmObjStrComp *cmp = 0; |
| 1559 | size_t trunc_len = 0; |
| 1560 | if (get_ext()->comparator_type_ == VMDICT_COMP_STRCOMP) |
| 1561 | { |
| 1562 | /* get the comparator */ |
| 1563 | cmp = (CVmObjStrComp *)get_ext()->comparator_obj_; |
| 1564 | |
| 1565 | /* note the truncation length */ |
| 1566 | trunc_len = cmp->trunc_len(); |
| 1567 | } |
| 1568 | |
| 1569 | /* if we don't have the Trie yet, build it */ |
| 1570 | if (get_ext()->trie_ == 0) |
| 1571 | build_trie(vmg0_); |
| 1572 | |
| 1573 | /* create a stack with the initial empty state */ |
| 1574 | corr_stack stk(get_ext()->trie_); |
| 1575 | |
| 1576 | /* start with an empty result list */ |
| 1577 | corr_word *results = 0; |
| 1578 | |
| 1579 | /* process states until the stack is empty */ |
| 1580 | while (!stk.empty()) |
| 1581 | { |
| 1582 | /* pop the next state */ |
| 1583 | corr_state *s = stk.pop(); |
| 1584 | |
| 1585 | /* |
| 1586 | * Check for an 'accept' state. This is a state where we've |
| 1587 | * exhausted the input word, and we're at a Trie node that has at |
| 1588 | * least one word defined. |
| 1589 | */ |
| 1590 | if (s->node->word_cnt != 0 && s->ipos == wcnt) |
| 1591 | { |
| 1592 | /* |
| 1593 | * We've found a matching word (i.e., a candidate dictionary |
| 1594 | * match for the misspelled input word). Scan the list of |
| 1595 | * candidates we've already found to see if we've already |
| 1596 | * matched this word. |
| 1597 | */ |
| 1598 | corr_word *w; |
| 1599 | for (w = results ; w != 0 ; w = w->nxt) |
| 1600 | { |
| 1601 | if (w->strl == s->strl |
| 1602 | && memcmp(w->str, s->str, s->strl*sizeof(wchar_t)) == 0) |
| 1603 | break; |
| 1604 | } |
| 1605 | |
| 1606 | /* if we didn't find an entry, add a new one */ |
| 1607 | if (w == 0) |
| 1608 | results = w = new (s->strl) corr_word( |
| 1609 | s->str, s->strl, s->dist, s->repl, results); |
| 1610 | |
| 1611 | /* keep the lowest edit distance we've found so far */ |
| 1612 | if (s->dist < w->dist |
| 1613 | || (s->dist == w->dist && s->repl < w->repl)) |
| 1614 | { |
| 1615 | w->dist = s->dist; |
| 1616 | w->repl = s->repl; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * If we have any edit distance left, try an insertion (i.e., the |
| 1622 | * input word has an extra letter relative to the dictionary word). |
| 1623 | * Don't do insertions directly after deletions, though, since |
| 1624 | * they'd just cancel out. |
| 1625 | */ |
| 1626 | if (s->dist < max_dist && s->ipos < wcnt && s->typ != Deletion) |
| 1627 | stk.push(s->str, s->strl, s->dist + 1, s->repl, s->ipos + 1, |
| 1628 | s->node, Insertion); |
| 1629 | |
| 1630 | /* try each possible dictionary transition from here */ |
| 1631 | for (vmdict_TrieNode *chi = s->node->chi ; chi != 0 ; |
| 1632 | chi = chi->nxt) |
| 1633 | { |
| 1634 | /* |
| 1635 | * Build the next-state string. Note that we can just add the |
| 1636 | * character directly to the current state's string buffer, |
| 1637 | * since we always allocate state structures with one extra |
| 1638 | * character of padding for just this purpose. |
| 1639 | */ |
| 1640 | s->str[s->strl] = chi->ch; |
| 1641 | |
| 1642 | /* |
| 1643 | * Check for a match to the current character. If we have a |
| 1644 | * StringComparator, ask the comparator to check the match. |
| 1645 | * Otherwise just check for an exact character match. |
| 1646 | */ |
| 1647 | size_t matchlen = 0; |
| 1648 | if (s->ipos < wcnt) |
| 1649 | { |
| 1650 | if (cmp != 0) |
| 1651 | { |
| 1652 | /* we have a comparator - check it for the match */ |
| 1653 | matchlen = cmp->match_chars( |
| 1654 | wstr + s->ipos, wcnt - s->ipos, chi->ch); |
| 1655 | } |
| 1656 | else |
| 1657 | { |
| 1658 | /* no comparator - do a simple character comparison */ |
| 1659 | if (wstr[s->ipos] == chi->ch) |
| 1660 | matchlen = 1; |
| 1661 | } |
| 1662 | |
| 1663 | /* if we have a match, push a state for it */ |
| 1664 | if (matchlen != 0) |
| 1665 | stk.push(s->str, s->strl + 1, s->dist, s->repl, |
| 1666 | s->ipos + matchlen, chi, NoChange); |
| 1667 | } |
| 1668 | else |
| 1669 | { |
| 1670 | /* |
| 1671 | * we've reached the end of the input; but if the current |
| 1672 | * edit string is at least the truncation length in the |
| 1673 | * string comparator, consider this a character match as |
| 1674 | * well |
| 1675 | */ |
| 1676 | if (trunc_len != 0 |
| 1677 | && s->strl >= trunc_len |
| 1678 | && s->ipos >= trunc_len) |
| 1679 | stk.push(s->str, s->strl + 1, s->dist, s->repl, |
| 1680 | s->ipos, chi, NoChange); |
| 1681 | } |
| 1682 | |
| 1683 | /* if we have any edit distance remaining, try corrections */ |
| 1684 | if (s->dist < max_dist) |
| 1685 | { |
| 1686 | /* try a replaced letter */ |
| 1687 | if (s->ipos < wcnt && matchlen == 0) |
| 1688 | stk.push(s->str, s->strl + 1, s->dist + 1, s->repl + 1, |
| 1689 | s->ipos + 1, chi, Replacement); |
| 1690 | |
| 1691 | /* try a deletion (i.e., the input is missing a character) */ |
| 1692 | if (s->typ != Insertion) |
| 1693 | stk.push(s->str, s->strl + 1, s->dist + 1, s->repl, |
| 1694 | s->ipos, chi, Deletion); |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * if we just did a replacement edit, check to see if it's |
| 1699 | * actually a transposition |
| 1700 | */ |
| 1701 | if (s->typ == Replacement |
| 1702 | && s->ipos > 0 && s->ipos < wcnt |
| 1703 | && wstr[s->ipos - 1] == chi->ch |
| 1704 | && wstr[s->ipos] == s->str[s->strl - 1]) |
| 1705 | stk.push(s->str, s->strl + 1, s->dist, s->repl - 1, |
| 1706 | s->ipos + 1, chi, Transposition); |
| 1707 | } |
| 1708 | |
| 1709 | /* we've finished processing the current state */ |
| 1710 | delete s; |
| 1711 | } |
| 1712 | |
| 1713 | /* done with the wchar_t version of the word */ |
| 1714 | delete [] wstr; |
| 1715 | |
| 1716 | /* |
| 1717 | * Count up the number of result entries. Only count words with a |
| 1718 | * non-zero edit distance - words with a zero edit distance are already |
| 1719 | * matches for the input word, so we can't consider them to be |
| 1720 | * corrections for it. |
| 1721 | */ |
| 1722 | corr_word *r; |
| 1723 | int i; |
| 1724 | for (r = results, i = 0 ; r != 0 ; r = r->nxt) |
| 1725 | { |
| 1726 | /* if this word has a non-zero edit distance, count it */ |
| 1727 | if (r->dist != 0) |
| 1728 | ++i; |
| 1729 | } |
| 1730 | |
| 1731 | /* allocate a List to hold the results */ |
| 1732 | vm_obj_id_t lst_id = CVmObjList::create(vmg_ FALSE, i); |
| 1733 | CVmObjList *lst = (CVmObjList *)vm_objp(vmg_ lst_id); |
| 1734 | lst->cons_clear(); |
| 1735 | |
| 1736 | /* push it for gc protection */ |
| 1737 | G_stk->push()->set_obj(lst_id); |
| 1738 | |
| 1739 | /* create a List [word, distance] for each result */ |
| 1740 | for (r = results, i = 0 ; r != 0 ; r = r->nxt) |
| 1741 | { |
| 1742 | vm_val_t val; |
| 1743 | |
| 1744 | /* |
| 1745 | * If this word has a zero edit distance, skip it. A word with a |
| 1746 | * zero edit distance is already a match for the original, so it |
| 1747 | * can't be considered a correction. |
| 1748 | */ |
| 1749 | if (r->dist == 0) |
| 1750 | continue; |
| 1751 | |
| 1752 | /* create the sublist */ |
| 1753 | vm_obj_id_t sub_id = CVmObjList::create(vmg_ FALSE, 3); |
| 1754 | CVmObjList *sub = (CVmObjList *)vm_objp(vmg_ sub_id); |
| 1755 | |
| 1756 | /* add it to the main list */ |
| 1757 | val.set_obj(sub_id); |
| 1758 | lst->cons_set_element(i, &val); |
| 1759 | |
| 1760 | /* measure the utf8 space needed for the string */ |
| 1761 | utf8_ptr rp; |
| 1762 | size_t rplen = rp.setwchars(r->str, r->strl, 0); |
| 1763 | |
| 1764 | /* create the word string */ |
| 1765 | vm_obj_id_t lstr_id = CVmObjString::create(vmg_ FALSE, rplen); |
| 1766 | CVmObjString *lstr = (CVmObjString *)vm_objp(vmg_ lstr_id); |
| 1767 | |
| 1768 | /* encode it */ |
| 1769 | rp.set(lstr->cons_get_buf()); |
| 1770 | rp.setwchars(r->str, r->strl, rplen); |
| 1771 | |
| 1772 | /* add it to the sublist */ |
| 1773 | val.set_obj(lstr_id); |
| 1774 | sub->cons_set_element(0, &val); |
| 1775 | |
| 1776 | /* add the edit distance */ |
| 1777 | val.set_int(r->dist); |
| 1778 | sub->cons_set_element(1, &val); |
| 1779 | |
| 1780 | /* add the character replacement count */ |
| 1781 | val.set_int(r->repl); |
| 1782 | sub->cons_set_element(2, &val); |
| 1783 | |
| 1784 | /* count it */ |
| 1785 | ++i; |
| 1786 | } |
| 1787 | |
| 1788 | /* delete the result list */ |
| 1789 | while (results != 0) |
| 1790 | { |
| 1791 | /* unlink this element */ |
| 1792 | r = results; |
| 1793 | results = results->nxt; |
| 1794 | |
| 1795 | /* delete it */ |
| 1796 | delete r; |
| 1797 | } |
| 1798 | |
| 1799 | /* return the list value */ |
| 1800 | retval->set_obj(lst_id); |
| 1801 | |
| 1802 | /* done with the gc protection */ |
| 1803 | G_stk->discard(); |
| 1804 | |
| 1805 | /* handled */ |
| 1806 | return TRUE; |
| 1807 | } |
| 1808 | |
| 1809 | /* ------------------------------------------------------------------------ */ |
| 1810 | /* |
| 1811 | * Create an undo record |
| 1812 | */ |
| 1813 | dict_undo_rec *CVmObjDict::alloc_undo_rec(dict_undo_action action, |
| 1814 | const char *txt, size_t len) |
| 1815 | { |
| 1816 | size_t alloc_size; |
| 1817 | dict_undo_rec *rec; |
| 1818 | |
| 1819 | /* |
| 1820 | * compute the allocation size - start with the structure size, and |
| 1821 | * add in the length we need to store the string if one is present |
| 1822 | */ |
| 1823 | alloc_size = sizeof(dict_undo_rec) - 1; |
| 1824 | if (txt != 0) |
| 1825 | alloc_size += len; |
| 1826 | |
| 1827 | /* allocate the record */ |
| 1828 | rec = (dict_undo_rec *)t3malloc(alloc_size); |
| 1829 | |
| 1830 | /* set the action */ |
| 1831 | rec->action = action; |
| 1832 | |
| 1833 | /* if there's a word, copy the text */ |
| 1834 | if (txt != 0) |
| 1835 | memcpy(rec->txt, txt, len); |
| 1836 | |
| 1837 | /* save the word length */ |
| 1838 | rec->len = len; |
| 1839 | |
| 1840 | /* presume we won't store an object or property */ |
| 1841 | rec->obj = VM_INVALID_OBJ; |
| 1842 | rec->prop = VM_INVALID_PROP; |
| 1843 | |
| 1844 | /* return the new record */ |
| 1845 | return rec; |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * add an undo record |
| 1850 | */ |
| 1851 | void CVmObjDict::add_undo_rec(VMG_ vm_obj_id_t self, dict_undo_rec *rec) |
| 1852 | { |
| 1853 | vm_val_t val; |
| 1854 | |
| 1855 | /* add the record with an empty value */ |
| 1856 | val.set_empty(); |
| 1857 | if (!G_undo->add_new_record_ptr_key(vmg_ self, rec, &val)) |
| 1858 | { |
| 1859 | /* |
| 1860 | * we didn't add an undo record, so our extra undo information |
| 1861 | * isn't actually going to be stored in the undo system - hence |
| 1862 | * we must delete our extra information |
| 1863 | */ |
| 1864 | t3free(rec); |
| 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * apply undo |
| 1870 | */ |
| 1871 | void CVmObjDict::apply_undo(VMG_ CVmUndoRecord *undo_rec) |
| 1872 | { |
| 1873 | if (undo_rec->id.ptrval != 0) |
| 1874 | { |
| 1875 | dict_undo_rec *rec; |
| 1876 | |
| 1877 | /* get my undo record */ |
| 1878 | rec = (dict_undo_rec *)undo_rec->id.ptrval; |
| 1879 | |
| 1880 | /* take the appropriate action */ |
| 1881 | switch(rec->action) |
| 1882 | { |
| 1883 | case DICT_UNDO_ADD: |
| 1884 | /* we added the word, so we must now delete it */ |
| 1885 | del_hash_entry(vmg_ rec->txt, rec->len, rec->obj, rec->prop); |
| 1886 | break; |
| 1887 | |
| 1888 | case DICT_UNDO_DEL: |
| 1889 | /* we deleted the word, so now we must add it */ |
| 1890 | add_hash_entry(vmg_ rec->txt, rec->len, TRUE, |
| 1891 | rec->obj, rec->prop, FALSE); |
| 1892 | break; |
| 1893 | |
| 1894 | case DICT_UNDO_COMPARATOR: |
| 1895 | /* we changed the comparator object, so change it back */ |
| 1896 | set_comparator(vmg_ rec->obj); |
| 1897 | break; |
| 1898 | } |
| 1899 | |
| 1900 | /* discard my record */ |
| 1901 | t3free(rec); |
| 1902 | |
| 1903 | /* clear the pointer in the undo record so we know it's gone */ |
| 1904 | undo_rec->id.ptrval = 0; |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * discard extra undo information |
| 1910 | */ |
| 1911 | void CVmObjDict::discard_undo(VMG_ CVmUndoRecord *rec) |
| 1912 | { |
| 1913 | /* delete our extra information record */ |
| 1914 | if (rec->id.ptrval != 0) |
| 1915 | { |
| 1916 | /* free the record */ |
| 1917 | t3free((dict_undo_rec *)rec->id.ptrval); |
| 1918 | |
| 1919 | /* clear the pointer so we know it's gone */ |
| 1920 | rec->id.ptrval = 0; |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * mark references in an undo record |
| 1926 | */ |
| 1927 | void CVmObjDict::mark_undo_ref(VMG_ CVmUndoRecord *undo_rec) |
| 1928 | { |
| 1929 | /* check our private record if we have one */ |
| 1930 | if (undo_rec->id.ptrval != 0) |
| 1931 | { |
| 1932 | dict_undo_rec *rec; |
| 1933 | |
| 1934 | /* get my undo record */ |
| 1935 | rec = (dict_undo_rec *)undo_rec->id.ptrval; |
| 1936 | |
| 1937 | /* take the appropriate action */ |
| 1938 | switch(rec->action) |
| 1939 | { |
| 1940 | case DICT_UNDO_COMPARATOR: |
| 1941 | /* the 'obj' entry is the old comparator object */ |
| 1942 | G_obj_table->mark_all_refs(rec->obj, VMOBJ_REACHABLE); |
| 1943 | break; |
| 1944 | |
| 1945 | case DICT_UNDO_ADD: |
| 1946 | case DICT_UNDO_DEL: |
| 1947 | /* |
| 1948 | * these actions use only weak references, so there's nothing |
| 1949 | * to do here |
| 1950 | */ |
| 1951 | break; |
| 1952 | } |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * remove stale weak references from an undo record |
| 1958 | */ |
| 1959 | void CVmObjDict::remove_stale_undo_weak_ref(VMG_ CVmUndoRecord *undo_rec) |
| 1960 | { |
| 1961 | /* check our private record if we have one */ |
| 1962 | if (undo_rec->id.ptrval != 0) |
| 1963 | { |
| 1964 | dict_undo_rec *rec; |
| 1965 | |
| 1966 | /* get my undo record */ |
| 1967 | rec = (dict_undo_rec *)undo_rec->id.ptrval; |
| 1968 | |
| 1969 | /* take the appropriate action */ |
| 1970 | switch(rec->action) |
| 1971 | { |
| 1972 | case DICT_UNDO_ADD: |
| 1973 | case DICT_UNDO_DEL: |
| 1974 | /* check to see if our object is being deleted */ |
| 1975 | if (rec->obj != VM_INVALID_OBJ |
| 1976 | && G_obj_table->is_obj_deletable(rec->obj)) |
| 1977 | { |
| 1978 | /* |
| 1979 | * the object is being deleted - since we only weakly |
| 1980 | * reference our objects, we must forget about this |
| 1981 | * object now, which means we can forget this entire |
| 1982 | * record |
| 1983 | */ |
| 1984 | t3free(rec); |
| 1985 | undo_rec->id.ptrval = 0; |
| 1986 | } |
| 1987 | break; |
| 1988 | |
| 1989 | case DICT_UNDO_COMPARATOR: |
| 1990 | /* this action does not use weak references */ |
| 1991 | break; |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | /* ------------------------------------------------------------------------ */ |
| 1997 | /* |
| 1998 | * callback context for weak reference function |
| 1999 | */ |
| 2000 | struct enum_hashtab_ctx |
| 2001 | { |
| 2002 | /* global context pointer */ |
| 2003 | vm_globals *vmg; |
| 2004 | |
| 2005 | /* dictionary object */ |
| 2006 | CVmObjDict *dict; |
| 2007 | }; |
| 2008 | |
| 2009 | /* |
| 2010 | * Mark references |
| 2011 | */ |
| 2012 | void CVmObjDict::mark_refs(VMG_ uint state) |
| 2013 | { |
| 2014 | /* if I have a comparator object, mark it as referenced */ |
| 2015 | if (get_ext()->comparator_ != VM_INVALID_OBJ) |
| 2016 | G_obj_table->mark_all_refs(get_ext()->comparator_, state); |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * Remove weak references that have become stale. For each object for |
| 2021 | * which we have a weak reference, check to see if the object is marked |
| 2022 | * for deletion; if so, remove our reference to the object. |
| 2023 | */ |
| 2024 | void CVmObjDict::remove_stale_weak_refs(VMG0_) |
| 2025 | { |
| 2026 | enum_hashtab_ctx ctx; |
| 2027 | |
| 2028 | /* iterate over our hash table */ |
| 2029 | ctx.vmg = VMGLOB_ADDR; |
| 2030 | ctx.dict = this; |
| 2031 | get_ext()->hashtab_->enum_entries(&remove_weak_ref_cb, &ctx); |
| 2032 | } |
| 2033 | |
| 2034 | /* |
| 2035 | * enumeration callback to eliminate stale weak references |
| 2036 | */ |
| 2037 | void CVmObjDict::remove_weak_ref_cb(void *ctx0, CVmHashEntry *entry0) |
| 2038 | { |
| 2039 | enum_hashtab_ctx *ctx = (enum_hashtab_ctx *)ctx0; |
| 2040 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 2041 | vm_dict_entry *cur; |
| 2042 | vm_dict_entry *nxt; |
| 2043 | |
| 2044 | /* set up for global access through the context */ |
| 2045 | VMGLOB_PTR(ctx->vmg); |
| 2046 | |
| 2047 | /* scan the entry's list */ |
| 2048 | for (cur = entry->get_head() ; cur != 0 ; cur = nxt) |
| 2049 | { |
| 2050 | /* remember the next entry in case we delete this one */ |
| 2051 | nxt = cur->nxt_; |
| 2052 | |
| 2053 | /* check to see if this object is free - if so, remove this record */ |
| 2054 | if (G_obj_table->is_obj_deletable(cur->obj_)) |
| 2055 | { |
| 2056 | /* if we have a trie, delete the trie entry */ |
| 2057 | if (ctx->dict->get_ext()->trie_ != 0) |
| 2058 | ctx->dict->get_ext()->trie_->del_word( |
| 2059 | entry->getstr(), entry->getlen()); |
| 2060 | |
| 2061 | /* delete the entry */ |
| 2062 | entry->del_entry(ctx->dict->get_ext()->hashtab_, |
| 2063 | cur->obj_, cur->prop_); |
| 2064 | } |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | |
| 2069 | /* ------------------------------------------------------------------------ */ |
| 2070 | /* |
| 2071 | * load from an image file |
| 2072 | */ |
| 2073 | void CVmObjDict::load_from_image(VMG_ vm_obj_id_t self, |
| 2074 | const char *ptr, size_t siz) |
| 2075 | { |
| 2076 | /* remember where our image data comes from */ |
| 2077 | get_ext()->image_data_ = ptr; |
| 2078 | get_ext()->image_data_size_ = siz; |
| 2079 | |
| 2080 | /* build the hash table from the image data */ |
| 2081 | build_hash_from_image(vmg0_); |
| 2082 | |
| 2083 | /* |
| 2084 | * register for post-load initialization, as we might need to rebuild |
| 2085 | * our hash table once we have access to the comparator object |
| 2086 | */ |
| 2087 | G_obj_table->request_post_load_init(self); |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Post-load initialization |
| 2092 | */ |
| 2093 | void CVmObjDict::post_load_init(VMG_ vm_obj_id_t self) |
| 2094 | { |
| 2095 | vm_obj_id_t comp; |
| 2096 | |
| 2097 | /* |
| 2098 | * Rebuild the hash table with the comparator, if we have one. When |
| 2099 | * we load, reset, or restore, we build a tentative hash table with no |
| 2100 | * comparator (i.e., the default exact literal comparator), because we |
| 2101 | * can't assume we have access to the comparator object (as it might |
| 2102 | * be loaded after us). So, we register for post-load initialization, |
| 2103 | * and then we go back and re-install the comparator for real, |
| 2104 | * rebuilding the hash table with the actual comparator. |
| 2105 | */ |
| 2106 | if ((comp = get_ext()->comparator_) != VM_INVALID_OBJ) |
| 2107 | { |
| 2108 | /* ensure the comparator object is initialized */ |
| 2109 | G_obj_table->ensure_post_load_init(vmg_ comp); |
| 2110 | |
| 2111 | /* set the comparator object type, now that it's loaded */ |
| 2112 | set_comparator_type(vmg_ comp); |
| 2113 | |
| 2114 | /* |
| 2115 | * force a rebuild the hash table, so that we build it with the |
| 2116 | * comparator properly installed |
| 2117 | */ |
| 2118 | create_hash_table(vmg0_); |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | /* |
| 2123 | * Build the hash table from the image data |
| 2124 | */ |
| 2125 | void CVmObjDict::build_hash_from_image(VMG0_) |
| 2126 | { |
| 2127 | uint cnt; |
| 2128 | uint i; |
| 2129 | const char *p; |
| 2130 | const char *endp; |
| 2131 | vm_obj_id_t comp; |
| 2132 | |
| 2133 | /* start reading at the start of the image data */ |
| 2134 | p = get_ext()->image_data_; |
| 2135 | endp = p + get_ext()->image_data_size_; |
| 2136 | |
| 2137 | /* read the comparator object ID */ |
| 2138 | comp = (vm_obj_id_t)t3rp4u(p); |
| 2139 | p += 4; |
| 2140 | |
| 2141 | /* |
| 2142 | * Do NOT install the actual comparator object at this point, but |
| 2143 | * simply build the table tentatively with a nil comparator. We can't |
| 2144 | * assume that the comparator object has been loaded yet (as it might |
| 2145 | * be loaded after us), so we cannot use it to build the hash table. |
| 2146 | * We have to do something with the data, though, so build the hash |
| 2147 | * table tentatively with no comparator; we'll rebuild it again at |
| 2148 | * post-load-init time with the real comparator. |
| 2149 | * |
| 2150 | * (This isn't as inefficient as it sounds, as we retain all of the |
| 2151 | * original hash table entries when we rebuild the table. All we end |
| 2152 | * up doing is scanning the entries again to recompute their new hash |
| 2153 | * values, and reallocating the hash table object itself.) |
| 2154 | */ |
| 2155 | get_ext()->comparator_ = VM_INVALID_OBJ; |
| 2156 | set_comparator_type(vmg_ VM_INVALID_OBJ); |
| 2157 | |
| 2158 | /* create the new hash table */ |
| 2159 | create_hash_table(vmg0_); |
| 2160 | |
| 2161 | /* read the entry count */ |
| 2162 | cnt = osrp2(p); |
| 2163 | p += 2; |
| 2164 | |
| 2165 | /* scan the entries */ |
| 2166 | for (i = 0 ; p < endp && i < cnt ; ++i) |
| 2167 | { |
| 2168 | vm_obj_id_t obj; |
| 2169 | vm_prop_id_t prop; |
| 2170 | uint key_len; |
| 2171 | uint item_cnt; |
| 2172 | const char *key; |
| 2173 | CVmHashEntryDict *entry; |
| 2174 | char key_buf[256]; |
| 2175 | char *keyp; |
| 2176 | size_t key_rem; |
| 2177 | |
| 2178 | /* read the key length */ |
| 2179 | key_len = *(unsigned char *)p++; |
| 2180 | |
| 2181 | /* remember the key pointer */ |
| 2182 | key = p; |
| 2183 | p += key_len; |
| 2184 | |
| 2185 | /* copy the key to our buffer for decoding */ |
| 2186 | memcpy(key_buf, key, key_len); |
| 2187 | |
| 2188 | /* decode the key */ |
| 2189 | for (keyp = key_buf, key_rem = key_len ; key_rem != 0 ; |
| 2190 | --key_rem, ++keyp) |
| 2191 | *keyp ^= 0xBD; |
| 2192 | |
| 2193 | /* read the key from the decoded buffer now */ |
| 2194 | key = key_buf; |
| 2195 | |
| 2196 | /* read the item count */ |
| 2197 | item_cnt = osrp2(p); |
| 2198 | p += 2; |
| 2199 | |
| 2200 | /* find an existing entry for this key */ |
| 2201 | entry = (CVmHashEntryDict *)get_ext()->hashtab_->find(key, key_len); |
| 2202 | |
| 2203 | /* if there's no existing entry, add one */ |
| 2204 | if (entry == 0) |
| 2205 | { |
| 2206 | /* create a new entry */ |
| 2207 | entry = new CVmHashEntryDict(key, key_len, TRUE, TRUE); |
| 2208 | |
| 2209 | /* add it to the table */ |
| 2210 | get_ext()->hashtab_->add(entry); |
| 2211 | } |
| 2212 | |
| 2213 | /* read the items */ |
| 2214 | for ( ; item_cnt != 0 ; --item_cnt) |
| 2215 | { |
| 2216 | /* read the object ID and property ID */ |
| 2217 | obj = (vm_obj_id_t)t3rp4u(p); |
| 2218 | prop = (vm_prop_id_t)osrp2(p + 4); |
| 2219 | |
| 2220 | /* |
| 2221 | * add the entry - this entry is from the image file, so |
| 2222 | * mark it as such |
| 2223 | */ |
| 2224 | entry->add_entry(obj, prop, TRUE); |
| 2225 | |
| 2226 | /* move on to the next item */ |
| 2227 | p += 6; |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | /* |
| 2232 | * Now that we're done building the table, remember the comparator. We |
| 2233 | * can't set the type yet, because the object might not be loaded yet - |
| 2234 | * defer this until post-load initialization time. |
| 2235 | */ |
| 2236 | get_ext()->comparator_ = comp; |
| 2237 | } |
| 2238 | |
| 2239 | /* ------------------------------------------------------------------------ */ |
| 2240 | /* |
| 2241 | * Callback context for trie-building enumeration |
| 2242 | */ |
| 2243 | struct trie_cb_ctx |
| 2244 | { |
| 2245 | vmdict_TrieNode *t; |
| 2246 | }; |
| 2247 | |
| 2248 | /* hash table enumeration callback for building the trie */ |
| 2249 | static void trie_cb(void *ctx0, CVmHashEntry *entry0) |
| 2250 | { |
| 2251 | /* get the context and hash entries, properly cast */ |
| 2252 | trie_cb_ctx *ctx = (trie_cb_ctx *)ctx0; |
| 2253 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 2254 | |
| 2255 | /* add this word to the trie */ |
| 2256 | ctx->t->add_word(entry->getstr(), entry->getlen()); |
| 2257 | } |
| 2258 | |
| 2259 | /* |
| 2260 | * Build the Trie from the hash table |
| 2261 | */ |
| 2262 | void CVmObjDict::build_trie(VMG0_) |
| 2263 | { |
| 2264 | trie_cb_ctx ctx; |
| 2265 | |
| 2266 | /* if we already have a trie, there's nothing to do */ |
| 2267 | if (get_ext()->trie_ != 0) |
| 2268 | return; |
| 2269 | |
| 2270 | /* create the root node */ |
| 2271 | ctx.t = get_ext()->trie_ = new vmdict_TrieNode(0, 0); |
| 2272 | |
| 2273 | /* enumerate the hash table to build the trie */ |
| 2274 | get_ext()->hashtab_->enum_entries(trie_cb, &ctx); |
| 2275 | } |
| 2276 | |
| 2277 | /* ------------------------------------------------------------------------ */ |
| 2278 | /* |
| 2279 | * Trie operations |
| 2280 | */ |
| 2281 | |
| 2282 | /* add a word to the Trie */ |
| 2283 | void vmdict_TrieNode::add_word(const char *str, size_t len) |
| 2284 | { |
| 2285 | vmdict_TrieNode *n; |
| 2286 | utf8_ptr p((char *)str); |
| 2287 | |
| 2288 | /* scan the string and walk the Trie */ |
| 2289 | for (n = this ; len != 0 ; p.inc(&len)) |
| 2290 | { |
| 2291 | /* get this character */ |
| 2292 | wchar_t ch = p.getch(); |
| 2293 | |
| 2294 | /* find the child node for this letter */ |
| 2295 | vmdict_TrieNode *chi; |
| 2296 | for (chi = n->chi ; chi != 0 && chi->ch != ch ; chi = chi->nxt) ; |
| 2297 | |
| 2298 | /* if there's no existing child for this letter, add one */ |
| 2299 | if (chi == 0) |
| 2300 | n->chi = chi = new vmdict_TrieNode(n->chi, ch); |
| 2301 | |
| 2302 | /* advance to this child node */ |
| 2303 | n = chi; |
| 2304 | } |
| 2305 | |
| 2306 | /* 'n' is the final node for this word, so count the word there */ |
| 2307 | n->word_cnt += 1; |
| 2308 | } |
| 2309 | |
| 2310 | /* find a node for a given word */ |
| 2311 | vmdict_TrieNode *vmdict_TrieNode::find_word(const char *str, size_t len) |
| 2312 | { |
| 2313 | vmdict_TrieNode *n; |
| 2314 | utf8_ptr p((char *)str); |
| 2315 | |
| 2316 | /* scan the string and walk the Trie */ |
| 2317 | for (n = this ; len != 0 ; p.inc(&len)) |
| 2318 | { |
| 2319 | /* get this character */ |
| 2320 | wchar_t ch = p.getch(); |
| 2321 | |
| 2322 | /* find the child node for this letter */ |
| 2323 | vmdict_TrieNode *chi; |
| 2324 | for (chi = n->chi ; chi != 0 && chi->ch != ch ; chi = chi->nxt) ; |
| 2325 | |
| 2326 | /* if we didn't find a child, the word isn't in the trie */ |
| 2327 | if (chi == 0) |
| 2328 | return 0; |
| 2329 | |
| 2330 | /* advance to this child node */ |
| 2331 | n = chi; |
| 2332 | } |
| 2333 | |
| 2334 | /* we've reached the final node for the word - return it */ |
| 2335 | return n; |
| 2336 | } |
| 2337 | |
| 2338 | /* delete a word from the Trie */ |
| 2339 | void vmdict_TrieNode::del_word(const char *str, size_t len) |
| 2340 | { |
| 2341 | /* find the final node for this word */ |
| 2342 | vmdict_TrieNode *n = find_word(str, len); |
| 2343 | |
| 2344 | /* if we found it, decrement its word count */ |
| 2345 | if (n != 0 && n->word_cnt != 0) |
| 2346 | n->word_cnt -= 1; |
| 2347 | } |
| 2348 | |
| 2349 | |
| 2350 | /* ------------------------------------------------------------------------ */ |
| 2351 | /* |
| 2352 | * Add an entry |
| 2353 | */ |
| 2354 | int CVmObjDict::add_hash_entry(VMG_ const char *p, size_t len, int copy, |
| 2355 | vm_obj_id_t obj, vm_prop_id_t prop, |
| 2356 | int from_image) |
| 2357 | { |
| 2358 | CVmHashEntryDict *entry; |
| 2359 | |
| 2360 | /* find an existing entry for this key */ |
| 2361 | entry = (CVmHashEntryDict *)get_ext()->hashtab_->find(p, len); |
| 2362 | |
| 2363 | /* if we didn't find an entry, add one */ |
| 2364 | if (entry == 0) |
| 2365 | { |
| 2366 | /* create an entry */ |
| 2367 | entry = new CVmHashEntryDict(p, len, copy, from_image); |
| 2368 | |
| 2369 | /* add it to the table */ |
| 2370 | get_ext()->hashtab_->add(entry); |
| 2371 | |
| 2372 | /* if we have a trie, add it to the trie */ |
| 2373 | if (get_ext()->trie_ != 0) |
| 2374 | get_ext()->trie_->add_word(p, len); |
| 2375 | } |
| 2376 | |
| 2377 | /* add the obj/prop to the entry's item list */ |
| 2378 | return entry->add_entry(obj, prop, from_image); |
| 2379 | } |
| 2380 | |
| 2381 | /* ------------------------------------------------------------------------ */ |
| 2382 | /* |
| 2383 | * delete an entry |
| 2384 | */ |
| 2385 | int CVmObjDict::del_hash_entry(VMG_ const char *str, size_t len, |
| 2386 | vm_obj_id_t obj, vm_prop_id_t voc_prop) |
| 2387 | { |
| 2388 | CVmHashEntryDict *entry; |
| 2389 | |
| 2390 | /* find the hash table entry for the word */ |
| 2391 | entry = (CVmHashEntryDict *)get_ext()->hashtab_->find(str, len); |
| 2392 | |
| 2393 | /* if we found it, delete the obj/prop entry */ |
| 2394 | if (entry != 0) |
| 2395 | { |
| 2396 | /* if we have a trie, delete the trie entry */ |
| 2397 | if (get_ext()->trie_ != 0) |
| 2398 | get_ext()->trie_->del_word(str, len); |
| 2399 | |
| 2400 | /* delete the hash table entry */ |
| 2401 | return entry->del_entry(get_ext()->hashtab_, obj, voc_prop); |
| 2402 | } |
| 2403 | |
| 2404 | /* we didn't find anything to delete */ |
| 2405 | return FALSE; |
| 2406 | } |
| 2407 | |
| 2408 | /* ------------------------------------------------------------------------ */ |
| 2409 | /* |
| 2410 | * restore to image file state |
| 2411 | */ |
| 2412 | void CVmObjDict::reset_to_image(VMG_ vm_obj_id_t self) |
| 2413 | { |
| 2414 | /* delete the hash table */ |
| 2415 | if (get_ext()->hashtab_ != 0) |
| 2416 | { |
| 2417 | /* delete it */ |
| 2418 | delete get_ext()->hashtab_; |
| 2419 | |
| 2420 | /* forget it */ |
| 2421 | get_ext()->hashtab_ = 0; |
| 2422 | } |
| 2423 | |
| 2424 | /* delete the Trie */ |
| 2425 | if (get_ext()->trie_ != 0) |
| 2426 | { |
| 2427 | delete get_ext()->trie_; |
| 2428 | get_ext()->trie_ = 0; |
| 2429 | } |
| 2430 | |
| 2431 | /* rebuild the hash table from the image file data */ |
| 2432 | build_hash_from_image(vmg0_); |
| 2433 | |
| 2434 | /* |
| 2435 | * register for post-load initialization, as we might need to rebuild |
| 2436 | * our hash table once we have access to the comparator object |
| 2437 | */ |
| 2438 | G_obj_table->request_post_load_init(self); |
| 2439 | } |
| 2440 | |
| 2441 | /* ------------------------------------------------------------------------ */ |
| 2442 | /* |
| 2443 | * determine if the object has been changed since it was loaded |
| 2444 | */ |
| 2445 | int CVmObjDict::is_changed_since_load() const |
| 2446 | { |
| 2447 | /* |
| 2448 | * return true if our 'modified' flag is true - if it is, we must have |
| 2449 | * been changed since we were loaded |
| 2450 | */ |
| 2451 | return (get_ext()->modified_ != 0); |
| 2452 | } |
| 2453 | |
| 2454 | /* ------------------------------------------------------------------------ */ |
| 2455 | /* |
| 2456 | * save-file enumeration context |
| 2457 | */ |
| 2458 | struct save_file_ctx |
| 2459 | { |
| 2460 | /* file object */ |
| 2461 | CVmFile *fp; |
| 2462 | |
| 2463 | /* entry count */ |
| 2464 | ulong cnt; |
| 2465 | |
| 2466 | /* globals */ |
| 2467 | vm_globals *vmg; |
| 2468 | }; |
| 2469 | |
| 2470 | /* |
| 2471 | * save to a file |
| 2472 | */ |
| 2473 | void CVmObjDict::save_to_file(VMG_ CVmFile *fp) |
| 2474 | { |
| 2475 | long cnt_pos; |
| 2476 | long end_pos; |
| 2477 | save_file_ctx ctx; |
| 2478 | |
| 2479 | /* write the current comparator object */ |
| 2480 | fp->write_uint4(get_ext()->comparator_); |
| 2481 | |
| 2482 | /* remember the starting seek position and write a placeholder count */ |
| 2483 | cnt_pos = fp->get_pos(); |
| 2484 | fp->write_uint4(0); |
| 2485 | |
| 2486 | /* enumerate the entries to write out each one */ |
| 2487 | ctx.fp = fp; |
| 2488 | ctx.cnt = 0; |
| 2489 | ctx.vmg = VMGLOB_ADDR; |
| 2490 | get_ext()->hashtab_->enum_entries(&save_file_cb, &ctx); |
| 2491 | |
| 2492 | /* remember our position for a moment */ |
| 2493 | end_pos = fp->get_pos(); |
| 2494 | |
| 2495 | /* go back and write out the symbol count prefix */ |
| 2496 | fp->set_pos(cnt_pos); |
| 2497 | fp->write_uint4(ctx.cnt); |
| 2498 | |
| 2499 | /* seek back to the end */ |
| 2500 | fp->set_pos(end_pos); |
| 2501 | } |
| 2502 | |
| 2503 | /* |
| 2504 | * save file enumeration callback |
| 2505 | */ |
| 2506 | void CVmObjDict::save_file_cb(void *ctx0, CVmHashEntry *entry0) |
| 2507 | { |
| 2508 | save_file_ctx *ctx = (save_file_ctx *)ctx0; |
| 2509 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 2510 | vm_dict_entry *cur; |
| 2511 | uint cnt; |
| 2512 | |
| 2513 | /* set up global access */ |
| 2514 | VMGLOB_PTR(ctx->vmg); |
| 2515 | |
| 2516 | /* write out this entry's name */ |
| 2517 | ctx->fp->write_uint2(entry->getlen()); |
| 2518 | ctx->fp->write_bytes(entry->getstr(), entry->getlen()); |
| 2519 | |
| 2520 | /* count the items in this entry's list */ |
| 2521 | for (cnt = 0, cur = entry->get_head() ; cur != 0 ; cur = cur->nxt_) |
| 2522 | { |
| 2523 | /* |
| 2524 | * if the referenced object is persistent, count this item; |
| 2525 | * don't count items that refer to non-persistent items, because |
| 2526 | * these objects will not be saved to the file and hence will |
| 2527 | * not be present when the saved state is restored |
| 2528 | */ |
| 2529 | if (G_obj_table->is_obj_persistent(cur->obj_)) |
| 2530 | ++cnt; |
| 2531 | } |
| 2532 | |
| 2533 | /* |
| 2534 | * if there are no saveable items for this entry, do not write out |
| 2535 | * the entry at all |
| 2536 | */ |
| 2537 | if (cnt == 0) |
| 2538 | return; |
| 2539 | |
| 2540 | /* count this entry */ |
| 2541 | ++ctx->cnt; |
| 2542 | |
| 2543 | /* write the item count */ |
| 2544 | ctx->fp->write_uint2(cnt); |
| 2545 | |
| 2546 | /* write out each item in this entry's list */ |
| 2547 | for (cur = entry->get_head() ; cur != 0 ; cur = cur->nxt_) |
| 2548 | { |
| 2549 | /* if this item's object is persistent, write out the item */ |
| 2550 | if (G_obj_table->is_obj_persistent(cur->obj_)) |
| 2551 | { |
| 2552 | /* write out the object and property for this entry */ |
| 2553 | ctx->fp->write_uint4((long)cur->obj_); |
| 2554 | ctx->fp->write_uint2((int)cur->prop_); |
| 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | /* ------------------------------------------------------------------------ */ |
| 2560 | /* |
| 2561 | * restore from a file |
| 2562 | */ |
| 2563 | void CVmObjDict::restore_from_file(VMG_ vm_obj_id_t self, |
| 2564 | CVmFile *fp, CVmObjFixup *fixups) |
| 2565 | { |
| 2566 | ulong cnt; |
| 2567 | vm_obj_id_t comp; |
| 2568 | |
| 2569 | /* delete the old hash table if we have one */ |
| 2570 | if (get_ext()->hashtab_ != 0) |
| 2571 | { |
| 2572 | delete get_ext()->hashtab_; |
| 2573 | get_ext()->hashtab_ = 0; |
| 2574 | } |
| 2575 | |
| 2576 | /* delete the Trie if we have one */ |
| 2577 | if (get_ext()->trie_ != 0) |
| 2578 | { |
| 2579 | delete get_ext()->trie_; |
| 2580 | get_ext()->trie_ = 0; |
| 2581 | } |
| 2582 | |
| 2583 | /* |
| 2584 | * Read the comparator and fix it up to the new object numbering |
| 2585 | * scheme, but do not install it (as it might not be loaded yet). |
| 2586 | */ |
| 2587 | comp = fixups->get_new_id(vmg_ (vm_obj_id_t)fp->read_uint4()); |
| 2588 | |
| 2589 | /* create the new, empty hash table */ |
| 2590 | create_hash_table(vmg0_); |
| 2591 | |
| 2592 | /* read the number of symbols */ |
| 2593 | cnt = fp->read_uint4(); |
| 2594 | |
| 2595 | /* read the symbols */ |
| 2596 | for ( ; cnt != 0 ; --cnt) |
| 2597 | { |
| 2598 | uint len; |
| 2599 | uint read_len; |
| 2600 | char buf[256]; |
| 2601 | uint item_cnt; |
| 2602 | |
| 2603 | /* read the symbol length */ |
| 2604 | len = fp->read_uint2(); |
| 2605 | |
| 2606 | /* limit the reading to our buffer size */ |
| 2607 | read_len = len; |
| 2608 | if (read_len > sizeof(buf)) |
| 2609 | read_len = sizeof(buf); |
| 2610 | |
| 2611 | /* read the string */ |
| 2612 | fp->read_bytes(buf, read_len); |
| 2613 | |
| 2614 | /* skip any extra data */ |
| 2615 | if (len > read_len) |
| 2616 | fp->set_pos(fp->get_pos() + len - read_len); |
| 2617 | |
| 2618 | /* read the item count */ |
| 2619 | item_cnt = fp->read_uint2(); |
| 2620 | |
| 2621 | /* read the items */ |
| 2622 | for ( ; item_cnt != 0 ; --item_cnt) |
| 2623 | { |
| 2624 | vm_obj_id_t obj; |
| 2625 | vm_prop_id_t prop; |
| 2626 | |
| 2627 | /* read the object and property ID's */ |
| 2628 | obj = (vm_obj_id_t)fp->read_uint4(); |
| 2629 | prop = (vm_prop_id_t)fp->read_uint2(); |
| 2630 | |
| 2631 | /* translate the object ID through the fixup table */ |
| 2632 | obj = fixups->get_new_id(vmg_ obj); |
| 2633 | |
| 2634 | /* add the entry, if it refers to a valid object */ |
| 2635 | if (obj != VM_INVALID_OBJ) |
| 2636 | add_hash_entry(vmg_ buf, len, TRUE, obj, prop, FALSE); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | /* |
| 2641 | * install the comparator (we can't set its type yet, though, because |
| 2642 | * the object might not be loaded yet) |
| 2643 | */ |
| 2644 | get_ext()->comparator_ = comp; |
| 2645 | set_comparator_type(vmg_ VM_INVALID_OBJ); |
| 2646 | |
| 2647 | /* |
| 2648 | * If we had to restore it, we'll have to save it if the current state |
| 2649 | * is later saved, even if we don't modify it again. The fact that |
| 2650 | * this version was saved before means this version has to be saved |
| 2651 | * from now on. |
| 2652 | */ |
| 2653 | get_ext()->modified_ = TRUE; |
| 2654 | |
| 2655 | /* |
| 2656 | * register for post-load initialization, so that we can rebuild the |
| 2657 | * hash table with the actual comparator if necessary |
| 2658 | */ |
| 2659 | G_obj_table->request_post_load_init(self); |
| 2660 | } |
| 2661 | |
| 2662 | /* ------------------------------------------------------------------------ */ |
| 2663 | /* |
| 2664 | * impedence-matcher callback context |
| 2665 | */ |
| 2666 | struct enum_word_props_ctx |
| 2667 | { |
| 2668 | /* client callback to invoke */ |
| 2669 | void (*cb_func)(VMG_ void *ctx, vm_prop_id_t prop, |
| 2670 | const vm_val_t *match_val); |
| 2671 | |
| 2672 | /* string value to match */ |
| 2673 | const vm_val_t *strval; |
| 2674 | const char *strp; |
| 2675 | size_t strl; |
| 2676 | |
| 2677 | /* client callback context */ |
| 2678 | void *cb_ctx; |
| 2679 | |
| 2680 | /* globals */ |
| 2681 | vm_globals *globals; |
| 2682 | |
| 2683 | /* the dictionary object we're searching */ |
| 2684 | CVmObjDict *dict; |
| 2685 | }; |
| 2686 | |
| 2687 | /* |
| 2688 | * enum_word_props hash table enumeration callback |
| 2689 | */ |
| 2690 | void CVmObjDict::enum_word_props_cb(void *ctx0, CVmHashEntry *entry0) |
| 2691 | { |
| 2692 | enum_word_props_ctx *ctx = (enum_word_props_ctx *)ctx0; |
| 2693 | CVmHashEntryDict *entry = (CVmHashEntryDict *)entry0; |
| 2694 | vm_val_t match_val; |
| 2695 | VMGLOB_PTR(ctx->globals); |
| 2696 | |
| 2697 | /* if this entry matches the search string, process it */ |
| 2698 | if (ctx->dict->match_strings(vmg_ ctx->strval, ctx->strp, ctx->strl, |
| 2699 | entry->getstr(), entry->getlen(), |
| 2700 | &match_val)) |
| 2701 | { |
| 2702 | vm_dict_entry *cur; |
| 2703 | |
| 2704 | /* process the items under this entry */ |
| 2705 | for (cur = entry->get_head() ; cur != 0 ; cur = cur->nxt_) |
| 2706 | { |
| 2707 | /* invoke the callback */ |
| 2708 | (*ctx->cb_func)(vmg_ ctx->cb_ctx, cur->prop_, &match_val); |
| 2709 | } |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | /* |
| 2714 | * Enumerate the properties for which a word is defined |
| 2715 | */ |
| 2716 | void CVmObjDict::enum_word_props(VMG_ |
| 2717 | void (*cb_func)(VMG_ void *, vm_prop_id_t, |
| 2718 | const vm_val_t *), |
| 2719 | void *cb_ctx, const vm_val_t *strval, |
| 2720 | const char *strp, size_t strl) |
| 2721 | { |
| 2722 | enum_word_props_ctx ctx; |
| 2723 | unsigned int hash; |
| 2724 | |
| 2725 | /* set up the enumeration callback context */ |
| 2726 | ctx.strval = strval; |
| 2727 | ctx.strp = strp; |
| 2728 | ctx.strl = strl; |
| 2729 | ctx.cb_func = cb_func; |
| 2730 | ctx.cb_ctx = cb_ctx; |
| 2731 | ctx.globals = VMGLOB_ADDR; |
| 2732 | ctx.dict = this; |
| 2733 | |
| 2734 | /* calculate the hash code */ |
| 2735 | hash = calc_str_hash(vmg_ strval, strp, strl); |
| 2736 | |
| 2737 | /* enumerate the matches */ |
| 2738 | get_ext()->hashtab_->enum_hash_matches(hash, &enum_word_props_cb, &ctx); |
| 2739 | } |
| 2740 |