VRUT::JSEnvironment Class Reference

JavaScript Environment class. More...

#include <jsenvironment.h>

List of all members.

Public Member Functions

 JSEnvironment (JSHandler *_handler)
 Class constructor.
virtual ~JSEnvironment ()
 Class destructor.
bool RunScript (Kernel *_kernel, const char *fileName, SCENE_ID sceneID)
 Run the script.

Static Public Attributes

static const unsigned long MAX_SLEEP = 100
 The maximal sleeping time in milis without checking demand for script termination.

Private Member Functions

void initJSClasses () throw (ScriptException)
 Initialize all JS classes.

Private Attributes

JSRuntime * rt
 JS runtime.
JSContext * cx
 JS context.
JSObject * globalObj
 JS global object.
JSObject * sceneObj
 JS scene object.
JSHandlerhandler
 JSHandler instance.


Detailed Description

JavaScript Environment class.

Definition at line 26 of file jsenvironment.h.


Constructor & Destructor Documentation

JSEnvironment::JSEnvironment ( JSHandler _handler  ) 

Class constructor.

Create a runtime

Save objects' pointers to global context (for callbacks)

Definition at line 2517 of file jsenvironment.cpp.

02518 {
02520        rt = JS_NewRuntime(8L * 1024L * 1024L);
02521 
02523        jsHandler = handler = _handler;
02524        kernel = NULL;
02525 
02526        initJSEvents();
02527        initJSMemory();
02528 
02529 }

JSEnvironment::~JSEnvironment (  )  [virtual]

Class destructor.

Definition at line 2531 of file jsenvironment.cpp.

02532 {
02533        JS_DestroyRuntime(rt);
02534        JS_ShutDown();
02535 }


Member Function Documentation

void JSEnvironment::initJSClasses (  )  throw (ScriptException) [private]

Initialize all JS classes.

Define the Scene class

Define the Node class

Define the Matrix class

Define the Vector class

Define the Evenet class

Define the Memory class

Define the Chunk class

Definition at line 2538 of file jsenvironment.cpp.

02539 {
02541        if (!JS_InitClass(cx, globalObj, NULL, &SceneClass,
02542                      NULL, 0, /* constructor */
02543                      NULL, sceneFunctions, /* non-static */
02544                      NULL, sceneStaticFunctions /* static */ ))
02545               throw ScriptException("The Scene class cannot be created");
02546 
02548        if (!JS_InitClass(cx, globalObj, NULL, &NodeClass,
02549                      NULL, 0, /* constructor */
02550                      NULL, nodeFunctions, /* non-static */
02551                      nodeStaticProps, NULL /* static */ ))
02552               throw ScriptException("The Scene class cannot be created");
02553 
02555        if (!JS_InitClass(cx, globalObj, NULL, &MatrixClass,
02556                      matrixConstructor, MATRIX_SIZE, /* constructor */
02557                      NULL, matrixFunctions, /* non-static */
02558                      NULL, NULL /* static */ ))
02559               throw ScriptException("The Scene class cannot be created");
02560 
02562        if (!JS_InitClass(cx, globalObj, NULL, &VectorClass,
02563                      vectorConstructor, VECTOR_SIZE, /* constructor */
02564                      NULL, vectorFunctions, /* non-static */
02565                      NULL, NULL /* static */ ))
02566               throw ScriptException("The Scene class cannot be created");
02567 
02569        /*
02570        JSObject *eventProtoObj = JS_NewObject(cx, &EventProto, NULL, NULL);
02571        JS_DefineProperties(cx, eventProtoObj, eventStaticProps);
02572        JS_DefineFunctions(cx, eventProtoObj, eventStaticFunctions
02573        */
02574        if (!JS_InitClass(cx, globalObj, NULL, &EventClass,
02575                      NULL, 0, /* constructor */
02576                      NULL, eventFunctions, /* non-static */
02577                      eventStaticProps, eventStaticFunctions /* static */ ))
02578               throw ScriptException("The Event class cannot be created");
02579 
02581        if (!JS_InitClass(cx, globalObj, NULL, &MemoryClass,
02582                      NULL, 0, /* constructor */
02583                      NULL, memoryFunctions, /* non-static */
02584                      memoryStaticProps, memoryStaticFunctions /* static */ ))
02585               throw ScriptException("The Memory class cannot be created");
02586 
02588        if (!JS_InitClass(cx, globalObj, NULL, &ChunkClass,
02589                      NULL, 0, /* constructor */
02590                      chunkProps, chunkFunctions, /* non-static */
02591                      NULL, NULL /* static */ ))
02592               throw ScriptException("The Chunk class cannot be created");
02593 }

