xaxxon / v8toolkit

Library for building hybrid C++/JavaScript applications with the V8 JavaScript engine
MIT License
48 stars 6 forks source link
cmake cpp cpp17 embedded javascript

v8toolkit

v8toolkit is a library for easily integrating JavaScript with C++. Just tell v8toolkit what parts of your C++ you want it to expose to JavaScript and v8toolkit takes care of the rest -- or you can annotate your C++ code inline and use the included clang plugin to scan your code and generate the JavaScript bindings automatically.

Features

Requirements

Doxygen docs available here: http://xaxxon.github.io/v8toolkit/docs/doxygen/html/index.html

New Feature!

Auto-generation of PIMPL members.

Annotate the main class, put a friend on it, and all the members of the PIMPL member pointer (subject to the usual rules/exceptions) will be exposed as part of the main wrapped class.

Additionally, add_member(_readonly) can now expose any variable - just provide it with a function callback which returns the variable you want exposed when given an object pointer.

Examples

Your first v8toolkit program

    #include <v8toolkit/javascript.h>
    using v8toolkit;

   int main(int argc, char ** argv) {
      // one-time initialization
      Platform::init(argc, argv, argv[0]); 

      // An isolate is the top-level JavaScript container.
      auto isolate = Platform::create_isolate();
      isolate->add_print(); // JavaScript can't print to standard out on its own, so this adds some custom functions

      // a context is the environment in which JavaScript code is executed and belongs to an Isolate
      auto context = isolate->create_context();

      // prints "Hello JavaScript World!" to standard out.
      context->run("println('Hello JavaScript World!');");
    }

Exposing a C++ class to JavaScript manually

Here is an example class and the code required to make the class useable from JavaScript:

    class MyClassToWrap : public v8toolkit::WrappedClassBase {
    public:
        void member_function();
        void member_function_not_exposed();

        static void static_method();

        std::string data_member;
        int const const_data_member;

        MyClassToWrap(int i);

    };

   void create_class_bindings(v8toolkit::Isolate & isolate) {
        auto & class_wrapper = isolate.wrap_class<MyClassToWrap>();
        class_wrapper.add_function("a_member_function", &MyClassToWrap::a_member_function);
        class_wrapper.add_static_function("static_method", &MyClassToWrap::static_method);
        class_wrapper.add_member<&MyClassToWrap::data_member>("data_member");
        class_wrapper.finalize();
        class_wrapper.add_contructor<int>("MyClassWrapper", *i);
   }

Automatic generation of JavaScript bindings for a C++ class

By annotating your C++ class (note the V8TOOLKIT_SKIP in the code below), you can generate the exact same bindings as above automatically using the clang plugin.

    class MyClassToWrap : public v8toolkit::WrappedClassBase {
    public:
        void member_function();
        V8TOOLKIT_SKIP void member_function_not_exposed(); 

        static void static_method();

        std::string data_member;
        int const const_data_member;

        MyClassToWrap(int i);

    };