-
Notifications
You must be signed in to change notification settings - Fork 1
/
smokec.h
254 lines (214 loc) · 6.82 KB
/
smokec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* smokec.h
*
* Created on: Nov 18, 2013
* Author: pankaj
*/
#ifndef libSmokeC_H
#define libSmokeC_H
#ifdef __cplusplus
extern "C" {
#endif
// BEGIN API
typedef unsigned char cbool;
/*
* C/C++ interop data structures.
*/
// We could just pass everything around using void* (pass-by-reference)
// I don't want to, though. -aw
typedef union StackItem {
void* s_voidp;
cbool s_bool;
signed char s_char;
unsigned char s_uchar;
short s_short;
unsigned short s_ushort;
int s_int;
unsigned int s_uint;
long s_long;
unsigned long s_ulong;
float s_float;
double s_double;
long s_enum;
void* s_class;
} StackItem;
typedef enum EnumOperation {
EnumNew, EnumDelete, EnumFromLong, EnumToLong
} EnumOperation;
typedef StackItem* Stack;
typedef short Index;
typedef void (*ClassFn)(Index method, void* obj, Stack args);
typedef void* (*CastFn)(void* obj, Index from, Index to);
typedef void (*EnumFn)(EnumOperation, Index, void*, long);
typedef enum ClassFlags {
cf_constructor = 0x01, // has a constructor
cf_deepcopy = 0x02, // has copy constructor
cf_virtual = 0x04, // has virtual destructor
cf_namespace = 0x08, // is a namespace
cf_undefined = 0x10 // defined elsewhere
} ClassFlags;
/**
* Describe one class.
*/
typedef struct Class {
const char *className; // Name of the class
cbool external; // Whether the class is in another module
Index parents; // Index into inheritanceList
ClassFn classFn; // Calls any method in the class
EnumFn enumFn; // Handles enum pointers
unsigned short flags; // ClassFlags
unsigned int size;
} Class;
typedef enum MethodFlags {
mf_static = 0x01, mf_const = 0x02, mf_copyctor = 0x04, // Copy constructor
mf_internal = 0x08, // For internal use only
mf_enum = 0x10, // An enum value
mf_ctor = 0x20,
mf_dtor = 0x40,
mf_protected = 0x80,
mf_attribute = 0x100, // accessor method for a field
mf_property = 0x200, // accessor method of a property
mf_virtual = 0x400,
mf_purevirtual = 0x800,
mf_signal = 0x1000, // method is a signal
mf_slot = 0x2000, // method is a slot
mf_explicit = 0x4000 // method is an 'explicit' constructor
} MethodFlags;
/**
* Describe one method of one class.
*/
typedef struct Method {
Index classId; // Index into classes
Index name; // Index into methodNames; real name
Index args; // Index into argumentList
unsigned char numArgs; // Number of arguments
unsigned short flags; // MethodFlags (const/static/etc...)
Index ret; // Index into types for the return type
Index method; // Passed to Class.classFn, to call method
} Method;
/**
* One MethodMap entry maps the munged method prototype
* to the Method entry.
*
* The munging works this way:
* $ is a plain scalar
* # is an object
* ? is a non-scalar (reference to array or hash, undef)
*
* e.g. QApplication(int &, char **) becomes QApplication$?
*/
typedef struct MethodMap {
Index classId; // Index into classes
Index name; // Index into methodNames; munged name
Index method; // Index into methods
} MethodMap;
typedef enum TypeFlags {
// The first 4 bits indicate the TypeId value, i.e. which field
// of the StackItem union is used.
tf_elem = 0x0F,
// Always only one of the next three flags should be set
tf_stack = 0x10, // Stored on the stack, 'type'
tf_ptr = 0x20, // Pointer, 'type*'
tf_ref = 0x30, // Reference, 'type&'
// Can | whatever ones of these apply
tf_const = 0x40 // const argument
} TypeFlags;
/**
* One Type entry is one argument type needed by a method.
* Type entries are shared, there is only one entry for "int" etc.
*/
typedef struct Type {
const char *name; // Stringified type name
Index classId; // Index into classes. -1 for none
unsigned short flags; // TypeFlags
} Type;
typedef enum TypeId {
t_voidp,
t_bool,
t_char,
t_uchar,
t_short,
t_ushort,
t_int,
t_uint,
t_long,
t_ulong,
t_float,
t_double,
t_enum,
t_class,
t_last // number of pre-defined types
} TypeId;
/*
* Opaque Smoke and SmokeBinding structs
*/
typedef struct CSmoke {
void * smoke;
} CSmoke;
/**
* Describe one index in a given module.
*/
typedef struct CModuleIndex {
CSmoke smoke;
Index index;
} CModuleIndex;
/*
* CSmoke accessors
*/
Method * Smoke_methods(CSmoke smoke);
Index Smoke_numMethods(CSmoke smoke);
const char ** Smoke_methodNames(CSmoke smoke);
Index Smoke_numMethodNames(CSmoke smoke);
MethodMap * Smoke_methodMaps(CSmoke smoke);
Index Smoke_numMethodMaps(CSmoke smoke);
Index * Smoke_argumentList(CSmoke smoke);
Type * Smoke_types(CSmoke smoke);
Index Smoke_numTypes(CSmoke smoke);
Class *Smoke_classes(CSmoke smoke);
Index Smoke_numClasses(CSmoke smoke);
Index * Smoke_inheritanceList(CSmoke smoke);
Index * Smoke_ambiguousMethodList(CSmoke smoke);
CastFn Smoke_castFn(CSmoke smoke);
/*
* Smoke Methods
*/
const char * Smoke_moduleName(CSmoke smoke);
void * Smoke_castM(CSmoke smoke, void *ptr, const CModuleIndex from, const CModuleIndex to);
void * Smoke_cast(CSmoke smoke, void *ptr, Index from, Index to);
const char * Smoke_className(CSmoke smoke, Index classId);
int Smoke_leg(CSmoke smoke, Index a, Index b);
Index Smoke_idType(CSmoke smoke, const char *t);
CModuleIndex Smoke_idClass(CSmoke smoke, const char *c, cbool external);
CModuleIndex Smoke_idMethodName(CSmoke smoke, const char *m);
CModuleIndex Smoke_findMethodName(CSmoke smoke, const char *c, const char *m);
CModuleIndex Smoke_idMethod(CSmoke smoke, Index c, Index name);
CModuleIndex Smoke_findMethodM(CSmoke smoke, CModuleIndex c, CModuleIndex name);
CModuleIndex Smoke_findMethod(CSmoke smoke, const char *c, const char *name);
/*
* Static functions
*/
CModuleIndex findClass(const char *c);
cbool isDerivedFromM(const CModuleIndex classId, const CModuleIndex baseClassId);
cbool isDerivedFromI(CSmoke smoke, Index classId, CSmoke baseSmoke, Index baseId);
cbool isDerivedFrom(const char *className, const char *baseClassName);
/*
* SmokeBinding
*/
typedef struct CSmokeBinding {
void * binding;
} CSmokeBinding;
typedef void (*SmokeBinding_DeletedFn)(CSmokeBinding, Index, void *);
typedef cbool (*SmokeBinding_CallMethodFn)(CSmokeBinding, Index, void*, Stack, cbool);
typedef char* (*SmokeBinding_ClassNameFn)(CSmokeBinding, Index);
CSmokeBinding SmokeBinding_new(CSmoke smoke,
SmokeBinding_DeletedFn deleteFn,
SmokeBinding_CallMethodFn callMethodFn,
SmokeBinding_ClassNameFn classNameFn);
void SmokeBinding_delete(CSmokeBinding binding);
char * SmokeBinding_className(CSmokeBinding, Index);
CSmoke CSmoke_FromBinding(CSmokeBinding binding);
// END API
#ifdef __cplusplus
}
#endif
#endif