# HG changeset patch # User David Demelier # Date 1459678812 -7200 # Node ID a8aabea64f17582fe5a318a61c8678e1bb5d6c37 # Parent 3b9ea40722637fe4ff8816ee80a5122a1a901bde Client: add JavaScript bindings for Rectangle, #459 diff -r 3b9ea4072263 -r a8aabea64f17 libclient/CMakeLists.txt --- a/libclient/CMakeLists.txt Sun Apr 03 11:42:03 2016 +0200 +++ b/libclient/CMakeLists.txt Sun Apr 03 12:20:12 2016 +0200 @@ -26,6 +26,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/malikania/image.h ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-color.h ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-point.h + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-rectangle.h ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-window.h ${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.h ${CMAKE_CURRENT_SOURCE_DIR}/malikania/line.h @@ -48,6 +49,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/malikania/image.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-color.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-point.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-rectangle.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-size.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-window.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.cpp diff -r 3b9ea4072263 -r a8aabea64f17 libclient/malikania/js-rectangle.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/js-rectangle.cpp Sun Apr 03 12:20:12 2016 +0200 @@ -0,0 +1,148 @@ +/* + * js-rectangle.cpp -- rectangle description (JavaScript binding) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "js-rectangle.h" + +namespace malikania { + +namespace { + +unsigned clamp(duk::ContextPtr ctx, int value, bool required) +{ + if (value < 0) { + if (required) { + duk::raise(ctx, DUK_ERR_RANGE_ERROR, "%d can not be negative", value); + } else { + value = 0; + } + } + + return static_cast(value); +} + +Rectangle parse(duk::ContextPtr ctx, duk::Index index, bool required, Rectangle rect = {}) +{ + duk::StackAssert sa(ctx); + + if (duk::type(ctx, index) == DUK_TYPE_OBJECT) { + if (required && !duk::hasProperty(ctx, index, "x")) { + duk::raise(ctx, DUK_ERR_ERROR, "missing x property in rectangle description"); + } else if (required && !duk::hasProperty(ctx, index, "y")) { + duk::raise(ctx, DUK_ERR_ERROR, "missing y property in rectangle description"); + } else if (required && !duk::hasProperty(ctx, index, "width")) { + duk::raise(ctx, DUK_ERR_ERROR, "missing width property in rectangle description"); + } else if (required && !duk::hasProperty(ctx, index, "height")) { + duk::raise(ctx, DUK_ERR_ERROR, "missing height property in rectangle description"); + } + + rect = Rectangle( + duk::getProperty(ctx, index, "x"), + duk::getProperty(ctx, index, "y"), + clamp(ctx, duk::getProperty(ctx, index, "width"), required), + clamp(ctx, duk::getProperty(ctx, index, "height"), required) + ); + } else if (required) { + duk::raise(ctx, DUK_ERR_TYPE_ERROR, "size object expected"); + } + + return rect; +} + +duk::Ret constructor(duk::ContextPtr ctx) +{ + Rectangle rect; + + if (duk::top(ctx) == 4) { + rect = Rectangle( + duk::require(ctx, 0), + duk::require(ctx, 1), + clamp(ctx, duk::require(ctx, 2), true), + clamp(ctx, duk::require(ctx, 3), true) + ); + } else if (duk::top(ctx) == 1) { + rect = parse(ctx, 0, true); + } + + duk::Ret ret; + + /* Allow both constructor and non constructor calls */ + if (duk_is_constructor_call(ctx)) { + duk::push(ctx, duk::This()); + /* TODO: use duk::put when available */ + duk::TypeTraits::put(ctx, rect); + duk::pop(ctx); + ret = 0; + } else { + duk::push(ctx, rect); + ret = 1; + } + + return ret; +} + +} // !namespace + +namespace duk { + +Rectangle TypeTraits::get(ContextPtr ctx, Index index) +{ + return parse(ctx, index, false); +} + +Rectangle TypeTraits::require(ContextPtr ctx, Index index) +{ + return parse(ctx, index, true); +} + +Rectangle TypeTraits::optional(ContextPtr ctx, Index index, Rectangle def) +{ + return parse(ctx, index, false, std::move(def)); +} + +void TypeTraits::push(ContextPtr ctx, const Rectangle &rect) +{ + duk::StackAssert sa(ctx, 1); + + duk::push(ctx, duk::Object()); + duk::TypeTraits::put(ctx, rect); +} + +void TypeTraits::put(ContextPtr ctx, const Rectangle &rect) +{ + assert(duk::type(ctx, -1) == DUK_TYPE_OBJECT); + + duk::StackAssert sa(ctx); + + duk::putProperty(ctx, -1, "x", rect.x()); + duk::putProperty(ctx, -1, "y", rect.y()); + duk::putProperty(ctx, -1, "width", static_cast(rect.width())); + duk::putProperty(ctx, -1, "height", static_cast(rect.height())); +} + +} // !duk + +void loadMalikaniaRectangle(duk::ContextPtr ctx) +{ + duk::StackAssert sa(ctx, 0); + + duk::getGlobal(ctx, "Malikania"); + duk::putProperty(ctx, -1, "Rectangle", duk::Function{constructor, DUK_VARARGS}); + duk::pop(ctx); +} + +} // !malikania diff -r 3b9ea4072263 -r a8aabea64f17 libclient/malikania/js-rectangle.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/js-rectangle.h Sun Apr 03 12:20:12 2016 +0200 @@ -0,0 +1,102 @@ +/* + * js-rectangle.h -- rectangle description (JavaScript binding) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MALIKANIA_JS_RECTANGLE_H +#define MALIKANIA_JS_RECTANGLE_H + +#include "js.h" +#include "rectangle.h" + +namespace malikania { + +namespace duk { + +/** + * @brief JavaScript binding for Rectangle. + * + * Rectangles are plain objects. + * + * ```` + * { + * x: 10, + * y: 20, + * width: 100, + * height: 200 + * } + * ```` + */ +template <> +class TypeTraits { +public: + /** + * Get a rectangle. + * + * The rectangle may be adjusted if any values are incorrect. + * + * @param ctx the context + * @param index the value index + * @return the rectangle + */ + static Rectangle get(ContextPtr ctx, Index index); + + /** + * Require a rectangle. + * + * If the object is not a rectangle or if width, height are invalid, raise a JavaScript error. + * + * @param ctx the context + * @param index the index + * @return the rectangle + */ + static Rectangle require(ContextPtr ctx, Index index); + + + /** + * Like get but return the default value if the value at the given index is not an object or invalid + * + * @param ctx the context + * @param index the idnex + * @param def the default value + * @return the rectangle + */ + static Rectangle optional(ContextPtr ctx, Index index, Rectangle def); + + /** + * Push the rectangle as object. + * + * @param ctx the context + * @param rect the rectangle + */ + static void push(ContextPtr ctx, const Rectangle &rect); + + /** + * Put the rectangle properties into the object at the top of the stack. + * + * @param ctx the context + * @param rect the rectangle + */ + static void put(ContextPtr ctx, const Rectangle &rect); +}; + +} // !duk + +void loadMalikaniaRectangle(duk::ContextPtr ctx); + +} // !malikania + +#endif // !MALIKANIA_JS_RECTANGLE_H diff -r 3b9ea4072263 -r a8aabea64f17 tests/libclient/CMakeLists.txt --- a/tests/libclient/CMakeLists.txt Sun Apr 03 11:42:03 2016 +0200 +++ b/tests/libclient/CMakeLists.txt Sun Apr 03 12:20:12 2016 +0200 @@ -29,5 +29,6 @@ # JavaScript bindings add_subdirectory(js-color) add_subdirectory(js-point) +add_subdirectory(js-rectangle) add_subdirectory(js-size) add_subdirectory(js-window) diff -r 3b9ea4072263 -r a8aabea64f17 tests/libclient/js-rectangle/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libclient/js-rectangle/CMakeLists.txt Sun Apr 03 12:20:12 2016 +0200 @@ -0,0 +1,23 @@ +# +# CMakeLists.txt -- CMake build system for malikania +# +# Copyright (c) 2013-2016 Malikania Authors +# +# Permission to use, copy, modify, and/or distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +# + +malikania_create_test( + NAME js-rectangle + LIBRARIES libclient + SOURCES main.cpp +) diff -r 3b9ea4072263 -r a8aabea64f17 tests/libclient/js-rectangle/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libclient/js-rectangle/main.cpp Sun Apr 03 12:20:12 2016 +0200 @@ -0,0 +1,292 @@ +/* + * main.cpp -- test Rectangle (JavaScript binding) + * + * Copyright (c) 2013-2016 Malikania Authors + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include + +using namespace malikania; + +class TestRectangle : public testing::Test { +protected: + duk::Context m_ctx; + +public: + TestRectangle() + { + duk::putGlobal(m_ctx, "Malikania", duk::Object()); + + loadMalikaniaRectangle(m_ctx); + } +}; + +/* + * Valid constructors + * ------------------------------------------------------------------ + */ + +TEST_F(TestRectangle, ConstructorNoArgs) +{ + try { + auto ret = duk::pevalString(m_ctx, + "r = Malikania.Rectangle();" + "x = r.x;" + "y = r.y;" + "w = r.width;" + "h = r.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(0, duk::getGlobal(m_ctx, "x")); + ASSERT_EQ(0, duk::getGlobal(m_ctx, "y")); + ASSERT_EQ(0, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(0, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestRectangle, Constructor4Args) +{ + try { + auto ret = duk::pevalString(m_ctx, + "r = Malikania.Rectangle(10, 20, 30, 40);" + "x = r.x;" + "y = r.y;" + "w = r.width;" + "h = r.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(10, duk::getGlobal(m_ctx, "x")); + ASSERT_EQ(20, duk::getGlobal(m_ctx, "y")); + ASSERT_EQ(30, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(40, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestRectangle, ConstructorObject) +{ + try { + auto ret = duk::pevalString(m_ctx, + "r = Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" + "x = r.x;" + "y = r.y;" + "w = r.width;" + "h = r.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(10, duk::getGlobal(m_ctx, "x")); + ASSERT_EQ(20, duk::getGlobal(m_ctx, "y")); + ASSERT_EQ(30, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(40, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestRectangle, ConstructorNew) +{ + try { + auto ret = duk::pevalString(m_ctx, + "r = new Malikania.Rectangle({ x: 10, y: 20, width: 30, height: 40 });" + "x = r.x;" + "y = r.y;" + "w = r.width;" + "h = r.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(10, duk::getGlobal(m_ctx, "x")); + ASSERT_EQ(20, duk::getGlobal(m_ctx, "y")); + ASSERT_EQ(30, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(40, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +/* + * Invalid constructors + * ------------------------------------------------------------------ + */ + +TEST_F(TestRectangle, InvalidConstructorArg1) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Rectangle(null);" + "} catch (e) {" + " name = e.name;" + " correct = (e instanceof TypeError);" + "}" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ("TypeError", duk::getGlobal(m_ctx, "name")); + ASSERT_TRUE(duk::getGlobal(m_ctx, "correct")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestRectangle, InvalidConstructorRange1) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Rectangle(0, 0, -10, -10);" + "} catch (e) {" + " name = e.name;" + " correct = (e instanceof RangeError);" + "}" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ("RangeError", duk::getGlobal(m_ctx, "name")); + ASSERT_TRUE(duk::getGlobal(m_ctx, "correct")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +/* + * Require. + * ------------------------------------------------------------------ + */ + +TEST_F(TestRectangle, requireSuccess) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + Rectangle rect = duk::require(ctx, 0); + + duk::putGlobal(ctx, "x", rect.x()); + duk::putGlobal(ctx, "y", rect.y()); + duk::putGlobal(ctx, "w", static_cast(rect.width())); + duk::putGlobal(ctx, "h", static_cast(rect.height())); + + return 0; + }, 1}); + + auto ret = duk::pevalString(m_ctx, "build({ x: 50, y: 80, width: 100, height: 200 });"); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(50, duk::getGlobal(m_ctx, "x")); + ASSERT_EQ(80, duk::getGlobal(m_ctx, "y")); + ASSERT_EQ(100, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(200, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestRectangle, requireFail) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + duk::require(ctx, 0); + + return 0; + }, 1}); + + auto ret = duk::pevalString(m_ctx, + "try {" + " build({});" + "} catch (e) {" + " name = e.name;" + " correct = (e instanceof Error);" + "}" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ("Error", duk::getGlobal(m_ctx, "name")); + ASSERT_TRUE(duk::getGlobal(m_ctx, "correct")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +/* + * Get. + * ------------------------------------------------------------------ + */ + +TEST_F(TestRectangle, getAdjustAll) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + Rectangle rect = duk::get(ctx, 0); + + duk::putGlobal(ctx, "x", rect.x()); + duk::putGlobal(ctx, "y", rect.y()); + duk::putGlobal(ctx, "w", static_cast(rect.width())); + duk::putGlobal(ctx, "h", static_cast(rect.height())); + + return 0; + }, 1}); + + auto ret = duk::pevalString(m_ctx, "build({});"); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(0, duk::getGlobal(m_ctx, "x")); + ASSERT_EQ(0, duk::getGlobal(m_ctx, "y")); + ASSERT_EQ(0, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(0, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}