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