/*
 * 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.
 */
/*
 *  JavaScript Interpreter
 *  The JavaScript Interpreter is used for executing JavaScript programs
 *  in EWB that control user events on buttons, restrict inputs on textlines
 *  and so on.
 *
 *  Developmened by: Institute of Artificial Intelligence & Robotics
 *                                       of Xi'an Jiaotong University
 *
 *  Create by:          Tian Wenqiang
 *  Last modified:      2001-11-13 19:40
 */

/*
   JavaScript Interpreter Application Programming Interface definitions
*/

#ifndef _JS_API_H_
#define _JS_API_H_

#include "js-types.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Procedures */
jsBool_t
		JS_IsObject(jsVal_t value);
jsBool_t
		JS_IsInteger(jsVal_t value);
jsBool_t
		JS_IsDouble(jsVal_t value);
jsBool_t
		JS_IsString(jsVal_t value);
jsBool_t
		JS_IsBoolean(jsVal_t value);

/* Convert JS value to a specified type value */
jsBool_t
		JS_ValueToBoolean(jsContext_t context, jsVal_t val);
jsInt_t JS_ValueToInteger(jsContext_t context, jsVal_t val);
jsString_t
		JS_ValueToString(jsContext_t context, jsVal_t val);
jsDouble_t
		JS_ValueToDouble(jsContext_t context, jsVal_t val);
jsObject_t
		JS_ValueToObject(jsContext_t context, jsObject_t scope, jsVal_t val);

/* Convert a specified type value to JS value */
jsVal_t JS_ObjectToValue(jsContext_t context, jsObject_t obj);
jsVal_t JS_IntegerToValue(jsContext_t context, jsInt_t i);
jsVal_t JS_BooleanToValue(jsContext_t context, jsBool_t b);
jsVal_t JS_DoubleToValue(jsContext_t context, jsDouble_t d);
jsVal_t JS_StringToValue(jsContext_t context, jsString_t s);


/* Runtime and Context *******************************************************/

/*
   Initialize
   Initialize all modules in the JavaScript Interpreter.
*/
void    Initialize_JSInterpreter();

/*
   Runtime
   JS_NewRuntime creates a JavaScript Runtime and JS_DeleteRuntime
   destroys a JavaScript Runtime.
*/
jsRuntime_t
		JS_NewRuntime(long maxBytes);
void    JS_DeleteRuntime(jsRuntime_t runtime);

/*
   Context
   JS_NewContext creates a JavaScript execution context and JS_DeleteContext
   destroys a JavaScript execution context.
*/
jsContext_t
		JS_NewContext(jsRuntime_t runtime);
void    JS_DeleteContext(jsContext_t context);
void    JS_CleanupContext(jsContext_t context);

/*
	Bind a thread and a context
	JS_EnterContext binds a thread and a context.
	User must execute this procedure before compiling or eveluating JavaScript.
*/
void    JS_EnterContext(jsContext_t context);

/*
   JS_ExitContext unbinds a thread and a context.
   User must execute this procedure after compiling and eveluating JavaScript.
   Since the status that multiple contexts are binded influences starting GC,
   user must execute the function as soon as possible after compiling or
   eveluating JavaScript.
*/
void    JS_ExitContext(jsContext_t context);

/*
   Initialize Standard objects
   The function adds standard objects to scope object such as Math,
   Date and so on.
*/
void    JS_InitStandardObjects(jsContext_t context, jsObject_t scope);

/* Object ********************************************************************/
/*
   Object
   JS_NewGlobalObject creates a new object according to arguments.
   User must call JS_DefineProperties and JS_DefineFunctions functions
	as necessary.
*/
jsObject_t
		JS_NewGlobalObject(jsContext_t context, jsClass_t *clazz);

/*
   JS_NewPlaneObject create a new object with proto object and call construct
   with args.  The proto object is created by JS_DefineClass.
*/
jsObject_t
		JS_NewPlaneObject(jsContext_t context, jsObject_t scope,
				jsObject_t proto);

/*
   JS_NewObject create a new object with name, sucn as "Array",
   "Date" and call construct with args.
*/
jsObject_t
		JS_NewObject(jsContext_t context, jsObject_t scope,
				jsString_t constructor,int numArgs, jsVal_t *args);

/*
   Define Class
   The function creates a new object with native constructor under  obj scope.
   The returned object is used as proto of JS_NewObject to create new object.
*/
jsObject_t
		JS_DefineClass(jsContext_t context, jsObject_t obj,
				jsObject_t parent_proto, jsClass_t *clazz,
				jsConstructor_t constructor,
				int numProperties, jsProperty_t *properties,
				int numFunctions, jsFunction_t *functions,
				int numCtorProperties, jsProperty_t *ctorProperties,
				int numCtorFunctions, jsFunction_t *ctorFunctions);
