Commit dcf76df8 by Dean Mao

Merge pull request #202 from deanmao/master

mostly a refactor to clean up lots of redundant methods, and to make things a little bit more DRY.
parents 615097b4 33b8ab53
......@@ -11,96 +11,114 @@ using namespace std;
void WorkOnContext(uv_work_t* req) {
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
sass_compile(ctx);
}
void MakeOldCallback(uv_work_t* req) {
NanScope();
TryCatch try_catch;
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
if (ctx->error_status == 0) {
// if no error, do callback(null, result)
const unsigned argc = 2;
Local<Value> argv[argc] = {
NanNewLocal(Null()),
NanNewLocal(String::New(ctx->output_string))
};
ctx_w->callback->Call(argc, argv);
} else {
// if error, do callback(error)
const unsigned argc = 1;
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->error_message))
};
ctx_w->callback->Call(argc, argv);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
if (ctx_w->ctx) {
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
sass_compile(ctx);
} else if (ctx_w->fctx) {
sass_file_context* ctx = static_cast<sass_file_context*>(ctx_w->fctx);
sass_compile_file(ctx);
}
delete ctx->source_string;
sass_free_context_wrapper(ctx_w);
}
NAN_METHOD(OldRender) {
NanScope();
sass_context* ctx = sass_new_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
void extractOptions(_NAN_METHOD_ARGS, void* cptr, sass_context_wrapper* ctx_w, bool isFile) {
char *source;
char* pathOrData;
int output_style;
int source_comments;
String::AsciiValue astr(args[0]);
Local<Function> callback = Local<Function>::Cast(args[1]);
String::AsciiValue bstr(args[2]);
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.image_path = new char[0];
ctx->options.include_paths = *bstr;
// ctx->options.output_style = SASS_STYLE_NESTED;
ctx->options.output_style = args[3]->Int32Value();
ctx->options.source_comments = args[4]->Int32Value();
ctx_w->ctx = ctx;
ctx_w->callback = new NanCallback(callback);
ctx_w->request.data = ctx_w;
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeOldCallback);
assert(status == 0);
if (ctx_w) {
// async (callback) style
Local<Function> callback = Local<Function>::Cast(args[1]);
Local<Function> errorCallback = Local<Function>::Cast(args[2]);
if (isFile) {
ctx_w->fctx = (sass_file_context*) cptr;
} else {
ctx_w->ctx = (sass_context*) cptr;
}
ctx_w->request.data = ctx_w;
ctx_w->callback = new NanCallback(callback);
ctx_w->errorCallback = new NanCallback(errorCallback);
output_style = args[4]->Int32Value();
source_comments = args[5]->Int32Value();
String::AsciiValue bstr(args[3]);
pathOrData = new char[strlen(*bstr)+1];
strcpy(pathOrData, *bstr);
} else {
// synchronous style
output_style = args[2]->Int32Value();
source_comments = args[3]->Int32Value();
String::AsciiValue bstr(args[1]);
pathOrData = new char[strlen(*bstr)+1];
strcpy(pathOrData, *bstr);
}
NanReturnUndefined();
if (isFile) {
sass_file_context *ctx = (sass_file_context*)cptr;
char *filename = new char[strlen(*astr)+1];
strcpy(filename, *astr);
ctx->input_path = filename;
ctx->options.image_path = new char[0];
ctx->options.output_style = output_style;
ctx->options.source_comments = source_comments;
ctx->options.include_paths = pathOrData;
if (source_comments == SASS_SOURCE_COMMENTS_MAP) {
String::AsciiValue cstr(args[6]);
ctx->source_map_file = new char[strlen(*cstr)+1];
strcpy(ctx->source_map_file, *cstr);
}
} else {
sass_context *ctx = (sass_context*)cptr;
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.image_path = new char[0];
ctx->options.output_style = output_style;
ctx->options.source_comments = source_comments;
ctx->options.include_paths = pathOrData;
}
}
void MakeCallback(uv_work_t* req) {
NanScope();
TryCatch try_catch;
sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
sass_context* ctx = static_cast<sass_context*>(ctx_w->ctx);
Handle<Value> val, err;
const unsigned argc = 2;
int error_status = ctx_w->ctx ? ctx_w->ctx->error_status : ctx_w->fctx->error_status;
if (ctx->error_status == 0) {
if (error_status == 0) {
// if no error, do callback(null, result)
const unsigned argc = 1;
Handle<Value> source_map;
if (ctx_w->fctx && ctx_w->fctx->options.source_comments == SASS_SOURCE_COMMENTS_MAP) {
source_map = String::New(ctx_w->fctx->source_map_string);
} else {
source_map = Null();
}
val = ctx_w->ctx ? NanNewLocal(String::New(ctx_w->ctx->output_string)) : NanNewLocal(String::New(ctx_w->fctx->output_string));
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->output_string))
NanNewLocal(val),
NanNewLocal(source_map),
};
ctx_w->callback->Call(argc, argv);
} else {
// if error, do callback(error)
const unsigned argc = 1;
err = ctx_w->ctx ? NanNewLocal(String::New(ctx_w->ctx->error_message)) : NanNewLocal(String::New(ctx_w->fctx->error_message));
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->error_message))
NanNewLocal(err),
NanNewLocal(Integer::New(error_status))
};
ctx_w->errorCallback->Call(argc, argv);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
delete ctx->source_string;
if (ctx_w->ctx) {
delete ctx_w->ctx->source_string;
} else {
delete ctx_w->fctx->input_path;
}
sass_free_context_wrapper(ctx_w);
}
......@@ -108,25 +126,8 @@ NAN_METHOD(Render) {
NanScope();
sass_context* ctx = sass_new_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
char *source;
String::AsciiValue astr(args[0]);
Local<Function> callback = Local<Function>::Cast(args[1]);
Local<Function> errorCallback = Local<Function>::Cast(args[2]);
String::AsciiValue bstr(args[3]);
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
// ctx->options.output_style = SASS_STYLE_NESTED;
ctx->options.image_path = new char[0];
ctx->options.output_style = args[4]->Int32Value();
ctx->options.source_comments = args[5]->Int32Value();
ctx_w->ctx = ctx;
ctx_w->callback = new NanCallback(callback);
ctx_w->errorCallback = new NanCallback(errorCallback);
ctx_w->request.data = ctx_w;
extractOptions(args, ctx, ctx_w, false);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeCallback);
assert(status == 0);
......@@ -137,28 +138,14 @@ NAN_METHOD(Render) {
NAN_METHOD(RenderSync) {
NanScope();
sass_context* ctx = sass_new_context();
char *source;
String::AsciiValue astr(args[0]);
String::AsciiValue bstr(args[1]);
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
ctx->options.output_style = args[2]->Int32Value();
ctx->options.image_path = new char[0];
ctx->options.source_comments = args[3]->Int32Value();
extractOptions(args, ctx, NULL, false);
sass_compile(ctx);
source = NULL;
delete ctx->source_string;
ctx->source_string = NULL;
delete ctx->options.include_paths;
ctx->options.include_paths = NULL;
delete ctx->options.image_path;
ctx->options.image_path = NULL;
if (ctx->error_status == 0) {
Local<Value> output = NanNewLocal(String::New(ctx->output_string));
......@@ -173,84 +160,14 @@ NAN_METHOD(RenderSync) {
NanReturnUndefined();
}
/**
Rendering Files
**/
void WorkOnFileContext(uv_work_t* req) {
sass_file_context_wrapper* ctx_w = static_cast<sass_file_context_wrapper*>(req->data);
sass_file_context* ctx = static_cast<sass_file_context*>(ctx_w->ctx);
sass_compile_file(ctx);
}
void MakeFileCallback(uv_work_t* req) {
NanScope();
TryCatch try_catch;
sass_file_context_wrapper* ctx_w = static_cast<sass_file_context_wrapper*>(req->data);
sass_file_context* ctx = static_cast<sass_file_context*>(ctx_w->ctx);
if (ctx->error_status == 0) {
// if no error, do callback(null, result)
Handle<Value> source_map;
if (ctx->options.source_comments == SASS_SOURCE_COMMENTS_MAP) {
source_map = String::New(ctx->source_map_string);
} else {
source_map = Null();
}
const unsigned argc = 2;
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->output_string)),
NanNewLocal(source_map)
};
ctx_w->callback->Call(argc, argv);
} else {
// if error, do callback(error)
const unsigned argc = 1;
Local<Value> argv[argc] = {
NanNewLocal(String::New(ctx->error_message))
};
ctx_w->errorCallback->Call(argc, argv);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
delete ctx->input_path;
sass_free_file_context_wrapper(ctx_w);
}
NAN_METHOD(RenderFile) {
NanScope();
sass_file_context* ctx = sass_new_file_context();
sass_file_context_wrapper* ctx_w = sass_new_file_context_wrapper();
char *filename;
String::AsciiValue astr(args[0]);
Local<Function> callback = Local<Function>::Cast(args[1]);
Local<Function> errorCallback = Local<Function>::Cast(args[2]);
String::AsciiValue bstr(args[3]);
String::AsciiValue cstr(args[6]);
filename = new char[strlen(*astr)+1];
strcpy(filename, *astr);
ctx->input_path = filename;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
// ctx->options.output_style = SASS_STYLE_NESTED;
ctx->options.output_style = args[4]->Int32Value();
ctx->options.image_path = new char[0];
ctx->options.source_comments = args[5]->Int32Value();
if (ctx->options.source_comments == SASS_SOURCE_COMMENTS_MAP) {
ctx->source_map_file = new char[strlen(*cstr)+1];
strcpy(ctx->source_map_file, *cstr);
}
ctx_w->ctx = ctx;
ctx_w->callback = new NanCallback(callback);
ctx_w->errorCallback = new NanCallback(errorCallback);
ctx_w->request.data = ctx_w;
sass_file_context* fctx = sass_new_file_context();
sass_context_wrapper* ctx_w = sass_new_context_wrapper();
ctx_w->fctx = fctx;
extractOptions(args, fctx, ctx_w, true);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnFileContext, (uv_after_work_cb)MakeFileCallback);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, (uv_after_work_cb)MakeCallback);
assert(status == 0);
NanReturnUndefined();
......@@ -259,28 +176,14 @@ NAN_METHOD(RenderFile) {
NAN_METHOD(RenderFileSync) {
NanScope();
sass_file_context* ctx = sass_new_file_context();
char *filename;
String::AsciiValue astr(args[0]);
String::AsciiValue bstr(args[1]);
filename = new char[strlen(*astr)+1];
strcpy(filename, *astr);
ctx->input_path = filename;
ctx->options.include_paths = new char[strlen(*bstr)+1];
ctx->options.include_paths = *bstr;
ctx->options.image_path = new char[0];
ctx->options.output_style = args[2]->Int32Value();
ctx->options.source_comments = args[3]->Int32Value();
extractOptions(args, ctx, NULL, true);
sass_compile_file(ctx);
filename = NULL;
delete ctx->input_path;
ctx->input_path = NULL;
delete ctx->options.include_paths;
ctx->options.include_paths = NULL;
delete ctx->options.image_path;
ctx->options.image_path = NULL;
if (ctx->error_status == 0) {
Local<Value> output = NanNewLocal(String::New(ctx->output_string));
......@@ -296,7 +199,6 @@ NAN_METHOD(RenderFileSync) {
}
void RegisterModule(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "oldRender", OldRender);
NODE_SET_METHOD(target, "render", Render);
NODE_SET_METHOD(target, "renderSync", RenderSync);
NODE_SET_METHOD(target, "renderFile", RenderFile);
......
......@@ -34,11 +34,11 @@ var SASS_SOURCE_COMMENTS = {
};
var prepareOptions = function(options) {
var paths, style;
var options = typeof options !== 'object' ? {} : options;
var paths = options.include_paths || options.includePaths || [];
var style = SASS_OUTPUT_STYLE[options.output_style || options.outputStyle] || 0;
var comments = SASS_SOURCE_COMMENTS[options.source_comments || options.sourceComments] || 0;
var paths, style, comments;
options = typeof options !== 'object' ? {} : options;
paths = options.include_paths || options.includePaths || [];
style = SASS_OUTPUT_STYLE[options.output_style || options.outputStyle] || 0;
comments = SASS_SOURCE_COMMENTS[options.source_comments || options.sourceComments] || 0;
return {
paths: paths,
......@@ -49,7 +49,13 @@ var prepareOptions = function(options) {
var deprecatedRender = function(css, callback, options) {
options = prepareOptions(options);
return binding.oldRender(css, callback, options.paths.join(':'), options.style, options.comments);
var errCallback = function(err) {
callback(err);
};
var oldCallback = function(css, err) {
callback(null, css);
};
return binding.render(css, oldCallback, errCallback, options.paths.join(':'), options.style, options.comments);
};
var deprecatedRenderSync = function(css, options) {
......@@ -68,10 +74,11 @@ exports.render = function(options) {
options.error = options.error || function(){};
if (options.file !== undefined && options.file !== null) {
return binding.renderFile(options.file, options.success, options.error, newOptions.paths.join(path.delimiter), newOptions.style, newOptions.comments, options.sourceMap);
return binding.renderFile(options.file, options.success, options.error, newOptions.paths.join(':'), newOptions.style, newOptions.comments, options.sourceMap);
}
//Assume data is present if file is not. binding/libsass will tell the user otherwise!
return binding.render(options.data, options.success, options.error, newOptions.paths.join(path.delimiter), newOptions.style);
return binding.render(options.data, options.success, options.error, newOptions.paths.join(":"), newOptions.style);
};
exports.renderSync = function(options) {
......@@ -84,11 +91,11 @@ exports.renderSync = function(options) {
newOptions = prepareOptions(options);
if (options.file !== undefined && options.file !== null) {
return binding.renderFileSync(options.file, newOptions.paths.join(path.delimiter), newOptions.style, newOptions.comments);
return binding.renderFileSync(options.file, newOptions.paths.join(':'), newOptions.style, newOptions.comments);
}
//Assume data is present if file is not. binding/libsass will tell the user otherwise!
return binding.renderSync(options.data, newOptions.paths.join(path.delimiter), newOptions.style);
return binding.renderSync(options.data, newOptions.paths.join(":"), newOptions.style);
};
exports.middleware = require('./lib/middleware');
......@@ -12,21 +12,7 @@ extern "C" {
void sass_free_context_wrapper(sass_context_wrapper* ctx_w)
{
if (ctx_w->ctx) sass_free_context(ctx_w->ctx);
delete ctx_w->callback;
delete ctx_w->errorCallback;
free(ctx_w);
}
sass_file_context_wrapper* sass_new_file_context_wrapper()
{
return (sass_file_context_wrapper*) calloc(1, sizeof(sass_file_context_wrapper));
}
void sass_free_file_context_wrapper(sass_file_context_wrapper* ctx_w)
{
if (ctx_w->ctx) sass_free_file_context(ctx_w->ctx);
if (ctx_w->fctx) sass_free_file_context(ctx_w->fctx);
delete ctx_w->callback;
delete ctx_w->errorCallback;
......
......@@ -7,6 +7,7 @@ extern "C" {
struct sass_context_wrapper {
sass_context* ctx;
sass_file_context* fctx;
uv_work_t request;
NanCallback* callback;
NanCallback* errorCallback;
......@@ -15,16 +16,6 @@ struct sass_context_wrapper {
struct sass_context_wrapper* sass_new_context_wrapper(void);
void sass_free_context_wrapper(struct sass_context_wrapper* ctx);
struct sass_file_context_wrapper {
sass_file_context* ctx;
uv_work_t request;
NanCallback* callback;
NanCallback* errorCallback;
};
struct sass_file_context_wrapper* sass_new_file_context_wrapper(void);
void sass_free_file_context_wrapper(struct sass_file_context_wrapper* ctx);
#ifdef __cplusplus
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment