cfad47cfa3/tads3/vmcoll.cpp

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