LibWeb: Add atob/btoa to WindowObject

This commit is contained in:
Joel Puig Rubio 2020-06-20 16:44:27 +02:00 committed by Andreas Kling
parent 37598de582
commit 1d59053494
Notes: sideshowbarker 2024-07-19 05:31:13 +09:00
2 changed files with 35 additions and 0 deletions

View File

@ -24,8 +24,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Base64.h>
#include <AK/ByteBuffer.h>
#include <AK/FlyString.h>
#include <AK/Function.h>
#include <AK/String.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/Function.h>
@ -60,6 +63,8 @@ void WindowObject::initialize()
define_native_function("setTimeout", set_timeout, 1);
define_native_function("requestAnimationFrame", request_animation_frame, 1);
define_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
define_native_function("atob", atob, 1);
define_native_function("btoa", btoa, 1);
define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
@ -205,6 +210,34 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
if (!interpreter.argument_count())
return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "atob");
auto string = interpreter.argument(0).to_string(interpreter);
if (interpreter.exception())
return {};
auto decoded = decode_base64(StringView(string));
return JS::js_string(interpreter, String::copy(decoded));
}
JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
if (!interpreter.argument_count())
return interpreter.throw_exception<JS::TypeError>(JS::ErrorType::BadArgCountOne, "btoa");
auto string = interpreter.argument(0).to_string(interpreter);
if (interpreter.exception())
return {};
auto encoded = encode_base64(StringView(string));
return JS::js_string(interpreter, String::copy(encoded));
}
JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
{
auto* impl = impl_from(interpreter, global_object);

View File

@ -60,6 +60,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(set_timeout);
JS_DECLARE_NATIVE_FUNCTION(request_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(cancel_animation_frame);
JS_DECLARE_NATIVE_FUNCTION(atob);
JS_DECLARE_NATIVE_FUNCTION(btoa);
NonnullRefPtr<Window> m_impl;