Skip to content

Commit bb4777c

Browse files
committed
Fix bug in C-API with context compiler status
Fixes #1035
1 parent fd7f20f commit bb4777c

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

sass_context.cpp

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ extern "C" {
342342
}
343343

344344
// generic compilation function (not exported, use file/data compile instead)
345-
static Context* sass_prepare_context (Sass_Context* c_ctx, Context::Data cpp_opt) throw()
345+
static Sass_Compiler* sass_prepare_context (Sass_Context* c_ctx, Context::Data cpp_opt) throw()
346346
{
347347
try {
348348

@@ -452,8 +452,17 @@ extern "C" {
452452
c_ctx->error_line = string::npos;
453453
c_ctx->error_column = string::npos;
454454

455+
// allocate a new compiler instance
456+
Sass_Compiler* compiler = (struct Sass_Compiler*) calloc(1, sizeof(struct Sass_Compiler));
457+
compiler->state = SASS_COMPILER_CREATED;
458+
459+
// store in sass compiler
460+
compiler->c_ctx = c_ctx;
461+
compiler->cpp_ctx = cpp_ctx;
462+
cpp_ctx->c_compiler = compiler;
463+
455464
// use to parse block
456-
return cpp_ctx;
465+
return compiler;
457466

458467
}
459468
// pass errors to generic error handler
@@ -464,8 +473,18 @@ extern "C" {
464473

465474
}
466475

467-
static Block* sass_parse_block (Sass_Context* c_ctx, Context* cpp_ctx) throw()
476+
static Block* sass_parse_block (Sass_Compiler* compiler) throw()
468477
{
478+
479+
// assert valid pointer
480+
if (compiler == 0) return 0;
481+
// The cpp context must be set by now
482+
Context* cpp_ctx = compiler->cpp_ctx;
483+
Sass_Context* c_ctx = compiler->c_ctx;
484+
// We will take care to wire up the rest
485+
compiler->cpp_ctx->c_compiler = compiler;
486+
compiler->state = SASS_COMPILER_PARSED;
487+
469488
try {
470489

471490
// get input/output path from options
@@ -509,29 +528,18 @@ extern "C" {
509528
static int sass_compile_context (Sass_Context* c_ctx, Context::Data cpp_opt)
510529
{
511530

512-
// first prepare the c++ context
513-
Context* cpp_ctx = sass_prepare_context(c_ctx, cpp_opt);
514-
515-
// parse given context and return root block
516-
Block* root = cpp_ctx ? sass_parse_block(c_ctx, cpp_ctx) : 0;
517-
518-
if (cpp_ctx && root) {
519-
520-
try {
521-
522-
// now compile the parsed root block
523-
c_ctx->output_string = cpp_ctx->compile_block(root);
524-
525-
// generate source map json and store on context
526-
c_ctx->source_map_string = cpp_ctx->generate_source_map();
527-
528-
}
529-
// pass errors to generic error handler
530-
catch (...) { handle_errors(c_ctx); }
531+
// prepare sass compiler with context and options
532+
Sass_Compiler* compiler = sass_prepare_context(c_ctx, cpp_opt);
531533

534+
try {
535+
// call each compiler step
536+
sass_compiler_parse(compiler);
537+
sass_compiler_execute(compiler);
532538
}
539+
// pass errors to generic error handler
540+
catch (...) { handle_errors(c_ctx); }
533541

534-
delete cpp_ctx;
542+
sass_delete_compiler(compiler);
535543

536544
return c_ctx->error_status;
537545
}
@@ -586,29 +594,17 @@ extern "C" {
586594
struct Sass_Compiler* ADDCALL sass_make_file_compiler (struct Sass_File_Context* c_ctx)
587595
{
588596
if (c_ctx == 0) return 0;
589-
struct Sass_Compiler* compiler = (struct Sass_Compiler*) calloc(1, sizeof(struct Sass_Compiler));
590-
if (compiler == 0) { cerr << "Error allocating memory for file compiler" << endl; return 0; }
591-
compiler->state = SASS_COMPILER_CREATED;
592-
compiler->c_ctx = c_ctx;
593597
Context::Data cpp_opt = Context::Data();
594598
cpp_opt.entry_point(c_ctx->input_path);
595-
compiler->cpp_ctx = sass_prepare_context(c_ctx, cpp_opt);
596-
compiler->cpp_ctx->c_compiler = compiler;
597-
return compiler;
599+
return sass_prepare_context(c_ctx, cpp_opt);
598600
}
599601

600602
struct Sass_Compiler* ADDCALL sass_make_data_compiler (struct Sass_Data_Context* c_ctx)
601603
{
602604
if (c_ctx == 0) return 0;
603-
struct Sass_Compiler* compiler = (struct Sass_Compiler*) calloc(1, sizeof(struct Sass_Compiler));
604-
if (compiler == 0) { cerr << "Error allocating memory for data compiler" << endl; return 0; }
605-
compiler->state = SASS_COMPILER_CREATED;
606-
compiler->c_ctx = c_ctx;
607605
Context::Data cpp_opt = Context::Data();
608606
cpp_opt.source_c_str(c_ctx->source_string);
609-
compiler->cpp_ctx = sass_prepare_context(c_ctx, cpp_opt);
610-
compiler->cpp_ctx->c_compiler = compiler;
611-
return compiler;
607+
return sass_prepare_context(c_ctx, cpp_opt);
612608
}
613609

614610
int ADDCALL sass_compile_data_context(Sass_Data_Context* data_ctx)
@@ -652,10 +648,8 @@ extern "C" {
652648
if (compiler->cpp_ctx == NULL) return 1;
653649
if (compiler->c_ctx->error_status)
654650
return compiler->c_ctx->error_status;
655-
compiler->state = SASS_COMPILER_PARSED;
656-
Context* cpp_ctx = (Context*) compiler->cpp_ctx;
657651
// parse the context we have set up (file or data)
658-
compiler->root = sass_parse_block(compiler->c_ctx, cpp_ctx);
652+
compiler->root = sass_parse_block(compiler);
659653
// success
660654
return 0;
661655
}

0 commit comments

Comments
 (0)