Debian Codegraph @

terps/tads/tads3/vmbifc.cpp

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header$";
4 #endif
5
6 /*
7 * Copyright (c) 1999, 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 vmbifc.cpp - built-in function - Call-time resolution
15 Function
16 This is a version of the built-in function interface for resolving
17 built-ins only when each built-in is invoked. This version allows
18 loading an image file with unresolved function sets, then checks
19 on each built-in function's invocation to make sure the function is
20 available.
21
22 This version can be used in a version of the interpreter used by
23 the compiler for running 'preinit' or similar situations in which it
24 is desirable to be able to load and run a program with unresolved
25 function sets. This version is less efficient than the load-time
26 resolver, so normal stand-alone interpreters should use the load-time
27 version instead.
28 Notes
29
30 Modified
31 07/21/99 MJRoberts - Creation
32 */
33
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "t3std.h"
38 #include "vmtype.h"
39 #include "vmerr.h"
40 #include "vmerrnum.h"
41 #include "vmglob.h"
42 #include "vmbif.h"
43 #include "vmbifreg.h"
44 #include "vmstr.h"
45 #include "vmobj.h"
46 #include "vmrun.h"
47
48
49 /* ------------------------------------------------------------------------ */
50 /*
51 * Call the given function from the given function set.
52 */
53 void CVmBifTable::call_func(VMG_ uint set_index, uint func_index, uint argc)
54 {
55 vm_bif_entry_t *entry;
56 vm_bif_desc *desc;
57
58 /* get the function set */
59 entry = table_[set_index];
60
61 /* if the function set is null, we can't call the function */
62 if (entry == 0)
63 err_throw_a(VMERR_UNKNOWN_FUNC_SET, 1,
64 ERR_TYPE_TEXTCHAR, names_[set_index]);
65
66 /* get the function pointer */
67 desc = &entry->func[func_index];
68
69 /* if the function is null, we can't call it */
70 if (desc->func == 0)
71 err_throw_a(VMERR_UNAVAIL_INTRINSIC, 2,
72 ERR_TYPE_TEXTCHAR, names_[set_index],
73 ERR_TYPE_INT, func_index);
74
75 /* call the function */
76 (*desc->func)(vmg_ argc);
77 }
78
79 /*
80 * Handle adding a function set entry that's unresolvable at load-time
81 */
82 void CVmBifTable::add_entry_unresolved(VMG_ const char *func_set_id)
83 {
84 /*
85 * Since this is the call-time resolver, allow loading of the image
86 * file even though this function set is unresolved. Store a null
87 * entry in the function set table, and store the name of the
88 * function set - we'll need this in case the program attempts to
89 * invoke a function in this function set, so that we can generate
90 * an error containing the unresolved function set name.
91 */
92 table_[count_] = 0;
93 names_[count_] = lib_copy_str(func_set_id);
94
95 /* count the new entry */
96 ++count_;
97 }
98
99 /*
100 * Get a function's descriptor
101 */
102 const vm_bif_desc *CVmBifTable::get_desc(uint set_index, uint func_index)
103 {
104 vm_bif_entry_t *entry;
105 vm_bif_desc *desc;
106
107 /* validate that the set index is in range */
108 if (set_index >= count_)
109 return 0;
110
111 /* get the function set, and make sure it's valid */
112 entry = table_[set_index];
113 if (entry == 0)
114 return 0;
115
116 /* validate that the function index is in range */
117 if (func_index > entry->func_count)
118 return 0;
119
120 /* get the descriptor, and make sure there's a function pointer */
121 desc = &entry->func[func_index];
122 if (desc->func == 0)
123 return 0;
124
125 /* return the descriptor */
126 return desc;
127 }
128