| 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 | vmiter.cpp - T3 iterator metaclass |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 04/22/00 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include "vmtype.h" |
| 25 | #include "vmobj.h" |
| 26 | #include "vmiter.h" |
| 27 | #include "vmglob.h" |
| 28 | #include "vmmeta.h" |
| 29 | #include "vmstack.h" |
| 30 | #include "vmundo.h" |
| 31 | #include "vmlst.h" |
| 32 | #include "vmfile.h" |
| 33 | |
| 34 | |
| 35 | /* ------------------------------------------------------------------------ */ |
| 36 | /* |
| 37 | * Base Iterator metaclass |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | * statics |
| 42 | */ |
| 43 | |
| 44 | /* metaclass registration object */ |
| 45 | static CVmMetaclassIter iter_metaclass_reg_obj; |
| 46 | CVmMetaclass *CVmObjIter::metaclass_reg_ = &iter_metaclass_reg_obj; |
| 47 | |
| 48 | /* function table */ |
| 49 | int (CVmObjIter:: |
| 50 | *CVmObjIter::func_table_[])(VMG_ vm_obj_id_t self, |
| 51 | vm_val_t *retval, uint *argc) = |
| 52 | { |
| 53 | &CVmObjIter::getp_undef, |
| 54 | &CVmObjIter::getp_get_next, |
| 55 | &CVmObjIter::getp_is_next_avail, |
| 56 | &CVmObjIter::getp_reset_iter, |
| 57 | &CVmObjIter::getp_get_cur_key, |
| 58 | &CVmObjIter::getp_get_cur_val |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * get a property |
| 63 | */ |
| 64 | int CVmObjIter::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 65 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 66 | uint *argc) |
| 67 | { |
| 68 | /* translate the property into a function vector index */ |
| 69 | uint func_idx = G_meta_table->prop_to_vector_idx( |
| 70 | metaclass_reg_->get_reg_idx(), prop); |
| 71 | |
| 72 | /* call the appropriate function */ |
| 73 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 74 | { |
| 75 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 76 | return TRUE; |
| 77 | } |
| 78 | |
| 79 | /* inherit default handling from the base object class */ |
| 80 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 81 | } |
| 82 | |
| 83 | /* ------------------------------------------------------------------------ */ |
| 84 | /* |
| 85 | * Indexed Iterator metaclass |
| 86 | */ |
| 87 | |
| 88 | /* |
| 89 | * statics |
| 90 | */ |
| 91 | |
| 92 | /* metaclass registration object */ |
| 93 | static CVmMetaclassIterIdx idx_metaclass_reg_obj; |
| 94 | CVmMetaclass *CVmObjIterIdx::metaclass_reg_ = &idx_metaclass_reg_obj; |
| 95 | |
| 96 | /* create a list with no initial contents */ |
| 97 | vm_obj_id_t CVmObjIterIdx::create_for_coll(VMG_ const vm_val_t *coll, |
| 98 | long first_valid_index, |
| 99 | long last_valid_index) |
| 100 | { |
| 101 | vm_obj_id_t id; |
| 102 | |
| 103 | /* push the collection object reference for gc protection */ |
| 104 | G_stk->push(coll); |
| 105 | |
| 106 | /* create a non-root-set object */ |
| 107 | id = vm_new_id(vmg_ FALSE, TRUE, FALSE); |
| 108 | |
| 109 | /* instantiate the iterator */ |
| 110 | new (vmg_ id) CVmObjIterIdx(vmg_ coll, first_valid_index, |
| 111 | last_valid_index); |
| 112 | |
| 113 | /* done with the gc protection */ |
| 114 | G_stk->discard(); |
| 115 | |
| 116 | /* return the new object's ID */ |
| 117 | return id; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * constructor |
| 122 | */ |
| 123 | CVmObjIterIdx::CVmObjIterIdx(VMG_ const vm_val_t *coll, |
| 124 | long first_valid_index, long last_valid_index) |
| 125 | { |
| 126 | /* allocate space for our extension data */ |
| 127 | ext_ = (char *)G_mem->get_var_heap() |
| 128 | ->alloc_mem(VMOBJITERIDX_EXT_SIZE, this); |
| 129 | |
| 130 | /* save the collection value */ |
| 131 | vmb_put_dh(ext_, coll); |
| 132 | |
| 133 | /* |
| 134 | * set the current index to the first index minus 1, so that we start |
| 135 | * with the first element when we make our first call to getNext() |
| 136 | */ |
| 137 | set_cur_index_no_undo(first_valid_index - 1); |
| 138 | |
| 139 | /* remember the first and last valid index values */ |
| 140 | set_first_valid(first_valid_index); |
| 141 | set_last_valid(last_valid_index); |
| 142 | |
| 143 | /* clear the flags */ |
| 144 | set_flags(0); |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * notify of deletion |
| 149 | */ |
| 150 | void CVmObjIterIdx::notify_delete(VMG_ int) |
| 151 | { |
| 152 | /* free our extension */ |
| 153 | if (ext_ != 0) |
| 154 | G_mem->get_var_heap()->free_mem(ext_); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * property evaluator - get the current item's value |
| 159 | */ |
| 160 | int CVmObjIterIdx::getp_get_cur_val(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 161 | uint *argc) |
| 162 | { |
| 163 | long idx; |
| 164 | static CVmNativeCodeDesc desc(0); |
| 165 | |
| 166 | /* check arguments */ |
| 167 | if (get_prop_check_argc(retval, argc, &desc)) |
| 168 | return TRUE; |
| 169 | |
| 170 | /* get the current index */ |
| 171 | idx = get_cur_index(); |
| 172 | |
| 173 | /* if the current value is out of range, throw an error */ |
| 174 | if (idx < 1 || idx > get_last_valid()) |
| 175 | err_throw(VMERR_OUT_OF_RANGE); |
| 176 | |
| 177 | /* retrieve the value for this index */ |
| 178 | get_indexed_val(vmg_ idx, retval); |
| 179 | |
| 180 | /* handled */ |
| 181 | return TRUE; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * property evaluator - get the current item's key |
| 186 | */ |
| 187 | int CVmObjIterIdx::getp_get_cur_key(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 188 | uint *argc) |
| 189 | { |
| 190 | long idx; |
| 191 | static CVmNativeCodeDesc desc(0); |
| 192 | |
| 193 | /* check arguments */ |
| 194 | if (get_prop_check_argc(retval, argc, &desc)) |
| 195 | return TRUE; |
| 196 | |
| 197 | /* get the current index */ |
| 198 | idx = get_cur_index(); |
| 199 | |
| 200 | /* if the current value is out of range, throw an error */ |
| 201 | if (idx < 1 || idx > get_last_valid()) |
| 202 | err_throw(VMERR_OUT_OF_RANGE); |
| 203 | |
| 204 | /* return the index */ |
| 205 | retval->set_int(idx); |
| 206 | |
| 207 | /* handled */ |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * property evaluator - get the next item |
| 213 | */ |
| 214 | int CVmObjIterIdx::getp_get_next(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 215 | uint *argc) |
| 216 | { |
| 217 | /* check arguments */ |
| 218 | static CVmNativeCodeDesc desc(0); |
| 219 | if (get_prop_check_argc(retval, argc, &desc)) |
| 220 | return TRUE; |
| 221 | |
| 222 | /* get the next index, which is one higher than the current index */ |
| 223 | long idx = get_cur_index() + 1; |
| 224 | |
| 225 | /* if the current value is out of range, throw an error */ |
| 226 | if (idx > get_last_valid()) |
| 227 | err_throw(VMERR_OUT_OF_RANGE); |
| 228 | |
| 229 | /* retrieve the value */ |
| 230 | get_indexed_val(vmg_ idx, retval); |
| 231 | |
| 232 | /* save the current index in our internal state */ |
| 233 | set_cur_index(vmg_ self, idx); |
| 234 | |
| 235 | /* handled */ |
| 236 | return TRUE; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * get the next iteration item |
| 241 | */ |
| 242 | int CVmObjIterIdx::iter_next(VMG_ vm_obj_id_t self, vm_val_t *val) |
| 243 | { |
| 244 | /* get the next index, which is one higher than the current index */ |
| 245 | long idx = get_cur_index() + 1; |
| 246 | |
| 247 | /* if the current value is out of range, throw an error */ |
| 248 | if (idx <= get_last_valid()) |
| 249 | { |
| 250 | /* retrieve the value */ |
| 251 | get_indexed_val(vmg_ idx, val); |
| 252 | |
| 253 | /* save the current index in our internal state */ |
| 254 | set_cur_index(vmg_ self, idx); |
| 255 | |
| 256 | /* we got a value */ |
| 257 | return TRUE; |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | /* no more values available */ |
| 262 | return FALSE; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /* |
| 268 | * Retrieve an indexed value from my collection |
| 269 | */ |
| 270 | void CVmObjIterIdx::get_indexed_val(VMG_ long idx, vm_val_t *retval) |
| 271 | { |
| 272 | vm_val_t coll; |
| 273 | vm_val_t idx_val; |
| 274 | |
| 275 | /* get my collection value and the next index value */ |
| 276 | get_coll_val(&coll); |
| 277 | |
| 278 | /* check to see if we have an object or a constant list */ |
| 279 | switch(coll.typ) |
| 280 | { |
| 281 | case VM_LIST: |
| 282 | /* it's a constant list - index the constant value */ |
| 283 | CVmObjList::index_list(vmg_ retval, coll.get_as_list(vmg0_), idx); |
| 284 | break; |
| 285 | |
| 286 | case VM_OBJ: |
| 287 | /* it's an object - index it */ |
| 288 | idx_val.set_int(idx); |
| 289 | vm_objp(vmg_ coll.val.obj) |
| 290 | ->index_val_ov(vmg_ retval, coll.val.obj, &idx_val); |
| 291 | break; |
| 292 | |
| 293 | default: |
| 294 | /* |
| 295 | * Anything else is an error. We really should never be able to |
| 296 | * get here, since the only way to instantiate an iterator should |
| 297 | * be via the collection object's createIter() method; so if we |
| 298 | * get here it must be an internal error, hence we could probably |
| 299 | * assert failure here. Nonetheless, just throw an error, since |
| 300 | * this will make for a more pleasant indication of the problem. |
| 301 | */ |
| 302 | err_throw(VMERR_CANNOT_INDEX_TYPE); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * property evaluator - is next value available? |
| 309 | */ |
| 310 | int CVmObjIterIdx::getp_is_next_avail(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 311 | uint *argc) |
| 312 | { |
| 313 | /* check arguments */ |
| 314 | static CVmNativeCodeDesc desc(0); |
| 315 | if (get_prop_check_argc(retval, argc, &desc)) |
| 316 | return TRUE; |
| 317 | |
| 318 | /* |
| 319 | * if the current index plus one is less than or equal to the last |
| 320 | * valid index, another item is available |
| 321 | */ |
| 322 | retval->set_logical(get_cur_index() + 1 <= get_last_valid()); |
| 323 | |
| 324 | /* handled */ |
| 325 | return TRUE; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * property evaluator - reset to first item |
| 330 | */ |
| 331 | int CVmObjIterIdx::getp_reset_iter(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 332 | uint *argc) |
| 333 | { |
| 334 | static CVmNativeCodeDesc desc(0); |
| 335 | |
| 336 | /* check arguments */ |
| 337 | if (get_prop_check_argc(retval, argc, &desc)) |
| 338 | return TRUE; |
| 339 | |
| 340 | /* set the index to the first valid index minus one */ |
| 341 | set_cur_index(vmg_ self, get_first_valid() - 1); |
| 342 | |
| 343 | /* no return value */ |
| 344 | retval->set_nil(); |
| 345 | |
| 346 | /* handled */ |
| 347 | return TRUE; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Set the current index value, saving undo if necessary |
| 352 | */ |
| 353 | void CVmObjIterIdx::set_cur_index(VMG_ vm_obj_id_t self, long idx) |
| 354 | { |
| 355 | /* save undo if necessary */ |
| 356 | if (G_undo != 0 && !(get_flags() & VMOBJITERIDX_UNDO)) |
| 357 | { |
| 358 | vm_val_t dummy; |
| 359 | |
| 360 | /* |
| 361 | * Add the undo record. Note that the only information we need |
| 362 | * to store is the index value, so we can store this as the key |
| 363 | * value - supply a dummy payload, since we have no use for it. |
| 364 | */ |
| 365 | dummy.set_nil(); |
| 366 | G_undo->add_new_record_int_key(vmg_ self, get_cur_index(), &dummy); |
| 367 | |
| 368 | /* |
| 369 | * set the undo bit so we don't save redundant undo for this |
| 370 | * savepoint |
| 371 | */ |
| 372 | set_flags(get_flags() | VMOBJITERIDX_UNDO); |
| 373 | } |
| 374 | |
| 375 | /* set the index */ |
| 376 | set_cur_index_no_undo(idx); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * apply undo |
| 381 | */ |
| 382 | void CVmObjIterIdx::apply_undo(VMG_ CVmUndoRecord *rec) |
| 383 | { |
| 384 | /* |
| 385 | * the integer key in the undo record is my saved index value (and |
| 386 | * is the only thing in an indexed iterator that can ever change) |
| 387 | */ |
| 388 | set_cur_index_no_undo(rec->id.intval); |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * mark references |
| 393 | */ |
| 394 | void CVmObjIterIdx::mark_refs(VMG_ uint state) |
| 395 | { |
| 396 | vm_val_t coll; |
| 397 | |
| 398 | /* if my collection is an object, mark it as referenced */ |
| 399 | get_coll_val(&coll); |
| 400 | if (coll.typ == VM_OBJ) |
| 401 | G_obj_table->mark_all_refs(coll.val.obj, state); |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * load from an image file |
| 406 | */ |
| 407 | void CVmObjIterIdx::load_from_image(VMG_ vm_obj_id_t self, |
| 408 | const char *ptr, size_t siz) |
| 409 | { |
| 410 | /* if we already have memory allocated, free it */ |
| 411 | if (ext_ != 0) |
| 412 | { |
| 413 | G_mem->get_var_heap()->free_mem(ext_); |
| 414 | ext_ = 0; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Allocate a new extension. Make sure it's at least as large as |
| 419 | * the current standard extension size. |
| 420 | */ |
| 421 | ext_ = (char *)G_mem->get_var_heap() |
| 422 | ->alloc_mem(siz < VMOBJITERIDX_EXT_SIZE |
| 423 | ? VMOBJITERIDX_EXT_SIZE : siz, this); |
| 424 | |
| 425 | /* copy the image data to our extension */ |
| 426 | memcpy(ext_, ptr, siz); |
| 427 | |
| 428 | /* clear the undo flag */ |
| 429 | set_flags(get_flags() & ~VMOBJITERIDX_UNDO); |
| 430 | |
| 431 | /* save our image data pointer, so we can use it for reloading */ |
| 432 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * reload from an image file |
| 437 | */ |
| 438 | void CVmObjIterIdx::reload_from_image(VMG_ vm_obj_id_t, |
| 439 | const char *ptr, size_t siz) |
| 440 | { |
| 441 | /* copy the image data over our data */ |
| 442 | memcpy(ext_, ptr, siz); |
| 443 | |
| 444 | /* clear the undo flag */ |
| 445 | set_flags(get_flags() & ~VMOBJITERIDX_UNDO); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | /* |
| 450 | * save |
| 451 | */ |
| 452 | void CVmObjIterIdx::save_to_file(VMG_ CVmFile *fp) |
| 453 | { |
| 454 | /* write my extension */ |
| 455 | fp->write_bytes(ext_, VMOBJITERIDX_EXT_SIZE); |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * restore |
| 460 | */ |
| 461 | void CVmObjIterIdx::restore_from_file(VMG_ vm_obj_id_t, |
| 462 | CVmFile *fp, CVmObjFixup *fixups) |
| 463 | { |
| 464 | /* free any existing extension */ |
| 465 | if (ext_ != 0) |
| 466 | { |
| 467 | G_mem->get_var_heap()->free_mem(ext_); |
| 468 | ext_ = 0; |
| 469 | } |
| 470 | |
| 471 | /* allocate a new extension */ |
| 472 | ext_ = (char *)G_mem->get_var_heap() |
| 473 | ->alloc_mem(VMOBJITERIDX_EXT_SIZE, this); |
| 474 | |
| 475 | /* read my extension */ |
| 476 | fp->read_bytes(ext_, VMOBJITERIDX_EXT_SIZE); |
| 477 | |
| 478 | /* fix up my collection object reference */ |
| 479 | fixups->fix_dh(vmg_ ext_); |
| 480 | } |
| 481 |