Ejemplo n.º 1
0
 /*
 ** Internal function to manipulate arrays.
 ** Given an array object and a reference value, return the next element
 ** in the hash.
 ** This function pushs the element value and its reference to the stack.
 */
 private static void firstnode(Hash a, int h)
 {
     if (h < nhash(a))
     {
         int i;
         for (i = h; i < nhash(a); i++)
         {
             if (list(a, i) != null)
             {
                 if (tag(list(a, i).get().val) != Type.T_NIL)
                 {
                     lua_pushobject(list(a, i).get().@ref);
                     lua_pushobject(list(a, i).get().val);
                     return;
                 }
                 else
                 {
                     NodeRef next = list(a, i).get().next;
                     while (next != null && tag(next.get().val) == Type.T_NIL)
                     {
                         next = next.get().next;
                     }
                     if (next != null)
                     {
                         lua_pushobject(next.get().@ref);
                         lua_pushobject(next.get().val);
                         return;
                     }
                 }
             }
         }
     }
     lua_pushnil();
     lua_pushnil();
 }
Ejemplo n.º 2
0
 private static void freelist(NodeRef n)
 {
     while (n != null)
     {
         NodeRef next = NodeRef.assign(n.get().next);
         free(n.get());
         n = next;
     }
 }
Ejemplo n.º 3
0
        private static NodeRef present(Hash t, Object_ @ref, int h)
        {
            NodeRef n = null, p = null;

            if (tag(@ref) == Type.T_NUMBER)
            {
                for (p = null, n = list(t, h); n != null; p = n, n = NodeRef.assign(n.get().next))
                {
                    if (ref_tag(n.get()) == Type.T_NUMBER && nvalue(@ref) == ref_nvalue(n.get()))
                    {
                        break;
                    }
                }
            }
            else if (tag(@ref) == Type.T_STRING)
            {
                for (p = null, n = list(t, h); n != null; p = n, n = NodeRef.assign(n.get().next))
                {
//					if (ref_tag(n.get()) == Type.T_STRING)
//					{
//						Console.WriteLine("=========================" + svalue(@ref).ToString() + ", " + ref_svalue(n.get()));
//					}
                    if (ref_tag(n.get()) == Type.T_STRING && streq(svalue(@ref), ref_svalue(n.get())))
                    {
//						Console.WriteLine("=========================");
                        break;
                    }
                }
            }
            if (n == null)                                      /* name not present */
            {
                return(null);
            }
#if false
            if (p != null)                                      /* name present but not first */
            {
                p.next = n.next;                                /* move-to-front self-organization */
                n.next = list(t, h);
                list(t, h, n);
            }
#endif
            return(n);
        }
Ejemplo n.º 4
0
        /*
        ** Re-hash
        */
        private static void rehash(Hash t)
        {
            Word    i;
            Word    nold = nhash(t);
            NodeRef vold = nodevector(t);

            nhash(t, redimension(nhash(t)));
            nodevector(t, hashnodecreate(nhash(t)));
            for (i = 0; i < nold; i++)
            {
                Node n = vold.get(i);
                if (tag(ref_(n)) != lua_Type.LUA_T_NIL && tag(val_(n)) != lua_Type.LUA_T_NIL)
                {
                    node(t, present(t, ref_(n))).set(n);                      /* copy old node to new hahs */
                }
            }
            luaI_free_NodeRef(vold);
        }
Ejemplo n.º 5
0
        public static void lua_next()
        {
            Hash    a;
            Object_ o = lua_getparam(1);
            Object_ r = lua_getparam(2);

            if (o == null || r == null)
            {
                lua_error("too few arguments to function `next'"); return;
            }
            if (lua_getparam(3) != null)
            {
                lua_error("too many arguments to function `next'"); return;
            }
            if (tag(o) != Type.T_ARRAY)
            {
                lua_error("first argument of function `next' is not a table"); return;
            }
            a = avalue(o);
            if (tag(r) == Type.T_NIL)
            {
                firstnode(a, 0);
                return;
            }
            else
            {
                int h = head(a, r);
                if (h >= 0)
                {
                    NodeRef n = list(a, h);
                    while (n != null)
                    {
                        if (n.get()[email protected](r))
                        {
                            if (n.get().next == null)
                            {
                                firstnode(a, h + 1);
                                return;
                            }
                            else if (tag(n.get().next.get().val) != Type.T_NIL)
                            {
                                lua_pushobject(n.get().next.get().@ref);
                                lua_pushobject(n.get().next.get().val);
                                return;
                            }
                            else
                            {
                                NodeRef next = NodeRef.assign(n.get().next.get().next);
                                while (next != null && tag(next.get().val) == Type.T_NIL)
                                {
                                    next = NodeRef.assign(next.get().next);
                                }
                                if (next == null)
                                {
                                    firstnode(a, h + 1);
                                    return;
                                }
                                else
                                {
                                    lua_pushobject(next.get().@ref);
                                    lua_pushobject(next.get().val);
                                }
                                return;
                            }
                        }
                        n = NodeRef.assign(n.get().next);
                    }
                    if (n == null)
                    {
                        lua_error("error in function 'next': reference not found");
                    }
                }
            }
        }