Serenity Operating System
at hosted 140 lines 5.2 kB view raw
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 27#include <AK/FlyString.h> 28#include <AK/Function.h> 29#include <LibJS/Interpreter.h> 30#include <LibJS/Runtime/PrimitiveString.h> 31#include <LibJS/Runtime/Value.h> 32#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h> 33#include <LibWeb/DOM/CanvasRenderingContext2D.h> 34 35namespace Web { 36namespace Bindings { 37 38CanvasRenderingContext2DWrapper* wrap(JS::Heap& heap, CanvasRenderingContext2D& impl) 39{ 40 return static_cast<CanvasRenderingContext2DWrapper*>(wrap_impl(heap, impl)); 41} 42 43CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRenderingContext2D& impl) 44 : m_impl(impl) 45{ 46 put_native_property("fillStyle", fill_style_getter, fill_style_setter); 47 put_native_function("fillRect", fill_rect, 4); 48 put_native_function("scale", scale, 2); 49 put_native_function("translate", translate, 2); 50 put_native_property("strokeStyle", stroke_style_getter, stroke_style_setter); 51 put_native_function("strokeRect", stroke_rect, 4); 52} 53 54CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper() 55{ 56} 57 58static CanvasRenderingContext2D* impl_from(JS::Interpreter& interpreter) 59{ 60 auto* this_object = interpreter.this_value().to_object(interpreter.heap()); 61 if (!this_object) 62 return nullptr; 63 // FIXME: Verify that it's a CanvasRenderingContext2DWrapper somehow! 64 return &static_cast<CanvasRenderingContext2DWrapper*>(this_object)->impl(); 65} 66 67JS::Value CanvasRenderingContext2DWrapper::fill_rect(JS::Interpreter& interpreter) 68{ 69 auto* impl = impl_from(interpreter); 70 if (!impl) 71 return {}; 72 auto& arguments = interpreter.call_frame().arguments; 73 if (arguments.size() >= 4) 74 impl->fill_rect(arguments[0].to_double(), arguments[1].to_double(), arguments[2].to_double(), arguments[3].to_double()); 75 return JS::js_undefined(); 76} 77 78JS::Value CanvasRenderingContext2DWrapper::stroke_rect(JS::Interpreter& interpreter) 79{ 80 auto* impl = impl_from(interpreter); 81 if (!impl) 82 return {}; 83 auto& arguments = interpreter.call_frame().arguments; 84 if (arguments.size() >= 4) 85 impl->stroke_rect(arguments[0].to_double(), arguments[1].to_double(), arguments[2].to_double(), arguments[3].to_double()); 86 return JS::js_undefined(); 87} 88 89JS::Value CanvasRenderingContext2DWrapper::scale(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() >= 2) 96 impl->scale(arguments[0].to_double(), arguments[1].to_double()); 97 return JS::js_undefined(); 98} 99 100JS::Value CanvasRenderingContext2DWrapper::translate(JS::Interpreter& interpreter) 101{ 102 auto* impl = impl_from(interpreter); 103 if (!impl) 104 return {}; 105 auto& arguments = interpreter.call_frame().arguments; 106 if (arguments.size() >= 2) 107 impl->translate(arguments[0].to_double(), arguments[1].to_double()); 108 return JS::js_undefined(); 109} 110 111JS::Value CanvasRenderingContext2DWrapper::fill_style_getter(JS::Interpreter& interpreter) 112{ 113 auto* impl = impl_from(interpreter); 114 if (!impl) 115 return {}; 116 return JS::js_string(interpreter, impl->fill_style()); 117} 118 119void CanvasRenderingContext2DWrapper::fill_style_setter(JS::Interpreter& interpreter, JS::Value value) 120{ 121 if (auto* impl = impl_from(interpreter)) 122 impl->set_fill_style(value.to_string()); 123} 124 125JS::Value CanvasRenderingContext2DWrapper::stroke_style_getter(JS::Interpreter& interpreter) 126{ 127 auto* impl = impl_from(interpreter); 128 if (!impl) 129 return {}; 130 return JS::js_string(interpreter, impl->stroke_style()); 131} 132 133void CanvasRenderingContext2DWrapper::stroke_style_setter(JS::Interpreter& interpreter, JS::Value value) 134{ 135 if (auto* impl = impl_from(interpreter)) 136 impl->set_stroke_style(value.to_string()); 137} 138 139} 140}