/*
   Set object properties
   JS_DefineFunctions adds function properties to an object.
   JS_DefineProperties adds properties to an object.
*/
void    JS_DefineFunctions(jsContext_t context, jsObject_t object,
				int numFunctions, jsFunction_t *functions);
void    JS_DefineProperties(jsContext_t context, jsObject_t object,
				int numProperties, jsProperty_t *properties);

/*
   Compile and execute JavaScript
   JS_CompileString compiles the string that is JavaScrit program.
   JS_ExecuteScript executes the script that is compiled by JS_CompileString.
   JS_EvaluateString compiles the string and executes the script.
   JS_DeleteScript deletes the script.
   JS_CallFunction call a function.
*/
jsScript_t
		JS_CompileString(jsContext_t context, jsObject_t scope,
				jsString_t string, jsString_t sourceName, int lineno);
jsVal_t JS_ExecuteScript(jsContext_t context, jsObject_t scope,
				jsScript_t script);
jsVal_t JS_EvaluateString(jsContext_t context, jsObject_t scope,
				jsString_t string, jsString_t sourceName, int lineno);
void    JS_DeleteScript(jsContext_t context, jsScript_t script);
jsVal_t JS_CallFunction(jsContext_t context, jsObject_t function,
				jsObject_t thisObject,int numArgs, jsVal_t *args);

/*
   Object property operation
   JS_GetTopLevelProp returns property of top level scope.  The function
   traverses scope chain, get top level scope and search property.
   JS_GetPrototype returns prototype of an object.
   JS_PutProperty put an object to scope as a named object.
*/
jsObject_t
		JS_GetClassPrototype(jsContext_t context, jsObject_t scope,
				jsString_t className);
jsVal_t JS_GetTopLevelProp(jsContext_t context, jsObject_t scope,
				jsString_t id);
jsObject_t
		JS_GetPrototype(jsContext_t context, jsObject_t object);
void    JS_PutProperty(jsContext_t context, jsObject_t object, jsString_t name,
				jsVal_t val);
jsObject_t
		JS_GetParentScope(jsContext_t context, jsObject_t object);

/*
   Context/Object closure
   JS_GetContextClosure gets extensive data from a context.
   JS_SetObjectContextClosure sets extensive data to a context.
   JS_GetObjectClosure gets extensive data from an object.
   JS_SetObjectClosure sets extensive data to an object.
*/
void    *JS_GetContextClosure(jsContext_t context);
void    JS_SetContextClosure (jsContext_t context, void *closure);

void    *JS_GetObjectClosure(jsObject_t object);
void    JS_SetObjectClosure(jsObject_t object, void *closure);

/*
	Default class
	The function gets a default class.  The default class is used
	in user defined class to call the default procedure.
*/
jsClass_t
		*JS_DefaultClass();


/* GC ************************************************************************/
/*
	Root object
   JS_AddRoot adds an object as a root object of GC.
   JS_RemoveRoot removes an object from root objects of GC.
   JS_GC do gc explicitly.
*/
void    JS_AddRoot(jsContext_t context, jsObject_t object);
void    JS_RemoveRoot(jsContext_t context, jsObject_t object);
void    JS_GC(jsContext_t context);
void    JS_GCMemFreeShow(jsContext_t context, int mode);
void    JS_GCMemFreeShowDetail(jsContext_t context, int mode);
int     JS_GCGetMemFree(jsContext_t context);
int     JS_GCGetMaxUse(jsContext_t context);
void    JS_GCResetMaxUse(jsContext_t context);
void    JS_ResetByteCodeMaxUse();

/* Others ********************************************************************/
/*
	Branch callback
	The function sets branch callback procedure in executing JavaScript
	to prevent infinite loop.  The callback is called on backword branch.
*/
void    JS_SetBranchCallback(jsContext_t context, jsBranchProc_t branch);

/*
   Error callback
   The function enables to define and use own error reporting mechanism in
   appilications.
*/
void    JS_SetErrorReporter(jsContext_t context, jsErrorReporter_t *reporter);

#ifdef MEASURMENT
void    JS_BytecodeCountShow();
void    JS_BytecodeCountReset();
#endif
  void    JS_BytecodeCountShow();
#ifdef STATS
void    JS_printGCRoots();
#endif
void    JS_RemoveAllProperties(jsContext_t context, jsObject_t object);
void    JS_ResetAllProperties(jsContext_t context, jsObject_t object);

#if defined(SUNOS5) || defined(LINUX)
void JS_ShowGCMemforSun();
void JS_ChangeGCMemSize(int gc, int bytecode, int compile);
#endif

#ifdef __cplusplus
}
#endif

#endif /* _JS_API_H_ */
