WindowObject.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/FlyString.h>
  27. #include <AK/Function.h>
  28. #include <LibJS/Interpreter.h>
  29. #include <LibJS/Runtime/Error.h>
  30. #include <LibJS/Runtime/Function.h>
  31. #include <LibWeb/Bindings/DocumentWrapper.h>
  32. #include <LibWeb/Bindings/WindowObject.h>
  33. #include <LibWeb/DOM/Document.h>
  34. #include <LibWeb/DOM/Window.h>
  35. namespace Web {
  36. namespace Bindings {
  37. WindowObject::WindowObject(Window& impl)
  38. : m_impl(impl)
  39. {
  40. put_native_property("document", document_getter, document_setter);
  41. put_native_function("alert", alert);
  42. put_native_function("setInterval", set_interval);
  43. put_native_function("requestAnimationFrame", request_animation_frame);
  44. put_native_function("cancelAnimationFrame", cancel_animation_frame);
  45. }
  46. WindowObject::~WindowObject()
  47. {
  48. }
  49. static Window* impl_from(JS::Interpreter& interpreter)
  50. {
  51. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  52. if (!this_object) {
  53. ASSERT_NOT_REACHED();
  54. return nullptr;
  55. }
  56. if (StringView("WindowObject") != this_object->class_name()) {
  57. interpreter.throw_exception<JS::Error>("TypeError", "That's not a WindowObject, bro.");
  58. return nullptr;
  59. }
  60. return &static_cast<WindowObject*>(this_object)->impl();
  61. }
  62. JS::Value WindowObject::alert(JS::Interpreter& interpreter)
  63. {
  64. auto* impl = impl_from(interpreter);
  65. if (!impl)
  66. return {};
  67. auto& arguments = interpreter.call_frame().arguments;
  68. if (arguments.size() < 1)
  69. return {};
  70. impl->alert(arguments[0].to_string());
  71. return {};
  72. }
  73. JS::Value WindowObject::set_interval(JS::Interpreter& interpreter)
  74. {
  75. auto* impl = impl_from(interpreter);
  76. if (!impl)
  77. return {};
  78. auto& arguments = interpreter.call_frame().arguments;
  79. if (arguments.size() < 2)
  80. return {};
  81. auto* callback_object = arguments[0].to_object(interpreter.heap());
  82. if (!callback_object)
  83. return {};
  84. if (!callback_object->is_function())
  85. return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
  86. impl->set_interval(*static_cast<JS::Function*>(callback_object), arguments[1].to_i32());
  87. return {};
  88. }
  89. JS::Value WindowObject::request_animation_frame(JS::Interpreter& interpreter)
  90. {
  91. auto* impl = impl_from(interpreter);
  92. if (!impl)
  93. return {};
  94. auto& arguments = interpreter.call_frame().arguments;
  95. if (arguments.size() < 1)
  96. return {};
  97. auto* callback_object = arguments[0].to_object(interpreter.heap());
  98. if (!callback_object)
  99. return {};
  100. if (!callback_object->is_function())
  101. return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
  102. return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
  103. }
  104. JS::Value WindowObject::cancel_animation_frame(JS::Interpreter& interpreter)
  105. {
  106. auto* impl = impl_from(interpreter);
  107. if (!impl)
  108. return {};
  109. auto& arguments = interpreter.call_frame().arguments;
  110. if (arguments.size() < 1)
  111. return {};
  112. impl->cancel_animation_frame(arguments[0].to_i32());
  113. return {};
  114. }
  115. JS::Value WindowObject::document_getter(JS::Interpreter& interpreter)
  116. {
  117. auto* impl = impl_from(interpreter);
  118. if (!impl)
  119. return {};
  120. return wrap(interpreter.heap(), impl->document());
  121. }
  122. void WindowObject::document_setter(JS::Interpreter&, JS::Value)
  123. {
  124. // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now.
  125. }
  126. }
  127. }