/*
 * The contents of this file are subject to the Netscape Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/NPL/

 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.

 * The Original Code is Rhino code, released
 * May 6, 1999.

 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
 * Rights Reserved.

 * Contributor(s): 

 * Alternatively, the contents of this file may be used under the
 * terms of the GNU Public License (the "GPL"), in which case the
 * provisions of the GPL are applicable instead of those above.
 * If you wish to allow use of your version of this file only
 * under the terms of the GPL and not to allow others to use your
 * version of this file under the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL.  If you do not delete
 * the provisions above, a recipient may use your version of this
 * file under either the NPL or the GPL.
 */
/**
 * This is the enumeration needed by the for..in statement.
 *
 * See ECMA 12.6.3.
 *
 * IdEnumeration maintains a Hashtable to make sure a given
 * id is enumerated only once across multiple objects in a
 * prototype chain.
 *
 * XXX - ECMA delete doesn't hide properties in the prototype,
 * but js/ref does. This means that the js/ref for..in can
 * avoid maintaining a hash table and instead perform lookups
 * to see if a given property has already been enumerated.
 *
 */

#ifndef _JS_ID_ENUMERATION_H_
#define _JS_ID_ENUMERATION_H_

#include <assert.h>
#include "js-types.h"
#include "js-zonenew.h"
#include "js-context.h"
#include "js-scriptable.h"
#include "js-hashtable.h"
#include "js-runtime.h"
#include "js-utils.h"
#include "js-enumeration.h"

/* Classes */
class JSIdEnumeration: public JSZoneNew, public JSEnumeration<jsVal_t> {
    protected:
	typedef JSHashtable<jsVal_t, jsVal_t, JSContext::ZoneAllocator>
		_JSIdEnumHashtable;
	class JSIdEnumHashtable: public _JSIdEnumHashtable {
	    protected:
		static size_t hashCode(jsVal_t key) {
		    return (size_t) key;
		}
		static int cmpKey(jsVal_t key1, jsVal_t key2) {
		    if (JS_VAL_IS_NUMBER(key1) && JS_VAL_IS_NUMBER(key2)) {
			int i1 = (int) JS_VAL_TO_NUMBER(key1);
			int i2 = (int) JS_VAL_TO_NUMBER(key2);

			return (i1 - i2);
		    } else if (JS_VAL_IS_STRING(key1)
				&& JS_VAL_IS_STRING(key2)) {
			jsString_t s1 = JS_VAL_TO_STRING(key1);
			jsString_t s2 = JS_VAL_TO_STRING(key2);

			return strcmp(s1, s2);
		    }
		    return -1;
		}
		static void dupEntry(_JSIdEnumHashtable::HashtableEntry *dst,
				_JSIdEnumHashtable::HashtableEntry *src,
				JSContext::ZoneAllocator *allocator) {
		    *dst = *src;
		}
		static void destroyEntry(_JSIdEnumHashtable::HashtableEntry
				*tabEntry,
				JSContext::ZoneAllocator *allocator) {
			allocator->deallocate(tabEntry);
		}

	    public:
		JSIdEnumHashtable(JSContext *cx, int initialCapacity)
			: _JSIdEnumHashtable(hashCode, cmpKey, dupEntry,
				destroyEntry, &cx->allocator,
				initialCapacity) {
		}
		void  operator delete(void *ptr) {
			assert(0);
		}
	} ;

    public:
	JSContext	*cx;

	jsVal_t		next;
	JSScriptable	*obj;
	int		index;
	jsVal_t		*array;
	int		arrayLength;
	JSIdEnumHashtable
			used;

    protected:
	void	changeObject(JSContext *cx, JSScriptable *m);
	jsVal_t getNext(JSContext *cx);

    public:
	JSIdEnumeration(JSContext *cx, JSScriptable *m);
	virtual ~JSIdEnumeration();

	void	destroy(JSContext *cx);
 	jsBool_t
		hasMoreElements();
	jsVal_t nextElement();

	/* gc methods */
	void	mark(JSContext *cx);
};

#endif /* _JS_ID_ENUMERATION_H_ */
