Commit eae642c9 by Enric Lluelles

Wrap ctx so we don't mangle with libsass

Use a wrapper to store the callback info and the context, this way we
can use the unmodified libsass
parent 5ae945ac
......@@ -4,20 +4,23 @@
#include <cstring>
#include <iostream>
#include <cstdlib>
#include "libsass/sass_interface.h"
#include "sass_context_wrapper.h"
using namespace v8;
using namespace std;
void WorkOnContext(uv_work_t* req) {
sass_context* ctx = static_cast<sass_context*>(req->data);
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 MakeCallback(uv_work_t* req) {
HandleScope scope;
TryCatch try_catch;
sass_context* ctx = static_cast<sass_context*>(req->data);
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)
......@@ -27,7 +30,7 @@ void MakeCallback(uv_work_t* req) {
Local<Value>::New(String::New(ctx->output_string))
};
ctx->callback->Call(Context::GetCurrent()->Global(), argc, argv);
ctx_w->callback->Call(Context::GetCurrent()->Global(), argc, argv);
} else {
// if error, do callback(error)
const unsigned argc = 1;
......@@ -35,31 +38,35 @@ void MakeCallback(uv_work_t* req) {
Local<Value>::New(String::New(ctx->error_message))
};
ctx->callback->Call(Context::GetCurrent()->Global(), argc, argv);
ctx_w->callback->Call(Context::GetCurrent()->Global(), argc, argv);
}
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
sass_free_context(ctx);
sass_free_context_wrapper(ctx_w);
}
Handle<Value> Render(const Arguments& args) {
HandleScope scope;
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]);
String::AsciiValue bstr(args[2]);
ctx->source_string = new char[strlen(*astr)+1];
strcpy(ctx->source_string, *astr);
source = new char[strlen(*astr)+1];
strcpy(source, *astr);
ctx->source_string = source;
ctx->options.include_paths = new char[strlen(*bstr)+1];
strcpy(ctx->options.include_paths, *bstr);
// ctx->options.output_style = SASS_STYLE_NESTED;
ctx->options.output_style = args[3]->Int32Value();
ctx->callback = Persistent<Function>::New(callback);
ctx->request.data = ctx;
ctx_w->ctx = ctx;
ctx_w->callback = Persistent<Function>::New(callback);
ctx_w->request.data = ctx_w;
int status = uv_queue_work(uv_default_loop(), &ctx->request, WorkOnContext, MakeCallback);
int status = uv_queue_work(uv_default_loop(), &ctx_w->request, WorkOnContext, MakeCallback);
assert(status == 0);
return Undefined();
......
......@@ -4,6 +4,7 @@
'target_name': 'binding',
'sources': [
'binding.cpp',
'sass_context_wrapper.cpp',
'libsass/constants.cpp',
'libsass/context.cpp',
'libsass/document.cpp',
......
#include "sass_context_wrapper.h"
#include <cstdlib>
extern "C" {
using namespace std;
sass_context_wrapper* sass_new_context_wrapper()
{
return (sass_context_wrapper*) calloc(1, sizeof(sass_context_wrapper));
}
void sass_free_context_wrapper(sass_context_wrapper* ctx_w)
{
if (ctx_w->ctx) sass_free_context(ctx_w->ctx);
free(ctx_w);
}
}
#include "libsass/sass_interface.h"
#include <node.h>
#ifdef __cplusplus
extern "C" {
#endif
struct sass_context_wrapper {
sass_context* ctx;
uv_work_t request;
v8::Persistent<v8::Function> callback;
};
struct sass_context_wrapper* sass_new_context_wrapper(void);
void sass_free_context_wrapper(struct sass_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