Skip to content

Repository files navigation

Puerts Node.js Addon: High-Performance Bridge between C++ and JavaScript

Welcome to our Node.js addon, powered by Puerts. This high-performance tool allows you to bind C++ functions and classes to the V8 JavaScript engine, enabling seamless and efficient use of C++ code in your JavaScript environment.

Installation

You can install puerts using npm:

npm install -g puerts

Example

C++ code to be call

classHelloWorld
{
public:HelloWorld(int p) {
Field = p;
}
voidFoo(std::function<bool(int, int)> cmp) {
bool ret = cmp(Field, StaticField);
std::cout << "Foo, Field: " << Field << ", StaticField: " << StaticField << ", compare result:" << ret << std::endl;
}
staticintBar(std::string str) {
std::cout << "Bar, str:" << str << std::endl;
return StaticField + 1;
}
int Field;
staticint StaticField;
};
int HelloWorld::StaticField = 0;

Export C++ API

UsingCppType(HelloWorld);
voidInit() {
puerts::DefineClass<HelloWorld>()
.Constructor<int>()
.Method("Foo", MakeFunction(&HelloWorld::Foo))
.Function("Bar", MakeFunction(&HelloWorld::Bar))
.Property("Field", MakeProperty(&HelloWorld::Field))
.Variable("StaticField", MakeVariable(&HelloWorld::StaticField))
.Register();
}
//hello_world is module name, will use in js later.PESAPI_MODULE(hello_world, Init)

ps: Above C++ example project can be generate by below command line:

puerts init hello_world

Compile the generated addon project:

cd hello_world
mkdir build
cd build
cmake ..
cmake --build . --config Release

Calling addon in JavaScript

constpuerts=require("puerts");lethello_world=puerts.load('path/to/hello_world');constHelloWorld=hello_world.HelloWorld;constobj=newHelloWorld(101);obj.Foo((x,y)=>x>y);HelloWorld.Bar("hello");HelloWorld.StaticField=999;obj.Field=888;obj.Foo((x,y)=>x>y);

TypeScript support

Generate typescript declaration file (index.d.ts) for a puerts addon

puerts gen_dts path\to\hello_world -t typing

Add typing directory to tsconfig.json/compilerOptions/typeRoots.

Calling addon in TypeScript

import{load}from"puerts";import*asHelloWorldModluefrom'hello_world'lethello_world=load<typeofHelloWorldModlue>('path/to/hello_world');constHelloWorld=hello_world.HelloWorld;constobj=newHelloWorld(101);obj.Foo((x,y)=>x>y);HelloWorld.Bar("hello");HelloWorld.StaticField=999;obj.Field=888;obj.Foo((x,y)=>x>y);

Function & constructor overloading

classTestClass : publicBaseClass
{
public:TestClass();
TestClass(int32_t InX, int32_t InY);
staticvoidOverload();
staticvoidOverload(std::string a, int32_t b);
int32_tOverloadMethod();
int32_tOverloadMethod(int32_t a);
};
puerts::DefineClass<TestClass>()
.Extends<BaseClass>()
.Constructor(CombineConstructors(
MakeConstructor(TestClass, int32_t, int32_t),
MakeConstructor(TestClass)
))
.Function("Overload", CombineOverloads(
MakeOverload(void(*)(), &TestClass::Overload),
MakeOverload(void(*)(std::string, int32_t), &TestClass::Overload)
))
.Method("OverloadMethod", CombineOverloads(
MakeOverload(int32_t(TestClass::*)(), &TestClass::OverloadMethod),
MakeOverload(int32_t(TestClass::*)(int32_t), &TestClass::OverloadMethod)
))
.Register();

Features

The following C++ features are supported: constructors, inheritance, member variables, member functions, static functions, static variables, function overloading (constructors/static functions/member functions)

Supports the generation of TypeScript declarations

For more examples, see: https://github.com/puerts/puerts_addon_demos

Performance

If you want to achieve higher performance, please add the PES_EXTENSION_WITH_V8_API macro during compilation. If you want to achieve the highest performance, add an additional WITH_V8_FAST_CALL and include the --turbo-fast-api-calls parameter when starting Node.js. For performance test data, please see here.

About

Bind C++ functions and classes into Node.js.

Resources

Stars

16 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages