本文整理汇总了C++中js::Value类的典型用法代码示例。如果您正苦于以下问题:C++ Value类的具体用法?C++ Value怎么用?C++ Value使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Value类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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));
}
示例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: expando
js::DOMProxyShadowsResult
DOMProxyShadows(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id)
{
JS::Rooted<JSObject*> expando(cx, DOMProxyHandler::GetExpandoObject(proxy));
JS::Value v = js::GetProxyExtra(proxy, JSPROXYSLOT_EXPANDO);
bool isOverrideBuiltins = !v.isObject() && !v.isUndefined();
if (expando) {
bool hasOwn;
if (!JS_AlreadyHasOwnPropertyById(cx, expando, id, &hasOwn))
return js::ShadowCheckFailed;
if (hasOwn) {
return isOverrideBuiltins ?
js::ShadowsViaIndirectExpando : js::ShadowsViaDirectExpando;
}
}
if (!isOverrideBuiltins) {
// Our expando, if any, didn't shadow, so we're not shadowing at all.
return js::DoesntShadow;
}
bool hasOwn;
if (!GetProxyHandler(proxy)->hasOwn(cx, proxy, id, &hasOwn))
return js::ShadowCheckFailed;
return hasOwn ? js::Shadows : js::DoesntShadowUnique;
}
示例6: 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);
}
示例7: CheckExpandoAndGeneration
static inline void CheckExpandoAndGeneration(
JSObject* proxy, js::ExpandoAndGeneration* expandoAndGeneration) {
#ifdef DEBUG
JS::Value value = expandoAndGeneration->expando;
if (!value.isUndefined()) CheckExpandoObject(proxy, value);
#endif
}
示例8: str
bool
gjs_string_to_utf8 (JSContext *context,
const JS::Value value,
char **utf8_string_p)
{
gsize len;
char *bytes;
JS_BeginRequest(context);
if (!value.isString()) {
gjs_throw(context,
"Value is not a string, cannot convert to UTF-8");
JS_EndRequest(context);
return false;
}
JS::RootedString str(context, value.toString());
len = JS_GetStringEncodingLength(context, str);
if (len == (gsize)(-1)) {
JS_EndRequest(context);
return false;
}
if (utf8_string_p) {
bytes = JS_EncodeStringToUTF8(context, str);
*utf8_string_p = bytes;
}
JS_EndRequest(context);
return true;
}
示例9:
/**
* gjs_string_get_char16_data:
* @context: js context
* @value: a JS::Value
* @data_p: address to return allocated data buffer
* @len_p: address to return length of data (number of 16-bit characters)
*
* Get the binary data (as a sequence of 16-bit characters) in the JSString
* contained in @value.
* Throws a JS exception if value is not a string.
*
* Returns: false if exception thrown
**/
bool
gjs_string_get_char16_data(JSContext *context,
JS::Value value,
char16_t **data_p,
size_t *len_p)
{
const char16_t *js_data;
bool retval = false;
JS_BeginRequest(context);
if (!value.isString()) {
gjs_throw(context,
"Value is not a string, can't return binary data from it");
goto out;
}
js_data = JS_GetStringCharsAndLength(context, value.toString(), len_p);
if (js_data == NULL)
goto out;
*data_p = (char16_t *) g_memdup(js_data, sizeof(*js_data) * (*len_p));
retval = true;
out:
JS_EndRequest(context);
return retval;
}
示例10:
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;
}
示例11: 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;
}
示例12: 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);
}
示例13:
void
nsGeolocationSettings::HandleGeolocationAlaEnabledChange(const JS::Value& aVal)
{
if (!aVal.isBoolean()) {
return;
}
mAlaEnabled = aVal.toBoolean();
}
示例14: jsString
NS_IMETHODIMP
nsScreen::MozLockOrientation(const JS::Value& aOrientation, JSContext* aCx,
bool* aReturn)
{
if (aOrientation.isObject()) {
JS::Rooted<JSObject*> seq(aCx, &aOrientation.toObject());
if (IsArrayLike(aCx, seq)) {
uint32_t length;
// JS_GetArrayLength actually works on all objects
if (!JS_GetArrayLength(aCx, seq, &length)) {
return NS_ERROR_FAILURE;
}
Sequence<nsString> orientations;
if (!orientations.SetCapacity(length)) {
return NS_ERROR_OUT_OF_MEMORY;
}
for (uint32_t i = 0; i < length; ++i) {
JS::Rooted<JS::Value> temp(aCx);
if (!JS_GetElement(aCx, seq, i, &temp)) {
return NS_ERROR_FAILURE;
}
JS::RootedString jsString(aCx, JS_ValueToString(aCx, temp));
if (!jsString) {
return NS_ERROR_FAILURE;
}
nsDependentJSString str;
if (!str.init(aCx, jsString)) {
return NS_ERROR_FAILURE;
}
*orientations.AppendElement() = str;
}
ErrorResult rv;
*aReturn = MozLockOrientation(orientations, rv);
return rv.ErrorCode();
}
}
JS::RootedString jsString(aCx, JS_ValueToString(aCx, aOrientation));
if (!jsString) {
return NS_ERROR_FAILURE;
}
nsDependentJSString orientation;
if (!orientation.init(aCx, jsString)) {
return NS_ERROR_FAILURE;
}
ErrorResult rv;
*aReturn = MozLockOrientation(orientation, rv);
return rv.ErrorCode();
}
示例15: VerifyJSValueIsString
static bool VerifyJSValueIsString(
JSContext *cx, const JS::Value &value, const char *string) {
JSBool match;
if (!value.isString() ||
!JS_StringEqualsAscii(cx, value.toString(), string, &match) ||
(match != JS_TRUE)) {
return false;
}
return true;
}