本文整理汇总了C++中js::HandleValue::toString方法的典型用法代码示例。如果您正苦于以下问题:C++ HandleValue::toString方法的具体用法?C++ HandleValue::toString怎么用?C++ HandleValue::toString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类js::HandleValue
的用法示例。
在下文中一共展示了HandleValue::toString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: HandleGeckoMessage
NS_IMETHODIMP nsAndroidBridge::HandleGeckoMessage(JS::HandleValue val,
JSContext *cx)
{
if (val.isObject()) {
JS::RootedObject object(cx, &val.toObject());
AndroidBridge::Bridge()->HandleGeckoMessage(cx, object);
return NS_OK;
}
// Now handle legacy JSON messages.
if (!val.isString()) {
return NS_ERROR_INVALID_ARG;
}
JS::RootedString jsonStr(cx, val.toString());
JS::RootedValue jsonVal(cx);
if (!JS_ParseJSON(cx, jsonStr, &jsonVal) || !jsonVal.isObject()) {
return NS_ERROR_INVALID_ARG;
}
// Spit out a warning before sending the message.
nsContentUtils::ReportToConsoleNonLocalized(
NS_LITERAL_STRING("Use of JSON is deprecated. "
"Please pass Javascript objects directly to handleGeckoMessage."),
nsIScriptError::warningFlag,
NS_LITERAL_CSTRING("nsIAndroidBridge"),
nullptr);
JS::RootedObject object(cx, &jsonVal.toObject());
AndroidBridge::Bridge()->HandleGeckoMessage(cx, object);
return NS_OK;
}
示例2: GetOVRStringVal
bool GetOVRStringVal(JSContext* cx, JS::HandleValue val, OVR::String* out) {
if (!val.isString()) {
JS_ReportError(cx, "Expected a string");
return false;
}
JS::RootedString str(cx, val.toString());
bool resp = GetOVRString(cx, str, out);
return resp;
}
示例3: ar
/**
* gjs_string_to_ucs4:
* @cx: a #JSContext
* @value: JS::Value containing a string
* @ucs4_string_p: return location for a #gunichar array
* @len_p: return location for @ucs4_string_p length
*
* Returns: true on success, false otherwise in which case a JS error is thrown
*/
bool
gjs_string_to_ucs4(JSContext *cx,
JS::HandleValue value,
gunichar **ucs4_string_p,
size_t *len_p)
{
if (ucs4_string_p == NULL)
return true;
if (!value.isString()) {
gjs_throw(cx, "Value is not a string, cannot convert to UCS-4");
return false;
}
JSAutoRequest ar(cx);
JS::RootedString str(cx, value.toString());
size_t utf16_len;
GError *error = NULL;
const char16_t *utf16 = JS_GetStringCharsAndLength(cx, str, &utf16_len);
if (utf16 == NULL) {
gjs_throw(cx, "Failed to get UTF-16 string data");
return false;
}
if (ucs4_string_p != NULL) {
long length;
*ucs4_string_p = g_utf16_to_ucs4(reinterpret_cast<const gunichar2 *>(utf16),
utf16_len, NULL, &length, &error);
if (*ucs4_string_p == NULL) {
gjs_throw(cx, "Failed to convert UTF-16 string to UCS-4: %s",
error->message);
g_clear_error(&error);
return false;
}
if (len_p != NULL)
*len_p = (size_t) length;
}
return true;
}
示例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();
}
}
}