/*
 * 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 "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);
}

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