Search code examples
npapifirebreath

Firebreath NPAPI plugin pass javascript arrays by reference


I am new with using Firebreath. As an exercise I am trying to write a plugin that does a vector addition. For some reason I am struggling with that. I will relevant code of what I am trying to do.

This is my Javascript code.

    var problemsize = 512 * 100 
var allocsize   = problemsize * 8

var bufferA = new ArrayBuffer(allocsize);
var bufferB = new ArrayBuffer(allocsize);
var bufferC = new ArrayBuffer(allocsize);
var N = problemsize

var float32ViewA = new Float32Array(bufferA);
var float32ViewB = new Float32Array(bufferB);
var float32ViewC = new Float32Array(bufferC);

for (var i = 0; i < problemsize; i++) {
    float32ViewA[i] = 4;
    float32ViewB[i] = 3;
    float32ViewC[i] = 0;
}

plugin().addvect(float32ViewA.buffer,float32ViewB.buffer,float32ViewC.buffer,problemsize);

And code in my plugin API.cpp

void TestJSAPI::addvect(const FB::JSObjectPtr& A, const FB::JSObjectPtr& B, const  FB::JSObjectPtr& C, int N){

std::cout << "Entering TestJSAPI::addvect " << std::endl;


int i = 0;
for( i = 0 ; i < N ; i++){
    double val = A->GetProperty(i).cast<double>() + B->GetProperty(i).cast<double>();
    C->SetProperty(i,val);

}


std::cout << "Exiting TestJSAPI::addvect" << std::endl;

}

I can't seem to figure what's going wrong. I am starting firefox from console and it just prints Entering... and Not Exiting.. What am I missing here ? Thanks


Solution

  • First of all you shouldn't be using .cast, you should use .convert_cast. If you use .cast it will only work if it happens to actually be a double which may or may not be the case. Remember that NPAPI doesn't know anything about the typed arrays / buffers that you're using.

    My guess is that the reason that you're not getting anywhere is that there is a FB::variant::bad_variant_cast exception being thrown (I think tha'ts the correct exception type; look in variant.h to be sure) on the cast or possibly your GetProperty is failing and throwing FB::script_error.