0.5.8 • Published 6 years ago

bridjs v0.5.8

Weekly downloads
192
License
-
Repository
github
Last release
6 years ago

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 bridjs@0.1.9-3

###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.

0.5.8

6 years ago

0.5.7

6 years ago

0.5.6

8 years ago

0.5.5

8 years ago

0.5.4

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.0

8 years ago

0.3.9

8 years ago

0.3.8

8 years ago

0.3.7

8 years ago

0.3.6

8 years ago

0.3.5

8 years ago

0.3.4-5

8 years ago

0.3.4-4

9 years ago

0.3.4-3

9 years ago

0.3.4-2

9 years ago

0.3.4-1

9 years ago

0.3.3-5

9 years ago

0.3.3-4

9 years ago

0.3.3-3

9 years ago

0.3.3-2

9 years ago

0.3.3-1

9 years ago

0.3.2-5

9 years ago

0.3.2-4

9 years ago

0.3.2-3

9 years ago

0.3.2-2

9 years ago

0.3.2-1

9 years ago

0.3.1-1

9 years ago

0.2.1-1

9 years ago

0.1.9-3

9 years ago

0.1.9-2

9 years ago

0.1.9-1

9 years ago

0.2.0-9

9 years ago

0.2.0-8

9 years ago

0.2.0-7

9 years ago

0.2.0-6

9 years ago

0.2.0-5

9 years ago

0.2.0-4

9 years ago

0.2.0-3

9 years ago

0.2.0-2

9 years ago

0.2.0-1

9 years ago

0.1.8-2

10 years ago

0.1.8-1

10 years ago

0.1.7-3

10 years ago

0.1.7-2

10 years ago

0.1.7-1

10 years ago