本文整理汇总了C++中js::Value::toObject方法的典型用法代码示例。如果您正苦于以下问题:C++ Value::toObject方法的具体用法?C++ Value::toObject怎么用?C++ Value::toObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类js::Value
的用法示例。
在下文中一共展示了Value::toObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dataStr
void
nsGeolocationService::HandleMozsettingChanged(const PRUnichar* aData)
{
// The string that we're interested in will be a JSON string that looks like:
// {"key":"gelocation.enabled","value":true}
SafeAutoJSContext cx;
nsDependentString dataStr(aData);
JS::Value val;
if (!JS_ParseJSON(cx, dataStr.get(), dataStr.Length(), &val) || !val.isObject()) {
return;
}
JSObject &obj(val.toObject());
JS::Value key;
if (!JS_GetProperty(cx, &obj, "key", &key) || !key.isString()) {
return;
}
JSBool match;
if (!JS_StringEqualsAscii(cx, key.toString(), GEO_SETINGS_ENABLED, &match) || (match != JS_TRUE)) {
return;
}
JS::Value value;
if (!JS_GetProperty(cx, &obj, "value", &value) || !value.isBoolean()) {
return;
}
HandleMozsettingValue(value.toBoolean());
}
示例2: SendData
NS_IMETHODIMP
TCPSocketChild::Send(const JS::Value& aData,
uint32_t aByteOffset,
uint32_t aByteLength,
JSContext* aCx)
{
if (aData.isString()) {
JSString* jsstr = aData.toString();
nsDependentJSString str;
bool ok = str.init(aCx, jsstr);
NS_ENSURE_TRUE(ok, NS_ERROR_FAILURE);
SendData(str);
} else {
NS_ENSURE_TRUE(aData.isObject(), NS_ERROR_FAILURE);
JS::Rooted<JSObject*> obj(aCx, &aData.toObject());
NS_ENSURE_TRUE(JS_IsArrayBufferObject(obj), NS_ERROR_FAILURE);
uint32_t buflen = JS_GetArrayBufferByteLength(obj);
aByteOffset = std::min(buflen, aByteOffset);
uint32_t nbytes = std::min(buflen - aByteOffset, aByteLength);
uint8_t* data = JS_GetArrayBufferData(obj);
if (!data) {
return NS_ERROR_OUT_OF_MEMORY;
}
FallibleTArray<uint8_t> fallibleArr;
if (!fallibleArr.InsertElementsAt(0, data, nbytes)) {
return NS_ERROR_OUT_OF_MEMORY;
}
InfallibleTArray<uint8_t> arr;
arr.SwapElements(fallibleArr);
SendData(arr);
}
return NS_OK;
}
示例3:
NS_IMETHODIMP
TCPSocketParent::InitJS(const JS::Value& aIntermediary, JSContext* aCx)
{
MOZ_ASSERT(aIntermediary.isObject());
mIntermediaryObj = &aIntermediary.toObject();
return NS_OK;
}
示例4: regions
// Helper for weighted regions.
nsresult
CameraControlImpl::Set(JSContext* aCx, uint32_t aKey, const JS::Value& aValue, uint32_t aLimit)
{
if (aLimit == 0) {
DOM_CAMERA_LOGI("%s:%d : aLimit = 0, nothing to do\n", __func__, __LINE__);
return NS_OK;
}
if (!aValue.isObject()) {
return NS_ERROR_INVALID_ARG;
}
uint32_t length = 0;
JSObject* regions = &aValue.toObject();
if (!JS_GetArrayLength(aCx, regions, &length)) {
return NS_ERROR_FAILURE;
}
DOM_CAMERA_LOGI("%s:%d : got %d regions (limited to %d)\n", __func__, __LINE__, length, aLimit);
if (length > aLimit) {
length = aLimit;
}
nsTArray<CameraRegion> regionArray;
regionArray.SetCapacity(length);
for (uint32_t i = 0; i < length; ++i) {
JS::Value v;
if (!JS_GetElement(aCx, regions, i, &v)) {
return NS_ERROR_FAILURE;
}
CameraRegion* r = regionArray.AppendElement();
/**
* These are the default values. We can remove these when the xpidl
* dictionary parser gains the ability to grok default values.
*/
r->top = -1000;
r->left = -1000;
r->bottom = 1000;
r->right = 1000;
r->weight = 1000;
nsresult rv = r->Init(aCx, &v);
NS_ENSURE_SUCCESS(rv, rv);
DOM_CAMERA_LOGI("region %d: top=%d, left=%d, bottom=%d, right=%d, weight=%d\n",
i,
r->top,
r->left,
r->bottom,
r->right,
r->weight
);
}
SetParameter(aKey, regionArray);
return NS_OK;
}
示例5: global
JSScript *createScriptViaXDR(JSPrincipals *prin, JSPrincipals *orig, int testCase)
{
const char src[] =
"function f() { return 1; }\n"
"f;\n";
js::RootedObject global(cx, JS_GetGlobalObject(cx));
JSScript *script = CompileScriptForPrincipalsVersionOrigin(cx, global, prin, orig,
src, strlen(src), "test", 1,
JSVERSION_DEFAULT);
if (!script)
return NULL;
if (testCase == TEST_SCRIPT || testCase == TEST_SERIALIZED_FUNCTION) {
script = FreezeThaw(cx, script);
if (!script)
return NULL;
if (testCase == TEST_SCRIPT)
return script;
}
JS::Value v;
JSBool ok = JS_ExecuteScript(cx, global, script, &v);
if (!ok || !v.isObject())
return NULL;
js::RootedObject funobj(cx, &v.toObject());
if (testCase == TEST_FUNCTION) {
funobj = FreezeThaw(cx, funobj);
if (!funobj)
return NULL;
}
return GetScript(cx, funobj);
}
示例6: UndefinedValue
// static
JSObject*
DOMProxyHandler::GetAndClearExpandoObject(JSObject* obj)
{
MOZ_ASSERT(IsDOMProxy(obj), "expected a DOM proxy object");
JS::Value v = js::GetProxyExtra(obj, JSPROXYSLOT_EXPANDO);
if (v.isUndefined()) {
return nullptr;
}
if (v.isObject()) {
js::SetProxyExtra(obj, JSPROXYSLOT_EXPANDO, UndefinedValue());
xpc::GetObjectScope(obj)->RemoveDOMExpandoObject(obj);
} else {
js::ExpandoAndGeneration* expandoAndGeneration =
static_cast<js::ExpandoAndGeneration*>(v.toPrivate());
v = expandoAndGeneration->expando;
if (v.isUndefined()) {
return nullptr;
}
expandoAndGeneration->expando = UndefinedValue();
}
return &v.toObject();
}
示例7: Init
nsresult xpcJSWeakReference::Init(JSContext* cx, const JS::Value& object)
{
if (!object.isObject())
return NS_OK;
JS::RootedObject obj(cx, &object.toObject());
XPCCallContext ccx(NATIVE_CALLER, cx);
// See if the object is a wrapped native that supports weak references.
nsISupports* supports =
nsXPConnect::XPConnect()->GetNativeOfWrapper(cx, obj);
nsCOMPtr<nsISupportsWeakReference> supportsWeakRef =
do_QueryInterface(supports);
if (supportsWeakRef) {
supportsWeakRef->GetWeakReference(getter_AddRefs(mReferent));
if (mReferent) {
return NS_OK;
}
}
// If it's not a wrapped native, or it is a wrapped native that does not
// support weak references, fall back to getting a weak ref to the object.
// See if object is a wrapped JSObject.
nsRefPtr<nsXPCWrappedJS> wrapped;
nsresult rv = nsXPCWrappedJS::GetNewOrUsed(obj,
NS_GET_IID(nsISupports),
getter_AddRefs(wrapped));
if (!wrapped) {
NS_ERROR("can't get nsISupportsWeakReference wrapper for obj");
return rv;
}
return wrapped->GetWeakReference(getter_AddRefs(mReferent));
}
示例8:
Promise*
WrapperPromiseCallback::GetDependentPromise()
{
// Per spec, various algorithms like all() and race() are actually implemented
// in terms of calling then() but passing it the resolve/reject functions that
// are passed as arguments to function passed to the Promise constructor.
// That will cause the promise in question to hold on to a
// WrapperPromiseCallback, but the dependent promise should really be the one
// whose constructor those functions came from, not the about-to-be-ignored
// return value of "then". So try to determine whether we're in that case and
// if so go ahead and dig the dependent promise out of the function we have.
JSObject* callable = mCallback->Callable();
// Unwrap it, in case it's a cross-compartment wrapper. Our caller here is
// system, so it's really ok to just go and unwrap.
callable = js::UncheckedUnwrap(callable);
if (JS_IsNativeFunction(callable, Promise::JSCallback)) {
JS::Value promiseVal =
js::GetFunctionNativeReserved(callable, Promise::SLOT_PROMISE);
Promise* promise;
UNWRAP_OBJECT(Promise, &promiseVal.toObject(), promise);
return promise;
}
if (mNextPromise) {
return mNextPromise;
}
Promise* promise;
if (NS_SUCCEEDED(UNWRAP_OBJECT(Promise, mNextPromiseObj, promise))) {
return promise;
}
// Oh, well.
return nullptr;
}
示例9: cx
NS_IMETHODIMP
SmsManager::Send(const JS::Value& aNumber, const nsAString& aMessage, JS::Value* aReturn)
{
nsresult rv;
nsIScriptContext* sc = GetContextForEventHandlers(&rv);
NS_ENSURE_STATE(sc);
AutoPushJSContext cx(sc->GetNativeContext());
NS_ASSERTION(cx, "Failed to get a context!");
if (!aNumber.isString() &&
!(aNumber.isObject() && JS_IsArrayObject(cx, &aNumber.toObject()))) {
return NS_ERROR_INVALID_ARG;
}
JSObject* global = sc->GetNativeGlobal();
NS_ASSERTION(global, "Failed to get global object!");
JSAutoRequest ar(cx);
JSAutoCompartment ac(cx, global);
if (aNumber.isString()) {
return Send(cx, global, aNumber.toString(), aMessage, aReturn);
}
// Must be an array then.
JSObject& numbers = aNumber.toObject();
uint32_t size;
JS_ALWAYS_TRUE(JS_GetArrayLength(cx, &numbers, &size));
JS::Value* requests = new JS::Value[size];
for (uint32_t i=0; i<size; ++i) {
JS::Value number;
if (!JS_GetElement(cx, &numbers, i, &number)) {
return NS_ERROR_INVALID_ARG;
}
nsresult rv = Send(cx, global, number.toString(), aMessage, &requests[i]);
NS_ENSURE_SUCCESS(rv, rv);
}
aReturn->setObjectOrNull(JS_NewArrayObject(cx, size, requests));
NS_ENSURE_TRUE(aReturn->isObject(), NS_ERROR_FAILURE);
return NS_OK;
}
示例10: dataStr
NS_IMETHODIMP
TimeZoneSettingObserver::Observe(nsISupports *aSubject,
const char *aTopic,
const PRUnichar *aData)
{
if (strcmp(aTopic, MOZSETTINGS_CHANGED) != 0) {
return NS_OK;
}
// Note that this function gets called for any and all settings changes,
// so we need to carefully check if we have the one we're interested in.
//
// The string that we're interested in will be a JSON string that looks like:
// {"key":"time.timezone","value":"America/Chicago"}
// Get the safe JS context.
nsCOMPtr<nsIThreadJSContextStack> stack =
do_GetService("@mozilla.org/js/xpc/ContextStack;1");
if (!stack) {
ERR("Failed to get JSContextStack");
return NS_OK;
}
JSContext *cx = stack->GetSafeJSContext();
if (!cx) {
ERR("Failed to GetSafeJSContext");
return NS_OK;
}
// Parse the JSON value.
nsDependentString dataStr(aData);
JS::Value val;
if (!JS_ParseJSON(cx, dataStr.get(), dataStr.Length(), &val) ||
!val.isObject()) {
return NS_OK;
}
// Get the key, which should be the JS string "time.timezone".
JSObject &obj(val.toObject());
JS::Value key;
if (!JS_GetProperty(cx, &obj, "key", &key) ||
!key.isString()) {
return NS_OK;
}
JSBool match;
if (!JS_StringEqualsAscii(cx, key.toString(), TIME_TIMEZONE, &match) ||
match != JS_TRUE) {
return NS_OK;
}
// Get the value, which should be a JS string like "America/Chicago".
JS::Value value;
if (!JS_GetProperty(cx, &obj, "value", &value) ||
!value.isString()) {
return NS_OK;
}
// Set the system timezone.
return SetTimeZone(value, cx);
}
示例11: GetDOMClass
bool
ValueHasISupportsPrivate(const JS::Value &v)
{
if (!v.isObject()) {
return false;
}
const DOMClass* domClass = GetDOMClass(&v.toObject());
if (domClass) {
return domClass->mDOMObjectIsISupports;
}
JSClass* clasp = ::JS_GetClass(&v.toObject());
const uint32_t HAS_PRIVATE_NSISUPPORTS =
JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS;
return (clasp->flags & HAS_PRIVATE_NSISUPPORTS) == HAS_PRIVATE_NSISUPPORTS;
}
示例12: ar
nsresult
nsJSON::EncodeInternal(JSContext* cx, const JS::Value& aValue,
nsJSONWriter* writer)
{
JSAutoRequest ar(cx);
// Backward compatibility:
// nsIJSON does not allow to serialize anything other than objects
if (!aValue.isObject()) {
return NS_ERROR_INVALID_ARG;
}
JSObject* obj = &aValue.toObject();
JS::Value val = aValue;
/* Backward compatibility:
* Manually call toJSON if implemented by the object and check that
* the result is still an object
* Note: It is perfectly fine to not implement toJSON, so it is
* perfectly fine for GetMethod to fail
*/
JS::Value toJSON;
if (JS_GetMethod(cx, obj, "toJSON", NULL, &toJSON) &&
!JSVAL_IS_PRIMITIVE(toJSON) &&
JS_ObjectIsCallable(cx, JSVAL_TO_OBJECT(toJSON))) {
// If toJSON is implemented, it must not throw
if (!JS_CallFunctionValue(cx, obj, toJSON, 0, NULL, &val)) {
if (JS_IsExceptionPending(cx))
// passing NS_OK will throw the pending exception
return NS_OK;
// No exception, but still failed
return NS_ERROR_FAILURE;
}
// Backward compatibility:
// nsIJSON does not allow to serialize anything other than objects
if (JSVAL_IS_PRIMITIVE(val))
return NS_ERROR_INVALID_ARG;
}
// GetMethod may have thrown
else if (JS_IsExceptionPending(cx))
// passing NS_OK will throw the pending exception
return NS_OK;
// Backward compatibility:
// function shall not pass, just "plain" objects and arrays
JSType type = JS_TypeOfValue(cx, val);
if (type == JSTYPE_FUNCTION)
return NS_ERROR_INVALID_ARG;
// We're good now; try to stringify
if (!JS_Stringify(cx, &val, NULL, JSVAL_NULL, WriteCallback, writer))
return NS_ERROR_FAILURE;
return NS_OK;
}
示例13:
//static
JSObject *
DOMProxyHandler::GetExpandoObject(JSObject *obj)
{
MOZ_ASSERT(IsDOMProxy(obj), "expected a DOM proxy object");
JS::Value v = js::GetProxyExtra(obj, JSPROXYSLOT_EXPANDO);
if (v.isObject()) {
return &v.toObject();
}
if (v.isUndefined()) {
return nullptr;
}
js::ExpandoAndGeneration* expandoAndGeneration =
static_cast<js::ExpandoAndGeneration*>(v.toPrivate());
v = expandoAndGeneration->expando;
return v.isUndefined() ? nullptr : &v.toObject();
}
示例14: regions
// JS-to-native helpers
// Setter for weighted regions: { top, bottom, left, right, weight }
nsresult
nsDOMCameraControl::Set(JSContext* aCx, uint32_t aKey, const JS::Value& aValue, uint32_t aLimit)
{
if (aLimit == 0) {
DOM_CAMERA_LOGI("%s:%d : aLimit = 0, nothing to do\n", __func__, __LINE__);
return NS_OK;
}
if (!aValue.isObject()) {
return NS_ERROR_INVALID_ARG;
}
uint32_t length = 0;
JS::Rooted<JSObject*> regions(aCx, &aValue.toObject());
if (!JS_GetArrayLength(aCx, regions, &length)) {
return NS_ERROR_FAILURE;
}
DOM_CAMERA_LOGI("%s:%d : got %d regions (limited to %d)\n", __func__, __LINE__, length, aLimit);
if (length > aLimit) {
length = aLimit;
}
nsTArray<ICameraControl::Region> regionArray;
regionArray.SetCapacity(length);
for (uint32_t i = 0; i < length; ++i) {
JS::Rooted<JS::Value> v(aCx);
if (!JS_GetElement(aCx, regions, i, &v)) {
return NS_ERROR_FAILURE;
}
CameraRegion region;
if (!region.Init(aCx, v)) {
return NS_ERROR_FAILURE;
}
ICameraControl::Region* r = regionArray.AppendElement();
r->top = region.mTop;
r->left = region.mLeft;
r->bottom = region.mBottom;
r->right = region.mRight;
r->weight = region.mWeight;
DOM_CAMERA_LOGI("region %d: top=%d, left=%d, bottom=%d, right=%d, weight=%u\n",
i,
r->top,
r->left,
r->bottom,
r->right,
r->weight
);
}
return mCameraControl->Set(aKey, regionArray);
}
示例15:
nsresult
MobileMessageManager::GetMessageId(AutoPushJSContext &aCx,
const JS::Value &aMessage, int32_t &aId)
{
nsCOMPtr<nsIDOMMozSmsMessage> smsMessage =
do_QueryInterface(nsContentUtils::XPConnect()->GetNativeOfWrapper(aCx, &aMessage.toObject()));
if (smsMessage) {
return smsMessage->GetId(&aId);
}
nsCOMPtr<nsIDOMMozMmsMessage> mmsMessage =
do_QueryInterface(nsContentUtils::XPConnect()->GetNativeOfWrapper(aCx, &aMessage.toObject()));
if (mmsMessage) {
return mmsMessage->GetId(&aId);
}
return NS_ERROR_INVALID_ARG;
}