WindowObject.cpp 4.9 KB

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