cfad47cfa3/t3compiler/tads3/test/test_obj.cpp

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