Unable to pass integer from javascript to npapi plugin

纵然是瞬间 提交于 2019-12-02 03:58:15

When providing a number argument to a NPAPI method, it is undefined whether you will receive a Int32 or Double variant, so you have to handle both cases in your code.
Furthermore, the NPVARIANT_TO_* macros only extract the respective value - they don't do any conversion.

To extract an integer from any numeric argument, you have to write your own code, e.g. something like:

bool convertToInt(const NPVariant& v, int32_t& out) {
  if (NPVARIANT_IS_INT32(v)) {
    out = NPVARIANT_TO_INT32(v);
    return true;
  }

  if (NPVARIANT_IS_DOUBLE(v)) {
    out = NPVARIANT_TO_DOUBLE(v);
    return true;
  }

  // not a numeric variant
  return false;
}

From here http://code.google.com/p/chromium/issues/detail?id=68175 and here https://bugs.webkit.org/show_bug.cgi?id=49036 I understand that it is bug in WEB kit, so I add next in "npruntime.h":

#define FIX_WEB_KIT_INT32_BUG

#ifdef FIX_WEB_KIT_INT32_BUG
    #define NPVARIANT_IS_INT32(_v)   ((_v).type == NPVariantType_Int32 || (_v).type == NPVariantType_Double)
    #define NPVARIANT_TO_INT32(_v)   ((_v).type == NPVariantType_Double ? (_v).value.doubleValue : (_v).value.intValue)
#else
    #define NPVARIANT_IS_INT32(_v)   ((_v).type == NPVariantType_Int32)
    #define NPVARIANT_TO_INT32(_v)  (_v).value.intValue)
#endif

on web(from javascript) I use parseInt(myVal,10) for all int values that passed to plugin. Check it on Google Chrome and Safari.

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