本文整理汇总了C++中js::HandleValue::isObjectOrNull方法的典型用法代码示例。如果您正苦于以下问题:C++ HandleValue::isObjectOrNull方法的具体用法?C++ HandleValue::isObjectOrNull怎么用?C++ HandleValue::isObjectOrNull使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类js::HandleValue
的用法示例。
在下文中一共展示了HandleValue::isObjectOrNull方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: EnumeratePropertyNamesWithPrefix
bool ScriptInterface::EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector<std::string>& out)
{
JSAutoRequest rq(m->m_cx);
if (!objVal.isObjectOrNull())
{
LOGERROR("EnumeratePropertyNamesWithPrefix expected object type!");
return false;
}
if(objVal.isNull())
return true; // reached the end of the prototype chain
JS::RootedObject obj(m->m_cx, &objVal.toObject());
JS::RootedObject it(m->m_cx, JS_NewPropertyIterator(m->m_cx, obj));
if (!it)
return false;
while (true)
{
JS::RootedId idp(m->m_cx);
JS::RootedValue val(m->m_cx);
if (! JS_NextProperty(m->m_cx, it, idp.address()) || ! JS_IdToValue(m->m_cx, idp, &val))
return false;
if (val.isUndefined())
break; // end of iteration
if (!val.isString())
continue; // ignore integer properties
JS::RootedString name(m->m_cx, val.toString());
size_t len = strlen(prefix)+1;
std::vector<char> buf(len);
size_t prefixLen = strlen(prefix) * sizeof(char);
JS_EncodeStringToBuffer(m->m_cx, name, &buf[0], prefixLen);
buf[len-1]= '\0';
if(0 == strcmp(&buf[0], prefix))
{
size_t len;
const jschar* chars = JS_GetStringCharsAndLength(m->m_cx, name, &len);
out.push_back(std::string(chars, chars+len));
}
}
// Recurse up the prototype chain
JS::RootedObject prototype(m->m_cx);
if (JS_GetPrototype(m->m_cx, obj, &prototype))
{
JS::RootedValue prototypeVal(m->m_cx, JS::ObjectOrNullValue(prototype));
if (! EnumeratePropertyNamesWithPrefix(prototypeVal, prefix, out))
return false;
}
return true;
}
示例2: EnumeratePropertyNamesWithPrefix
bool ScriptInterface::EnumeratePropertyNamesWithPrefix(JS::HandleValue objVal, const char* prefix, std::vector<std::string>& out)
{
JSAutoRequest rq(m->m_cx);
if (!objVal.isObjectOrNull())
{
LOGERROR("EnumeratePropertyNamesWithPrefix expected object type!");
return false;
}
if (objVal.isNull())
return true; // reached the end of the prototype chain
JS::RootedObject obj(m->m_cx, &objVal.toObject());
JS::AutoIdArray props(m->m_cx, JS_Enumerate(m->m_cx, obj));
if (!props)
return false;
for (size_t i = 0; i < props.length(); ++i)
{
JS::RootedId id(m->m_cx, props[i]);
JS::RootedValue val(m->m_cx);
if (!JS_IdToValue(m->m_cx, id, &val))
return false;
if (!val.isString())
continue; // ignore integer properties
JS::RootedString name(m->m_cx, val.toString());
size_t len = strlen(prefix)+1;
std::vector<char> buf(len);
size_t prefixLen = strlen(prefix) * sizeof(char);
JS_EncodeStringToBuffer(m->m_cx, name, &buf[0], prefixLen);
buf[len-1]= '\0';
if (0 == strcmp(&buf[0], prefix))
{
if (JS_StringHasLatin1Chars(name))
{
size_t length;
JS::AutoCheckCannotGC nogc;
const JS::Latin1Char* chars = JS_GetLatin1StringCharsAndLength(m->m_cx, nogc, name, &length);
if (chars)
out.push_back(std::string(chars, chars+length));
}
else
{
size_t length;
JS::AutoCheckCannotGC nogc;
const char16_t* chars = JS_GetTwoByteStringCharsAndLength(m->m_cx, nogc, name, &length);
if (chars)
out.push_back(std::string(chars, chars+length));
}
}
}
// Recurse up the prototype chain
JS::RootedObject prototype(m->m_cx);
if (JS_GetPrototype(m->m_cx, obj, &prototype))
{
JS::RootedValue prototypeVal(m->m_cx, JS::ObjectOrNullValue(prototype));
if (!EnumeratePropertyNamesWithPrefix(prototypeVal, prefix, out))
return false;
}
return true;
}