CanvasRenderingContext2DWrapper.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/PrimitiveString.h>
  30. #include <LibJS/Runtime/Value.h>
  31. #include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
  32. #include <LibWeb/DOM/CanvasRenderingContext2D.h>
  33. namespace Web {
  34. namespace Bindings {
  35. CanvasRenderingContext2DWrapper* wrap(JS::Heap& heap, CanvasRenderingContext2D& impl)
  36. {
  37. return static_cast<CanvasRenderingContext2DWrapper*>(wrap_impl(heap, impl));
  38. }
  39. CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRenderingContext2D& impl)
  40. : m_impl(impl)
  41. {
  42. put_native_property("fillStyle", fill_style_getter, fill_style_setter);
  43. put_native_function("fillRect", fill_rect, 4);
  44. }
  45. CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper()
  46. {
  47. }
  48. static CanvasRenderingContext2D* impl_from(JS::Interpreter& interpreter)
  49. {
  50. auto* this_object = interpreter.this_value().to_object(interpreter.heap());
  51. if (!this_object)
  52. return nullptr;
  53. // FIXME: Verify that it's a CanvasRenderingContext2DWrapper somehow!
  54. return &static_cast<CanvasRenderingContext2DWrapper*>(this_object)->impl();
  55. }
  56. JS::Value CanvasRenderingContext2DWrapper::fill_rect(JS::Interpreter& interpreter)
  57. {
  58. auto* impl = impl_from(interpreter);
  59. if (!impl)
  60. return {};
  61. auto& arguments = interpreter.call_frame().arguments;
  62. if (arguments.size() >= 4)
  63. impl->fill_rect(arguments[0].to_i32(), arguments[1].to_i32(), arguments[2].to_i32(), arguments[3].to_i32());
  64. return JS::js_undefined();
  65. }
  66. JS::Value CanvasRenderingContext2DWrapper::fill_style_getter(JS::Interpreter& interpreter)
  67. {
  68. auto* impl = impl_from(interpreter);
  69. if (!impl)
  70. return {};
  71. return JS::js_string(interpreter, impl->fill_style());
  72. }
  73. void CanvasRenderingContext2DWrapper::fill_style_setter(JS::Interpreter& interpreter, JS::Value value)
  74. {
  75. if (auto* impl = impl_from(interpreter))
  76. impl->set_fill_style(value.to_string());
  77. }
  78. }
  79. }