本文整理汇总了C++中js::HandleValue::isString方法的典型用法代码示例。如果您正苦于以下问题:C++ HandleValue::isString方法的具体用法?C++ HandleValue::isString怎么用?C++ HandleValue::isString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类js::HandleValue
的用法示例。
在下文中一共展示了HandleValue::isString方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rq
template<> bool ScriptInterface::FromJSVal<std::wstring>(JSContext* cx, JS::HandleValue v, std::wstring& out)
{
JSAutoRequest rq(cx);
WARN_IF_NOT(v.isString() || v.isNumber(), v); // allow implicit number conversions
JS::RootedString str(cx, JS::ToString(cx, v));
if (!str)
FAIL("Argument must be convertible to a string");
if (JS_StringHasLatin1Chars(str))
{
size_t length;
JS::AutoCheckCannotGC nogc;
const JS::Latin1Char* ch = JS_GetLatin1StringCharsAndLength(cx, nogc, str, &length);
if (!ch)
FAIL("JS_GetLatin1StringCharsAndLength failed");
out.assign(ch, ch + length);
}
else
{
size_t length;
JS::AutoCheckCannotGC nogc;
const char16_t* ch = JS_GetTwoByteStringCharsAndLength(cx, nogc, str, &length);
if (!ch)
FAIL("JS_GetTwoByteStringsCharsAndLength failed"); // out of memory
out.assign(ch, ch + length);
}
return true;
}
示例2: 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;
}
示例3: construct
void CodeInfo::construct(JSContext* cx, JS::CallArgs args) {
uassert(ErrorCodes::BadValue,
"Code needs 0, 1 or 2 arguments",
args.length() == 0 || args.length() == 1 || args.length() == 2);
auto scope = getScope(cx);
JS::RootedObject thisv(cx);
scope->getProto<CodeInfo>().newObject(&thisv);
ObjectWrapper o(cx, thisv);
if (args.length() == 0) {
o.setString(InternedString::code, "");
} else if (args.length() == 1) {
JS::HandleValue codeArg = args.get(0);
if (!codeArg.isString())
uasserted(ErrorCodes::BadValue, "code must be a string");
o.setValue(InternedString::code, codeArg);
} else {
if (!args.get(0).isString())
uasserted(ErrorCodes::BadValue, "code must be a string");
if (!args.get(1).isObject())
uasserted(ErrorCodes::BadValue, "scope must be an object");
o.setValue(InternedString::code, args.get(0));
o.setValue(InternedString::scope, args.get(1));
}
args.rval().setObjectOrNull(thisv);
}
示例4: 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;
}
示例5: jsval_to_std_string
bool jsval_to_std_string(JSContext *cx, JS::HandleValue v, std::string* ret) {
if(v.isString() || v.isNumber())
{
JSString *tmp = JS::ToString(cx, v);
JSB_PRECONDITION3(tmp, cx, false, "Error processing jsval_to_std_string");
JSStringWrapper str(tmp, cx);
*ret = str.get();
return true;
}
return false;
}
示例6: 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;
}