v8toolkit
0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
|
#include <javascript.h>
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< Isolate > | get_isolate_helper () const |
std::shared_ptr< Script > | compile (const std::string &source, const std::string &filename="Unspecified") |
std::shared_ptr< Script > | compile (const v8::Local< v8::String > source, const std::string &filename="Unspecified") |
std::shared_ptr< Script > | compile_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 |
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.
|
protected |
constructor should only be called by an Isolate or derived class
Definition at line 15 of file javascript.cpp.
|
virtual |
Definition at line 49 of file javascript.cpp.
void v8toolkit::Context::shutdown | ( | ) |
std::vector<ScriptPtr> const& v8toolkit::Context::get_scripts | ( | ) | const |
Returns 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)
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
external_script | script that was created 'by hand' not with a method on this context |
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
Definition at line 160 of file javascript.cpp.
Referenced by v8toolkit::WebsocketChannel::WebsocketChannel().
|
inline |
|
inline |
Implicit cast to v8::Local<v8::Context>
Definition at line 118 of file javascript.h.
|
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().
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().
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().
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 >().
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().
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().
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().
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().
v8::Global< v8::Value > v8toolkit::Context::run | ( | const v8::Local< v8::Value > | script | ) |
Definition at line 146 of file javascript.cpp.
References run().
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 >().
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 >().
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().
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().
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().
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().
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?
|
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.
|
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.
|
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.
|
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().
|
inline |
Creates a global javascript variable to the specified context and sets it to the given javascript value.
name | name of the JavaScript variable |
variable | JavaScript 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.
|
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().
|
inline |
Definition at line 327 of file javascript.h.
References v8toolkit::expose_variable_readonly(), get_context(), get_function_by_id(), get_script_by_id(), get_url(), get_uuid(), get_uuid_string(), json(), require(), require_directory(), and wrap_object().
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().
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().
boost::uuids::uuid const & v8toolkit::Context::get_uuid | ( | ) | const |
Definition at line 192 of file javascript.cpp.
Referenced by expose_variable_readonly().
std::string v8toolkit::Context::get_uuid_string | ( | ) | const |
Definition at line 195 of file javascript.cpp.
Referenced by expose_variable_readonly(), and get_url().
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().
Script const& v8toolkit::Context::get_script_by_id | ( | int64_t | script_id | ) |
Returns the script corresponding to the given id or throws
script_id | script id to find |
Referenced by expose_variable_readonly().
v8::Local<v8::Function> v8toolkit::Context::get_function_by_id | ( | int64_t | script_id | ) |
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
filename | file containing javascript or json to evaluate |
Definition at line 208 of file javascript.cpp.
References get_context(), get_url(), and v8toolkit::require().
Referenced by expose_variable_readonly(), and require_directory().
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().
|
friend |
Definition at line 37 of file javascript.h.
Referenced by v8toolkit::Platform::create_isolate().
v8::Isolate* const v8toolkit::Context::isolate |
shortcut to the v8::isolate object instead of always going through the Isolate
Definition at line 69 of file javascript.h.
Referenced by compile(), v8toolkit::Platform::create_isolate(), v8toolkit::DebugContext::DebugContext(), get_context(), get_isolate(), v8toolkit::Isolate::get_isolate(), operator v8::Global< v8::Context >(), operator v8::Isolate *(), v8toolkit::Isolate::operator v8::Isolate *(), require_directory(), and run().