bool JSEnvironment::RunScript ( Kernel _kernel,
const char *  fileName,
SCENE_ID  sceneID 
)

Run the script.

Create a cx

Return value

Set the static kernel pointer

Set the script manager pointer

Create the global object

Define the global functions

Define the scene object

Compile the script

Create the script object

Add the script object to root (through GC)

Definition at line 2595 of file jsenvironment.cpp.

02596 {
02597        if (rt == NULL || _kernel == NULL)
02598               return false;
02599 
02600        FILE *file = fopen(fileName, "r");
02601        if (file == NULL)
02602        {
02603               logError("File couldn't be read");
02604               return false;
02605        }
02606 
02608        if ((cx = JS_NewContext(rt, 8192)) == NULL)
02609               return false;
02610 
02611        JS_SetOptions(cx, JSOPTION_VAROBJFIX);
02612        JS_SetVersion(cx, JSVERSION_1_7);
02613        JS_SetErrorReporter(cx, reportError);
02614 
02616        bool rc = true;
02617        realError = true;
02618 
02620        kernel = _kernel;
02621 
02623        ssm = new ScriptSceneManager(_kernel);
02624 
02625        try
02626        {
02628               globalObj = JS_NewObject(cx, &GlobalClass, NULL, NULL);
02629               if (globalObj == NULL || !JS_InitStandardClasses(cx, globalObj))
02630                      throw ScriptException("The global object can't be created");
02631 
02633               if (!JS_DefineFunctions(cx, globalObj, globalFunctions))
02634                      throw ScriptException("The global functions can't be defined");
02635 
02636               initJSClasses();
02637 
02638               if (sceneID != SCENE_ID_NONE) {
02640                      if (!(sceneObj = JS_DefineObject(cx, globalObj, "scene", &SceneClass, NULL, JSPROP_ENUMERATE)))
02641                             throw ScriptException("The scene object cannot be created");
02642 
02643                      if (!JS_SetPrivate(cx, sceneObj, (void *) new SceneData(sceneID)))
02644                             throw ScriptException("The scene data cannot be set");
02645               }
02646               else {
02647                      if (!JS_DefineProperty(cx, globalObj, "scene",
02648                                    JSVAL_VOID, NULL, NULL, 0))
02649                      {
02650                             throw ScriptException(
02651                                    "The scene object cannot be created");
02652                      }
02653               }
02655               JSScript *script = JS_CompileFileHandle(cx, globalObj, fileName, file);
02656               if (!script)
02657                      throw ScriptException("The script cannot be compilated");
02659               JSObject *scriptObj = JS_NewScriptObject(cx, script);
02660               if (scriptObj == NULL) {
02661                      JS_DestroyScript(cx, script);
02662                      throw ScriptException("The script object cannot be created");
02663               }
02665               if (!JS_AddNamedRoot(cx, &scriptObj, "mainScript"))
02666                      throw ScriptException("The script object cannot be added to root");
02667 
02668               jsval rval;
02669               rc = JS_ExecuteScript(cx, globalObj, script, &rval) == JS_TRUE;
02670               JS_RemoveRoot(cx, &scriptObj);
02671               if (!rc && realError)
02672                      throw ScriptException("Unsuccessfull executing the script");
02673        }
02674        catch (const ScriptException & exc)
02675        {
02676               rc = false;
02677               logError(exc.what());
02678 
02679 #if JSE_LOG_TO_CONSOLE
02680               cerr << exc.what() << endl;
02681 #endif
02682        }
02683 
02684        JS_DestroyContext(cx);
02685        delete ssm;
02686 
02687        return rc;
02688 }


Member Data Documentation

JSRuntime* VRUT::JSEnvironment::rt [private]

JS runtime.

Definition at line 30 of file jsenvironment.h.

JSContext* VRUT::JSEnvironment::cx [private]

JS context.

Definition at line 32 of file jsenvironment.h.

JSObject* VRUT::JSEnvironment::globalObj [private]

JS global object.

Definition at line 34 of file jsenvironment.h.

JSObject* VRUT::JSEnvironment::sceneObj [private]

JS scene object.

Definition at line 36 of file jsenvironment.h.

JSHandler instance.

Definition at line 39 of file jsenvironment.h.

const unsigned long VRUT::JSEnvironment::MAX_SLEEP = 100 [static]

The maximal sleeping time in milis without checking demand for script termination.

Definition at line 47 of file jsenvironment.h.


The documentation for this class was generated from the following files:

Generated on Tue Mar 10 14:41:49 2009 for VRUT by  doxygen 1.5.5