Debian Codegraph @

terps/tads/tads3/vmintcls.h

raw symbols SCIP
1 /* $Header$ */
2
3 /* Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved. */
4 /*
5 Name
6 vmintcls.h - T3 metaclass - intrinsic class
7 Function
8
9 Notes
10
11 Modified
12 03/08/00 MJRoberts - Creation
13 */
14
15 #ifndef VMINTCLS_H
16 #define VMINTCLS_H
17
18
19 #include <stdlib.h>
20
21 #include "vmtype.h"
22 #include "vmobj.h"
23 #include "vmglob.h"
24
25 /*
26 * An IntrinsicClass object represents the class of an instance of an
27 * intrinsic class. For example, if we create a BigNumber instance, then
28 * ask for its class, the result is the IntrinsicClass object associated
29 * with the BigNumber intrinsic class:
30 *
31 * Each metaclass in the metaclass dependency table will be associated with
32 * an IntrinsicClass object.
33 *
34 * The image file format for an IntrinsicClass object consists of the
35 * following:
36 *
37 * UINT2 byte_count (currently, this is always 8)
38 *. UINT2 metaclass_dependency_table_index
39 *. UINT4 modifier_object_id
40 *. DATAHOLDER class_state
41 *
42 * The 'class_state' entry is a generic data holder value for use by the
43 * intrinsic class for storing static class state. We save and restore
44 * this value automatically and can keep undo information for it. This can
45 * be used to implement static class-level setprop/getprop to store special
46 * values associated with the class object.
47 */
48
49 /* intrinsic class extension */
50 struct vm_intcls_ext
51 {
52 /* metaclass dependency table index */
53 int meta_idx;
54
55 /* modifier object */
56 vm_obj_id_t mod_obj;
57
58 /* class state */
59 vm_val_t class_state;
60
61 /* has the class state value changed since load time? */
62 int changed;
63 };
64
65 /*
66 * intrinsic class object
67 */
68 class CVmObjClass: public CVmObject
69 {
70 friend class CVmMetaclassClass;
71
72 public:
73 /* metaclass registration object */
74 static class CVmMetaclass *metaclass_reg_;
75 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
76
77 /* am I of the given metaclass? */
78 virtual int is_of_metaclass(class CVmMetaclass *meta) const
79 {
80 /* try my own metaclass and my base class */
81 return (meta == metaclass_reg_
82 || CVmObject::is_of_metaclass(meta));
83 }
84
85 /* is the given object an IntrinsicClass object? */
86 static int is_intcls_obj(VMG_ vm_obj_id_t obj)
87 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
88
89 /*
90 * get my metaclass registration table index - this can be compared to
91 * the metaclass_reg_ element for a given C++ intrinsic class
92 * implementation to determine if this intrinsic class object is the
93 * intrinsic class object for a given C++ intrinsic class
94 */
95 uint get_meta_idx() const { return get_ext()->meta_idx; }
96
97 /* get my metaclass table entry */
98 struct vm_meta_entry_t *get_meta_entry(VMG0_) const;
99
100 /*
101 * Get the class state value. This is for use by the class
102 * implementation to store static (class) state.
103 */
104 const vm_val_t *get_class_state() { return &get_ext()->class_state; }
105
106 /* Set the class state value, without saving undo. */
107 void set_class_state(const vm_val_t *val)
108 {
109 get_ext()->class_state = *val;
110 get_ext()->changed = TRUE;
111 }
112
113 /*
114 * Set the class state value with undo. This is unimplemented for now,
115 * since no one needs it. In principle it could be useful to be able
116 * to store a scalar value in the class state slot, in which case we'd
117 * need to have special code here in order to undo changes. So far,
118 * though, we always store an object reference in class_state, such as
119 * a Vector or TadsObject, since we need to be able to store more than
120 * just a single scalar value. Since all of the state data are then
121 * stored within those container objects, and the container classes we
122 * use all handle undo themselves, there's no need for any undo action
123 * at the class_state level.
124 */
125 // Unimplemented!
126 void set_class_state_undo(VMG_ class CVmUndo *undo, const vm_val_t *val);
127
128 /* create dynamically using stack arguments */
129 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
130 uint argc);
131
132 /* create for a given dependency table index */
133 static vm_obj_id_t create_dyn(VMG_ uint meta_idx);
134
135 /*
136 * call a static property - we don't have any of our own, so simply
137 * "inherit" the base class handling
138 */
139 static int call_stat_prop(VMG_ vm_val_t *result,
140 const uchar **pc_ptr, uint *argc,
141 vm_prop_id_t prop);
142
143 /* determine if I'm an instance of the given object */
144 virtual int is_instance_of(VMG_ vm_obj_id_t obj);
145
146 /* get superclass information */
147 int get_superclass_count(VMG_ vm_obj_id_t self) const;
148 vm_obj_id_t get_superclass(VMG_ vm_obj_id_t self, int idx) const;
149
150 /*
151 * Determine if this is a class object. All intrinsic class objects
152 * indicate true.
153 */
154 virtual int is_class_object(VMG_ vm_obj_id_t /*self*/) const
155 { return TRUE; }
156
157 /* reserve constant data */
158 virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper,
159 vm_obj_id_t self)
160 {
161 /*
162 * we reference no other data and cannot be converted to constant
163 * data ourselves, so there's nothing to do here
164 */
165 }
166
167 /* convert to constant data */
168 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
169 vm_obj_id_t self)
170 {
171 /*
172 * we reference no other data and cannot be converted to constant
173 * data ourselves, so there's nothing to do here
174 */
175 }
176
177 /* create with no initial contents */
178 static vm_obj_id_t create(VMG_ int in_root_set);
179
180 /* notify of deletion */
181 void notify_delete(VMG_ int in_root_set);
182
183 /* set a property */
184 void set_prop(VMG_ class CVmUndo *undo,
185 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
186
187 /* get a property */
188 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
189 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
190
191 /* build a list of my properties */
192 void build_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval);
193
194 /* undo operations - classes are immutable and hence keep no undo */
195 void notify_new_savept() { }
196 void apply_undo(VMG_ struct CVmUndoRecord *) { }
197 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
198 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
199
200 /* mark references */
201 void mark_refs(VMG_ uint state);
202
203 /* remove weak references - we have none */
204 void remove_stale_weak_refs(VMG0_) { }
205
206 /* have we changed since load time? */
207 int is_changed_since_load() const { return get_ext()->changed; }
208
209 /* load/reload from an image file */
210 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
211 void reload_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
212
213 /* rebuild for image file */
214 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
215
216 /* save to a file */
217 void save_to_file(VMG_ class CVmFile *fp);
218
219 /* restore from a file */
220 void restore_from_file(VMG_ vm_obj_id_t self,
221 class CVmFile *fp, class CVmObjFixup *fixups);
222
223 /* get the user modifier object for the intrinsic class */
224 vm_obj_id_t get_mod_obj() const { return get_ext()->mod_obj; }
225
226 /*
227 * find the intrinsic class for the given modifier object, searching
228 * myself and my intrinsic superclasses
229 */
230 vm_obj_id_t find_mod_src_obj(VMG_ vm_obj_id_t self, vm_obj_id_t mod_obj);
231
232 /* static method: isIntrinsicClass(x) */
233 static int s_getp_is_int_class(VMG_ vm_val_t *result, uint *argc);
234
235 protected:
236 /* create with no initial contents */
237 CVmObjClass() { ext_ = 0; }
238
239 /* create with a given dependency table index */
240 CVmObjClass(VMG_ int in_root_set, uint meta_idx, vm_obj_id_t self);
241
242 /* allocate or reallocate my extension */
243 vm_intcls_ext *alloc_ext(VMG0_);
244
245 /* get my extension */
246 vm_intcls_ext *get_ext() const { return (vm_intcls_ext *)ext_; }
247
248 /* load/reload the image data */
249 void load_image_data(VMG_ vm_obj_id_t self,
250 const char *ptr, size_t siz);
251
252 /* list our intrinsic class's properties */
253 size_t list_class_props(VMG_ vm_obj_id_t self,
254 struct vm_meta_entry_t *entry,
255 class CVmObjList *lst, size_t starting_idx,
256 int static_only);
257
258 /*
259 * Find the intrinsic class for the given modifier object. We override
260 * this because we want an intrinsic class's effective intrinsic class
261 * to be its intrinsic superclass, not its metaclass. The metaclass of
262 * an intrinsic class object is always IntrinsicClass; instead, we want
263 * to see, for example, List->Collection->Object, which is the
264 * intrinsic superclass hierarchy.
265 */
266 vm_obj_id_t find_intcls_for_mod(VMG_ vm_obj_id_t self,
267 vm_obj_id_t mod_obj)
268 {
269 /*
270 * The implementation is very simple: just look for a modifier
271 * object attached to this object or one of its intrinsic
272 * superclasses. The difference between this and the regular
273 * CVmObject implementation is that the CVmObject implementation
274 * looks in the object's metaclass; we simply look in our intrinsic
275 * superclasses directly, since, for reflection purposes, we are
276 * our own metaclass.
277 */
278 return find_mod_src_obj(vmg_ self, mod_obj);
279 }
280
281 /*
282 * search for a property among our modifiers, searching our own
283 * modifier and modifiers for our superclasses
284 */
285 int get_prop_from_mod(VMG_ vm_prop_id_t prop, vm_val_t *val,
286 vm_obj_id_t self, vm_obj_id_t *source_obj,
287 uint *argc);
288
289 /* register myself with the dependency table */
290 void register_meta(VMG_ vm_obj_id_t self);
291 };
292
293 /* ------------------------------------------------------------------------ */
294 /*
295 * Registration table object
296 */
297 class CVmMetaclassClass: public CVmMetaclass
298 {
299 public:
300 /* get the global name */
301 const char *get_meta_name() const { return "intrinsic-class/030001"; }
302
303 /* create from image file */
304 void create_for_image_load(VMG_ vm_obj_id_t id)
305 {
306 new (vmg_ id) CVmObjClass();
307 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
308 }
309
310 /* create from restoring from saved state */
311 void create_for_restore(VMG_ vm_obj_id_t id)
312 {
313 new (vmg_ id) CVmObjClass();
314 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
315 }
316
317 /* create dynamically using stack arguments */
318 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
319 { return CVmObjClass::create_from_stack(vmg_ pc_ptr, argc); }
320
321 /* call a static property */
322 int call_stat_prop(VMG_ vm_val_t *result,
323 const uchar **pc_ptr, uint *argc,
324 vm_prop_id_t prop)
325 {
326 return CVmObjClass::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
327 }
328 };
329
330 #endif /* VMINTCLS_H */
331
332 /*
333 * Register the class
334 */
335 VM_REGISTER_METACLASS(CVmObjClass)