v8toolkit  0.0.1
Utility library for embedding V8 Javascript engine in a c++ program
Classes | Namespaces | Macros | Functions
bidirectional.h File Reference
#include <string>
#include "v8toolkit.h"
#include "v8_class_wrapper.h"
Include dependency graph for bidirectional.h:

Go to the source code of this file.

Classes

class  v8toolkit::BidirectionalException
 
class  v8toolkit::JSWrapperBase
 
class  v8toolkit::JSWrapper< T >
 
struct  v8toolkit::CastToJS< JSWrapper< T > >
 
class  v8toolkit::EmptyFactoryBase
 
class  v8toolkit::Factory< Base, class, FactoryBase, Deleter >
 
class  v8toolkit::Factory< Base, TypeList< ConstructorArgs... >, FactoryBase, Deleter >
 
class  v8toolkit::CppFactory< Base, Child, ExternalTypeList, ParentType, FactoryBase >
 
class  v8toolkit::CppFactory< Base, Child, TypeList< FixedParams... >, TypeList< ExternalConstructorParams... >, FactoryBase, Deleter >
 
class  v8toolkit::JSFactory< Base, JSWrapperClass, Internal, External, FactoryBase, Deleter >
 
class  v8toolkit::JSFactory< Base, JSWrapperClass, TypeList< InternalConstructorParams... >, TypeList< ExternalConstructorParams... >, FactoryBase, Deleter >
 

Namespaces

 v8toolkit
 

Macros

#define V8TOOLKIT_BIDIRECTIONAL_ENABLED
 
#define JS_ACCESS_CORE_DEBUG   false
 
#define JS_ACCESS_CORE(ReturnType, name, ...)
 
#define JS_ACCESS(return_type, name)
 
#define JS_ACCESS_0(return_type, name)
 
#define JS_ACCESS_1(return_type, name, t1)
 
#define JS_ACCESS_2(return_type, name, t1, t2)
 
#define JS_ACCESS_3(return_type, name, t1, t2, t3)
 
#define JS_ACCESS_4(return_type, name, t1, t2, t3, t4)
 
#define JS_ACCESS_5(return_type, name, t1, t2, t3, t4, t5)
 
#define JS_ACCESS_6(return_type, name, t1, t2, t3, t4, t5, t6)
 
#define JS_ACCESS_7(return_type, name, t1, t2, t3, t4, t5, t6, t7)
 
#define JS_ACCESS_8(return_type, name, t1, t2, t3, t4, t5, t6, t7, t8)
 
#define JS_ACCESS_9(return_type, name, t1, t2, t3, t4, t5, t6, t7, t8, t9)
 
#define JS_ACCESS_CONST(return_type, name)
 
#define JS_ACCESS_0_CONST(return_type, name)
 
#define JS_ACCESS_1_CONST(return_type, name, t1)
 
#define JS_ACCESS_2_CONST(return_type, name, t1, t2)
 
#define JS_ACCESS_3_CONST(return_type, name, t1, t2, t3)
 
#define JS_ACCESS_4_CONST(return_type, name, t1, t2, t3, t4)
 
#define JS_ACCESS_5_CONST(return_type, name, t1, t2, t3, t4, t5)
 
#define JS_ACCESS_6_CONST(return_type, name, t1, t2, t3, t4, t5, t6)
 
#define JS_ACCESS_7_CONST(return_type, name, t1, t2, t3, t4, t5, t6, t7)
 
#define JS_ACCESS_8_CONST(return_type, name, t1, t2, t3, t4, t5, t6, t7, t8)
 
#define JS_ACCESS_9_CONST(return_type, name, t1, t2, t3, t4, t5, t6, t7, t8, t9)
 

Functions

template<class T >
std::enable_if_t< std::is_base_of< JSWrapperBase, T >::value, v8::Local< v8::Object > > v8toolkit::safe_get_javascript_object (T *object)
 
template<class T >
std::enable_if_t<!std::is_base_of< JSWrapperBase, T >::value, v8::Local< v8::Object > > v8toolkit::safe_get_javascript_object (T *object)
 

Class Documentation

class v8toolkit::EmptyFactoryBase

Class for Factory to inherit from when no other parent is specified Must be empty

Definition at line 63 of file sample2.h.

class v8toolkit::Factory

template<class Base, class = TypeList<>, class FactoryBase = EmptyFactoryBase, class Deleter = std::default_delete<Base>>
class v8toolkit::Factory< Base, class, FactoryBase, Deleter >

