Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 85f7b2e

Browse files
author
Aaron Leung
committed
Merge pull request #181 from mgreter/master
Fixes for sass_interface (C Bindings)
2 parents a561f12 + 691e101 commit 85f7b2e

File tree

15 files changed

+127
-27
lines changed

15 files changed

+127
-27
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "sass2scss"]
2+
path = sass2scss
3+
url = https://github.com/mgreter/sass2scss.git

ast.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ namespace Sass {
409409
ADD_PROPERTY(Type, type);
410410
ADD_PROPERTY(Native_Function, native_function);
411411
ADD_PROPERTY(Sass_C_Function, c_function);
412+
ADD_PROPERTY(void*, cookie);
412413
ADD_PROPERTY(bool, is_overload_stub);
413414
ADD_PROPERTY(Signature, signature);
414415
public:
@@ -425,6 +426,7 @@ namespace Sass {
425426
type_(t),
426427
native_function_(0),
427428
c_function_(0),
429+
cookie_(0),
428430
is_overload_stub_(false),
429431
signature_(0)
430432
{ }
@@ -442,6 +444,7 @@ namespace Sass {
442444
type_(FUNCTION),
443445
native_function_(func_ptr),
444446
c_function_(0),
447+
cookie_(0),
445448
is_overload_stub_(overload_stub),
446449
signature_(sig)
447450
{ }
@@ -451,6 +454,7 @@ namespace Sass {
451454
string n,
452455
Parameters* params,
453456
Sass_C_Function func_ptr,
457+
void* cookie,
454458
bool whatever,
455459
bool whatever2)
456460
: Has_Block(path, position, 0),
@@ -460,6 +464,7 @@ namespace Sass {
460464
type_(FUNCTION),
461465
native_function_(0),
462466
c_function_(func_ptr),
467+
cookie_(cookie),
463468
is_overload_stub_(false),
464469
signature_(sig)
465470
{ }
@@ -597,9 +602,13 @@ namespace Sass {
597602
class Function_Call : public Expression {
598603
ADD_PROPERTY(string, name);
599604
ADD_PROPERTY(Arguments*, arguments);
605+
ADD_PROPERTY(void*, cookie);
600606
public:
607+
Function_Call(string path, Position position, string n, Arguments* args, void* cookie)
608+
: Expression(path, position), name_(n), arguments_(args), cookie_(cookie)
609+
{ concrete_type(STRING); }
601610
Function_Call(string path, Position position, string n, Arguments* args)
602-
: Expression(path, position), name_(n), arguments_(args)
611+
: Expression(path, position), name_(n), arguments_(args), cookie_(0)
603612
{ concrete_type(STRING); }
604613
ATTACH_OPERATIONS();
605614
};

constants.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace Sass {
6666
extern const char even_kwd[] = "even";
6767
extern const char odd_kwd[] = "odd";
6868
extern const char progid_kwd[] = "progid";
69+
extern const char expression_kwd[] = "expression";
6970
extern const char calc_kwd[] = "calc(";
7071

7172
// css attribute-matching operators

constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ namespace Sass {
6666
extern const char even_kwd[];
6767
extern const char odd_kwd[];
6868
extern const char progid_kwd[];
69+
extern const char expression_kwd[];
6970
extern const char calc_kwd[];
7071

7172
// css attribute-matching operators

context.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace Sass {
4545
: mem(Memory_Manager<AST_Node>()),
4646
source_c_str (initializers.source_c_str()),
4747
sources (vector<const char*>()),
48+
c_functions (vector<Sass_C_Function_Descriptor>()),
4849
include_paths (initializers.include_paths()),
4950
queue (vector<pair<string, const char*> >()),
5051
style_sheets (map<string, Block*>()),
@@ -212,6 +213,9 @@ namespace Sass {
212213
Env tge;
213214
Backtrace backtrace(0, "", Position(), "");
214215
register_built_in_functions(*this, &tge);
216+
for (size_t i = 0, S = c_functions.size(); i < S; ++i) {
217+
register_c_function(*this, &tge, c_functions[i]);
218+
}
215219
Eval eval(*this, &tge, &backtrace);
216220
Contextualize contextualize(*this, &eval, &tge, &backtrace);
217221
Expand expand(*this, &eval, &contextualize, &tge, &backtrace);
@@ -282,7 +286,7 @@ namespace Sass {
282286
string cwd = getcwd(wd, wd_len);
283287
if (cwd[cwd.length() - 1] != '/') cwd += '/';
284288
#ifdef _WIN32
285-
replace(begin(cwd), end(cwd), '\\', '/'); //convert Windows backslashes to URL forward slashes
289+
replace(cwd.begin(), cwd.end(), '\\', '/'); //convert Windows backslashes to URL forward slashes
286290
#endif
287291
return cwd;
288292
}
@@ -320,7 +324,7 @@ namespace Sass {
320324
{
321325
using namespace Functions;
322326
// RGB Functions
323-
register_function(ctx, rgb_sig, rgb, env);
327+
register_function(ctx, rgb_sig, rgb, env);
324328
register_overload_stub(ctx, "rgba", env);
325329
register_function(ctx, rgba_4_sig, rgba_4, 4, env);
326330
register_function(ctx, rgba_2_sig, rgba_2, 2, env);
@@ -394,7 +398,7 @@ namespace Sass {
394398
}
395399
void register_c_function(Context& ctx, Env* env, Sass_C_Function_Descriptor descr)
396400
{
397-
Definition* def = make_c_function(descr.signature, descr.function, ctx);
401+
Definition* def = make_c_function(descr.signature, descr.function, descr.cookie, ctx);
398402
def->environment(env);
399403
(*env)[def->name() + "[f]"] = def;
400404
}

context.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "subset_map.hpp"
2222
#endif
2323

24+
struct Sass_C_Function_Descriptor;
25+
2426
namespace Sass {
2527
using namespace std;
2628
class AST_Node;
@@ -44,6 +46,7 @@ namespace Sass {
4446
vector<pair<string, const char*> > queue; // queue of files to be parsed
4547
map<string, Block*> style_sheets; // map of paths to ASTs
4648
SourceMap source_map;
49+
vector<Sass_C_Function_Descriptor> c_functions;
4750

4851
string image_path; // for the image-url Sass function
4952
bool source_comments;

eval.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ namespace Sass {
340340
backtrace = &here;
341341

342342
To_C to_c;
343-
Sass_Value c_val = c_func(args->perform(&to_c));
343+
Sass_Value c_val = c_func(args->perform(&to_c), def->cookie());
344344
if (c_val.unknown.tag == SASS_ERROR) {
345345
error("error in C function " + c->name() + ": " + c_val.error.message, c->path(), c->position(), backtrace);
346346
}

file.cpp

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sys/stat.h>
1010
#include "file.hpp"
1111
#include "context.hpp"
12+
#include "sass2scss/sass2scss.h"
1213

1314
namespace Sass {
1415
namespace File {
@@ -134,13 +135,32 @@ namespace Sass {
134135
real_path = dir + _base_scss;
135136
// if the file isn't found with '_' + filename + ".scss" ...
136137
if (!(contents = read_file(real_path))) {
137-
string base_scss(base + ".scss");
138-
// try filename + ".scss" as the last resort
139-
real_path = dir + base_scss;
140-
contents = read_file(real_path);
138+
string _base_sass(_base + ".sass");
139+
real_path = dir + _base_sass;
140+
// if the file isn't found with '_' + filename + ".sass" ...
141+
if (!(contents = read_file(real_path))) {
142+
string base_scss(base + ".scss");
143+
real_path = dir + base_scss;
144+
// if the file isn't found with filename + ".scss" ...
145+
if (!(contents = read_file(real_path))) {
146+
string base_sass(base + ".sass");
147+
real_path = dir + base_sass;
148+
// if the file isn't found with filename + ".sass" ...
149+
if (!(contents = read_file(real_path))) {
150+
// default back to scss version
151+
real_path = dir + base_scss;
152+
}
153+
}
154+
}
141155
}
142156
}
143157
}
158+
string extension;
159+
if (real_path.length() > 5) {
160+
extension = real_path.substr(real_path.length() - 5, 5);
161+
}
162+
for(int i=0; i<extension.size();++i)
163+
extension[i] = tolower(extension[i]);
144164
return contents;
145165
}
146166

@@ -149,6 +169,10 @@ namespace Sass {
149169
struct stat st;
150170
if (stat(path.c_str(), &st) == -1 || S_ISDIR(st.st_mode)) return 0;
151171
ifstream file(path.c_str(), ios::in | ios::binary | ios::ate);
172+
string extension;
173+
if (path.length() > 5) {
174+
extension = path.substr(path.length() - 5, 5);
175+
}
152176
char* contents = 0;
153177
if (file.is_open()) {
154178
size_t size = file.tellg();
@@ -158,7 +182,15 @@ namespace Sass {
158182
contents[size] = '\0';
159183
file.close();
160184
}
161-
return contents;
185+
for(int i=0; i<extension.size();++i)
186+
extension[i] = tolower(extension[i]);
187+
if (extension == ".sass" && contents != 0) {
188+
char * converted = Sass::sass2scss(contents, SASS2SCSS_PRETTIFY_1);
189+
delete[] contents; // free the indented contents
190+
return converted; // should be freed by caller
191+
} else {
192+
return contents;
193+
}
162194
}
163195

164196
}

functions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace Sass {
3636
false);
3737
}
3838

39-
Definition* make_c_function(Signature sig, Sass_C_Function f, Context& ctx)
39+
Definition* make_c_function(Signature sig, Sass_C_Function f, void* cookie, Context& ctx)
4040
{
4141
Parser sig_parser = Parser::from_c_str(sig, ctx, "[c function]");
4242
sig_parser.lex<Prelexer::identifier>();
@@ -48,6 +48,7 @@ namespace Sass {
4848
name,
4949
params,
5050
f,
51+
cookie,
5152
false, true);
5253
}
5354

functions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Sass {
2828
typedef Expression* (*Native_Function)(Env&, Context&, Signature, const string&, Position, Backtrace*);
2929

3030
Definition* make_native_function(Signature, Native_Function, Context&);
31-
Definition* make_c_function(Signature sig, Sass_C_Function f, Context& ctx);
31+
Definition* make_c_function(Signature sig, Sass_C_Function f, void* cookie, Context& ctx);
3232

3333
namespace Functions {
3434

0 commit comments

Comments
 (0)