/*
 * 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.
 */
/* Header file */
#include <stdio.h>
#include "js-variabletable.h"

/* Procedures */
#ifndef FASTER
int
JSVariableTable::size()
{
    return itsVariables.size();
}
#endif
int
JSVariableTable::getParameterCount()
{
    return varStart;
}
JSLocalVariable *
JSVariableTable::createLocalVariable(JSContext *cx, jsString_t name,
				jsBool_t isParameter) 
{
    return new (cx) JSLocalVariable(cx, name, isParameter);
}
JSLocalVariable *
JSVariableTable::get(int index)
{
    return (JSLocalVariable *)(itsVariables.elementAt(index));
}
JSLocalVariable *
JSVariableTable::get(jsString_t name)
{
    int vIndex = itsVariableNames.get(name);
    
    if (vIndex != 0) {
        return itsVariables.elementAt(vIndex - 1);
    } else {
        return NULL;
    }
}
int
JSVariableTable::getOrdinal(jsString_t name)
{
    int vIndex = itsVariableNames.get(name);

    if (vIndex != 0) {
        return vIndex - 1;
    } else {
        return -1;
    }
}
jsString_t
JSVariableTable::getName(int index)
{
    return itsVariables.elementAt(index)->getName();
}
void
JSVariableTable::establishIndices()
{
    for (int i = 0; i < itsVariables.size(); i++) {
        JSLocalVariable *lVar = (JSLocalVariable *) (itsVariables.elementAt(i));
        lVar->setIndex(i);
    }
}
void
JSVariableTable::addParameter(JSContext *cx, jsString_t pName)
{
    int pIndex = itsVariableNames.get(pName);

    if (pIndex != 0) {
        JSLocalVariable *p = itsVariables.elementAt(pIndex - 1);

        if (p->isParameter()) {
            jsString_t message
			= JSContext::getMessage(cx, "msg.dup.parms %s", pName);
            JSContext::reportWarning(cx, message, NULL, 0, NULL, 0);
        } else {  // there's a local variable with this name, blow it off
            itsVariables.removeElementAt(pIndex - 1);
        }
    }
    int curIndex = varStart++;
    JSLocalVariable *lVar = createLocalVariable(cx, pName, TRUE);

    itsVariables.insertElementAt(lVar, curIndex);
    itsVariableNames.put(pName, curIndex + 1);
}
void
JSVariableTable::addLocal(JSContext *cx, jsString_t vName)
{
    int vIndex = itsVariableNames.get(vName);

    if (vIndex != 0) {
        JSLocalVariable *v = (JSLocalVariable *)
                        	itsVariables.elementAt(vIndex - 1);
        if (v->isParameter()) {
            // this is o.k. the parameter subsumes the variable def.
        } else {
            return;
        }
    }
    int index = itsVariables.size();
    JSLocalVariable *lVar = createLocalVariable(cx, vName, FALSE);

    itsVariables.addElement(lVar);
    itsVariableNames.put(vName, index + 1);
}

