0

I have written a NPAPI Plugin using firebreath framework. I am able to pass simple numeric values from Javascript and access them in my (C++)plugin, perform operations and then return the result. I would like to know how to operate on vectors now i.e arrays. I do not want to allocate new array inside my plugin and copy the array from JavaScript(Although I have no clue on how to do it). How can I directly access the JavaScript array in my plugin ? Is there a special way to do it ?

1 Answer 1

1

From the Firebreath website:

Javascript objects can be used with the FB::JSObjectPtr type. Examples of JavaScript objects that you may want to use include:

  1. Javascript objects (with methods and/or value members)
  2. Javascript Arrays (that you plan to modify; otherwise you can use a container type)
  3. Javascript methods for callback

Arrays are objects; get values with getProperty(n) or getProperty("length") etc

You can also use methods like ->invoke("push", FB::variant_list_of(val)) etc

(on the JSAPI method in order to use the JSObjectPtr type you should use a method that looks something like:)

void doSomethingWithAnArray(const FB::JSObjectPtr& array) { ... }

Also remember that FireBreath can't tell what type of js object it is, just that there is a js object; you'll have to do your own error detection for the case where they don't give you an actual array but some other object instead.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot taxilian. I am even considering using js-ctypes(which apparently is much simpler). I know js-ctypes is mozilla specific and used in extensions. Do you see any difference i.e in what way one is better than the other ? I do not have any strict requirement on whether it should be extension or plugin. I just need to able to access native library.
As a general rule of thumb, if something other than a plugin will do a job, do it that way. Plugins should not be used unless there are no other viable options for your application. That said, I've never used js-ctypes; I don't do extensions. The main downside is they lock you into a specific browser unless you write multiple versions.
Hi taxilian, I am trying to do it this way in firebreath. But it doesn't work. Can you please let me know what's wrong ? ` void TestJSAPI::addvect(const FB::JSObjectPtr A, const FB::JSObjectPtr B, FB::JSObjectPtr& C, int N){ int i = 0; for(i = 0; i < N / 2; i++){ std::cout << "Adding" << std::endl; double sum = A->GetProperty(i).cast<double>() + B->GetProperty(i).cast<double>(); C->SetProperty(i,sum); }}`
new questions should be ask as a new question, not in a difficult to read comment; most likely, though, you need to make those "const FB::JSObjectPtr&" (with the reference) not just const. I can't read it well enough to guess what else it could be
OK. Sorry about that. I am posting a new detailed question right-away

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.