]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/data.h
Olaf Flebbe:
[simgear.git] / simgear / nasal / data.h
1 #ifndef _DATA_H
2 #define _DATA_H
3
4 #include "nasal.h"
5
6 enum { T_STR, T_VEC, T_HASH, T_CODE, T_FUNC, T_CCODE, T_GHOST,
7        NUM_NASAL_TYPES }; // V. important that this come last!
8
9 #define IS_REF(r) ((r).ref.reftag == NASAL_REFTAG)
10 #define IS_NUM(r) ((r).ref.reftag != NASAL_REFTAG)
11 #define IS_OBJ(r) (IS_REF((r)) && (r).ref.ptr.obj != 0)
12 //#define IS_OBJ(r) (IS_REF((r)) && (r).ref.ptr.obj != 0 && (((r).ref.ptr.obj->type == 123) ? *(int*)0 : 1))
13 #define IS_NIL(r) (IS_REF((r)) && (r).ref.ptr.obj == 0)
14 #define IS_STR(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_STR)
15 #define IS_VEC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_VEC)
16 #define IS_HASH(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_HASH)
17 #define IS_CODE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CODE)
18 #define IS_FUNC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_FUNC)
19 #define IS_CCODE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CCODE)
20 #define IS_GHOST(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_GHOST)
21 #define IS_CONTAINER(r) (IS_VEC(r)||IS_HASH(r))
22 #define IS_SCALAR(r) (IS_NUM((r)) || IS_STR((r)))
23 #define IDENTICAL(a, b) (IS_REF(a) && IS_REF(b) \
24                          && a.ref.ptr.obj == b.ref.ptr.obj)
25
26 #define MUTABLE(r) (IS_STR(r) && (r).ref.ptr.str->hashcode == 0)
27
28 // This is a macro instead of a separate struct to allow compilers to
29 // avoid padding.  GCC on x86, at least, will always padd the size of
30 // an embedded struct up to 32 bits.  Doing it this way allows the
31 // implementing objects to pack in 16 bits worth of data "for free".
32 #define GC_HEADER \
33     unsigned char mark; \
34     unsigned char type
35
36 struct naObj {
37     GC_HEADER;
38 };
39
40 struct naStr {
41     GC_HEADER;
42     int len;
43     unsigned char* data;
44     unsigned int hashcode;
45 };
46
47 struct VecRec {
48     int size;
49     int alloced;
50     naRef array[];
51 };
52
53 struct naVec {
54     GC_HEADER;
55     struct VecRec* rec;
56 };
57
58 struct HashNode {
59     naRef key;
60     naRef val;
61     struct HashNode* next;
62 };
63
64 struct HashRec {
65     int size;
66     int dels;
67     int lgalloced;
68     struct HashNode* nodes;
69     struct HashNode* table[];
70 };
71
72 struct naHash {
73     GC_HEADER;
74     struct HashRec* rec;
75 };
76
77 struct naCode {
78     GC_HEADER;
79     unsigned char nArgs;
80     unsigned char nOptArgs;
81     unsigned char needArgVector;
82     unsigned short nConstants;
83     unsigned short nLines;
84     unsigned short codesz;
85     unsigned short* byteCode;
86     naRef* constants;
87     int* argSyms; // indices into constants
88     int* optArgSyms;
89     int* optArgVals;
90     unsigned short* lineIps; // pairs of {ip, line}
91     naRef srcFile;
92     naRef restArgSym; // The "..." vector name, defaults to "arg"
93 };
94
95 struct naFunc {
96     GC_HEADER;
97     naRef code;
98     naRef namespace;
99     naRef next; // parent closure
100 };
101
102 struct naCCode {
103     GC_HEADER;
104     naCFunction fptr;
105 };
106
107 struct naGhost {
108     GC_HEADER;
109     naGhostType* gtype;
110     void* ptr;
111 };
112
113 struct naPool {
114     int           type;
115     int           elemsz;
116     struct Block* blocks;
117     void**   free0; // pointer to the alloced buffer
118     int     freesz; // size of the alloced buffer
119     void**    free; // current "free frame"
120     int      nfree; // down-counting index within the free frame
121     int    freetop; // curr. top of the free list
122 };
123
124 void naFree(void* m);
125 void* naAlloc(int n);
126 void* naRealloc(void* buf, int sz);
127 void naBZero(void* m, int n);
128
129 int naTypeSize(int type);
130 naRef naObj(int type, struct naObj* o);
131 naRef naNew(naContext c, int type);
132 naRef naNewCode(naContext c);
133
134 int naStr_equal(naRef s1, naRef s2);
135 naRef naStr_fromnum(naRef dest, double num);
136 int naStr_numeric(naRef str);
137 int naStr_parsenum(char* str, int len, double* result);
138 int naStr_tonum(naRef str, double* out);
139 naRef naStr_buf(naRef str, int len);
140
141 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
142 int naHash_sym(struct naHash* h, struct naStr* sym, naRef* out);
143 void naHash_newsym(struct naHash* h, naRef* sym, naRef* val);
144
145 void naGC_init(struct naPool* p, int type);
146 struct naObj** naGC_get(struct naPool* p, int n, int* nout);
147 void naGC_swapfree(void** target, void* val);
148 void naGC_freedead();
149
150 void naStr_gcclean(struct naStr* s);
151 void naVec_gcclean(struct naVec* s);
152 void naHash_gcclean(struct naHash* s);
153
154 #endif // _DATA_H