v8toolkit  0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
javascript.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <thread>
4 #include <mutex>
5 #include <future>
6 
7 #include <boost/uuid/uuid.hpp> // uuid class
8 #include <boost/uuid/uuid_generators.hpp> // generators
9 #include <boost/uuid/uuid_io.hpp> // streaming operators etc.
10 
11 
12 
13 #include "v8_class_wrapper.h"
14 
15 //#define V8TOOLKIT_JAVASCRIPT_DEBUG
16 
17 namespace v8toolkit {
18 
19 class DebugContext;
20 
21 extern boost::uuids::random_generator uuid_generator;
22 
23 class Isolate;
24 
25 class Script;
26 using ScriptPtr = std::shared_ptr<Script>;
27 
28 
29 /**
30 * Wrapper around a v8::Cnotext object with a link back to its associated isolate
31 * This object can be used wherever a v8::Isolate * or a Local or Global v8::Context
32 * is wanted.
33 * Can only be created via Isolate::create_context()
34 */
35 class Context : public std::enable_shared_from_this<Context>
36 {
37  friend class Isolate;
38 protected:
39  /// constructor should only be called by an Isolate or derived class
40  Context(std::shared_ptr<Isolate> isolate_helper, v8::Local<v8::Context> context);
41 
42 
43 private:
44  std::atomic<int> script_id_counter;
45 
46  Context() = delete;
47  Context(const Context &) = delete;
48  Context(Context &&) = default;
49  Context & operator=(const Context &) = delete;
50  Context & operator=(Context &&) = default;
51 
52  // isolate_helper MUST be first so it's the last element cleaned up
53  /// The Isolate that created this Context will be kept around as long as a Context it created is still around
54  std::shared_ptr<Isolate> isolate_helper;
55 
56 
57  /// The actual v8::Context object backing this Context
58  v8::Global<v8::Context> context;
59 
60 
61 
62  /// unique identifier for each context
63  boost::uuids::uuid const uuid = v8toolkit::uuid_generator();
64 
65 
66 public:
67 
68  /// shortcut to the v8::isolate object instead of always going through the Isolate
69  v8::Isolate * const isolate;
70 
71  virtual ~Context();
72 
73  /**
74  * Allows for possible destruction of the Context once all Script objects are released. This clears out
75  * all internal references to scripts to stop any circular references. The context will have diminished
76  * functionality after shutdown is called on it.
77  */
78  void shutdown();
79 
80  /**
81  * Returns all the scripts associated with this context
82  * @return a vector of all the scripts associated with this context
83  */
84  std::vector<ScriptPtr> const & get_scripts() const;
85 
86  /**
87  * Returns a list of functions compiled directly to this context (vs those in a script)
88  * @return a list of functions compiled directly to this context (vs those in a script)
89  */
90  std::vector<v8::Global<v8::Function>> const & get_functions() const;
91 
92 
93  /**
94  * Registers an externally created script object with this Context and returns a wrapped
95  * Script object
96  * @param external_script script that was created 'by hand' not with a method on this context
97  * @return wrapped v8toolkit::Script object
98  */
99  void register_external_script(v8::Local<v8::Script> external_script, std::string const & source_code);
100 
101  void register_external_function(v8::Global<v8::Function> external_function);
102 
103 
104  /**
105  * Returns the global context object - useful for GLOBAL_CONTEXT_SCOPED_RUN
106  * @return the global context object
107  */
108  v8::Global<v8::Context> const & get_global_context() const;
109 
110  /**
111  * Implicit cast to v8::Isolate *
112  */
113  inline operator v8::Isolate*() const {return this->isolate;}
114 
115  /**
116  * Implicit cast to v8::Local<v8::Context>
117  */
118  inline operator v8::Local<v8::Context>() const {return this->context.Get(isolate);}
119 
120  /**
121  * Implicit cast to v8::Global<v8::Context>
122  */
123  inline operator v8::Global<v8::Context>() const {return v8::Global<v8::Context>(isolate, this->context.Get(isolate));}
124 
125  /**
126  * Returns a Local copy of the associated v8::Context
127  */
129 
130  /**
131  * Returns the v8::Isolate * this context is associated with
132  */
133  v8::Isolate * get_isolate() const;
134 
135  /**
136  * Returns the Isolate wrapping the isolate this context is associated with
137  */
138  std::shared_ptr<Isolate> get_isolate_helper() const;
139 
140  /**
141  * Compiles the contents of the passed in string as javascripts
142  * Throws v8toolkit::CompilationError on compilation error
143  */
144  std::shared_ptr<Script> compile(const std::string & source, const std::string & filename = "Unspecified");
145 
146  /**
147  * Compiles the contents of the passed in v8::String as javascript
148  * Throws v8toolkit::CompilationError on compilation error
149  */
150  std::shared_ptr<Script> compile(const v8::Local<v8::String> source, const std::string & filename = "Unspecified");
151 
152  /**
153  * Loads the contents of the given file as javascript
154  * Throws v8toolkit::CompilationError on compilation error
155  * TODO: what if the file can't be opened?
156  */
157  std::shared_ptr<Script> compile_from_file(const std::string & filename);
158 
159  /**
160  * Runs the previously compiled v8::Script.
161  * Throws v8toolkit::ExecutionError on execution error
162  */
163  v8::Global<v8::Value> run(const v8::Global<v8::Script> & script);
164 
165  /**
166  * Compiles and runs the contents ot the passed in string
167  * Throws v8toolkit::CompilationError on compilation error
168  * Throws v8toolkit::ExecutionError on execution error
169  */
170  v8::Global<v8::Value> run(const std::string & source);
171  v8::Global<v8::Value> run(const v8::Local<v8::Value> script);
172 
173  v8::Global<v8::Value> run_from_file(const std::string & filename);
174 
175  /**
176  * Compiles and runs the contents of the passed in string in a std::async and returns
177  * the std::future associated with it. The future has the result of the javascript
178  * as well as a shared_ptr to the Script to make sure the value can still be used
179  *
180  * While any number of threaded calls can be made, only one context per
181  * isolate can be actively running at a time. Additional calls will be
182  * queued but will block until they can acquire the v8::Locker object for
183  * their isolate
184  * TODO: what happens if there are errors in compilation?
185  * TODO: what happens if there are errors in execution?
186  */
187  std::future<std::pair<ScriptPtr, v8::Global<v8::Value>>>
188  run_async(const std::string & source,
189  std::launch launch_policy =
190  std::launch::async | std::launch::deferred);
191 
192 
193  /**
194  * Executes the previously compiled v8::script in a std::thread and returns
195  * the std::thread associated with it. It must either be joined or detached
196  * before the std::thread object is destroyed
197  * While any number of threaded calls can be made, only one context per
198  * isolate can be actively running at a time. Additional calls will be
199  * queued but will block until they can acquire the v8::Locker object for
200  * their isolate
201  * TODO: what happens if there are errors in execution?
202  */
203  std::thread run_thread(const v8::Global<v8::Script> & script);
204 
205  /**
206  * Compiles and runs the contents of the passed in string in a in a std::thread and returns
207  * the std::thread associated with it. It must either be joined or detached
208  * before the std::thread object is destroyed
209  * While any number of threaded calls can be made, only one context per
210  * isolate can be actively running at a time. Additional calls will be
211  * queued but will block until they can acquire the v8::Locker object for
212  * their isolate
213  * TODO: what happens if there are errors in compilation?
214  * TODO: what happens if there are errors in execution?
215  */
216  std::thread run_thread(const std::string & source);
217 
218  /**
219  * Executes the previously compiled v8::script in a std::thread and returns
220  * the std::thread associated with it. It must either be joined or detached
221  * before the std::thread object is destroyed
222  * While any number of threaded calls can be made, only one context per
223  * isolate can be actively running at a time. Additional calls will be
224  * queued but will block until they can acquire the v8::Locker object for
225  * their isolate
226  * TODO: what happens if there are errors in execution?
227  */
228  std::thread run_thread(const v8::Local<v8::Value> script);
229 
230  /**
231  * Executes the previously compiled v8::script in a detached std::thread.
232  * While any number of threaded calls can be made, only one context per
233  * isolate can be actively running at a time. Additional calls will be
234  * queued but will block until they can acquire the v8::Locker object for
235  * their isolate
236  * TODO: what happens if there are errors in execution?
237  */
238  void run_detached(const v8::Global<v8::Script> & script);
239 
240  /**
241  * Compiles and runs the contents of the passed in string in a detached std::thread.
242  * While any number of threaded calls can be made, only one context per
243  * isolate can be actively running at a time. Additional calls will be
244  * queued but will block until they can acquire the v8::Locker object for
245  * their isolate
246  * TODO: what happens if there are errors in compilation?
247  * TODO: what happens if there are errors in execution?
248  */
249  void run_detached(const std::string & source);
250 
251  /**
252  * Executes the previously compiled v8::script in a detached std::thread.
253  * While any number of threaded calls can be made, only one context per
254  * isolate can be actively running at a time. Additional calls will be
255  * queued but will block until they can acquire the v8::Locker object for
256  * their isolate
257  * TODO: what happens if there are errors in execution?
258  */
259  void run_detached(const v8::Local<v8::Value> script);
260 
261 
262  /**
263  * Calls v8toolkit::scoped_run with the associated isolate and context data
264  */
265  template<class Callable>
266  auto operator()(Callable && callable) -> std::result_of_t<Callable()>
267  {
268  GLOBAL_CONTEXT_SCOPED_RUN(isolate, context);
269  return callable();
270  }
271 
272  /**
273  * Calls v8toolkit::scoped_run with the associated isolate and context data
274  * Passes the v8::Isolate * into the callback
275  */
276  template<class Callable>
277  auto operator()(Callable && callable) -> std::result_of_t<Callable(v8::Isolate*)>
278  {
279  GLOBAL_CONTEXT_SCOPED_RUN(isolate, context);
280  return callable(isolate);
281  }
282 
283  /**
284  * Calls v8toolkit::scoped_run with the assciated isolate and context data
285  * Passes the v8::Isolate * and context into the callback
286  */
287  template<class Callable>
288  auto operator()(Callable && callable) -> typename std::result_of<Callable(v8::Isolate*, v8::Local<v8::Context>)>::type
289  {
290  GLOBAL_CONTEXT_SCOPED_RUN(isolate, context);
291  return callable(isolate, context.Get(isolate));
292  }
293 
294  /**
295  * Adds a function to this context only
296  * See: v8toolkit::add_function
297  */
298  template<class Function>
299  void add_function(std::string name, Function function)
300  {
301  GLOBAL_CONTEXT_SCOPED_RUN(isolate, context);
302  v8toolkit::add_function(get_context(), get_context()->Global(), name.c_str(), function);
303  }
304 
305  /**
306  * Creates a global javascript variable to the specified context and sets it to the given javascript value.
307  * @param name name of the JavaScript variable
308  * @param variable JavaScript value for the variable to refer to
309  */
310  inline void add_variable(std::string name, v8::Local<v8::Value> variable)
311  {
312  GLOBAL_CONTEXT_SCOPED_RUN(isolate, context);
313  v8toolkit::add_variable(get_context(), get_context()->Global(), name.c_str(), variable);
314  }
315 
316  /**
317  * Exposes a C++ variable to this context only
318  * see: v8toolkit::expose_variable
319  */
320  template<class Variable>
321  void expose_variable(std::string name, Variable & variable)
322  {
323  v8toolkit::expose_variable(get_context(), get_context()->Global(), name.c_str(), variable);
324  }
325 
326  template<class Variable>
327  void expose_variable_readonly(std::string name, Variable & variable)
328  {
329  v8toolkit::expose_variable_readonly(get_context(), get_context()->Global(), name.c_str(), variable);
330 
331  }
332 
334 
335 
336 
337  /**
338  * Returns a javascript object representation of the given c++ object
339  * see: votoolkit::V8ClassWrapper
340  */
341  template<class T>
343 
344  boost::uuids::uuid const & get_uuid() const;
346  std::string get_url(std::string const & name) const;
347 
348  /**
349  * Returns the script corresponding to the given id or throws
350  * @param script_id script id to find
351  * @return script with given script_id
352  */
353  Script const & get_script_by_id(int64_t script_id);
354 
355  v8::Local<v8::Function> get_function_by_id(int64_t script_id);
356 
357  /**
358  * Evaluates the specified file and returns the result - can be .js or .json
359  * @param filename file containing javascript or json to evaluate
360  * @return the result of the evaluation or empty on failure
361  */
362  v8::Local<v8::Value> require(std::string const & filename, std::vector<std::string> const & paths);
363  void require_directory(std::string const & directory_name);
364 
365 };
366 
367 using ContextPtr = std::shared_ptr<Context>;
368 
369 
370 /**
371 * Helper class for a v8::Script object. As long as a Script shared_ptr is around,
372 * the associated Context will be maintined (which keeps the Isolate around, too)
373 */
374 class Script : public std::enable_shared_from_this<Script>
375 {
376  friend class Context;
377 
378 private:
379  Script(ContextPtr context_helper,
380  v8::Local<v8::Script> script,
381  std::string const & source_code = "");
382 
383  // shared_ptr to Context should be first so it's the last cleaned up
384  std::shared_ptr<Context> context_helper;
385  v8::Isolate * isolate;
386  v8::Global<v8::Script> script;
387  std::string script_source_code;
388 
389 public:
390 
391  Script() = delete;
392  Script(const Script &) = delete;
393  Script(Script &&) = default;
394  Script & operator=(const Script &) = delete;
395  Script & operator=(Script &&) = default;
396  virtual ~Script(){
397 #ifdef V8TOOLKIT_JAVASCRIPT_DEBUG
398  printf("Done deleting Script\n");
399 #endif
400  }
401 
402  std::string const & get_source_code() const;
403 
404  // this should go back to being a ref to an instance variable
405  std::string get_source_location() const;
406  int64_t get_script_id() const;
407 
408  /**
409  * Allows implicit conversion to a v8::Global<v8::Script>
410  */
411  inline operator v8::Global<v8::Script>&(){return script;}
412 
413  /**
414  * Calls scoped_run with the associated isolate and context
415  */
416  template<class... Args>
417  void operator()(Args&&... args){(*context_helper)(std::forward<Args>(args)...);}
418 
419  /**
420  * Returns the Context associated with this Script
421  */
422  inline auto get_context_helper(){return context_helper;}
423 
424  // TODO: Run code should be moved out of contexthelper and into this class
425  v8::Global<v8::Value> run(){return context_helper->run(*this);}
426 
427  /**
428  * Run this script in a std::async and return the associated future. The future value is a
429  * std::pair<javascript_result, shared_ptr<Script>>. It contains a
430  * shared_ptr to the Script so the Script (and it's associated dependencies)
431  * cannot be destroyed until after the async has finished and the caller has had a chance
432  * to use the results contained in the future
433  * The order of the pair elements matters to make sure the Global is cleaned up before the ScriptPtr
434  */
435  auto run_async(std::launch launch_policy = std::launch::async | std::launch::deferred){
436 
437  return std::async(launch_policy, [this](ScriptPtr script)->std::pair<std::shared_ptr<Script>, v8::Global<v8::Value>> {
438 
439  return (*this->context_helper)([this, script](){
440  return std::make_pair(script, this->run());
441  });
442 
443  }, shared_from_this());
444 
445  }
446 
447  /**
448  * Run this script in a std::thread, returning the std::thread object for joining/detaching.
449  * The thread maintains a shared_ptr to this Script so it cannot be destroyed
450  * until after the thread completes.
451  * Remember, letting the std::thread go out of scope without joinin/detaching is very bad.
452  */
453  std::thread run_thread();
454 
455 
456  /**
457  * Same as run_thread, but the thread is automatically detached. The Script
458  * object is still protected for the lifetime of the
459  */
460  void run_detached();
461 
462 
463  v8::Local<v8::UnboundScript> get_unbound_script() const;
464 
465 };
466 
467 
468 
469 /**
470 * Represents a v8::Isolate object. Any changes made here will be reflected in any
471 * contexts created after the change was made.
472 * An IsolateHleper will remain as long as the user has a shared_ptr to the Isolate
473 * itself or a unique_ptr to any Context created from the Isolate.
474 * Can only be created by calling Platform::create_isolate()
475 */
476 class Isolate : public std::enable_shared_from_this<Isolate> {
477  friend class Platform; // calls the Isolate's private constructor
478 private:
479 
480  // Only called by Platform
481  Isolate(v8::Isolate * isolate);
482 
483  /// The actual v8::Isolate object represented by this Isolate
484  v8::Isolate * isolate;
485 
486  /**
487  * The global object template used when creating a context
488  * Contexts will reflect all changes made to this template at the
489  * time of context creation, but template changes after context
490  * creation will not affect contexts already created, only
491  * subsequently created ones
492  */
493  v8::Global<v8::ObjectTemplate> global_object_template;
494 
495  boost::uuids::uuid const uuid = v8toolkit::uuid_generator();
496 
497 
498 public:
499 
500  virtual ~Isolate();
501 
502  /**
503  * Implicit cast to v8::Isolate* so an Isolate can be used
504  * where a v8::Isolate* would otherwise be required
505  */
506  operator v8::Isolate*();
507 
508  /**
509  * Implicit cast to Local<ObjectTemplate> for the global object
510  * template used to create new contexts
511  */
513 
514  /**
515  * Adds print helpers to global object template as defined in
516  * v8toolkit::add_print()
517  */
518  Isolate & add_print(func::function<void(const std::string &)>);
519  Isolate & add_print();
520 
521  void add_assert();
522 
523  /**
524  * Adds require() function to javascript as defined in
525  * v8toolkit::add_require()
526  */
527  void add_require(std::vector<std::string> paths=std::vector<std::string>{"./"});
528 
529  void add_module_list(){(*this)([this]{v8toolkit::add_module_list(isolate, global_object_template.Get(isolate));});}
530 
531  /**
532  * Creates a Context populated by all customizations already
533  * applied to this Isolate, but subsequent customizations to
534  * the Isolate will not be applied to Contexts already
535  * created.
536  */
537  std::shared_ptr<Context> create_context();
538 
539  std::shared_ptr<DebugContext> create_debug_context(short port);
540 
541  /**
542  * Returns the isolate associated with this Isolate
543  */
544  v8::Isolate * get_isolate();
545 
546  /**
547  * Returns the v8::ObjectTemplate used to create contexts from this Isolate
548  */
549  v8::Local<v8::ObjectTemplate> get_object_template();
550 
551 
552  /**
553  * wraps "callable" in appropriate thread locks, isolate, and handle scopes
554  */
555  template<class Callable>
556  auto operator()(Callable && callable) -> std::result_of_t<Callable()>
557  {
558  ISOLATE_SCOPED_RUN(isolate);
559  return callable();
560  }
561 
562  /**
563  * wraps "callable" in appropriate thread locks, isolate, and handle scopes
564  * Passes the v8::Isolate * to the callable function
565  */
566  template<class Callable>
567  auto operator()(Callable && callable) -> typename std::result_of<Callable(v8::Isolate*)>::type
568  {
569  ISOLATE_SCOPED_RUN(isolate);
570  return callable(isolate);
571  }
572 
573  /**
574  * wraps "callable" in appropriate thread locks, isolate, and handle scopes
575  * Passes the v8::Isolate * and v8::Local<v8::Context> to the callable function.
576  * Throws v8toolkit::InvalidCallException if the isolate is not currently in a context
577  */
578  template<class Callable>
579  auto operator()(Callable && callable) -> typename std::result_of_t<Callable(v8::Isolate*, v8::Local<v8::Context>)>
580  {
581  return v8toolkit::scoped_run(isolate, std::forward<Callable>(callable));
582  }
583 
584  /**
585  * Adds the specified callable as <name> in javascript
586  * This can be a standard C++ function, a lambda, or anything else that supports
587  * being called with operator()
588  */
589  template<class Callable>
590  void add_function(std::string name, Callable && callable)
591  {
592  (*this)([&](){
593  v8toolkit::add_function(isolate,
594  this->get_object_template(),
595  name.c_str(),
596  std::forward<Callable>(callable));
597  });
598  }
599 
600  /**
601  * Exposes an existing C++ variable as <name> to javascript
602  */
603  template<class Variable>
604  void expose_variable(std::string name, Variable & variable)
605  {
606  v8toolkit::expose_variable(isolate, this->get_object_template(), name.c_str(), variable);
607  }
608 
609  template<class Variable>
610  void expose_variable_readonly(std::string name, Variable & variable)
611  {
612  v8toolkit::expose_variable_readonly(isolate, this->get_object_template(), name.c_str(), variable);
613  }
614 
615  /// Not sure what this is used for
616  void add_variable(const std::string & name, v8::Local<v8::ObjectTemplate> template_to_attach)
617  {
618  v8toolkit::add_variable(this->isolate, this->get_object_template(), name.c_str(), template_to_attach);
619  }
620 
621  /**
622  * Returns a V8ClassWrapper object for wrapping C++ classes in this isolate. Classes must be wrapped
623  * before contexts using them are created
624  */
625  template<class T>
626  auto & wrap_class() {
627  v8toolkit::V8ClassWrapper<std::add_const_t<T>>::get_instance(this->isolate);
628  return v8toolkit::V8ClassWrapper<T>::get_instance(this->isolate);
629 
630  }
631 
632  /**
633  * Returns a value representing the JSON string specified or throws on bad JSON
634  */
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()) {
639  throw V8ExecutionException(this->isolate, tc);
640  }
641  return maybe.ToLocalChecked();
642  }
643 
644  /**
645  * Returns the debug context for the isolate
646  * @return the debug context for the isolate
647  */
649 };
650 
651 using IsolatePtr = std::shared_ptr<Isolate>;
652 
653 /**
654 * A singleton responsible for initializing the v8 platform and creating isolate helpers.
655 */
656 class Platform {
657 
658  static std::unique_ptr<v8::Platform> platform;
659  static v8toolkit::ArrayBufferAllocator allocator;
660  static bool initialized;
661  static bool expose_gc_value;
662  static std::string expose_debug_name;
663  static int memory_size_in_mb; // used for Isolate::CreateParams::constraints::set_max_old_space_size()
664 public:
665 
666  // how to increase max memory available to javascript
667  // Isolate::CreateParams::constraints::set_max_old_space_size().
668 
669 
670  static void expose_gc();
671 
672  static void set_max_memory(int memory_size_in_mb);
673 
674  /**
675  * Parses argv for v8-specific options, applies them, and removes them
676  * from argv and adjusts argc accordingly.
677  * @param snapshot_directory directory in which to look for v8 snapshot .bin files. Leave as empty
678  * string if linking against v8 compiled with use_snapshots=false
679  */
680  static void init(int argc, char ** argv, std::string const & snapshot_directory = "");
681 
682  /**
683  * Shuts down V8. Any subsequent V8 usage is probably undefined, so
684  * make sure everything is done before you call this.
685  */
686  static void cleanup();
687 
688 
689  /**
690  * Creates a new Isolate wrapping a new v8::Isolate instance.
691  * An Isolate will remain as long as the caller has a shared_ptr to the Isolate or any Contexts created from
692  * the Isolate still exist.
693  */
694  static IsolatePtr create_isolate();
695 };
696 
697 
698 template<class T>
700 {
701  auto & class_wrapper = V8ClassWrapper<T>::get_instance(this->get_isolate());
702  return class_wrapper.template wrap_existing_cpp_object(this->get_context(), object, *class_wrapper.destructor_behavior_leave_alone);
703 }
704 
705 
706 } // end v8toolkit namespace
std::atomic< int > script_id_counter(0)
auto run_async(std::launch launch_policy=std::launch::async|std::launch::deferred)
Definition: javascript.h:435
void expose_variable_readonly(std::string name, Variable &variable)
Definition: javascript.h:327
friend class Isolate
Definition: javascript.h:37
v8::Local< v8::Value > json(std::string json)
Definition: javascript.h:635
v8::Global< v8::Value > run_from_file(const std::string &filename)
Definition: javascript.cpp:154
void register_external_script(v8::Local< v8::Script > external_script, std::string const &source_code)
void expose_variable_readonly(v8::Isolate *isolate, const v8::Local< v8::ObjectTemplate > &object_template, const char *name, T &variable)
Definition: v8toolkit.h:669
void operator()(Args &&...args)
Definition: javascript.h:417
void expose_gc()
Definition: v8toolkit.cpp:52
auto get_context_helper()
Definition: javascript.h:422
void expose_variable_readonly(std::string name, Variable &variable)
Definition: javascript.h:610
v8::Global< v8::Value > run(const v8::Global< v8::Script > &script)
Definition: javascript.cpp:98
auto operator()(Callable &&callable) -> std::result_of_t< Callable()>
Definition: javascript.h:266
auto operator()(Callable &&callable) -> typename std::result_of_t< Callable(v8::Isolate *, v8::Local< v8::Context >)>
Definition: javascript.h:579
void expose_variable(v8::Isolate *isolate, const v8::Local< v8::ObjectTemplate > &object_template, const char *name, T &variable)
Definition: v8toolkit.h:642
v8::Global< v8::Value > run()
Definition: javascript.h:425
::std::string string
Definition: gtest-port.h:1097
auto operator()(Callable &&callable) -> typename std::result_of< Callable(v8::Isolate *, v8::Local< v8::Context >)>::type
Definition: javascript.h:288
void add_assert(v8::Isolate *isolate, v8::Local< v8::ObjectTemplate > object_template)
Definition: v8toolkit.cpp:212
void run_detached(const v8::Global< v8::Script > &script)
v8::Local< v8::Context > get_debug_context()
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)
Definition: javascript.cpp:208
std::vector< ScriptPtr > const & get_scripts() const
boost::uuids::uuid const & get_uuid() const
Definition: javascript.cpp:192
void add_require(v8::Isolate *isolate, const v8::Local< v8::ObjectTemplate > &context, const std::vector< std::string > &paths)
Definition: v8toolkit.cpp:650
auto operator()(Callable &&callable) -> std::result_of_t< Callable(v8::Isolate *)>
Definition: javascript.h:277
virtual ~Script()
Definition: javascript.h:396
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
void add_function(std::string name, Callable &&callable)
Definition: javascript.h:590
boost::uuids::random_generator uuid_generator
Definition: javascript.cpp:10
v8::Isolate *const isolate
shortcut to the v8::isolate object instead of always going through the Isolate
Definition: javascript.h:69
v8::Global< v8::Context > const & get_global_context() const
Definition: javascript.cpp:160
void add_variable(const std::string &name, v8::Local< v8::ObjectTemplate > template_to_attach)
Not sure what this is used for.
Definition: javascript.h:616
void expose_variable(std::string name, Variable &variable)
Definition: javascript.h:321
void require_directory(std::string const &directory_name)
Definition: javascript.cpp:217
void add_module_list(v8::Isolate *isolate, const v8::Local< v8::ObjectTemplate > &object_template)
Definition: v8toolkit.cpp:632
std::shared_ptr< Script > compile(const std::string &source, const std::string &filename="Unspecified")
Definition: javascript.cpp:67
std::shared_ptr< Isolate > get_isolate_helper() const
Definition: javascript.cpp:35
void register_external_function(v8::Global< v8::Function > external_function)
nlohmann::json json
Definition: debugger.cpp:16
v8::Local< v8::Function > get_function_by_id(int64_t script_id)
void add_module_list()
Definition: javascript.h:529
void add_variable(std::string name, v8::Local< v8::Value > variable)
Definition: javascript.h:310
#define GLOBAL_CONTEXT_SCOPED_RUN(isolate, global_context)
Definition: v8toolkit.h:85
std::thread run_thread(const v8::Global< v8::Script > &script)
auto scoped_run(v8::Isolate *isolate, T callable) -> typename std::result_of< T()>::type
Definition: v8toolkit.h:111
std::shared_ptr< Isolate > IsolatePtr
Definition: javascript.h:651
auto & wrap_class()
Definition: javascript.h:626
std::shared_ptr< Context > ContextPtr
Definition: javascript.h:367
v8::Local< v8::Value > json(const std::string &json)
Definition: javascript.cpp:40
auto operator()(Callable &&callable) -> std::result_of_t< Callable()>
Definition: javascript.h:556
auto operator()(Callable &&callable) -> typename std::result_of< Callable(v8::Isolate *)>::type
Definition: javascript.h:567
v8::Local< v8::Value > wrap_object(T *object)
Definition: javascript.h:699
void add_function(std::string name, Function function)
Definition: javascript.h:299
v8::Local< v8::Context > get_context() const
Definition: javascript.cpp:24
v8::Isolate * get_isolate() const
Definition: javascript.cpp:29
std::shared_ptr< Script > ScriptPtr
Definition: javascript.h:26
std::shared_ptr< Script > compile_from_file(const std::string &filename)
Definition: javascript.cpp:54
void add_function(v8::Isolate *isolate, const v8::Local< v8::ObjectTemplate > &object_template, const char *name, func::function< R(Args...)> function)
Definition: v8toolkit.h:431
virtual ~Context()
Definition: javascript.cpp:49
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)
Definition: javascript.cpp:166
Script const & get_script_by_id(int64_t script_id)
#define ISOLATE_SCOPED_RUN(isolate)
Definition: v8toolkit.h:93
std::string get_url(std::string const &name) const
Definition: javascript.cpp:200
void add_variable(v8::Isolate *isolate, const v8::Local< v8::ObjectTemplate > &object_template, const char *name, const v8::Local< v8::Data > value)
Definition: v8toolkit.cpp:65
void add_print(v8::Isolate *isolate, v8::Local< v8::ObjectTemplate > object_template, func::function< void(const std::string &)>=[](const std::string &s){printf("%s", s.c_str());})
Definition: v8toolkit.cpp:166
std::string get_uuid_string() const
Definition: javascript.cpp:195
void expose_variable(std::string name, Variable &variable)
Definition: javascript.h:604