/*
 * 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.
 */

#if defined( SUNOS5 ) || linux
#include <libgen.h>
#include "ewb-dbg.h"
#include "js-exceptionhandler.h"
#include "js-filereader.h"
#include "js-filerandomwriter.h"
#include "js-api.h"
#include "js-runtime.h"
#include "js-compile.h"

/* Dummy */
int EwbTracePointExt  = 0;
int ewbPrintOn     = 0;
int EwbTracePointEnt  = 0;
int EwbSetTrace  = 0;
int ewbJsErrorDetails    = 0;
int ewbTimeTrace   = 0;

void
EwbSetLog( char *msg )
{
    printf( msg );
}

/* Class data */
const jsString_t	JSCompile::Global::className = "global";

/* Procedures */
int
JSCompile::compile( jsString_t srcFileName, jsString_t dstFileName )
{
    JSContext::enter(cx);

    JSException *ex;

    if ((ex = enterTry(cx)) == NULL) {
	JSFileReader in( srcFileName );
	jsString_t name = JS_NewZoneString( cx, srcFileName );

    	cx->setSourceInfo( NULL, srcFileName, 0 );

        JSScript* script = cx->compileReader(global, &in,
					basename( name ), 1);

	if ( script != NULL ) {
	    JSFileRandomWriter out( dstFileName );

	    int result = cx->saveWriter( global, script, &out );

	    if ( result >= 0 ) {
		JS_DeleteZoneObj( cx, name );

	    	cx->exceptionHandler->exitTry();

    	    	JSContext::exit();
	    	return 0;
	    }
        }
	JS_DeleteZoneObj( cx, name );
	cx->exceptionHandler->exitTry();
    }
    JSContext::exit();
    return -1;
}

int
JSCompile::initialize()
{
    static bool	initialized;	// assume FALSE

    if ( !initialized ) {
    	Initialize_JSInterpreter();
	initialized = TRUE;
    }

    runtime = new (NULL) JSRuntime( heapSize );

    cx = new (runtime) JSContext(runtime);

    JSContext::enter(cx);

    // A bit of shorthand: since Shell extends ScriptableObject,
    // we can make it the global object.
    global = new (cx) Global();
    cx->runtime->addRoot(global);
    
    // Initialize the standard objects (Object, Function, etc.)
    // This must be done before scripts can be executed.
    cx->initStandardObjects(global);

    JSContext::exit();
    return 0;
}
/**
 * Return name of this class, the global object.
 *
 * This method must be implemented in all concrete classes
 * extending ScriptableObject.
 *
 * @see com.netscape.javascript.Scriptable#getClassName
 */
jsString_t
JSCompile::Global::getClassName(JSContext *cx)
{
    return className;
}
JSCompile::JSCompile()
	: runtime( NULL ), cx( NULL )
{
    heapSize = 1024 * 2000;
}
JSCompile::~JSCompile()
{
    if ( cx != NULL ) {
    	JSContext::enter(cx);
    	cx->runtime->removeRoot(global);
	cx->cleanup();
    	JSContext::exit();

	cx->destroy(runtime);
    }
    if ( runtime != NULL ) delete runtime;
}

int
compile( jsString_t srcFileName, jsString_t dstFileName )
{
    JSCompile compile;

    int result = compile.initialize();

    if ( result < 0 ) {
	return -1;
    }
    result = compile.compile( srcFileName, dstFileName );
    if ( result < 0 ) {
	return -1;
    }
    return 0;
}
#ifdef COMPILER
int
main(int argc, char **argv)
{
    if ( argc < 3 ) {
	printf( "Usage: compile source-file output-file\n" );
	exit( 1 );
    }
    int result = compile( argv[ 1 ], argv[ 2 ] );

    if ( result < 0 ) {
	printf( "error in compile\n" );
	return -1;
    }
    return 0;
}
#endif /* COMPILER */
#endif /* defined( SUNOS5 ) || linux */

