本文整理汇总了C++中js::HandleValue类的典型用法代码示例。如果您正苦于以下问题:C++ HandleValue类的具体用法?C++ HandleValue怎么用?C++ HandleValue使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HandleValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: ac
NS_IMETHODIMP
calDateTime::SetJsDate(JSContext* aCx, JS::HandleValue aDate)
{
if (!aDate.isObject()) {
mIsValid = false;
return NS_OK;
}
JS::Rooted<JSObject*> dobj(aCx, &aDate.toObject());
dobj = js::CheckedUnwrap(dobj);
if (!dobj) {
mIsValid = false;
return NS_OK;
}
JSAutoCompartment ac(aCx, dobj);
if (!JS_ObjectIsDate(aCx, dobj) || !js_DateIsValid(dobj)) {
mIsValid = false;
return NS_OK;
}
PRTime utcTime = PRTime(js_DateGetMsecSinceEpoch(dobj)) * 1000;
mIsValid = NS_SUCCEEDED(SetNativeTime(utcTime));
return NS_OK;
}
示例3: 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;
}
示例4: 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);
}
示例5: 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;
}
示例6: 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;
}
}
示例7: js_to_number
bool js_to_number(JSContext *cx, JS::HandleValue v, double *dp) {
if (v.isNumber()) {
*dp = v.toNumber();
return true;
} else {
*dp = 0;
return false;
}
}
示例8: 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;
}
示例9: SetPrototype
bool ScriptInterface::SetPrototype(JS::HandleValue objVal, JS::HandleValue protoVal)
{
JSAutoRequest rq(m->m_cx);
if (!objVal.isObject() || !protoVal.isObject())
return false;
JS::RootedObject obj(m->m_cx, &objVal.toObject());
JS::RootedObject proto(m->m_cx, &protoVal.toObject());
return JS_SetPrototype(m->m_cx, obj, proto);
}
示例10: 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;
}
示例11: GetProperty_
bool ScriptInterface::GetProperty_(JS::HandleValue obj, const char* name, JS::MutableHandleValue out)
{
JSAutoRequest rq(m->m_cx);
if (!obj.isObject())
return false;
JS::RootedObject object(m->m_cx, &obj.toObject());
if (!JS_GetProperty(m->m_cx, object, name, out))
return false;
return true;
}
示例12: ScriptObjectAppend
void CStdDeserializer::ScriptObjectAppend(const char* name, JS::HandleValue objVal)
{
JSContext* cx = m_ScriptInterface.GetContext();
JSAutoRequest rq(cx);
if (!objVal.isObject())
throw PSERROR_Deserialize_ScriptError();
JS::RootedObject obj(cx, &objVal.toObject());
ReadScriptVal(name, obj);
}
示例13: GetPropertyInt_
bool ScriptInterface::GetPropertyInt_(JS::HandleValue obj, int name, JS::MutableHandleValue out)
{
JSAutoRequest rq(m->m_cx);
JS::RootedId nameId(m->m_cx, INT_TO_JSID(name));
if (!obj.isObject())
return false;
JS::RootedObject object(m->m_cx, &obj.toObject());
if (!JS_GetPropertyById(m->m_cx, object, nameId, out))
return false;
return true;
}
示例14: HasProperty
bool ScriptInterface::HasProperty(JS::HandleValue obj, const char* name)
{
// TODO: proper errorhandling
JSAutoRequest rq(m->m_cx);
if (!obj.isObject())
return false;
JS::RootedObject object(m->m_cx, &obj.toObject());
bool found;
if (!JS_HasProperty(m->m_cx, object, name, &found))
return false;
return found;
}
示例15: CallConstructor
void ScriptInterface::CallConstructor(JS::HandleValue ctor, JS::HandleValueArray argv, JS::MutableHandleValue out)
{
JSAutoRequest rq(m->m_cx);
if (!ctor.isObject())
{
LOGERROR("CallConstructor: ctor is not an object");
out.setNull();
return;
}
JS::RootedObject ctorObj(m->m_cx, &ctor.toObject());
out.setObjectOrNull(JS_New(m->m_cx, ctorObj, argv));
}