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

/* Class data */
const jsString_t	JSEcmaError::className = "EcmaError";

/* Procedures */
jsString_t
JSEcmaError::toString(JSContext *cx) // 2004.6.2 ichino
{
    jsString_t error = errorObject->toString(cx);
    jsString_t string;
    size_t length;

    if ( sourceName != NULL ) {
        if ( error != NULL ) {
            length = strlen( error )
                        + TableSize( " (" ) - 1
                        + strlen( sourceName )
                        + TableSize( "; line " ) - 1
                        + 8
                        + TableSize( ")\n" ) - 1
                        + 1;
            string = ( jsString_t ) JS_NewZoneObj( cx,
                                        length * sizeof ( *string ) );
            sprintf( string, "%s(%s; line %8d)\n", error,
                sourceName, lineNumber );
            JS_DeleteZoneObj( cx, error );
        } else {
            length = TableSize( " (" ) - 1
                        + strlen( sourceName )
                        + TableSize( "; line " ) - 1
                        + 8
                        + TableSize( ")\n" ) - 1
                        + 1 ;
            string = ( jsString_t ) JS_NewZoneObj( cx,
                                        length * sizeof ( *string ) );
            sprintf( string, "(%s; line %8d)\n", this->sourceName,
                this->lineNumber );
        }
        return string;
    } else { //2003.11.27 ichino
        if(error != NULL){
            length = strlen( error )
                        + TableSize( "\n" ) - 1
                        + 1;
            string  = ( jsString_t ) JS_NewZoneObj(cx,
                                        length * sizeof ( *string ) );
            sprintf( string, "%s\n", error );
            JS_DeleteZoneObj( cx, error );
            return string;
        } else {
            length = TableSize( " \n" ) - 1
                        + 1;
            string = ( jsString_t ) JS_NewZoneObj( cx,
                                        length * sizeof ( *string ) );
            sprintf( string, " \n" );
            JS_DeleteZoneObj( cx, error );
            return string;
        }
    }
}

/* 2004.6.2 ichino */
jsString_t
JSEcmaError::toStringWithSource(JSContext *cx)
{
   jsString_t error = errorObject->toString(cx);
    jsString_t interpreterSourceFile = cx->interpreterSourceFile;
    jsString_t source  = cx->source;

    if ( interpreterSourceFile == NULL ) {
        interpreterSourceFile = " ";
    }
    if ( source == NULL ) {
        source = " ";
    }
    int length = strlen( error )
                + TableSize( " (" ) - 1
                + strlen( interpreterSourceFile )
                + TableSize( "; line " ) - 1
                + 8
                + TableSize( ")\n" ) - 1
                + 1;

    if ( source != NULL ) length += strlen( source );

    jsString_t string
        = ( jsString_t ) JS_NewZoneObj( cx, length * sizeof ( *string ) );

    sprintf( string, "%s(%s; line %8d) %s\n", error, interpreterSourceFile,
        cx->interpreterLine, source );
    JS_DeleteZoneObj( cx, error );

    return string;
}
/**
 * ECMA edition 3 defines the following
 * errors: EvalError, RangeError, ReferenceError,
 * SyntaxError, TypeError, and URIError. Additional error names
 * may be added in the future.
 *   
 * See ECMA edition 3, 15.11.7.9.
 */  
jsString_t
JSEcmaError::getName(JSContext *cx)
{
    return errorObject->getName(cx);
}
/**
 * See ECMA edition 3, 15.11.7.10.
 */
jsString_t
JSEcmaError::getMessage(JSContext *cx)
{
    return errorObject->getMessage(cx);
}
jsBool_t
JSEcmaError::isInstanceOf(const jsString_t className)
{
    if (className == JSEcmaError::className) return TRUE;
    return JSRuntimeException::isInstanceOf(className);
}
void
JSEcmaError::mark(JSContext *cx)
{
    JSGCHeap *gcHeap = &cx->runtime->gcHeap;

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

    JSRuntimeException::mark(cx);
    errorObject->mark(cx);
}

JSEcmaError::JSEcmaError(JSNativeError *nativeError,
			jsString_t sourceName, int lineNumber, int columnNumber,
			jsString_t lineSource)
		: JSRuntimeException("EcmaError")
{
    errorObject = nativeError;
    this->sourceName = sourceName;
    this->lineNumber = lineNumber;
    this->columnNumber = columnNumber;
    this->lineSource = lineSource;
}

