WindowObject.cpp 5.7 KB

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