v8toolkit  0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
Public Member Functions | Public Attributes | Protected Member Functions | Friends | List of all members
v8toolkit::Context Class Reference

#include <javascript.h>

Inheritance diagram for v8toolkit::Context:
Inheritance graph
[legend]
Collaboration diagram for v8toolkit::Context:
Collaboration graph
[legend]

Public Member Functions

virtual ~Context ()
 
void shutdown ()
 
std::vector< ScriptPtr > const & get_scripts () const
 
std::vector< v8::Global< v8::Function > > const & get_functions () const
 
void register_external_script (v8::Local< v8::Script > external_script, std::string const &source_code)
 
void register_external_function (v8::Global< v8::Function > external_function)
 
v8::Global< v8::Context > const & get_global_context () const
 
 operator v8::Isolate * () const
 
 operator v8::Local< v8::Context > () const
 
 operator v8::Global< v8::Context > () const
 
v8::Local< v8::Context > get_context () const
 
v8::Isolate * get_isolate () const
 
std::shared_ptr< Isolateget_isolate_helper () const
 
std::shared_ptr< Scriptcompile (const std::string &source, const std::string &filename="Unspecified")
 
std::shared_ptr< Scriptcompile (const v8::Local< v8::String > source, const std::string &filename="Unspecified")
 
std::shared_ptr< Scriptcompile_from_file (const std::string &filename)
 
v8::Global< v8::Value > run (const v8::Global< v8::Script > &script)
 
v8::Global< v8::Value > run (const std::string &source)
 
v8::Global< v8::Value > run (const v8::Local< v8::Value > script)
 
v8::Global< v8::Value > run_from_file (const std::string &filename)
 
std::future< std::pair< ScriptPtr, v8::Global< v8::Value > > > run_async (const std::string &source, std::launch launch_policy=std::launch::async|std::launch::deferred)
 
std::thread run_thread (const v8::Global< v8::Script > &script)
 
std::thread run_thread (const std::string &source)
 
std::thread run_thread (const v8::Local< v8::Value > script)
 
void run_detached (const v8::Global< v8::Script > &script)
 
void run_detached (const std::string &source)
 
void run_detached (const v8::Local< v8::Value > script)
 
template<class Callable >
auto operator() (Callable &&callable) -> std::result_of_t< Callable()>
 
template<class Callable >
auto operator() (Callable &&callable) -> std::result_of_t< Callable(v8::Isolate *)>
 
template<class Callable >
auto operator() (Callable &&callable) -> typename std::result_of< Callable(v8::Isolate *, v8::Local< v8::Context >)>::type
 
template<class Function >
void add_function (std::string name, Function function)
 
void add_variable (std::string name, v8::Local< v8::Value > variable)
 
template<class Variable >
void expose_variable (std::string name, Variable &variable)
 
template<class Variable >
void expose_variable_readonly (std::string name, Variable &variable)
 
v8::Local< v8::Value > json (const std::string &json)
 
template<class T >
v8::Local< v8::Value > wrap_object (T *object)
 
boost::uuids::uuid const & get_uuid () const
 
std::string get_uuid_string () const
 
std::string get_url (std::string const &name) const
 
Script const & get_script_by_id (int64_t script_id)
 
v8::Local< v8::Function > get_function_by_id (int64_t script_id)
 
v8::Local< v8::Value > require (std::string const &filename, std::vector< std::string > const &paths)
 
void require_directory (std::string const &directory_name)
 

Public Attributes

v8::Isolate *const isolate
 shortcut to the v8::isolate object instead of always going through the Isolate More...
 

Protected Member Functions

 Context (std::shared_ptr< Isolate > isolate_helper, v8::Local< v8::Context > context)
 constructor should only be called by an Isolate or derived class More...
 

Friends

class Isolate
 

Detailed Description

Wrapper around a v8::Cnotext object with a link back to its associated isolate This object can be used wherever a v8::Isolate * or a Local or Global v8::Context is wanted. Can only be created via Isolate::create_context()

Definition at line 35 of file javascript.h.

Constructor & Destructor Documentation

v8toolkit::Context::Context ( std::shared_ptr< Isolate isolate_helper,
v8::Local< v8::Context >  context 
)
protected

constructor should only be called by an Isolate or derived class

Definition at line 15 of file javascript.cpp.

v8toolkit::Context::~Context ( )
virtual

Definition at line 49 of file javascript.cpp.

Member Function Documentation

void v8toolkit::Context::shutdown ( )

Allows for possible destruction of the Context once all Script objects are released. This clears out all internal references to scripts to stop any circular references. The context will have diminished functionality after shutdown is called on it.

std::vector<ScriptPtr> const& v8toolkit::Context::get_scripts ( ) const

Returns all the scripts associated with this context

