Debian Codegraph @

terps/tads/tads3/vmtype.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/VMTYPE.CPP,v 1.3 1999/05/17 02:52:29 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1998, 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 vmtype.cpp - VM types
15 Function
16
17 Notes
18
19 Modified
20 11/18/98 MJRoberts - Creation
21 */
22
23 #include "t3std.h"
24 #include "vmtype.h"
25 #include "vmobj.h"
26 #include "vmstr.h"
27 #include "vmlst.h"
28 #include "vmpool.h"
29
30 /* ------------------------------------------------------------------------ */
31 /*
32 * Compare this value to another value to determine if the two values
33 * are equal.
34 */
35 int vm_val_t::equals(VMG_ const vm_val_t *v, int depth) const
36 {
37 /*
38 * if the second value is an object and the first isn't, use the object
39 * comparison of the second
40 */
41 if (v->typ == VM_OBJ && typ != VM_OBJ)
42 return vm_objp(vmg_ v->val.obj)
43 ->equals(vmg_ v->val.obj, this, depth);
44
45 /* figure out what to do based on my type */
46 switch(typ)
47 {
48 case VM_NIL:
49 case VM_TRUE:
50 /* we match only if the other value is the same boolean value */
51 return (v->typ == typ);
52
53 case VM_STACK:
54 case VM_CODEPTR:
55 /*
56 * we match only if the other value has the same type and its
57 * pointer value matches
58 */
59 return (v->typ == typ && v->val.ptr == this->val.ptr);
60
61 case VM_OBJ:
62 /* use the object's polymorphic equality test routine */
63 return vm_objp(vmg_ this->val.obj)
64 ->equals(vmg_ this->val.obj, v, depth);
65
66 case VM_PROP:
67 /* we match if the other value is the same property ID */
68 return (v->typ == VM_PROP && v->val.prop == this->val.prop);
69
70 case VM_INT:
71 /* we match if the other value is the same integer */
72 return (v->typ == VM_INT && v->val.intval == this->val.intval);
73
74 case VM_BIFPTR:
75 case VM_BIFPTRX:
76 /* we match if the other value refers to the same function */
77 return (v->typ == VM_BIFPTR
78 && v->val.bifptr.set_idx == this->val.bifptr.set_idx
79 && v->val.bifptr.func_idx == this->val.bifptr.func_idx);
80
81 case VM_ENUM:
82 return (v->typ == VM_ENUM && v->val.enumval == this->val.enumval);
83
84 case VM_SSTRING:
85 /* use the standard string comparison routine */
86 return CVmObjString::
87 const_equals(vmg_ G_const_pool->get_ptr(this->val.ofs), v);
88
89 case VM_LIST:
90 /* use the standard list comparison routine */
91 return CVmObjList::
92 const_equals(vmg_ this,
93 G_const_pool->get_ptr(this->val.ofs), v, depth);
94
95 case VM_OBJX:
96 /* match if it's the same object */
97 return (v->typ == typ && v->val.obj == this->val.obj);
98
99 case VM_CODEOFS:
100 case VM_FUNCPTR:
101 /* we match if the other value is the same code offset */
102 return (v->typ == typ && v->val.ofs == this->val.ofs);
103
104 case VM_EMPTY:
105 /* empty never matches anything */
106 return FALSE;
107
108 case VM_DSTRING:
109 /* dstrings have no value, and are thus never equal to anything */
110 return FALSE;
111
112 default:
113 /* other types are not recognized */
114 return FALSE;
115 }
116 }
117
118 /* ------------------------------------------------------------------------ */
119 /*
120 * Calculate a hash value
121 */
122 uint vm_val_t::calc_hash(VMG_ int depth) const
123 {
124 /* see what we have */
125 switch(typ)
126 {
127 case VM_NIL:
128 /* this is rather arbitrary */
129 return 0;
130
131 case VM_TRUE:
132 /* this is arbitrary, but at least make it different from nil */
133 return 1;
134
135 case VM_EMPTY:
136 /* also arbitrary */
137 return 2;
138
139 case VM_CODEOFS:
140 case VM_FUNCPTR:
141 /* use a 16-bit hash of the code address */
142 return (uint)((val.ofs & 0xffff)
143 ^ ((val.ofs & 0xffff0000) >> 16));
144
145 case VM_OBJX:
146 /* use a 16-bit hash of the object ID */
147 return (uint)((val.obj & 0xffff)
148 ^ ((val.obj & 0xffff0000) >> 16));
149
150 case VM_PROP:
151 /* use the property ID as the hash */
152 return (uint)val.prop;
153
154 case VM_INT:
155 case VM_BIFPTR:
156 case VM_BIFPTRX:
157 /*
158 * the set index and function index both tend to be small integers;
159 * multiply them and keep the low-order 16 bits
160 */
161 return (uint)(val.bifptr.set_idx * val.bifptr.func_idx) & 0xffff;
162
163 case VM_ENUM:
164 /* use a 16-bit hash of the enum value */
165 return (uint)((val.enumval & 0xffff)
166 ^ ((val.enumval & 0xffff0000) >> 16));
167
168 case VM_OBJ:
169 /* ask the object to calculate its hash value */
170 return vm_objp(vmg_ val.obj)->calc_hash(vmg_ val.obj, depth);
171 break;
172
173 case VM_SSTRING:
174 /* get the hash of the constant string */
175 return CVmObjString::
176 const_calc_hash(G_const_pool->get_ptr(val.ofs));
177 break;
178
179 case VM_LIST:
180 /* get the hash of the constant list */
181 return CVmObjList::
182 const_calc_hash(vmg_ this, G_const_pool->get_ptr(val.ofs), depth);
183
184 default:
185 /* return an arbitrary value for any other type */
186 return 3;
187 }
188 }
189
190 /* ------------------------------------------------------------------------ */
191 /*
192 * Compare this value to the given value. Returns a positive value if
193 * this value is greater than 'val', a negative value if this value is
194 * less than 'val', and 0 if the two values are equal. Throws an error
195 * if a magnitude comparison is not meaningful for the involved types.
196 */
197 int vm_val_t::gen_compare_to(VMG_ const vm_val_t *v) const
198 {
199 /* the comparison depends on my type */
200 switch(typ)
201 {
202 case VM_OBJ:
203 /* let the object perform the comparison */
204 return vm_objp(vmg_ this->val.obj)
205 ->compare_to(vmg_ this->val.obj, v);
206
207 case VM_SSTRING:
208 /* compare the string */
209 return CVmObjString::
210 const_compare(vmg_ G_const_pool->get_ptr(this->val.ofs), v);
211
212 case VM_INT:
213 if (v->typ == VM_INT)
214 {
215 /* comparing two integers */
216 int32_t a = this->val.intval, b = v->val.intval;
217 return a - b;
218 }
219 else if (v->typ == VM_OBJ)
220 {
221 /* the other value is an object, so let it do the comparison */
222 return -(vm_objp(vmg_ v->val.obj)->
223 compare_to(vmg_ v->val.obj, this));
224 }
225 else
226 {
227 /* other types can't be compared to integer */
228 err_throw(VMERR_INVALID_COMPARISON);
229 AFTER_ERR_THROW(return 0;)
230 }
231
232 default:
233 /* if the other value is an object, let it do the comparison */
234 if (v->typ == VM_OBJ)
235 {
236 return -(vm_objp(vmg_ v->val.obj)->
237 compare_to(vmg_ v->val.obj, this));
238 }
239 else
240 {
241 /* other types cannot be compared for magnitude */
242 err_throw(VMERR_INVALID_COMPARISON);
243 AFTER_ERR_THROW(return 0;)
244 }
245 }
246 }
247
248 /* ------------------------------------------------------------------------ */
249 /*
250 * Is this a numeric value? Returns true for integers and BigNumber
251 * values.
252 */
253 int vm_val_t::nonint_is_numeric(VMG0_) const
254 {
255 /* some objects represent numeric types (e.g., BigNumber) */
256 if (typ == VM_OBJ)
257 return vm_objp(vmg_ val.obj)->is_numeric();
258
259 /* we shouldn't be called with an int, but just in case */
260 if (typ == VM_INT)
261 return TRUE;
262
263 /* other types are non-numeric */
264 return FALSE;
265 }
266
267
268 /*
269 * Convert a numeric type to integer
270 */
271 int32_t vm_val_t::nonint_num_to_int(VMG0_) const
272 {
273 /* if it's an object, ask the object for its integer value */
274 long l;
275 if (typ == VM_OBJ && vm_objp(vmg_ val.obj)->get_as_int(&l))
276 return l;
277
278 /* we shouldn't be called with an int type, but just in case */
279 if (typ == VM_INT)
280 return val.intval;
281
282 /* other types are not convertible */
283 err_throw(VMERR_NUM_VAL_REQD);
284 AFTER_ERR_THROW(return 0);
285 }
286
287 /*
288 * Convert a numeric type to doule
289 */
290 double vm_val_t::nonint_num_to_double(VMG0_) const
291 {
292 /* if it's an object, ask the object for its integer value */
293 double d;
294 if (typ == VM_OBJ && vm_objp(vmg_ val.obj)->get_as_double(vmg_ &d))
295 return d;
296
297 /* we shouldn't be called with an int type, but just in case */
298 if (typ == VM_INT)
299 return (double)val.intval;
300
301 /* other types are not convertible */
302 err_throw(VMERR_NUM_VAL_REQD);
303 AFTER_ERR_THROW(return 0.);
304 }
305
306 /*
307 * Cast to integer
308 */
309 int32_t vm_val_t::nonint_cast_to_int(VMG0_) const
310 {
311 /* check the type */
312 switch (typ)
313 {
314 case VM_TRUE:
315 return 1;
316
317 case VM_NIL:
318 return 0;
319
320 case VM_INT:
321 /* we shouldn't ever be called with an int value, but just in case */
322 return val.intval;
323
324 case VM_SSTRING:
325 {
326 /* get the string constant */
327 const char *p = G_const_pool->get_ptr(val.ofs);
328
329 /* parse it as an integer */
330 vm_val_t i;
331 CVmObjString::parse_num_val(
332 vmg_ &i, p + VMB_LEN, vmb_get_len(p), 10, TRUE);
333
334 /* return the integer */
335 return i.val.intval;
336 }
337
338 case VM_OBJ:
339 /* ask the object to do the conversion */
340 return vm_objp(vmg_ val.obj)->cast_to_int(vmg0_);
341
342 default:
343 /* there's no integer conversion */
344 err_throw(VMERR_NO_INT_CONV);
345 AFTER_ERR_THROW(return 0;)
346 }
347 }
348
349 /*
350 * Cast to numeric (integer or BigNumber)
351 */
352 void vm_val_t::cast_to_num(VMG_ vm_val_t *retval) const
353 {
354 /* check the type */
355 switch (typ)
356 {
357 case VM_TRUE:
358 retval->set_int(1);
359 break;
360
361 case VM_NIL:
362 retval->set_int(0);
363 break;
364
365 case VM_INT:
366 /* it's already numeric */
367 retval->set_int(val.intval);
368 break;
369
370 case VM_SSTRING:
371 {
372 /* get the string constant */
373 const char *p = G_const_pool->get_ptr(val.ofs);
374
375 /* parse it as an integer */
376 CVmObjString::parse_num_val(
377 vmg_ retval, p + VMB_LEN, vmb_get_len(p), 10, FALSE);
378 }
379 break;
380
381 case VM_OBJ:
382 /* ask the object to do the conversion */
383 vm_objp(vmg_ val.obj)->cast_to_num(vmg_ retval, val.obj);
384 break;
385
386 default:
387 /* there's no numeric conversion */
388 err_throw(VMERR_NO_NUM_CONV);
389 }
390 }
391
392 /*
393 * Promote an integer to match my type
394 */
395 void vm_val_t::promote_int(VMG_ vm_val_t *val) const
396 {
397 if (typ == VM_OBJ)
398 {
399 /* ask the object to perform the promotion */
400 vm_objp(vmg_ this->val.obj)->promote_int(vmg_ val);
401 }
402 else
403 {
404 /*
405 * we can't promote integers to other types; this is a "numeric
406 * value required" error, since we only try to promote integers
407 * when performing arithmetic involving an int and another type
408 */
409 err_throw(VMERR_NUM_VAL_REQD);
410 }
411 }
412
413
414 /* ------------------------------------------------------------------------ */
415 /*
416 * Cast to string
417 */
418 const char *vm_val_t::cast_to_string(VMG_ vm_val_t *new_str) const
419 {
420 /* presume the value is just our own value */
421 *new_str = *this;
422
423 switch (typ)
424 {
425 case VM_SSTRING:
426 /* return the string as is */
427 return get_as_string(vmg0_);
428
429 case VM_OBJ:
430 /* cast the object to string */
431 return vm_objp(vmg_ val.obj)->cast_to_string(vmg_ val.obj, new_str);
432
433 case VM_INT:
434 /* create a printable decimal representation of the string */
435 {
436 /* format the number into a temporary buffer */
437 char buf[20];
438 sprintf(buf, "%ld", (long)val.intval);
439
440 /* create a new string object for the number */
441 new_str->set_obj(
442 CVmObjString::create(vmg_ FALSE, buf, strlen(buf)));
443
444 /* return the string buffer */
445 return new_str->get_as_string(vmg0_);
446 }
447
448 case VM_NIL:
449 /* return an empty string */
450 new_str->set_obj(CVmObjString::create(vmg_ FALSE, 0));
451 return new_str->get_as_string(vmg0_);
452
453 case VM_TRUE:
454 /* return 'true' */
455 new_str->set_obj(CVmObjString::create(vmg_ FALSE, "true", 4));
456 return new_str->get_as_string(vmg0_);
457
458 case VM_LIST:
459 /* join the list into a string, separating items with commas */
460 CVmObjList::join(vmg_ new_str, this, ",", 1);
461 return new_str->get_as_string(vmg0_);
462
463 default:
464 err_throw(VMERR_NO_STR_CONV);
465 AFTER_ERR_THROW(return 0;)
466 }
467 }
468
469 /*
470 * Get the underlying string constant value.
471 */
472 const char *vm_val_t::get_as_string(VMG0_) const
473 {
474 /* check my type */
475 if (typ == VM_SSTRING)
476 {
477 /* it's a constant string - return its text from the constant pool */
478 return G_const_pool->get_ptr(val.ofs);
479 }
480 else if (typ == VM_OBJ)
481 {
482 /* it's an object - ask for its underlying string, if any */
483 return vm_objp(vmg_ val.obj)->get_as_string(vmg0_);
484 }
485 else
486 {
487 /* other types do not have underlying strings */
488 return 0;
489 }
490 }
491
492 /* ------------------------------------------------------------------------ */
493 /*
494 * Get as a code pointer
495 */
496 const uchar *vm_val_t::get_as_codeptr(VMG0_) const
497 {
498 /* check my type */
499 switch (typ)
500 {
501 case VM_CODEOFS:
502 case VM_FUNCPTR:
503 /*
504 * these types contain code pool offsets - translate the offset to
505 * a physical pointer
506 */
507 return (const uchar *)G_code_pool->get_ptr(val.ofs);
508
509 case VM_CODEPTR:
510 /* it's already a physical code pointer */
511 return (const uchar *)val.ptr;
512
513 default:
514 /* other types do not contain code pointers */
515 return 0;
516 }
517 }
518
519 /* ------------------------------------------------------------------------ */
520 /*
521 * Get the underlying list constant value.
522 */
523 const char *vm_val_t::get_as_list(VMG0_) const
524 {
525 /* check my type */
526 if (typ == VM_LIST)
527 {
528 /* it's a constant list - return its data from the constant pool */
529 return G_const_pool->get_ptr(val.ofs);
530 }
531 else if (typ == VM_OBJ)
532 {
533 /* it's an object - ask for its underlying list, if any */
534 return vm_objp(vmg_ val.obj)->get_as_list();
535 }
536 else
537 {
538 /* other types do not have underlying lists */
539 return 0;
540 }
541 }
542
543 /* ------------------------------------------------------------------------ */
544 /*
545 * Is this indexable as a list?
546 */
547 int vm_val_t::is_listlike(VMG0_) const
548 {
549 return (get_as_list(vmg0_) != 0
550 || (typ == VM_OBJ
551 && (vm_objp(vmg_ val.obj)->get_as_list()
552 || vm_objp(vmg_ val.obj)->is_listlike(vmg_ val.obj))));
553 }
554
555 /*
556 * Get the number of elements in a list-like object
557 */
558 int vm_val_t::ll_length(VMG0_) const
559 {
560 /* try it as a regular list first */
561 const char *p;
562 if ((p = get_as_list(vmg0_)) != 0)
563 return vmb_get_len(p);
564
565 /* try it as an object */
566 if (typ == VM_OBJ)
567 return vm_objp(vmg_ val.obj)->ll_length(vmg_ val.obj);
568
569 /* we don't have a length */
570 return -1;
571 }
572
573 /*
574 * Get an indexed value
575 */
576 void vm_val_t::ll_index(VMG_ vm_val_t *ret, const vm_val_t *idx) const
577 {
578 /* try it as a regular list first, then as an object */
579 const char *p;
580 if ((p = get_as_list(vmg0_)) != 0)
581 {
582 /* get the index as an integer value */
583 int i = idx->cast_to_int(vmg0_);
584
585 /* check the range */
586 if (i < 1 || i > (int)vmb_get_len(p))
587 err_throw(VMERR_INDEX_OUT_OF_RANGE);
588
589 /* it's in range - retrieve the element */
590 vmb_get_dh(p + VMB_LEN + (size_t)((i - 1) * VMB_DATAHOLDER), ret);
591 }
592 else if (typ == VM_OBJ)
593 {
594 /* use the object method, allowing operator overloading */
595 vm_objp(vmg_ val.obj)->index_val_ov(vmg_ ret, val.obj, idx);
596 }
597 else if ((p = get_as_string(vmg0_)) != 0)
598 {
599 /* string - parse the string as an integer in decimal format */
600 CVmObjString::parse_num_val(
601 vmg_ ret, p + VMB_LEN, vmb_get_len(p), 10, TRUE);
602 }
603 else
604 {
605 /* no value */
606 ret->set_nil();
607 }
608 }
609
610
611 /* ------------------------------------------------------------------------ */
612 /*
613 * Is this an object of the given class?
614 */
615 int vm_val_t::is_instance_of(VMG_ vm_obj_id_t cls) const
616 {
617 return (typ == VM_OBJ
618 && cls != VM_INVALID_OBJ
619 && vm_objp(vmg_ val.obj)->is_instance_of(vmg_ cls));
620 }
621
622
623 /* ------------------------------------------------------------------------ */
624 /*
625 * Set to an integer giving the datatype code for the given value
626 */
627 void vm_val_t::set_datatype(VMG_ const vm_val_t *v)
628 {
629 /* check for an object */
630 if (v->typ == VM_OBJ)
631 {
632 /* check for List and String values */
633 if (v->get_as_string(vmg0_) != 0)
634 {
635 /* treat this the same as a string constant */
636 set_int((int)VM_SSTRING);
637 }
638 else if (v->get_as_list(vmg0_) != 0)
639 {
640 /* treat this the same as a list constant */
641 set_int((int)VM_LIST);
642 }
643 else
644 {
645 /* any other type of object is simply an object */
646 set_int((int)VM_OBJ);
647 }
648 }
649 else
650 {
651 /* for any other type, return our internal type code */
652 set_int((int)v->typ);
653 }
654 }
655
656 /* ------------------------------------------------------------------------ */
657 /*
658 * Is this a function pointer of some kind?
659 */
660 int vm_val_t::is_func_ptr(VMG0_) const
661 {
662 return (typ == VM_FUNCPTR
663 || typ == VM_BIFPTR
664 || (typ == VM_OBJ
665 && vm_objp(vmg_ val.obj)->get_invoker(vmg_ 0)));
666 }