Debian Codegraph @

terps/tads/tads3/vmbif.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/tads3/vmbif.cpp,v 1.3 1999/07/11 00:46:59 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 vmbif.cpp - built-in function set table implementation
15 Function
16
17 Notes
18
19 Modified
20 12/05/98 MJRoberts - Creation
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "t3std.h"
27 #include "utf8.h"
28 #include "charmap.h"
29 #include "vmtype.h"
30 #include "vmerr.h"
31 #include "vmerrnum.h"
32 #include "vmglob.h"
33 #include "vmbif.h"
34 #include "vmbifreg.h"
35 #include "vmstr.h"
36 #include "vmobj.h"
37 #include "vmrun.h"
38 #include "vmcset.h"
39 #include "vmfilnam.h"
40
41
42 /* ------------------------------------------------------------------------ */
43 /*
44 * Create the function set table with a given number of initial entries
45 */
46 CVmBifTable::CVmBifTable(size_t init_entries)
47 {
48 /* allocate space for our entries */
49 if (init_entries != 0)
50 {
51 /* allocate the space */
52 table_ = (vm_bif_entry_t **)
53 t3malloc(init_entries * sizeof(table_[0]));
54 names_ = (char **)
55 t3malloc(init_entries * sizeof(names_[0]));
56 }
57 else
58 {
59 /* we have no entries */
60 table_ = 0;
61 names_ = 0;
62 }
63
64 /* no entries are defined yet */
65 count_ = 0;
66
67 /* remember the allocation size */
68 alloc_ = init_entries;
69 }
70
71 /* ------------------------------------------------------------------------ */
72 /*
73 * Delete the table
74 */
75 CVmBifTable::~CVmBifTable()
76 {
77 /* free the table, if we ever allocated one */
78 if (table_ != 0)
79 t3free(table_);
80
81 /* free the function set names list, if we allocated it */
82 if (names_ != 0)
83 t3free(names_);
84 }
85
86 /* ------------------------------------------------------------------------ */
87 /*
88 * Clear all entries from the table
89 */
90 void CVmBifTable::clear(VMG0_)
91 {
92 size_t i;
93
94 /* detach each function set */
95 if (table_ != 0)
96 {
97 for (i = 0 ; i < count_ ; ++i)
98 {
99 if (table_[i] != 0)
100 table_[i]->unload_image(vmg_ i);
101 }
102 }
103
104 /* delete the 'names' entries, if we allocated any */
105 if (names_ != 0)
106 {
107 /* free each element */
108 for (i = 0 ; i < count_ ; ++i)
109 lib_free_str(names_[i]);
110 }
111
112 /*
113 * Reset the entry counter. Note that this doesn't affect any
114 * allocation; we keep a separate count of the number of table slots
115 * we have allocated. Table slots don't have any additional
116 * associated memory, so we don't need to worry about cleaning
117 * anything up at this point.
118 */
119 count_ = 0;
120 }
121
122 /* ------------------------------------------------------------------------ */
123 /*
124 * Ensure that we have space for a given number of entries
125 */
126 void CVmBifTable::ensure_space(size_t entries, size_t increment)
127 {
128 /* if we don't have enough space, allocate more */
129 if (entries >= alloc_)
130 {
131 size_t new_table_size;
132 size_t new_names_size;
133
134 /* increase the allocation size by the given increment */
135 alloc_ += increment;
136
137 /* if it's still too small, bump it up to the required size */
138 if (alloc_ < entries)
139 alloc_ = entries;
140
141 /* compute the new sizes */
142 new_table_size = alloc_ * sizeof(table_[0]);
143 new_names_size = alloc_ * sizeof(names_[0]);
144
145 /*
146 * if we have a table already, reallocate it at the larger size;
147 * otherwise, allocate a new table
148 */
149 if (table_ != 0)
150 {
151 table_ = (vm_bif_entry_t **)t3realloc(table_, new_table_size);
152 names_ = (char **)t3realloc(names_, new_names_size);
153 }
154 else
155 {
156 table_ = (vm_bif_entry_t **)t3malloc(new_table_size);
157 names_ = (char **)t3malloc(alloc_ * new_names_size);
158 }
159 }
160 }
161
162 /* ------------------------------------------------------------------------ */
163 /*
164 * Add an entry to the table
165 */
166 void CVmBifTable::add_entry(VMG_ const char *func_set_id)
167 {
168 vm_bif_entry_t *entry;
169 const char *vsn;
170 size_t name_len;
171
172 /* ensure we have space for one more entry */
173 ensure_space(count_ + 1, 5);
174
175 /* find the version suffix in the name, if any */
176 vsn = lib_find_vsn_suffix(func_set_id, '/', "000000", &name_len);
177
178 /* look up the function set by name */
179 for (entry = G_bif_reg_table ; entry->func_set_id != 0 ; ++entry)
180 {
181 const char *entry_vsn;
182 size_t entry_name_len;
183
184 /* find the version number in this entry */
185 entry_vsn = lib_find_vsn_suffix(entry->func_set_id, '/', "000000",
186 &entry_name_len);
187
188 /* see if this is a match */
189 if (name_len == entry_name_len
190 && memcmp(func_set_id, entry->func_set_id, name_len) == 0)
191 {
192 /*
193 * make sure the version provided in the VM is at least as
194 * high as the requested version
195 */
196 if (strcmp(vsn, entry_vsn) > 0)
197 {
198 /* it's out of date - throw "function set too old" */
199 err_throw_a(VMERR_FUNCSET_TOO_OLD, 4,
200 ERR_TYPE_TEXTCHAR, func_set_id,
201 ERR_TYPE_TEXTCHAR, entry_vsn,
202 ERR_TYPE_FUNCSET, func_set_id,
203 ERR_TYPE_VERSION_FLAG);
204 }
205
206 /*
207 * It's a match - add the new entry. Simply keep a pointer
208 * to the static table entry.
209 */
210 table_[count_] = entry;
211
212 /* store the new name element as well */
213 names_[count_] = lib_copy_str(func_set_id);
214
215 /* complete the linkage to the image file */
216 entry->link_to_image(vmg_ count_);
217
218 /* count the new entry */
219 ++count_;
220
221 /* we're done */
222 return;
223 }
224 }
225
226 /* we didn't find it - handle it according to our resolution mode */
227 add_entry_unresolved(vmg_ func_set_id);
228 }
229
230 /* ------------------------------------------------------------------------ */
231 /*
232 * Look up an entry by name
233 */
234 const vm_bif_entry_t *CVmBifTable::get_entry(const char *name)
235 {
236 /* find the version suffix in the name, if any */
237 size_t namelen;
238 const char *vsn = lib_find_vsn_suffix(name, '/', "000000", &namelen);
239
240 /* scan the table */
241 for (size_t i = 0 ; i < count_ ; ++i)
242 {
243 /* find the version number in this entry */
244 size_t enamelen;
245 const char *evsn = lib_find_vsn_suffix(
246 names_[i], '/', "000000", &enamelen);
247
248 /* compare names */
249 if (namelen == enamelen && memcmp(name, names_[i], namelen) == 0)
250 {
251 /* the names match - compare versions */
252 if (strcmp(vsn, evsn) <= 0)
253 {
254 /*
255 * the loaded version is at least as new as the requested
256 * version, so we have a match
257 */
258 return table_[i];
259 }
260 else
261 {
262 /*
263 * this function set is loaded, but the linked version is
264 * older than the requested version, so it counts as not
265 * loaded
266 */
267 return 0;
268 }
269 }
270 }
271
272 /* didn't find a match */
273 return 0;
274 }
275
276
277 /* ------------------------------------------------------------------------ */
278 /*
279 * Validate an entry
280 */
281 int CVmBifTable::validate_entry(uint set_index, uint func_index)
282 {
283 /* validate that the set index is in range */
284 if (set_index >= count_)
285 return FALSE;
286
287 /* get the function set, and make sure it's valid */
288 vm_bif_entry_t *entry = table_[set_index];
289 if (entry == 0)
290 return FALSE;
291
292 /* validate that the function index is in range for the set */
293 if (func_index > entry->func_count)
294 return FALSE;
295
296 /* make sure there's a function pointer in the descriptor */
297 vm_bif_desc *desc = &entry->func[func_index];
298 if (desc->func == 0)
299 return FALSE;
300
301 /* it's a valid entry */
302 return TRUE;
303 }
304
305 /* ------------------------------------------------------------------------ */
306 /*
307 * Function Set helper functions
308 */
309
310 /*
311 * check arguments; throws an error if the argument count doesn't match
312 * the given value
313 */
314 void CVmBif::check_argc(VMG_ uint argc, uint needed_argc)
315 {
316 if (argc != needed_argc)
317 err_throw(VMERR_WRONG_NUM_OF_ARGS);
318 }
319
320 /*
321 * check arguments; throws an error if the argument count is outside of
322 * the given range
323 */
324 void CVmBif::check_argc_range(VMG_ uint argc, uint argc_min, uint argc_max)
325 {
326 if (argc < argc_min || argc > argc_max)
327 err_throw(VMERR_WRONG_NUM_OF_ARGS);
328 }
329
330 /*
331 * return a string value
332 */
333 void CVmBif::retval_str(VMG_ const char *str)
334 {
335 retval_str(vmg_ str, strlen(str));
336 }
337
338 void CVmBif::retval_str(VMG_ const char *str, size_t len)
339 {
340 vm_obj_id_t str_obj;
341
342 /* create a string to hold the return value */
343 str_obj = CVmObjString::create(vmg_ FALSE, str, len);
344
345 /* return the string */
346 retval_obj(vmg_ str_obj);
347 }
348
349 /*
350 * return a string value that came from the UI character set
351 */
352 void CVmBif::retval_ui_str(VMG_ const char *str)
353 {
354 retval_ui_str(vmg_ str, strlen(str));
355 }
356
357 void CVmBif::retval_ui_str(VMG_ const char *str, size_t len)
358 {
359 /* make a string object from the UI string, and return it */
360 retval_obj(vmg_ str_from_ui_str(vmg_ str, len));
361 }
362
363 /*
364 * create a new string object from a string in the UI character set
365 */
366 vm_obj_id_t CVmBif::str_from_ui_str(VMG_ const char *str)
367 {
368 return str_from_ui_str(vmg_ str, strlen(str));
369 }
370
371 vm_obj_id_t CVmBif::str_from_ui_str(VMG_ const char *str, size_t len)
372 {
373 /* figure out how much space we need */
374 char *outp = 0;
375 size_t outlen = 0;
376 outlen = G_cmap_from_ui->map(0, &outlen, str, len);
377
378 /* allocate a string of that size */
379 vm_obj_id_t str_id = CVmObjString::create(vmg_ FALSE, outlen);
380 CVmObjString *str_obj = (CVmObjString *)vm_objp(vmg_ str_id);
381
382 /* map the string into the new string buffer */
383 outp = str_obj->cons_get_buf();
384 G_cmap_from_ui->map(&outp, &outlen, str, len);
385
386 /* return the new string object */
387 return str_id;
388 }
389
390 /*
391 * return an object value
392 */
393 void CVmBif::retval_obj(VMG_ vm_obj_id_t obj)
394 {
395 if (obj == VM_INVALID_OBJ)
396 G_interpreter->get_r0()->set_nil();
397 else
398 G_interpreter->get_r0()->set_obj(obj);
399 }
400
401 /*
402 * return a property value
403 */
404 void CVmBif::retval_prop(VMG_ vm_prop_id_t prop)
405 {
406 G_interpreter->get_r0()->set_propid(prop);
407 }
408
409 /*
410 * return an integer value
411 */
412 void CVmBif::retval_int(VMG_ long val)
413 {
414 G_interpreter->get_r0()->set_int(val);
415 }
416
417 /*
418 * return true
419 */
420 void CVmBif::retval_true(VMG0_)
421 {
422 G_interpreter->get_r0()->set_true();
423 }
424
425 /*
426 * return nil
427 */
428 void CVmBif::retval_nil(VMG0_)
429 {
430 G_interpreter->get_r0()->set_nil();
431 }
432
433 /*
434 * return a boolean value - nil if false, true if true
435 */
436 void CVmBif::retval_bool(VMG_ int val)
437 {
438 G_interpreter->get_r0()->set_logical(val);
439 }
440
441 /*
442 * return a function pointer value
443 */
444 void CVmBif::retval_fnptr(VMG_ pool_ofs_t ofs)
445 {
446 G_interpreter->get_r0()->set_fnptr(ofs);
447 }
448
449 /*
450 * return a value
451 */
452 void CVmBif::retval(VMG_ const vm_val_t *val)
453 {
454 *G_interpreter->get_r0() = *val;
455 }
456
457 /*
458 * return a built-in function pointer
459 */
460 void CVmBif::retval_bifptr(VMG_ ushort set_idx, ushort func_idx)
461 {
462 G_interpreter->get_r0()->set_bifptr(set_idx, func_idx);
463 }
464
465 /*
466 * return the value at top of stack
467 */
468 void CVmBif::retval_pop(VMG0_)
469 {
470 G_stk->pop(G_interpreter->get_r0());
471 }
472
473 /* ------------------------------------------------------------------------ */
474 /*
475 * Pop a string value
476 */
477 const char *CVmBif::pop_str_val(VMG0_)
478 {
479 /* pop the value */
480 vm_val_t val;
481 G_stk->pop(&val);
482
483 /* interpret it as a string */
484 return get_str_val(vmg_ &val);
485 }
486
487 /* get a string value */
488 const char *CVmBif::get_str_val(VMG_ const vm_val_t *val)
489 {
490 CVmObject *obj;
491 const char *p;
492
493 /* see what we have */
494 switch(val->typ)
495 {
496 case VM_SSTRING:
497 /* it's a constant string - get the constant pool pointer */
498 return G_const_pool->get_ptr(val->val.ofs);
499
500 case VM_OBJ:
501 /* get the object */
502 obj = vm_objp(vmg_ val->val.obj);
503
504 /* get the string value, if it has one */
505 p = obj->get_as_string(vmg0_);
506
507 /* if it has a string value, return it */
508 if (p != 0)
509 return p;
510
511 /* we didn't get a string value */
512 break;
513
514 default:
515 /* other types don't have a string value */
516 break;
517 }
518
519 /* if we got here, the value isn't a string */
520 err_throw(VMERR_STRING_VAL_REQD);
521 AFTER_ERR_THROW(return 0;)
522 }
523
524 /* ------------------------------------------------------------------------ */
525 /*
526 * Pop a list value
527 */
528 const char *CVmBif::pop_list_val(VMG0_)
529 {
530 vm_val_t val;
531 CVmObject *obj;
532 const char *p;
533
534 /* pop the value */
535 G_stk->pop(&val);
536
537 /* see what we have */
538 switch(val.typ)
539 {
540 case VM_LIST:
541 /* it's a constant list - get the constant pool pointer */
542 return G_const_pool->get_ptr(val.val.ofs);
543
544 case VM_OBJ:
545 /* get the object */
546 obj = vm_objp(vmg_ val.val.obj);
547
548 /* get the list value, if it has one */
549 p = obj->get_as_list();
550
551 /* if it has a list value, return it */
552 if (p != 0)
553 return p;
554
555 /* we didn't get a list value */
556 break;
557
558 default:
559 /* other types don't have a list value */
560 break;
561 }
562
563 /* if we got here, the value isn't a list */
564 err_throw(VMERR_LIST_VAL_REQD);
565 AFTER_ERR_THROW(return 0;)
566 }
567
568 /* ------------------------------------------------------------------------ */
569 /*
570 * Pop a string into a buffer, and null-terminate the result.
571 */
572 void CVmBif::pop_str_val_buf(VMG_ char *buf, size_t buflen)
573 {
574 /* pop the top value from the stack and copy its string value */
575 vm_val_t val;
576 G_stk->pop(&val);
577 get_str_val_buf(vmg_ buf, buflen, &val);
578 }
579
580 /*
581 * Get a string value into a buffer, null-terminating the result
582 */
583 void CVmBif::get_str_val_buf(VMG_ char *buf, size_t buflen,
584 const vm_val_t *val)
585 {
586 /* pop the string value */
587 const char *strp = get_str_val(vmg_ val);
588
589 /*
590 * get the length, but limit it to our buffer size, less one byte
591 * for null termination
592 */
593 size_t copy_len = vmb_get_len(strp);
594 if (copy_len > buflen - 1)
595 copy_len = utf8_ptr::s_trunc(strp + VMB_LEN, buflen - 1);
596
597 /* copy the string */
598 memcpy(buf, strp + VMB_LEN, copy_len);
599
600 /* null-terminate the result */
601 buf[copy_len] = '\0';
602 }
603
604
605 /* ------------------------------------------------------------------------ */
606 /*
607 * Pop a filename value. The source value can be string or FileName
608 * object.
609 */
610 void CVmBif::pop_fname_val(VMG_ char *buf, size_t buflen)
611 {
612 get_fname_val(vmg_ buf, buflen, G_stk->get(0));
613 G_stk->discard(1);
614 }
615
616 void CVmBif::get_fname_val(VMG_ char *buf, size_t buflen, const vm_val_t *val)
617 {
618 CVmObjFileName *fn;
619 const char *str;
620 if ((fn = vm_val_cast(CVmObjFileName, val)) != 0)
621 {
622 /* get the FileName object's path as a local filename string */
623 get_str_val_fname(vmg_ buf, buflen, fn->get_path_string());
624 }
625 else if ((str = val->get_as_string(vmg0_)) != 0)
626 {
627 /* get the string value as a local filename string */
628 get_str_val_fname(vmg_ buf, buflen, str);
629 }
630 else
631 {
632 err_throw(VMERR_BAD_VAL_BIF);
633 }
634 }
635
636 /*
637 * Pop a string into a buffer, translating the string into the filename
638 * character set and null-terminating the result.
639 */
640 void CVmBif::pop_str_val_fname(VMG_ char *buf, size_t buflen)
641 {
642 /* pop the string and translate to a filename */
643 get_str_val_fname(vmg_ buf, buflen, pop_str_val(vmg0_));
644 }
645
646 /*
647 * Get a string into a buffer, translting the string into the filename
648 * character set and null-terminating the result.
649 */
650 void CVmBif::get_str_val_fname(VMG_ char *buf, size_t buflen, const char *str)
651 {
652 /* get the length */
653 size_t copy_len = vmb_get_len(str);
654
655 /*
656 * map it into the local filename character set and store the result
657 * in the output buffer - reserve one byte for the null termination
658 * byte
659 */
660 copy_len = G_cmap_to_fname->map_utf8(buf, buflen - 1,
661 str + VMB_LEN, copy_len, 0);
662
663 /* null-terminate the result */
664 buf[copy_len] = '\0';
665 }
666
667 /*
668 * Pop a string into a buffer, translating the string into the user
669 * interface character set and null-terminating the result.
670 */
671 char *CVmBif::pop_str_val_ui(VMG_ char *buf, size_t buflen)
672 {
673 /* pop the string value */
674 const char *strp = pop_str_val(vmg0_);
675
676 /* decode and skip the length prefix */
677 size_t copy_len = vmb_get_len(strp);
678 strp += VMB_LEN;
679
680 /*
681 * if they didn't allocate any space for the buffer, allocate one on
682 * the caller's behalf; otherwise map the string into the caller's
683 * buffer
684 */
685 if (buflen == 0)
686 {
687 /* the caller didn't provide a buffer, so allocate one */
688 copy_len = G_cmap_to_ui->map_utf8_alo(&buf, strp, copy_len);
689 }
690 else
691 {
692 /* the caller provided a buffer, so use it for the mapping result */
693 copy_len = G_cmap_to_ui->map_utf8(buf, buflen - 1, strp, copy_len, 0);
694 }
695
696 /* null-terminate the result */
697 buf[copy_len] = '\0';
698
699 /* return the buffer */
700 return buf;
701 }
702
703 /* ------------------------------------------------------------------------ */
704 /*
705 * Pop an integer value
706 */
707 int CVmBif::pop_int_val(VMG0_)
708 {
709 /* pop a number */
710 vm_val_t val;
711 G_interpreter->pop_int(vmg_ &val);
712
713 /* return the value */
714 return (int)val.val.intval;
715 }
716
717 int CVmBif::pop_int_or_nil(VMG_ int defval)
718 {
719 /* check for nil */
720 if (G_stk->get(0)->typ == VM_NIL)
721 {
722 /* it's nil - discard the nil and return the default value */
723 G_stk->discard();
724 return defval;
725 }
726 else
727 {
728 /* not nil - return the integer value */
729 return pop_int_val(vmg0_);
730 }
731 }
732
733 /*
734 * Pop a long integer value
735 */
736 int32_t CVmBif::pop_long_val(VMG0_)
737 {
738 /* pop a number */
739 vm_val_t val;
740 G_interpreter->pop_int(vmg_ &val);
741
742 /* return the value */
743 return val.val.intval;
744 }
745
746 /*
747 * Pop a true/nil logical value
748 */
749 int CVmBif::pop_bool_val(VMG0_)
750 {
751 /* pop a value */
752 vm_val_t val;
753 G_stk->pop(&val);
754
755 /* check the type */
756 switch(val.typ)
757 {
758 case VM_NIL:
759 /* nil - interpret this as false */
760 return FALSE;
761
762 case VM_TRUE:
763 /* true */
764 return TRUE;
765
766 case VM_INT:
767 /* integer - return true if it's nonzero */
768 return (val.val.intval != 0);
769
770 default:
771 /* anything else is unacceptable */
772 err_throw(VMERR_BAD_TYPE_BIF);
773
774 /*
775 * (for the compiler's benefit, which doesn't know err_throw
776 * doesn't return and thus might want to warn about our failure to
777 * return a value)
778 */
779 AFTER_ERR_THROW(return FALSE;)
780 }
781 }
782
783 /*
784 * Pop an object reference value
785 */
786 vm_obj_id_t CVmBif::pop_obj_val(VMG0_)
787 {
788 /* pop an object reference */
789 vm_val_t val;
790 G_interpreter->pop_obj(vmg_ &val);
791
792 /* return the value */
793 return val.val.obj;
794 }
795
796 /*
797 * Pop a property ID value
798 */
799 vm_prop_id_t CVmBif::pop_propid_val(VMG0_)
800 {
801 /* pop a property ID */
802 vm_val_t val;
803 G_interpreter->pop_prop(vmg_ &val);
804
805 /* return the value */
806 return val.val.prop;
807 }
808
809 /*
810 * Pop a CharacterSet argument. This can also be given as a string with
811 * the name of a character set, in which case we'll create a CharacterSet
812 * object for the given name.
813 */
814 vm_obj_id_t CVmBif::pop_charset_obj(VMG0_)
815 {
816 /* pop the argument */
817 vm_val_t val;
818 G_stk->pop(&val);
819
820 /* get the character set object from the argument value */
821 return get_charset_obj(vmg_ &val);
822 }
823
824 vm_obj_id_t CVmBif::get_charset_obj(VMG_ int stk_idx)
825 {
826 /* get the character set object from the value at the stack index */
827 return get_charset_obj(vmg_ G_stk->get(stk_idx));
828 }
829
830 vm_obj_id_t CVmBif::get_charset_obj(VMG_ const vm_val_t *val)
831 {
832 const char *str;
833
834 /*
835 * check to see if it's a CharacterSet object; if it's not, it must be
836 * a string giving the character set name
837 */
838 if (val->typ == VM_OBJ && CVmObjCharSet::is_charset(vmg_ val->val.obj))
839 {
840 /* it's a CharacterSet object - return it */
841 return val->val.obj;
842 }
843 else if ((str = val->get_as_string(vmg0_)) != 0)
844 {
845 /* it's a string - create a mapper for the given character set name */
846 return CVmObjCharSet::create(
847 vmg_ FALSE, str + VMB_LEN, vmb_get_len(str));
848 }
849 else if (val->typ == VM_NIL)
850 {
851 /* nil simply means no character set */
852 return VM_INVALID_OBJ;
853 }
854 else
855 {
856 /* invalid argument */
857 err_throw(VMERR_BAD_TYPE_BIF);
858 AFTER_ERR_THROW(return VM_INVALID_OBJ);
859 }
860 }
861
862