本文整理汇总了C++中js::HandleValue::toBoolean方法的典型用法代码示例。如果您正苦于以下问题:C++ HandleValue::toBoolean方法的具体用法?C++ HandleValue::toBoolean怎么用?C++ HandleValue::toBoolean使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类js::HandleValue
的用法示例。
在下文中一共展示了HandleValue::toBoolean方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: js_to_bool
bool js_to_bool( JSContext *cx, JS::HandleValue vp, bool *ret ) {
bool ok = vp.isBoolean();
if (ok) {
*ret = vp.toBoolean();
}
return ok;
}
示例2: obj
bool
JavaScriptShared::toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to)
{
switch (JS_TypeOfValue(cx, from)) {
case JSTYPE_VOID:
*to = UndefinedVariant();
return true;
case JSTYPE_OBJECT:
case JSTYPE_FUNCTION:
{
RootedObject obj(cx, from.toObjectOrNull());
if (!obj) {
MOZ_ASSERT(from == JSVAL_NULL);
*to = NullVariant();
return true;
}
if (xpc_JSObjectIsID(cx, obj)) {
JSIID iid;
const nsID *id = xpc_JSObjectToID(cx, obj);
ConvertID(*id, &iid);
*to = iid;
return true;
}
ObjectVariant objVar;
if (!toObjectVariant(cx, obj, &objVar))
return false;
*to = objVar;
return true;
}
case JSTYPE_STRING:
{
nsAutoJSString autoStr;
if (!autoStr.init(cx, from))
return false;
*to = autoStr;
return true;
}
case JSTYPE_NUMBER:
if (from.isInt32())
*to = double(from.toInt32());
else
*to = from.toDouble();
return true;
case JSTYPE_BOOLEAN:
*to = from.toBoolean();
return true;
default:
MOZ_ASSERT(false);
return false;
}
}
示例3: obj
bool
JavaScriptShared::toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to)
{
switch (JS_TypeOfValue(cx, from)) {
case JSTYPE_VOID:
*to = void_t();
return true;
case JSTYPE_NULL:
{
*to = uint64_t(0);
return true;
}
case JSTYPE_OBJECT:
case JSTYPE_FUNCTION:
{
RootedObject obj(cx, from.toObjectOrNull());
if (!obj) {
MOZ_ASSERT(from == JSVAL_NULL);
*to = uint64_t(0);
return true;
}
if (xpc_JSObjectIsID(cx, obj)) {
JSIID iid;
const nsID *id = xpc_JSObjectToID(cx, obj);
ConvertID(*id, &iid);
*to = iid;
return true;
}
ObjectId id;
if (!makeId(cx, obj, &id))
return false;
*to = uint64_t(id);
return true;
}
case JSTYPE_STRING:
{
nsDependentJSString dep;
if (!dep.init(cx, from))
return false;
*to = dep;
return true;
}
case JSTYPE_NUMBER:
if (JSVAL_IS_INT(from))
*to = double(from.toInt32());
else
*to = from.toDouble();
return true;
case JSTYPE_BOOLEAN:
*to = from.toBoolean();
return true;
default:
MOZ_ASSERT(false);
return false;
}
}
示例4: HandleScriptVal
//.........这里部分代码省略.........
JS::Rooted<JSPropertyDescriptor> desc(cx);
if (!JS_GetPropertyDescriptorById(cx, obj, id, &desc))
throw PSERROR_Serialize_ScriptError("JS_GetPropertyDescriptorById failed");
if (desc.hasGetterObject())
throw PSERROR_Serialize_ScriptError("Cannot serialize property getters");
// Get the property name as a string
if (!JS_IdToValue(cx, id, &idval))
throw PSERROR_Serialize_ScriptError("JS_IdToValue failed");
JS::RootedString idstr(cx, JS::ToString(cx, idval));
if (!idstr)
throw PSERROR_Serialize_ScriptError("JS_ValueToString failed");
ScriptString("prop name", idstr);
if (!JS_GetPropertyById(cx, obj, id, &propval))
throw PSERROR_Serialize_ScriptError("JS_GetPropertyById failed");
HandleScriptVal(propval);
}
break;
}
case JSTYPE_FUNCTION:
{
// We can't serialise functions, but we can at least name the offender (hopefully)
std::wstring funcname(L"(unnamed)");
JS::RootedFunction func(cx, JS_ValueToFunction(cx, val));
if (func)
{
JS::RootedString string(cx, JS_GetFunctionId(func));
if (string)
{
if (JS_StringHasLatin1Chars(string))
{
size_t length;
JS::AutoCheckCannotGC nogc;
const JS::Latin1Char* ch = JS_GetLatin1StringCharsAndLength(cx, nogc, string, &length);
if (ch && length > 0)
funcname.assign(ch, ch + length);
}
else
{
size_t length;
JS::AutoCheckCannotGC nogc;
const char16_t* ch = JS_GetTwoByteStringCharsAndLength(cx, nogc, string, &length);
if (ch && length > 0)
funcname.assign(ch, ch + length);
}
}
}
LOGERROR("Cannot serialise JS objects of type 'function': %s", utf8_from_wstring(funcname));
throw PSERROR_Serialize_InvalidScriptValue();
}
case JSTYPE_STRING:
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_STRING);
JS::RootedString stringVal(cx, val.toString());
ScriptString("string", stringVal);
break;
}
case JSTYPE_NUMBER:
{
// To reduce the size of the serialized data, we handle integers and doubles separately.
// We can't check for val.isInt32 and val.isDouble directly, because integer numbers are not guaranteed
// to be represented as integers. A number like 33 could be stored as integer on the computer of one player
// and as double on the other player's computer. That would cause out of sync errors in multiplayer games because
// their binary representation and thus the hash would be different.
double d;
d = val.toNumber();
i32 integer;
if (JS_DoubleIsInt32(d, &integer))
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_INT);
m_Serializer.NumberI32_Unbounded("value", integer);
}
else
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_DOUBLE);
m_Serializer.NumberDouble_Unbounded("value", d);
}
break;
}
case JSTYPE_BOOLEAN:
{
m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BOOLEAN);
bool b = val.toBoolean();
m_Serializer.NumberU8_Unbounded("value", b ? 1 : 0);
break;
}
default:
{
debug_warn(L"Invalid TypeOfValue");
throw PSERROR_Serialize_InvalidScriptValue();
}
}
}