// This should only be called very early in compilation
void
JSVariableTable::removeLocal(JSContext *cx, jsString_t name)
{
    int i = itsVariableNames.get(name);

    if (i != 0) {
        itsVariables.removeElementAt(i - 1);
        itsVariableNames.remove(name);

        VariableNames::HashtableKeyEnumerator e = itsVariableNames.keys();

        while (e.hasMoreElements()) {
            jsString_t k = e.nextElement();
            int v = itsVariableNames.get(k);

            if (v > i) {
                v = v - 1;
	    }
            itsVariableNames.put(k, v);
        }
    }
}
void *
JSVariableTable::operator new(size_t size, void *placement)
{
    JSContext *cx = (JSContext *) placement;
    void *ptr = JS_NewGCNormalObj(cx, size);

    memset(ptr, 0, size);	// gc safe at construct object
    return ptr;
}
void
JSVariableTable::mark(JSContext *cx)
{
    JSGCHeap *gcHeap = &cx->runtime->gcHeap;

    if (gcHeap->isMarkObj(this)) return;

    gcHeap->markObj(this);

    itsVariables.mark(cx);
    for (int i = 0; i < itsVariables.size(); i++) {
        JSLocalVariable *lVar
			= (JSLocalVariable *) (itsVariables.elementAt(i));

	lVar->mark(cx);
    }
    itsVariableNames.mark(cx);
}
#ifdef PERSIST
void
JSVariableTable::presave( JSContext *cx, JSScriptable *scope,
			JSPersist::StringConstants& stringConstants, 
			JSPersist::NumberConstants& numberConstants )
{
    for ( int i = 0; i < size(); ++i ) {
	JSLocalVariable *lVar
		= ( JSLocalVariable* ) ( itsVariables.elementAt( i ) );

	( void ) stringConstants.addString( lVar->getName() );
    }
}
void
JSVariableTable::save( JSContext *cx, JSScriptable *scope, JSRandomWriter* out,
			JSPersist::StringConstants& stringConstants, 
			JSPersist::NumberConstants& numberConstants )
{
    uint16_t val16;

    val16 = JSPersist::normalize( ( uint16_t ) varStart );

    int n = out->write( &val16, 0, sizeof ( val16 ) );

    if ( n < ( int ) sizeof ( val16 ) ) {
	printf( "can't write varStart\n" );
	return;
    }
    int16_t count16;

    count16 = JSPersist::normalize( ( uint16_t ) size() );
    n = out->write( &count16, 0, sizeof ( count16 ) );
    if ( n < ( int ) sizeof ( count16 ) ) {
	printf( "can't write variable size\n" );
	return;
    }
    for ( int i = 0; i < size(); ++i ) {
	JSLocalVariable *lVar
		= ( JSLocalVariable* ) ( itsVariables.elementAt( i ) );
	jsBFVar_t var;

        memset( &var, 0, sizeof ( var ) );
	var.v_name = stringConstants.getString( lVar->getName() );
	var.v_index = itsVariableNames.get( lVar->getName() );
	var.v_isParameter = lVar->isParameter();
#define	set( var, member )	\
		( var )->member = JSPersist::normalize( ( var )->member )
        set( &var, v_name );
        set( &var, v_index );
#undef	set
	n = out->write( &var, 0, sizeof ( var ) );
	if ( n < ( int ) sizeof ( var ) ) {
	    printf( "can't write variable\n" );
	    return;
	}
    }
}
void
JSVariableTable::load( JSContext *cx, JSScriptable *scope, JSRandomReader* in,
			jsString_t* strings, double* numbers )
{
    uint16_t val16;
    int n = in->read( &val16, 0, sizeof ( val16 ) );
   
    if ( n < ( int ) sizeof ( val16 ) ) {
	printf( "can't read varStart\n" );
	return;
    }
    varStart = JSPersist::localize( val16 );

    uint16_t count16;

    n = in->read( &count16, 0, sizeof ( count16 ) );
    if ( n < ( int ) sizeof ( count16 ) ) {
	printf( "can't read var count\n" );
	return;
    }
    count16 = JSPersist::localize( count16 );
    for ( int i = 0; i < count16; ++i ) {
	jsBFVar_t var;

	n = in->read( &var, 0, sizeof ( var ) );
        if ( n < ( int ) sizeof ( var ) ) {
	    printf( "can't read variable\n" );
	    return;
        }
#define	set( var, member )	\
		( var )->member = JSPersist::localize( ( var )->member )
        set( &var, v_name );
        set( &var, v_index );
#undef	set
	JSLocalVariable *lVar
		= createLocalVariable( cx, strings[ var.v_name - 1 ],
				var.v_isParameter );

    	itsVariables.addElement(lVar);
    	itsVariableNames.put( strings[ var.v_name - 1 ], var.v_index );
    }
}
#endif /* PERSIST */

JSVariableTable::JSVariableTable(JSContext *cx)
	// initial capacity must be 0 on WIN32
	: itsVariables(cx, 0), itsVariableNames(cx, 0)
{
    varStart = 0;
}

