Quantcast

Passing Arrays to JS-XPCOM from C++

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Passing Arrays to JS-XPCOM from C++

shansymantec
Hi,


I have to pass an array of strings (or JS::Value array) to JS-XPCOM from my C++ code. I could not find the proper example in C++ to implements this.

Based on the "jsFunction" I am passing in JS-XPCOM, i will extract the correct number of values in the JS and call the expected function.

How to pass the array of JS::Value to JS::HandleValue in the C++ fn ? Your help is highly appreciated


1 - My IDL as below

#include "nsISupports.idl"
#include "nsIDOMWindow.idl"
[scriptable, uuid(e583b11e-2043-11e4-9070-fd8b1d5d46b0)]
interface nsISbCollectURL: nsISupports
{
 jsval callJSXPCOMFunction(in nsIDOMWindow domWindow, in wstring jsFunction, in unsigned short argc, in jsval argv);
 
};  

2 My "C++" Header file has the following signature (after compiled by /gecko-sdk/xulrunner-sdk/sdk/bin/typelib.py )

  NS_IMETHOD CallJSXPCOMFunction(nsIDOMWindow *domWindow, const char16_t * jsFunction, uint16_t argc, JS::HandleValue argv, JS::MutableHandleValue _retval) = 0;

--> How to convert the populate JS::Value arrays to "JS::HandleValue param" in the method signature? I got stuck here. <---


3 - MY JS-XPCOM as below

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function MyComponent() { }

MyComponent.prototype = {
        classDescription: "test JS XPCOM Component",
        classID:          Components.ID("{8f1a877e-2045-11e4-b92a-1b8d1d5d46b0}"),
        contractID:       "@test.com/mytest/jssource;1",
        QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsISbCollectURL,Components.interfaces.nsISupports]),

  callJSXPCOMFunction: function(win, jsFunction, argc,  argv)
        {
                win.wrappedJSObject.alert("Coming to callJSXPCOMFunction");
                win.wrappedJSObject.alert(argc);
                if(jsFunction == "readMe")
                {
                       
                        win.wrappedJSObject.alert("Coming to readMe");
                        var name ;
                        var age;
                        var hobby;
                        if(argc >= 1)
                                var name = argv[0];
                        if(argc >= 2)
                                var age = argv[1];
                        if(argc >= 3)
                                var hobby = argv[2];
                        var as  = "shan : name : " +name +  " age " + age  + "  hobby : " + hobby);
                        var myData = win.wrappedJSObject.shan_readme(name,  age, hobby);
                        return myData;
                       
                }else if(jsFunction == "writeMe"){
                        win.wrappedJSObject.alert("Coming to writeMe");
                        if(argc >= 1)
                                var xmlStuff =  argv[0];
                         
                        win.wrappedJSObject.alert(xmldata);
                        return win.wrappedJSObject.shan_rwriteme(xmldata);
                }
        }
         
};


var components = [MyComponent];

if ("generateNSGetFactory" in XPCOMUtils)
 var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);  
else
 var NSGetModule = XPCOMUtils.generateNSGetModule(components);    
       

Thanks
Shan
_______________________________________________
dev-tech-xpcom mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xpcom
Loading...