| 1 | /* |
| 2 | * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | vmcore.h - T3 VM "core" interpreter - example function set definition |
| 10 | Function |
| 11 | This is an example of how to define custom intrinsic (i.e., native C++) |
| 12 | functions for the T3 VM. An intrinsic function is a function written in |
| 13 | C++, and part of the application executable, that can be called from TADS |
| 14 | code (i.e., from within .t source). |
| 15 | |
| 16 | This file can be used as a template for creating custom intrinsics. |
| 17 | Simply make a copy of this entire file, and then make the following |
| 18 | changes: |
| 19 | |
| 20 | 1. Change the #ifndef symbol from VMCORE_H to a different name of your |
| 21 | own choosing - usually, the name matches the name of the .h file itself. |
| 22 | This symbol appears three times in the file, so you should be careful to |
| 23 | change all three to the same new symbol. |
| 24 | |
| 25 | 2. Change the name of class CVmBifSample to a name of your own choosing. |
| 26 | Choose a name that describes the custom function set you're defining. This |
| 27 | name appears in the class definition and also in each entry in the list |
| 28 | near the bottom of the file, so make sure to change every occurrence of the |
| 29 | name to the same new name. |
| 30 | |
| 31 | 3. Change the names of the functions themselves. Note that each function |
| 32 | name appears twice - first in the class definition near the start of the |
| 33 | file, then again in the initialization list at the bottom of the file. You |
| 34 | can add and delete functions as you wish. If you add new functions, simply |
| 35 | copy an existing definition and change its name, being sure to add the |
| 36 | function in both the class definition and the list at the bottom of the |
| 37 | file. |
| 38 | |
| 39 | 4. You must, of course, provide an implementation for each of your |
| 40 | functions. Do that in a new .cpp file corresponding to your new header |
| 41 | file. Make sure you #include your new header file from your new .cpp file. |
| 42 | You can use the implementations in vmcore.cpp as a template for your new |
| 43 | functions. |
| 44 | |
| 45 | 5. Make a copy of the file vmbifreg_core.cpp. Add a #include line for |
| 46 | the new copy of this file you have created (you can add it right after the |
| 47 | #include for vmbiftad.h). Also add a MAKE_ENTRY() line for your new |
| 48 | function set - you can add it where the comments indicate in your copy of |
| 49 | vmbifreg_core.cpp. The MAKE_ENTRY() line would look like this: |
| 50 | |
| 51 | MAKE_ENTRY("core-sample/010000", CVmBifSample); |
| 52 | |
| 53 | Use the same name you use for the variable defined in the initialization |
| 54 | list near the end of the file. For the string, you can use anything you |
| 55 | want, but you should choose something reasonably long and descriptive to |
| 56 | avoid overlapping with anyone else's function set identifiers. |
| 57 | |
| 58 | 6. Take vmbifreg_core.obj out of the application build, and replace it |
| 59 | with the copy you created in step 5. |
| 60 | |
| 61 | 7. Take vmcore.obj out of the application build, and replace it with your |
| 62 | new .cpp file from step 4. |
| 63 | |
| 64 | 8. Change the variable name G_bif_sample near the end of this file to a |
| 65 | different name of your own choosing. |
| 66 | |
| 67 | 9. Create a TADS header file, for inclusion in your TADS (.t) source |
| 68 | code, defining your intrinsic functions. See the sample file "core.h" |
| 69 | |
| 70 | intrinsic 'core-sample/010000' |
| 71 | { |
| 72 | // display the given string |
| 73 | displayText(str); |
| 74 | |
| 75 | // read a line of text from the keyboard, and return it as a string |
| 76 | readText(); |
| 77 | } |
| 78 | |
| 79 | You MUST define the intrinsic functions in the EXACT SAME ORDER that they |
| 80 | appear in the initialization list near the end of this file. For the name |
| 81 | of the function set (the 'core-sample/010000' string), you must use the |
| 82 | EXACT SAME string you used in the MAKE_ENTRY() definition in step 5. |
| 83 | |
| 84 | Everything else in this file is boilerplate text, so you shouldn't have |
| 85 | to change anything else. |
| 86 | |
| 87 | Notes |
| 88 | |
| 89 | Modified |
| 90 | 04/06/02 MJRoberts - Creation |
| 91 | */ |
| 92 | |
| 93 | #ifndef VMCORE_H |
| 94 | #define VMCORE_H |
| 95 | |
| 96 | #include "vmbif.h" |
| 97 | |
| 98 | /* ------------------------------------------------------------------------ */ |
| 99 | /* |
| 100 | * Our sample built-in functions. Each function we define here will be |
| 101 | * callable as an intrinsic function from TADS source code. |
| 102 | * |
| 103 | * This is only the interface definition. The actual implementations of |
| 104 | * these functions appear in our .cpp file. |
| 105 | * |
| 106 | * Note that every function has the same C++ interface. This has nothing |
| 107 | * to do with the arguments that the TADS program passes to these |
| 108 | * functions; the functions get their TADS arguments from the VM stack. |
| 109 | * See the implementations for details. |
| 110 | */ |
| 111 | class CVmBifSample: public CVmBif |
| 112 | { |
| 113 | public: |
| 114 | /* |
| 115 | * The function vector. Every function-set class must have a static |
| 116 | * member called 'bif_table' containing an array of pointers to the |
| 117 | * functions in the set. The registration mechanism uses this to |
| 118 | * connect calls from the image file to their actual C++ |
| 119 | * implementations. The order of the vector must match the order |
| 120 | * defined in the library header file for the function set, since the |
| 121 | * compiler generates ordinal references to the member functions. |
| 122 | */ |
| 123 | static vm_bif_desc bif_table[]; |
| 124 | |
| 125 | /* function set member functions */ |
| 126 | static void display_text(VMG_ uint argc); |
| 127 | static void read_text(VMG_ uint argc); |
| 128 | }; |
| 129 | |
| 130 | #endif /* VMCORE_H */ |
| 131 | |
| 132 | /* ------------------------------------------------------------------------ */ |
| 133 | /* |
| 134 | * Sample function set vector. Define this only if VMBIF_DEFINE_VECTOR has |
| 135 | * been defined. |
| 136 | * |
| 137 | * IMPORTANT - this definition is outside the #ifdef VMCORE_H section of |
| 138 | * the header file, because we specifically want this part of the file to |
| 139 | * be able to be included multiple times. |
| 140 | * |
| 141 | * ALSO IMPORTANT - the ORDER of the definitions here is significant. You |
| 142 | * must use the EXACT SAME ORDER in your "intrinsic" definition in the |
| 143 | * header file you create for inclusion in your TADS (.t) source code. |
| 144 | * |
| 145 | * The vector must always be called 'bif_table', and it must be a static |
| 146 | * member of the function-set class. The order of the functions defined |
| 147 | * here MUST match the order in the library header file for the function |
| 148 | * set, since the compiler generates ordinal references to the functions. |
| 149 | */ |
| 150 | #ifdef VMBIF_DEFINE_VECTOR |
| 151 | |
| 152 | vm_bif_desc CVmBifSample::bif_table[] = |
| 153 | { |
| 154 | { &CVmBifSample::display_text, 1, 0, FALSE }, |
| 155 | { &CVmBifSample::read_text, 0, 0, FALSE } |
| 156 | }; |
| 157 | |
| 158 | #endif |