![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Streamlined C/C++ v8 bindings generator for Node.JS inspired by tolua++ (requires C++11)
Streamlined automatic C/C++ v8 bindings generator for Node.JS inspired by tolua++ (requires C++11)
Core idea is the ability to easily bind to C/C++ by providing a header-like description of the native interface that is a subset of C++ (format is called Native Interface Description https://github.com/CodeCharmLtd/NID):
Simple things:
int global_variable;
std::string global_function(int a, float b);
struct Rect {
int x;
int y;
int w;
int h;
};
bool rectangles_intersect(Rect a, Rect b);
More complex things:
#include <stdio.h>
int errno; // make errno available as getter and setter
// make fopen available and mark FILE* pointers for automatic closing when gced
FILE * [[free(fclose)]] fopen(const char* filename, const char* mode);
// make fread available and automatically set one of its arguments
size_t fread(void* ptr [[buffer]], size_t size, size_t nmemb [[set(ptr.length / size)]], FILE* stream [[handle]]);
// tell cbind to mark the object containing pointer as invalid
int fclose(FILE* f [[clear_free,unref]]);
## Complex weird cases function pointer handling
Consider this declaration (valid C and valid NID):
```c++
void (*catch_and_return(void (*callback)(char* a, char* b, int* c), char *name_one, char* name_two, int* number))(char* a, char * b, int c);
```
Do you know what it does? Actually it is a declaration of function taking 4 arguments:
* `callback` - pointer to function `void(char* a, char* b, int* c)`
* `name_one` - string
* `name_two` - string
* `number` - pointer to number
This function returns another function pointer void(char* a, char* b, int)
It turns out cbind can generate bindings for this that fully work:
bindings.catch_and_return(function(a, b, c) {
console.log(a, b, cBind.derefInt(c));
}, "str1", "str2", cBind.createInt(30))("FINAL", "TEST", 44);
npm install --save cbind
example.nid
file
Put your native interface definition there, for example:void hello(int b);
binding.gyp
follow this example:{
"targets": [
{
"target_name": "cbind_example",
"sources": [
"src/addon.cc"
],
"include_dirs" : [
"<!(node -e \"require('nan')\" 2> /dev/null)",
"<!(node -e \"require('cbind')('example.nid')\" 2> /dev/null)"
],
"cflags": ["-g", "-std=c++11"],
"cflags_cc!": [ '-fno-exceptions' ]
}
]
}
addon.cc
the following content
#include <cstdio>
void hello(int a) {
printf("Hello world: %i\n", a);
}
#include <cbind_example.h>
void init(v8::Handle<v8::Object> exports) {
cbind::init_example(exports);
}
NODE_MODULE(tov8_example, init);
Run node-gyp rebuild
With hello.js having the following content:
var bindings = require('./build/Release/cbind_example.node');
bindings = hello(42);
node hello.js
should print "Hello world 42`The above is in the repository https://github.com/CodeCharmLtd/cbind-example
Please open issues or follow example tests. README will be gradually expanded.
https://github.com/celer/fire-ts - awesome templates for code generation
## Authors * Damian Kaczmarek ## Sponsors Please open an issue if you would like a specific feature to be implemented and sponsored. ## License Copyright (c) 2013-2014 [Code Charm Ltd](http://codecharm.co.uk)Licensed under the MIT license, see LICENSE
for details.
FAQs
Streamlined C/C++ v8 bindings generator for Node.JS inspired by tolua++ (requires C++11)
We found that cbind demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.