Returns
a vector of all the scripts associated with this context
std::vector<v8::Global<v8::Function> > const& v8toolkit::Context::get_functions ( ) const

Returns a list of functions compiled directly to this context (vs those in a script)

Returns
a list of functions compiled directly to this context (vs those in a script)
void v8toolkit::Context::register_external_script ( v8::Local< v8::Script >  external_script,
std::string const &  source_code 
)

Registers an externally created script object with this Context and returns a wrapped Script object

Parameters
external_scriptscript that was created 'by hand' not with a method on this context
Returns
wrapped v8toolkit::Script object
void v8toolkit::Context::register_external_function ( v8::Global< v8::Function >  external_function)
v8::Global< v8::Context > const & v8toolkit::Context::get_global_context ( ) const

Returns the global context object - useful for GLOBAL_CONTEXT_SCOPED_RUN

Returns
the global context object

Definition at line 160 of file javascript.cpp.

Referenced by v8toolkit::WebsocketChannel::WebsocketChannel().

Here is the caller graph for this function:

v8toolkit::Context::operator v8::Isolate * ( ) const
inline

Implicit cast to v8::Isolate *

Definition at line 113 of file javascript.h.

References isolate.

v8toolkit::Context::operator v8::Local< v8::Context > ( ) const
inline

Implicit cast to v8::Local<v8::Context>

Definition at line 118 of file javascript.h.

v8toolkit::Context::operator v8::Global< v8::Context > ( ) const
inline

Implicit cast to v8::Global<v8::Context>

Definition at line 123 of file javascript.h.

References compile(), compile_from_file(), get_context(), get_isolate(), get_isolate_helper(), isolate, run(), run_async(), run_detached(), run_from_file(), and run_thread().

Here is the call graph for this function:

v8::Local< v8::Context > v8toolkit::Context::get_context ( ) const

Returns a Local copy of the associated v8::Context

Definition at line 24 of file javascript.cpp.

References isolate.

Referenced by add_function(), add_variable(), v8toolkit::DebugContext::DebugContext(), expose_variable(), expose_variable_readonly(), operator v8::Global< v8::Context >(), require(), and wrap_object().

Here is the caller graph for this function:

v8::Isolate * v8toolkit::Context::get_isolate ( ) const

Returns the v8::Isolate * this context is associated with

Definition at line 29 of file javascript.cpp.

References isolate.

Referenced by v8toolkit::Isolate::add_module_list(), operator v8::Global< v8::Context >(), require_directory(), v8toolkit::WebsocketChannel::WebsocketChannel(), and wrap_object().

Here is the caller graph for this function:

std::shared_ptr< Isolate > v8toolkit::Context::get_isolate_helper ( ) const

Returns the Isolate wrapping the isolate this context is associated with

Definition at line 35 of file javascript.cpp.

Referenced by operator v8::Global< v8::Context >().

Here is the caller graph for this function:

std::shared_ptr< Script > v8toolkit::Context::compile ( const std::string &  source,
const std::string &  filename = "Unspecified" 
)

Compiles the contents of the passed in string as javascripts Throws v8toolkit::CompilationError on compilation error

Definition at line 67 of file javascript.cpp.

References get_url(), GLOBAL_CONTEXT_SCOPED_RUN, and isolate.

Referenced by compile_from_file(), operator v8::Global< v8::Context >(), run(), run_async(), run_detached(), and run_thread().

Here is the call graph for this function:

Here is the caller graph for this function:

std::shared_ptr<Script> v8toolkit::Context::compile ( const v8::Local< v8::String >  source,
const std::string &  filename = "Unspecified" 
)

Compiles the contents of the passed in v8::String as javascript Throws v8toolkit::CompilationError on compilation error

std::shared_ptr< Script > v8toolkit::Context::compile_from_file ( const std::string &  filename)

Loads the contents of the given file as javascript Throws v8toolkit::CompilationError on compilation error TODO: what if the file can't be opened?

Definition at line 54 of file javascript.cpp.

References compile(), and v8toolkit::get_file_contents().

Referenced by operator v8::Global< v8::Context >(), and run_from_file().

Here is the call graph for this function:

Here is the caller graph for this function:

v8::Global< v8::Value > v8toolkit::Context::run ( const v8::Global< v8::Script > &  script)

Runs the previously compiled v8::Script. Throws v8toolkit::ExecutionError on execution error

Definition at line 98 of file javascript.cpp.

References GLOBAL_CONTEXT_SCOPED_RUN, isolate, and v8toolkit::ReportException().

Referenced by operator v8::Global< v8::Context >(), run(), v8toolkit::Script::run_async(), and v8toolkit::Script::run_thread().

Here is the call graph for this function:

Here is the caller graph for this function:

v8::Global< v8::Value > v8toolkit::Context::run ( const std::string &  source)

