From d5faaeca1ac9fa9be03bddc615c531b49f8e2b68 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 1 Mar 2018 11:20:22 -0700 Subject: [PATCH 01/14] Add failing unicode test from #415 --- unittests/string_unicode_unicode.chai | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unittests/string_unicode_unicode.chai b/unittests/string_unicode_unicode.chai index 2a237ed8..5ce08bb7 100644 --- a/unittests/string_unicode_unicode.chai +++ b/unittests/string_unicode_unicode.chai @@ -3,3 +3,6 @@ assert_equal("U for \uc39cmlauts", "U for Ümlauts") assert_equal("More \uc39cml\uc3a4\uc3bcts", "More Ümläüts") assert_equal("Thorn \uc3be sign", "Thorn þ sign") +assert_equal("Test\u20Me", "Test Me") +assert_equal("Test\u2022Me", "Test•Me") +assert_equal("Test\U1F534Me", "Test🔴Me") From 1acfb4f7b85f92be2e678595925fe197ec7868c0 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 1 Mar 2018 11:22:20 -0700 Subject: [PATCH 02/14] Apply patch from @chris0e3 --- .../chaiscript/language/chaiscript_parser.hpp | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 163d2bb3..3c0be095 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1036,7 +1036,7 @@ namespace chaiscript bool saw_interpolation_marker = false; bool is_octal = false; bool is_hex = false; - bool is_unicode = false; + char is_unicode = 0; const bool interpolation_allowed; string_type octal_matches; @@ -1086,11 +1086,34 @@ namespace chaiscript void process_unicode() { - auto val = stoll(hex_matches, nullptr, 16); + unsigned ch = std::stoi(hex_matches, nullptr, 16); hex_matches.clear(); - match += detail::Char_Parser_Helper::str_from_ll(val); + char buf[4]; + if (ch < 0x80) + match += static_cast(ch); + else if (ch < 0x800) + { + buf[0] = 0xC0 | (ch >> 6); + buf[1] = 0x80 | (ch & 0x3F); + match.append(buf, 2); + } + else if (ch < 0x10000) + { + buf[0] = 0xE0 | (ch >> 12); + buf[1] = 0x80 | ((ch >> 6) & 0x3F); + buf[2] = 0x80 | (ch & 0x3F); + match.append(buf, 3); + } + else //if (ch < 0x200000) + { + buf[0] = 0xF0 | (ch >> 18); + buf[1] = 0x80 | ((ch >> 12) & 0x3F); + buf[2] = 0x80 | ((ch >> 6) & 0x3F); + buf[3] = 0x80 | (ch & 0x3F); + match.append(buf, 4); + } is_escaped = false; - is_unicode = false; + is_unicode = 0; } void parse(const char_type t_char, const int line, const int col, const std::string &filename) { @@ -1130,7 +1153,7 @@ namespace chaiscript if (is_hex_char) { hex_matches.push_back(t_char); - if(hex_matches.size() == 4) { + if(hex_matches.size() == is_unicode) { // Format is specified to be 'slash'uABCD // on collecting from A to D do parsing process_unicode(); @@ -1158,7 +1181,9 @@ namespace chaiscript } else if (t_char == 'x') { is_hex = true; } else if (t_char == 'u') { - is_unicode = true; + is_unicode = 4; + } else if (t_char == 'U') { + is_unicode = 6; } else { switch (t_char) { case ('\'') : match.push_back('\''); break; From 1311c97c49130b7520fcf35f61bf243adc1a95a3 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 1 Mar 2018 12:57:56 -0700 Subject: [PATCH 03/14] Fix unit tests for unicode to be consistent with how it should work Addresses #415 --- unittests/string_unicode_parse.chai | 11 ++--------- unittests/string_unicode_unicode.chai | 7 +++---- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/unittests/string_unicode_parse.chai b/unittests/string_unicode_parse.chai index 50da68bb..ae2f247e 100644 --- a/unittests/string_unicode_parse.chai +++ b/unittests/string_unicode_parse.chai @@ -1,11 +1,4 @@ -assert_equal('\u00aa', '\u00AA') -assert_equal('\u00bb', '\uBB') -assert_equal('\ucc', '\u00CC') -assert_equal('\udd', '\uDD') +assert_equal("\u00aa", "\u00AA") +assert_equal("\u00bb", "\xC2\xBB") -assert_equal('\u0ee', '\uEE') -assert_equal('\ue', '\u000E') - -assert_equal("\u30\u31\u32", "012") -assert_equal("\u33Test", "3Test") assert_equal("Test\u0040", "Test@") diff --git a/unittests/string_unicode_unicode.chai b/unittests/string_unicode_unicode.chai index 5ce08bb7..15da24e5 100644 --- a/unittests/string_unicode_unicode.chai +++ b/unittests/string_unicode_unicode.chai @@ -1,8 +1,7 @@ -assert_equal("\uc39c", "Ü") -assert_equal("U for \uc39cmlauts", "U for Ümlauts") -assert_equal("More \uc39cml\uc3a4\uc3bcts", "More Ümläüts") +assert_equal("\uc39c", "쎜") +assert_equal("U for \u00dcmlauts", "U for Ümlauts") -assert_equal("Thorn \uc3be sign", "Thorn þ sign") +assert_equal("Thorn \u00fe sign", "Thorn þ sign") assert_equal("Test\u20Me", "Test Me") assert_equal("Test\u2022Me", "Test•Me") assert_equal("Test\U1F534Me", "Test🔴Me") From 81ebe1a7be92aeb32edde5fb1f0712156f2e18ea Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 1 Mar 2018 13:40:49 -0700 Subject: [PATCH 04/14] Fix the compiler warnings related to unicode parsing Re #415 --- .../chaiscript/language/chaiscript_parser.hpp | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 3c0be095..2a44c922 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1036,7 +1036,7 @@ namespace chaiscript bool saw_interpolation_marker = false; bool is_octal = false; bool is_hex = false; - char is_unicode = 0; + std::size_t unicode_size = 0; const bool interpolation_allowed; string_type octal_matches; @@ -1059,7 +1059,7 @@ namespace chaiscript process_hex(); } - if (is_unicode) { + if (unicode_size > 0) { process_unicode(); } } @@ -1086,34 +1086,32 @@ namespace chaiscript void process_unicode() { - unsigned ch = std::stoi(hex_matches, nullptr, 16); + const auto ch = static_cast(std::stoi(hex_matches, nullptr, 16)); hex_matches.clear(); char buf[4]; - if (ch < 0x80) + if (ch < 0x80) { match += static_cast(ch); - else if (ch < 0x800) - { - buf[0] = 0xC0 | (ch >> 6); - buf[1] = 0x80 | (ch & 0x3F); + } else if (ch < 0x800) { + buf[0] = static_cast(0xC0 | (ch >> 6)); + buf[1] = static_cast(0x80 | (ch & 0x3F)); match.append(buf, 2); - } - else if (ch < 0x10000) - { - buf[0] = 0xE0 | (ch >> 12); - buf[1] = 0x80 | ((ch >> 6) & 0x3F); - buf[2] = 0x80 | (ch & 0x3F); + } else if (ch < 0x10000) { + buf[0] = static_cast(0xE0 | (ch >> 12)); + buf[1] = static_cast(0x80 | ((ch >> 6) & 0x3F)); + buf[2] = static_cast(0x80 | (ch & 0x3F)); match.append(buf, 3); - } - else //if (ch < 0x200000) - { - buf[0] = 0xF0 | (ch >> 18); - buf[1] = 0x80 | ((ch >> 12) & 0x3F); - buf[2] = 0x80 | ((ch >> 6) & 0x3F); - buf[3] = 0x80 | (ch & 0x3F); + } else if (ch < 0x200000) { + buf[0] = static_cast(0xF0 | (ch >> 18)); + buf[1] = static_cast(0x80 | ((ch >> 12) & 0x3F)); + buf[2] = static_cast(0x80 | ((ch >> 6) & 0x3F)); + buf[3] = static_cast(0x80 | (ch & 0x3F)); match.append(buf, 4); + } else { + // this must be an invalid escape sequence? + throw exception::eval_error("Unknown 32 bit unicode literal sequence"); } is_escaped = false; - is_unicode = 0; + unicode_size = 0; } void parse(const char_type t_char, const int line, const int col, const std::string &filename) { @@ -1149,16 +1147,17 @@ namespace chaiscript } else { process_hex(); } - } else if (is_unicode) { + } else if (unicode_size > 0) { if (is_hex_char) { hex_matches.push_back(t_char); - if(hex_matches.size() == is_unicode) { - // Format is specified to be 'slash'uABCD - // on collecting from A to D do parsing - process_unicode(); - } - return; + if(hex_matches.size() == unicode_size) { + // Format is specified to be 'slash'uABCD + // on collecting from A to D do parsing + process_unicode(); + } + return; + } else { // Not a unicode anymore, try parsing any way // May be someone used 'slash'uAA only @@ -1181,9 +1180,9 @@ namespace chaiscript } else if (t_char == 'x') { is_hex = true; } else if (t_char == 'u') { - is_unicode = 4; + unicode_size = 4; } else if (t_char == 'U') { - is_unicode = 6; + unicode_size = 6; } else { switch (t_char) { case ('\'') : match.push_back('\''); break; From 1b9027a24fcf5fee44214b1e876702a993cded5e Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Thu, 1 Mar 2018 17:03:50 -0700 Subject: [PATCH 05/14] Fix handling of 32 bit unicode character escapes --- .../chaiscript/language/chaiscript_parser.hpp | 17 ++++++++++++----- unittests/string_unicode_unicode.chai | 11 ++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 2a44c922..2c957ca8 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1088,7 +1088,17 @@ namespace chaiscript { const auto ch = static_cast(std::stoi(hex_matches, nullptr, 16)); hex_matches.clear(); + is_escaped = false; + const auto u_size = unicode_size; + unicode_size = 0; + char buf[4]; + if (u_size == 4 && ch >= 0xD800 && ch <= 0xDFFF) { + throw exception::eval_error("Invalid 16 bit universal character"); + } + + unicode_size = 0; + if (ch < 0x80) { match += static_cast(ch); } else if (ch < 0x800) { @@ -1108,10 +1118,8 @@ namespace chaiscript match.append(buf, 4); } else { // this must be an invalid escape sequence? - throw exception::eval_error("Unknown 32 bit unicode literal sequence"); + throw exception::eval_error("Invalid 32 bit universal character"); } - is_escaped = false; - unicode_size = 0; } void parse(const char_type t_char, const int line, const int col, const std::string &filename) { @@ -1157,7 +1165,6 @@ namespace chaiscript process_unicode(); } return; - } else { // Not a unicode anymore, try parsing any way // May be someone used 'slash'uAA only @@ -1182,7 +1189,7 @@ namespace chaiscript } else if (t_char == 'u') { unicode_size = 4; } else if (t_char == 'U') { - unicode_size = 6; + unicode_size = 8; } else { switch (t_char) { case ('\'') : match.push_back('\''); break; diff --git a/unittests/string_unicode_unicode.chai b/unittests/string_unicode_unicode.chai index 15da24e5..267fbb64 100644 --- a/unittests/string_unicode_unicode.chai +++ b/unittests/string_unicode_unicode.chai @@ -4,4 +4,13 @@ assert_equal("U for \u00dcmlauts", "U for Ümlauts") assert_equal("Thorn \u00fe sign", "Thorn þ sign") assert_equal("Test\u20Me", "Test Me") assert_equal("Test\u2022Me", "Test•Me") -assert_equal("Test\U1F534Me", "Test🔴Me") +//assert_equal("Test\uDD34\uD83DMe", "Test🔴Me") + +assert_equal("\xF0\x9F\x8D\x8C", "🍌") +assert_equal("\U0001F34C", "🍌") + +assert_throws("Invalid 16 bit universal character", fun(){ parse("\"\\uD83C\""); }); + +assert_equal("\U24B62", "𤭢") + +assert_equal("Test\U0001F534Me", "Test🔴Me") From 1a9165f7fce2fe325a0381ef056eb00f8d3014f1 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 2 Mar 2018 07:45:24 -0700 Subject: [PATCH 06/14] Normalize on C++'s standards for \u and \U --- include/chaiscript/language/chaiscript_parser.hpp | 14 +++++++++++++- unittests/string_unicode_unicode.chai | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 2c957ca8..1cac8d95 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1064,6 +1064,13 @@ namespace chaiscript } } + void finalize_unicode() + { + if (unicode_size > 0) { + process_unicode(); + } + } + void process_hex() { auto val = stoll(hex_matches, nullptr, 16); @@ -1087,17 +1094,20 @@ namespace chaiscript void process_unicode() { const auto ch = static_cast(std::stoi(hex_matches, nullptr, 16)); + const auto match_size = hex_matches.size(); hex_matches.clear(); is_escaped = false; const auto u_size = unicode_size; unicode_size = 0; char buf[4]; + if (u_size != match_size) { + throw exception::eval_error("Incomplete unicode escape sequence"); + } if (u_size == 4 && ch >= 0xD800 && ch <= 0xDFFF) { throw exception::eval_error("Invalid 16 bit universal character"); } - unicode_size = 0; if (ch < 0x80) { match += static_cast(ch); @@ -1289,6 +1299,7 @@ namespace chaiscript } } + cparser.finalize_unicode(); return cparser.is_interpolated; }(); @@ -1347,6 +1358,7 @@ namespace chaiscript for (auto s = start + 1, end = m_position - 1; s != end; ++s) { cparser.parse(*s, start.line, start.col, *m_filename); } + cparser.finalize_unicode(); } if (match.size() != 1) { diff --git a/unittests/string_unicode_unicode.chai b/unittests/string_unicode_unicode.chai index 267fbb64..d79dcaf3 100644 --- a/unittests/string_unicode_unicode.chai +++ b/unittests/string_unicode_unicode.chai @@ -2,15 +2,15 @@ assert_equal("\uc39c", "쎜") assert_equal("U for \u00dcmlauts", "U for Ümlauts") assert_equal("Thorn \u00fe sign", "Thorn þ sign") -assert_equal("Test\u20Me", "Test Me") +assert_equal("Test\u0020Me", "Test Me") assert_equal("Test\u2022Me", "Test•Me") -//assert_equal("Test\uDD34\uD83DMe", "Test🔴Me") assert_equal("\xF0\x9F\x8D\x8C", "🍌") assert_equal("\U0001F34C", "🍌") assert_throws("Invalid 16 bit universal character", fun(){ parse("\"\\uD83C\""); }); +assert_throws("Incomplete unicode escape sequence", fun(){ parse("\"\\uD83\""); }); -assert_equal("\U24B62", "𤭢") +assert_equal("\U00024B62", "𤭢") assert_equal("Test\U0001F534Me", "Test🔴Me") From 12797c0a0385de0fe46f879e6ffb7b3130b11356 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 2 Mar 2018 08:01:03 -0700 Subject: [PATCH 07/14] Add C++ tests to verify strings match Re #415 --- unittests/compiled_tests.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index c1716ec9..e7d1685e 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -1269,4 +1269,14 @@ TEST_CASE("Test reference member being registered") CHECK(d == Approx(2.3)); } +TEST_CASE("Test unicode matches C++") +{ + chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); + CHECK(u8"\U000000AC" == chai.eval(R"("\U000000AC")")); + CHECK(u8"\xF0\x9F\x8D\x8C" == chai.eval(R"("\xF0\x9F\x8D\x8C")")); + CHECK(u8"\U0001F34C" == chai.eval(R"("\U0001F34C")")); + CHECK(u8"\u2022" == chai.eval(R"("\u2022")")); + +} + From 2e9512c6b8b5581c317ac26e50e95158cf321885 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 2 Mar 2018 08:54:27 -0700 Subject: [PATCH 08/14] Adjust unicode tests for MSVC --- unittests/compiled_tests.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index e7d1685e..660753d7 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -4,7 +4,7 @@ #ifdef _MSC_VER #pragma warning(push) -#pragma warning(disable : 4062 4242 4640 4702 6330 28251) +#pragma warning(disable : 4062 4242 4566 4640 4702 6330 28251) #endif @@ -1273,8 +1273,8 @@ TEST_CASE("Test unicode matches C++") { chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); CHECK(u8"\U000000AC" == chai.eval(R"("\U000000AC")")); - CHECK(u8"\xF0\x9F\x8D\x8C" == chai.eval(R"("\xF0\x9F\x8D\x8C")")); - CHECK(u8"\U0001F34C" == chai.eval(R"("\U0001F34C")")); + CHECK("\xF0\x9F\x8D\x8C" == chai.eval(R"("\xF0\x9F\x8D\x8C")")); + CHECK("\U0001F34C" == chai.eval(R"("\U0001F34C")")); CHECK(u8"\u2022" == chai.eval(R"("\u2022")")); } From 476a752a08b640a02cab8b08bef620a85fc52279 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 2 Mar 2018 15:09:52 -0700 Subject: [PATCH 09/14] Fix last merge --- .../chaiscript/language/chaiscript_parser.hpp | 17 ++++++----------- unittests/string_unicode_unicode.chai | 2 +- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index 101d0883..649f6f21 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -1062,15 +1062,12 @@ namespace chaiscript process_hex(); } - if (unicode_size > 0) { - process_unicode(); - } - } - - void finalize_unicode() - { - if (unicode_size > 0) { - process_unicode(); + if (unicode_size > 0) { + process_unicode(); + } + } catch (const std::invalid_argument &) { + } catch (const exception::eval_error &) { + // Something happened with parsing, we'll catch it later? } } @@ -1307,7 +1304,6 @@ namespace chaiscript } } - cparser.finalize_unicode(); return cparser.is_interpolated; }(); @@ -1366,7 +1362,6 @@ namespace chaiscript for (auto s = start + 1, end = m_position - 1; s != end; ++s) { cparser.parse(*s, start.line, start.col, *m_filename); } - cparser.finalize_unicode(); } if (match.size() != 1) { diff --git a/unittests/string_unicode_unicode.chai b/unittests/string_unicode_unicode.chai index d79dcaf3..93364f05 100644 --- a/unittests/string_unicode_unicode.chai +++ b/unittests/string_unicode_unicode.chai @@ -9,7 +9,7 @@ assert_equal("\xF0\x9F\x8D\x8C", "🍌") assert_equal("\U0001F34C", "🍌") assert_throws("Invalid 16 bit universal character", fun(){ parse("\"\\uD83C\""); }); -assert_throws("Incomplete unicode escape sequence", fun(){ parse("\"\\uD83\""); }); +assert_throws("Incomplete unicode escape sequence", fun(){ parse("\"\\uD83 \""); }); assert_equal("\U00024B62", "𤭢") From 258cb23dda73dc3cf1a0cc0dd42d434418be63c1 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 2 Mar 2018 15:40:22 -0700 Subject: [PATCH 10/14] Further fixes for compiled tests for VS 2015 --- unittests/compiled_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unittests/compiled_tests.cpp b/unittests/compiled_tests.cpp index c7d37ffc..3df1dbe2 100644 --- a/unittests/compiled_tests.cpp +++ b/unittests/compiled_tests.cpp @@ -1276,7 +1276,7 @@ TEST_CASE("Test unicode matches C++") chaiscript::ChaiScript_Basic chai(create_chaiscript_stdlib(),create_chaiscript_parser()); CHECK(u8"\U000000AC" == chai.eval(R"("\U000000AC")")); CHECK("\xF0\x9F\x8D\x8C" == chai.eval(R"("\xF0\x9F\x8D\x8C")")); - CHECK("\U0001F34C" == chai.eval(R"("\U0001F34C")")); + CHECK(u8"\U0001F34C" == chai.eval(R"("\U0001F34C")")); CHECK(u8"\u2022" == chai.eval(R"("\u2022")")); } From f695a24e1b2ec4ec162b148e7717ad69fca7589d Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 8 May 2018 09:46:01 -0600 Subject: [PATCH 11/14] Fix clang warning for undefined msvc symbol check --- include/chaiscript/dispatchkit/register_function.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/chaiscript/dispatchkit/register_function.hpp b/include/chaiscript/dispatchkit/register_function.hpp index e5dec699..2e56fe0e 100644 --- a/include/chaiscript/dispatchkit/register_function.hpp +++ b/include/chaiscript/dispatchkit/register_function.hpp @@ -86,7 +86,7 @@ namespace chaiscript // only compile this bit if noexcept is part of the type system // -#if __cpp_noexcept_function_type >= 201510 || (defined(_NOEXCEPT_TYPES_SUPPORTED) && _MSC_VER >= 1912) +#if (defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510) || (defined(_NOEXCEPT_TYPES_SUPPORTED) && _MSC_VER >= 1912) template Proxy_Function fun(Ret (*func)(Param...) noexcept) { From c14d9dfb6e862e98f4e722790fb6e399aaa7f7f2 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 8 May 2018 10:05:10 -0600 Subject: [PATCH 12/14] Fix some new clang related warnings --- include/chaiscript/dispatchkit/bootstrap.hpp | 8 ++++---- src/test_module.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index bcaebfca..1cddf81e 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -23,10 +23,10 @@ namespace chaiscript void array(const std::string &type, Module& m) { typedef typename std::remove_extent::type ReturnType; - const auto extent = std::extent::value; + constexpr auto extent = std::extent::value; m.add(user_type(), type); m.add(fun( - [extent](T& t, size_t index)->ReturnType &{ + [](T& t, size_t index)->ReturnType &{ if (extent > 0 && index >= extent) { throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); } else { @@ -37,7 +37,7 @@ namespace chaiscript ); m.add(fun( - [extent](const T &t, size_t index)->const ReturnType &{ + [](const T &t, size_t index)->const ReturnType &{ if (extent > 0 && index >= extent) { throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); } else { @@ -48,7 +48,7 @@ namespace chaiscript ); m.add(fun( - [extent](const T &) { + [](const T &) { return extent; }), "size"); } diff --git a/src/test_module.cpp b/src/test_module.cpp index fcb255e5..90a1154a 100644 --- a/src/test_module.cpp +++ b/src/test_module.cpp @@ -78,7 +78,7 @@ int to_int(TestEnum t) class TestDerivedType : public TestBaseType { public: - virtual ~TestDerivedType() {} + ~TestDerivedType() override {} TestDerivedType(const TestDerivedType &) = default; TestDerivedType() = default; virtual int func() override { return 1; } From 5c2fd20a9e86f995952483fbdd6bac6d01152a24 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Wed, 9 May 2018 19:18:34 -0400 Subject: [PATCH 13/14] docs: Add Switch Statement example --- cheatsheet.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cheatsheet.md b/cheatsheet.md index 266ddc71..907fe329 100644 --- a/cheatsheet.md +++ b/cheatsheet.md @@ -370,6 +370,25 @@ if (expression) { } if (statement; expression) { } ``` +## Switch Statements + +``` chaiscript +var myvalue = 2 +switch (myvalue) { + case (1) { + print("My Value is 1"); + break; + } + case (2) { + print("My Value is 2"); + break; + } + default { + print("My Value is something else."; + } +} +``` + ## Built in Types ``` From 0520bb178c2f26d92ccaba9c0cd554c1260be6f4 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 20 May 2018 20:36:33 -0600 Subject: [PATCH 14/14] Fix capture error --- include/chaiscript/dispatchkit/bootstrap.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 1cddf81e..671a723a 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -23,10 +23,10 @@ namespace chaiscript void array(const std::string &type, Module& m) { typedef typename std::remove_extent::type ReturnType; - constexpr auto extent = std::extent::value; m.add(user_type(), type); m.add(fun( [](T& t, size_t index)->ReturnType &{ + constexpr auto extent = std::extent::value; if (extent > 0 && index >= extent) { throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); } else { @@ -38,6 +38,7 @@ namespace chaiscript m.add(fun( [](const T &t, size_t index)->const ReturnType &{ + constexpr auto extent = std::extent::value; if (extent > 0 && index >= extent) { throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); } else { @@ -49,6 +50,7 @@ namespace chaiscript m.add(fun( [](const T &) { + constexpr auto extent = std::extent::value; return extent; }), "size"); }