Line data Source code
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/. */
6 :
7 : #include "mozilla/dom/TextEncoder.h"
8 : #include "mozilla/Encoding.h"
9 :
10 : namespace mozilla {
11 : namespace dom {
12 :
13 : void
14 0 : TextEncoder::Init()
15 : {
16 0 : }
17 :
18 : void
19 0 : TextEncoder::Encode(JSContext* aCx,
20 : JS::Handle<JSObject*> aObj,
21 : const nsAString& aString,
22 : JS::MutableHandle<JSObject*> aRetval,
23 : ErrorResult& aRv)
24 : {
25 0 : nsAutoCString utf8;
26 : nsresult rv;
27 : const Encoding* ignored;
28 0 : Tie(rv, ignored) = UTF_8_ENCODING->Encode(aString, utf8);
29 0 : if (NS_FAILED(rv)) {
30 0 : aRv.Throw(rv);
31 0 : return;
32 : }
33 :
34 0 : JSAutoCompartment ac(aCx, aObj);
35 0 : JSObject* outView = Uint8Array::Create(
36 0 : aCx, utf8.Length(), reinterpret_cast<const uint8_t*>(utf8.BeginReading()));
37 0 : if (!outView) {
38 0 : aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
39 0 : return;
40 : }
41 :
42 0 : aRetval.set(outView);
43 : }
44 :
45 : void
46 0 : TextEncoder::GetEncoding(nsAString& aEncoding)
47 : {
48 0 : aEncoding.AssignLiteral("utf-8");
49 0 : }
50 :
51 : } // namespace dom
52 : } // namespace mozilla
|