Compiles and runs the contents ot the passed in string Throws v8toolkit::CompilationError on compilation error Throws v8toolkit::ExecutionError on execution error

Definition at line 136 of file javascript.cpp.

References compile().

Here is the call graph for this function:

v8::Global< v8::Value > v8toolkit::Context::run ( const v8::Local< v8::Value >  script)

Definition at line 146 of file javascript.cpp.

References run().

Here is the call graph for this function:

v8::Global< v8::Value > v8toolkit::Context::run_from_file ( const std::string &  filename)

Definition at line 154 of file javascript.cpp.

References compile_from_file().

Referenced by operator v8::Global< v8::Context >().

Here is the call graph for this function:

Here is the caller graph for this function:

std::future< std::pair< ScriptPtr, v8::Global< v8::Value > > > v8toolkit::Context::run_async ( const std::string &  source,
std::launch  launch_policy = std::launch::async | std::launch::deferred 
)

Compiles and runs the contents of the passed in string in a std::async and returns the std::future associated with it. The future has the result of the javascript as well as a shared_ptr to the Script to make sure the value can still be used

While any number of threaded calls can be made, only one context per isolate can be actively running at a time. Additional calls will be queued but will block until they can acquire the v8::Locker object for their isolate TODO: what happens if there are errors in compilation? TODO: what happens if there are errors in execution?

Definition at line 166 of file javascript.cpp.

References compile().

Referenced by operator v8::Global< v8::Context >().

Here is the call graph for this function:

Here is the caller graph for this function:

std::thread v8toolkit::Context::run_thread ( const v8::Global< v8::Script > &  script)

Executes the previously compiled v8::script in a std::thread and returns the std::thread associated with it. It must either be joined or detached before the std::thread object is destroyed While any number of threaded calls can be made, only one context per isolate can be actively running at a time. Additional calls will be queued but will block until they can acquire the v8::Locker object for their isolate TODO: what happens if there are errors in execution?

Referenced by operator v8::Global< v8::Context >(), v8toolkit::Script::run_async(), and v8toolkit::Script::run_detached().

Here is the caller graph for this function:

std::thread v8toolkit::Context::run_thread ( const std::string &  source)

Compiles and runs the contents of the passed in string in a in a std::thread and returns the std::thread associated with it. It must either be joined or detached before the std::thread object is destroyed While any number of threaded calls can be made, only one context per isolate can be actively running at a time. Additional calls will be queued but will block until they can acquire the v8::Locker object for their isolate TODO: what happens if there are errors in compilation? TODO: what happens if there are errors in execution?

Definition at line 184 of file javascript.cpp.

References compile().

Here is the call graph for this function:

std::thread v8toolkit::Context::run_thread ( const v8::Local< v8::Value >  script)

Executes the previously compiled v8::script in a std::thread and returns the std::thread associated with it. It must either be joined or detached before the std::thread object is destroyed While any number of threaded calls can be made, only one context per isolate can be actively running at a time. Additional calls will be queued but will block until they can acquire the v8::Locker object for their isolate TODO: what happens if there are errors in execution?

void v8toolkit::Context::run_detached ( const v8::Global< v8::Script > &  script)

Executes the previously compiled v8::script in a detached std::thread. While any number of threaded calls can be made, only one context per isolate can be actively running at a time. Additional calls will be queued but will block until they can acquire the v8::Locker object for their isolate TODO: what happens if there are errors in execution?

Referenced by operator v8::Global< v8::Context >(), and v8toolkit::Script::run_async().

Here is the caller graph for this function:

void v8toolkit::Context::run_detached ( const std::string &  source)

Compiles and runs the contents of the passed in string in a detached std::thread. While any number of threaded calls can be made, only one context per isolate can be actively running at a time. Additional calls will be queued but will block until they can acquire the v8::Locker object for their isolate TODO: what happens if there are errors in compilation? TODO: what happens if there are errors in execution?

Definition at line 176 of file javascript.cpp.

References compile().

Here is the call graph for this function:

void v8toolkit::Context::run_detached ( const v8::Local< v8::Value >  script)

Executes the previously compiled v8::script in a detached std::thread. While any number of threaded calls can be made, only one context per isolate can be actively running at a time. Additional calls will be queued but will block until they can acquire the v8::Locker object for their isolate TODO: what happens if there are errors in execution?

template<class Callable >
auto v8toolkit::Context::operator() ( Callable &&  callable) -> std::result_of_t<Callable()>
inline

Calls v8toolkit::scoped_run with the associated isolate and context data

Definition at line 266 of file javascript.h.

References GLOBAL_CONTEXT_SCOPED_RUN.

template<class Callable >
auto v8toolkit::Context::operator() ( Callable &&  callable) -> std::result_of_t<Callable(v8::Isolate*)>
inline

