From 47354add573d8f6ced7b70f084dbda7c2d923fea Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Sat, 17 Mar 2018 02:19:40 +0100 Subject: [PATCH] Move map functions to own compile unit --- Makefile.conf | 1 + src/context.cpp | 1 + src/fn_maps.cpp | 94 +++++++++++++++++++++++++++++++++++++ src/fn_maps.hpp | 30 ++++++++++++ src/functions.cpp | 84 +-------------------------------- src/functions.hpp | 12 ----- win/libsass.targets | 2 + win/libsass.vcxproj.filters | 6 +++ 8 files changed, 135 insertions(+), 95 deletions(-) create mode 100644 src/fn_maps.cpp create mode 100644 src/fn_maps.hpp diff --git a/Makefile.conf b/Makefile.conf index 9b4bfc84c..fa74da819 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -11,6 +11,7 @@ SOURCES = \ context.cpp \ constants.cpp \ fn_utils.cpp \ + fn_maps.cpp \ fn_lists.cpp \ fn_colors.cpp \ fn_numbers.cpp \ diff --git a/src/context.cpp b/src/context.cpp index 67354254a..368863270 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -30,6 +30,7 @@ #include "prelexer.hpp" #include "emitter.hpp" #include "fn_utils.hpp" +#include "fn_maps.hpp" #include "fn_lists.hpp" #include "fn_colors.hpp" #include "fn_numbers.hpp" diff --git a/src/fn_maps.cpp b/src/fn_maps.cpp new file mode 100644 index 000000000..54a659433 --- /dev/null +++ b/src/fn_maps.cpp @@ -0,0 +1,94 @@ +#include "operators.hpp" +#include "fn_utils.hpp" +#include "fn_maps.hpp" + +namespace Sass { + + namespace Functions { + + ///////////////// + // MAP FUNCTIONS + ///////////////// + + Signature map_get_sig = "map-get($map, $key)"; + BUILT_IN(map_get) + { + // leaks for "map-get((), foo)" if not Obj + // investigate why this is (unexpected) + Map_Obj m = ARGM("$map", Map); + Expression_Obj v = ARG("$key", Expression); + try { + Value_Obj val = m->at(v); + if (!val) return SASS_MEMORY_NEW(Null, pstate); + val->set_delayed(false); + return val.detach(); + } catch (const std::out_of_range&) { + return SASS_MEMORY_NEW(Null, pstate); + } + catch (...) { throw; } + } + + Signature map_has_key_sig = "map-has-key($map, $key)"; + BUILT_IN(map_has_key) + { + Map_Obj m = ARGM("$map", Map); + Expression_Obj v = ARG("$key", Expression); + return SASS_MEMORY_NEW(Boolean, pstate, m->has(v)); + } + + Signature map_keys_sig = "map-keys($map)"; + BUILT_IN(map_keys) + { + Map_Obj m = ARGM("$map", Map); + List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA); + for ( auto key : m->keys()) { + result->append(key); + } + return result; + } + + Signature map_values_sig = "map-values($map)"; + BUILT_IN(map_values) + { + Map_Obj m = ARGM("$map", Map); + List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA); + for ( auto key : m->keys()) { + result->append(m->at(key)); + } + return result; + } + + Signature map_merge_sig = "map-merge($map1, $map2)"; + BUILT_IN(map_merge) + { + Map_Obj m1 = ARGM("$map1", Map); + Map_Obj m2 = ARGM("$map2", Map); + + size_t len = m1->length() + m2->length(); + Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, len); + // concat not implemented for maps + *result += m1; + *result += m2; + return result; + } + + Signature map_remove_sig = "map-remove($map, $keys...)"; + BUILT_IN(map_remove) + { + bool remove; + Map_Obj m = ARGM("$map", Map); + List_Obj arglist = ARG("$keys", List); + Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, 1); + for (auto key : m->keys()) { + remove = false; + for (size_t j = 0, K = arglist->length(); j < K && !remove; ++j) { + remove = Operators::eq(key, arglist->value_at_index(j)); + } + if (!remove) *result << std::make_pair(key, m->at(key)); + } + return result; + } + + } + +} \ No newline at end of file diff --git a/src/fn_maps.hpp b/src/fn_maps.hpp new file mode 100644 index 000000000..9551ec33e --- /dev/null +++ b/src/fn_maps.hpp @@ -0,0 +1,30 @@ +#ifndef SASS_FN_MAPS_H +#define SASS_FN_MAPS_H + +#include "fn_utils.hpp" + +namespace Sass { + + namespace Functions { + + #define ARGM(argname, argtype) get_arg_m(argname, env, sig, pstate, traces) + + extern Signature map_get_sig; + extern Signature map_merge_sig; + extern Signature map_remove_sig; + extern Signature map_keys_sig; + extern Signature map_values_sig; + extern Signature map_has_key_sig; + + BUILT_IN(map_get); + BUILT_IN(map_merge); + BUILT_IN(map_remove); + BUILT_IN(map_keys); + BUILT_IN(map_values); + BUILT_IN(map_has_key); + + } + +} + +#endif diff --git a/src/functions.cpp b/src/functions.cpp index e098e96c3..d8ecdc826 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -26,6 +26,7 @@ #include #include "fn_utils.hpp" +#include "fn_maps.hpp" #include "fn_lists.hpp" #include "fn_colors.hpp" #include "fn_numbers.hpp" @@ -53,89 +54,6 @@ namespace Sass { "custom-property" }; - ///////////////// - // MAP FUNCTIONS - ///////////////// - - Signature map_get_sig = "map-get($map, $key)"; - BUILT_IN(map_get) - { - // leaks for "map-get((), foo)" if not Obj - // investigate why this is (unexpected) - Map_Obj m = ARGM("$map", Map, ctx); - Expression_Obj v = ARG("$key", Expression); - try { - Expression_Obj val = m->at(v); - if (!val) return SASS_MEMORY_NEW(Null, pstate); - val->set_delayed(false); - return val.detach(); - } catch (const std::out_of_range&) { - return SASS_MEMORY_NEW(Null, pstate); - } - catch (...) { throw; } - } - - Signature map_has_key_sig = "map-has-key($map, $key)"; - BUILT_IN(map_has_key) - { - Map_Obj m = ARGM("$map", Map, ctx); - Expression_Obj v = ARG("$key", Expression); - return SASS_MEMORY_NEW(Boolean, pstate, m->has(v)); - } - - Signature map_keys_sig = "map-keys($map)"; - BUILT_IN(map_keys) - { - Map_Obj m = ARGM("$map", Map, ctx); - List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA); - for ( auto key : m->keys()) { - result->append(key); - } - return result; - } - - Signature map_values_sig = "map-values($map)"; - BUILT_IN(map_values) - { - Map_Obj m = ARGM("$map", Map, ctx); - List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA); - for ( auto key : m->keys()) { - result->append(m->at(key)); - } - return result; - } - - Signature map_merge_sig = "map-merge($map1, $map2)"; - BUILT_IN(map_merge) - { - Map_Obj m1 = ARGM("$map1", Map, ctx); - Map_Obj m2 = ARGM("$map2", Map, ctx); - - size_t len = m1->length() + m2->length(); - Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, len); - // concat not implemented for maps - *result += m1; - *result += m2; - return result; - } - - Signature map_remove_sig = "map-remove($map, $keys...)"; - BUILT_IN(map_remove) - { - bool remove; - Map_Obj m = ARGM("$map", Map, ctx); - List_Obj arglist = ARG("$keys", List); - Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, 1); - for (auto key : m->keys()) { - remove = false; - for (size_t j = 0, K = arglist->length(); j < K && !remove; ++j) { - remove = Operators::eq(key, arglist->value_at_index(j)); - } - if (!remove) *result << std::make_pair(key, m->at(key)); - } - return result; - } - ////////////////////////// // INTROSPECTION FUNCTIONS ////////////////////////// diff --git a/src/functions.hpp b/src/functions.hpp index 547283361..da683abf9 100644 --- a/src/functions.hpp +++ b/src/functions.hpp @@ -26,12 +26,6 @@ namespace Sass { extern Signature call_sig; extern Signature not_sig; extern Signature if_sig; - extern Signature map_get_sig; - extern Signature map_merge_sig; - extern Signature map_remove_sig; - extern Signature map_keys_sig; - extern Signature map_values_sig; - extern Signature map_has_key_sig; extern Signature content_exists_sig; extern Signature get_function_sig; @@ -44,12 +38,6 @@ namespace Sass { BUILT_IN(call); BUILT_IN(sass_not); BUILT_IN(sass_if); - BUILT_IN(map_get); - BUILT_IN(map_merge); - BUILT_IN(map_remove); - BUILT_IN(map_keys); - BUILT_IN(map_values); - BUILT_IN(map_has_key); BUILT_IN(content_exists); BUILT_IN(get_function); } diff --git a/win/libsass.targets b/win/libsass.targets index 3c2140388..c26d7f858 100644 --- a/win/libsass.targets +++ b/win/libsass.targets @@ -34,6 +34,7 @@ + @@ -93,6 +94,7 @@ + diff --git a/win/libsass.vcxproj.filters b/win/libsass.vcxproj.filters index fb2a715a1..9c0e3b562 100644 --- a/win/libsass.vcxproj.filters +++ b/win/libsass.vcxproj.filters @@ -114,6 +114,9 @@ Headers + + Headers + Headers @@ -290,6 +293,9 @@ Sources + + Sources + Sources