void WorkerDebuggerGlobalScope::CreateSandbox(JSContext* aCx, const nsAString& aName, JS::Handle<JSObject*> aPrototype, JS::MutableHandle<JSObject*> aResult, ErrorResult& aRv) { mWorkerPrivate->AssertIsOnWorkerThread(); aResult.set(nullptr); JS::Rooted<JS::Value> protoVal(aCx); protoVal.setObjectOrNull(aPrototype); JS::Rooted<JSObject*> sandbox(aCx, SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::WorkerDebuggerSandbox, protoVal)); if (!sandbox) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } if (!JS_WrapObject(aCx, &sandbox)) { aRv.NoteJSContextException(aCx); return; } aResult.set(sandbox); }
void IDBIndex::GetKeyPath(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) { AssertIsOnOwningThread(); if (!mCachedKeyPath.isUndefined()) { MOZ_ASSERT(mRooted); JS::ExposeValueToActiveJS(mCachedKeyPath); aResult.set(mCachedKeyPath); return; } MOZ_ASSERT(!mRooted); aRv = GetKeyPath().ToJSVal(aCx, mCachedKeyPath); if (NS_WARN_IF(aRv.Failed())) { return; } if (mCachedKeyPath.isGCThing()) { mozilla::HoldJSObjects(this); mRooted = true; } JS::ExposeValueToActiveJS(mCachedKeyPath); aResult.set(mCachedKeyPath); }
/* jsval testJsval (in jsval a, in jsval b); */ NS_IMETHODIMP nsXPCTestParams::TestJsval(JS::Handle<JS::Value> a, JS::MutableHandle<JS::Value> b, JS::MutableHandle<JS::Value> _retval) { _retval.set(b); b.set(a); return NS_OK; }
void WebGLContext::GetExtension(JSContext* cx, const nsAString& wideName, JS::MutableHandle<JSObject*> retval, dom::CallerType callerType, ErrorResult& rv) { retval.set(nullptr); if (IsContextLost()) return; NS_LossyConvertUTF16toASCII name(wideName); WebGLExtensionID ext = WebGLExtensionID::Unknown; // step 1: figure what extension is wanted for (size_t i = 0; i < size_t(WebGLExtensionID::Max); i++) { WebGLExtensionID extension = WebGLExtensionID(i); if (CompareWebGLExtensionName(name, GetExtensionString(extension))) { ext = extension; break; } } if (ext == WebGLExtensionID::Unknown) return; // step 2: check if the extension is supported if (!IsExtensionSupported(callerType, ext)) return; // step 3: if the extension hadn't been previously been created, create it now, thus enabling it WebGLExtensionBase* extObj = EnableSupportedExtension(callerType, ext); if (!extObj) return; // Step 4: Enable any implied extensions. switch (ext) { case WebGLExtensionID::OES_texture_float: EnableSupportedExtension(callerType, WebGLExtensionID::WEBGL_color_buffer_float); break; case WebGLExtensionID::OES_texture_half_float: EnableSupportedExtension(callerType, WebGLExtensionID::EXT_color_buffer_half_float); break; default: break; } retval.set(WebGLObjectAsJSObject(cx, extObj, rv)); }
void WebGLExtensionMOZDebug::GetParameter(JSContext* cx, GLenum pname, JS::MutableHandle<JS::Value> retval, ErrorResult& er) const { const auto& gl = mContext->gl; gl->MakeCurrent(); switch (pname) { case LOCAL_GL_EXTENSIONS: { nsString ret; if (!gl->IsCoreProfile()) { const auto rawExts = (const char*)gl->fGetString(LOCAL_GL_EXTENSIONS); ret = NS_ConvertUTF8toUTF16(rawExts); } else { const auto& numExts = gl->GetIntAs<GLuint>(LOCAL_GL_NUM_EXTENSIONS); for (GLuint i = 0; i < numExts; i++) { const auto rawExt = (const char*)gl->fGetStringi(LOCAL_GL_EXTENSIONS, i); if (i > 0) { ret.AppendLiteral(" "); } ret.Append(NS_ConvertUTF8toUTF16(rawExt)); } } retval.set(StringValue(cx, ret, er)); return; } case LOCAL_GL_RENDERER: case LOCAL_GL_VENDOR: case LOCAL_GL_VERSION: { const auto raw = (const char*)gl->fGetString(pname); retval.set(StringValue(cx, NS_ConvertUTF8toUTF16(raw), er)); return; } case 0x10000: // "WSI_INFO" { nsCString info; gl->GetWSIInfo(&info); retval.set(StringValue(cx, NS_ConvertUTF8toUTF16(info), er)); return; } default: mContext->ErrorInvalidEnumArg("MOZ_debug.getParameter", "pname", pname); retval.set(JS::NullValue()); return; } }
void WaveShaperNode::GetCurve(JSContext* aCx, JS::MutableHandle<JSObject*> aRetval) { // Let's return a null value if the list is empty. if (mCurve.IsEmpty()) { aRetval.set(nullptr); return; } MOZ_ASSERT(mCurve.Length() >= 2); aRetval.set(Float32Array::Create(aCx, this, mCurve.Length(), mCurve.Elements())); }
nsresult DOMCameraCapabilities::ParameterListToNewArray(JSContext* aCx, JS::MutableHandle<JSObject*> aArray, uint32_t aKey, ParseItemAndAddFunc aParseItemAndAdd) { NS_ENSURE_TRUE(mCamera, NS_ERROR_NOT_AVAILABLE); const char* value = mCamera->GetParameterConstChar(aKey); if (!value) { // in case we get nonsense data back aArray.set(nullptr); return NS_OK; } aArray.set(JS_NewArrayObject(aCx, 0, nullptr)); if (!aArray) { return NS_ERROR_OUT_OF_MEMORY; } const char* p = value; uint32_t index = 0; nsresult rv; char* q; while (p) { /** * In C's string.h, strchr() is declared as returning 'char*'; in C++'s * cstring, it is declared as returning 'const char*', _except_ in MSVC, * where the C version is declared to return const like the C++ version. * * Unfortunately, for both cases, strtod() and strtol() take a 'char**' as * the end-of-conversion pointer, so we need to cast away strchr()'s * const-ness here to make the MSVC build everything happy. */ q = const_cast<char*>(strchr(p, ',')); if (q != p) { // skip consecutive delimiters, just in case rv = aParseItemAndAdd(aCx, aArray, index, p, &q); NS_ENSURE_SUCCESS(rv, rv); ++index; } p = q; if (p) { ++p; } } return JS_FreezeObject(aCx, aArray) ? NS_OK : NS_ERROR_FAILURE; }
void IDBCursor::GetPrimaryKey(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) { AssertIsOnOwningThread(); if (!mHaveValue) { aResult.setUndefined(); return; } if (!mHaveCachedPrimaryKey) { if (!mRooted) { mozilla::HoldJSObjects(this); mRooted = true; } const Key& key = (mType == Type_ObjectStore || mType == Type_ObjectStoreKey) ? mKey : mPrimaryKey; MOZ_ASSERT(!key.IsUnset()); aRv = key.ToJSVal(aCx, mCachedPrimaryKey); if (NS_WARN_IF(aRv.Failed())) { return; } mHaveCachedPrimaryKey = true; } JS::ExposeValueToActiveJS(mCachedPrimaryKey); aResult.set(mCachedPrimaryKey); }
bool BaseDOMProxyHandler::getPrototypeIfOrdinary( JSContext* cx, JS::Handle<JSObject*> proxy, bool* isOrdinary, JS::MutableHandle<JSObject*> proto) const { *isOrdinary = true; proto.set(GetStaticPrototype(proxy)); return true; }
void FileReader::GetResult(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) { JS::Rooted<JS::Value> result(aCx); if (mDataFormat == FILE_AS_ARRAYBUFFER) { if (mReadyState == DONE && mResultArrayBuffer) { result.setObject(*mResultArrayBuffer); } else { result.setNull(); } if (!JS_WrapValue(aCx, &result)) { aRv.Throw(NS_ERROR_FAILURE); return; } aResult.set(result); return; } nsString tmpResult = mResult; if (!xpc::StringToJsval(aCx, tmpResult, aResult)) { aRv.Throw(NS_ERROR_FAILURE); return; } }
void FetchStreamReader::StartConsuming(JSContext* aCx, JS::HandleObject aStream, JS::MutableHandle<JSObject*> aReader, ErrorResult& aRv) { MOZ_DIAGNOSTIC_ASSERT(!mReader); MOZ_DIAGNOSTIC_ASSERT(aStream); aRv.MightThrowJSException(); // Here, by spec, we can pick any global we want. Just to avoid extra // cross-compartment steps, we want to create the reader in the same // compartment of the owning Fetch Body object. // The same global will be used to retrieve data from this reader. JSAutoRealm ar(aCx, mGlobal->GetGlobalJSObject()); JS::Rooted<JSObject*> reader( aCx, JS::ReadableStreamGetReader(aCx, aStream, JS::ReadableStreamReaderMode::Default)); if (!reader) { aRv.StealExceptionFromJSContext(aCx); CloseAndRelease(aCx, NS_ERROR_DOM_INVALID_STATE_ERR); return; } mReader = reader; aReader.set(reader); aRv = mPipeOut->AsyncWait(this, 0, 0, mOwningEventTarget); if (NS_WARN_IF(aRv.Failed())) { return; } }
void IDBCursor::GetKey(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) { AssertIsOnOwningThread(); MOZ_ASSERT(!mKey.IsUnset() || !mHaveValue); if (!mHaveValue) { aResult.setUndefined(); return; } if (!mHaveCachedKey) { if (!mRooted) { mozilla::HoldJSObjects(this); mRooted = true; } aRv = mKey.ToJSVal(aCx, mCachedKey); if (NS_WARN_IF(aRv.Failed())) { return; } mHaveCachedKey = true; } JS::ExposeValueToActiveJS(mCachedKey); aResult.set(mCachedKey); }
void TextEncoder::Encode(JSContext* aCx, JS::Handle<JSObject*> aObj, const nsAString& aString, JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv) { nsAutoCString utf8; nsresult rv; const Encoding* ignored; Tie(rv, ignored) = UTF_8_ENCODING->Encode(aString, utf8); if (NS_FAILED(rv)) { aRv.Throw(rv); return; } JSAutoCompartment ac(aCx, aObj); JSObject* outView = Uint8Array::Create( aCx, utf8.Length(), reinterpret_cast<const uint8_t*>(utf8.BeginReading())); if (!outView) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } aRetval.set(outView); }
JSBool JavaObject::getPropertyWrapper(JSContext* ctx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp) { jsval rval; JSBool result = JavaObject::getProperty(ctx, obj.get(), id.get(), &rval); vp.set(rval); return result; }
void FetchStreamReader::StartConsuming(JSContext* aCx, JS::HandleObject aStream, JS::MutableHandle<JSObject*> aReader, ErrorResult& aRv) { MOZ_DIAGNOSTIC_ASSERT(!mReader); MOZ_DIAGNOSTIC_ASSERT(aStream); JS::Rooted<JSObject*> reader(aCx, JS::ReadableStreamGetReader(aCx, aStream, JS::ReadableStreamReaderMode::Default)); if (!reader) { aRv.StealExceptionFromJSContext(aCx); CloseAndRelease(aCx, NS_ERROR_DOM_INVALID_STATE_ERR); return; } mReader = reader; aReader.set(reader); aRv = mPipeOut->AsyncWait(this, 0, 0, mOwningEventTarget); if (NS_WARN_IF(aRv.Failed())) { return; } }
void IDBCursor::GetValue(JSContext* aCx, JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) { AssertIsOnOwningThread(); MOZ_ASSERT(mType == Type_ObjectStore || mType == Type_Index); if (!mHaveValue) { aResult.setUndefined(); return; } if (!mHaveCachedValue) { if (!mRooted) { mozilla::HoldJSObjects(this); mRooted = true; } JS::Rooted<JS::Value> val(aCx); if (NS_WARN_IF(!IDBObjectStore::DeserializeValue(aCx, mCloneInfo, &val))) { aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR); return; } IDBObjectStore::ClearCloneReadInfo(mCloneInfo); mCachedValue = val; mHaveCachedValue = true; } JS::ExposeValueToActiveJS(mCachedValue); aResult.set(mCachedValue); }
void WebGLExtensionDisjointTimerQuery::GetQueryObjectEXT(JSContext* cx, WebGLTimerQuery* query, GLenum pname, JS::MutableHandle<JS::Value> retval) { if (mIsLost) return; if (!mContext->ValidateObject("getQueryObjectEXT", query)) return; if (query == mActiveQuery.get()) { mContext->ErrorInvalidOperation("getQueryObjectEXT: Query must not be" " active."); } mContext->MakeContextCurrent(); // XXX: Note that the query result *may change* within the same task! // This does not follow the specification, which states that all calls // checking query results must return the same value until the event loop // is empty. switch (pname) { case LOCAL_GL_QUERY_RESULT_EXT: { GLuint64 result = 0; mContext->GL()->fGetQueryObjectui64v(query->mGLName, LOCAL_GL_QUERY_RESULT_EXT, &result); retval.set(JS::NumberValue(result)); break; } case LOCAL_GL_QUERY_RESULT_AVAILABLE_EXT: { GLuint avail = 0; mContext->GL()->fGetQueryObjectuiv(query->mGLName, LOCAL_GL_QUERY_RESULT_AVAILABLE_EXT, &avail); bool canBeAvailable = query->CanBeAvailable() || gfxPrefs::WebGLImmediateQueries(); retval.set(JS::BooleanValue(bool(avail) && canBeAvailable)); break; } default: mContext->ErrorInvalidEnumInfo("getQueryObjectEXT: Invalid query" " property.", pname); break; } }
void BluetoothGattCharacteristic::GetValue(JSContext* cx, JS::MutableHandle<JSObject*> aValue) const { aValue.set(mValue.IsEmpty() ? nullptr : ArrayBuffer::Create(cx, mValue.Length(), mValue.Elements())); }
void PushSubscription::GetKey(JSContext* aCx, PushEncryptionKeyName aType, JS::MutableHandle<JSObject*> aKey) { if (aType == PushEncryptionKeyName::P256dh && !mRawP256dhKey.IsEmpty()) { aKey.set(ArrayBuffer::Create(aCx, mRawP256dhKey.Length(), mRawP256dhKey.Elements())); } else if (aType == PushEncryptionKeyName::Auth && !mAuthSecret.IsEmpty()) { aKey.set(ArrayBuffer::Create(aCx, mAuthSecret.Length(), mAuthSecret.Elements())); } else { aKey.set(nullptr); } }
js::GetObjectProto(JSContext *cx, JS::Handle<JSObject*> obj, JS::MutableHandle<JSObject*> proto) { if (IsProxy(obj)) return JS_GetPrototype(cx, obj, proto); proto.set(reinterpret_cast<const shadow::Object*>(obj.get())->type->proto); return true; }
void TextEncoder::Encode(JSContext* aCx, JS::Handle<JSObject*> aObj, const nsAString& aString, const bool aStream, JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv) { // Run the steps of the encoding algorithm. int32_t srcLen = aString.Length(); int32_t maxLen; const char16_t* data = PromiseFlatString(aString).get(); nsresult rv = mEncoder->GetMaxLength(data, srcLen, &maxLen); if (NS_FAILED(rv)) { aRv.Throw(rv); return; } // Need a fallible allocator because the caller may be a content // and the content can specify the length of the string. static const fallible_t fallible = fallible_t(); nsAutoArrayPtr<char> buf(new (fallible) char[maxLen + 1]); if (!buf) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } int32_t dstLen = maxLen; rv = mEncoder->Convert(data, &srcLen, buf, &dstLen); // If the internal streaming flag is not set, then reset // the encoding algorithm state to the default values for encoding. if (!aStream) { int32_t finishLen = maxLen - dstLen; rv = mEncoder->Finish(buf + dstLen, &finishLen); if (NS_SUCCEEDED(rv)) { dstLen += finishLen; } } JSObject* outView = nullptr; if (NS_SUCCEEDED(rv)) { buf[dstLen] = '\0'; JSAutoCompartment ac(aCx, aObj); outView = Uint8Array::Create(aCx, dstLen, reinterpret_cast<uint8_t*>(buf.get())); if (!outView) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } } if (NS_FAILED(rv)) { aRv.Throw(rv); } aRetval.set(outView); }
/* static */ void PushUtil::CopyArrayToArrayBuffer(JSContext* aCx, const nsTArray<uint8_t>& aArray, JS::MutableHandle<JSObject*> aValue, ErrorResult& aRv) { if (aArray.IsEmpty()) { aValue.set(nullptr); return; } JS::Rooted<JSObject*> buffer(aCx, ArrayBuffer::Create(aCx, aArray.Length(), aArray.Elements())); if (NS_WARN_IF(!buffer)) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } aValue.set(buffer); }
void WebGLExtensionDisjointTimerQuery::GetQueryEXT(JSContext* cx, GLenum target, GLenum pname, JS::MutableHandle<JS::Value> retval) { if (mIsLost) return; mContext->MakeContextCurrent(); switch (pname) { case LOCAL_GL_CURRENT_QUERY_EXT: { if (target != LOCAL_GL_TIME_ELAPSED_EXT) { mContext->ErrorInvalidEnumInfo("getQueryEXT: Invalid query target.", target); return; } if (mActiveQuery) { JS::Rooted<JS::Value> v(cx); dom::GetOrCreateDOMReflector(cx, mActiveQuery.get(), &v); retval.set(v); } else { retval.set(JS::NullValue()); } break; } case LOCAL_GL_QUERY_COUNTER_BITS_EXT: { if (target != LOCAL_GL_TIME_ELAPSED_EXT && target != LOCAL_GL_TIMESTAMP_EXT) { mContext->ErrorInvalidEnumInfo("getQueryEXT: Invalid query target.", target); return; } GLint bits = 0; mContext->GL()->fGetQueryiv(target, pname, &bits); retval.set(JS::Int32Value(int32_t(bits))); break; } default: mContext->ErrorInvalidEnumInfo("getQueryEXT: Invalid query property.", pname); break; } }
void WorkerGlobalScope::GetJSTestingFunctions( JSContext* aCx, JS::MutableHandle<JSObject*> aFunctions, ErrorResult& aRv) { JSObject* obj = js::GetTestingFunctions(aCx); if (!obj) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } aFunctions.set(obj); }
/* static */ void PromiseDebugging::GetFullfillmentStack(GlobalObject&, JS::Handle<JSObject*> aPromise, JS::MutableHandle<JSObject*> aStack, ErrorResult& aRv) { Promise* promise = UnwrapPromise(aPromise, aRv); if (aRv.Failed()) { return; } aStack.set(promise->mFullfillmentStack); }
void WorkerDebuggerGlobalScope::GetGlobal(JSContext* aCx, JS::MutableHandle<JSObject*> aGlobal, ErrorResult& aRv) { WorkerGlobalScope* scope = mWorkerPrivate->GetOrCreateGlobalScope(aCx); if (!scope) { aRv.Throw(NS_ERROR_FAILURE); return; } aGlobal.set(scope->GetWrapper()); }
void nsPerformance::GetMozMemory(JSContext *aCx, JS::MutableHandle<JSObject*> aObj) { if (!mMozMemory) { mMozMemory = js::gc::NewMemoryInfoObject(aCx); if (mMozMemory) { mozilla::HoldJSObjects(this); } } aObj.set(mMozMemory); }
void FileReaderSync::ReadAsArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aScopeObj, JS::Handle<JSObject*> aBlob, JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv) { nsIDOMBlob* blob = file::GetDOMBlobFromJSObject(aBlob); if (!blob) { aRv.Throw(NS_ERROR_INVALID_ARG); return; } uint64_t blobSize; nsresult rv = blob->GetSize(&blobSize); if (NS_FAILED(rv)) { aRv.Throw(rv); return; } JS::Rooted<JSObject*> jsArrayBuffer(aCx, JS_NewArrayBuffer(aCx, blobSize)); if (!jsArrayBuffer) { // XXXkhuey we need a way to indicate to the bindings that the call failed // but there's already a pending exception that we should not clobber. aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } uint32_t bufferLength = JS_GetArrayBufferByteLength(jsArrayBuffer); uint8_t* arrayBuffer = JS_GetStableArrayBufferData(aCx, jsArrayBuffer); if (!arrayBuffer) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } nsCOMPtr<nsIInputStream> stream; rv = blob->GetInternalStream(getter_AddRefs(stream)); if (NS_FAILED(rv)) { aRv.Throw(rv); return; } uint32_t numRead; rv = stream->Read((char*)arrayBuffer, bufferLength, &numRead); if (NS_FAILED(rv)) { aRv.Throw(rv); return; } NS_ASSERTION(numRead == bufferLength, "failed to read data"); aRetval.set(jsArrayBuffer); }
void DOMMatrixReadOnly::ToFloat64Array(JSContext* aCx, JS::MutableHandle<JSObject*> aResult, ErrorResult& aRv) const { AutoTArray<double, 16> arr; arr.SetLength(16); GetDataFromMatrix(this, arr.Elements()); JS::Rooted<JS::Value> value(aCx); if (!ToJSValue(aCx, TypedArrayCreator<Float64Array>(arr), &value)) { aRv.Throw(NS_ERROR_OUT_OF_MEMORY); return; } aResult.set(&value.toObject()); }
void IDBRequest::GetResult(JS::MutableHandle<JS::Value> aResult, ErrorResult& aRv) const { AssertIsOnOwningThread(); if (!mHaveResultOrErrorCode) { aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR); return; } aResult.set(mResultVal); }