Debian Codegraph @

terps/tads/tads3/vmanonfn.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2000, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmanonfn.h - anonymous function metaclass
12 Function
13 The anonymous function metaclass is a subclass of the Vector metaclass
14 that provides encapsulation of an anonymous function and the context it
15 shares with its enclosing scope.
16 Notes
17
18 Modified
19 03/21/00 MJRoberts - Creation
20 */
21
22 #ifndef VMANONFN_H
23 #define VMANONFN_H
24
25 #include <stdlib.h>
26 #include "vmtype.h"
27 #include "vmobj.h"
28 #include "vmvec.h"
29 #include "vmglob.h"
30
31 /*
32 * Anonymous Function Metaclass
33 */
34 class CVmObjAnonFn: public CVmObjVector
35 {
36 friend class CVmMetaclassAnonFn;
37
38 public:
39 /* metaclass registration object */
40 static class CVmMetaclass *metaclass_reg_;
41 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
42
43 /* am I of the given metaclass? */
44 virtual int is_of_metaclass(class CVmMetaclass *meta) const
45 {
46 /* try my own metaclass and my base class */
47 return (meta == metaclass_reg_
48 || CVmObjVector::is_of_metaclass(meta));
49 }
50
51 /* is the given object an anonymous function object? */
52 static int is_anonfn_obj(VMG_ vm_obj_id_t obj)
53 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
54
55 /* we don't look like a list to user code */
56 int is_listlike(VMG_ vm_obj_id_t) { return FALSE; }
57
58 /* create dynamically using stack arguments */
59 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
60 uint argc);
61
62 /* invoke */
63 int get_invoker(VMG_ vm_val_t *val);
64
65 /* check for equality - compare strictly by reference */
66 int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int) const
67 {
68 /* return true if the other value is a reference to this object */
69 return (val->typ == VM_OBJ && val->val.obj == self);
70 }
71
72 /* calculate a hash value */
73 uint calc_hash(VMG_ vm_obj_id_t self, int) const
74 {
75 /* we compare by reference, so hash by object ID */
76 return (uint)(((ulong)self & 0xffff)
77 ^ (((ulong)self & 0xffff0000) >> 16));
78 }
79
80 protected:
81 /* create an empty object */
82 CVmObjAnonFn() { ext_ = 0; }
83
84 /* create with the given number of elements */
85 CVmObjAnonFn(VMG_ size_t cnt)
86 : CVmObjVector(vmg_ cnt)
87 {
88 /* set to our full initial size */
89 set_element_count(cnt);
90 }
91 };
92
93
94 /* ------------------------------------------------------------------------ */
95 /*
96 * Registration table objects
97 */
98 class CVmMetaclassAnonFn: public CVmMetaclass
99 {
100 public:
101 /* get the global name */
102 const char *get_meta_name() const { return "anon-func-ptr/030000"; }
103
104 /* create from image file */
105 void create_for_image_load(VMG_ vm_obj_id_t id)
106 {
107 new (vmg_ id) CVmObjAnonFn();
108 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
109 }
110
111 /* create from restoring from saved state */
112 void create_for_restore(VMG_ vm_obj_id_t id)
113 {
114 new (vmg_ id) CVmObjAnonFn();
115 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
116 }
117
118 /* create dynamically using stack arguments */
119 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
120 { return CVmObjAnonFn::create_from_stack(vmg_ pc_ptr, argc); }
121
122 /* call a static property */
123 int call_stat_prop(VMG_ vm_val_t *result,
124 const uchar **pc_ptr, uint *argc,
125 vm_prop_id_t prop)
126 {
127 /* fall back directly on the vector static property evaluator */
128 return CVmObjVector::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
129 }
130
131 /* I'm a Vector subclass */
132 CVmMetaclass *get_supermeta_reg() const
133 { return CVmObjVector::metaclass_reg_; }
134 };
135
136 #endif /* VMANONFN_H */
137
138 /*
139 * Register the class
140 */
141 VM_REGISTER_METACLASS(CVmObjAnonFn)
142
143