@@ -50,18 +50,25 @@ namespace Sass {
5050 this ->source = source;
5151 }
5252
53+ inline bool sort_importers (const Sass_Importer_Entry& i, const Sass_Importer_Entry& j)
54+ { return sass_importer_get_priority (i) > sass_importer_get_priority (j); }
5355
5456 Context::Context (Context::Data initializers)
5557 : // Output(this),
58+ head_imports (0 ),
5659 mem (Memory_Manager<AST_Node>()),
60+ c_options (initializers.c_options()),
61+ c_compiler (initializers.c_compiler()),
5762 source_c_str (initializers.source_c_str()),
5863 sources (vector<const char *>()),
5964 plugin_paths (initializers.plugin_paths()),
6065 include_paths (initializers.include_paths()),
6166 queue (vector<Sass_Queued>()),
6267 style_sheets (map<string, Block*>()),
6368 emitter (this ),
64- c_functions (vector<Sass_C_Function_Callback>()),
69+ c_headers (vector<Sass_Importer_Entry>()),
70+ c_importers (vector<Sass_Importer_Entry>()),
71+ c_functions (vector<Sass_Function_Entry>()),
6572 indent (initializers.indent()),
6673 linefeed (initializers.linefeed()),
6774 input_path (make_canonical_path(initializers.input_path())),
@@ -74,7 +81,6 @@ namespace Sass {
7481 source_map_contents (initializers.source_map_contents()),
7582 omit_source_map_url (initializers.omit_source_map_url()),
7683 is_indented_syntax_src (initializers.is_indented_syntax_src()),
77- importer (initializers.importer()),
7884 names_to_colors (map<string, Color*>()),
7985 colors_to_names (map<int , string>()),
8086 precision (initializers.precision()),
@@ -91,9 +97,9 @@ namespace Sass {
9197
9298 include_paths.push_back (cwd);
9399 collect_include_paths (initializers.include_paths_c_str ());
94- collect_include_paths (initializers.include_paths_array ());
100+ // collect_include_paths(initializers.include_paths_array());
95101 collect_plugin_paths (initializers.plugin_paths_c_str ());
96- collect_plugin_paths (initializers.plugin_paths_array ());
102+ // collect_plugin_paths(initializers.plugin_paths_array());
97103
98104 setup_color_map ();
99105
@@ -104,7 +110,15 @@ namespace Sass {
104110 for (auto fn : plugins.get_functions ()) {
105111 c_functions.push_back (fn);
106112 }
113+ for (auto fn : plugins.get_headers ()) {
114+ c_headers.push_back (fn);
115+ }
116+ for (auto fn : plugins.get_importers ()) {
117+ c_importers.push_back (fn);
118+ }
107119
120+ sort (c_headers.begin (), c_headers.end (), sort_importers);
121+ sort (c_importers.begin (), c_importers.end (), sort_importers);
108122 string entry_point = initializers.entry_point ();
109123 if (!entry_point.empty ()) {
110124 string result (add_file (entry_point));
@@ -117,6 +131,23 @@ namespace Sass {
117131
118132 }
119133
134+ void Context::add_c_function (Sass_Function_Entry function)
135+ {
136+ c_functions.push_back (function);
137+ }
138+ void Context::add_c_header (Sass_Importer_Entry header)
139+ {
140+ c_headers.push_back (header);
141+ // need to sort the array afterwards (no big deal)
142+ sort (c_headers.begin (), c_headers.end (), sort_importers);
143+ }
144+ void Context::add_c_importer (Sass_Importer_Entry importer)
145+ {
146+ c_importers.push_back (importer);
147+ // need to sort the array afterwards (no big deal)
148+ sort (c_importers.begin (), c_importers.end (), sort_importers);
149+ }
150+
120151 Context::~Context ()
121152 {
122153 // everything that gets put into sources will be freed by us
@@ -225,58 +256,45 @@ namespace Sass {
225256 include_links.push_back (resolve_relative_path (abs_path, source_map_file, cwd));
226257 }
227258
228- string Context::add_file (string path)
259+ // Add a new import file to the context
260+ string Context::add_file (const string& file)
229261 {
230262 using namespace File ;
231- char * contents = 0 ;
232- string real_path;
233- path = make_canonical_path (path);
234- for (size_t i = 0 , S = include_paths.size (); i < S; ++i) {
235- string full_path (join_paths (include_paths[i], path));
236- if (style_sheets.count (full_path)) return full_path;
237- contents = resolve_and_load (full_path, real_path);
238- if (contents) {
239- add_source (full_path, real_path, contents);
240- style_sheets[full_path] = 0 ;
241- return full_path;
242- }
263+ string path (make_canonical_path (file));
264+ string resolved (find_file (path, include_paths));
265+ if (resolved == " " ) return resolved;
266+ if (char * contents = read_file (resolved)) {
267+ add_source (path, resolved, contents);
268+ style_sheets[path] = 0 ;
269+ return path;
243270 }
244- return string ();
271+ return string (" " );
245272 }
246273
247- string Context::add_file (string dir, string rel_filepath)
274+ // Add a new import file to the context
275+ // This has some previous directory context
276+ string Context::add_file (const string& base, const string& file)
248277 {
249278 using namespace File ;
250- char * contents = 0 ;
251- string real_path;
252- rel_filepath = make_canonical_path (rel_filepath);
253- string full_path (join_paths (dir, rel_filepath));
254- if (style_sheets.count (full_path)) return full_path;
255- contents = resolve_and_load (full_path, real_path);
256- if (contents) {
257- add_source (full_path, real_path, contents);
258- style_sheets[full_path] = 0 ;
259- return full_path;
260- }
261- for (size_t i = 0 , S = include_paths.size (); i < S; ++i) {
262- string full_path (join_paths (include_paths[i], rel_filepath));
263- if (style_sheets.count (full_path)) return full_path;
264- contents = resolve_and_load (full_path, real_path);
265- if (contents) {
266- add_source (full_path, real_path, contents);
267- style_sheets[full_path] = 0 ;
268- return full_path;
269- }
279+ string path (make_canonical_path (file));
280+ string base_file (join_paths (base, path));
281+ string resolved (resolve_file (base_file));
282+ if (style_sheets.count (base_file)) return base_file;
283+ if (char * contents = read_file (resolved)) {
284+ add_source (base_file, resolved, contents);
285+ style_sheets[base_file] = 0 ;
286+ return base_file;
270287 }
271- return string ();
288+ // now go the regular code path
289+ return add_file (path);
272290 }
273291
274292 void register_function (Context&, Signature sig, Native_Function f, Env* env);
275293 void register_function (Context&, Signature sig, Native_Function f, size_t arity, Env* env);
276294 void register_overload_stub (Context&, string name, Env* env);
277295 void register_built_in_functions (Context&, Env* env);
278- void register_c_functions (Context&, Env* env, Sass_C_Function_List );
279- void register_c_function (Context&, Env* env, Sass_C_Function_Callback );
296+ void register_c_functions (Context&, Env* env, Sass_Function_List );
297+ void register_c_function (Context&, Env* env, Sass_Function_Entry );
280298
281299 char * Context::compile_block (Block* root)
282300 {
@@ -295,7 +313,7 @@ namespace Sass {
295313 {
296314 Block* root = 0 ;
297315 for (size_t i = 0 ; i < queue.size (); ++i) {
298- struct Sass_Import * import = sass_make_import (
316+ Sass_Import_Entry import = sass_make_import (
299317 queue[i].load_path .c_str (),
300318 queue[i].abs_path .c_str (),
301319 0 , 0
@@ -388,7 +406,9 @@ namespace Sass {
388406 std::vector<std::string> Context::get_included_files (size_t skip)
389407 {
390408 vector<string> includes = included_files;
409+ if (includes.size () == 0 ) return includes;
391410 std::sort ( includes.begin () + skip, includes.end () );
411+ includes.erase ( includes.begin (), includes.begin () + skip );
392412 includes.erase ( std::unique ( includes.begin (), includes.end () ), includes.end () );
393413 // the skip solution seems more robust, as we may have real files named stdin
394414 // includes.erase( std::remove( includes.begin(), includes.end(), "stdin" ), includes.end() );
@@ -423,6 +443,7 @@ namespace Sass {
423443 name,
424444 0 ,
425445 0 ,
446+ &ctx,
426447 true );
427448 (*env)[name + " [f]" ] = stub;
428449 }
@@ -521,21 +542,16 @@ namespace Sass {
521542 register_function (ctx, unique_id_sig, unique_id, env);
522543 }
523544
524- void register_c_functions (Context& ctx, Env* env, Sass_C_Function_List descrs)
545+ void register_c_functions (Context& ctx, Env* env, Sass_Function_List descrs)
525546 {
526547 while (descrs && *descrs) {
527548 register_c_function (ctx, env, *descrs);
528549 ++descrs;
529550 }
530551 }
531- void register_c_function (Context& ctx, Env* env, Sass_C_Function_Callback descr)
552+ void register_c_function (Context& ctx, Env* env, Sass_Function_Entry descr)
532553 {
533- Definition* def = make_c_function (
534- sass_function_get_signature (descr),
535- sass_function_get_function (descr),
536- sass_function_get_cookie (descr),
537- ctx
538- );
554+ Definition* def = make_c_function (descr, ctx);
539555 def->environment (env);
540556 (*env)[def->name () + " [f]" ] = def;
541557 }
0 commit comments