All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Scripting::JSContext Class Reference

JavaScript entry point to Cornerstone applications. More...

#include <JS/Context.hpp>

Public Member Functions

const v8::Persistent
< v8::Context > & 
context () const
 
void eval (const QString &src)
 Evaluate given snippet of JavaScript-code.
 
v8::Local< v8::Value > evalValue (const QString &src)
 Evaluate given snippet of JavaScript-code. More...
 
 JSContext (int &argc, char *argv[])
 Creates a new JS context with node.js. More...
 
bool load (const QString &filename)
 Load and execute JavaScript-code from file. More...
 
v8::Local< v8::Value > loadValue (const QString &filename, bool *ok=nullptr)
 Load and execute JavaScript-code from file and returns the same object that the script returns. More...
 
template<typename T >
Radiant::IntrusivePtr< T > loadWidget (const QString &filename, bool *ok=nullptr)
 Load and execute JavaScript-code from file and returns the returned object as WidgetPtr. More...
 
void makeDefault ()
 Makes this JS context the default context. More...
 
void update ()
 Run one iteration of nodejs/libuv main loop, should be called periodically. More...
 
virtual ~JSContext ()
 Deinitializes node.js and v8 and deletes the context.
 

Static Public Member Functions

static JSContextdefaultContext ()
 
static QString parseError (const v8::TryCatch &trycatch)
 Converts v8 exception error string to QString. More...
 
static void setCurrentStackLimit (int bytes)
 Sets v8 stack limit to given size, starting from current point at the C++ stack. More...
 

Detailed Description

JavaScript entry point to Cornerstone applications.

MultiWidgets::Application creates and manages one instance of this by default, usually there is no need to use this class directly, except the eval and load -functions.

Constructor & Destructor Documentation

Scripting::JSContext::JSContext ( int &  argc,
char *  argv[] 
)

Creates a new JS context with node.js.

Parameters
[in,out]argclength of argv, will be modified if argv is changed
[in,out]argvapplication command line arguments, v8-specific arguments are automatically removed

Member Function Documentation

const v8::Persistent<v8::Context>& Scripting::JSContext::context ( ) const
Returns
wrapped v8 context
static JSContext* Scripting::JSContext::defaultContext ( )
static
Returns
a pointer to default context, if there is any
v8::Local<v8::Value> Scripting::JSContext::evalValue ( const QString &  src)

Evaluate given snippet of JavaScript-code.

You can use ClassWrapper to convert the type to Cornerstone object

Returns
result of the evaluation as v8 value
bool Scripting::JSContext::load ( const QString &  filename)

Load and execute JavaScript-code from file.

Returns
false if there were some errors when loading or executing the file
v8::Local<v8::Value> Scripting::JSContext::loadValue ( const QString &  filename,
bool *  ok = nullptr 
)

Load and execute JavaScript-code from file and returns the same object that the script returns.

Parameters
filenamefile to load
[out]oknullptr or pointer to bool that will be set to true if loading succeeds or false if there are any errors
Returns
value that v8 returns
template<typename T >
Radiant::IntrusivePtr< T > Scripting::JSContext::loadWidget ( const QString &  filename,
bool *  ok = nullptr 
)

Load and execute JavaScript-code from file and returns the returned object as WidgetPtr.

For example: auto w = context.loadWidget<MultiWidgets::Widget>("file.js");

Parameters
filenamefile to load
[out]oknullptr or pointer to bool that will be set to true if loading succeeds or false if there are any errors
void Scripting::JSContext::makeDefault ( )

Makes this JS context the default context.

See Also
defaultContext
static QString Scripting::JSContext::parseError ( const v8::TryCatch &  trycatch)
static

Converts v8 exception error string to QString.

Parameters
trycatchv8 error
Returns
error as string
static void Scripting::JSContext::setCurrentStackLimit ( int  bytes)
static

Sets v8 stack limit to given size, starting from current point at the C++ stack.

Parameters
bytesstack size in bytes
void Scripting::JSContext::update ( )

Run one iteration of nodejs/libuv main loop, should be called periodically.

MultiWidgets::Application calls this automatically every frame