Common Factory Inheritance goes: v8toolkit::CppFactory => custom user-defined Factory type for additional data/functions => v8toolkit::Factory => v8toolkit::Factory<BaseType> => Common base type for all factories Internal constructor parameters are parameters only on the Base type constructor that are not specified to each instance of the derived type. The value is fixed across all isntances of the specific derived type.

External constructor parameters are parameers are all the parameters to the derived type (except an optional first parameter of the factory object which will be automatically populated) Base class for a Factory that creates a bidirectional "type" by name. The object returned can be used as a class T regardless of whether it is a pure C++ type or if it has been extended in javascript

Definition at line 159 of file bidirectional.h.

class v8toolkit::CppFactory

template<class Base, class Child, class ExternalTypeList = TypeList<>, template< class, class... > class ParentType = FlexibleParent, class FactoryBase = EmptyFactoryBase>
class v8toolkit::CppFactory< Base, Child, ExternalTypeList, ParentType, FactoryBase >

Returns a pure-C++ object of type Child which inherits from type Base. It's Base type and ConstructorArgs... must match with the Factory it is associated with. You can have it inherit from a type that inherits from v8toolkit::Factory but v8toolkit::Factory must be in the inheritance chain somewhere

Definition at line 110 of file sample.cpp.

class v8toolkit::JSFactory

template<class Base, class JSWrapperClass, class Internal, class External, class FactoryBase = Factory<Base, External, EmptyFactoryBase>, class Deleter = std::default_delete<JSWrapperClass>>
class v8toolkit::JSFactory< Base, JSWrapperClass, Internal, External, FactoryBase, Deleter >

Returns a JavaScript-extended object inheriting from Base. It's Base type and *ConstructorParams must match up with the Factory class it is associated

InternalConstructorParams are ones that will be specified in the javascript code declaring the new type

ExternalConstructorParams will be potentially change for each instance of that type

Example of internal vs external parameters: if the base type is "animal" and it takes two parameters "is_mammal" and "name". Whether or not the derived type is a mammal is known when making the derived type so it would be an internal parameter, while the name isn't known until the object is constructed so it would be an external parameter.

Perhaps the order should be swapped to take external first, since that is maybe more common?

Definition at line 290 of file bidirectional.h.

Macro Definition Documentation

#define V8TOOLKIT_BIDIRECTIONAL_ENABLED

Definition at line 5 of file bidirectional.h.

#define JS_ACCESS_CORE_DEBUG   false

Definition at line 493 of file bidirectional.h.

#define JS_ACCESS_CORE (   ReturnType,
  name,
  ... 
)
Value:
bool call_native = this->called_from_javascript; \
\
/* If there is infinite recursion happening here, it is likely */ \
/* that when this function looks for a JavaScript function to call */ \
/* it actually finds this function and calls itself. This flag is supposed */ \
/* to protect against that but the situations are complicated and this may not be */ \
/* sufficient. */ \
this->called_from_javascript = false; \
if (call_native) { \
if(JS_ACCESS_CORE_DEBUG) printf("Calling native version of %s\n", #name); \
/* See comment above for why static dispatch is used */ \
return this->BASE_TYPE::name( __VA_ARGS__ ); \
} \
if(JS_ACCESS_CORE_DEBUG) printf("IN JS_ACCESS_CORE for %s, not calling native code\n", #name); \
/*auto parameter_tuple = std::make_tuple( __VA_ARGS__ ); */ \
/* auto parameter_tuple = make_tuple_for_variables(__VA_ARGS__); */ \
v8toolkit::CastToNative<std::remove_reference<ReturnType>::type> cast_to_native; \
GLOBAL_CONTEXT_SCOPED_RUN(isolate, global_context); \
auto context = global_context.Get(isolate); \
auto js_object = global_js_object.Get(isolate); \
v8::Local<v8::Function> js_function; \
v8::TryCatch tc(isolate); \
try { \
js_function = v8toolkit::get_key_as<v8::Function>(context, js_object, #name); \
} catch (...) {assert(((void)"method probably not added to wrapped parent type", false) == true);} \
this->called_from_javascript = true; \
auto result = v8toolkit::call_javascript_function_with_vars(context, js_function, js_object, typelist, ##__VA_ARGS__); \
this->called_from_javascript = false; \
return cast_to_native(isolate, result);
#define JS_ACCESS_CORE_DEBUG

This code looks for a javascript method on the JavaScript object contained in the "this" JSWrapper object and call the "name"d method on it. It must work when this method is called directly to start the method call (using a bidirectional object from C++) as well as when the method call is started from javascript (where the javascript interpreter checks the prototype chain initially and might find this function) If not careful, this function can find itself while looking for a javascript version to call because even though its methods aren't mapped into javascript, the parent type's are and dynamic dispatch will call the derived class's version instead of the base class. That is why static dispatch is specifically used for the C++ fallback case: this->BASE_TYPE::name( __VA_ARGS__ );

Definition at line 507 of file bidirectional.h.

#define JS_ACCESS (   return_type,
  name 
)
Value:
virtual return_type name() override {\
v8toolkit::TypeList<> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 540 of file bidirectional.h.

#define JS_ACCESS_0 (   return_type,
  name 
)
Value:
virtual return_type name() override {\
v8toolkit::TypeList<> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 546 of file bidirectional.h.

#define JS_ACCESS_1 (   return_type,
  name,
  t1 
)
Value:
virtual return_type name(t1 p1) override {\
v8toolkit::TypeList<t1> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 552 of file bidirectional.h.

#define JS_ACCESS_2 (   return_type,
  name,
  t1,
  t2 
)
Value:
virtual return_type name(t1 p1, t2 p2) override { \
v8toolkit::TypeList<t1, t2> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2) \
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 558 of file bidirectional.h.

#define JS_ACCESS_3 (   return_type,
  name,
  t1,
  t2,
  t3 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3) override { \
v8toolkit::TypeList<t1, t2, t3> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3) \
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 564 of file bidirectional.h.

#define JS_ACCESS_4 (   return_type,
  name,
  t1,
  t2,
  t3,
  t4 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4) override {\
v8toolkit::TypeList<t1, t2, t3, t4> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 570 of file bidirectional.h.

#define JS_ACCESS_5 (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 576 of file bidirectional.h.

#define JS_ACCESS_6 (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6) override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 582 of file bidirectional.h.

#define JS_ACCESS_7 (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6,
  t7 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7) override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6, t7> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6, p7)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 588 of file bidirectional.h.

