# HG changeset patch # User David Demelier # Date 1459605944 -7200 # Node ID fb7c2c0961019851eba747278c77f66171535246 # Parent 0c62e0af6af7640111c647075c501e97b495b2c1 Client: add JavaScript bindings for Size, #460 diff -r 0c62e0af6af7 -r fb7c2c096101 libclient/CMakeLists.txt --- a/libclient/CMakeLists.txt Fri Apr 01 13:45:45 2016 +0200 +++ b/libclient/CMakeLists.txt Sat Apr 02 16:05:44 2016 +0200 @@ -45,6 +45,7 @@ ${CMAKE_CURRENT_SOURCE_DIR}/malikania/font.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/image.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-color.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/malikania/js-size.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/label.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/sprite.cpp ${CMAKE_CURRENT_SOURCE_DIR}/malikania/window.cpp diff -r 0c62e0af6af7 -r fb7c2c096101 libclient/malikania/js-size.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/js-size.cpp Sat Apr 02 16:05:44 2016 +0200 @@ -0,0 +1,138 @@ +/* + * js-size.cpp -- size 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-size.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); +} + +Size parse(duk::ContextPtr ctx, duk::Index index, bool required, Size size = {}) +{ + duk::StackAssert sa(ctx); + + if (duk::type(ctx, index) == DUK_TYPE_OBJECT) { + if (required && !duk::hasProperty(ctx, index, "width")) { + duk::raise(ctx, DUK_ERR_ERROR, "missing width property in size description"); + } else if (required && !duk::hasProperty(ctx, index, "height")) { + duk::raise(ctx, DUK_ERR_ERROR, "missing height property in size description"); + } + + size = Size( + 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 size; +} + +duk::Ret constructor(duk::ContextPtr ctx) +{ + malikania::Size size; + + if (duk::top(ctx) == 2) { + size = malikania::Size( + clamp(ctx, duk::require(ctx, 0), true), + clamp(ctx, duk::require(ctx, 1), true) + ); + } else if (duk::top(ctx) == 1) { + size = 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, size); + duk::pop(ctx); + ret = 0; + } else { + duk::push(ctx, size); + ret = 1; + } + + return ret; +} + +} // !namespace + +namespace duk { + +malikania::Size TypeTraits::get(ContextPtr ctx, Index index) +{ + return parse(ctx, index, false); +} + +malikania::Size TypeTraits::require(ContextPtr ctx, Index index) +{ + return parse(ctx, index, true); +} + +malikania::Size TypeTraits::optional(ContextPtr ctx, Index index, malikania::Size def) +{ + return parse(ctx, index, false, std::move(def)); +} + +void TypeTraits::push(ContextPtr ctx, const malikania::Size &size) +{ + StackAssert sa(ctx, 1); + + duk::push(ctx, duk::Object()); + duk::TypeTraits::put(ctx, size); +} + +void TypeTraits::put(ContextPtr ctx, const malikania::Size &size) +{ + assert(type(ctx, -1) == DUK_TYPE_OBJECT); + + StackAssert sa(ctx, 0); + + duk::putProperty(ctx, -1, "width", static_cast(size.width())); + duk::putProperty(ctx, -1, "height", static_cast(size.height())); +} + +} // !duk + +void loadMalikaniaSize(duk::ContextPtr ctx) +{ + duk::StackAssert sa(ctx, 0); + + duk::getGlobal(ctx, "Malikania"); + duk::putProperty(ctx, -1, "Size", duk::Function{constructor, DUK_VARARGS}); + duk::pop(ctx); +} + +} // !malikania diff -r 0c62e0af6af7 -r fb7c2c096101 libclient/malikania/js-size.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libclient/malikania/js-size.h Sat Apr 02 16:05:44 2016 +0200 @@ -0,0 +1,99 @@ +/* + * js-size.h -- size 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_SIZE_H +#define MALIKANIA_JS_SIZE_H + +#include "js.h" +#include "size.h" + +namespace malikania { + +namespace duk { + +/** + * @brief JavaScript binding for Size. + * + * Size are plain objects. + * + * ```` + * { + * width: 1000, + * height: 2000 + * } + * ```` + */ +template <> +class TypeTraits { +public: + /** + * Get a size. + * + * The size may be adjusted if any values are incorrect. + * + * @param ctx the context + * @param index the value index + * @return the size + */ + static malikania::Size get(ContextPtr ctx, Index index); + + /** + * Require a size + * + * If the object is not a size, raise a JavaScript error. + * + * @param ctx the context + * @param index the index + * @return the size + */ + static malikania::Size require(ContextPtr ctx, Index index); + + /** + * Like get but return the default value if the value at the given index is not an object. + * + * @param ctx the context + * @param index the idnex + * @param def the default value + * @return the size + */ + static malikania::Size optional(ContextPtr ctx, Index index, malikania::Size def); + + /** + * Push the size as object. + * + * @param ctx the context + * @param size the size + */ + static void push(ContextPtr ctx, const malikania::Size &size); + + /** + * Put the size properties into the object at the top of the stack. + * + * @param ctx the context + * @param size the size + */ + static void put(ContextPtr ctx, const malikania::Size &size); +}; + +} // !duk + +void loadMalikaniaSize(duk::ContextPtr ctx); + +} // !malikania + +#endif // !MALIKANIA_JS_SIZE_H diff -r 0c62e0af6af7 -r fb7c2c096101 tests/libclient/CMakeLists.txt --- a/tests/libclient/CMakeLists.txt Fri Apr 01 13:45:45 2016 +0200 +++ b/tests/libclient/CMakeLists.txt Sat Apr 02 16:05:44 2016 +0200 @@ -27,4 +27,5 @@ add_subdirectory(sprite) # JavaScript bindings -add_subdirectory(js-color) \ No newline at end of file +add_subdirectory(js-color) +add_subdirectory(js-size) diff -r 0c62e0af6af7 -r fb7c2c096101 tests/libclient/js-size/CMakeLists.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libclient/js-size/CMakeLists.txt Sat Apr 02 16:05:44 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-size + LIBRARIES libclient + SOURCES main.cpp +) diff -r 0c62e0af6af7 -r fb7c2c096101 tests/libclient/js-size/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libclient/js-size/main.cpp Sat Apr 02 16:05:44 2016 +0200 @@ -0,0 +1,337 @@ +/* + * main.cpp -- test Size (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 TestSize : public testing::Test { +protected: + duk::Context m_ctx; + +public: + TestSize() + { + duk::putGlobal(m_ctx, "Malikania", duk::Object()); + + loadMalikaniaSize(m_ctx); + } +}; + +/* + * Valid constructors + * ------------------------------------------------------------------ + */ + +TEST_F(TestSize, ConstructorNoArgs) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = Malikania.Size();" + "w = s.width;" + "h = s.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + 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(TestSize, Constructor2Args) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = Malikania.Size(100, 200);" + "w = s.width;" + "h = s.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + 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(TestSize, ConstructorObject) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = Malikania.Size({ width: 100, height: 200 });" + "w = s.width;" + "h = s.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + 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(TestSize, ConstructorNew) +{ + try { + auto ret = duk::pevalString(m_ctx, + "s = new Malikania.Size({ width: 100, height: 200 });" + "w = s.width;" + "h = s.height;" + ); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + ASSERT_EQ(100, duk::getGlobal(m_ctx, "w")); + ASSERT_EQ(200, duk::getGlobal(m_ctx, "h")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +/* + * Invalid constructors + * ------------------------------------------------------------------ + */ + +TEST_F(TestSize, InvalidConstructorArg1) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Size(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(TestSize, InvalidConstructorRange1) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Size(-1, 200);" + "} 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(); + } +} + +TEST_F(TestSize, InvalidConstructorRange2) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Size(100, -1);" + "} 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(); + } +} + +TEST_F(TestSize, InvalidConstructorRange3) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Size({ width: -1, height: 200 });" + "} 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(); + } +} + +TEST_F(TestSize, InvalidConstructorRange4) +{ + try { + auto ret = duk::pevalString(m_ctx, + "try {" + " Malikania.Size({ width: 100, height: -1 });" + "} 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(TestSize, requireSuccess) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + Size size = duk::require(ctx, 0); + + duk::putGlobal(ctx, "w", static_cast(size.width())); + duk::putGlobal(ctx, "h", static_cast(size.height())); + + return 0; + }, 1}); + + auto ret = duk::pevalString(m_ctx, "build({ width: 100, height: 200 });"); + + if (ret != 0) { + throw duk::error(m_ctx, -1); + } + + 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(TestSize, 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(TestSize, getAdjustAll) +{ + try { + duk::putGlobal(m_ctx, "build", duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + Size size = duk::get(ctx, 0); + + duk::putGlobal(ctx, "w", static_cast(size.width())); + duk::putGlobal(ctx, "h", static_cast(size.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, "r")); + ASSERT_EQ(0, duk::getGlobal(m_ctx, "g")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}