void naFree(void* m) { free(m); }
void* naAlloc(int n) { return malloc(n); }
+void* naRealloc(void* b, int n) { return realloc(b, n); }
void naBZero(void* m, int n) { memset(m, 0, n); }
+void naTempSave(naContext c, naRef r)
+{
+ int i;
+ if(!IS_OBJ(r)) return;
+ if(c->ntemps >= c->tempsz) {
+ struct naObj** newtemps;
+ c->tempsz *= 2;
+ newtemps = naAlloc(c->tempsz * sizeof(struct naObj*));
+ for(i=0; i<c->ntemps; i++)
+ newtemps[i] = c->temps[i];
+ naFree(c->temps);
+ c->temps = newtemps;
+ }
+ c->temps[c->ntemps++] = r.ref.ptr.obj;
+}
+
naRef naObj(int type, struct naObj* o)
{
naRef r;
naRef naNew(struct Context* c, int type)
{
- naRef result = naObj(type, naGC_get(&(c->pools[type])));
- naVec_append(c->temps, result);
+ naRef result;
+ if(c->nfree[type] == 0)
+ c->free[type] = naGC_get(&globals->pools[type],
+ OBJ_CACHE_SZ, &c->nfree[type]);
+ result = naObj(type, c->free[type][--c->nfree[type]]);
+ naTempSave(c, result);
return result;
}
naRef s = naNew(c, T_STR);
s.ref.ptr.str->len = 0;
s.ref.ptr.str->data = 0;
+ s.ref.ptr.str->hashcode = 0;
return s;
}
naRef naNewVector(struct Context* c)
{
naRef r = naNew(c, T_VEC);
- naVec_init(r);
+ r.ref.ptr.vec->rec = 0;
return r;
}
naRef naNewHash(struct Context* c)
{
naRef r = naNew(c, T_HASH);
- naHash_init(r);
+ r.ref.ptr.hash->rec = 0;
return r;
}
{
naRef func = naNew(c, T_FUNC);
func.ref.ptr.func->code = code;
- func.ref.ptr.func->closure = naNil();
+ func.ref.ptr.func->namespace = naNil();
+ func.ref.ptr.func->next = naNil();
return func;
}
-naRef naNewClosure(struct Context* c, naRef namespace, naRef next)
-{
- naRef closure = naNew(c, T_CLOSURE);
- closure.ref.ptr.closure->namespace = namespace;
- closure.ref.ptr.closure->next = next;
- return closure;
-}
-
naRef naNewGhost(naContext c, naGhostType* type, void* ptr)
{
naRef ghost = naNew(c, T_GHOST);
return na == nb ? 1 : 0;
}
+int naStrEqual(naRef a, naRef b)
+{
+ int i;
+ if(!(IS_STR(a) && IS_STR(b)))
+ return 0;
+ if(a.ref.ptr.str->len != b.ref.ptr.str->len)
+ return 0;
+ for(i=0; i<a.ref.ptr.str->len; i++)
+ if(a.ref.ptr.str->data[i] != b.ref.ptr.str->data[i])
+ return 0;
+ return 1;
+}
+
int naTypeSize(int type)
{
switch(type) {
case T_HASH: return sizeof(struct naHash);
case T_CODE: return sizeof(struct naCode);
case T_FUNC: return sizeof(struct naFunc);
- case T_CLOSURE: return sizeof(struct naClosure);
case T_CCODE: return sizeof(struct naCCode);
case T_GHOST: return sizeof(struct naGhost);
};
return 0x7fffffff; // Make sure the answer is nonsense :)
}
-int naIsNil(naRef r)
-{
- return IS_NIL(r);
-}
-
-int naIsNum(naRef r)
-{
- return IS_NUM(r);
-}
-
-int naIsString(naRef r)
-{
- return (!IS_NIL(r))&&IS_STR(r);
-}
-
-int naIsScalar(naRef r)
-{
- return IS_SCALAR(r);
-}
-
-int naIsVector(naRef r)
-{
- return (!IS_NIL(r))&&IS_VEC(r);
-}
-
-int naIsHash(naRef r)
-{
- return (!IS_NIL(r))&&IS_HASH(r);
-}
-
-int naIsFunc(naRef r)
-{
- return (!IS_NIL(r))&&IS_FUNC(r);
-}
-
-int naIsCode(naRef r)
-{
- return IS_CODE(r);
-}
-
-int naIsCCode(naRef r)
-{
- return IS_CCODE(r);
-}
-
-int naIsGhost(naRef r)
-{
- return IS_GHOST(r);
-}
+int naIsNil(naRef r) { return IS_NIL(r); }
+int naIsNum(naRef r) { return IS_NUM(r); }
+int naIsString(naRef r) { return IS_STR(r); }
+int naIsScalar(naRef r) { return IS_SCALAR(r); }
+int naIsVector(naRef r) { return IS_VEC(r); }
+int naIsHash(naRef r) { return IS_HASH(r); }
+int naIsFunc(naRef r) { return IS_FUNC(r); }
+int naIsCode(naRef r) { return IS_CODE(r); }
+int naIsCCode(naRef r) { return IS_CCODE(r); }
+int naIsGhost(naRef r) { return IS_GHOST(r); }