Search code examples
javascriptpluginsnpapifirebreathjsctypes

Access Javascript Array in C++/Plugin?


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 ?


Solution

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