WindowObject.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/NavigatorObject.h>
  33. #include <LibWeb/Bindings/WindowObject.h>
  34. #include <LibWeb/DOM/Document.h>
  35. #include <LibWeb/DOM/Window.h>
  36. namespace Web {
  37. namespace Bindings {
  38. WindowObject::WindowObject(Window& impl)
  39. : m_impl(impl)
  40. {
  41. put_native_property("document", document_getter, document_setter);
  42. put_native_function("alert", alert);
  43. put_native_function("setInterval", set_interval, 1);
  44. put_native_function("setTimeout", set_timeout, 1);
  45. put_native_function("requestAnimationFrame", request_animation_frame, 1);
  46. put_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
  47. put("navigator", heap().allocate<NavigatorObject>());
  48. }
  49. WindowObject::~WindowObject()
  50. {
  51. }
  52. static Window* impl_from(JS::Interpreter& interpreter)
  53. {
  54. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  55. if (!this_object) {
  56. ASSERT_NOT_REACHED();
  57. return nullptr;
  58. }
  59. if (StringView("WindowObject") != this_object->class_name()) {
  60. interpreter.throw_exception<JS::Error>("TypeError", "That's not a WindowObject, bro.");
  61. return nullptr;
  62. }
  63. return &static_cast<WindowObject*>(this_object)->impl();
  64. }
  65. JS::Value WindowObject::alert(JS::Interpreter& interpreter)
  66. {
  67. auto* impl = impl_from(interpreter);
  68. if (!impl)
  69. return {};
  70. auto& arguments = interpreter.call_frame().arguments;
  71. if (arguments.size() < 1)
  72. return {};
  73. impl->alert(arguments[0].to_string());
  74. return {};
  75. }
  76. JS::Value WindowObject::set_interval(JS::Interpreter& interpreter)
  77. {
  78. auto* impl = impl_from(interpreter);
  79. if (!impl)
  80. return {};
  81. auto& arguments = interpreter.call_frame().arguments;
  82. if (arguments.size() < 2)
  83. return {};
  84. auto* callback_object = arguments[0].to_object(interpreter.heap());
  85. if (!callback_object)
  86. return {};
  87. if (!callback_object->is_function())
  88. return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
  89. impl->set_interval(*static_cast<JS::Function*>(callback_object), arguments[1].to_i32());
  90. return {};
  91. }
  92. JS::Value WindowObject::set_timeout(JS::Interpreter& interpreter)
  93. {
  94. auto* impl = impl_from(interpreter);
  95. if (!impl)
  96. return {};
  97. auto& arguments = interpreter.call_frame().arguments;
  98. if (arguments.size() < 1)
  99. return {};
  100. auto* callback_object = arguments[0].to_object(interpreter.heap());
  101. if (!callback_object)
  102. return {};
  103. if (!callback_object->is_function())
  104. return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
  105. i32 interval = 0;
  106. if (interpreter.argument_count() >= 2)
  107. interval = arguments[1].to_i32();
  108. impl->set_timeout(*static_cast<JS::Function*>(callback_object), interval);
  109. return {};
  110. }
  111. JS::Value WindowObject::request_animation_frame(JS::Interpreter& interpreter)
  112. {
  113. auto* impl = impl_from(interpreter);
  114. if (!impl)
  115. return {};
  116. auto& arguments = interpreter.call_frame().arguments;
  117. if (arguments.size() < 1)
  118. return {};
  119. auto* callback_object = arguments[0].to_object(interpreter.heap());
  120. if (!callback_object)
  121. return {};
  122. if (!callback_object->is_function())
  123. return interpreter.throw_exception<JS::Error>("TypeError", "Not a function");
  124. return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
  125. }
  126. JS::Value WindowObject::cancel_animation_frame(JS::Interpreter& interpreter)
  127. {
  128. auto* impl = impl_from(interpreter);
  129. if (!impl)
  130. return {};
  131. auto& arguments = interpreter.call_frame().arguments;
  132. if (arguments.size() < 1)
  133. return {};
  134. impl->cancel_animation_frame(arguments[0].to_i32());
  135. return {};
  136. }
  137. JS::Value WindowObject::document_getter(JS::Interpreter& interpreter)
  138. {
  139. auto* impl = impl_from(interpreter);
  140. if (!impl)
  141. return {};
  142. return wrap(interpreter.heap(), impl->document());
  143. }
  144. void WindowObject::document_setter(JS::Interpreter&, JS::Value)
  145. {
  146. // FIXME: Figure out what we should do here. Just ignore attempts to set window.document for now.
  147. }
  148. }
  149. }