| 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 | vmcoll.cpp - collection 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 "vmcoll.h" |
| 27 | #include "vmglob.h" |
| 28 | #include "vmerr.h" |
| 29 | #include "vmerrnum.h" |
| 30 | #include "vmmeta.h" |
| 31 | #include "vmstack.h" |
| 32 | #include "vmrun.h" |
| 33 | |
| 34 | |
| 35 | /* ------------------------------------------------------------------------ */ |
| 36 | /* |
| 37 | * statics |
| 38 | */ |
| 39 | |
| 40 | /* metaclass registration object */ |
| 41 | static CVmMetaclassCollection metaclass_reg_obj; |
| 42 | CVmMetaclass *CVmObjCollection::metaclass_reg_ = &metaclass_reg_obj; |
| 43 | |
| 44 | /* function table */ |
| 45 | int (CVmObjCollection:: |
| 46 | *CVmObjCollection::func_table_[])(VMG_ vm_val_t *retval, |
| 47 | const vm_val_t *self_val, |
| 48 | uint *argc) = |
| 49 | { |
| 50 | &CVmObjCollection::getp_undef, |
| 51 | &CVmObjCollection::getp_create_iter, |
| 52 | &CVmObjCollection::getp_create_live_iter |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | |
| 57 | /* ------------------------------------------------------------------------ */ |
| 58 | /* |
| 59 | * Get a property |
| 60 | */ |
| 61 | int CVmObjCollection::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 62 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 63 | uint *argc) |
| 64 | { |
| 65 | vm_val_t self_val; |
| 66 | |
| 67 | /* set up the 'self' value */ |
| 68 | self_val.set_obj(self); |
| 69 | |
| 70 | /* use the constant collection version */ |
| 71 | if (const_get_coll_prop(vmg_ prop, retval, &self_val, source_obj, argc)) |
| 72 | { |
| 73 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 74 | return TRUE; |
| 75 | } |
| 76 | |
| 77 | /* inherit default handling */ |
| 78 | return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Get a property of a constant value |
| 83 | */ |
| 84 | int CVmObjCollection::const_get_coll_prop(VMG_ vm_prop_id_t prop, |
| 85 | vm_val_t *retval, |
| 86 | const vm_val_t *self_val, |
| 87 | vm_obj_id_t *src_obj, |
| 88 | uint *argc) |
| 89 | { |
| 90 | uint func_idx; |
| 91 | |
| 92 | /* presume no source object */ |
| 93 | *src_obj = VM_INVALID_OBJ; |
| 94 | |
| 95 | /* translate the property index to an index into our function table */ |
| 96 | func_idx = G_meta_table |
| 97 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 98 | |
| 99 | /* call the appropriate function */ |
| 100 | if ((this->*func_table_[func_idx])(vmg_ retval, self_val, argc)) |
| 101 | return TRUE; |
| 102 | |
| 103 | /* not found */ |
| 104 | return FALSE; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Create an iterator |
| 109 | */ |
| 110 | int CVmObjCollection::getp_create_iter(VMG_ vm_val_t *retval, |
| 111 | const vm_val_t *self_val, uint *argc) |
| 112 | { |
| 113 | static CVmNativeCodeDesc desc(0); |
| 114 | |
| 115 | /* check arguments */ |
| 116 | if (get_prop_check_argc(retval, argc, &desc)) |
| 117 | return TRUE; |
| 118 | |
| 119 | /* push a self-reference for gc protection */ |
| 120 | G_stk->push(self_val); |
| 121 | |
| 122 | /* create the iterator */ |
| 123 | new_iterator(vmg_ retval, self_val); |
| 124 | |
| 125 | /* discard the gc protection */ |
| 126 | G_stk->discard(); |
| 127 | |
| 128 | /* handled */ |
| 129 | return TRUE; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Create a live iterator |
| 134 | */ |
| 135 | int CVmObjCollection::getp_create_live_iter(VMG_ vm_val_t *retval, |
| 136 | const vm_val_t *self_val, |
| 137 | uint *argc) |
| 138 | { |
| 139 | static CVmNativeCodeDesc desc(0); |
| 140 | |
| 141 | /* check arguments */ |
| 142 | if (get_prop_check_argc(retval, argc, &desc)) |
| 143 | return TRUE; |
| 144 | |
| 145 | /* push a self-reference for gc protection */ |
| 146 | G_stk->push(self_val); |
| 147 | |
| 148 | /* create the "live" iterator */ |
| 149 | new_live_iterator(vmg_ retval, self_val); |
| 150 | |
| 151 | /* discard the gc protection */ |
| 152 | G_stk->discard(); |
| 153 | |
| 154 | /* handled */ |
| 155 | return TRUE; |
| 156 | } |
| 157 |