How to write a scriptable plugin with returned val?

Discuss building things with or for the Mozilla Platform.
Post Reply
netformx
Posts: 19
Joined: March 7th, 2005, 2:19 am

How to write a scriptable plugin with returned val?

Post by netformx »

Hi,
I use nscriptable plugin example from Mozilla sdk (it can be any other scriptable plugin).

Now I want plugin's fucntion will return a value, so I added to fucntion declaration in idl file:

void showVersion(out short outInt);

and in nsScriptablePeer.cpp file:
NS_IMETHODIMP nsScriptablePeer::ShowVersion(short *outInt)

and built it with idl compiler which generated new h and xpt files.
Updated xpt under components.

From my script I call in the folowing way:
var embed = document.embeds[0];
function ShowVersion()
{


var t = embed.showVersion();

}

but it does not come to the plugin's function!!!

If I remove out param - works fine!!!

I wanted to add to showVersion function
Mook
Posts: 1752
Joined: November 7th, 2002, 9:35 pm

Post by Mook »

Try doing this in the IDL:

Code: Select all

PRUint16 showVersion();

Your C++ file should remain the same, I think (other than perhaps renaming "short *outInt" to "short *_retval" for consistency with the Mozilla code base)

Or, alternatively, use the existing IDL but do this in JS:

Code: Select all

var t = {};
embed.showVersion(t);
return t.value;


Basically, an out-param means you have to pass it in and get the "value" property afterwords.

(Do you really want a short? Not sure if PRInt16 would be better, in case you ever want to move to a 64-bit platform and the sizes change...)
poot.
netformx
Posts: 19
Joined: March 7th, 2005, 2:19 am

Post by netformx »

thanks - this works

but now I want to return a string.
what the right way to do this?

I tried to define in idl:
string myFun()
and the in cpp
myFun(char **_retVal ):
{
_bstr_t ver = ...
int len = strlen(ver);
char *retVal = new char(len);
strcpy(retVal,ver);
*_retval = retVal;
}

it works but:
1. I am worried since I use new to allocate the memoty - who is realsing this memory?
2. In debugger I get "user breakpoint" - so something is wrong here - what is it?

thanks
Mook
Posts: 1752
Joined: November 7th, 2002, 9:35 pm

Post by Mook »

Perhaps the string talk and especially the string guide would be useful.

If you figure it out, please post here - I'll need to do this eventually too (havn't had time to poke at it yet).
poot.
netformx
Posts: 19
Joined: March 7th, 2005, 2:19 am

Post by netformx »

ok,

but if you find first - please tell me :)
netformx
Posts: 19
Joined: March 7th, 2005, 2:19 am

Post by netformx »

Please help!!!

Do you know how to use nsAString?
if I define in idl DOMString -> in C++ nsAString & _ret - compiles successfully but if I write in the body of the function:
ret.Assign(... -> error "use of undefined type nsAString

If I define in the body of the same function:
nsString str;
(which is not abstract) --> error "nsString' : undeclared identifier"

what to do????
Mook
Posts: 1752
Joined: November 7th, 2002, 9:35 pm

Post by Mook »

I guess that nsString is defined in nsStringAPI.h. Or maybe it's nsString.h, not sure which... :(

Similiarly, nsAString seems to be from nsAString.h

Totally untested :p
poot.
netformx
Posts: 19
Joined: March 7th, 2005, 2:19 am

Post by netformx »

thanks

so my solution is:
in idl:
AString showStr();

in cpp:
#include nsAString.h
NS_IMETHODIMP nsScriptablePeer::ShowStr(nsAString & _retval)
{
_bstr_t ver = "kuku"; //probably does not have to be _bstr_t
_retval.Assign(ver);
return NS_OK;
}

in JavaScript:
var embed = document.embeds[0];
var str = embed.showStr();
alert(str);

it's working, although I am not sure if to use AString or DOMString (from link you sent)

If you have any comments - I would like to receive them!
Mook
Posts: 1752
Joined: November 7th, 2002, 9:35 pm

Post by Mook »

Isn't _bstr_t Windows-specific? Not sure how else you'd get wide strings to go between XPCOM and the Win32 COM world though.

Also, in your particular case, it seems like Real Mozilla People (i.e., not me since I sadly havn't done anything with strings) tend to use NS_LITERAL_STRING.

I have no idea about the deal with DOMStrings.
poot.
netformx
Posts: 19
Joined: March 7th, 2005, 2:19 am

Post by netformx »

thanks for comments - my target is only Win platforms (for other reasons)
netformx
Posts: 19
Joined: March 7th, 2005, 2:19 am

Post by netformx »

Hi,

I submitted the question:
How can scriptable plugin return pointer to COM?

Maybe you can give me some idea? is it possible?

the body of the question copied:

I want my scriptable plugin to return pointer to COM (something like COMPtr, *IDispatch ..) so that Java sCript can script this COM.

I tried to define in idl:
nsQIResult getControl();
or
voidPtr getControl();

but receive error from xpidl:
Error: methods in [scriptable] interfaces that are non-scriptable because they return native types must be marked [noscript]

and if I mark this function as [noscript] -I can't call it from Java script.

what to do????
Post Reply