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 ?
Asked
Active
Viewed 532 times
1 Answers
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:
- Javascript objects (with methods and/or value members)
- Javascript Arrays (that you plan to modify; otherwise you can use a container type)
- 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.

taxilian
- 14,229
- 4
- 34
- 73
-
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. – lucent Sep 11 '12 at 12:17
-
1As 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. – taxilian Sep 11 '12 at 16:10
-
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
() + B->GetProperty(i).cast – lucent Dec 12 '12 at 18:24(); 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 – taxilian Dec 12 '12 at 19:12
-
OK. Sorry about that. I am posting a new detailed question right-away – lucent Dec 12 '12 at 19:20