您当前的位置: 首页 >  ar

JS/NPAP之间传递数组:NPVARIANT_TO_OBJECT/OBJECT_TO_NPVARIANT

柳鲲鹏 发布时间:2019-08-18 15:50:17 ,浏览量:1

从NPAPI中返回数组,代码要这样写。已经过本人测试:

// 这个写法看起来怪异,可是必须这样写!
// Get window object.
NPObject* pWindow = NULL;
NPN_GetValue(mNpp, NPNVWindowNPObject, &pWindow);

//Get NPVariant
char buf[16]="Array()";
NPString str;
str.UTF8Characters = buf;
str.UTF8Length = strlen(buf);

NPVariant arrayVar;
NPN_Evaluate(mNpp, pWindow, &str, &arrayVar);

//或者使用for循环处理。
//如果使用malloc,记得释放内存。
NPVariant values[2];
INT32_TO_NPVARIANT(123, values[0]);
NPN_SetProperty(mNpp, arrayVar.value.objectValue, NPN_GetIntIdentifier(0), &(values[0]));

INT32_TO_NPVARIANT(456, values[1]);
NPN_SetProperty(mNpp, arrayVar.value.objectValue, NPN_GetIntIdentifier(1), &(values[1]));

OBJECT_TO_NPVARIANT(arrayVar.value.objectValue, *result);

 

这是另外一段代码。经过测试返回时会出错。

std::vector vect;//容器
for (int i = 0; i < 2000; i++) {
      NPObject* _stream = nullptr;
      NPObject::CreateInstanceWithRef(m_npp, &_stream);//创建对象
      NPVariant var;
      OBJECT_TO_NPVARIANT(_stream, var);
       BrowserFuncs->retainobject(var.value.objectValue); // will be release by "NPVecClear()"
       vect.push_back(var);
       NPObject::ReleaseInstance(&_stream);//
}
NPObject* arrayObj = NULL;
Utils::CreateJsArray(m_npp, vect, &arrayObj);//通过设置属性的方式
OBJECT_TO_NPVARIANT(arrayObj, *result);//输出给js
Utils::NPVecClear(vect);

以下两段代码是分解从JS得到的数组。没有测试。

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
}

代码:


输入数组
NPObject *inObject = args->value.objectValue;
NPVariant npvLength;
NPN_GetProperty(mNpp, inObject, NPN_GetStringIdentifier("length"), &npvLength);
for (uint32_t i = 0; i < npvLength.value.intValue; i++) {
    NPVariant curValue;
    NPN_GetProperty(mNpp, inObject, NPN_GetIntIdentifier(i), &curValue);
    // Do something with curValue
}

 

关注
打赏
1688896170
查看更多评论

柳鲲鹏

暂无认证

  • 1浏览

    0关注

    4641博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0857s