npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bridjs

v0.5.8

Published

V8 bindings for dyncall, and BridJ-like API for nodejs.

Downloads

24

Readme

BridJS

BridJS is a BridJ-like API for runtime binding C function and struct without writing any extra native code.

###Key features

  • BridJS binds native function at runtime, you never need to compile any extra native code
  • Uncompromized speed and power by dyncall
  • Support implicit type wrapping/unwrapping (struct<->pointer and string<->number etc... )
  • Support complex struct type (sub-struct and array field), and access theme straightforwadly.
  • Execute any native function either by synchronous or by asynchronous way
  • Whatever the native callbacks are invoked by any other thread, BridJS always forward callback to V8's default thread

###Limitation Like BridJ, BridJS also has some limitations:

  • Pass structs by value not supported yet (neither as function arguments nor as function return values)
  • BridJS does not support C++, COM, Objective-C...

###Requirement

  • nodejs v0.10.8 or higher
  • Windows x64, Linux x86/x64 & Mac OSX

###Installation If node.js version is higher or equal than v0.12.0:

npm install bridjs

If node.js version is lower or equal than v0.10.38:

npm install [email protected]

###Tutorial ####1. C function

If C code is something like this:

double testMultiplyFunction(const int16_t w, const int32_t x,const long y, const LONGLONG z, const double e);

You can define JavaScript prototype like this:

var bridjs = require('bridjs');

var NativeModule = bridjs.defineModule({
    testMultiplyFunction:  bridjs.defineFunction("double testMultiplyFunction(int16_t,int32_t ,long ,longlong , double)")
    }, libraryPath);
    
var nativeModule = new NativeModule();

var result = nativeModule.testMultiplyFunction(2,2,2,2,2.5);

Bind C function API

bridjs.defineModule({
  functionName1: bridjs.DefineFunction(returnType, arg0Type, arg2Type...),
  //Or
  functionName2: bridjs.DefineFunction("function declaration in C"),
  ...
},libraryFile);

####2. C struct

If C code is something like this:

typedef struct{
  double x;
  double y;
  double z;
} Point3d;

tydef struct{
  char x;
  Point3d y;
  char str[10];
} ComplexStruct

double testComplexStructFunction(const ComplexStruct* pTestStruct)

You can define JavaScript prototype like this:

var Point3d = bridjs.defineStruct({
    x : {type: "double", order: 0},
    y : {type: "double", order: 1},
    z : {type: "double", order: 2}
});

var ComplexStruct = bridjs.defineStruct({
    x:{type: "char", order: 0},
    y:{type: Point3d, order: 1},
    z:{type: "char[10]", order: 2}
});

var NativeModule = bridjs.defineModule({
    testComplexStructFunction : bridjs.defineFunction("double testComplexStructFunction(ComplexStruct*)"}, libraryPath);

var complexStruct = new ComplexStruct();
var nativeModule = new NativeModule();

complexStruct.x = 's';
complexStruct.y.x = 2;
complexStruct.str.set(3) = 's';

var result = nativeModule.testComplexStructFunction(bridjs.byPointer(complexStruct));

Bind C struct API

bridjs.defineStruct({
    element1 : bridjs.structField(elementType,order),
    //Or
    element2 : {type: "<elementType>", order: <order>},
    element3 : bridjs.structArrayField(arrayType,arrayLength,order)
    ...
});

####3. Invoke native function asynchronously

/*You can execute any native function asynchronously (not in default thread), and get return value from callback*/
bridjs.aysnc(nativeModule).testMultiplyFunction(2,2,2,2,2.5, function(returnValue){
    console.log("Return value = "+returnValue)
});

Async execute native function API

bridjs.async(moduleInstance).function(param1, param2,....callbackFunction);

####4. C function pointer

If C code is something like this:

typedef double (*MultiplyCallbackFunction)(const int16_t w, const int32_t x,const long y, const LONGLONG z, const double e);
void testCallbackFunction(MultiplyCallbackFunction callbackFunction);

You can define JavaScript prototype like this:

var callbackFunctionDefine = bridjs.defineFunction("double (int16_t, int32_t, long, longlong, double)");

var callback = bridjs.newCallback(callbackFunctionDefine, function(w, x, y, z, e) {
        console.log("Callback function was invoked");
    
        return w*x*y*z*e;
});

var NativeModule = bridjs.defineModule({
    testCallbackFunction : bridjs.defineFunction("void testCallbackFunction(MultiplyCallbackFunction callbackFunction)", {MultiplyCallbackFunction:callbackFunctionDefine})
    }, libraryPath);

var nativeModule = new NativeModule();

nativeModule.testAsyncCallbackFunction(callback);    

Create function pointer API

bridjs.newCallback(functionSignature,callbackFunction);

####5. Pass primitive type by pointer

If C code is something like this:

const double* testValuePassByPointerFunction(const double *returnValue);

You can define JavaScript prototype like this:

var NativeModule = bridjs.defineModule({
    testValuePassByPointerFunction:  bridjs.defineFunction("double* testValuePassByPointerFunction(double*)")
    }, libraryPath);

var nativeDouble = new bridjs.NativeValue.double(2.5);  

var nativeModule = new NativeModule();

var returnNativeDouble = nativeModule.testValuePassByPointerFunction(bridjs.byPointer(nativeDouble));    

var result = returnNativeDouble.get();

###License

BSD License. See the LICENSE file.