/*
 * 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 */
#ifdef VX
#include <string.h>
#endif
#include "js-utils.h"
#include "js-arguments.h"

/* Internal data */
const jsString_t 	JSArguments::className = "Arguments";

/* Procedures */
jsBool_t
JSArguments::isInstanceOf(const jsString_t className)
{
    if (className == JSArguments::className) return TRUE;
    return JSScriptableObject::isInstanceOf(className);
}
jsString_t
JSArguments::getClassName(JSContext *cx)
{
    return className;
}
jsBool_t
JSArguments::has(JSContext *cx, jsString_t name, JSScriptable *start)
{
    return (
	(hasCaller && strcmp(name, "caller") == 0)
	|| JSScriptableObject::has(cx, name, start)
    ) ;
}
jsBool_t
JSArguments::has(JSContext *cx, int index, JSScriptable *start)
{
    int numArgs;

    (void) activation->getOriginalArguments(&numArgs);
    return (
	(0 <= index && index < numArgs)
	|| JSScriptableObject::has(cx, index, start)
    ) ;
}
jsBool_t
JSArguments::get(JSContext *cx, jsString_t name, JSScriptable *start,
		jsVal_t *value)
{
    if (hasCaller && strcmp(name, "caller") == 0) {
        JSNativeCall *caller = activation->getCaller();
	int numArgs;

        if (caller == NULL || caller->getOriginalArguments(&numArgs) == NULL) {
	    *value = JS_VAL_NULL;
            return TRUE;
 	}
        return caller->get(cx, "arguments", caller, value);
    }
    return JSScriptableObject::get(cx, name, start, value);
}
jsBool_t
JSArguments::get(JSContext *cx, int index, JSScriptable *start, jsVal_t *value)
{
    int numArgs;
    jsVal_t *args = activation->getOriginalArguments(&numArgs);

    if (0 <= index && index < numArgs) {
        JSNativeFunction *f = activation->getFunctionObject();

        if (index < f->argCount) {
            return activation->get(cx, f->names[index + 1], activation, value);
	}
	*value = args[index];
        return TRUE;
    }
    return JSScriptableObject::get(cx, index, start, value);
}
void
JSArguments::put(JSContext *cx, jsString_t name, JSScriptable *start,
		jsVal_t value)
{
   if (strcmp(name, "caller") == 0) {
       // Set "hasCaller" to false so that we won't look up a
       // computed value.    
       hasCaller = FALSE;
    }
    JSScriptableObject::put(cx, name, start, value);
}
void
JSArguments::put(JSContext *cx, int index, JSScriptable *start, jsVal_t value)
{
    int numArgs;
    jsVal_t *args = activation->getOriginalArguments(&numArgs);

    if (0 <= index && index < numArgs) {
        JSNativeFunction *f = activation->getFunctionObject();

        if (index < f->argCount) {
            activation->put(cx, f->names[index + 1], activation, value);
        } else {
            args[index] = value;
	}
        return;
    }
    JSScriptableObject::put(cx, index, start, value);
}
void
JSArguments::del(JSContext *cx, jsString_t name)
{
    if (strcmp(name, "caller") == 0) {
        hasCaller = FALSE;
    }
    JSScriptableObject::del(cx, name);
}
void
JSArguments::mark(JSContext *cx)
{
    JSScriptableObject::mark(cx);
    activation->mark(cx);
}

JSArguments::JSArguments(JSContext *cx, JSNativeCall *activation)
{
    this->activation = activation;
 
    JSScriptable *parent = activation->getParentScope(cx);

    setParentScope(cx, parent);
    setPrototype(cx, JSScriptableObject::getObjectPrototype(cx, parent));
 
    JSNativeFunction *callee = activation->getFunctionObject();
    int numArgs;

    (void) activation->getOriginalArguments(&numArgs);
    defineProperty(cx, "length",
		JS_DOUBLE_TO_VAL(JS_NewGCDouble(cx, (double) numArgs)),
		JS_PA_DontEnum);
    defineProperty(cx, "callee", JS_OBJECT_TO_VAL(callee), JS_PA_DontEnum);
 
    short version = callee->version;

    hasCaller = (version <= JSContext::VERSION_1_3
		 && version != JSContext::VERSION_DEFAULT);
}

