Skip to content

Commit 47354ad

Browse files
committed
Move map functions to own compile unit
1 parent 4d1e036 commit 47354ad

8 files changed

+135
-95
lines changed

Makefile.conf

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SOURCES = \
1111
context.cpp \
1212
constants.cpp \
1313
fn_utils.cpp \
14+
fn_maps.cpp \
1415
fn_lists.cpp \
1516
fn_colors.cpp \
1617
fn_numbers.cpp \

src/context.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "prelexer.hpp"
3131
#include "emitter.hpp"
3232
#include "fn_utils.hpp"
33+
#include "fn_maps.hpp"
3334
#include "fn_lists.hpp"
3435
#include "fn_colors.hpp"
3536
#include "fn_numbers.hpp"

src/fn_maps.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "operators.hpp"
2+
#include "fn_utils.hpp"
3+
#include "fn_maps.hpp"
4+
5+
namespace Sass {
6+
7+
namespace Functions {
8+
9+
/////////////////
10+
// MAP FUNCTIONS
11+
/////////////////
12+
13+
Signature map_get_sig = "map-get($map, $key)";
14+
BUILT_IN(map_get)
15+
{
16+
// leaks for "map-get((), foo)" if not Obj
17+
// investigate why this is (unexpected)
18+
Map_Obj m = ARGM("$map", Map);
19+
Expression_Obj v = ARG("$key", Expression);
20+
try {
21+
Value_Obj val = m->at(v);
22+
if (!val) return SASS_MEMORY_NEW(Null, pstate);
23+
val->set_delayed(false);
24+
return val.detach();
25+
} catch (const std::out_of_range&) {
26+
return SASS_MEMORY_NEW(Null, pstate);
27+
}
28+
catch (...) { throw; }
29+
}
30+
31+
Signature map_has_key_sig = "map-has-key($map, $key)";
32+
BUILT_IN(map_has_key)
33+
{
34+
Map_Obj m = ARGM("$map", Map);
35+
Expression_Obj v = ARG("$key", Expression);
36+
return SASS_MEMORY_NEW(Boolean, pstate, m->has(v));
37+
}
38+
39+
Signature map_keys_sig = "map-keys($map)";
40+
BUILT_IN(map_keys)
41+
{
42+
Map_Obj m = ARGM("$map", Map);
43+
List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA);
44+
for ( auto key : m->keys()) {
45+
result->append(key);
46+
}
47+
return result;
48+
}
49+
50+
Signature map_values_sig = "map-values($map)";
51+
BUILT_IN(map_values)
52+
{
53+
Map_Obj m = ARGM("$map", Map);
54+
List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA);
55+
for ( auto key : m->keys()) {
56+
result->append(m->at(key));
57+
}
58+
return result;
59+
}
60+
61+
Signature map_merge_sig = "map-merge($map1, $map2)";
62+
BUILT_IN(map_merge)
63+
{
64+
Map_Obj m1 = ARGM("$map1", Map);
65+
Map_Obj m2 = ARGM("$map2", Map);
66+
67+
size_t len = m1->length() + m2->length();
68+
Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, len);
69+
// concat not implemented for maps
70+
*result += m1;
71+
*result += m2;
72+
return result;
73+
}
74+
75+
Signature map_remove_sig = "map-remove($map, $keys...)";
76+
BUILT_IN(map_remove)
77+
{
78+
bool remove;
79+
Map_Obj m = ARGM("$map", Map);
80+
List_Obj arglist = ARG("$keys", List);
81+
Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, 1);
82+
for (auto key : m->keys()) {
83+
remove = false;
84+
for (size_t j = 0, K = arglist->length(); j < K && !remove; ++j) {
85+
remove = Operators::eq(key, arglist->value_at_index(j));
86+
}
87+
if (!remove) *result << std::make_pair(key, m->at(key));
88+
}
89+
return result;
90+
}
91+
92+
}
93+
94+
}

src/fn_maps.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef SASS_FN_MAPS_H
2+
#define SASS_FN_MAPS_H
3+
4+
#include "fn_utils.hpp"
5+
6+
namespace Sass {
7+
8+
namespace Functions {
9+
10+
#define ARGM(argname, argtype) get_arg_m(argname, env, sig, pstate, traces)
11+
12+
extern Signature map_get_sig;
13+
extern Signature map_merge_sig;
14+
extern Signature map_remove_sig;
15+
extern Signature map_keys_sig;
16+
extern Signature map_values_sig;
17+
extern Signature map_has_key_sig;
18+
19+
BUILT_IN(map_get);
20+
BUILT_IN(map_merge);
21+
BUILT_IN(map_remove);
22+
BUILT_IN(map_keys);
23+
BUILT_IN(map_values);
24+
BUILT_IN(map_has_key);
25+
26+
}
27+
28+
}
29+
30+
#endif

src/functions.cpp

+1-83
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <set>
2727

2828
#include "fn_utils.hpp"
29+
#include "fn_maps.hpp"
2930
#include "fn_lists.hpp"
3031
#include "fn_colors.hpp"
3132
#include "fn_numbers.hpp"
@@ -53,89 +54,6 @@ namespace Sass {
5354
"custom-property"
5455
};
5556

