NPVariant to string array

孤人 提交于 2020-01-03 02:46:05

问题


I would like to pass an array of strings to a NPAPI plugin. How do I convert NPVariant to an array of strings?


回答1:


Looks like you're going the other diretction from what jldupont suggested. When you pass an array in as a parameters to either a property or a method:

var arrayData = [1,2,3,4,5];
plugin.someProperty = arrayData;
// -or-
plugin.callSomeMethod(arrayData);

That parameter will get to your NPObject as a NPVariant of type NPVariantType_Object. You then query the length property:

NPObject *inObject = val->value.objectValue;
NPVariant npvLength;
NPN_GetProperty(npp, inObject, NPN_GetStringIdentifier("length"), &npvLength);

and then you just do a for loop to get all the values:

for (uint32_t i = 0; i < npvLength.value.intValue; i++) {
    NPVariant curValue;
    NPN_GetProperty(npp, inObject, NPN_GetIntIdentifier(i), &curValue);
    // Do something with curValue
}

Similarly, if you need to return an array to javascript, another option (other than writing a method to emulate an object, as I suggested in the thread that jldupont linked to) is to use NPN_GetValue to get the NPObject for the DOM window, and then Invoke "Array" on it with no parameters. This will return an empty JS Array object (as an NPObject*). Then you just loop through the items you want to return and call "push" with the item as the first (and only) parameter.

Hope this helps




回答2:


You can use an NPObject (see this thread) to act as a container for your strings (much like a JS object with var arrayOfString={...strings here...}.



来源:https://stackoverflow.com/questions/1896166/npvariant-to-string-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!