]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/data.h
Clamp pitch values rather than just dumping an error message.
[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 pointer to a CLOSURE chain of
9 // namespaces.
10 enum { T_STR, T_VEC, T_HASH, T_CODE, T_CLOSURE, 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_NIL(r) (IS_REF((r)) && (r).ref.ptr.obj == 0)
17 #define IS_STR(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_STR)
18 #define IS_VEC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_VEC)
19 #define IS_HASH(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_HASH)
20 #define IS_CODE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CODE)
21 #define IS_FUNC(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_FUNC)
22 #define IS_CLOSURE(r) (IS_OBJ((r)) && (r).ref.ptr.obj->type == T_CLOSURE)
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
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 };
45
46 struct naVec {
47     GC_HEADER;
48     int size;
49     int alloced;
50     naRef* array;
51 };
52
53 struct HashNode {
54     naRef key;
55     naRef val;
56     struct HashNode* next;
57 };
58
59 struct naHash {
60     GC_HEADER;
61     int size;
62     int lgalloced;
63     struct HashNode* nodes;
64     struct HashNode** table;
65     int nextnode;
66 };
67
68 struct naCode {
69     GC_HEADER;
70     unsigned char* byteCode;
71     int nBytes;
72     naRef* constants;
73     int nConstants;
74     naRef srcFile;
75 };
76
77 struct naFunc {
78     GC_HEADER;
79     naRef code;
80     naRef closure;
81 };
82
83 struct naClosure {
84     GC_HEADER;
85     naRef namespace;
86     naRef next; // parent closure
87 };
88
89 struct naCCode {
90     GC_HEADER;
91     naCFunction fptr;
92 };
93
94 struct naGhost {
95     GC_HEADER;
96     naGhostType* gtype;
97     void* ptr;
98 };
99
100 struct naPool {
101     int           type;
102     int           elemsz;
103     int           nblocks;
104     struct Block* blocks;
105     int           nfree;   // number of entries in the free array
106     int           freesz;  // size of the free array
107     void**        free;    // pointers to usable elements
108 };
109
110 void naFree(void* m);
111 void* naAlloc(int n);
112 void naBZero(void* m, int n);
113
114 int naTypeSize(int type);
115 void naGarbageCollect();
116 naRef naObj(int type, struct naObj* o);
117 naRef naNew(naContext c, int type);
118 naRef naNewCode(naContext c);
119 naRef naNewClosure(naContext c, naRef namespace, naRef next);
120
121 int naStr_equal(naRef s1, naRef s2);
122 naRef naStr_fromnum(naRef dest, double num);
123 int naStr_numeric(naRef str);
124 int naStr_parsenum(char* str, int len, double* result);
125 int naStr_tonum(naRef str, double* out);
126
127 void naVec_init(naRef vec);
128
129 int naHash_tryset(naRef hash, naRef key, naRef val); // sets if exists
130 void naHash_init(naRef hash);
131
132 void naGC_init(struct naPool* p, int type);
133 struct naObj* naGC_get(struct naPool* p);
134 int naGC_size(struct naPool* p);
135 void naGC_mark(naRef r);
136 void naGC_reap(struct naPool* p);
137
138 void naStr_gcclean(struct naStr* s);
139 void naVec_gcclean(struct naVec* s);
140 void naHash_gcclean(struct naHash* s);
141
142 #endif // _DATA_H