Skip to content

Commit

Permalink
Move map functions to own compile unit
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Mar 17, 2018
1 parent 4d1e036 commit 47354ad
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 95 deletions.
1 change: 1 addition & 0 deletions Makefile.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SOURCES = \
context.cpp \
constants.cpp \
fn_utils.cpp \
fn_maps.cpp \
fn_lists.cpp \
fn_colors.cpp \
fn_numbers.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
94 changes: 94 additions & 0 deletions src/fn_maps.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

}

}
30 changes: 30 additions & 0 deletions src/fn_maps.hpp
Original file line number Diff line number Diff line change
@@ -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
84 changes: 1 addition & 83 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
//////////////////////////
Expand Down
12 changes: 0 additions & 12 deletions src/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions win/libsass.targets
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down Expand Up @@ -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" />
Expand Down
6 changes: 6 additions & 0 deletions win/libsass.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 47354ad

Please sign in to comment.