2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-28 14:19:18 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/TextEncoder.h"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
2012-09-28 14:19:18 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
void
|
2016-03-24 13:27:15 +03:00
|
|
|
TextEncoder::Init()
|
2012-09-28 14:19:18 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
void
|
2013-08-23 09:17:10 +04:00
|
|
|
TextEncoder::Encode(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
const nsAString& aString,
|
2014-11-21 22:58:45 +03:00
|
|
|
JS::MutableHandle<JSObject*> aRetval,
|
2013-08-23 09:17:10 +04:00
|
|
|
ErrorResult& aRv)
|
2012-09-28 14:19:18 +04:00
|
|
|
{
|
2017-04-27 13:27:03 +03:00
|
|
|
nsAutoCString utf8;
|
|
|
|
nsresult rv;
|
|
|
|
const Encoding* ignored;
|
|
|
|
Tie(rv, ignored) = UTF_8_ENCODING->Encode(aString, utf8);
|
2012-09-28 14:19:18 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
|
|
|
|
JSAutoCompartment ac(aCx, aObj);
|
|
|
|
JSObject* outView = Uint8Array::Create(
|
|
|
|
aCx, utf8.Length(), reinterpret_cast<const uint8_t*>(utf8.BeginReading()));
|
|
|
|
if (!outView) {
|
2012-09-28 14:19:18 +04:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2014-06-12 00:26:52 +04:00
|
|
|
return;
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
2014-06-12 00:26:52 +04:00
|
|
|
aRetval.set(outView);
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-23 09:17:10 +04:00
|
|
|
TextEncoder::GetEncoding(nsAString& aEncoding)
|
2012-09-28 14:19:18 +04:00
|
|
|
{
|
2016-03-24 13:27:15 +03:00
|
|
|
aEncoding.AssignLiteral("utf-8");
|
2012-09-28 14:19:18 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|