Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
/ v8bind Public archive

Helpers for wrapping C++ classes -> V8

License

Notifications You must be signed in to change notification settings

ayles/v8bind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

v8bind

Header-only library for binding C++ classes and functions to V8 JS environment.

It was inspired by v8pp and adds support for binding overloaded functions & constructors (relies on template metaprogramming with recursive instantiation and requires compiler with C++17 support).

Can work without RTTI.

Tested on:

  • Microsoft Visual C++ 2019 (Windows 10)
  • GCC 7.4 and 8.3 (Ubuntu 18.04)
  • Clang 6 (Ubuntu 18.04)

Example

v8::Isolate *isolate = ...;
auto context = isolate->GetCurrentContext();

struct Test {
    int foo;
    const double bar;

    Test(int foo, double bar) : foo(foo), bar(bar) {}
    Test() : Test(0, 0.0) {}

    std::string ToString() {
        return "(" + std::to_string(foo) + ", " 
                   + std::to_string(bar) + ")";
    }

    int Sum() {
        return foo + bar;
    }

    int Sum(long third) {
        return foo + bar + third;
    }
};

v8b::Class<Test> my_class(isolate);
my_class

    // Use tuple with arguments to add constructor signature
    .Constructor<std::tuple<>, std::tuple<int, double>>()

    // Regular variable binding
    .Var("foo", &Test::foo)

    // Const will be readonly in JS
    .Var("bar", &Test::bar)
    
    // Bind function without overloads just by passing it
    .Function("toString", &Test::ToString)
    
    // Binding overloaded function requires type hints 
    // (through template parameters or via static_cast)
    //
    // Binding order is not important until you use
    // different functions that can match same arguments set
    // (for example (double, double) and (int, int))
    // or void (const v8::FunctionCallbackInfo<v8::Value> &)
    // that will match any arguments set
    // In such case place functions in preffered lookup order
    .Function<int (Test::*)(), int (Test::*)(long)>
        ("sum", &Test::Sum, &Test::Sum)
;

v8b::Module my_module(isolate);
my_module.Class("Test", my_class);

// Set to global object
context->Global()->Set(
    context, v8b::ToV8(isolate, "myModule"), my_module.NewInstance());

Use it in JS:

let test = new myModule.Test();
let test2 = new myModule.Test(1, 2);

console.log(test);
console.log(test2);

console.log(test.sum());
console.log(test2.sum(1));

test.foo = 123;

// Cannot assign to read only property 'bar' of object '[object Test]'
test.bar = 345;

About

Helpers for wrapping C++ classes -> V8

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published