Skip to content
Permalink
Browse files

Move map functions to own compile unit

  • Loading branch information...
mgreter committed Mar 17, 2018
1 parent 4d1e036 commit 47354add573d8f6ced7b70f084dbda7c2d923fea
Showing with 135 additions and 95 deletions.
  1. +1 −0 Makefile.conf
  2. +1 −0 src/context.cpp
  3. +94 −0 src/fn_maps.cpp
  4. +30 −0 src/fn_maps.hpp
  5. +1 −83 src/functions.cpp
  6. +0 −12 src/functions.hpp
  7. +2 −0 win/libsass.targets
  8. +6 −0 win/libsass.vcxproj.filters
@@ -11,6 +11,7 @@ SOURCES = \
context.cpp \
constants.cpp \
fn_utils.cpp \
fn_maps.cpp \
fn_lists.cpp \
fn_colors.cpp \
fn_numbers.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"
@@ -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;
}

}

}
@@ -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
@@ -26,6 +26,7 @@
#include <set>

#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
//////////////////////////
@@ -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);
}
@@ -34,6 +34,7 @@
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\file.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_utils.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\functions.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_maps.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_lists.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_colors.hpp" />
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_numbers.hpp" />
@@ -93,6 +94,7 @@
<ClCompile Include="$(LIBSASS_SRC_DIR)\extend.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\file.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_utils.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_maps.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_lists.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_colors.cpp" />
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_numbers.cpp" />
@@ -114,6 +114,9 @@
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\functions.hpp">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_maps.hpp">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_lists.hpp">
<Filter>Headers</Filter>
</ClInclude>
@@ -290,6 +293,9 @@
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_utils.cpp">
<Filter>Sources</Filter>
</ClCompile>
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_maps.cpp">
<Filter>Sources</Filter>
</ClCompile>
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_lists.cpp">
<Filter>Sources</Filter>
</ClCompile>

0 comments on commit 47354ad

Please sign in to comment.
You can’t perform that action at this time.