/*
 * 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.
 */
/*
 *  (C) Copyright 2008, Fuji Xerox Co.,Ltd.  All rights reserved.
 * Copyright protection claimed includes all forms and matters of
 * copyrightable material and information now allowed by statutory or
 * judicial law or hereafter granted, including without limitation, material
 * generated from the software programs which are displayed on the screen
 * such as icons, screen display looks, etc.
 */

#ifdef PERSIST

#ifndef _JS_PERSIST_H_
#define _JS_PERSIST_H_

#include "js-runtime.h"
#include "js-context.h"
#include "js-binaryformat.h"
#include "js-hashtable.h"

/* Classes */
class JSPersist {
    public:
	typedef JSHashtable<int, jsString_t, JSContext::ZoneAllocator>
		_StringHashtable;
	class StringHashtable: public _StringHashtable {
    	    protected:
		static  size_t hashCode(jsString_t key) {
	    	    const int M = 31;	// MULTIPLIER
	    	    const int maxLength = 8;
	    	    int value;

	    	    value = 0;
	    	    for (int i = 0; i < maxLength && key[i]; i++) {
			value = value * M + key[i] & 0xff;
	    	    }
	    	    return value;
		}
		static  int cmpKey(jsString_t key1, jsString_t key2) {
		    return strcmp(key1, key2);
		}
		static void dupEntry(StringHashtable::HashtableEntry *dst,
				StringHashtable::HashtableEntry *src,
				JSContext::ZoneAllocator *allocator) {
		    *dst = *src;
		}
		static void destroyEntry(
				StringHashtable::HashtableEntry *tabEntry,
				JSContext::ZoneAllocator *allocator) {
		    allocator->deallocate(tabEntry);
		}
 
	    public:
		StringHashtable(JSContext *cx)
			: _StringHashtable(hashCode, cmpKey, dupEntry,
					destroyEntry, &cx->allocator) {
		}
		virtual ~StringHashtable() {
		}
	} ;
	typedef JSHashtable<int, double, JSContext::ZoneAllocator>
		_NumberHashtable;
	class NumberHashtable: public _NumberHashtable {
    	    protected:
		static  size_t hashCode(double key) {
	    	    return ( size_t ) key;
		}
		static  int cmpKey(double key1, double key2) {
			if ( key1 == key2 ) return 0;
			if ( key1 < key2 ) return -1;
			return 1;
		}
		static void dupEntry(NumberHashtable::HashtableEntry *dst,
				NumberHashtable::HashtableEntry *src,
				JSContext::ZoneAllocator *allocator) {
		    *dst = *src;
		}
		static void destroyEntry(
				NumberHashtable::HashtableEntry *tabEntry,
				JSContext::ZoneAllocator *allocator) {
		    allocator->deallocate(tabEntry);
		}
 
	    public:
		NumberHashtable(JSContext *cx)
			: _NumberHashtable(hashCode, cmpKey, dupEntry,
					destroyEntry, &cx->allocator) {
		}
		virtual ~NumberHashtable() {
		}
	} ;
	class StringConstants {
	    public:
	  	int		start;
	    	StringHashtable	hashtable;

	    public:
		StringConstants( JSContext* cx )
		    : start( 0 ), hashtable( cx ) {
		}

    		int addString( jsString_t string );
    		int getString( jsString_t string );
	};
	class NumberConstants {
	    public:
	  	int		start;
	    	NumberHashtable	hashtable;

	    public:
		NumberConstants( JSContext* cx )
		    : start( 0 ), hashtable( cx ) {
		}

    		int addNumber( double number );
    		int getNumber( double number );
	};

    public:
    	static uint16_t
		    normalize( uint16_t v );
    	static uint32_t
		    normalize( uint32_t v );
    	static double
		    normalize( double v );
    	static uint16_t
		    localize( uint16_t v );
    	static uint32_t
		    localize( uint32_t v );
    	static double
		    localize( double v );
} ;

/* Inlines */
inline int
JSPersist::StringConstants::getString( jsString_t string ) 
{
    return hashtable.get( string );
}
inline int
JSPersist::NumberConstants::getNumber( double number ) 
{
    return hashtable.get( number );
}
inline uint16_t
JSPersist::normalize( uint16_t v )
{
#ifdef LBFIRST
    return ( ( v << 8 ) | ( v >> 8 ) );
#else
    return v;
#endif
}
inline uint32_t
JSPersist::normalize( uint32_t v )
{
#ifdef LBFIRST
    return (
        ( v << 24 )
        | ( ( v << 8 ) & 0x00ff0000 )
        | ( ( v >> 8 ) & 0x0000ff00 )
        | ( v >> 24 )
    ) ;
#else
    return v;
#endif
}
inline double
JSPersist::normalize( double v )
{
    return v;
}
inline uint16_t
JSPersist::localize( uint16_t v )
{
#ifdef LBFIRST
    return ( ( v << 8 ) | ( v >> 8 ) );
#else
    return v;
#endif
}
inline uint32_t
JSPersist::localize( uint32_t v )
{
#ifdef LBFIRST
    return (
        ( v << 24 )
        | ( ( v << 8 ) & 0x00ff0000 )
        | ( ( v >> 8 ) & 0x0000ff00 )
        | ( v >> 24 )
    ) ;
#else
    return v;
#endif
}
inline double
JSPersist::localize( double v )
{
    return v;
}

#endif /* _JS_PERSIST_H_ */

#endif /* PERSIST */

