7 #include <boost/uuid/uuid.hpp> 8 #include <boost/uuid/uuid_generators.hpp> 9 #include <boost/uuid/uuid_io.hpp> 35 class Context :
public std::enable_shared_from_this<Context>
54 std::shared_ptr<Isolate> isolate_helper;
58 v8::Global<v8::Context> context;
90 std::vector<v8::Global<v8::Function>>
const &
get_functions()
const;
113 inline operator v8::Isolate*()
const {
return this->
isolate;}
123 inline operator v8::Global<v8::Context>()
const {
return v8::Global<v8::Context>(
isolate, this->context.Get(isolate));}
163 v8::Global<v8::Value>
run(
const v8::Global<v8::Script> & script);
187 std::future<std::pair<ScriptPtr, v8::Global<v8::Value>>>
189 std::launch launch_policy =
190 std::launch::async | std::launch::deferred);
203 std::thread
run_thread(
const v8::Global<v8::Script> & script);
238 void run_detached(
const v8::Global<v8::Script> & script);
265 template<
class Callable>
266 auto operator()(Callable && callable) -> std::result_of_t<Callable()>
276 template<
class Callable>
277 auto operator()(Callable && callable) -> std::result_of_t<Callable(v8::Isolate*)>
280 return callable(isolate);
287 template<
class Callable>
288 auto operator()(Callable && callable) ->
typename std::result_of<Callable(v8::Isolate*, v8::Local<v8::Context>)>::type
291 return callable(isolate, context.Get(isolate));
298 template<
class Function>
320 template<
class Variable>
326 template<
class Variable>
344 boost::uuids::uuid
const &
get_uuid()
const;
374 class Script :
public std::enable_shared_from_this<Script>
384 std::shared_ptr<Context> context_helper;
386 v8::Global<v8::Script> script;
397 #ifdef V8TOOLKIT_JAVASCRIPT_DEBUG 398 printf(
"Done deleting Script\n");
406 int64_t get_script_id()
const;
411 inline operator v8::Global<v8::Script>&(){
return script;}
416 template<
class...
Args>
425 v8::Global<v8::Value>
run(){
return context_helper->run(*
this);}
435 auto run_async(std::launch launch_policy = std::launch::async | std::launch::deferred){
437 return std::async(launch_policy, [
this](
ScriptPtr script)->std::pair<std::shared_ptr<Script>, v8::Global<v8::Value>> {
439 return (*this->context_helper)([
this, script](){
440 return std::make_pair(script, this->
run());
443 }, shared_from_this());
476 class Isolate :
public std::enable_shared_from_this<Isolate> {
493 v8::Global<v8::ObjectTemplate> global_object_template;
506 operator v8::Isolate*();
527 void add_require(std::vector<std::string> paths=std::vector<std::string>{
"./"});
537 std::shared_ptr<Context> create_context();
539 std::shared_ptr<DebugContext> create_debug_context(
short port);
555 template<
class Callable>
556 auto operator()(Callable && callable) -> std::result_of_t<Callable()>
566 template<
class Callable>
567 auto operator()(Callable && callable) ->
typename std::result_of<Callable(v8::Isolate*)>::type
570 return callable(isolate);
578 template<
class Callable>
579 auto operator()(Callable && callable) ->
typename std::result_of_t<Callable(v8::Isolate*, v8::Local<v8::Context>)>
589 template<
class Callable>
594 this->get_object_template(),
596 std::forward<Callable>(callable));
603 template<
class Variable>
609 template<
class Variable>
636 v8::TryCatch tc(this->isolate);
637 auto maybe = v8::JSON::Parse(this->isolate, v8::String::NewFromUtf8(this->isolate, json.c_str()));
638 if (tc.HasCaught()) {
641 return maybe.ToLocalChecked();
658 static std::unique_ptr<v8::Platform> platform;
660 static bool initialized;
661 static bool expose_gc_value;
663 static int memory_size_in_mb;
672 static void set_max_memory(
int memory_size_in_mb);
680 static void init(
int argc,
char ** argv,
std::string const & snapshot_directory =
"");
686 static void cleanup();
702 return class_wrapper.template wrap_existing_cpp_object(this->
get_context(),
object, *class_wrapper.destructor_behavior_leave_alone);
void expose_variable_readonly(std::string name, Variable &variable)
v8::Global< v8::Value > run_from_file(const std::string &filename)
void register_external_script(v8::Local< v8::Script > external_script, std::string const &source_code)
v8::Global< v8::Value > run(const v8::Global< v8::Script > &script)
auto operator()(Callable &&callable) -> std::result_of_t< Callable()>
auto operator()(Callable &&callable) -> typename std::result_of< Callable(v8::Isolate *, v8::Local< v8::Context >)>::type
void run_detached(const v8::Global< v8::Script > &script)
std::vector< v8::Global< v8::Function > > const & get_functions() const
v8::Local< v8::Value > require(std::string const &filename, std::vector< std::string > const &paths)
std::vector< ScriptPtr > const & get_scripts() const
boost::uuids::uuid const & get_uuid() const
auto operator()(Callable &&callable) -> std::result_of_t< Callable(v8::Isolate *)>
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
v8::Isolate *const isolate
shortcut to the v8::isolate object instead of always going through the Isolate
v8::Global< v8::Context > const & get_global_context() const
void expose_variable(std::string name, Variable &variable)
void require_directory(std::string const &directory_name)
std::shared_ptr< Script > compile(const std::string &source, const std::string &filename="Unspecified")
std::shared_ptr< Isolate > get_isolate_helper() const
void register_external_function(v8::Global< v8::Function > external_function)
v8::Local< v8::Function > get_function_by_id(int64_t script_id)
void add_variable(std::string name, v8::Local< v8::Value > variable)
std::thread run_thread(const v8::Global< v8::Script > &script)
v8::Local< v8::Value > json(const std::string &json)
v8::Local< v8::Value > wrap_object(T *object)
void add_function(std::string name, Function function)
v8::Local< v8::Context > get_context() const
v8::Isolate * get_isolate() const
std::shared_ptr< Script > compile_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)
Script const & get_script_by_id(int64_t script_id)
std::string get_url(std::string const &name) const
std::string get_uuid_string() const