forked from sass/perl-libsass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSass.xs
363 lines (316 loc) · 14.2 KB
/
Sass.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright © 2013 David Caldwell.
//
// This library is free software; you can redistribute it and/or modify
// it under the same terms as Perl itself, either Perl version 5.12.4 or,
// at your option, any later version of Perl 5 you may have available.
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include "ppport.h"
#include "sass_interface.h"
#define hv_fetch_key(hv, key, lval) hv_fetch((hv), (key), sizeof(key)-1, (lval))
#define hv_store_key(hv, key, sv, hash) hv_store((hv), (key), sizeof(key)-1, (sv), (hash))
#define Constant(c) newCONSTSUB(stash, #c, newSViv(c))
char *safe_svpv(SV *sv, char *_default)
{
size_t length;
char *str = SvPV(sv, length);
if (memchr(str, 0, length+1)) // NULL Terminated?
return str;
return _default;
}
static inline IV sviv(SV *sv) { return SvIV(sv); } // un-macro-ized version of SvIV
static inline NV svnv(SV *sv) { return SvNV(sv); } // un-macro-ized version of SvNV
SV *sv_from_sass_value(union Sass_Value val)
{
AV *perl = newAV();
av_push(perl, newSViv(val.unknown.tag));
switch(val.unknown.tag) {
case SASS_BOOLEAN:
av_push(perl, newSViv(val.boolean.value));
break;
case SASS_NUMBER:
av_push(perl, newSViv(val.number.value));
av_push(perl, newSVpv(val.number.unit, 0));
break;
case SASS_COLOR:
av_push(perl, newSVnv(val.color.r));
av_push(perl, newSVnv(val.color.g));
av_push(perl, newSVnv(val.color.b));
av_push(perl, newSVnv(val.color.a));
break;
case SASS_STRING:
av_push(perl, newSVpv(val.string.value, 0));
break;
case SASS_LIST: {
int i;
for (i=0; i<val.list.length; i++)
av_push(perl, sv_from_sass_value(val.list.values[i]));
} break;
case SASS_ERROR:
av_push(perl, newSVpv(val.error.message, 0));
break;
default:
av_push(perl, newSVpvf("BUG: This Sass_Value is not handled yet (tag=%d).",val.unknown.tag));
break;
}
return newRV_noinc((SV*) perl);
}
union Sass_Value make_sass_error_f(char *format,...)
{
va_list ap;
va_start(ap, format);
char msg[100];
vsnprintf(msg, sizeof(msg), format, ap);
va_end(ap);
union Sass_Value err = make_sass_error(msg ? msg : format);
return err;
}
union Sass_Value sass_value_from_sv(SV *sv)
{
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVAV)
return make_sass_error_f("perl type must be an arrayref (SvTYPE=%u)", (unsigned)SvTYPE(SvRV(sv)));
AV *av = (AV*)SvRV(sv);
switch (sviv(*av_fetch(av, 0, false))) {
case SASS_BOOLEAN: return make_sass_boolean(sviv(*av_fetch(av, 1, false)));
case SASS_NUMBER: return make_sass_number(svnv(*av_fetch(av, 1, false)),
safe_svpv(*av_fetch(av, 2, false), ""));
case SASS_COLOR: return make_sass_color(svnv(*av_fetch(av, 1, false)),
svnv(*av_fetch(av, 2, false)),
svnv(*av_fetch(av, 3, false)),
svnv(*av_fetch(av, 4, false)));
case SASS_STRING: return make_sass_string(safe_svpv(*av_fetch(av, 1, false), ""));
case SASS_ERROR: return make_sass_error(safe_svpv(*av_fetch(av, 1, false), ""));
case SASS_LIST: {
enum Sass_Separator sep = sviv(*av_fetch(av, 1, false));
union Sass_Value list = make_sass_list(av_len(av)+1-2, sep);
int i;
for (i=0; i<list.list.length; i++)
list.list.values[i] = sass_value_from_sv(*av_fetch(av, i+2, false));
return list;
}
}
return make_sass_error_f("Unknown sass_type (tag=%u)", (unsigned)sviv(*av_fetch(av, 0, false)));
}
union Sass_Value sass_function_callback(union Sass_Value s_args, void *cookie)
{
dSP;
SV *perl_callback = cookie;
int i;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(perl_callback);
for (i=0; i<s_args.list.length; i++)
XPUSHs(sv_2mortal(sv_from_sass_value(s_args.list.values[i])));
PUTBACK;
int count = call_pv("CSS::Sass::sass_function_callback", G_SCALAR);
SPAGAIN;
SV *ret_sv = NULL;
if (count == 1)
ret_sv = POPs;
PUTBACK;
union Sass_Value ret_val = count == 1 ? sass_value_from_sv(ret_sv)
: make_sass_error_f("%s:%d %s: Oh no, a bug! count=%d\n", __FILE__, __LINE__, __func__, count); // should never happen!
FREETMPS;
LEAVE;
return ret_val;
}
MODULE = CSS::Sass PACKAGE = CSS::Sass
BOOT:
{
HV *stash = gv_stashpv("CSS::Sass", 0);
Constant(SASS_STYLE_NESTED);
//Constant(stash, SASS_STYLE_EXPANDED); // not implemented in libsass yet
//Constant(stash, SASS_STYLE_COMPACT); // not implemented in libsass yet
Constant(SASS_STYLE_COMPRESSED);
Constant(SASS_SOURCE_COMMENTS_DEFAULT);
Constant(SASS_SOURCE_COMMENTS_MAP);
Constant(SASS_BOOLEAN);
Constant(SASS_NUMBER);
Constant(SASS_COLOR);
Constant(SASS_STRING);
Constant(SASS_LIST);
Constant(SASS_ERROR);
Constant(SASS_COMMA);
Constant(SASS_SPACE);
// sass2scss constants
Constant(SASS2SCSS_PRETTIFY_0);
Constant(SASS2SCSS_PRETTIFY_1);
Constant(SASS2SCSS_PRETTIFY_2);
Constant(SASS2SCSS_PRETTIFY_3);
// more options for sass2scss
Constant(SASS2SCSS_KEEP_COMMENT);
Constant(SASS2SCSS_STRIP_COMMENT);
Constant(SASS2SCSS_CONVERT_COMMENT);
}
HV*
compile_sass(input_string, options)
char *input_string
HV *options
CODE:
RETVAL = newHV();
sv_2mortal((SV*)RETVAL);
{
struct sass_context *ctx = sass_new_context();
char error[100] = "";
ctx->source_string = input_string;
SV **input_path_sv = hv_fetch_key(options, "input_path", false);
SV **output_path_sv = hv_fetch_key(options, "output_path", false);
SV **output_style_sv = hv_fetch_key(options, "output_style", false);
SV **source_comments_sv = hv_fetch_key(options, "source_comments", false);
SV **include_paths_sv = hv_fetch_key(options, "include_paths", false);
SV **precision_sv = hv_fetch_key(options, "precision", false);
SV **image_path_sv = hv_fetch_key(options, "image_path", false);
SV **source_map_file_sv = hv_fetch_key(options, "source_map_file", false);
SV **omit_source_map_url_sv = hv_fetch_key(options, "omit_source_map_url", false);
SV **sass_functions_sv = hv_fetch_key(options, "sass_functions", false);
if (input_path_sv)
ctx->input_path = safe_svpv(*input_path_sv, "");
if (output_path_sv)
ctx->output_path = safe_svpv(*output_path_sv, "");
if (output_style_sv)
ctx->options.output_style = SvUV(*output_style_sv);
if (source_comments_sv)
ctx->options.source_comments = SvUV(*source_comments_sv);
if (include_paths_sv)
ctx->options.include_paths = safe_svpv(*include_paths_sv, "");
if (precision_sv)
ctx->options.precision = SvUV(*precision_sv);
if (image_path_sv)
ctx->options.image_path = safe_svpv(*image_path_sv, "");
if (source_map_file_sv)
ctx->source_map_file = safe_svpv(*source_map_file_sv, "");
if (omit_source_map_url_sv)
ctx->omit_source_map_url = safe_svpv(*omit_source_map_url_sv, "");
if (sass_functions_sv) {
int i;
AV* sass_functions_av;
if (!SvROK(*sass_functions_sv) || SvTYPE(SvRV(*sass_functions_sv)) != SVt_PVAV) {
snprintf(error, sizeof(error), "sass_functions should be an arrayref (SvTYPE=%u)", (unsigned)SvTYPE(SvRV(*sass_functions_sv)));
goto fail;
}
sass_functions_av = (AV*)SvRV(*sass_functions_sv);
ctx->c_functions = calloc(sizeof(struct Sass_C_Function_Descriptor), av_len(sass_functions_av) + 1/*av_len() is $#av*/ + 1/*null terminated array*/);
if (!ctx->c_functions) {
snprintf(error, sizeof(error), "couldn't alloc memory for c_functions");
goto fail;
}
for (i=0; i<=av_len(sass_functions_av); i++) {
SV** entry_sv = av_fetch(sass_functions_av, i, false);
AV* entry_av;
if (!SvROK(*entry_sv) || SvTYPE(SvRV(*entry_sv)) != SVt_PVAV) {
snprintf(error, sizeof(error), "each sass_function entry should be an arrayref (SvTYPE=%u)", (unsigned)SvTYPE(SvRV(*entry_sv)));
goto fail;
}
entry_av = (AV*)SvRV(*entry_sv);
SV **sig_sv = av_fetch(entry_av, 0, false);
SV **sub_sv = av_fetch(entry_av, 1, false);
ctx->c_functions[i].signature = safe_svpv(*sig_sv, "");
ctx->c_functions[i].function = sass_function_callback;
ctx->c_functions[i].cookie = *sub_sv;
}
}
sass_compile(ctx); // Always returns zero. What's the point??
fail:
hv_store_key(RETVAL, "error_status", newSViv(ctx->error_status || !!*error), 0);
hv_store_key(RETVAL, "output_string", ctx->output_string ? newSVpv(ctx->output_string, 0) : newSV(0), 0);
hv_store_key(RETVAL, "source_map_string", ctx->source_map_string ? newSVpv(ctx->source_map_string, 0) : newSV(0), 0);
hv_store_key(RETVAL, "error_message", *error ? newSVpv(error, 0) :
ctx->error_message ? newSVpv(ctx->error_message, 0) : newSV(0), 0);
sass_free_context(ctx);
}
OUTPUT:
RETVAL
HV*
compile_sass_file(input_path, options)
char *input_path
HV *options
CODE:
RETVAL = newHV();
sv_2mortal((SV*)RETVAL);
{
struct sass_file_context *ctx = sass_new_file_context();
char error[100] = "";
ctx->input_path = input_path;
SV **output_path_sv = hv_fetch_key(options, "output_path", false);
SV **output_style_sv = hv_fetch_key(options, "output_style", false);
SV **source_comments_sv = hv_fetch_key(options, "source_comments", false);
SV **include_paths_sv = hv_fetch_key(options, "include_paths", false);
SV **precision_sv = hv_fetch_key(options, "precision", false);
SV **image_path_sv = hv_fetch_key(options, "image_path", false);
SV **source_map_file_sv = hv_fetch_key(options, "source_map_file", false);
SV **omit_source_map_url_sv = hv_fetch_key(options, "omit_source_map_url", false);
SV **sass_functions_sv = hv_fetch_key(options, "sass_functions", false);
if (output_path_sv)
ctx->output_path = safe_svpv(*output_path_sv, "");
if (output_style_sv)
ctx->options.output_style = SvUV(*output_style_sv);
if (source_comments_sv)
ctx->options.source_comments = SvUV(*source_comments_sv);
if (include_paths_sv)
ctx->options.include_paths = safe_svpv(*include_paths_sv, "");
if (precision_sv)
ctx->options.precision = SvUV(*precision_sv);
if (image_path_sv)
ctx->options.image_path = safe_svpv(*image_path_sv, "");
if (source_map_file_sv)
ctx->source_map_file = safe_svpv(*source_map_file_sv, "");
if (omit_source_map_url_sv)
ctx->omit_source_map_url = safe_svpv(*omit_source_map_url_sv, "");
if (sass_functions_sv) {
int i;
AV* sass_functions_av;
if (!SvROK(*sass_functions_sv) || SvTYPE(SvRV(*sass_functions_sv)) != SVt_PVAV) {
snprintf(error, sizeof(error), "sass_functions should be an arrayref (SvTYPE=%u)", (unsigned)SvTYPE(SvRV(*sass_functions_sv)));
goto fail;
}
sass_functions_av = (AV*)SvRV(*sass_functions_sv);
ctx->c_functions = calloc(sizeof(struct Sass_C_Function_Descriptor), av_len(sass_functions_av) + 1/*av_len() is $#av*/ + 1/*null terminated array*/);
if (!ctx->c_functions) {
snprintf(error, sizeof(error), "couldn't alloc memory for c_functions");
goto fail;
}
for (i=0; i<=av_len(sass_functions_av); i++) {
SV** entry_sv = av_fetch(sass_functions_av, i, false);
AV* entry_av;
if (!SvROK(*entry_sv) || SvTYPE(SvRV(*entry_sv)) != SVt_PVAV) {
snprintf(error, sizeof(error), "each sass_function entry should be an arrayref (SvTYPE=%u)", (unsigned)SvTYPE(SvRV(*entry_sv)));
goto fail;
}
entry_av = (AV*)SvRV(*entry_sv);
SV **sig_sv = av_fetch(entry_av, 0, false);
SV **sub_sv = av_fetch(entry_av, 1, false);
ctx->c_functions[i].signature = safe_svpv(*sig_sv, "");
ctx->c_functions[i].function = sass_function_callback;
ctx->c_functions[i].cookie = *sub_sv;
}
}
sass_compile_file(ctx); // Always returns zero. What's the point??
fail:
hv_store_key(RETVAL, "error_status", newSViv(ctx->error_status || !!*error), 0);
hv_store_key(RETVAL, "output_string", ctx->output_string ? newSVpv(ctx->output_string, 0) : newSV(0), 0);
hv_store_key(RETVAL, "source_map_string", ctx->source_map_string ? newSVpv(ctx->source_map_string, 0) : newSV(0), 0);
hv_store_key(RETVAL, "error_message", *error ? newSVpv(error, 0) :
ctx->error_message ? newSVpv(ctx->error_message, 0) : newSV(0), 0);
sass_free_file_context(ctx);
}
OUTPUT:
RETVAL
const char*
sass2scss(sass, options = SASS2SCSS_PRETTIFY_1)
char *sass
int options
CODE:
sv_2mortal((SV*)RETVAL);
{
RETVAL = sass2scss(sass, options);
// seems to be removed automatically
// if enabled I get "double free" error
// safefree (sass);
}
OUTPUT:
RETVAL