Skip to content

Commit 3753948

Browse files
committed
Refactor C-API path handling
- Removes sass_option_get_plugin_path(opt) - Removes sass_option_get_include_path(opt) Removed path options are colon-separated lists of paths. Most implementations will probably only want to use the push functions for paths. We still support the old way to set this option in order to help support SASS_PATH environment variables (implementers can just pass it in). Use the new query functions if you need them. - Adds sass_option_get_include_path_size(opt) - Adds sass_option_get_include_path(opt, i) - Fixes a minor memory leak with plugins - Adds sass_compiler_find_include(file, comp) - Adds sass_compiler_find_file(file, comp) - Adds sass_find_include(file, opt) - Adds sass_find_file(file, opt)
1 parent 8703ac3 commit 3753948

20 files changed

+277
-92
lines changed

docs/api-context-internal.md

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,35 @@ enum Sass_Input_Style {
77
SASS_CONTEXT_FOLDER
88
};
99

10-
// simple linked list
11-
struct string_list {
12-
string_list* next;
13-
char* string;
14-
};
15-
1610
// sass config options structure
17-
struct Sass_Options {
18-
19-
// Precision for fractional numbers
20-
int precision;
11+
struct Sass_Inspect_Options {
2112

2213
// Output style for the generated css code
2314
// A value from above SASS_STYLE_* constants
2415
enum Sass_Output_Style output_style;
2516

17+
// Precision for fractional numbers
18+
int precision;
19+
20+
};
21+
22+
// sass config options structure
23+
struct Sass_Output_Options : Sass_Inspect_Options {
24+
25+
// String to be used for indentation
26+
const char* indent;
27+
// String to be used to for line feeds
28+
const char* linefeed;
29+
2630
// Emit comments in the generated CSS indicating
2731
// the corresponding source line.
2832
bool source_comments;
2933

34+
};
35+
36+
// sass config options structure
37+
struct Sass_Options : Sass_Output_Options {
38+
3039
// embed sourceMappingUrl as data uri
3140
bool source_map_embed;
3241

@@ -56,15 +65,9 @@ struct Sass_Options {
5665
// information in source-maps etc.
5766
char* output_path;
5867

59-
// String to be used for indentation
60-
const char* indent;
61-
// String to be used to for line feeds
62-
const char* linefeed;
63-
6468
// Colon-separated list of paths
6569
// Semicolon-separated on Windows
66-
// Note: It may be better to use
67-
// array interface instead
70+
// Maybe use array interface instead?
6871
char* include_path;
6972
char* plugin_path;
7073

@@ -82,10 +85,13 @@ struct Sass_Options {
8285
char* source_map_root;
8386

8487
// Custom functions that can be called from sccs code
85-
Sass_C_Function_List c_functions;
88+
Sass_Function_List c_functions;
8689

8790
// Callback to overload imports
88-
Sass_C_Import_Callback importer;
91+
Sass_Importer_List c_importers;
92+
93+
// List of custom headers
94+
Sass_Importer_List c_headers;
8995

9096
};
9197

@@ -111,6 +117,7 @@ struct Sass_Context : Sass_Options
111117
char* error_file;
112118
size_t error_line;
113119
size_t error_column;
120+
const char* error_src;
114121

115122
// report imported files
116123
char** included_files;
@@ -130,6 +137,7 @@ struct Sass_Data_Context : Sass_Context {
130137

131138
// provided source string
132139
char* source_string;
140+
char* srcmap_string;
133141

134142
};
135143

@@ -147,9 +155,9 @@ struct Sass_Compiler {
147155
// original c context
148156
Sass_Context* c_ctx;
149157
// Sass::Context
150-
void* cpp_ctx;
158+
Sass::Context* cpp_ctx;
151159
// Sass::Block
152-
void* root;
160+
Sass::Block_Obj root;
153161
};
154162
```
155163

docs/api-context.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,6 @@ char* sass_context_take_error_message (struct Sass_Context* ctx);
223223
char* sass_context_take_error_file (struct Sass_Context* ctx);
224224
char* sass_context_take_output_string (struct Sass_Context* ctx);
225225
char* sass_context_take_source_map_string (struct Sass_Context* ctx);
226-
227-
// Push function for plugin/include paths (no manipulation support for now)
228-
void sass_option_push_plugin_path (struct Sass_Options* options, const char* path);
229-
void sass_option_push_include_path (struct Sass_Options* options, const char* path);
230226
```
231227
232228
### Sass Options API
@@ -245,13 +241,15 @@ const char* sass_option_get_indent (struct Sass_Options* options);
245241
const char* sass_option_get_linefeed (struct Sass_Options* options);
246242
const char* sass_option_get_input_path (struct Sass_Options* options);
247243
const char* sass_option_get_output_path (struct Sass_Options* options);
248-
const char* sass_option_get_plugin_path (struct Sass_Options* options);
249-
const char* sass_option_get_include_path (struct Sass_Options* options);
250244
const char* sass_option_get_source_map_file (struct Sass_Options* options);
251245
const char* sass_option_get_source_map_root (struct Sass_Options* options);
252246
Sass_C_Function_List sass_option_get_c_functions (struct Sass_Options* options);
253247
Sass_C_Import_Callback sass_option_get_importer (struct Sass_Options* options);
254248
249+
// Getters for Context_Option include path array
250+
size_t sass_option_get_include_path_size(struct Sass_Options* options);
251+
const char* sass_option_get_include_path(struct Sass_Options* options, size_t i);
252+
255253
// Setters for Context_Option values
256254
void sass_option_set_precision (struct Sass_Options* options, int precision);
257255
void sass_option_set_output_style (struct Sass_Options* options, enum Sass_Output_Style output_style);
@@ -275,6 +273,16 @@ void sass_option_set_importer (struct Sass_Options* options, Sass_C_Import_Callb
275273
// Push function for paths (no manipulation support for now)
276274
void sass_option_push_plugin_path (struct Sass_Options* options, const char* path);
277275
void sass_option_push_include_path (struct Sass_Options* options, const char* path);
276+
277+
// Resolve a file via the given include paths in the sass option struct
278+
// find_file looks for the exact file name while find_include does a regular sass include
279+
char* sass_find_file (const char* path, struct Sass_Options* opt);
280+
char* sass_find_include (const char* path, struct Sass_Options* opt);
281+
282+
// Resolve a file relative to last import or include paths in the sass option struct
283+
// find_file looks for the exact file name while find_include does a regular sass include
284+
char* sass_compiler_find_file (const char* path, struct Sass_Compiler* compiler);
285+
char* sass_compiler_find_include (const char* path, struct Sass_Compiler* compiler);
278286
```
279287

280288
### More links

docs/api-doc.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ void sass_free_memory(void* ptr);
125125
char* sass_string_unquote (const char* str);
126126
char* sass_string_quote (const char* str, const char quote_mark);
127127
128-
// Resolve a file via the given include paths in the include char* array
129-
char* sass_resolve_file (const char* path, const char* incs[]);
130-
131128
// Get compiled libsass version
132129
const char* libsass_version(void);
133130

include/sass/base.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@ ADDAPI void ADDCALL sass_free_memory(void* ptr);
7575
ADDAPI char* ADDCALL sass_string_quote (const char* str, const char quote_mark);
7676
ADDAPI char* ADDCALL sass_string_unquote (const char* str);
7777

78-
// Resolve a file via the given include paths in the include char* array
79-
ADDAPI char* ADDCALL sass_resolve_file (const char* path, const char* incs[]);
80-
8178
// Implemented sass language version
8279
// Hardcoded version 3.4 for time being
8380
ADDAPI const char* ADDCALL libsass_version(void);

include/sass/context.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ ADDAPI const char* ADDCALL sass_option_get_indent (struct Sass_Options* options)
8181
ADDAPI const char* ADDCALL sass_option_get_linefeed (struct Sass_Options* options);
8282
ADDAPI const char* ADDCALL sass_option_get_input_path (struct Sass_Options* options);
8383
ADDAPI const char* ADDCALL sass_option_get_output_path (struct Sass_Options* options);
84-
ADDAPI const char* ADDCALL sass_option_get_plugin_path (struct Sass_Options* options);
85-
ADDAPI const char* ADDCALL sass_option_get_include_path (struct Sass_Options* options);
8684
ADDAPI const char* ADDCALL sass_option_get_source_map_file (struct Sass_Options* options);
8785
ADDAPI const char* ADDCALL sass_option_get_source_map_root (struct Sass_Options* options);
8886
ADDAPI Sass_Importer_List ADDCALL sass_option_get_c_headers (struct Sass_Options* options);
@@ -124,6 +122,10 @@ ADDAPI size_t ADDCALL sass_context_get_error_column (struct Sass_Context* ctx);
124122
ADDAPI const char* ADDCALL sass_context_get_source_map_string (struct Sass_Context* ctx);
125123
ADDAPI char** ADDCALL sass_context_get_included_files (struct Sass_Context* ctx);
126124

125+
// Getters for options include path array
126+
ADDAPI size_t ADDCALL sass_option_get_include_path_size(struct Sass_Options* options);
127+
ADDAPI const char* ADDCALL sass_option_get_include_path(struct Sass_Options* options, size_t i);
128+
127129
// Calculate the size of the stored null terminated array
128130
ADDAPI size_t ADDCALL sass_context_get_included_files_size (struct Sass_Context* ctx);
129131

@@ -151,6 +153,16 @@ ADDAPI Sass_Callee_Entry ADDCALL sass_compiler_get_callee_entry(struct Sass_Comp
151153
ADDAPI void ADDCALL sass_option_push_plugin_path (struct Sass_Options* options, const char* path);
152154
ADDAPI void ADDCALL sass_option_push_include_path (struct Sass_Options* options, const char* path);
153155

156+
// Resolve a file via the given include paths in the sass option struct
157+
// find_file looks for the exact file name while find_include does a regular sass include
158+
ADDAPI char* ADDCALL sass_find_file (const char* path, struct Sass_Options* opt);
159+
ADDAPI char* ADDCALL sass_find_include (const char* path, struct Sass_Options* opt);
160+
161+
// Resolve a file relative to last import or include paths in the sass option struct
162+
// find_file looks for the exact file name while find_include does a regular sass include
163+
ADDAPI char* ADDCALL sass_compiler_find_file (const char* path, struct Sass_Compiler* compiler);
164+
ADDAPI char* ADDCALL sass_compiler_find_include (const char* path, struct Sass_Compiler* compiler);
165+
154166
#ifdef __cplusplus
155167
} // __cplusplus defined.
156168
#endif

include/sass/functions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum Sass_Callee_Type {
5151
ADDAPI Sass_Importer_List ADDCALL sass_make_importer_list (size_t length);
5252
ADDAPI Sass_Importer_Entry ADDCALL sass_importer_get_list_entry (Sass_Importer_List list, size_t idx);
5353
ADDAPI void ADDCALL sass_importer_set_list_entry (Sass_Importer_List list, size_t idx, Sass_Importer_Entry entry);
54+
ADDAPI void ADDCALL sass_delete_importer_list (Sass_Importer_List list);
5455

5556

5657
// Creators for custom importer callback (with some additional pointer)
@@ -118,6 +119,8 @@ ADDAPI void ADDCALL sass_delete_import (Sass_Import_Entry);
118119
// Creators for sass function list and function descriptors
119120
ADDAPI Sass_Function_List ADDCALL sass_make_function_list (size_t length);
120121
ADDAPI Sass_Function_Entry ADDCALL sass_make_function (const char* signature, Sass_Function_Fn cb, void* cookie);
122+
ADDAPI void ADDCALL sass_delete_function (Sass_Function_Entry entry);
123+
ADDAPI void ADDCALL sass_delete_function_list (Sass_Function_List list);
121124

122125
// Setters and getters for callbacks on function lists
123126
ADDAPI Sass_Function_Entry ADDCALL sass_function_get_list_entry(Sass_Function_List list, size_t pos);

script/ci-build-libsass

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fi
3737
if [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]; then
3838
MAKE_OPTS="$MAKE_OPTS -j1 V=1"
3939
else
40-
MAKE_OPTS="$MAKE_OPTS -j3 V=1"
40+
MAKE_OPTS="$MAKE_OPTS -j5 V=1"
4141
fi
4242

4343
if [ "x$PREFIX" == "x" ]; then

src/context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ namespace Sass {
667667

668668
// clean up by removing empty placeholders
669669
// ToDo: maybe we can do this somewhere else?
670-
Remove_Placeholders remove_placeholders(*this);
670+
Remove_Placeholders remove_placeholders;
671671
root->perform(&remove_placeholders);
672672
// return processed tree
673673
return root;

src/eval.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,8 +1012,8 @@ namespace Sass {
10121012
value = SASS_MEMORY_CAST(Expression, (*env)[name]);
10131013
}
10141014
else error("Undefined variable: \"" + v->name() + "\".", v->pstate());
1015-
if (typeid(*value) == typeid(Argument)) {
1016-
value = SASS_MEMORY_CAST(Argument, value)->value();
1015+
if (Argument* arg = SASS_MEMORY_CAST(Argument, value)) {
1016+
value = arg->value();
10171017
}
10181018

10191019
// behave according to as ruby sass (add leading zero)

src/extend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ namespace Sass {
19331933
}
19341934
}
19351935

1936-
Remove_Placeholders remove_placeholders(ctx);
1936+
Remove_Placeholders remove_placeholders;
19371937
// it seems that we have to remove the place holders early here
19381938
// normally we do this as the very last step (compare to ruby sass)
19391939
pNewSelectors = remove_placeholders.remove_placeholders(&pNewSelectors);

src/file.cpp

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "context.hpp"
2121
#include "prelexer.hpp"
2222
#include "utf8_string.hpp"
23+
#include "sass_functions.hpp"
2324
#include "sass2scss.h"
2425

2526
#ifdef _WIN32
@@ -301,13 +302,9 @@ namespace Sass {
301302
// (2) underscore + given
302303
// (3) underscore + given + extension
303304
// (4) given + extension
304-
std::vector<Include> resolve_includes(const std::string& root, const std::string& file)
305+
std::vector<Include> resolve_includes(const std::string& root, const std::string& file, const std::vector<std::string>& exts)
305306
{
306307
std::string filename = join_paths(root, file);
307-
// supported extensions
308-
const std::vector<std::string> exts = {
309-
".scss", ".sass", ".css"
310-
};
311308
// split the filename
312309
std::string base(dir_name(file));
313310
std::string name(base_name(file));
@@ -336,8 +333,41 @@ namespace Sass {
336333
return includes;
337334
}
338335

339-
// helper function to resolve a filename
336+
std::vector<std::string> find_files(const std::string& file, const std::vector<std::string> paths)
337+
{
338+
std::vector<std::string> includes;
339+
for (std::string path : paths) {
340+
std::string abs_path(join_paths(path, file));
341+
if (file_exists(abs_path)) includes.push_back(abs_path);
342+
}
343+
return includes;
344+
}
345+
346+
std::vector<std::string> find_files(const std::string& file, struct Sass_Compiler* compiler)
347+
{
348+
// get the last import entry to get current base directory
349+
// struct Sass_Options* options = sass_compiler_get_options(compiler);
350+
Sass_Import_Entry import = sass_compiler_get_last_import(compiler);
351+
const std::vector<std::string>& incs = compiler->cpp_ctx->include_paths;
352+
// create the vector with paths to lookup
353+
std::vector<std::string> paths(1 + incs.size());
354+
paths.push_back(dir_name(import->abs_path));
355+
paths.insert(paths.end(), incs.begin(), incs.end());
356+
// dispatch to find files in paths
357+
return find_files(file, paths);
358+
}
359+
360+
// helper function to search one file in all include paths
361+
// this is normally not used internally by libsass (C-API sugar)
340362
std::string find_file(const std::string& file, const std::vector<std::string> paths)
363+
{
364+
if (file.empty()) return file;
365+
auto res = find_files(file, paths);
366+
return res.empty() ? "" : res.front();
367+
}
368+
369+
// helper function to resolve a filename
370+
std::string find_include(const std::string& file, const std::vector<std::string> paths)
341371
{
342372
// search in every include path for a match
343373
for (size_t i = 0, S = paths.size(); i < S; ++i)
@@ -349,20 +379,6 @@ namespace Sass {
349379
return std::string("");
350380
}
351381

352-
// inc paths can be directly passed from C code
353-
std::string find_file(const std::string& file, const char* paths[])
354-
{
355-
if (paths == 0) return std::string("");
356-
std::vector<std::string> includes(0);
357-
// includes.push_back(".");
358-
const char** it = paths;
359-
while (it && *it) {
360-
includes.push_back(*it);
361-
++it;
362-
}
363-
return find_file(file, includes);
364-
}
365-
366382
// try to load the given filename
367383
// returned memory must be freed
368384
// will auto convert .sass files
@@ -414,5 +430,25 @@ namespace Sass {
414430
}
415431
}
416432

433+
// split a path string delimited by semicolons or colons (OS dependent)
434+
std::vector<std::string> split_path_list(const char* str)
435+
{
436+
std::vector<std::string> paths;
437+
if (str == NULL) return paths;
438+
// find delimiter via prelexer (return zero at end)
439+
const char* end = Prelexer::find_first<PATH_SEP>(str);
440+
// search until null delimiter
441+
while (end) {
442+
// add path from current position to delimiter
443+
paths.push_back(std::string(str, end - str));
444+
str = end + 1; // skip delimiter
445+
end = Prelexer::find_first<PATH_SEP>(str);
446+
}
447+
// add path from current position to end
448+
paths.push_back(std::string(str));
449+
// return back
450+
return paths;
451+
}
452+
417453
}
418454
}

0 commit comments

Comments
 (0)