Debian Codegraph @

terps/tads/tads2/run.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/RUN.C,v 1.2 1999/05/17 02:52:13 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1991, 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 run.c - code execution
15 Function
16 Executes p-code
17 Notes
18 Due to the highly mobile memory architecture of this version of TADS,
19 objects and data within objects can move at certain junctures. At
20 these times, pointers to object data become invalid, and it's necessary
21 to re-establish those pointers. Two functions are provided to facilitate
22 this. runcpsav() is called prior to an operation that may move object
23 data; it returns the offset within the object and property of the
24 code being executed, and unlocks the object. runcprst() is called
25 after such an operation; it relocks the object, and returns a pointer
26 to the property data. Note that the special property number zero is
27 used for functions; this indicates that no prpdef structuring is done
28 on the object, but that its actual data start at offset 0.
29 Modified
30 04/11/99 CNebel - Fix warnings.
31 10/20/91 MJRoberts - creation
32 */
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "os.h"
38 #include "run.h"
39 #include "dbg.h"
40 #include "lst.h"
41 #include "obj.h"
42 #include "voc.h"
43 #include "sup.h"
44
45 /* forward declarations */
46 struct bifcxdef;
47
48 /*
49 * Create a new object
50 */
51 static void run_new(runcxdef *ctx, uchar *noreg *codepp,
52 objnum callobj, prpnum callprop)
53 {
54 objnum sc;
55 objnum objn;
56 objdef *objp;
57 int sccnt;
58 vocidef *voci;
59
60 /* get the superclass (nil means no superclass) */
61 if (runtostyp(ctx) == DAT_NIL)
62 sccnt = 0;
63 else
64 {
65 /* get the superclass */
66 sc = runpopobj(ctx);
67 sccnt = 1;
68
69 /* make sure it's not a dynamically-allocated object */
70 voci = vocinh(ctx->runcxvoc, sc);
71 if (voci->vociflg & VOCIFNEW)
72 runsig(ctx, ERR_BADNEWSC);
73 }
74
75 /* create a new object and set its superclass */
76 objp = objnew(ctx->runcxmem, sccnt, 64, &objn, FALSE);
77 if (sccnt) oswp2(objsc(objp), sc);
78
79 /* save undo for the object creation */
80 vocdusave_newobj(ctx->runcxvoc, objn);
81
82 /* touch and unlock the object */
83 mcmtch(ctx->runcxmem, (mcmon)objn);
84 mcmunlck(ctx->runcxmem, (mcmon)objn);
85
86 /* add a vocabulary inheritance record for the new object */
87 vociadd(ctx->runcxvoc, objn, MCMONINV, sccnt, &sc, VOCIFNEW | VOCIFVOC);
88
89 /* set up its vocabulary, inheriting from the class */
90 if (sccnt)
91 supivoc1((struct supcxdef *)0, ctx->runcxvoc,
92 vocinh(ctx->runcxvoc, objn), objn, TRUE, VOCFNEW);
93
94 /* run the constructor */
95 runpprop(ctx, codepp, callobj, callprop, objn, PRP_CONSTRUCT,
96 FALSE, 0, objn);
97 #ifdef NEVER
98 /*
99 * add it to its location's contents list by calling
100 * newobj.moveInto(newobj.location)
101 */
102 runppr(ctx, objn, PRP_LOCATION, 0);
103 if (runtostyp(ctx) == DAT_OBJECT)
104 runppr(ctx, objn, PRP_MOVEINTO, 1);
105 else
106 rundisc(ctx);
107 #endif
108
109 /* return the new object */
110 runpobj(ctx, objn);
111 }
112
113 /*
114 * Delete an object
115 */
116 static void run_delete(runcxdef *ctx, uchar *noreg *codepp,
117 objnum callobj, prpnum callprop)
118 {
119 objnum objn;
120 vocidef *voci;
121 int i;
122 voccxdef *vctx = ctx->runcxvoc;
123
124 /* get the object to be deleted */
125 objn = runpopobj(ctx);
126
127 /* make sure it was allocated with "new" */
128 voci = vocinh(vctx, objn);
129 if (voci == 0 || !(voci->vociflg & VOCIFNEW))
130 runsig(ctx, ERR_BADDEL);
131
132 /* run the destructor */
133 runpprop(ctx, codepp, callobj, callprop, objn, PRP_DESTRUCT,
134 FALSE, 0, objn);
135 #ifdef NEVER
136 /* remove it from its location, if any, by using moveInto(nil) */
137 runpnil(ctx);
138 runppr(ctx, objn, PRP_MOVEINTO, 1);
139 #endif
140
141 /* save undo for the object deletion */
142 vocdusave_delobj(vctx, objn);
143
144 /* delete the object's inheritance and vocabulary records */
145 vocdel(vctx, objn);
146 vocidel(vctx, objn);
147
148 /* forget 'it' if the deleted object is 'it' (or 'them', etc) */
149 if (vctx->voccxit == objn) vctx->voccxit = MCMONINV;
150 if (vctx->voccxhim == objn) vctx->voccxhim = MCMONINV;
151 if (vctx->voccxher == objn) vctx->voccxher = MCMONINV;
152 for (i = 0 ; i < vctx->voccxthc ; ++i)
153 {
154 if (vctx->voccxthm[i] == objn)
155 {
156 /* forget the entire 'them' list when deleting from it */
157 vctx->voccxthc = 0;
158 break;
159 }
160 }
161
162 /* forget the 'again' statistics if necessary */
163 if (vctx->voccxlsd.vocolobj == objn
164 || vctx->voccxlsi.vocolobj == objn
165 || vctx->voccxlsa == objn
166 || vctx->voccxlsv == objn
167 || vctx->voccxlsp == objn)
168 {
169 /* forget the verb */
170 vctx->voccxlsv = MCMONINV;
171
172 /*
173 * note in the flags why we lost the "again" verb, for better
174 * error reporting if the player tries to type "again"
175 */
176 vctx->voccxflg |= VOCCXAGAINDEL;
177 }
178
179 /* delete the memory manager object */
180 mcmfre(ctx->runcxmem, (mcmon)objn);
181 }
182
183
184 /*
185 * invoke a function
186 */
187 void runfn(runcxdef *ctx, noreg objnum objn, int argc)
188 {
189 uchar *fn;
190 int err;
191
192 NOREG((&objn))
193
194 /* get a lock on the object */
195 fn = mcmlck(ctx->runcxmem, objn);
196
197 /* catch any errors, so we can unlock the object */
198 ERRBEGIN(ctx->runcxerr)
199
200 /* execute the object */
201 runexe(ctx, fn, MCMONINV, objn, (prpnum)0, argc);
202
203 /* in case of error, unlock the object and resignal the error */
204 ERRCATCH(ctx->runcxerr, err)
205 mcmunlck(ctx->runcxmem, objn); /* release the lock on the object */
206 if (err < ERR_RUNEXIT || err > ERR_RUNEXITOBJ)
207 dbgdump(ctx->runcxdbg); /* dump the stack */
208 errrse(ctx->runcxerr);
209 ERREND(ctx->runcxerr)
210
211 /* we're done with the object, so unlock it */
212 mcmunlck(ctx->runcxmem, objn);
213 }
214
215 /*
216 * compress the heap - remove unreferenced items
217 */
218 void runhcmp(runcxdef *ctx, uint siz, uint below,
219 runsdef *val1, runsdef *val2, runsdef *val3)
220 {
221 uchar *hp = ctx->runcxheap;
222 uchar *htop = ctx->runcxhp;
223 runsdef *stop = ctx->runcxsp + below;
224 runsdef *stk = ctx->runcxstk;
225 runsdef *sp;
226 uchar *dst = hp;
227 uchar *hnxt;
228 int ref;
229
230 /* go through heap, finding references on stack */
231 for ( ; hp < htop ; hp = hnxt)
232 {
233 hnxt = hp + osrp2(hp); /* remember next heap element */
234
235 for (ref = FALSE, sp = stk ; sp < stop ; ++sp)
236 {
237 switch(sp->runstyp)
238 {
239 case DAT_SSTRING:
240 case DAT_LIST:
241 if (sp->runsv.runsvstr == hp) /* reference to this item? */
242 {
243 ref = TRUE; /* this heap item is referenced */
244 sp->runsv.runsvstr = dst; /* reflect imminent move */
245 }
246 break;
247
248 default: /* other types do not refer to the heap */
249 break;
250 }
251 }
252
253 /* check the explicitly referenced value pointers as well */
254 #define CHECK_VAL(val) \
255 if (val && val->runsv.runsvstr == hp) \
256 ref = TRUE, val->runsv.runsvstr = dst;
257 CHECK_VAL(val1);
258 CHECK_VAL(val2);
259 CHECK_VAL(val3);
260 #undef CHECK_VAL
261
262 /* if referenced, copy it to dst and advance dst */
263 if (ref)
264 {
265 if (hp != dst) memmove(dst, hp, (size_t)osrp2(hp));
266 dst += osrp2(dst);
267 }
268 }
269
270 /* set heap pointer based on shuffled heap */
271 ctx->runcxhp = dst;
272
273 /* check for space requested, and signal error if not available */
274 if ((uint)(ctx->runcxhtop - ctx->runcxhp) < siz)
275 runsig(ctx, ERR_HPOVF);
276 }
277
278 /*
279 * push a value onto the stack that's already been allocated in heap
280 */
281 void runrepush(runcxdef *ctx, runsdef *val)
282 {
283 /* check for stack overflow */
284 runstkovf(ctx);
285
286 OSCPYSTRUCT(*(ctx->runcxsp), *val);
287
288 /* increment stack pointer */
289 ++(ctx->runcxsp);
290 }
291
292 /* push a counted-length string onto the stack */
293 void runpstr(runcxdef *ctx, char *str, int len, int sav)
294 {
295 runsdef val;
296
297 /* allocate space and set up new string */
298 runhres(ctx, len+2, sav);
299 oswp2(ctx->runcxhp, len+2);
300 memcpy(ctx->runcxhp+2, str, (size_t)len);
301
302 /* push return value */
303 val.runsv.runsvstr = ctx->runcxhp;
304 val.runstyp = DAT_SSTRING;
305 ctx->runcxhp += len + 2;
306 runrepush(ctx, &val);
307 }
308
309 /* push a C-style string, converting escape codes */
310 void runpushcstr(runcxdef *ctx, char *str, size_t len, int sav)
311 {
312 char *p;
313 char *dst;
314 size_t need;
315 runsdef val;
316
317 /* determine how much space we'll need after converting escapes */
318 for (p = str, need = len ; p < str + len ; ++p)
319 {
320 switch(*p)
321 {
322 case '\\':
323 case '\n':
324 case '\r':
325 case '\t':
326 /* these characters need to be escaped */
327 ++need;
328 break;
329
330 default:
331 break;
332 }
333 }
334
335 /* reserve space */
336 runhres(ctx, need + 2, sav);
337
338 /* set up the length prefix */
339 oswp2(ctx->runcxhp, need + 2);
340
341 /* copy the string, expanding escapes */
342 for (p = str, dst = (char *)ctx->runcxhp + 2 ; p < str + len ; ++p)
343 {
344 switch(*p)
345 {
346 case '\\':
347 *dst++ = '\\';
348 *dst++ = '\\';
349 break;
350
351 case '\n':
352 case '\r':
353 *dst++ = '\\';
354 *dst++ = 'n';
355 break;
356
357 case '\t':
358 *dst++ = '\\';
359 *dst++ = '\t';
360 break;
361
362 default:
363 *dst++ = *p;
364 break;
365 }
366 }
367
368 /* push the return value */
369 val.runsv.runsvstr = ctx->runcxhp;
370 val.runstyp = DAT_SSTRING;
371 ctx->runcxhp += need + 2;
372 runrepush(ctx, &val);
373 }
374
375 /* push a value onto the stack */
376 void runpush(runcxdef *ctx, dattyp typ, runsdef *val)
377 {
378 int len;
379
380 /* check for stack overflow */
381 runstkovf(ctx);
382
383 OSCPYSTRUCT(*(ctx->runcxsp), *val);
384 ctx->runcxsp->runstyp = typ;
385
386 /* variable-length data must be copied into the heap */
387 if (typ == DAT_SSTRING || typ == DAT_LIST)
388 {
389 len = osrp2(val->runsv.runsvstr);
390 runhres(ctx, len, 0); /* reserve space in heap */
391 memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)len);
392 ctx->runcxsp->runsv.runsvstr = ctx->runcxhp;
393 ctx->runcxhp += len;
394 }
395
396 /* increment stack pointer */
397 ++(ctx->runcxsp);
398 }
399
400 /* push a number onto the stack */
401 void runpnum(runcxdef *ctx, long num)
402 {
403 runsdef val;
404
405 val.runsv.runsvnum = num;
406 runpush(ctx, DAT_NUMBER, &val);
407 }
408
409 /* push an object onto the stack (or nil if obj is MCMONINV) */
410 void runpobj(runcxdef *ctx, objnum obj)
411 {
412 runsdef val;
413
414 if (obj == MCMONINV)
415 runpnil(ctx);
416 else
417 {
418 val.runsv.runsvobj = obj;
419 runpush(ctx, DAT_OBJECT, &val);
420 }
421 }
422
423 /* push nil */
424 void runpnil(runcxdef *ctx)
425 {
426 runsdef val;
427 runpush(ctx, DAT_NIL, &val);
428 }
429
430 /* copy datatype + value from a runsdef into a buffer (such as list) */
431 static void runputbuf(uchar *dstp, runsdef *val)
432 {
433 *dstp++ = val->runstyp;
434 switch(val->runstyp)
435 {
436 case DAT_LIST:
437 case DAT_SSTRING:
438 memcpy(dstp, val->runsv.runsvstr, (size_t)osrp2(val->runsv.runsvstr));
439 break;
440
441 case DAT_NUMBER:
442 oswp4s(dstp, val->runsv.runsvnum);
443 break;
444
445 case DAT_PROPNUM:
446 oswp2(dstp, val->runsv.runsvprp);
447 break;
448
449 case DAT_OBJECT:
450 case DAT_FNADDR:
451 oswp2(dstp, val->runsv.runsvobj);
452 break;
453 }
454 }
455
456 /* push a value from a buffer (list, property, etc) onto stack */
457 void runpbuf(runcxdef *ctx, int typ, void *valp)
458 {
459 runsdef val;
460
461 switch(typ)
462 {
463 case DAT_NUMBER:
464 val.runsv.runsvnum = osrp4s(valp);
465 break;
466
467 case DAT_OBJECT:
468 case DAT_FNADDR:
469 val.runsv.runsvobj = osrp2(valp);
470 break;
471
472 case DAT_PROPNUM:
473 val.runsv.runsvprp = osrp2(valp);
474 break;
475
476 case DAT_SSTRING:
477 case DAT_LIST:
478 val.runsv.runsvstr = (uchar *)valp;
479 break;
480
481 case DAT_NIL:
482 case DAT_TRUE:
483 break;
484 }
485 runpush(ctx, typ, &val);
486 }
487
488 /* compare items at top of stack for equality; TRUE->equal, FALSE->unequal */
489 int runeq(runcxdef *ctx)
490 {
491 runsdef val1, val2;
492
493 /* get values, and see if they have identical type; not equal if not */
494 runpop(ctx, &val1);
495 runpop(ctx, &val2);
496 if (val1.runstyp != val2.runstyp) return(FALSE);
497
498 /* types match, so check values */
499 switch(val1.runstyp)
500 {
501 case DAT_NUMBER:
502 return(val1.runsv.runsvnum == val2.runsv.runsvnum);
503
504 case DAT_SSTRING:
505 case DAT_LIST:
506 return(osrp2(val1.runsv.runsvstr) == osrp2(val2.runsv.runsvstr)
507 && !memcmp(val1.runsv.runsvstr, val2.runsv.runsvstr,
508 (size_t)osrp2(val1.runsv.runsvstr)));
509
510 case DAT_PROPNUM:
511 return(val1.runsv.runsvprp == val2.runsv.runsvprp);
512
513 case DAT_OBJECT:
514 case DAT_FNADDR:
515 return(val1.runsv.runsvobj == val2.runsv.runsvobj);
516
517 default:
518 return(TRUE);
519 }
520 }
521
522 /* compare magnitudes of numbers/strings at top of stack; strcmp-like value */
523 int runmcmp(runcxdef *ctx)
524 {
525 if (runtostyp(ctx) == DAT_NUMBER)
526 {
527 long num2 = runpopnum(ctx);
528 long num1 = runpopnum(ctx);
529
530 if (num1 > num2) return(1);
531 else if (num1 < num2) return(-1);
532 else return(0);
533 }
534 else if (runtostyp(ctx) == DAT_SSTRING)
535 {
536 uchar *str2 = runpopstr(ctx);
537 uchar *str1 = runpopstr(ctx);
538 uint len1 = osrp2(str1) - 2;
539 uint len2 = osrp2(str2) - 2;
540
541 str1 += 2;
542 str2 += 2;
543 while (len1 && len2)
544 {
545 if (*str1 < *str2) return(-1); /* character from 1 is greater */
546 else if (*str1 > *str2) return(1); /* char from 1 is less */
547
548 ++str1;
549 ++str2;
550 --len1;
551 --len2;
552 }
553 if (len1) return(1); /* match up to len2, but string 1 is longer */
554 else if (len2) return(-1); /* match up to len1, but str2 is longer */
555 else return(0); /* strings are identical */
556 }
557 else
558 {
559 runsig(ctx, ERR_INVCMP);
560 }
561 return 0;
562 }
563
564 /* determine size of a runsdef item */
565 int runsiz(runsdef *item)
566 {
567 switch(item->runstyp)
568 {
569 case DAT_NUMBER:
570 return(4);
571 case DAT_SSTRING:
572 case DAT_LIST:
573 return(osrp2(item->runsv.runsvstr));
574 case DAT_PROPNUM:
575 case DAT_OBJECT:
576 case DAT_FNADDR:
577 return(2);
578 default:
579 return(0);
580 }
581 }
582
583 /* find a sublist within a list */
584 uchar *runfind(uchar *lst, runsdef *item)
585 {
586 uint len;
587 uint curlen;
588
589 for (len = osrp2(lst) - 2, lst += 2 ; len ; lst += curlen, len -= curlen)
590 {
591 if (*lst == item->runstyp)
592 {
593 switch(*lst)
594 {
595 case DAT_LIST:
596 case DAT_SSTRING:
597 if (osrp2(lst+1) == osrp2(item->runsv.runsvstr) &&
598 !memcmp(lst+1, item->runsv.runsvstr, (size_t)osrp2(lst+1)))
599 return(lst);
600 break;
601 case DAT_NUMBER:
602 if (osrp4s(lst+1) == item->runsv.runsvnum)
603 return(lst);
604 break;
605
606 case DAT_TRUE:
607 case DAT_NIL:
608 return(lst);
609
610 case DAT_OBJECT:
611 case DAT_FNADDR:
612 if (osrp2(lst+1) == item->runsv.runsvobj)
613 return(lst);
614 break;
615
616 case DAT_PROPNUM:
617 if (osrp2(lst+1) == item->runsv.runsvprp)
618 return(lst);
619 break;
620 }
621 }
622 curlen = datsiz(*lst, lst+1) + 1;
623 }
624 return((uchar *)0);
625 }
626
627 /* add values */
628 void runadd(runcxdef *ctx, runsdef *val, runsdef *val2, uint below)
629 {
630 if (val->runstyp == DAT_LIST)
631 {
632 int len1 = osrp2(val->runsv.runsvstr);
633 int len2 = runsiz(val2);
634 int newlen;
635
636 /* if concatenating a list, take out length + datatype from 2nd */
637 if (val2->runstyp == DAT_LIST)
638 newlen = len1 + len2 - 2; /* leave out second list len */
639 else
640 newlen = len1 + len2 + 1; /* add in datatype header */
641
642 /* get space in heap, copy first list, and set new length */
643 runhres2(ctx, newlen, below, val, val2);
644 memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)len1);
645 oswp2(ctx->runcxhp, newlen);
646
647 /* append the new element or list of elements */
648 if (val2->runstyp == DAT_LIST)
649 memcpy(ctx->runcxhp + len1, val2->runsv.runsvstr + 2,
650 (size_t)(len2 - 2));
651 else
652 runputbuf(ctx->runcxhp + len1, val2);
653
654 /* set up return value and update heap pointer */
655 val->runsv.runsvstr = ctx->runcxhp;
656 ctx->runcxhp += newlen;
657 }
658 else if (val->runstyp==DAT_SSTRING && val2->runstyp==DAT_SSTRING)
659 {
660 int len1 = osrp2(val->runsv.runsvstr);
661 int len2 = osrp2(val2->runsv.runsvstr);
662
663 /* reserve space, and concatenate the two strings */
664 runhres2(ctx, len1 + len2 - 2, below, val, val2);
665 memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)len1);
666 memcpy(ctx->runcxhp + len1, val2->runsv.runsvstr + 2,
667 (size_t)len2 - 2);
668
669 /* set length to sum of two lengths, minus 2nd length word */
670 oswp2(ctx->runcxhp, len1 + len2 - 2);
671 val->runsv.runsvstr = ctx->runcxhp;
672 ctx->runcxhp += len1 + len2 - 2;
673 }
674 else if (val->runstyp == DAT_NUMBER && val2->runstyp == DAT_NUMBER)
675 val->runsv.runsvnum += val2->runsv.runsvnum;
676 else
677 runsig(ctx, ERR_INVADD);
678 }
679
680 /* returns TRUE if value changed */
681 int runsub(runcxdef *ctx, runsdef *val, runsdef *val2, uint below)
682 {
683 if (val->runstyp == DAT_LIST)
684 {
685 uchar *sublist;
686 int subsize;
687 int listsize;
688 int part1sz;
689
690 if (val2->runstyp == DAT_LIST)
691 {
692 uchar *p1;
693 uchar *p2;
694 uint rem1;
695 uint rem2;
696 uchar *dst;
697
698 /* reserve space for another copy of first list */
699 listsize = runsiz(val);
700 runhres2(ctx, listsize, below, val, val2);
701 dst = ctx->runcxhp + 2;
702
703 /* get pointer to first list */
704 p1 = val->runsv.runsvstr;
705 rem1 = osrp2(p1) - 2;
706 p1 += 2;
707
708 /*
709 * loop through left list, copying elements to output if
710 * not in the right list
711 */
712 for ( ; rem1 ; lstadv(&p1, &rem1))
713 {
714 int found = FALSE;
715
716 /* find current element of first list in second list */
717 p2 = val2->runsv.runsvstr;
718 rem2 = osrp2(p2) - 2;
719 p2 += 2;
720 for ( ; rem2 ; lstadv(&p2, &rem2))
721 {
722 if (*p1 == *p2)
723 {
724 int siz1 = datsiz(*p1, p1+1);
725 int siz2 = datsiz(*p2, p2+1);
726
727 if (siz1 == siz2 &&
728 (siz1 == 0 || !memcmp(p1+1, p2+1, (size_t)siz1)))
729 {
730 found = TRUE;
731 break;
732 }
733 }
734 }
735
736 /* if this element wasn't found, copy to output list */
737 if (!found)
738 {
739 uint siz;
740
741 *dst++ = *p1;
742 if ((siz = datsiz(*p1, p1+1)) != 0)
743 {
744 memcpy(dst, p1+1, siz);
745 dst += siz;
746 }
747 }
748 }
749
750 /* we've built the list; write size and we're done */
751 oswp2(ctx->runcxhp, dst - ctx->runcxhp);
752 val->runsv.runsvstr = ctx->runcxhp;
753 ctx->runcxhp = dst;
754 }
755 else if ((sublist = runfind(val->runsv.runsvstr, val2)) != 0)
756 {
757 subsize = datsiz(*sublist, sublist + 1) + 1;
758 listsize = runsiz(val);
759 part1sz = sublist - (uchar *)val->runsv.runsvstr;
760
761 runhres2(ctx, listsize - subsize, below, val, val2);
762 memcpy(ctx->runcxhp, val->runsv.runsvstr, (size_t)part1sz);
763 memcpy(ctx->runcxhp + part1sz, sublist + subsize,
764 (size_t)(listsize - subsize - part1sz));
765 oswp2(ctx->runcxhp, listsize - subsize);
766 val->runsv.runsvstr = ctx->runcxhp;
767 ctx->runcxhp += listsize - subsize;
768 }
769 else
770 {
771 return(FALSE); /* no change - value can be re-pushed */
772 }
773 }
774 else if (val->runstyp == DAT_NUMBER && val2->runstyp == DAT_NUMBER)
775 val->runsv.runsvnum -= val2->runsv.runsvnum;
776 else
777 runsig(ctx, ERR_INVSUB);
778
779 return(TRUE); /* value has changed; must be pushed anew */
780 }
781
782 /* return code pointer offset */
783 static uint runcpsav(runcxdef *ctx, uchar *noreg *cp, objnum obj, prpnum prop)
784 {
785 uint ofs;
786
787 VARUSED(prop);
788
789 /* get offset from start of object */
790 ofs = *cp - mcmobjptr(ctx->runcxmem, (mcmon)obj);
791
792 /* clear the pointer so the caller knows the object is unlocked */
793 *cp = 0;
794
795 /* unlock the object, and return the derived offset */
796 mcmunlck(ctx->runcxmem, (mcmon)obj);
797 return(ofs);
798 }
799
800 /* restore code pointer based on object.property */
801 uchar *runcprst(runcxdef *ctx, uint ofs, objnum obj, prpnum prop)
802 {
803 uchar *ptr;
804
805 VARUSED(prop);
806
807 /* lock object, and get pointer based on offset */
808 ptr = mcmlck(ctx->runcxmem, (mcmon)obj) + ofs;
809
810 return(ptr);
811 }
812
813 /* get offset of an element within a list */
814 static uint runindofs(runcxdef *ctx, uint indx, uchar *lstp)
815 {
816 uint lstsiz;
817 uchar *orgp = lstp;
818
819 /* verify that index is in range */
820 if (indx <= 0) runsig(ctx, ERR_LOWINX);
821
822 /* get list's size, and point to its data string */
823 lstsiz = osrp2(lstp) - 2;
824 lstp += 2;
825
826 /* skip the first indx-1 elements */
827 for (--indx ; indx && lstsiz ; --indx) lstadv(&lstp, &lstsiz);
828
829 /* if we ran out of list, the index is out of range */
830 if (!lstsiz) runsig(ctx, ERR_HIGHINX);
831
832 /* return the offset */
833 return((uint)(lstp - orgp));
834 }
835
836 /* push an indexed element of a list; index is tos, list is next on stack */
837 static void runpind(runcxdef *ctx, uint indx, uchar *lstp)
838 {
839 uchar *ele;
840 runsdef val;
841
842 /* find the element we want to push */
843 ele = lstp + runindofs(ctx, indx, lstp);
844
845 /* reserve space first, in case lstp gets moved around */
846 val.runstyp = DAT_LIST;
847 val.runsv.runsvstr = lstp;
848 runhres1(ctx, datsiz(*ele, ele + 1), 0, &val);
849 if (val.runsv.runsvstr != lstp)
850 ele = val.runsv.runsvstr + runindofs(ctx, indx, val.runsv.runsvstr);
851
852 /* push the operand */
853 runpbuf(ctx, *ele, ele+1);
854 }
855
856 /*
857 * Check a property to ensure that it's a data property. Throws an
858 * error if the property contains a method. This is used for debugger
859 * speculative evaluation to ensure that we don't call any methods from
860 * within speculative expressions.
861 */
862 static void runcheckpropdata(runcxdef *ctx, objnum obj, prpnum prop)
863 {
864 uint pofs;
865 objnum target;
866 objdef *objptr;
867 prpdef *prpptr;
868 int typ;
869
870 /* if the object is invalid, it's an error */
871 if (obj == MCMONINV)
872 errsig(ctx->runcxerr, ERR_REQVOB);
873
874 /* get the property */
875 pofs = objgetap(ctx->runcxmem, obj, prop, &target, FALSE);
876
877 /* if there's no property, it's okay - it will just return nil */
878 if (pofs == 0)
879 return;
880
881 /* get the object */
882 objptr = mcmlck(ctx->runcxmem, target);
883
884 /* get the property */
885 prpptr = (prpdef *)(((uchar *)objptr) + pofs);
886 typ = prptype(prpptr);
887
888 /* we're done with the object's memory now */
889 mcmunlck(ctx->runcxmem, target);
890
891 /* check the type */
892 switch(typ)
893 {
894 case DAT_CODE:
895 case DAT_DSTRING:
896 /*
897 * we can't call code or evaluate (i.e., print) double-quoted
898 * strings during speculative evaluation
899 */
900 errsig(ctx->runcxerr, ERR_RTBADSPECEXPR);
901
902 default:
903 /* other types do not involve method calls, so they're okay */
904 break;
905 }
906 }
907
908 /* push an object's property */
909 void runpprop(runcxdef *ctx, uchar *noreg *codepp,
910 objnum callobj, prpnum callprop,
911 noreg objnum obj, prpnum prop, int inh, int argc, objnum self)
912 {
913 uint pofs;
914 uint saveofs;
915 objdef *objptr;
916 prpdef *prpptr;
917 uchar *val;
918 int typ;
919 runsdef sval;
920 objnum target;
921 int times_through = 0;
922 int err;
923 objnum otherobj;
924
925 NOREG((&obj, &codepp));
926
927 if (obj == MCMONINV) runsig(ctx, ERR_RUNNOBJ);
928
929 startover:
930 pofs = objgetap(ctx->runcxmem, obj, prop, &target, inh);
931
932 /* if nothing was found, push nil */
933 if (!pofs)
934 {
935 runpush(ctx, DAT_NIL, &sval);
936 return;
937 }
938
939 /* found a property; get the prpdef, and the value and type of data */
940 objptr = mcmlck(ctx->runcxmem, target);
941 ERRBEGIN(ctx->runcxerr) /* catch errors so we can unlock object */
942
943 prpptr = (prpdef *)(((uchar *)objptr) + pofs);
944 val = prpvalp(prpptr);
945 typ = prptype(prpptr);
946
947 /* determine what to do based on property type */
948 switch(typ)
949 {
950 case DAT_CODE:
951 /* save caller's code offset - caller's object may move */
952 if (codepp)
953 saveofs = runcpsav(ctx, codepp, callobj, callprop);
954
955 /* execute the code */
956 runexe(ctx, val, self, target, prop, argc);
957
958 /* restore caller's code pointer in case object moved */
959 if (codepp)
960 *codepp = runcprst(ctx, saveofs, callobj, callprop);
961 break;
962
963 case DAT_REDIR:
964 otherobj = osrp2(val);
965 break;
966
967 case DAT_DSTRING:
968 outfmt(ctx->runcxtio, val);
969 break;
970
971 case DAT_DEMAND:
972 break;
973
974 default:
975 runpbuf(ctx, typ, val);
976 break;
977 }
978
979 /* we're done - unlock the object */
980 mcmunlck(ctx->runcxmem, target);
981
982 /* if it's redirected, redirect it now */
983 if (typ == DAT_REDIR)
984 {
985 runpprop(ctx, codepp, callobj, callprop, otherobj, prop,
986 FALSE, argc, otherobj);
987 }
988
989 /* if an error occurs, unlock the object, and resignal the error */
990 ERRCATCH(ctx->runcxerr, err)
991 mcmunlck(ctx->runcxmem, target);
992 if (err < ERR_RUNEXIT || err > ERR_RUNEXITOBJ)
993 dbgdump(ctx->runcxdbg); /* dump the stack */
994 errrse(ctx->runcxerr);
995 ERREND(ctx->runcxerr)
996
997 /* apply special handling for set-on-first-use data */
998 if (typ == DAT_DEMAND)
999 {
1000 /*
1001 * if we've already done this, the property isn't being set by
1002 * the callback, so we'll never get out of this loop - abort if
1003 * so
1004 */
1005 if (++times_through != 1)
1006 runsig(ctx, ERR_DMDLOOP);
1007
1008 /* save caller's code offset - caller's object may move */
1009 if (codepp)
1010 saveofs = runcpsav(ctx, codepp, callobj, callprop);
1011
1012 /* invoke the callback to set the property on demand */
1013 (*ctx->runcxdmd)(ctx->runcxdmc, obj, prop);
1014
1015 /* restore caller's code pointer */
1016 if (codepp)
1017 *codepp = runcprst(ctx, saveofs, callobj, callprop);
1018
1019 /* try again now that it's been set up */
1020 goto startover;
1021 }
1022 }
1023
1024 /* ======================================================================== */
1025 /*
1026 * user exit callbacks
1027 */
1028
1029 static int runuftyp(runuxdef *ctx)
1030 {
1031 return(runtostyp(ctx->runuxctx));
1032 }
1033
1034 static long runufnpo(runuxdef *ctx)
1035 {
1036 return(runpopnum(ctx->runuxctx));
1037 }
1038
1039 static uchar *runufspo(runuxdef *ctx)
1040 {
1041 return(runpopstr(ctx->runuxctx));
1042 }
1043
1044 static void runufdsc(runuxdef *ctx)
1045 {
1046 rundisc(ctx->runuxctx);
1047 }
1048
1049 static void runufnpu(runuxdef *ctx, long num)
1050 {
1051 runpnum(ctx->runuxctx, num);
1052 }
1053
1054 static void runufspu(runuxdef *ctx, uchar *str)
1055 {
1056 runsdef val;
1057
1058 val.runstyp = DAT_SSTRING;
1059 val.runsv.runsvstr = str - 2;
1060 runrepush(ctx->runuxctx, &val);
1061 }
1062
1063 static void runufcspu(runuxdef *ctx, char *str)
1064 {
1065 runpstr(ctx->runuxctx, str, (int)strlen(str), ctx->runuxargc);
1066 }
1067
1068 static uchar *runufsal(runuxdef *ctx, int len)
1069 {
1070 uchar *ret;
1071
1072 len += 2;
1073 runhres(ctx->runuxctx, len, ctx->runuxargc);
1074 ret = ctx->runuxctx->runcxhp;
1075 oswp2(ret, len);
1076 ret += 2;
1077
1078 ctx->runuxctx->runcxhp += len;
1079 return(ret);
1080 }
1081
1082 static void runuflpu(runuxdef *ctx, int typ)
1083 {
1084 runsdef val;
1085
1086 val.runstyp = typ;
1087 runrepush(ctx->runuxctx, &val);
1088 }
1089
1090
1091
1092 /* convert an osrp2 value to a signed short value */
1093 #define runrp2s(p) ((short)(ushort)osrp2(p))
1094
1095
1096 /* ======================================================================== */
1097 /*
1098 * execute p-code
1099 */
1100 void runexe(runcxdef *ctx, uchar *p0, objnum self, objnum target,
1101 prpnum targprop, int argc)
1102 {
1103 uchar *noreg p = p0;
1104 uchar opc; /* opcode we're currently working on */
1105 runsdef val; /* stack element (for pushing) */
1106 runsdef val2; /* another one (for popping in two-op instructions) */
1107 uint ofs; /* offset in code of current execution */
1108 prpnum prop; /* property number, when needed */
1109 objnum obj; /* object number, when needed */
1110 runsdef *noreg rstsp; /* sp to reset to on DISCARD instructions */
1111 uchar *lstp; /* list pointer */
1112 int nargc; /* argument count of called function */
1113 runsdef *valp;
1114 runsdef *stkval;
1115 int i;
1116 int brkchk;
1117
1118 #ifndef DBG_OFF
1119 int err;
1120 #endif
1121
1122 NOREG((&rstp, &p));
1123
1124 /* save entry SP - this is reset point until ENTER */
1125 rstsp = ctx->runcxsp;
1126
1127 #ifndef DBG_OFF
1128 /*
1129 * For the debugger's sake, set up an error frame so that we catch
1130 * any errors thrown during p-code execution within this function.
1131 * If an error occurs, and the debugger is present, we'll set the
1132 * instruction pointer back to the start of the line that caused the
1133 * error and enter the debugger with the error indication. If the
1134 * debugger isn't present, we'll simply re-throw the error. This
1135 * entire block can be compiled out of the execution engine when
1136 * linking a stand-alone (non-debug) version of the run-time.
1137 */
1138 resume_from_error:
1139 ERRBEGIN(ctx->runcxerr)
1140 #endif /* DBG_OFF */
1141
1142 for (brkchk = 0 ;; ++brkchk)
1143 {
1144 /* check for break - signal if user has hit break */
1145 if (brkchk == 1000)
1146 {
1147 brkchk = 0;
1148 if (os_break()) runsig(ctx, ERR_USRINT);
1149 }
1150
1151 opc = *p++;
1152
1153 switch(opc)
1154 {
1155 case OPCPUSHNUM:
1156 val.runsv.runsvnum = osrp4s(p);
1157 runpush(ctx, DAT_NUMBER, &val);
1158 p += 4;
1159 break;
1160
1161 case OPCPUSHOBJ:
1162 val.runsv.runsvobj = osrp2(p);
1163 runpush(ctx, DAT_OBJECT, &val);
1164 p += 2;
1165 break;
1166
1167 case OPCPUSHSELF:
1168 val.runsv.runsvobj = self;
1169 runpush(ctx, DAT_OBJECT, &val);
1170 break;
1171
1172 case OPCPUSHSTR:
1173 val.runsv.runsvstr = p;
1174 runpush(ctx, DAT_SSTRING, &val);
1175 p += osrp2(p); /* skip past string */
1176 break;
1177
1178 case OPCPUSHLST:
1179 val.runsv.runsvstr = p;
1180 runpush(ctx, DAT_LIST, &val);
1181 p += osrp2(p); /* skip past list */
1182 break;
1183
1184 case OPCPUSHNIL:
1185 runpush(ctx, DAT_NIL, &val);
1186 break;
1187
1188 case OPCPUSHTRUE:
1189 runpush(ctx, DAT_TRUE, &val);
1190 break;
1191
1192 case OPCPUSHFN:
1193 val.runsv.runsvobj = osrp2(p);
1194 runpush(ctx, DAT_FNADDR, &val);
1195 p += 2;
1196 break;
1197
1198 case OPCPUSHPN:
1199 val.runsv.runsvprp = osrp2(p);
1200 runpush(ctx, DAT_PROPNUM, &val);
1201 p += 2;
1202 break;
1203
1204 case OPCNEG:
1205 val.runstyp = DAT_NUMBER;
1206 val.runsv.runsvnum = -runpopnum(ctx);
1207 runrepush(ctx, &val);
1208 break;
1209
1210 case OPCBNOT:
1211 val.runstyp = DAT_NUMBER;
1212 val.runsv.runsvnum = ~runpopnum(ctx);
1213 runrepush(ctx, &val);
1214 break;
1215
1216 case OPCNOT:
1217 if (runtoslog(ctx))
1218 runpush(ctx, runclog(!runpoplog(ctx)), &val);
1219 else
1220 runpush(ctx, runclog(runpopnum(ctx)), &val);
1221 break;
1222
1223 case OPCADD:
1224 runpop(ctx, &val2); /* right op is pushed last -> popped 1st */
1225 runpop(ctx, &val);
1226 runadd(ctx, &val, &val2, 2);
1227 runrepush(ctx, &val);
1228 break;
1229
1230 case OPCSUB:
1231 runpop(ctx, &val2); /* right op is pushed last -> popped 1st */
1232 runpop(ctx, &val);
1233 (void)runsub(ctx, &val, &val2, 2);
1234 runrepush(ctx, &val);
1235 break;
1236
1237 case OPCMUL:
1238 val.runstyp = DAT_NUMBER;
1239 val.runsv.runsvnum = runpopnum(ctx);
1240 val.runsv.runsvnum *= runpopnum(ctx);
1241 runrepush(ctx, &val);
1242 break;
1243
1244 case OPCBAND:
1245 val.runstyp = DAT_NUMBER;
1246 val.runsv.runsvnum = runpopnum(ctx);
1247 val.runsv.runsvnum &= runpopnum(ctx);
1248 runrepush(ctx, &val);
1249 break;
1250
1251 case OPCBOR:
1252 val.runstyp = DAT_NUMBER;
1253 val.runsv.runsvnum = runpopnum(ctx);
1254 val.runsv.runsvnum |= runpopnum(ctx);
1255 runrepush(ctx, &val);
1256 break;
1257
1258 case OPCSHL:
1259 val.runstyp = DAT_NUMBER;
1260 val.runsv.runsvnum = runpopnum(ctx);
1261 val.runsv.runsvnum = runpopnum(ctx) << val.runsv.runsvnum;
1262 runrepush(ctx, &val);
1263 break;
1264
1265 case OPCSHR:
1266 val.runstyp = DAT_NUMBER;
1267 val.runsv.runsvnum = runpopnum(ctx);
1268 val.runsv.runsvnum = runpopnum(ctx) >> val.runsv.runsvnum;
1269 runrepush(ctx, &val);
1270 break;
1271
1272 case OPCXOR:
1273 /* allow logical ^ logical or number ^ number */
1274 if (runtoslog(ctx))
1275 {
1276 int a, b;
1277
1278 /* logicals - return a logical value */
1279 a = runpoplog(ctx);
1280 b = runpoplog(ctx);
1281 val.runstyp = runclog(a ^ b);
1282 }
1283 else
1284 {
1285 /* numeric value - return binary xor */
1286 val.runstyp = DAT_NUMBER;
1287 val.runsv.runsvnum = runpopnum(ctx);
1288 val.runsv.runsvnum ^= runpopnum(ctx);
1289 }
1290 runrepush(ctx, &val);
1291 break;
1292
1293 case OPCDIV:
1294 val.runsv.runsvnum = runpopnum(ctx);
1295 if (val.runsv.runsvnum == 0)
1296 runsig(ctx, ERR_DIVZERO);
1297 val.runsv.runsvnum = runpopnum(ctx) / val.runsv.runsvnum;
1298 val.runstyp = DAT_NUMBER;
1299 runrepush(ctx, &val);
1300 break;
1301
1302 case OPCMOD:
1303 val.runsv.runsvnum = runpopnum(ctx);
1304 if (val.runsv.runsvnum == 0)
1305 runsig(ctx, ERR_DIVZERO);
1306 val.runsv.runsvnum = runpopnum(ctx) % val.runsv.runsvnum;
1307 val.runstyp = DAT_NUMBER;
1308 runrepush(ctx, &val);
1309 break;
1310
1311 #ifdef NEVER
1312 case OPCAND:
1313 if (runtostyp(ctx) == DAT_LIST)
1314 runlstisect(ctx);
1315 else
1316 runpush(ctx, runclog(runpoplog(ctx) && runpoplog(ctx)), &val);
1317 break;
1318
1319 case OPCOR:
1320 runpush(ctx, runclog(runpoplog(ctx) || runpoplog(ctx)), &val);
1321 break;
1322 #endif /* NEVER */
1323
1324 case OPCEQ:
1325 runpush(ctx, runclog(runeq(ctx)), &val);
1326 break;
1327
1328 case OPCNE:
1329 runpush(ctx, runclog(!runeq(ctx)), &val);
1330 break;
1331
1332 case OPCLT:
1333 runpush(ctx, runclog(runmcmp(ctx) < 0), &val);
1334 break;
1335
1336 case OPCLE:
1337 runpush(ctx, runclog(runmcmp(ctx) <= 0), &val);
1338 break;
1339
1340 case OPCGT:
1341 runpush(ctx, runclog(runmcmp(ctx) > 0), &val);
1342 break;
1343
1344 case OPCGE:
1345 runpush(ctx, runclog(runmcmp(ctx) >= 0), &val);
1346 break;
1347
1348 case OPCCALL:
1349 {
1350 objnum o;
1351
1352 /* get the argument count */
1353 nargc = *p++;
1354
1355 /* ensure we have enough values to pass as arguments */
1356 runcheckargc(ctx, &nargc);
1357
1358 /* object could move--save offset to restore 'p' after call */
1359 o = osrp2(p);
1360 ofs = runcpsav(ctx, &p, target, targprop);
1361
1362 /* execute the function */
1363 runfn(ctx, o, nargc);
1364
1365 /* restore code pointer in case target object moved */
1366 p = runcprst(ctx, ofs, target, targprop) + 2;
1367 break;
1368 }
1369
1370 case OPCGETP:
1371 nargc = *p++;
1372 runcheckargc(ctx, &nargc);
1373 prop = osrp2(p);
1374 p += 2;
1375 obj = runpopobj(ctx);
1376 runpprop(ctx, &p, target, targprop, obj, prop, FALSE, nargc,
1377 obj);
1378 break;
1379
1380 case OPCGETPDATA:
1381 prop = osrp2(p);
1382 p += 2;
1383 obj = runpopobj(ctx);
1384 runcheckpropdata(ctx, obj, prop);
1385 runpprop(ctx, &p, target, targprop, obj, prop, FALSE, 0, obj);
1386 break;
1387
1388 case OPCGETDBLCL:
1389 #ifdef DBG_OFF
1390 /* non-debug mode - this will always throw an error */
1391 dbgfrfind(ctx->runcxdbg, 0, 0);
1392 #else
1393 /* debug mode - look up the local in the stack frame */
1394 {
1395 objnum frobj;
1396 uint frofs;
1397 runsdef *otherbp;
1398
1399 frobj = osrp2(p);
1400 frofs = osrp2(p + 2);
1401 otherbp = dbgfrfind(ctx->runcxdbg, frobj, frofs);
1402 runrepush(ctx, otherbp + runrp2s(p + 4) - 1);
1403 p += 6;
1404 }
1405 break;
1406 #endif
1407
1408 case OPCGETLCL:
1409 runrepush(ctx, ctx->runcxbp + runrp2s(p) - 1);
1410 p += 2;
1411 break;
1412
1413 case OPCRETURN:
1414 runleave(ctx, argc /* was: osrp2(p) */);
1415 dbgleave(ctx->runcxdbg, DBGEXRET);
1416 goto done;
1417
1418 case OPCRETVAL:
1419 /* if there's nothing on the stack, return nil */
1420 if (runtostyp(ctx) != DAT_BASEPTR)
1421 runpop(ctx, &val);
1422 else
1423 val.runstyp = DAT_NIL;
1424
1425 runleave(ctx, argc /* was: osrp2(p) */);
1426 runrepush(ctx, &val);
1427 dbgleave(ctx->runcxdbg, DBGEXVAL);
1428 goto done;
1429
1430 case OPCENTER:
1431 /* push old base pointer and set up new one */
1432 ctx->runcxsp = rstsp;
1433 val.runsv.runsvstr = (uchar *)ctx->runcxbp;
1434 runpush(ctx, DAT_BASEPTR, &val);
1435 ctx->runcxbp = ctx->runcxsp;
1436
1437 /* add a trace record */
1438 dbgenter(ctx->runcxdbg, ctx->runcxbp, self, target, targprop,
1439 0, argc);
1440
1441 /* initialize locals to nil */
1442 for (i = osrp2(p) ; i ; --i) runpush(ctx, DAT_NIL, &val);
1443 p += 2; /* skip the local count operand */
1444
1445 /* save stack pointer - reset sp to this value on DISCARD */
1446 rstsp = ctx->runcxsp;
1447 break;
1448
1449 case OPCDISCARD:
1450 ctx->runcxsp = rstsp;
1451 break;
1452
1453 case OPCSWITCH:
1454 {
1455 int i;
1456 int tostyp;
1457 int match, typmatch;
1458
1459 runpop(ctx, &val);
1460 tostyp = val.runstyp;
1461 switch(tostyp)
1462 {
1463 case DAT_SSTRING:
1464 tostyp = OPCPUSHSTR;
1465 break;
1466 case DAT_LIST:
1467 tostyp = OPCPUSHLST;
1468 break;
1469 case DAT_PROPNUM:
1470 tostyp = OPCPUSHPN;
1471 break;
1472 case DAT_FNADDR:
1473 tostyp = OPCPUSHFN;
1474 break;
1475 case DAT_TRUE:
1476 tostyp = OPCPUSHTRUE;
1477 break;
1478 case DAT_NIL:
1479 tostyp = OPCPUSHNIL;
1480 break;
1481 }
1482
1483 p += osrp2(p); /* find the switch table */
1484 i = osrp2(p); /* get number of cases */
1485
1486 /* look for a matching case */
1487 for (match = FALSE ; i && !match ; --i)
1488 {
1489 p += 2; /* skip previous jump/size word */
1490 typmatch = (*p == tostyp);
1491 switch(*p++)
1492 {
1493 case OPCPUSHNUM:
1494 match = (typmatch
1495 && val.runsv.runsvnum == osrp4s(p));
1496 p += 4;
1497 break;
1498
1499 case OPCPUSHLST:
1500 case OPCPUSHSTR:
1501 match = (typmatch
1502 && osrp2(val.runsv.runsvstr) == osrp2(p)
1503 && !memcmp(val.runsv.runsvstr,
1504 p, (size_t)osrp2(p)));
1505 p += runrp2s(p);
1506 break;
1507
1508 case OPCPUSHPN:
1509 match = (typmatch
1510 && val.runsv.runsvprp == osrp2(p));
1511 p += 2;
1512 break;
1513
1514 case OPCPUSHOBJ:
1515 case OPCPUSHFN:
1516 match = (typmatch
1517 && val.runsv.runsvobj == osrp2(p));
1518 p += 2;
1519 break;
1520
1521 case OPCPUSHSELF:
1522 match = (typmatch && val.runsv.runsvobj == self);
1523 break;
1524
1525 case OPCPUSHTRUE:
1526 case OPCPUSHNIL:
1527 match = typmatch;
1528 break;
1529 }
1530 }
1531
1532 if (!match) p += 2; /* if default, skip to default case */
1533 p += runrp2s(p); /* wherever we left off, p points to jump */
1534 break;
1535 }
1536
1537 case OPCJMP:
1538 p += runrp2s(p);
1539 break;
1540
1541 case OPCJT:
1542 if (runtoslog(ctx))
1543 p += (runpoplog(ctx) ? runrp2s(p) : 2);
1544 else
1545 p += (runpopnum(ctx) != 0 ? runrp2s(p) : 2);
1546 break;
1547
1548 case OPCJF:
1549 if (runtoslog(ctx))
1550 p += ((!runpoplog(ctx)) ? runrp2s(p) : 2);
1551 else if (runtostyp(ctx) == DAT_NUMBER)
1552 p += ((runpopnum(ctx) == 0) ? runrp2s(p) : 2);
1553 else /* consider any other type to be true */
1554 {
1555 rundisc(ctx); /* throw away the item considered to be true */
1556 p += 2;
1557 }
1558 break;
1559
1560 case OPCSAY:
1561 outfmt(ctx->runcxtio, p);
1562 p += osrp2(p); /* skip past string */
1563 break;
1564
1565 case OPCBUILTIN:
1566 {
1567 int binum;
1568 runsdef *stkp;
1569
1570 nargc = *p++;
1571 runcheckargc(ctx, &nargc);
1572 binum = osrp2(p);
1573 ofs = runcpsav(ctx, &p, target, targprop);
1574 stkp = ctx->runcxsp - nargc;
1575
1576 dbgenter(ctx->runcxdbg, ctx->runcxsp + 1, MCMONINV, MCMONINV,
1577 (prpnum)0, binum, nargc);
1578 (*ctx->runcxbi[binum])((struct bifcxdef *)ctx->runcxbcx,
1579 nargc);
1580 dbgleave(ctx->runcxdbg,
1581 ctx->runcxsp != stkp ? DBGEXVAL : DBGEXRET);
1582
1583 p = runcprst(ctx, ofs, target, targprop);
1584 p += 2;
1585 break;
1586 }
1587
1588 case OPCPTRCALL:
1589 nargc = *p++;
1590 runcheckargc(ctx, &nargc);
1591 ofs = runcpsav(ctx, &p, target, targprop);
1592 runfn(ctx, runpopfn(ctx), nargc);
1593 p = runcprst(ctx, ofs, target, targprop);
1594 break;
1595
1596 case OPCINHERIT:
1597 nargc = *p++;
1598 runcheckargc(ctx, &nargc);
1599 prop = osrp2(p);
1600 p += 2;
1601 runpprop(ctx, &p, target, targprop, target, prop, TRUE, nargc,
1602 self);
1603 break;
1604
1605 case OPCPTRINH:
1606 nargc = *p++;
1607 runcheckargc(ctx, &nargc);
1608 prop = runpopprp(ctx);
1609 runpprop(ctx, &p, target, targprop, target, prop, TRUE, nargc,
1610 self);
1611 break;
1612
1613 case OPCPTRGETP:
1614 nargc = *p++;
1615 runcheckargc(ctx, &nargc);
1616 prop = runpopprp(ctx);
1617 obj = runpopobj(ctx);
1618 runpprop(ctx, &p, target, targprop, obj, prop, FALSE, nargc,
1619 obj);
1620 break;
1621
1622 case OPCPTRGETPDATA:
1623 prop = runpopprp(ctx);
1624 obj = runpopobj(ctx);
1625 runcheckpropdata(ctx, obj, prop);
1626 runpprop(ctx, &p, target, targprop, obj, prop, FALSE, 0, obj);
1627 break;
1628
1629 case OPCEXPINH:
1630 /* inheritance from explicit superclass */
1631 nargc = *p++;
1632 runcheckargc(ctx, &nargc);
1633 prop = osrp2(p);
1634 obj = osrp2(p + 2);
1635 p += 4;
1636
1637 /*
1638 * Evaluate the property of the given object, but keeping
1639 * the same 'self' as is currently in effect. Note that the
1640 * 'inherit' flag is FALSE in this call, even though we're
1641 * inheriting, because the opcode explicitly specifies the
1642 * object we want to inherit from.
1643 */
1644 runpprop(ctx, &p, target, targprop, obj, prop, FALSE,
1645 nargc, self);
1646 break;
1647
1648 case OPCEXPINHPTR:
1649 nargc = *p++;
1650 runcheckargc(ctx, &nargc);
1651 prop = runpopprp(ctx);
1652 obj = osrp2(p);
1653 p += 2;
1654 runpprop(ctx, &p, target, targprop, obj, prop, FALSE,
1655 nargc, self);
1656 break;
1657
1658 case OPCPASS:
1659 prop = osrp2(p);
1660 runleave(ctx, 0);
1661 dbgleave(ctx->runcxdbg, DBGEXPASS);
1662 runpprop(ctx, &p, target, targprop, target, prop, TRUE, argc,
1663 self);
1664 goto done;
1665
1666 case OPCEXIT:
1667 errsig(ctx->runcxerr, ERR_RUNEXIT);
1668 /* NOTREACHED */
1669
1670 case OPCABORT:
1671 errsig(ctx->runcxerr, ERR_RUNABRT);
1672 /* NOTREACHED */
1673
1674 case OPCASKDO:
1675 errsig(ctx->runcxerr, ERR_RUNASKD);
1676 /* NOTREACHED */
1677
1678 case OPCASKIO:
1679 errsig1(ctx->runcxerr, ERR_RUNASKI, ERRTINT, osrp2(p));
1680 /* NOTREACHED */
1681
1682 case OPCJE:
1683 p += (runeq(ctx) ? runrp2s(p) : 2);
1684 break;
1685
1686 case OPCJNE:
1687 p += (!runeq(ctx) ? runrp2s(p) : 2);
1688 break;
1689
1690 case OPCJGT:
1691 p += (runmcmp(ctx) > 0 ? runrp2s(p) : 2);
1692 break;
1693
1694 case OPCJGE:
1695 p += (runmcmp(ctx) >= 0 ? runrp2s(p) : 2);
1696 break;
1697
1698 case OPCJLT:
1699 p += (runmcmp(ctx) < 0 ? runrp2s(p) : 2);
1700 break;
1701
1702 case OPCJLE:
1703 p += (runmcmp(ctx) <= 0 ? runrp2s(p) : 2);
1704 break;
1705
1706 case OPCJNAND:
1707 p += (!(runpoplog(ctx) && runpoplog(ctx)) ? runrp2s(p) : 2);
1708 break;
1709
1710 case OPCJNOR:
1711 p += (!(runpoplog(ctx) || runpoplog(ctx)) ? runrp2s(p) : 2);
1712 break;
1713
1714 case OPCGETPSELF:
1715 nargc = *p++;
1716 runcheckargc(ctx, &nargc);
1717 prop = osrp2(p);
1718 p += 2;
1719 runpprop(ctx, &p, target, targprop, self, prop, FALSE, nargc,
1720 self);
1721 break;
1722
1723 case OPCGETPSELFDATA:
1724 prop = osrp2(p);
1725 p += 2;
1726 runcheckpropdata(ctx, self, prop);
1727 runpprop(ctx, &p, target, targprop, self, prop, FALSE, 0, self);
1728 break;
1729
1730 case OPCGETPPTRSELF:
1731 nargc = *p++;
1732 runcheckargc(ctx, &nargc);
1733 prop = runpopprp(ctx);
1734 runpprop(ctx, &p, target, targprop, self, prop, FALSE, nargc,
1735 self);
1736 break;
1737
1738 case OPCGETPOBJ:
1739 nargc = *p++;
1740 runcheckargc(ctx, &nargc);
1741 obj = osrp2(p);
1742 prop = osrp2(p+2);
1743 p += 4;
1744 runpprop(ctx, &p, target, targprop, obj, prop, FALSE, nargc,
1745 obj);
1746 break;
1747
1748 case OPCINDEX:
1749 i = runpopnum(ctx); /* get index */
1750 lstp = runpoplst(ctx); /* get the list */
1751 runpind(ctx, i, lstp);
1752 break;
1753
1754 case OPCJST:
1755 if (runtostyp(ctx) == DAT_TRUE)
1756 p += runrp2s(p);
1757 else
1758 {
1759 (void)runpoplog(ctx);
1760 p += 2;
1761 }
1762 break;
1763
1764 case OPCJSF:
1765 if (runtostyp(ctx) == DAT_NIL ||
1766 (runtostyp(ctx) == DAT_NUMBER &&
1767 (ctx->runcxsp - 1)->runsv.runsvnum == 0))
1768 p += runrp2s(p);
1769 else
1770 {
1771 runpop(ctx, &val);
1772 p += 2;
1773 }
1774 break;
1775
1776 case OPCCALLEXT:
1777 {
1778 #if 0 // external functions are now obsolete
1779 static runufdef uf =
1780 {
1781 runuftyp, runufnpo, runufspo, runufdsc,
1782 runufnpu, runufspu, runufcspu, runufsal,
1783 runuflpu
1784 };
1785 int fn;
1786 runxdef *ex;
1787
1788 runuxdef ux;
1789
1790 /* set up callback context */
1791 ux.runuxctx = ctx;
1792 ux.runuxvec = &uf;
1793 ux.runuxargc = *p++;
1794
1795 fn = osrp2(p);
1796 p += 2;
1797 ex = &ctx->runcxext[fn];
1798
1799 if (!ex->runxptr)
1800 {
1801 if ((ex->runxptr = os_exfil(ex->runxnam)) == 0)
1802 runsig1(ctx, ERR_EXTLOAD, ERRTSTR, ex->runxnam);
1803 }
1804 if (os_excall(ex->runxptr, &ux))
1805 runsig1(ctx, ERR_EXTRUN, ERRTSTR, ex->runxnam);
1806 #else
1807 /* external functions are obsolete - throw an error */
1808 runxdef *ex;
1809 p += 1;
1810 ex = &ctx->runcxext[osrp2(p)];
1811 p += 2;
1812 runsig1(ctx, ERR_EXTRUN, ERRTSTR, ex->runxnam);
1813 #endif
1814 }
1815 break;
1816
1817 case OPCDBGRET:
1818 goto done;
1819
1820 case OPCCONS:
1821 {
1822 uint totsiz;
1823 uint oldsiz;
1824 uint tot;
1825 uint cursiz;
1826 runsdef lstend;
1827
1828 tot = i = osrp2(p); /* get # of items to build into list */
1829 p += 2;
1830
1831 /* reserve space for initial list (w/length word only) */
1832 runhres(ctx, 2, 0);
1833
1834 /*
1835 * Set up value to point to output list, making room
1836 * for length prefix. Remember size-so-far separately.
1837 */
1838 lstend.runstyp = DAT_LIST;
1839 lstend.runsv.runsvstr = ctx->runcxhp;
1840 ctx->runcxhp += 2;
1841 totsiz = 2;
1842
1843 while (i--)
1844 {
1845 runpop(ctx, &val); /* get next value off stack */
1846 cursiz = runsiz(&val);
1847
1848 /*
1849 * Set up to allocate space. Before doing so, make
1850 * sure the list under construction is valid, to
1851 * ensure that it stays around after garbage
1852 * collection.
1853 */
1854 oldsiz = totsiz;
1855 totsiz += cursiz + 1;
1856 oswp2(lstend.runsv.runsvstr, oldsiz);
1857 ctx->runcxhp = lstend.runsv.runsvstr + oldsiz;
1858 runhres2(ctx, cursiz + 1, tot - i, &val, &lstend);
1859
1860 /* write this item to the list */
1861 runputbuf(lstend.runsv.runsvstr + oldsiz, &val);
1862 }
1863 oswp2(lstend.runsv.runsvstr, totsiz);
1864 ctx->runcxhp = lstend.runsv.runsvstr + totsiz;
1865 runrepush(ctx, &lstend);
1866 }
1867 break;
1868
1869 case OPCARGC:
1870 val.runsv.runsvnum = argc;
1871 runpush(ctx, DAT_NUMBER, &val);
1872 break;
1873
1874 case OPCCHKARGC:
1875 if ((*p & 0x80) ? argc < (*p & 0x7f) : argc != *p)
1876 {
1877 char namebuf[128];
1878 size_t namelen;
1879
1880 /*
1881 * debugger is present - look up the name of the current
1882 * function or method, so that we can report it in the
1883 * error message
1884 */
1885 if (targprop == 0)
1886 {
1887 /* we're in a function */
1888 namelen = dbgnam(ctx->runcxdbg, namebuf, TOKSTFUNC,
1889 target);
1890 }
1891 else
1892 {
1893 /* we're in an object.method */
1894 namelen = dbgnam(ctx->runcxdbg, namebuf, TOKSTOBJ,
1895 target);
1896 namebuf[namelen++] = '.';
1897 namelen += dbgnam(ctx->runcxdbg, namebuf + namelen,
1898 TOKSTPROP, targprop);
1899 }
1900 namebuf[namelen] = '\0';
1901 runsig1(ctx, ERR_ARGC, ERRTSTR, namebuf);
1902 }
1903 ++p;
1904 break;
1905
1906 case OPCLINE:
1907 case OPCBP:
1908 {
1909 uchar *ptr = mcmobjptr(ctx->runcxmem, (mcmon)target);
1910 uint ofs;
1911 uchar instr;
1912
1913 /* set up the debugger frame record for this line */
1914 dbgframe(ctx->runcxdbg, osrp2(p+1), p - ptr);
1915
1916 /* remember the instruction */
1917 instr = *(p-1);
1918
1919 /* remember the offset of the line record */
1920 ctx->runcxlofs = ofs = (p + 2 - ptr);
1921
1922 /* skip to the next instruction */
1923 p += *p;
1924
1925 /* let the debugger take over, if it wants to */
1926 dbgssi(ctx->runcxdbg, ofs, instr, 0, &p);
1927 break;
1928 }
1929
1930 case OPCFRAME:
1931 /* this is a frame record - just jump past it */
1932 p += osrp2(p);
1933 break;
1934
1935 case OPCASI_MASK | OPCASIDIR | OPCASILCL:
1936 runpop(ctx, &val);
1937 OSCPYSTRUCT(*(ctx->runcxbp + runrp2s(p) - 1), val);
1938 stkval = &val;
1939 p += 2;
1940 goto no_assign;
1941
1942 case OPCASI_MASK | OPCASIDIR | OPCASIPRP:
1943 obj = runpopobj(ctx);
1944 prop = osrp2(p);
1945 p += 2;
1946 runpop(ctx, &val);
1947 stkval = valp = &val;
1948 goto assign_property;
1949
1950 case OPCASI_MASK | OPCASIDIR | OPCASIPRPPTR:
1951 prop = runpopprp(ctx);
1952 obj = runpopobj(ctx);
1953 runpop(ctx, &val);
1954 stkval = valp = &val;
1955 goto assign_property;
1956
1957 case OPCNEW:
1958 run_new(ctx, &p, target, targprop);
1959 break;
1960
1961 case OPCDELETE:
1962 run_delete(ctx, &p, target, targprop);
1963 break;
1964
1965 default:
1966 if ((opc & OPCASI_MASK) == OPCASI_MASK)
1967 {
1968 runsdef val3;
1969 int asityp;
1970 int asiext;
1971 int lclnum;
1972
1973 valp = &val;
1974 stkval = &val;
1975
1976 asityp = (opc & OPCASITYP_MASK);
1977 if (asityp == OPCASIEXT)
1978 asiext = *p++;
1979
1980 /* get list element/property number if needed */
1981 switch(opc & OPCASIDEST_MASK)
1982 {
1983 case OPCASIPRP:
1984 obj = runpopobj(ctx);
1985 prop = osrp2(p);
1986 p += 2;
1987 break;
1988
1989 case OPCASIPRPPTR:
1990 prop = runpopprp(ctx);
1991 obj = runpopobj(ctx);
1992 break;
1993
1994 case OPCASIIND:
1995 i = runpopnum(ctx);
1996 lstp = runpoplst(ctx);
1997 break;
1998
1999 case OPCASILCL:
2000 lclnum = runrp2s(p);
2001 p += 2;
2002 break;
2003 }
2004
2005 if (asityp != OPCASIDIR)
2006 {
2007 /* we have an <op>= operator - get lval, modify, & set */
2008 switch(opc & OPCASIDEST_MASK)
2009 {
2010 case OPCASILCL:
2011 OSCPYSTRUCT(val, *(ctx->runcxbp + lclnum - 1));
2012 break;
2013
2014 case OPCASIPRP:
2015 case OPCASIPRPPTR:
2016 runpprop(ctx, &p, target, targprop, obj, prop,
2017 FALSE, 0, obj);
2018 runpop(ctx, &val);
2019 break;
2020
2021 case OPCASIIND:
2022 runpind(ctx, i, lstp);
2023 runpop(ctx, &val);
2024 break;
2025 }
2026
2027 /* if saving pre-inc/dec value, get the value now */
2028 if ((opc & OPCASIPRE_MASK) == OPCASIPOST)
2029 {
2030 OSCPYSTRUCT(val3, val);
2031 stkval = &val3;
2032 }
2033 }
2034
2035 /* get rvalue, except for inc/dec operations */
2036 if (asityp != OPCASIINC && asityp != OPCASIDEC)
2037 runpop(ctx, &val2);
2038
2039 /* now apply operation to lvalue using rvalue */
2040 switch(asityp)
2041 {
2042 case OPCASIADD:
2043 if ((opc & OPCASIIND) != 0)
2044 {
2045 runsdef val4;
2046
2047 /*
2048 * we're adding to an indexed value out of a list -
2049 * we need to make sure the list is protected from
2050 * garbage collection, so push it back on the stack
2051 * while we're working
2052 */
2053 val4.runstyp = DAT_LIST;
2054 val4.runsv.runsvstr = lstp;
2055 runrepush(ctx, &val4);
2056
2057 /* carry out the addition */
2058 runadd(ctx, &val, &val2, 2);
2059
2060 /*
2061 * in case the list got moved during garbage
2062 * collection, retrieve it from the stack
2063 */
2064 lstp = runpoplst(ctx);
2065 }
2066 else
2067 {
2068 /* no list indexing - just carry out the addition */
2069 runadd(ctx, &val, &val2, 2);
2070 }
2071 break;
2072
2073 case OPCASISUB:
2074 if ((opc & OPCASIIND) != 0)
2075 {
2076 runsdef val4;
2077 int result;
2078
2079 /* as with adding, protect the list from GC */
2080 val4.runstyp = DAT_LIST;
2081 val4.runsv.runsvstr = lstp;
2082 runrepush(ctx, &val4);
2083
2084 /* carry out the subtraction and note the result */
2085 result = runsub(ctx, &val, &val2, 2);
2086
2087 /* recover the list pointer */
2088 lstp = runpoplst(ctx);
2089
2090 /* check to see if we have an assignment */
2091 if (!result)
2092 goto no_assign;
2093 }
2094 else
2095 {
2096 /* no list indexing - just do the subtraction */
2097 if (!runsub(ctx, &val, &val2, 2))
2098 goto no_assign;
2099 }
2100 break;
2101
2102 case OPCASIMUL:
2103 if (val.runstyp != DAT_NUMBER
2104 || val2.runstyp != DAT_NUMBER)
2105 runsig(ctx, ERR_REQNUM);
2106 val.runsv.runsvnum *= val2.runsv.runsvnum;
2107 break;
2108
2109 case OPCASIDIV:
2110 if (val.runstyp != DAT_NUMBER
2111 || val2.runstyp != DAT_NUMBER)
2112 runsig(ctx, ERR_REQNUM);
2113 if (val2.runsv.runsvnum == 0)
2114 runsig(ctx, ERR_DIVZERO);
2115 val.runsv.runsvnum /= val2.runsv.runsvnum;
2116 break;
2117
2118 case OPCASIINC:
2119 if (val.runstyp != DAT_NUMBER)
2120 runsig(ctx, ERR_REQNUM);
2121 ++(val.runsv.runsvnum);
2122 break;
2123
2124 case OPCASIDEC:
2125 if (val.runstyp != DAT_NUMBER)
2126 runsig(ctx, ERR_REQNUM);
2127 --(val.runsv.runsvnum);
2128 break;
2129
2130 case OPCASIDIR:
2131 valp = stkval = &val2;
2132 break;
2133
2134 case OPCASIEXT:
2135 switch (asiext)
2136 {
2137 case OPCASIMOD:
2138 if (val.runstyp != DAT_NUMBER
2139 || val2.runstyp != DAT_NUMBER)
2140 runsig(ctx, ERR_REQNUM);
2141 if (val2.runsv.runsvnum == 0)
2142 runsig(ctx, ERR_DIVZERO);
2143 val.runsv.runsvnum %= val2.runsv.runsvnum;
2144 break;
2145
2146 case OPCASIBAND:
2147 if ((val.runstyp == DAT_TRUE
2148 || val.runstyp == DAT_NIL)
2149 && (val2.runstyp == DAT_TRUE
2150 || val2.runstyp == DAT_NIL))
2151 {
2152 int a, b;
2153
2154 a = (val.runstyp == DAT_TRUE ? 1 : 0);
2155 b = (val2.runstyp == DAT_TRUE ? 1 : 0);
2156 val.runstyp = runclog(a && b);
2157 }
2158 else if (val.runstyp == DAT_NUMBER
2159 && val2.runstyp == DAT_NUMBER)
2160 val.runsv.runsvnum &= val2.runsv.runsvnum;
2161 else
2162 runsig(ctx, ERR_REQNUM);
2163 break;
2164
2165 case OPCASIBOR:
2166 if ((val.runstyp == DAT_TRUE
2167 || val.runstyp == DAT_NIL)
2168 && (val2.runstyp == DAT_TRUE
2169 || val2.runstyp == DAT_NIL))
2170 {
2171 int a, b;
2172
2173 a = (val.runstyp == DAT_TRUE ? 1 : 0);
2174 b = (val2.runstyp == DAT_TRUE ? 1 : 0);
2175 val.runstyp = runclog(a || b);
2176 }
2177 else if (val.runstyp == DAT_NUMBER
2178 && val2.runstyp == DAT_NUMBER)
2179 val.runsv.runsvnum |= val2.runsv.runsvnum;
2180 else
2181 runsig(ctx, ERR_REQNUM);
2182 break;
2183
2184 case OPCASIXOR:
2185 if ((val.runstyp == DAT_TRUE || val.runstyp == DAT_NIL)
2186 && (val2.runstyp == DAT_TRUE
2187 || val2.runstyp == DAT_NIL))
2188 {
2189 int a, b;
2190
2191 a = (val.runstyp == DAT_TRUE ? 1 : 0);
2192 b = (val2.runstyp == DAT_TRUE ? 1 : 0);
2193 val.runstyp = runclog(a ^ b);
2194 }
2195 else if (val.runstyp == DAT_NUMBER
2196 && val2.runstyp == DAT_NUMBER)
2197 val.runsv.runsvnum ^= val2.runsv.runsvnum;
2198 else
2199 runsig(ctx, ERR_REQNUM);
2200 break;
2201
2202 case OPCASISHL:
2203 if (val.runstyp != DAT_NUMBER
2204 || val2.runstyp != DAT_NUMBER)
2205 runsig(ctx, ERR_REQNUM);
2206 val.runsv.runsvnum <<= val2.runsv.runsvnum;
2207 break;
2208
2209 case OPCASISHR:
2210 if (val.runstyp != DAT_NUMBER
2211 || val2.runstyp != DAT_NUMBER)
2212 runsig(ctx, ERR_REQNUM);
2213 val.runsv.runsvnum >>= val2.runsv.runsvnum;
2214 break;
2215
2216 default:
2217 runsig(ctx, ERR_INVOPC);
2218 }
2219 break;
2220
2221 default:
2222 runsig(ctx, ERR_INVOPC);
2223 }
2224
2225 /* write the rvalue at *valp to the lvalue */
2226 switch(opc & OPCASIDEST_MASK)
2227 {
2228 case OPCASILCL:
2229 OSCPYSTRUCT(*(ctx->runcxbp + lclnum - 1), *valp);
2230 break;
2231
2232 case OPCASIPRP:
2233 case OPCASIPRPPTR:
2234 assign_property:
2235 {
2236 void *valbuf;
2237 uchar outbuf[4];
2238
2239 switch(valp->runstyp)
2240 {
2241 case DAT_LIST:
2242 case DAT_SSTRING:
2243 valbuf = valp->runsv.runsvstr;
2244 break;
2245
2246 case DAT_NUMBER:
2247 valbuf = outbuf;
2248 oswp4s(outbuf, valp->runsv.runsvnum);
2249 break;
2250
2251 case DAT_OBJECT:
2252 case DAT_FNADDR:
2253 valbuf = outbuf;
2254 oswp2(outbuf, valp->runsv.runsvobj);
2255 break;
2256
2257 case DAT_PROPNUM:
2258 valbuf = outbuf;
2259 oswp2(outbuf, valp->runsv.runsvprp);
2260 break;
2261
2262 default:
2263 valbuf = &valp->runsv;
2264 break;
2265 }
2266
2267 ofs = runcpsav(ctx, &p, target, targprop);
2268 objsetp(ctx->runcxmem, obj, prop, valp->runstyp,
2269 valbuf, ctx->runcxundo);
2270 p = runcprst(ctx, ofs, target, targprop);
2271 break;
2272 }
2273
2274 case OPCASIIND:
2275 {
2276 uint newtot;
2277 uint newsiz;
2278 uint remsiz;
2279 uint delsiz;
2280 uchar *delp;
2281 uchar *remp;
2282
2283 /* compute sizes and pointers to various parts */
2284 ofs = runindofs(ctx, i, lstp);
2285 delp = lstp + ofs; /* ptr to item to replace */
2286 delsiz = datsiz(*delp, delp + 1); /* size of *delp */
2287 remsiz = osrp2(lstp) - ofs - delsiz - 1;
2288 newsiz = runsiz(valp); /* size of new item */
2289 newtot = osrp2(lstp) + newsiz - delsiz; /* new tot */
2290
2291 /* reserve space for the new list & copy first part */
2292 {
2293 runsdef val3;
2294
2295 /* make sure lstp stays valid before and after */
2296 val3.runstyp = DAT_LIST;
2297 val3.runsv.runsvstr = lstp;
2298 runhres3(ctx, newtot, 3, &val, &val2, &val3);
2299
2300 /* update all of the pointers within lstp */
2301 lstp = val3.runsv.runsvstr;
2302 delp = lstp + ofs;
2303 remp = lstp + ofs + delsiz + 1;
2304 }
2305 memcpy(ctx->runcxhp + 2, lstp + 2, (size_t)(ofs - 2));
2306
2307 /* set size of new list */
2308 oswp2(ctx->runcxhp, newtot);
2309
2310 /* copy new item into buffer */
2311 runputbuf(ctx->runcxhp + ofs, valp);
2312
2313 /* copy remainder and update heap pointer */
2314 memcpy(ctx->runcxhp + ofs + newsiz + 1, remp,
2315 (size_t)remsiz);
2316 val.runstyp = DAT_LIST;
2317 val.runsv.runsvstr = ctx->runcxhp;
2318 stkval = &val;
2319 ctx->runcxhp += newtot;
2320 break;
2321 }
2322 }
2323
2324 no_assign: /* skip assignment - operation didn't change value */
2325 if (*p == OPCDISCARD)
2326 {
2327 /* next assignment is DISCARD - deal with it now */
2328 ++p;
2329 ctx->runcxsp = rstsp;
2330 }
2331 else
2332 runrepush(ctx, stkval);
2333 }
2334 else
2335 errsig(ctx->runcxerr, ERR_INVOPC);
2336 }
2337 }
2338
2339 /*
2340 * come here to return - don't use 'return' directly, since that
2341 * would not properly exit the error frame
2342 */
2343 done: ;
2344
2345 #ifndef DBG_OFF
2346 /*
2347 * Come here to catch any errors that occur during execution of this
2348 * p-code
2349 */
2350 ERRCATCH(ctx->runcxerr, err)
2351 {
2352 /*
2353 * if the debugger isn't present, or we're already in the
2354 * debugger, or if the debugger can't resume from errors, or if
2355 * we're not in user code (in which case the debugger can't
2356 * resume from this error even if it normally could resume from
2357 * an error), simply re-signal the error
2358 */
2359 if (!dbgpresent()
2360 || ctx->runcxdbg->dbgcxfcn == 0
2361 || !dbgu_err_resume(ctx->runcxdbg)
2362 || (ctx->runcxdbg->dbgcxflg & DBGCXFIND) != 0)
2363 errrse(ctx->runcxerr);
2364
2365 /* check the error code */
2366 switch(err)
2367 {
2368 case ERR_RUNEXIT:
2369 case ERR_RUNABRT:
2370 case ERR_RUNASKD:
2371 case ERR_RUNASKI:
2372 case ERR_RUNQUIT:
2373 case ERR_RUNRESTART:
2374 case ERR_RUNEXITOBJ:
2375 /* don't trap these errors - resignal it immediately */
2376 errrse(ctx->runcxerr);
2377
2378 default:
2379 /* trap other errors to the debugger */
2380 break;
2381 }
2382
2383 /* if the object was unlocked, re-lock it */
2384 if (p == 0)
2385 mcmlck(ctx->runcxmem, target);
2386
2387 /* set up after the last OPCLINE instruction */
2388 p = mcmobjptr(ctx->runcxmem, (mcmon)target) + ctx->runcxlofs - 2;
2389 p += *p;
2390
2391 /*
2392 * Keep the current error's arguments around for handling
2393 * outside of this handler, since we'll need them in dbgssi.
2394 */
2395 errkeepargs(ctx->runcxerr);
2396
2397 /* enter the debugger with the error code */
2398 dbgssi(ctx->runcxdbg, ctx->runcxlofs, OPCLINE, err, &p);
2399
2400 /* check the error again */
2401 switch (err)
2402 {
2403 case ERR_ARGC:
2404 /* we can't continue from this - simply return */
2405 break;
2406
2407 default:
2408 /* resume execution */
2409 goto resume_from_error;
2410 }
2411 }
2412 ERREND(ctx->runcxerr);
2413 #endif /* DBG_OFF */
2414 }
2415
2416 /*
2417 * Signal a run-time error. This function first calls the debugger
2418 * single-step function to allow the debugger to trap the error, then
2419 * signals the error as usual when the debugger returns.
2420 */
2421 void runsign(runcxdef *ctx, int err)
2422 {
2423 /*
2424 * If the debugger isn't capable of resuming from a run-time error,
2425 * trap to the debugger now so that the user can see what happened.
2426 * Do not trap to the debugger here if the debugger can resume from
2427 * an error; instead, we'll trap in the p-code loop, since we'll be
2428 * able to resume execution from the point of the error.
2429 *
2430 * Note that we can't resume from an error when there's no stack
2431 * frame, so we'll trap to the debugger here in that case.
2432 */
2433 if (ctx->runcxdbg->dbgcxfcn == 0
2434 || !dbgu_err_resume(ctx->runcxdbg))
2435 dbgssi(ctx->runcxdbg, ctx->runcxlofs, OPCLINE, err, 0);
2436
2437 /* signal the error */
2438 errsign(ctx->runcxerr, err, "TADS");
2439 }
2440