Debian Codegraph @

terps/tads/tads3/vmintcls.cpp

raw symbols SCIP
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 vmintcls.cpp - T3 metaclass - intrinsic class
15 Function
16
17 Notes
18
19 Modified
20 03/08/00 MJRoberts - Creation
21 */
22
23 #include <stdlib.h>
24 #include <assert.h>
25
26 #include "vmtype.h"
27 #include "vmobj.h"
28 #include "vmglob.h"
29 #include "vmintcls.h"
30 #include "vmmeta.h"
31 #include "vmfile.h"
32 #include "vmlst.h"
33 #include "vmstack.h"
34 #include "vmtobj.h"
35
36
37 /* ------------------------------------------------------------------------ */
38 /*
39 * statics
40 */
41
42 /* metaclass registration object */
43 static CVmMetaclassClass metaclass_reg_obj;
44 CVmMetaclass *CVmObjClass::metaclass_reg_ = &metaclass_reg_obj;
45
46
47 /* ------------------------------------------------------------------------ */
48 /*
49 * create dynamically using stack arguments
50 */
51 vm_obj_id_t CVmObjClass::create_from_stack(VMG_ const uchar **pc_ptr,
52 uint argc)
53 {
54 /* it is illegal to create this type of object dynamically */
55 err_throw(VMERR_ILLEGAL_NEW);
56 AFTER_ERR_THROW(return VM_INVALID_OBJ;)
57 }
58
59 /*
60 * create with no initial contents
61 */
62 vm_obj_id_t CVmObjClass::create(VMG_ int in_root_set)
63 {
64 vm_obj_id_t id = vm_new_id(vmg_ in_root_set, TRUE, FALSE);
65 new (vmg_ id) CVmObjClass();
66 return id;
67 }
68
69 /*
70 * create with a given dependency index
71 */
72 vm_obj_id_t CVmObjClass::create_dyn(VMG_ uint meta_idx)
73 {
74 vm_obj_id_t id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
75 new (vmg_ id) CVmObjClass(vmg_ FALSE, meta_idx, id);
76 return id;
77 }
78
79 /* allocate our extension */
80 vm_intcls_ext *CVmObjClass::alloc_ext(VMG0_)
81 {
82 /* if I already have an extension, return it */
83 if (ext_ != 0)
84 return (vm_intcls_ext *)ext_;
85
86 /* allocate the new extension */
87 vm_intcls_ext *ext = (vm_intcls_ext *)G_mem->get_var_heap()->alloc_mem(
88 sizeof(vm_intcls_ext), this);
89
90 /* save it */
91 ext_ = (char *)ext;
92
93 /* return it */
94 return ext;
95 }
96
97 /*
98 * create with a given dependency index
99 */
100 CVmObjClass::CVmObjClass(VMG_ int in_root_set, uint meta_idx,
101 vm_obj_id_t self)
102 {
103 /* calls of this form can't come from the image file */
104 assert(!in_root_set);
105
106 /* allocate the extension */
107 ext_ = 0;
108 vm_intcls_ext *ext = alloc_ext(vmg0_);
109
110 /* set up the extension */
111 ext->meta_idx = meta_idx;
112 ext->mod_obj = VM_INVALID_OBJ;
113 ext->class_state.set_nil();
114 ext->changed = FALSE;
115
116 /* register myself with the metaclass table */
117 register_meta(vmg_ self);
118 }
119
120 /*
121 * notify of deletion
122 */
123 void CVmObjClass::notify_delete(VMG_ int in_root_set)
124 {
125 /* free our extension */
126 if (ext_ != 0 && !in_root_set)
127 G_mem->get_var_heap()->free_mem(ext_);
128 }
129
130 /* set a property */
131 void CVmObjClass::set_prop(VMG_ CVmUndo *undo,
132 vm_obj_id_t self, vm_prop_id_t prop,
133 const vm_val_t *val)
134 {
135 /* try treating the request as a static property of the metaclass */
136 vm_meta_entry_t *entry = get_meta_entry(vmg0_);
137 if (entry != 0
138 && entry->meta_->set_stat_prop(
139 vmg_ undo, self, &get_ext()->class_state, prop, val))
140 return;
141
142 /* the class doesn't handle it, so check for a modifier object */
143 vm_obj_id_t mod_obj = get_mod_obj();
144 if (mod_obj != VM_INVALID_OBJ)
145 {
146 /* we have a modifier - set the property in the modifier */
147 vm_objp(vmg_ mod_obj)->set_prop(vmg_ undo, mod_obj, prop, val);
148 }
149 else
150 {
151 /* if we don't have a modifier, we can't set the property */
152 err_throw(VMERR_INVALID_SETPROP);
153 }
154 }
155
156 /*
157 * Get a list of our properties
158 */
159 void CVmObjClass::build_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval)
160 {
161 vm_obj_id_t mod_obj;
162 vm_meta_entry_t *entry;
163 size_t my_prop_cnt;
164 size_t mod_prop_cnt;
165 vm_val_t mod_val;
166 CVmObjList *lst;
167 CVmObjList *mod_lst;
168
169 /* presume we won't find any static properties of our own */
170 my_prop_cnt = 0;
171
172 /* get my metaclass table entry */
173 entry = get_meta_entry(vmg0_);
174
175 /* if we have an entry, count the properties */
176 if (entry != 0)
177 my_prop_cnt = list_class_props(vmg_ self, entry, 0, 0, FALSE);
178
179 /* if we have a modifier object, get its property list */
180 if ((mod_obj = get_mod_obj()) != VM_INVALID_OBJ)
181 {
182 /* get the modifier's property list - we'll add it to our own */
183 vm_objp(vmg_ mod_obj)->build_prop_list(vmg_ self, &mod_val);
184
185 /* get the result as a list object, properly cast */
186 mod_lst = (CVmObjList *)vm_objp(vmg_ mod_val.val.obj);
187
188 /*
189 * As an optimization, if we don't have any properties of our own
190 * to add to the modifier list, just return the modifier list
191 * directly (thus avoiding unnecessarily creating another copy of
192 * the list with no changes).
193 */
194 if (my_prop_cnt == 0)
195 {
196 /* the modifier list is the entire return value */
197 *retval = mod_val;
198 return;
199 }
200
201 /* get the size of the modifier list */
202 mod_prop_cnt = vmb_get_len(mod_lst->get_as_list());
203 }
204 else
205 {
206 /*
207 * we have no modifier object - for the result list, we simply
208 * need our own list, so set the modifier list to nil
209 */
210 mod_val.set_nil();
211 mod_prop_cnt = 0;
212 mod_lst = 0;
213 }
214
215 /* for gc protection, push the modifier's list */
216 G_stk->push(&mod_val);
217
218 /*
219 * Allocate a list big enough to hold the modifier's list plus our own
220 * list.
221 */
222 retval->set_obj(CVmObjList::
223 create(vmg_ FALSE, my_prop_cnt + mod_prop_cnt));
224
225 /* push the return value list for gc protection */
226 G_stk->push(retval);
227
228 /* get it as a list object, properly cast */
229 lst = (CVmObjList *)vm_objp(vmg_ retval->val.obj);
230 lst->cons_clear();
231
232 /* start the list with our own properties */
233 if (entry != 0)
234 list_class_props(vmg_ self, entry, lst, 0, FALSE);
235
236 /* copy the modifier list into the results, if there is a modifier list */
237 if (mod_prop_cnt != 0)
238 lst->cons_copy_elements(my_prop_cnt, mod_lst->get_as_list());
239
240 /* done with the gc protection */
241 G_stk->discard(2);
242 }
243
244 /*
245 * mark references for garbage collection
246 */
247 void CVmObjClass::mark_refs(VMG_ uint state)
248 {
249 /* if we have an object in our class state value, mark it */
250 vm_intcls_ext *ext = get_ext();
251 if (ext->class_state.typ == VM_OBJ)
252 G_obj_table->mark_all_refs(ext->class_state.val.obj, state);
253 }
254
255
256 /*
257 * List my metaclass's properties. Returns the number of properties we
258 * find. We'll add the properties to the given list; if the list is null,
259 * we'll simply return the count.
260 */
261 size_t CVmObjClass::list_class_props(VMG_ vm_obj_id_t self,
262 vm_meta_entry_t *entry,
263 CVmObjList *lst, size_t starting_idx,
264 int static_only)
265 {
266 ushort i;
267 size_t cnt;
268 size_t idx;
269
270 /* count the valid entries */
271 for (i = 1, idx = starting_idx, cnt = 0 ;
272 i <= entry->func_xlat_cnt_ ; ++i)
273 {
274 vm_prop_id_t prop;
275 vm_val_t val;
276 vm_obj_id_t source_obj;
277
278 /* get this property */
279 prop = entry->xlat_func(i);
280
281 /*
282 * If this one's valid, check to see if it's we're interested in
283 * it. If we want only statics, include it only if it's
284 * implemented as a static method on this intrinsic class object;
285 * otherwise, include it unconditionally.
286 *
287 * To determine if the property is implemented as a static, call
288 * the property on self without an argc pointer - this is the
289 * special form of the call which merely retrieves the value of the
290 * property without evaluating it. If the property is defined on
291 * the object, and the source of the definition is self, include
292 * this one in the list of directly-defined properties.
293 */
294 if (prop != VM_INVALID_PROP
295 && (!static_only
296 || (get_prop(vmg_ prop, &val, self, &source_obj, 0)
297 && source_obj == self)))
298 {
299 /* we're interested - add it to the list if we have one */
300 if (lst != 0)
301 {
302 /* set up a value containing the property pointer */
303 val.set_propid(prop);
304
305 /* add it to the list at the next free slot */
306 lst->cons_set_element(idx, &val);
307
308 /* advance to the next slot */
309 ++idx;
310 }
311
312 /* count it */
313 ++cnt;
314 }
315 }
316
317 /* return the number of properties we found */
318 return cnt;
319 }
320
321
322 /*
323 * get a static property
324 */
325 int CVmObjClass::call_stat_prop(VMG_ vm_val_t *result, const uchar **pc_ptr,
326 uint *argc, vm_prop_id_t prop)
327 {
328 /* translate the property into a function vector index */
329 switch(G_meta_table
330 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop))
331 {
332 case 1:
333 /* isIntrinsicClass(x) */
334 return s_getp_is_int_class(vmg_ result, argc);
335
336 default:
337 /*
338 * we don't define this one - inherit the default from the base
339 * object metaclass
340 */
341 return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
342 }
343 }
344
345 /*
346 * static method: isIntrinsicClass(x)
347 */
348 int CVmObjClass::s_getp_is_int_class(VMG_ vm_val_t *result, uint *argc)
349 {
350 /* check arguments */
351 static CVmNativeCodeDesc desc(1);
352 if (get_prop_check_argc(result, argc, &desc))
353 return TRUE;
354
355 /* return true if we have an object of type intrinsic class */
356 vm_val_t *v = G_stk->get(0);
357 result->set_logical(v->typ == VM_OBJ && is_intcls_obj(vmg_ v->val.obj));
358
359 /* discard the argument */
360 G_stk->discard();
361
362 /* handled */
363 return TRUE;
364 }
365
366 /*
367 * get a property
368 */
369 int CVmObjClass::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
370 vm_obj_id_t self, vm_obj_id_t *source_obj,
371 uint *argc)
372 {
373 /*
374 * pass the property request as a static property of the metaclass
375 * with which we're associated
376 */
377 vm_meta_entry_t *entry = get_meta_entry(vmg0_);
378 if (entry != 0 && entry->meta_->call_stat_prop(vmg_ val, 0, argc, prop))
379 {
380 /* the metaclass object handled it, so we are the definer */
381 *source_obj = self;
382 return TRUE;
383 }
384
385 /*
386 * Try handling it through the modifier object, if we have one. Note
387 * that we must search our own intrinsic superclass chain, because our
388 * own true intrinsic class is 'intrinsic class,' but we want to expose
389 * the nominal superclass hierarchy of the intrinsic class itself (for
390 * example, we want to search List->Collection->Object, not
391 * List->IntrinsicClass->Object).
392 */
393 if (get_prop_from_mod(vmg_ prop, val, self, source_obj, argc))
394 return TRUE;
395
396 /* inherit default handling */
397 return CVmObject::get_prop(vmg_ prop, val, self, source_obj, argc);
398 }
399
400 /*
401 * Get a property from an intrinsic class modifier object. Look up the
402 * property in my own modifier, and recursively in my superclass modifiers.
403 */
404 int CVmObjClass::get_prop_from_mod(VMG_ vm_prop_id_t prop, vm_val_t *val,
405 vm_obj_id_t self, vm_obj_id_t *source_obj,
406 uint *argc)
407 {
408 vm_obj_id_t sc;
409 vm_obj_id_t mod_obj;
410
411 /* if we have a modifier object, look it up in the modifier */
412 if ((mod_obj = get_mod_obj()) != VM_INVALID_OBJ
413 && vm_objp(vmg_ mod_obj)->get_prop(vmg_ prop, val, mod_obj,
414 source_obj, argc))
415 {
416 /* got it - return it from the modifier */
417 return TRUE;
418 }
419
420 /*
421 * it's not in our modifier(s); check with any intrinsic superclass
422 * modifiers
423 */
424 if (get_superclass_count(vmg_ self) != 0
425 && (sc = get_superclass(vmg_ self, 0)) != VM_INVALID_OBJ)
426 {
427 /* we have a superclass - check it recursively */
428 return ((CVmObjClass *)vm_objp(vmg_ sc))
429 ->get_prop_from_mod(vmg_ prop, val, sc, source_obj, argc);
430 }
431
432 /*
433 * we didn't find it, and we have no superclass, so there's nowhere
434 * else to look - return failure
435 */
436 return FALSE;
437 }
438
439 /*
440 * Find the intrinsic class which the given modifier object modifies. This
441 * can only be used with a modifier that modifies me or one of my
442 * superclasses.
443 */
444 vm_obj_id_t CVmObjClass::find_mod_src_obj(VMG_ vm_obj_id_t self,
445 vm_obj_id_t mod_obj)
446 {
447 vm_obj_id_t my_mod_obj;
448 vm_obj_id_t sc;
449
450 /*
451 * Is this one of my modifier objects? It is if it's my most
452 * specialized modifier object (i.e., get_mod_obj()), or if my most
453 * specialized modifier object descends from the given object.
454 */
455 my_mod_obj = get_mod_obj();
456 if (my_mod_obj != VM_INVALID_OBJ
457 && (mod_obj == my_mod_obj
458 || vm_objp(vmg_ my_mod_obj)->is_instance_of(vmg_ mod_obj)))
459 {
460 /* it's one of mine, so I'm the intrinsic class mod_obj modifies */
461 return self;
462 }
463
464 /*
465 * It's not one of mine, so check my superclasses recursively. If we
466 * have no direct superclass, we've failed to find the object.
467 */
468 if (get_superclass_count(vmg_ self) == 0
469 || (sc = get_superclass(vmg_ self, 0)) == VM_INVALID_OBJ)
470 return VM_INVALID_OBJ;
471
472 /* ask the superclass to find the modifier */
473 return ((CVmObjClass *)vm_objp(vmg_ sc))
474 ->find_mod_src_obj(vmg_ sc, mod_obj);
475 }
476
477 /*
478 * Get my metaclass table entry
479 */
480 vm_meta_entry_t *CVmObjClass::get_meta_entry(VMG0_) const
481 {
482 /* get my metaclass table index */
483 uint meta_idx = get_meta_idx();
484
485 /* look up my metaclass table entry, if we have one */
486 if (meta_idx < G_meta_table->get_count())
487 return G_meta_table->get_entry(meta_idx);
488 else
489 return 0;
490 }
491
492 /*
493 * save to a file
494 */
495 void CVmObjClass::save_to_file(VMG_ class CVmFile *fp)
496 {
497 /* write our data: data size, meta table index, mod ID, class state */
498 vm_intcls_ext *ext = get_ext();
499 fp->write_uint2(2 + 2 + 4 + VMB_DATAHOLDER);
500 fp->write_uint2(ext->meta_idx);
501 fp->write_uint4(ext->mod_obj);
502
503 char buf[VMB_DATAHOLDER];
504 vmb_put_dh(buf, &ext->class_state);
505 fp->write_bytes(buf, VMB_DATAHOLDER);
506 }
507
508 /*
509 * restore from a file
510 */
511 void CVmObjClass::restore_from_file(VMG_ vm_obj_id_t self,
512 CVmFile *fp, CVmObjFixup *fixups)
513 {
514 /* allocate/reallocate the extension */
515 vm_intcls_ext *ext = alloc_ext(vmg0_);
516
517 /* read the length */
518 size_t len = fp->read_uint2();
519 const size_t expected_len = 2+2+4+VMB_DATAHOLDER;
520
521 /*
522 * read the metaclass index and modifier object ID; note that modifier
523 * objects are always root set objects, so there's no need to worry
524 * about fixups
525 */
526 ext->meta_idx = fp->read_uint2();
527 ext->mod_obj = (vm_obj_id_t)fp->read_uint4();
528
529 /* if we have a class state value, read it */
530 ext->class_state.set_nil();
531 if (len >= 2+2+4+VMB_DATAHOLDER)
532 {
533 char buf[VMB_DATAHOLDER];
534 fp->read_bytes(buf, VMB_DATAHOLDER);
535 fixups->fix_dh(vmg_ buf);
536 vmb_get_dh(buf, &ext->class_state);
537 }
538
539 /* skip any extra data we don't recognize */
540 if (len > expected_len)
541 fp->set_pos_from_cur(len - expected_len);
542
543 /* register myself with the metaclass table */
544 register_meta(vmg_ self);
545 }
546
547 /* load from an image file */
548 void CVmObjClass::load_from_image(VMG_ vm_obj_id_t self,
549 const char *ptr, size_t siz)
550 {
551 /* load the image data */
552 load_image_data(vmg_ self, ptr, siz);
553
554 /* we need the image pointer saved for re-loading on RESTART */
555 G_obj_table->save_image_pointer(self, ptr, siz);
556 }
557
558 /* reload from an image file */
559 void CVmObjClass::reload_from_image(VMG_ vm_obj_id_t self,
560 const char *ptr, size_t siz)
561 {
562 /* load the image data */
563 load_image_data(vmg_ self, ptr, siz);
564 }
565
566 /* load image data */
567 void CVmObjClass::load_image_data(VMG_ vm_obj_id_t self,
568 const char *ptr, size_t siz)
569 {
570 /* make sure the length is valid */
571 if (siz < 8)
572 err_throw(VMERR_INVAL_METACLASS_DATA);
573
574 /* allocate or reallocate the extension */
575 vm_intcls_ext *ext = alloc_ext(vmg0_);
576
577 /*
578 * read the metaclass index and modifier object ID; note that modifier
579 * objects are always root set objects, so there's no need to worry
580 * about fixups
581 */
582 ext->meta_idx = osrp2(ptr+2);
583 ext->mod_obj = (vm_obj_id_t)t3rp4u(ptr+4);
584
585 /* if we have a class state value, read it */
586 ext->class_state.set_nil();
587 if (siz >= 2+4+VMB_DATAHOLDER)
588 vmb_get_dh(ptr+8, &ext->class_state);
589
590 /* register myself */
591 register_meta(vmg_ self);
592 }
593
594 /*
595 * Register myself with the metaclass dependency table - this establishes
596 * the association between the metaclass in the dependency table and this
597 * instance, so that the metaclass knows about the instance that
598 * represents it.
599 */
600 void CVmObjClass::register_meta(VMG_ vm_obj_id_t self)
601 {
602 /* get my metaclass table entry */
603 vm_meta_entry_t *entry = get_meta_entry(vmg0_);
604
605 /*
606 * if we have a valid entry, store a reference to myself in the
607 * metaclass table - this will let the metaclass that we represent find
608 * us when asked for its class object
609 */
610 if (entry != 0)
611 entry->class_obj_ = self;
612 }
613
614 /*
615 * Get the number of superclasses
616 */
617 int CVmObjClass::get_superclass_count(VMG_ vm_obj_id_t) const
618 {
619 /* get my metaclass table entry */
620 vm_meta_entry_t *entry = get_meta_entry(vmg0_);
621
622 /*
623 * if we have a valid entry, ask the metaclass object to tell us the
624 * superclass count
625 */
626 if (entry != 0)
627 return entry->meta_->get_supermeta_count(vmg0_);
628 else
629 return 0;
630 }
631
632 /*
633 * Get a superclass
634 */
635 vm_obj_id_t CVmObjClass::get_superclass(VMG_ vm_obj_id_t, int sc_idx) const
636 {
637 /* get my metaclass table entry */
638 vm_meta_entry_t *entry = get_meta_entry(vmg0_);
639
640 /*
641 * if we have a valid entry, ask the metaclass object to retrieve the
642 * superclass
643 */
644 if (entry != 0)
645 return entry->meta_->get_supermeta(vmg_ sc_idx);
646 else
647 return VM_INVALID_OBJ;
648 }
649
650 /*
651 * Determine if I'm an instance of the given object
652 */
653 int CVmObjClass::is_instance_of(VMG_ vm_obj_id_t obj)
654 {
655 /* get my metaclass table entry */
656 vm_meta_entry_t *entry = get_meta_entry(vmg0_);
657
658 /* if we have a valid entry, ask the metaclass object */
659 if (entry != 0)
660 {
661 /* ask the metaclass if it's an instance of the given object */
662 return entry->meta_->is_meta_instance_of(vmg_ obj);
663 }
664 else
665 {
666 /* not a valid metaclass - we can't make sense of this */
667 return FALSE;
668 }
669 }