no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / encoding / TextEncoder.cpp
blob68ee4aa02d502f33287b83f3288f137409c816f2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/TextEncoder.h"
8 #include "mozilla/CheckedInt.h"
9 #include "mozilla/ErrorResult.h"
10 #include "mozilla/UniquePtrExtensions.h"
11 #include "nsReadableUtils.h"
13 namespace mozilla::dom {
15 void TextEncoder::Encode(JSContext* aCx, JS::Handle<JSObject*> aObj,
16 const nsACString& aUtf8String,
17 JS::MutableHandle<JSObject*> aRetval,
18 ErrorResult& aRv) {
19 JSAutoRealm ar(aCx, aObj);
20 JSObject* outView = Uint8Array::Create(aCx, aUtf8String, aRv);
21 if (aRv.Failed()) {
22 return;
25 aRetval.set(outView);
28 void TextEncoder::EncodeInto(JSContext* aCx, JS::Handle<JSString*> aSrc,
29 const Uint8Array& aDst,
30 TextEncoderEncodeIntoResult& aResult,
31 OOMReporter& aError) {
32 DebugOnly<size_t> dstLength = 0;
33 auto maybe = aDst.ProcessData(
34 [&](const Span<uint8_t>& aData, JS::AutoCheckCannotGC&&) {
35 dstLength = aData.Length();
36 return JS_EncodeStringToUTF8BufferPartial(aCx, aSrc,
37 AsWritableChars(aData));
38 });
39 if (!maybe) {
40 aError.ReportOOM();
41 return;
43 size_t read;
44 size_t written;
45 std::tie(read, written) = *maybe;
46 MOZ_ASSERT(written <= dstLength);
47 aResult.mRead.Construct() = read;
48 aResult.mWritten.Construct() = written;
51 void TextEncoder::GetEncoding(nsACString& aEncoding) {
52 aEncoding.AssignLiteral("utf-8");
55 } // namespace mozilla::dom