56-
/////////////////
57-
// MAP FUNCTIONS
58-
/////////////////
59-
60-
Signature map_get_sig = "map-get($map, $key)";
61-
BUILT_IN(map_get)
62-
{
63-
// leaks for "map-get((), foo)" if not Obj
64-
// investigate why this is (unexpected)
65-
Map_Obj m = ARGM("$map", Map, ctx);
66-
Expression_Obj v = ARG("$key", Expression);
67-
try {
68-
Expression_Obj val = m->at(v);
69-
if (!val) return SASS_MEMORY_NEW(Null, pstate);
70-
val->set_delayed(false);
71-
return val.detach();
72-
} catch (const std::out_of_range&) {
73-
return SASS_MEMORY_NEW(Null, pstate);
74-
}
75-
catch (...) { throw; }
76-
}
77-
78-
Signature map_has_key_sig = "map-has-key($map, $key)";
79-
BUILT_IN(map_has_key)
80-
{
81-
Map_Obj m = ARGM("$map", Map, ctx);
82-
Expression_Obj v = ARG("$key", Expression);
83-
return SASS_MEMORY_NEW(Boolean, pstate, m->has(v));
84-
}
85-
86-
Signature map_keys_sig = "map-keys($map)";
87-
BUILT_IN(map_keys)
88-
{
89-
Map_Obj m = ARGM("$map", Map, ctx);
90-
List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA);
91-
for ( auto key : m->keys()) {
92-
result->append(key);
93-
}
94-
return result;
95-
}
96-
97-
Signature map_values_sig = "map-values($map)";
98-
BUILT_IN(map_values)
99-
{
100-
Map_Obj m = ARGM("$map", Map, ctx);
101-
List_Ptr result = SASS_MEMORY_NEW(List, pstate, m->length(), SASS_COMMA);
102-
for ( auto key : m->keys()) {
103-
result->append(m->at(key));
104-
}
105-
return result;
106-
}
107-
108-
Signature map_merge_sig = "map-merge($map1, $map2)";
109-
BUILT_IN(map_merge)
110-
{
111-
Map_Obj m1 = ARGM("$map1", Map, ctx);
112-
Map_Obj m2 = ARGM("$map2", Map, ctx);
113-
114-
size_t len = m1->length() + m2->length();
115-
Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, len);
116-
// concat not implemented for maps
117-
*result += m1;
118-
*result += m2;
119-
return result;
120-
}
121-
122-
Signature map_remove_sig = "map-remove($map, $keys...)";
123-
BUILT_IN(map_remove)
124-
{
125-
bool remove;
126-
Map_Obj m = ARGM("$map", Map, ctx);
127-
List_Obj arglist = ARG("$keys", List);
128-
Map_Ptr result = SASS_MEMORY_NEW(Map, pstate, 1);
129-
for (auto key : m->keys()) {
130-
remove = false;
131-
for (size_t j = 0, K = arglist->length(); j < K && !remove; ++j) {
132-
remove = Operators::eq(key, arglist->value_at_index(j));
133-
}
134-
if (!remove) *result << std::make_pair(key, m->at(key));
135-
}
136-
return result;
137-
}
138-
13957
//////////////////////////
14058
// INTROSPECTION FUNCTIONS
14159
//////////////////////////

src/functions.hpp

-12
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@ namespace Sass {
2626
extern Signature call_sig;
2727
extern Signature not_sig;
2828
extern Signature if_sig;
29-
extern Signature map_get_sig;
30-
extern Signature map_merge_sig;
31-
extern Signature map_remove_sig;
32-
extern Signature map_keys_sig;
33-
extern Signature map_values_sig;
34-
extern Signature map_has_key_sig;
3529
extern Signature content_exists_sig;
3630
extern Signature get_function_sig;
3731

@@ -44,12 +38,6 @@ namespace Sass {
4438
BUILT_IN(call);
4539
BUILT_IN(sass_not);
4640
BUILT_IN(sass_if);
47-
BUILT_IN(map_get);
48-
BUILT_IN(map_merge);
49-
BUILT_IN(map_remove);
50-
BUILT_IN(map_keys);
51-
BUILT_IN(map_values);
52-
BUILT_IN(map_has_key);
5341
BUILT_IN(content_exists);
5442
BUILT_IN(get_function);
5543
}

win/libsass.targets

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\file.hpp" />
3535
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_utils.hpp" />
3636
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\functions.hpp" />
37+
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_maps.hpp" />
3738
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_lists.hpp" />
3839
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_colors.hpp" />
3940
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_numbers.hpp" />
@@ -93,6 +94,7 @@
9394
<ClCompile Include="$(LIBSASS_SRC_DIR)\extend.cpp" />
9495
<ClCompile Include="$(LIBSASS_SRC_DIR)\file.cpp" />
9596
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_utils.cpp" />
97+
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_maps.cpp" />
9698
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_lists.cpp" />
9799
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_colors.cpp" />
98100
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_numbers.cpp" />

win/libsass.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\functions.hpp">
115115
<Filter>Headers</Filter>
116116
</ClInclude>
117+
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_maps.hpp">
118+
<Filter>Headers</Filter>
119+
</ClInclude>
117120
<ClInclude Include="$(LIBSASS_HEADERS_DIR)\fn_lists.hpp">
118121
<Filter>Headers</Filter>
119122
</ClInclude>
@@ -290,6 +293,9 @@
290293
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_utils.cpp">
291294
<Filter>Sources</Filter>
292295
</ClCompile>
296+
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_maps.cpp">
297+
<Filter>Sources</Filter>
298+
</ClCompile>
293299
<ClCompile Include="$(LIBSASS_SRC_DIR)\fn_lists.cpp">
294300
<Filter>Sources</Filter>
295301
</ClCompile>

0 commit comments

Comments
 (0)