
/*******************************************************************************
 * Copyright (c) 2004, 2005 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/
package org.eclipse.birt.core.script.bre;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.script.JavascriptEvalUtil;
import org.mozilla.javascript.BaseFunction;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;

/**
 * 
 */

public class BreUtility
{
	static Object[] convertToJavaObjects( Object[] args )
	{
		for( int i = 0; i < args.length; i++ )
		{
			args[i] = JavascriptEvalUtil.convertJavascriptValue( args[i] );
		}
		return args;
	}
}

abstract class Function_temp extends BaseFunction implements Function
{
	protected int length;
	protected boolean isFixed;
	
	public Object call( Context cx, Scriptable scope, Scriptable thisObj,
			java.lang.Object[] args )
	{
		if ( args == null || ( isFixed? args.length != length: args.length > length) )
			throw new IllegalArgumentException( "The number of arguement is incorrect." );
		args = BreUtility.convertToJavaObjects( args );
		
		try
		{
			return getValue( args );
		}
		catch ( BirtException e )
		{
			throw new IllegalArgumentException( "The type of arguement is incorrect." );
		}
	}	
	
	protected abstract Object getValue( Object[] args ) throws BirtException;
}