Calls v8toolkit::scoped_run with the associated isolate and context data Passes the v8::Isolate * into the callback

Definition at line 277 of file javascript.h.

References GLOBAL_CONTEXT_SCOPED_RUN.

template<class Callable >
auto v8toolkit::Context::operator() ( Callable &&  callable) -> typename std::result_of<Callable(v8::Isolate*, v8::Local<v8::Context>)>::type
inline

Calls v8toolkit::scoped_run with the assciated isolate and context data Passes the v8::Isolate * and context into the callback

Definition at line 288 of file javascript.h.

References GLOBAL_CONTEXT_SCOPED_RUN.

template<class Function >
void v8toolkit::Context::add_function ( std::string  name,
Function  function 
)
inline

Adds a function to this context only See: v8toolkit::add_function

Definition at line 299 of file javascript.h.

References v8toolkit::add_function(), get_context(), and GLOBAL_CONTEXT_SCOPED_RUN.

Referenced by v8toolkit::Isolate::add_assert().

Here is the call graph for this function:

Here is the caller graph for this function:

void v8toolkit::Context::add_variable ( std::string  name,
v8::Local< v8::Value >  variable 
)
inline

Creates a global javascript variable to the specified context and sets it to the given javascript value.

Parameters
namename of the JavaScript variable
variableJavaScript value for the variable to refer to

Definition at line 310 of file javascript.h.

References v8toolkit::add_variable(), get_context(), and GLOBAL_CONTEXT_SCOPED_RUN.

Here is the call graph for this function:

template<class Variable >
void v8toolkit::Context::expose_variable ( std::string  name,
Variable &  variable 
)
inline

Exposes a C++ variable to this context only see: v8toolkit::expose_variable

Definition at line 321 of file javascript.h.

References v8toolkit::expose_variable(), and get_context().

Here is the call graph for this function:

template<class Variable >
void v8toolkit::Context::expose_variable_readonly ( std::string  name,
Variable &  variable 
)
inline
v8::Local< v8::Value > v8toolkit::Context::json ( const std::string &  json)

Definition at line 40 of file javascript.cpp.

References v8toolkit::get_debug_context().

Referenced by expose_variable_readonly().

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T >
v8::Local< v8::Value > v8toolkit::Context::wrap_object ( T *  object)

Returns a javascript object representation of the given c++ object see: votoolkit::V8ClassWrapper

Definition at line 699 of file javascript.h.

References get_context(), and get_isolate().

Referenced by expose_variable_readonly().

Here is the call graph for this function:

Here is the caller graph for this function:

boost::uuids::uuid const & v8toolkit::Context::get_uuid ( ) const

Definition at line 192 of file javascript.cpp.

Referenced by expose_variable_readonly().

Here is the caller graph for this function:

std::string v8toolkit::Context::get_uuid_string ( ) const

Definition at line 195 of file javascript.cpp.

Referenced by expose_variable_readonly(), and get_url().

Here is the caller graph for this function:

std::string v8toolkit::Context::get_url ( std::string const &  name) const

Definition at line 200 of file javascript.cpp.

References get_uuid_string().

Referenced by compile(), expose_variable_readonly(), and require().

Here is the call graph for this function:

Here is the caller graph for this function:

Script const& v8toolkit::Context::get_script_by_id ( int64_t  script_id)

Returns the script corresponding to the given id or throws

Parameters
script_idscript id to find
Returns
script with given script_id

Referenced by expose_variable_readonly().

Here is the caller graph for this function:

v8::Local<v8::Function> v8toolkit::Context::get_function_by_id ( int64_t  script_id)

Referenced by expose_variable_readonly().

Here is the caller graph for this function:

v8::Local< v8::Value > v8toolkit::Context::require ( std::string const &  filename,
std::vector< std::string > const &  paths 
)

Evaluates the specified file and returns the result - can be .js or .json

Parameters
filenamefile containing javascript or json to evaluate
Returns
the result of the evaluation or empty on failure

Definition at line 208 of file javascript.cpp.

References get_context(), get_url(), and v8toolkit::require().

Referenced by expose_variable_readonly(), and require_directory().

Here is the call graph for this function:

Here is the caller graph for this function:

void v8toolkit::Context::require_directory ( std::string const &  directory_name)

Definition at line 217 of file javascript.cpp.

References v8toolkit::foreach_file(), get_isolate(), isolate, require(), and v8toolkit::scoped_run().

Referenced by expose_variable_readonly().

Here is the call graph for this function:

Here is the caller graph for this function:

Friends And Related Function Documentation

friend class Isolate
friend

Definition at line 37 of file javascript.h.

Referenced by v8toolkit::Platform::create_isolate().

Member Data Documentation

v8::Isolate* const v8toolkit::Context::isolate

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