Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

scripting arrays

174 views
Skip to first unread message

DP

unread,
Aug 19, 2009, 9:27:25 AM8/19/09
to
I've got a scriptable Firefox plugin using the NPAPI, and generally
speaking, all that is working fine. One of the classes I'm trying to
abstract, however, has a list of items. To be "native", this should
be represented as a JavaScript array. The question is how can this be
done using the NPAPI? Obviously, a "length" property could be added
to get the size of the array, but how do I address the individual
array elements (e.g. myarray[2]) given that NPN_GetStringIdentifier()
operates on the exact property name, which I don't know at compile
time? Is there a way to use wildcards so that all "myarray[*]"
references would be directed to the same code? Do I have to hard code
some maximum array size (e.g. myarray[0], myarray[1], ... myarray[max
- 1])? Any help would be appreciated.

taxilian

unread,
Aug 19, 2009, 4:52:19 PM8/19/09
to

This is no problem at all. I'm assuming that you're using npruntime
(NPObjects) for your interface... if you're using xpcom, you should
probably think about changing since support for it is being dropped
=] Also, I don't know how to do this with xpcom =]

In your NPObject's Get/SetProperty method (or invoke, for that
matter), you normally would use NPN_UTF8FromIdentifier to get the
function name; when you're using array access, you just use
NPN_GetIntIdentifier instead. You can even support both; just use
NPN_IdentifierIsString to see if it's a string or an int.

So, you could do (pseudo-code):

bool GetProperty(NPIdentifier propertyName, NPVariant *result)
{
if (NPN_IdentifierIsString(propertyName)) {
GetStringProperty(NPN_UTF8FromIdentifier(propertyName, result));
} else {
GetIntProperty(NPN_GetIntIdentifier(propertyName, result));
}
}

and then just implement GetStringProperty to handle property names
with strings, and GetIntProperty to handle integer indexes.

i.e.:

pluginObj.length would result in GetStringProperty("length", result),
and pluginObj[4] would result in GetIntProperty(4, result)

Hope that helps,

Richard Bateman

DP

unread,
Aug 20, 2009, 10:19:05 AM8/20/09
to

Thanks! Very helpful.

0 new messages