#define JS_ACCESS_8 (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6,
  t7,
  t8 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8) override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6, t7, t8> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6, p7, p8)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 594 of file bidirectional.h.

#define JS_ACCESS_9 (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6,
  t7,
  t8,
  t9 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9) override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6, t7, t8, t9> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6, p7, p8, p9)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 600 of file bidirectional.h.

#define JS_ACCESS_CONST (   return_type,
  name 
)
Value:
virtual return_type name() const override {\
v8toolkit::TypeList<> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 606 of file bidirectional.h.

#define JS_ACCESS_0_CONST (   return_type,
  name 
)
Value:
virtual return_type name() const override {\
v8toolkit::TypeList<> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 612 of file bidirectional.h.

#define JS_ACCESS_1_CONST (   return_type,
  name,
  t1 
)
Value:
virtual return_type name(t1 p1) const override {\
v8toolkit::TypeList<t1> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 619 of file bidirectional.h.

#define JS_ACCESS_2_CONST (   return_type,
  name,
  t1,
  t2 
)
Value:
virtual return_type name(t1 p1, t2 p2) const override {\
v8toolkit::TypeList<t1, t2> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 625 of file bidirectional.h.

#define JS_ACCESS_3_CONST (   return_type,
  name,
  t1,
  t2,
  t3 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3) const override {\
v8toolkit::TypeList<t1, t2, t3> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 631 of file bidirectional.h.

#define JS_ACCESS_4_CONST (   return_type,
  name,
  t1,
  t2,
  t3,
  t4 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4) const override {\
v8toolkit::TypeList<t1, t2, t3, t4> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 637 of file bidirectional.h.

#define JS_ACCESS_5_CONST (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5) const override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 643 of file bidirectional.h.

#define JS_ACCESS_6_CONST (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6) const override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 649 of file bidirectional.h.

#define JS_ACCESS_7_CONST (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6,
  t7 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7) const override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6, t7> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6, p7)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 655 of file bidirectional.h.

#define JS_ACCESS_8_CONST (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6,
  t7,
  t8 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8) const override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6, t7, t8> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6, p7, p8)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 661 of file bidirectional.h.

#define JS_ACCESS_9_CONST (   return_type,
  name,
  t1,
  t2,
  t3,
  t4,
  t5,
  t6,
  t7,
  t8,
  t9 
)
Value:
virtual return_type name(t1 p1, t2 p2, t3 p3, t4 p4, t5 p5, t6 p6, t7 p7, t8 p8, t9 p9) const override {\
v8toolkit::TypeList<t1, t2, t3, t4, t5, t6, t7, t8, t9> typelist; \
JS_ACCESS_CORE(V8TOOLKIT_MACRO_TYPE(return_type), name, p1, p2, p3, p4, p5, p6, p7, p8, p9)\
}
#define V8TOOLKIT_MACRO_TYPE(...)
Definition: v8helpers.h:134

Definition at line 667 of file bidirectional.h.