Returning a JS Array to JavaScript from a C++/JavaScript XPCOM object access through XPConnect

Cited from

http://groups.google.co.in/group/mozilla.dev.tech.xpcom/browse_thread/thread/

37b18383a15b82a7/b26a4e4568c3caf4?lnk=gst&q=nsiarray&rnum=2&hl=en#b26a4e4568c3caf4

// XPIDL

void getArray(out unsigned long count, [array, size_is(count), retval] out long retv);

// C++  XPCOM

NS_IMETHODIMP MyComponent::GetArray(PRUint32* count, PRInt32** retv) {

    *count = 10;

    *retv = (PRInt32*)nsMemory::Alloc(*count * sizeof(PRInt32));

    for (int i = 0; i < 10; ++i) (*retv)[i] = i;

    return NS_OK;

}

// JavaScript  XPCOM

Component.prototype.getArray = function(count, retv) {

    count.value = 10; // You must assign the length of array to count.

    var array = new Array();

    ……..

    return array;

}

// JavaScript Calling

var arr = myComponent.getArray({});

Blogged with Flock

Advertisements

9 Responses to “Returning a JS Array to JavaScript from a C++/JavaScript XPCOM object access through XPConnect”

  1. praveenmatanam Says:

    // XPIDL
    nsIArray getArray();

    // C++ XPCOM
    /* nsIArray getArray (); */
    NS_IMETHODIMP MyComponent::GetArray(nsIArray **_retval)
    {
    unsigned int i = 0;

    unsigned int num = 5;

    nsresult rv = NS_OK;
    nsCOMPtr myMutableArray = do_CreateInstance(“@mozilla.org/array;1”);

    for (i = 0; i < num; i++)
    {
    char *name = “cString”;

    nsCOMPtr strArg;
    strArg = do_CreateInstance(“@mozilla.org/supports-cstring;1”, &rv);
    NS_ENSURE_TRUE(strArg, NS_ERROR_FAILURE);
    strArg->SetData(nsCString(name));

    rv = myMutableArray->AppendElement(strArg, PR_FALSE);
    NS_ENSURE_SUCCESS(rv, rv);
    }

    *_retval = myMutableArray;
    NS_ADDREF(*_retval);

    return NS_OK;
    }

    // JavaScript Calling
    var arr = myComponent.getArray();
    var enumerate = arr.enumerate();

    while (enumerate.hasMoreElements())
    var name = enumerate.getNext().QueryInterface(Components.interfaces.nsISupportsCString).data;

  2. xpcomic Says:

    Hi all,

    First of all, thanks for the article , i really needed that
    kind of sample code and it worked smoothly!

    However i also need to pass a struct/interface to JS
    from C++ so i made the relevant changes based on this url:

    http://admisource.gouv.fr/plugins/scmsvn/viewcvs.php/tags/triple_wrapping/TRIPLE_WRAPPING_0_2_RELEASE/extensions/java/xpcom/tests/dll/TestParams.cpp?rev=254&root=milimail&sortby=file&sortdir=down&view=auto
    (see the TestIfaceTypeArrayOut sample)

    which compiled and linked fine but returned a runtime
    error at the end of the function call:

    (Access violation reading location 0xbaadf00d.).

    Thru debugging, i can see that the out param is getting
    correctly the value of the internal allocated memory which
    is 0xbaadf00d. I also made sure that i am allocating
    “count” elements. (returned size matches array size).

    I don’t understand why this is happening and I would
    really need some help… so If anyone faced the
    same problem or have an answer concerning this issue
    plz help !

    Thanks

  3. praveenmatanam Says:

    Try by adding reference count to the aResult like as follows.
    NS_ADDREF(*aResult);

  4. xpcomic Says:

    thx a lot, i managed to get it working !

  5. jojo Says:

    hello,
    Thanks , I’ve tried the first example : http://codepad.org/9iocCHoK, but when calling the fonction getarray() in jvascript I’ve got this error :
    “TypeError: Components.classes[cid] is undefined” ,
    is there a simple more recent sample, I want to return a byte array to javascript from C++
    thanks

  6. jojo Says:

    sorry, the first method work finally,
    now I want to do the contrary return a bte array to c++ from javascript
    thanks

  7. amol Says:

    Hi ,

    I am trying to access an array of strings(of size 12) from XPCom object.
    In JS, I am receiving correct length of array.However ,in JS array all 12 elements have same value (one of those allocated in cpp).
    I debugged my code in cpp and all 12 different strings are getting added correctly.AppendElement of NSMutableArray is used.I have used the code written above.

  8. Suyambu Says:

    I want to pass an array from Javascript to C++. Could you please help me with the solution.

    Thanks in advance.

    -suyambu


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: