]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/misc.c
f74ca95e22eb4f8709c1f8d2dc2b66e29035ad0e
[simgear.git] / simgear / nasal / misc.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "nasal.h"
6 #include "code.h"
7
8 void naFree(void* m) { free(m); }
9 void* naAlloc(int n) { return malloc(n); }
10 void naBZero(void* m, int n) { memset(m, 0, n); }
11
12 naRef naObj(int type, struct naObj* o)
13 {
14     naRef r;
15     r.ref.reftag = NASAL_REFTAG;
16     r.ref.ptr.obj = o;
17     o->type = type;
18     return r;
19 }
20
21 int naTrue(naRef r)
22 {
23     if(IS_NIL(r)) return 0;
24     if(IS_NUM(r)) return r.num != 0;
25     if(IS_STR(r)) return 1;
26     return 0;
27 }
28
29 naRef naNumValue(naRef n)
30 {
31     double d;
32     if(IS_NUM(n)) return n;
33     if(IS_NIL(n)) return naNil();
34     if(IS_STR(n) && naStr_tonum(n, &d))
35         return naNum(d);
36     return naNil();
37 }
38
39 naRef naStringValue(naContext c, naRef r)
40 {
41     if(IS_NIL(r) || IS_STR(r)) return r;
42     if(IS_NUM(r)) {
43         naRef s = naNewString(c);
44         naStr_fromnum(s, r.num);
45         return s;
46     }
47     return naNil();
48 }
49
50 naRef naNew(struct Context* c, int type)
51 {
52     if(c->nfree[type] == 0)
53         c->free[type] = naGC_get(&globals->pools[type],
54                                  OBJ_CACHE_SZ, &c->nfree[type]);
55     naRef result = naObj(type, c->free[type][--c->nfree[type]]);
56     naVec_append(c->temps, result);
57     return result;
58 }
59
60 naRef naNewString(struct Context* c)
61 {
62     naRef s = naNew(c, T_STR);
63     s.ref.ptr.str->len = 0;
64     s.ref.ptr.str->data = 0;
65     s.ref.ptr.str->hashcode = 0;
66     return s;
67 }
68
69 naRef naNewVector(struct Context* c)
70 {
71     naRef r = naNew(c, T_VEC);
72     r.ref.ptr.vec->rec = 0;
73     return r;
74 }
75
76 naRef naNewHash(struct Context* c)
77 {
78     naRef r = naNew(c, T_HASH);
79     r.ref.ptr.hash->rec = 0;
80     return r;
81 }
82
83 naRef naNewCode(struct Context* c)
84 {
85     return naNew(c, T_CODE);
86 }
87
88 naRef naNewCCode(struct Context* c, naCFunction fptr)
89 {
90     naRef r = naNew(c, T_CCODE);
91     r.ref.ptr.ccode->fptr = fptr;
92     return r;
93 }
94
95 naRef naNewFunc(struct Context* c, naRef code)
96 {
97     naRef func = naNew(c, T_FUNC);
98     func.ref.ptr.func->code = code;
99     func.ref.ptr.func->namespace = naNil();
100     func.ref.ptr.func->next = naNil();
101     return func;
102 }
103
104 naRef naNewGhost(naContext c, naGhostType* type, void* ptr)
105 {
106     naRef ghost = naNew(c, T_GHOST);
107     ghost.ref.ptr.ghost->gtype = type;
108     ghost.ref.ptr.ghost->ptr = ptr;
109     return ghost;
110 }
111
112 naGhostType* naGhost_type(naRef ghost)
113 {
114     if(!IS_GHOST(ghost)) return 0;
115     return ghost.ref.ptr.ghost->gtype;
116 }
117
118 void* naGhost_ptr(naRef ghost)
119 {
120     if(!IS_GHOST(ghost)) return 0;
121     return ghost.ref.ptr.ghost->ptr;
122 }
123
124 naRef naNil()
125 {
126     naRef r;
127     r.ref.reftag = NASAL_REFTAG;
128     r.ref.ptr.obj = 0;
129     return r;
130 }
131
132 naRef naNum(double num)
133 {
134     naRef r;
135     r.ref.reftag = ~NASAL_REFTAG;
136     r.num = num;
137     return r;
138 }
139
140 int naEqual(naRef a, naRef b)
141 {
142     double na=0, nb=0;
143     if(IS_REF(a) && IS_REF(b) && a.ref.ptr.obj == b.ref.ptr.obj)
144         return 1; // Object identity (and nil == nil)
145     if(IS_NIL(a) || IS_NIL(b))
146         return 0;
147     if(IS_NUM(a) && IS_NUM(b) && a.num == b.num)
148         return 1; // Numeric equality
149     if(IS_STR(a) && IS_STR(b) && naStr_equal(a, b))
150         return 1; // String equality
151
152     // Numeric equality after conversion
153     if(IS_NUM(a)) { na = a.num; }
154     else if(!(IS_STR(a) && naStr_tonum(a, &na))) { return 0; }
155
156     if(IS_NUM(b)) { nb = b.num; }
157     else if(!(IS_STR(b) && naStr_tonum(b, &nb))) { return 0; }
158
159     return na == nb ? 1 : 0;
160 }
161
162 int naStrEqual(naRef a, naRef b)
163 {
164     int i;
165     if(!(IS_STR(a) && IS_STR(b)))
166         return 0;
167     if(a.ref.ptr.str->len != b.ref.ptr.str->len)
168         return 0;
169     for(i=0; i<a.ref.ptr.str->len; i++)
170         if(a.ref.ptr.str->data[i] != b.ref.ptr.str->data[i])
171             return 0;
172     return 1;
173 }
174
175 int naTypeSize(int type)
176 {
177     switch(type) {
178     case T_STR: return sizeof(struct naStr);
179     case T_VEC: return sizeof(struct naVec);
180     case T_HASH: return sizeof(struct naHash);
181     case T_CODE: return sizeof(struct naCode);
182     case T_FUNC: return sizeof(struct naFunc);
183     case T_CCODE: return sizeof(struct naCCode);
184     case T_GHOST: return sizeof(struct naGhost);
185     };
186     return 0x7fffffff; // Make sure the answer is nonsense :)
187 }
188
189 int naIsNil(naRef r)
190 {
191     return IS_NIL(r);
192 }
193
194 int naIsNum(naRef r)
195 {
196     return IS_NUM(r);
197 }
198
199 int naIsString(naRef r)
200 {
201     return (!IS_NIL(r))&&IS_STR(r);
202 }
203
204 int naIsScalar(naRef r)
205 {
206     return IS_SCALAR(r);
207 }
208
209 int naIsVector(naRef r)
210 {
211     return (!IS_NIL(r))&&IS_VEC(r);
212 }
213
214 int naIsHash(naRef r)
215 {
216     return (!IS_NIL(r))&&IS_HASH(r);
217 }
218
219 int naIsFunc(naRef r)
220 {
221     return (!IS_NIL(r))&&IS_FUNC(r);
222 }
223
224 int naIsCode(naRef r)
225 {
226     return IS_CODE(r);
227 }
228
229 int naIsCCode(naRef r)
230 {
231     return IS_CCODE(r);
232 }
233
234 int naIsGhost(naRef r)
235 {
236     return IS_GHOST(r);
237 }