Debian Codegraph @

terps/tads/tads3/vmfref.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */
7 /*
8 Name
9 vmfref.cpp - stack frame reference object
10 Function
11
12 Notes
13
14 Modified
15 02/18/10 MJRoberts - Creation
16 */
17
18 #include <stdlib.h>
19 #include "vmtype.h"
20 #include "vmobj.h"
21 #include "vmfref.h"
22 #include "vmglob.h"
23 #include "vmmeta.h"
24 #include "vmstack.h"
25 #include "vmundo.h"
26 #include "vmlst.h"
27 #include "vmfile.h"
28 #include "vmstr.h"
29 #include "vmvec.h"
30 #include "vmlookup.h"
31 #include "vmfunc.h"
32 #include "vmerr.h"
33 #include "vmerrnum.h"
34
35
36 /* ------------------------------------------------------------------------ */
37 /*
38 * Stack frame descriptor
39 */
40
41
42 /*
43 * statics
44 */
45 static CVmMetaclassFrameDesc desc_metaclass_reg_obj;
46 CVmMetaclass *CVmObjFrameDesc::metaclass_reg_ = &desc_metaclass_reg_obj;
47
48 /*
49 * function table
50 */
51 int (CVmObjFrameDesc::
52 *CVmObjFrameDesc::func_table_[])
53 (VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc) =
54 {
55 &CVmObjFrameDesc::getp_undef,
56 &CVmObjFrameDesc::getp_is_active,
57 &CVmObjFrameDesc::getp_get_vars,
58 &CVmObjFrameDesc::getp_get_self,
59 &CVmObjFrameDesc::getp_get_defobj,
60 &CVmObjFrameDesc::getp_get_targobj,
61 &CVmObjFrameDesc::getp_get_targprop,
62 &CVmObjFrameDesc::getp_get_invokee,
63 };
64
65 /*
66 * construction
67 */
68 CVmObjFrameDesc::CVmObjFrameDesc(VMG_ vm_obj_id_t fref,
69 int frame_idx, uint ret_ofs)
70 {
71 /* allocate our extension */
72 ext_ = 0;
73 alloc_ext(vmg_ fref, frame_idx, ret_ofs);
74 }
75
76 /*
77 * create
78 */
79 vm_obj_id_t CVmObjFrameDesc::create(
80 VMG_ vm_obj_id_t fref, int frame_idx, uint ret_ofs)
81 {
82 /* allocate an object ID */
83 vm_obj_id_t id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
84
85 /* instantiate the new object */
86 new (vmg_ id) CVmObjFrameDesc(vmg_ fref, frame_idx, ret_ofs);
87
88 /* return the new object's ID */
89 return id;
90 }
91
92 /*
93 * allocate our extension
94 */
95 vm_framedesc_ext *CVmObjFrameDesc::alloc_ext(
96 VMG_ vm_obj_id_t fref, int frame_idx, uint ret_ofs)
97 {
98 /* delete any existing extension */
99 if (ext_ != 0)
100 G_mem->get_var_heap()->free_mem(ext_);
101
102 /* allocate the extension */
103 vm_framedesc_ext *ext =
104 (vm_framedesc_ext *)G_mem->get_var_heap()->alloc_mem(
105 sizeof(vm_framedesc_ext), this);
106
107 /* save the data values */
108 ext->fref = fref;
109 ext->frame_idx = frame_idx;
110 ext->ret_ofs = ret_ofs;
111
112 /* save the new extension in 'self' */
113 ext_ = (char *)ext;
114
115 /* return the new extension */
116 return ext;
117 }
118
119 /*
120 * notify of deletion
121 */
122 void CVmObjFrameDesc::notify_delete(VMG_ int)
123 {
124 /* free our extension */
125 if (ext_ != 0)
126 G_mem->get_var_heap()->free_mem(ext_);
127 }
128
129 /*
130 * mark references
131 */
132 void CVmObjFrameDesc::mark_refs(VMG_ uint state)
133 {
134 /* get the extension */
135 vm_framedesc_ext *ext = get_ext();
136
137 /* mark our frame ref object */
138 G_obj_table->mark_all_refs(ext->fref, state);
139 }
140
141
142 /*
143 * load from the image file
144 */
145 void CVmObjFrameDesc::load_from_image(VMG_ vm_obj_id_t self,
146 const char *ptr, size_t siz)
147 {
148 /* load our image data */
149 load_image_data(vmg_ self, ptr, siz);
150
151 /*
152 * save our image data pointer in the object table, so that we can
153 * access it (without storing it ourselves) during a reload
154 */
155 G_obj_table->save_image_pointer(self, ptr, siz);
156 }
157
158 /*
159 * reload from the image
160 */
161 void CVmObjFrameDesc::reload_from_image(VMG_ vm_obj_id_t self,
162 const char *ptr, size_t siz)
163 {
164 /* load our image data */
165 load_image_data(vmg_ self, ptr, siz);
166 }
167
168 /*
169 * load or reload our image data
170 */
171 void CVmObjFrameDesc::load_image_data(VMG_ vm_obj_id_t self,
172 const char *ptr, size_t siz)
173 {
174 /* read the frame information */
175 vm_obj_id_t fref = vmb_get_objid(ptr);
176 int frame_idx = osrp2(ptr + VMB_OBJECT_ID);
177 uint ret_ofs = osrp2(ptr + VMB_OBJECT_ID + 2);
178
179 /* allocate the extension */
180 alloc_ext(vmg_ fref, frame_idx, ret_ofs);
181 }
182
183 /*
184 * save
185 */
186 void CVmObjFrameDesc::save_to_file(VMG_ class CVmFile *fp)
187 {
188 /* get our extension */
189 vm_framedesc_ext *ext = get_ext();
190
191 /* save our extension data */
192 fp->write_uint4(ext->fref);
193 fp->write_uint2(ext->frame_idx);
194 fp->write_uint2(ext->ret_ofs);
195 }
196
197 /*
198 * restore
199 */
200 void CVmObjFrameDesc::restore_from_file(VMG_ vm_obj_id_t self,
201 class CVmFile *fp,
202 class CVmObjFixup *fixups)
203 {
204 /* read the values */
205 vm_obj_id_t fref = fixups->get_new_id(vmg_ (vm_obj_id_t)fp->read_uint4());
206 int frame_idx = fp->read_int2();
207 uint ret_ofs = fp->read_uint2();
208
209 /* allocate the extension */
210 alloc_ext(vmg_ fref, frame_idx, ret_ofs);
211 }
212
213 /*
214 * Find a symbol. If we find the name, we fill in 'symp' with the
215 * description of the variable and return true; otherwise we return false.
216 */
217 int CVmObjFrameDesc::find_local(VMG_ const textchar_t *name, size_t namelen,
218 CVmDbgFrameSymPtr *symp)
219 {
220
221 /* get our extension and the underlying frame's extension */
222 vm_framedesc_ext *ext = get_ext();
223 vm_frameref_ext *fext = get_frame_ref(vmg0_)->get_ext();
224
225 /* if the caller doesn't care about the symbol information, use a local */
226 CVmDbgFrameSymPtr oursym;
227 if (symp == 0)
228 symp = &oursym;
229
230 /* if there's no frame, there are no locals */
231 if (ext->frame_idx == 0)
232 return FALSE;
233
234 /* set up pointer to this method's debug records */
235 CVmDbgTablePtr dp;
236 if (!dp.set(fext->entryp))
237 return FALSE;
238
239 /* set up a pointer to our frame */
240 CVmDbgFramePtr dfp;
241 dp.set_frame_ptr(vmg_ &dfp, ext->frame_idx);
242
243 /* walk up the list of frames from innermost to outermost */
244 for (;;)
245 {
246 /* set up a pointer to the first symbol */
247 dfp.set_first_sym_ptr(vmg_ symp);
248
249 /* scan this frame's local symbol list */
250 int sym_cnt = dfp.get_sym_count();
251 for (int i = 0 ; i < sym_cnt ; ++i, symp->inc(vmg0_))
252 {
253 /* check for a match */
254 if (symp->get_sym_len(vmg0_) == namelen
255 && memcmp(symp->get_sym(vmg0_), name, namelen) == 0)
256 {
257 /* this is it - return with symp pointing to the symbol */
258 return TRUE;
259 }
260 }
261
262 /* didn't find it - move up to the enclosing frame */
263 int parent = dfp.get_enclosing_frame();
264 if (parent == 0)
265 break;
266
267 /* set up dfp to point to the parent fraem */
268 dp.set_frame_ptr(vmg_ &dfp, parent);
269 }
270
271 /* we didn't find the symbol */
272 return FALSE;
273 }
274
275 /*
276 * find a local variable in our frame by name, given a VM string value
277 */
278 int CVmObjFrameDesc::find_local(VMG_ const vm_val_t *nval,
279 CVmDbgFrameSymPtr *symp)
280 {
281 /* make sure the name is a string */
282 const char *name = nval->get_as_string(vmg0_);
283 if (name == 0)
284 err_throw(VMERR_BAD_TYPE_BIF);
285
286 /* parse the length */
287 size_t namelen = vmb_get_len(name);
288 name += VMB_LEN;
289
290 /* look up the symbol */
291 return find_local(vmg_ name, namelen, symp);
292 }
293
294 /*
295 * Get the value of a local by name
296 */
297 int CVmObjFrameDesc::get_local_val(VMG_ vm_val_t *result, const vm_val_t *name)
298 {
299 /* look up the symbol by name */
300 CVmDbgFrameSymPtr sym;
301 if (!find_local(vmg_ name, &sym))
302 return FALSE;
303
304 /* retrieve the value based on the descriptor */
305 get_frame_ref(vmg0_)->get_local_val(vmg_ result, &sym);
306
307 /* found it */
308 return TRUE;
309 }
310
311 /*
312 * Set the value of a local by name
313 */
314 int CVmObjFrameDesc::set_local_val(VMG_ const vm_val_t *name,
315 const vm_val_t *new_val)
316 {
317 /* look up the symbol by name */
318 CVmDbgFrameSymPtr sym;
319 if (!find_local(vmg_ name, &sym))
320 return FALSE;
321
322 /* retrieve the value based on the descriptor */
323 get_frame_ref(vmg0_)->set_local_val(vmg_ &sym, new_val);
324
325 /* found it */
326 return TRUE;
327 }
328
329 /*
330 * index the frame: this looks up a variable's value by name
331 */
332 int CVmObjFrameDesc::index_val_q(VMG_ vm_val_t *result,
333 vm_obj_id_t self,
334 const vm_val_t *index_val)
335 {
336 /* check the index type */
337 if (index_val->get_as_string(vmg0_))
338 {
339 /* it's a string - look up the value by name */
340 if (!get_local_val(vmg_ result, index_val))
341 err_throw(VMERR_INDEX_OUT_OF_RANGE);
342 }
343 else
344 {
345 /* invalid index type */
346 err_throw(VMERR_INDEX_OUT_OF_RANGE);
347 }
348
349 /* handled */
350 return TRUE;
351 }
352
353 /*
354 * assign an indexed value: this sets a variable's value by name
355 */
356 int CVmObjFrameDesc::set_index_val_q(VMG_ vm_val_t *new_container,
357 vm_obj_id_t self,
358 const vm_val_t *index_val,
359 const vm_val_t *new_val)
360 {
361 /* check the index type */
362 if (index_val->get_as_string(vmg0_))
363 {
364 /* it's a string - look up the value by name */
365 if (!set_local_val(vmg_ index_val, new_val))
366 err_throw(VMERR_INDEX_OUT_OF_RANGE);
367 }
368 else
369 {
370 /* invalid index type */
371 err_throw(VMERR_INDEX_OUT_OF_RANGE);
372 }
373
374 /* the container doesn't change */
375 new_container->set_obj(self);
376
377 /* handled */
378 return TRUE;
379 }
380
381 /*
382 * get a property
383 */
384 int CVmObjFrameDesc::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval,
385 vm_obj_id_t self, vm_obj_id_t *source_obj,
386 uint *argc)
387 {
388 uint func_idx;
389
390 /* translate the property into a function vector index */
391 func_idx = G_meta_table
392 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop);
393
394 /* call the appropriate function */
395 if ((this->*func_table_[func_idx])(vmg_ self, retval, argc))
396 {
397 *source_obj = metaclass_reg_->get_class_obj(vmg0_);
398 return TRUE;
399 }
400
401 /* inherit default handling from the base object class */
402 return CVmObject::get_prop(vmg_ prop, retval, self, source_obj, argc);
403 }
404
405 /*
406 * property evaluator - determine if the frame is still alive
407 */
408 int CVmObjFrameDesc::getp_is_active(VMG_ vm_obj_id_t self, vm_val_t *retval,
409 uint *argc)
410 {
411 /* check arguments */
412 static CVmNativeCodeDesc desc(0);
413 if (get_prop_check_argc(retval, argc, &desc))
414 return TRUE;
415
416 /* the frame is valid if we still have a frame pointer */
417 retval->set_logical(get_frame_ref(vmg0_)->is_active());
418
419 /* handled */
420 return TRUE;
421 }
422
423 /*
424 * property evaluator - set 'self' from the frame
425 */
426 int CVmObjFrameDesc::getp_get_self(VMG_ vm_obj_id_t self, vm_val_t *retval,
427 uint *argc)
428 {
429 /* check arguments */
430 static CVmNativeCodeDesc desc(0);
431 if (get_prop_check_argc(retval, argc, &desc))
432 return TRUE;
433
434 /* return the 'self' object */
435 get_frame_ref(vmg0_)->get_self(vmg_ retval);
436
437 /* handled */
438 return TRUE;
439 }
440
441 /*
442 * property evaluator - set 'definingobj' from the frame
443 */
444 int CVmObjFrameDesc::getp_get_defobj(VMG_ vm_obj_id_t self, vm_val_t *retval,
445 uint *argc)
446 {
447 /* check arguments */
448 static CVmNativeCodeDesc desc(0);
449 if (get_prop_check_argc(retval, argc, &desc))
450 return TRUE;
451
452 /* get 'definingobj' from the frame */
453 get_frame_ref(vmg0_)->get_defobj(vmg_ retval);
454
455 /* handled */
456 return TRUE;
457 }
458
459 /*
460 * property evaluator - set 'targetobj' from the frame
461 */
462 int CVmObjFrameDesc::getp_get_targobj(VMG_ vm_obj_id_t self, vm_val_t *retval,
463 uint *argc)
464 {
465 /* check arguments */
466 static CVmNativeCodeDesc desc(0);
467 if (get_prop_check_argc(retval, argc, &desc))
468 return TRUE;
469
470 /* get 'targetobj' from the frame */
471 get_frame_ref(vmg0_)->get_targobj(vmg_ retval);
472
473 /* handled */
474 return TRUE;
475 }
476
477 /*
478 * property evaluator - get 'targetprop' from the frame
479 */
480 int CVmObjFrameDesc::getp_get_targprop(VMG_ vm_obj_id_t self, vm_val_t *retval,
481 uint *argc)
482 {
483 /* check arguments */
484 static CVmNativeCodeDesc desc(0);
485 if (get_prop_check_argc(retval, argc, &desc))
486 return TRUE;
487
488 /* get 'targetprop' from the frame */
489 get_frame_ref(vmg0_)->get_targprop(vmg_ retval);
490
491 /* handled */
492 return TRUE;
493 }
494
495 /*
496 * property evaluator - get 'invokee' from the frame
497 */
498 int CVmObjFrameDesc::getp_get_invokee(VMG_ vm_obj_id_t self, vm_val_t *retval,
499 uint *argc)
500 {
501 /* check arguments */
502 static CVmNativeCodeDesc desc(0);
503 if (get_prop_check_argc(retval, argc, &desc))
504 return TRUE;
505
506 /* get 'invokee' from the frame */
507 get_frame_ref(vmg0_)->get_invokee(vmg_ retval);
508
509 /* handled */
510 return TRUE;
511 }
512
513 /*
514 * property evaluator - get a lookup table of the local variables, with
515 * their current values, keyed by name
516 */
517 int CVmObjFrameDesc::getp_get_vars(VMG_ vm_obj_id_t self, vm_val_t *retval,
518 uint *argc)
519 {
520 /* check arguments */
521 static CVmNativeCodeDesc desc(0);
522 if (get_prop_check_argc(retval, argc, &desc))
523 return TRUE;
524
525 /* create our lookup table object */
526 retval->set_obj(CVmObjLookupTable::create(vmg_ FALSE, 32, 64));
527 CVmObjLookupTable *tab = (CVmObjLookupTable *)
528 vm_objp(vmg_ retval->val.obj);
529
530 /* get our extension */
531 vm_framedesc_ext *ext = get_ext();
532
533 /* get our underlying frame ref and its extension */
534 CVmObjFrameRef *fr = get_frame_ref(vmg0_);
535 vm_frameref_ext *fext = fr->get_ext();
536
537 /* set up pointer to this method's debug records */
538 CVmDbgTablePtr dp;
539 if (!dp.set(fext->entryp))
540 return TRUE;
541
542 /* set up a pointer to our frame */
543 CVmDbgFramePtr dfp;
544 dp.set_frame_ptr(vmg_ &dfp, ext->frame_idx);
545
546 /* push the table for gc protection */
547 G_stk->push(retval);
548
549 /* walk up the list of frames from innermost to outermost */
550 for (;;)
551 {
552 /* set up a pointer to the first symbol */
553 CVmDbgFrameSymPtr sym;
554 dfp.set_first_sym_ptr(vmg_ &sym);
555
556 /* scan this frame's local symbol list */
557 int sym_cnt = dfp.get_sym_count();
558 for (int i = 0 ; i < sym_cnt ; ++i, sym.inc(vmg0_))
559 {
560 /* set up a string value for the key */
561 vm_val_t key;
562 sym.get_str_val(vmg_ &key);
563 G_stk->push(&key);
564
565 /*
566 * If this entry isn't already in the table, add it. Don't
567 * bother if it already exists: we work from inner to outer
568 * scopes, and inner scopes hide things in outer scopes, so if
569 * we find an entry in the table already it means that it was
570 * an inner-scope entry that hides the one we're processing
571 * now.
572 */
573 vm_val_t val;
574 if (!tab->index_check(vmg_ &val, &key))
575 {
576 /* get the value */
577 fr->get_local_val(vmg_ &val, &sym);
578
579 /* add the entry to the table */
580 tab->add_entry(vmg_ &key, &val);
581 }
582
583 /* done with the key for gc protection */
584 G_stk->discard();
585 }
586
587 /* move up to the enclosing frame */
588 int parent = dfp.get_enclosing_frame();
589 if (parent == 0)
590 break;
591
592 /* set up dfp to point to the parent fraem */
593 dp.set_frame_ptr(vmg_ &dfp, parent);
594 }
595
596 /* discard the table ref we pushed for gc protection */
597 G_stk->discard();
598
599 /* handled */
600 return TRUE;
601 }
602
603
604 /* ------------------------------------------------------------------------ */
605 /*
606 * Stack frame reference
607 */
608
609 /*
610 * statics
611 */
612 static CVmMetaclassFrameRef ref_metaclass_reg_obj;
613 CVmMetaclass *CVmObjFrameRef::metaclass_reg_ = &ref_metaclass_reg_obj;
614
615 /*
616 * construction
617 */
618 CVmObjFrameRef::CVmObjFrameRef(VMG_ vm_val_t *fp,
619 const uchar *entry)
620 {
621 /* set up a function pointer */
622 CVmFuncPtr f(entry);
623
624 /*
625 * figure the total number of variable snapshot slots we need to
626 * allocate: this is the number of local variables plus the number of
627 * actual arguments
628 */
629 int nlocals = f.get_local_cnt();
630 int nparams = G_interpreter->get_argc_from_frame(vmg_ fp);
631
632 /* allocate our extension */
633 ext_ = 0;
634 vm_frameref_ext *ext = alloc_ext(vmg_ nlocals, nparams);
635
636 /* save the extra frame information */
637 ext->fp = fp;
638 ext->entryp = entry;
639
640 /* get the function pointer value for the entry pointer */
641 f.get_fnptr(vmg_ &ext->entry);
642
643 /* initialize the variable slots to nil */
644 for (int i = 0 ; i < nlocals + nparams ; ++i)
645 ext->vars[i].set_nil();
646
647 /*
648 * save the method context variables - these are immutable, so we can
649 * save them immediately
650 */
651 ext->self = *G_interpreter->get_self_val_from_frame(vmg_ fp);
652 ext->defobj = G_interpreter->get_defining_obj_from_frame(vmg_ fp);
653 ext->targobj = G_interpreter->get_orig_target_obj_from_frame(vmg_ fp);
654 ext->defobj = G_interpreter->get_defining_obj_from_frame(vmg_ fp);
655 }
656
657 /*
658 * create
659 */
660 vm_obj_id_t CVmObjFrameRef::create(
661 VMG_ vm_val_t *fp, const uchar *entry)
662 {
663 /* allocate an object ID */
664 vm_obj_id_t id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
665
666 /* instantiate the new object */
667 new (vmg_ id) CVmObjFrameRef(vmg_ fp, entry);
668
669 /* return the new object's ID */
670 return id;
671 }
672
673 /*
674 * allocate our extension
675 */
676 vm_frameref_ext *CVmObjFrameRef::alloc_ext(VMG_ int nlocals, int nparams)
677 {
678 /* delete any existing extension */
679 if (ext_ != 0)
680 G_mem->get_var_heap()->free_mem(ext_);
681
682 /* figure the allocation size */
683 size_t siz = sizeof(vm_frameref_ext)
684 + (nparams + nlocals - 1)*sizeof(vm_val_t);
685
686 /* allocate the extension */
687 vm_frameref_ext *ext =
688 (vm_frameref_ext *)G_mem->get_var_heap()->alloc_mem(siz, this);
689
690 /* save the variable counts */
691 ext->nlocals = nlocals;
692 ext->nparams = nparams;
693
694 /* save the new extension in 'self' */
695 ext_ = (char *)ext;
696
697 /* return the new extension */
698 return ext;
699 }
700
701 /*
702 * notify of deletion
703 */
704 void CVmObjFrameRef::notify_delete(VMG_ int)
705 {
706 /* free our extension */
707 if (ext_ != 0)
708 G_mem->get_var_heap()->free_mem(ext_);
709 }
710
711 /*
712 * mark references
713 */
714 void CVmObjFrameRef::mark_refs(VMG_ uint state)
715 {
716 /* get the extension */
717 vm_frameref_ext *ext = get_ext();
718
719 /* mark the function */
720 if (ext->entry.typ == VM_OBJ)
721 G_obj_table->mark_all_refs(ext->entry.val.obj, state);
722
723 /* mark the method context objects */
724 if (ext->self.typ == VM_OBJ)
725 G_obj_table->mark_all_refs(ext->self.val.obj, state);
726 if (ext->defobj != VM_INVALID_OBJ)
727 G_obj_table->mark_all_refs(ext->defobj, state);
728 if (ext->targobj != VM_INVALID_OBJ)
729 G_obj_table->mark_all_refs(ext->targobj, state);
730
731 /* mark the invokee */
732 if (ext->invokee.typ == VM_OBJ)
733 G_obj_table->mark_all_refs(ext->invokee.val.obj, state);
734
735 /* mark each snapshot variable */
736 int i;
737 const vm_val_t *v;
738 for (i = ext->nlocals + ext->nparams, v = ext->vars ; i > 0 ; --i, ++v)
739 {
740 if (v->typ == VM_OBJ)
741 G_obj_table->mark_all_refs(v->val.obj, state);
742 }
743 }
744
745
746 /*
747 * load from the image file
748 */
749 void CVmObjFrameRef::load_from_image(VMG_ vm_obj_id_t self,
750 const char *ptr, size_t siz)
751 {
752 /* load our image data */
753 load_image_data(vmg_ self, ptr, siz);
754
755 /*
756 * save our image data pointer in the object table, so that we can
757 * access it (without storing it ourselves) during a reload
758 */
759 G_obj_table->save_image_pointer(self, ptr, siz);
760 }
761
762 /*
763 * reload from the image
764 */
765 void CVmObjFrameRef::reload_from_image(VMG_ vm_obj_id_t self,
766 const char *ptr, size_t siz)
767 {
768 /* load our image data */
769 load_image_data(vmg_ self, ptr, siz);
770 }
771
772 /*
773 * load or reload our image data
774 */
775 void CVmObjFrameRef::load_image_data(VMG_ vm_obj_id_t self,
776 const char *ptr, size_t siz)
777 {
778 /* read the number of variables */
779 int nlocals = osrp2(ptr);
780 int nparams = osrp2(ptr+2);
781 ptr += 4;
782
783 /* allocate the extension */
784 vm_frameref_ext *ext = alloc_ext(vmg_ nlocals, nparams);
785
786 /*
787 * Since stack frames are inherently transient, a saved frame ref
788 * object can't point back to a live stack frame, so on restore we have
789 * to assume that our stack frame is inactive.
790 */
791 ext->fp = 0;
792
793 /* load the entry pointer */
794 vmb_get_dh(ptr, &ext->entry);
795 ptr += VMB_DATAHOLDER;
796
797 /* after other objects are loaded, resolve the entry pointer */
798 G_obj_table->request_post_load_init(self);
799
800 /* load the method context variables */
801 vmb_get_dh(ptr, &ext->self);
802 ptr += VMB_DATAHOLDER;
803
804 ext->defobj = vmb_get_objid(ptr);
805 ptr += VMB_OBJECT_ID;
806
807 ext->targobj = vmb_get_objid(ptr);
808 ptr += VMB_OBJECT_ID;
809
810 ext->targprop = vmb_get_propid(ptr);
811 ptr += VMB_PROP_ID;
812
813 vmb_get_dh(ptr, &ext->invokee);
814 ptr += VMB_DATAHOLDER;
815
816 /* read the variable snapshot values */
817 int i;
818 vm_val_t *v;
819 for (i = nlocals + nparams, v = ext->vars ; i > 0 ; --i, ++v)
820 {
821 vmb_get_dh(ptr, v);
822 ptr += VMB_DATAHOLDER;
823 }
824 }
825
826 /*
827 * save
828 */
829 void CVmObjFrameRef::save_to_file(VMG_ class CVmFile *fp)
830 {
831 char buf[VMB_DATAHOLDER];
832
833 /* get our extension */
834 vm_frameref_ext *ext = get_ext();
835
836 /*
837 * If our frame is still active, make a snapshot of the variables. The
838 * stack frame itself is inherently transient, so we can only save the
839 * snapshot version.
840 */
841 if (ext->fp != 0)
842 make_snapshot(vmg0_);
843
844 /* save the variable counts */
845 fp->write_uint2(ext->nlocals);
846 fp->write_uint2(ext->nparams);
847
848 /* save the entry pointer */
849 vmb_put_dh(buf, &ext->entry);
850 fp->write_bytes(buf, VMB_DATAHOLDER);
851
852 /* save the method context values */
853 vmb_put_dh(buf, &ext->self);
854 fp->write_bytes(buf, VMB_DATAHOLDER);
855 fp->write_uint4(ext->defobj);
856 fp->write_uint4(ext->targobj);
857 fp->write_uint2(ext->targprop);
858 vmb_put_dh(buf, &ext->invokee);
859 fp->write_bytes(buf, VMB_DATAHOLDER);
860
861 /* save the variable snapshot values */
862 int i;
863 const vm_val_t *v;
864 for (i = ext->nparams + ext->nlocals, v = ext->vars ; i > 0 ; --i, ++v)
865 {
866 vmb_put_dh(buf, v);
867 fp->write_bytes(buf, VMB_DATAHOLDER);
868 }
869 }
870
871 /*
872 * restore
873 */
874 void CVmObjFrameRef::restore_from_file(VMG_ vm_obj_id_t self,
875 class CVmFile *fp,
876 class CVmObjFixup *fixups)
877 {
878 char buf[VMB_DATAHOLDER];
879
880 /* read the variables counts */
881 int nlocals = fp->read_int2();
882 int nparams = fp->read_int2();
883
884 /* allocate the extension */
885 vm_frameref_ext *ext = alloc_ext(vmg_ nlocals, nparams);
886
887 /*
888 * Since stack frames are inherently transient, a saved frame ref
889 * object can't point back to a live stack frame, so on restore we have
890 * to assume that our stack frame is inactive.
891 */
892 get_ext()->fp = 0;
893
894 /* load the entry pointer */
895 fp->read_bytes(buf, VMB_DATAHOLDER);
896 fixups->fix_dh(vmg_ buf);
897 vmb_get_dh(buf, &ext->entry);
898
899 /* after other objects are loaded, resolve the entry pointer */
900 G_obj_table->request_post_load_init(self);
901
902 /* restore the method context values */
903 fp->read_bytes(buf, VMB_DATAHOLDER);
904 fixups->fix_dh(vmg_ buf);
905 vmb_get_dh(buf, &ext->self);
906
907 ext->defobj = (vm_obj_id_t)fp->read_uint4();
908 if (ext->defobj != VM_INVALID_OBJ)
909 ext->defobj = fixups->get_new_id(vmg_ ext->defobj);
910
911 ext->targobj = (vm_obj_id_t)fp->read_uint4();
912 if (ext->targobj != VM_INVALID_OBJ)
913 ext->targobj = fixups->get_new_id(vmg_ ext->targobj);
914
915 ext->targprop = (vm_prop_id_t)fp->read_uint2();
916
917 fp->read_bytes(buf, VMB_DATAHOLDER);
918 fixups->fix_dh(vmg_ buf);
919 vmb_get_dh(buf, &ext->invokee);
920
921 /* load the snapshot values */
922 int i;
923 vm_val_t *v;
924 for (i = nlocals + nparams, v = ext->vars ; i > 0 ; --i, ++v)
925 {
926 fp->read_bytes(buf, VMB_DATAHOLDER);
927 fixups->fix_dh(vmg_ buf);
928 vmb_get_dh(buf, v);
929 }
930 }
931
932 /*
933 * After loading, resolve the entry pointer. We need to wait until loading
934 * is complete to do this, since the entry pointer might refer to another
935 * object, in which case the other object needs to be loaded before we can
936 * resolve a pointer to it.
937 */
938 void CVmObjFrameRef::post_load_init(VMG_ vm_obj_id_t self)
939 {
940 /* get our extension */
941 vm_frameref_ext *ext = get_ext();
942
943 /* resolve the entry pointer */
944 CVmFuncPtr f(vmg_ &ext->entry);
945 ext->entryp = f.get();
946 }
947
948 /*
949 * Invalidate the frame. The VM calls this just before our frame exits.
950 * We make a snapshot of the local variables in the frame, then we set our
951 * frame pointer to null to indicate that we no longer have an active frame
952 * in the stack.
953 */
954 void CVmObjFrameRef::inval_frame(VMG0_)
955 {
956 if (ext_ != 0 && get_ext()->fp != 0)
957 {
958 /* make a snapshot of the frame */
959 make_snapshot(vmg0_);
960
961 /* forget our frame pointer */
962 get_ext()->fp = 0;
963 }
964 }
965
966 /*
967 * make a snapshot of the local variables in the frame
968 */
969 void CVmObjFrameRef::make_snapshot(VMG0_)
970 {
971 int i;
972 vm_val_t *v;
973 vm_frameref_ext *ext = get_ext();
974 vm_val_t *fp = ext->fp;
975
976 /* make a copy of each local */
977 for (i = 0, v = ext->vars ; i < ext->nlocals ; ++i, ++v)
978 *v = *G_interpreter->get_local_from_frame(vmg_ fp, i);
979
980 /* make a copy of each parameter */
981 for (i = 0 ; i < ext->nparams ; ++i, ++v)
982 *v = *G_interpreter->get_param_from_frame(vmg_ fp, i);
983 }
984
985 /*
986 * index the frame: this looks up a variable by integer frame index
987 */
988 int CVmObjFrameRef::index_val_q(VMG_ vm_val_t *result,
989 vm_obj_id_t self,
990 const vm_val_t *index_val)
991 {
992 /* get our extension */
993 vm_frameref_ext *ext = get_ext();
994
995 /* check the type */
996 if (index_val->typ == VM_INT)
997 {
998 /*
999 * It's a direct index into the frame. Make sure the value is in
1000 * range.
1001 */
1002 int n = index_val->val.intval;
1003 if (n < 0 || n >= ext->nlocals + ext->nparams)
1004 err_throw(VMERR_INDEX_OUT_OF_RANGE);
1005
1006 /*
1007 * If we have an active stack frame, read the value from the stack;
1008 * otherwise read it from the snapshot array.
1009 */
1010 if (ext->fp != 0)
1011 {
1012 /* get it from the stack frame */
1013 if (n < ext->nlocals)
1014 {
1015 /* it's in the local variable range */
1016 *result =
1017 *G_interpreter->get_local_from_frame(vmg_ ext->fp, n);
1018 }
1019 else
1020 {
1021 /* it's in the parameter range */
1022 *result = *G_interpreter->get_param_from_frame(
1023 vmg_ ext->fp, n - ext->nlocals);
1024 }
1025 }
1026 else
1027 {
1028 /* there's no frame - get it from the snapshot array */
1029 *result = ext->vars[n];
1030 }
1031 }
1032 else
1033 {
1034 /* invalid index type */
1035 err_throw(VMERR_INDEX_OUT_OF_RANGE);
1036 }
1037
1038 /* handled */
1039 return TRUE;
1040 }
1041
1042 /*
1043 * assign an indexed value: this sets a variable's value by index
1044 */
1045 int CVmObjFrameRef::set_index_val_q(VMG_ vm_val_t *new_container,
1046 vm_obj_id_t self,
1047 const vm_val_t *index_val,
1048 const vm_val_t *new_val)
1049 {
1050 /* get our extension */
1051 vm_frameref_ext *ext = get_ext();
1052
1053 /* check the type */
1054 if (index_val->typ == VM_INT)
1055 {
1056 /*
1057 * It's a direct index into the frame. Make sure the value is in
1058 * range.
1059 */
1060 int n = index_val->val.intval;
1061 if (n < 0 || n >= ext->nlocals + ext->nparams)
1062 err_throw(VMERR_INDEX_OUT_OF_RANGE);
1063
1064 /*
1065 * If we have an active stack frame, read the value from the stack;
1066 * otherwise read it from the snapshot array.
1067 */
1068 if (ext->fp != 0)
1069 {
1070 /* get it from the stack frame */
1071 if (n < ext->nlocals)
1072 {
1073 /* it's in the local variable range */
1074 *G_interpreter->get_local_from_frame(vmg_ ext->fp, n) =
1075 *new_val;
1076 }
1077 else
1078 {
1079 /* it's in the parameter range */
1080 *G_interpreter->get_param_from_frame(
1081 vmg_ ext->fp, n - ext->nlocals) = *new_val;
1082 }
1083 }
1084 else
1085 {
1086 /* there's no frame - get it from the snapshot array */
1087 ext->vars[n] = *new_val;
1088 }
1089 }
1090 else
1091 {
1092 /* invalid index type */
1093 err_throw(VMERR_INDEX_OUT_OF_RANGE);
1094 }
1095
1096 /* the container doesn't change */
1097 new_container->set_obj(self);
1098
1099 /* handled */
1100 return TRUE;
1101 }
1102
1103 /*
1104 * Get the frame index for a local. This is the integer value that can be
1105 * used with the [] operator to index the FrameRef object to retrieve or
1106 * assign the value of the local. For a regular local, this is simply the
1107 * same as the local variable number in the frame. For a parameter, this
1108 * is 'n + nlocals', where 'n' is the parameter number in the frame.
1109 */
1110 int CVmObjFrameRef::get_var_frame_index(const CVmDbgFrameSymPtr *symp)
1111 {
1112 /* check to see if it's a local or a parameter */
1113 if (symp->is_local())
1114 {
1115 /* it's a local - use the plain frame index */
1116 return symp->get_var_num();
1117 }
1118 else
1119 {
1120 /* it's a parameter - use the frame index plus the param offset */
1121 return symp->get_var_num() + get_ext()->nlocals;
1122 }
1123 }
1124
1125 /*
1126 * Get the value of a local given the variable descriptor
1127 */
1128 void CVmObjFrameRef::get_local_val(VMG_ vm_val_t *result,
1129 const CVmDbgFrameSymPtr *sym)
1130 {
1131 /*
1132 * if we have an active stack frame, get the value from the frame;
1133 * otherwise get the value from our local snapshot
1134 */
1135 vm_frameref_ext *ext = get_ext();
1136 if (ext->fp != 0)
1137 {
1138 /* we have a frame - get the value from the frame */
1139 G_interpreter->get_local_from_frame(vmg_ result, ext->fp, sym);
1140 }
1141 else
1142 {
1143 /*
1144 * There's no frame, so we must retrieve the value from the
1145 * snapshot. First, get the value of the local or parameter. Our
1146 * snapshot array consists of all of the locals followed by all of
1147 * the parameters, so local N is at vars[N] and parameter N is at
1148 * vars[nlocals + N].
1149 */
1150 vm_val_t *v = &ext->vars[sym->get_var_num()
1151 + (sym->is_param() ? ext->nlocals : 0)];
1152
1153 /*
1154 * If it's a context local, index the local by the context index.
1155 * Otherwise the value is simply the value in the snapshot array.
1156 */
1157 if (sym->is_ctx_local())
1158 v->ll_index(vmg_ result, sym->get_ctx_arr_idx());
1159 else
1160 *result = *v;
1161 }
1162 }
1163
1164 /*
1165 * Set the value of a local given the variable descriptor
1166 */
1167 void CVmObjFrameRef::set_local_val(VMG_ const CVmDbgFrameSymPtr *sym,
1168 const vm_val_t *new_val)
1169 {
1170 /*
1171 * if we have an active stack frame, get the value from the frame;
1172 * otherwise get the value from our local snapshot
1173 */
1174 vm_frameref_ext *ext = get_ext();
1175 if (ext->fp != 0)
1176 {
1177 /* we have a frame - get the value from the frame */
1178 G_interpreter->set_local_in_frame(vmg_ new_val, ext->fp, sym);
1179 }
1180 else
1181 {
1182 /*
1183 * There's no frame, so we must retrieve the value from the
1184 * snapshot. First, get the value of the local or parameter. Our
1185 * snapshot array consists of all of the locals followed by all of
1186 * the parameters, so local N is at vars[N] and parameter N is at
1187 * vars[nlocals + N].
1188 */
1189 vm_val_t *v = &ext->vars[sym->get_var_num()
1190 + (sym->is_param() ? ext->nlocals : 0)];
1191
1192 /*
1193 * If it's a context local, index the local by the context index.
1194 * Otherwise the value is simply the value in the snapshot array.
1195 */
1196 if (sym->is_ctx_local())
1197 {
1198 vm_val_t cont;
1199 vm_val_t ival;
1200 ival.set_int(sym->get_ctx_arr_idx());
1201
1202 if (v->typ == VM_OBJ)
1203 vm_objp(vmg_ v->val.obj)->set_index_val_ov(
1204 vmg_ &cont, v->val.obj, &ival, new_val);
1205 else
1206 err_throw(VMERR_CANNOT_INDEX_TYPE);
1207 }
1208 else
1209 *v = *new_val;
1210 }
1211 }
1212
1213
1214 /*
1215 * Get 'self' from the frame
1216 */
1217 void CVmObjFrameRef::get_self(VMG_ vm_val_t *result)
1218 {
1219 /* return the 'self' value from the frame, if active, or our snapshot */
1220 if (get_ext()->fp != 0)
1221 *result = *G_interpreter->get_self_val_from_frame(vmg_ get_ext()->fp);
1222 else
1223 *result = get_ext()->self;
1224
1225 /* if the return value is an invalid object ID, return nil */
1226 if (result->typ == VM_OBJ && result->val.obj == VM_INVALID_OBJ)
1227 result->set_nil();
1228 }
1229
1230 /*
1231 * Get 'definingobj' from the frame
1232 */
1233 void CVmObjFrameRef::get_defobj(VMG_ vm_val_t *result)
1234 {
1235 /* get it from the active frame, or our snapshot if the frame has exited */
1236 result->set_obj_or_nil(
1237 get_ext()->fp != 0
1238 ? G_interpreter->get_defining_obj_from_frame(vmg_ get_ext()->fp)
1239 : get_ext()->defobj);
1240 }
1241
1242 /*
1243 * Get 'targetobj' from the frame
1244 */
1245 void CVmObjFrameRef::get_targobj(VMG_ vm_val_t *result)
1246 {
1247 /* get 'targetobj' from the frame, if active, or our snapshot */
1248 result->set_obj_or_nil(
1249 get_ext()->fp != 0
1250 ? G_interpreter->get_orig_target_obj_from_frame(vmg_ get_ext()->fp)
1251 : get_ext()->targobj);
1252 }
1253
1254 /*
1255 * Get 'targetprop' from the frame
1256 */
1257 void CVmObjFrameRef::get_targprop(VMG_ vm_val_t *result)
1258 {
1259 /* get 'targetobj' from the frame, if active, or our snapshot */
1260 vm_prop_id_t prop =
1261 get_ext()->fp != 0
1262 ? G_interpreter->get_target_prop_from_frame(vmg_ get_ext()->fp)
1263 : get_ext()->targprop;
1264
1265 /* set the return value to the property, or nil if there isn't one */
1266 if (prop != VM_INVALID_PROP)
1267 result->set_propid(prop);
1268 else
1269 result->set_nil();
1270 }
1271
1272 /*
1273 * Get 'invokee' from the frame
1274 */
1275 void CVmObjFrameRef::get_invokee(VMG_ vm_val_t *result)
1276 {
1277 /* get 'invokee' from the frame, if active, or our snapshot */
1278 *result =
1279 get_ext()->fp != 0
1280 ? *G_interpreter->get_invokee_from_frame(vmg_ get_ext()->fp)
1281 : get_ext()->invokee;
1282 }
1283
1284 /*
1285 * Create a method context object
1286 */
1287 void CVmObjFrameRef::create_loadctx_obj(VMG_ vm_val_t *result)
1288 {
1289 vm_frameref_ext *ext = get_ext();
1290 vm_val_t *fp = ext->fp;
1291 vm_obj_id_t self, defobj, targobj;
1292 vm_prop_id_t targprop;
1293
1294 /* retrieve the method context elements */
1295 if (fp != 0)
1296 {
1297 /* get the context elements from the live stack frame */
1298 self = G_interpreter->get_self_from_frame(vmg_ fp);
1299 defobj = G_interpreter->get_defining_obj_from_frame(vmg_ fp);
1300 targobj = G_interpreter->get_orig_target_obj_from_frame(vmg_ fp);
1301 targprop = G_interpreter->get_target_prop_from_frame(vmg_ fp);
1302 }
1303 else
1304 {
1305 /* the stack frame has exited, so use our snapshot */
1306 self = (ext->self.typ == VM_OBJ ? ext->self.val.obj : VM_INVALID_OBJ);
1307 defobj = ext->defobj;
1308 targobj = ext->targobj;
1309 targprop = ext->targprop;
1310 }
1311
1312 /* create the context object */
1313 CVmRun::create_loadctx_obj(vmg_ result, self, defobj, targobj, targprop);
1314 }
1315