| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/test/TEST_OBJ.CPP,v 1.3 1999/07/11 00:47:03 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 | test_obj.cpp - test the object subsystem |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 11/06/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmglob.h" |
| 29 | #include "vmtype.h" |
| 30 | #include "vmobj.h" |
| 31 | #include "vmlst.h" |
| 32 | #include "vmstr.h" |
| 33 | #include "vmtobj.h" |
| 34 | #include "vmundo.h" |
| 35 | #include "vmstack.h" |
| 36 | #include "vmpool.h" |
| 37 | #include "vmimage.h" |
| 38 | #include "vmrun.h" |
| 39 | #include "vminit.h" |
| 40 | #include "vmhostsi.h" |
| 41 | #include "vmmaincn.h" |
| 42 | #include "t3test.h" |
| 43 | |
| 44 | |
| 45 | const vm_prop_id_t PROP_A = 100; |
| 46 | const vm_prop_id_t PROP_B = 101; |
| 47 | const vm_prop_id_t PROP_C = 200; |
| 48 | const vm_prop_id_t PROP_D = 201; |
| 49 | const vm_prop_id_t PROP_E = 204; |
| 50 | |
| 51 | class CImageFile: public CVmPoolBackingStore |
| 52 | { |
| 53 | /* |
| 54 | * CVmPoolBackingStore implementation |
| 55 | */ |
| 56 | |
| 57 | size_t vmpbs_get_page_count() { return 1; } |
| 58 | size_t vmpbs_get_common_page_size() { return 4096; } |
| 59 | size_t vmpbs_get_page_size(pool_ofs_t, size_t) { return 4096; } |
| 60 | void vmpbs_load_page(pool_ofs_t, size_t, size_t, char *) { } |
| 61 | const char *vmpbs_alloc_and_load_page(pool_ofs_t ofs, size_t page_size, |
| 62 | size_t load_size) |
| 63 | { |
| 64 | char *mem = (char *)t3malloc(load_size); |
| 65 | vmpbs_load_page(ofs, page_size, load_size, mem); |
| 66 | return mem; |
| 67 | } |
| 68 | void vmpbs_free_page(const char *mem, pool_ofs_t, size_t) |
| 69 | { |
| 70 | t3free((char *)mem); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | int main(int argc, char **argv) |
| 75 | { |
| 76 | CImageFile *imagefp; |
| 77 | vm_val_t val; |
| 78 | vm_val_t *stkval; |
| 79 | vm_globals *vmg__; |
| 80 | CVmHostIfc *hostifc; |
| 81 | CVmMainClientConsole clientifc; |
| 82 | |
| 83 | /* initialize for testing */ |
| 84 | test_init(); |
| 85 | |
| 86 | /* initialize the VM */ |
| 87 | hostifc = new CVmHostIfcStdio(argv[0]); |
| 88 | vm_init_options iniopts(hostifc, &clientifc, "us-ascii", "us_ascii"); |
| 89 | vm_initialize(&vmg__, &iniopts); |
| 90 | |
| 91 | /* create a fake host file object */ |
| 92 | imagefp = new CImageFile(); |
| 93 | |
| 94 | /* create the constant pool */ |
| 95 | G_const_pool->attach_backing_store(imagefp); |
| 96 | |
| 97 | /* create a couple of objects and push them on the stack */ |
| 98 | val.set_obj(CVmObjTads::create(vmg_ FALSE, 0, 4)); |
| 99 | G_stk->push(&val); |
| 100 | val.set_obj(CVmObjTads::create(vmg_ FALSE, 0, 4)); |
| 101 | G_stk->push(&val); |
| 102 | |
| 103 | /* |
| 104 | * create another object, and store it in a property of the first |
| 105 | * object |
| 106 | */ |
| 107 | val.set_obj(CVmObjTads::create(vmg_ FALSE, 0, 4)); |
| 108 | stkval = G_stk->get(1); |
| 109 | vm_objp(vmg_ stkval->val.obj)-> |
| 110 | set_prop(vmg_ G_undo, stkval->val.obj, PROP_A, &val); |
| 111 | |
| 112 | /* collect garbage - nothing should be deleted at this point */ |
| 113 | G_obj_table->gc_full(vmg0_); |
| 114 | |
| 115 | /* |
| 116 | * forget about the second object by popping it off the stack, then |
| 117 | * collect garbage again -- the second object should be deleted at |
| 118 | * this point, because it's no longer reachable |
| 119 | */ |
| 120 | G_stk->discard(); |
| 121 | G_obj_table->gc_full(vmg0_); |
| 122 | |
| 123 | /* |
| 124 | * Force the first object's property table to expand by filling up |
| 125 | * its initial table |
| 126 | */ |
| 127 | stkval = G_stk->get(0); |
| 128 | val.set_nil(); |
| 129 | vm_objp(vmg_ stkval->val.obj)-> |
| 130 | set_prop(vmg_ G_undo, stkval->val.obj, PROP_A, &val); |
| 131 | vm_objp(vmg_ stkval->val.obj)-> |
| 132 | set_prop(vmg_ G_undo, stkval->val.obj, PROP_B, &val); |
| 133 | vm_objp(vmg_ stkval->val.obj)-> |
| 134 | set_prop(vmg_ G_undo, stkval->val.obj, PROP_C, &val); |
| 135 | vm_objp(vmg_ stkval->val.obj)-> |
| 136 | set_prop(vmg_ G_undo, stkval->val.obj, PROP_D, &val); |
| 137 | vm_objp(vmg_ stkval->val.obj)-> |
| 138 | set_prop(vmg_ G_undo, stkval->val.obj, PROP_E, &val); |
| 139 | |
| 140 | /* set an existing property */ |
| 141 | vm_objp(vmg_ stkval->val.obj)-> |
| 142 | set_prop(vmg_ G_undo, stkval->val.obj, PROP_B, &val); |
| 143 | |
| 144 | /* we're done, so delete all of our managers */ |
| 145 | G_const_pool->detach_backing_store(); |
| 146 | delete imagefp; |
| 147 | |
| 148 | /* shut down the VM */ |
| 149 | vm_terminate(vmg__, &clientifc); |
| 150 | |
| 151 | /* delete the host interface */ |
| 152 | delete hostifc; |
| 153 | |
| 154 | /* terminate */ |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* ------------------------------------------------------------------------ */ |
| 159 | /* |
| 160 | * dummy entrypoints - these are needed for linking, but we don't actually |
| 161 | * call anything that will end up invoking any of these |
| 162 | */ |
| 163 | |
| 164 | /* dummy implementation of dynamic link */ |
| 165 | void CVmImageLoader::do_dynamic_link(VMG0_) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | /* more image loader dummy implementations */ |
| 170 | void CVmImageLoader::save_synth_exports(VMG_ class CVmFile *fp) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | void CVmImageLoader::create_global_symtab_lookup_table(VMG0_) |
| 175 | { |
| 176 | } |
| 177 | |
| 178 | int CVmImageLoader::restore_synth_exports(VMG_ class CVmFile *fp, |
| 179 | class CVmObjFixup *) |
| 180 | { |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | void CVmImageLoader::run_static_init(VMG0_) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | void CVmImageLoader::discard_synth_exports() |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | vm_prop_id_t CVmImageLoader::alloc_new_prop(VMG0_) |
| 193 | { |
| 194 | return VM_INVALID_PROP; |
| 195 | } |
| 196 | |
| 197 | vm_prop_id_t CVmImageLoader::get_last_prop(VMG0_) |
| 198 | { |
| 199 | return (vm_prop_id_t)0; |
| 200 | } |
| 201 |