binding.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include <nan.h>
  2. #include <vector>
  3. #include "sass_context_wrapper.h"
  4. #include "custom_function_bridge.h"
  5. #include "create_string.h"
  6. #include "sass_types/factory.h"
  7. Sass_Import_List sass_importer(const char* cur_path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
  8. {
  9. void* cookie = sass_importer_get_cookie(cb);
  10. struct Sass_Import* previous = sass_compiler_get_last_import(comp);
  11. const char* prev_path = sass_import_get_abs_path(previous);
  12. CustomImporterBridge& bridge = *(static_cast<CustomImporterBridge*>(cookie));
  13. std::vector<void*> argv;
  14. argv.push_back((void*)cur_path);
  15. argv.push_back((void*)prev_path);
  16. return bridge(argv);
  17. }
  18. union Sass_Value* sass_custom_function(const union Sass_Value* s_args, Sass_Function_Entry cb, struct Sass_Compiler* comp)
  19. {
  20. void* cookie = sass_function_get_cookie(cb);
  21. CustomFunctionBridge& bridge = *(static_cast<CustomFunctionBridge*>(cookie));
  22. std::vector<void*> argv;
  23. for (unsigned l = sass_list_get_length(s_args), i = 0; i < l; i++) {
  24. argv.push_back((void*)sass_list_get_value(s_args, i));
  25. }
  26. return bridge(argv);
  27. }
  28. int ExtractOptions(v8::Local<v8::Object> options, void* cptr, sass_context_wrapper* ctx_w, bool is_file, bool is_sync) {
  29. Nan::HandleScope scope;
  30. struct Sass_Context* ctx;
  31. v8::Local<v8::Value> result_ = Nan::Get(
  32. options,
  33. Nan::New("result").ToLocalChecked()
  34. ).ToLocalChecked();
  35. if (!result_->IsObject()) {
  36. Nan::ThrowTypeError("\"result\" element is not an object");
  37. return -1;
  38. }
  39. ctx_w->result.Reset(result_.As<v8::Object>());
  40. if (is_file) {
  41. ctx_w->fctx = (struct Sass_File_Context*) cptr;
  42. ctx = sass_file_context_get_context(ctx_w->fctx);
  43. }
  44. else {
  45. ctx_w->dctx = (struct Sass_Data_Context*) cptr;
  46. ctx = sass_data_context_get_context(ctx_w->dctx);
  47. }
  48. struct Sass_Options* sass_options = sass_context_get_options(ctx);
  49. ctx_w->is_sync = is_sync;
  50. if (!is_sync) {
  51. ctx_w->request.data = ctx_w;
  52. // async (callback) style
  53. v8::Local<v8::Function> success_callback = v8::Local<v8::Function>::Cast(Nan::Get(options, Nan::New("success").ToLocalChecked()).ToLocalChecked());
  54. v8::Local<v8::Function> error_callback = v8::Local<v8::Function>::Cast(Nan::Get(options, Nan::New("error").ToLocalChecked()).ToLocalChecked());
  55. ctx_w->success_callback = new Nan::Callback(success_callback);
  56. ctx_w->error_callback = new Nan::Callback(error_callback);
  57. }
  58. if (!is_file) {
  59. ctx_w->file = create_string(Nan::Get(options, Nan::New("file").ToLocalChecked()));
  60. sass_option_set_input_path(sass_options, ctx_w->file);
  61. }
  62. int indent_len = Nan::To<int32_t>(
  63. Nan::Get(
  64. options,
  65. Nan::New("indentWidth").ToLocalChecked()
  66. ).ToLocalChecked()).FromJust();
  67. ctx_w->indent = (char*)malloc(indent_len + 1);
  68. strcpy(ctx_w->indent, std::string(
  69. indent_len,
  70. Nan::To<int32_t>(
  71. Nan::Get(
  72. options,
  73. Nan::New("indentType").ToLocalChecked()
  74. ).ToLocalChecked()).FromJust() == 1 ? '\t' : ' '
  75. ).c_str());
  76. ctx_w->linefeed = create_string(Nan::Get(options, Nan::New("linefeed").ToLocalChecked()));
  77. ctx_w->include_path = create_string(Nan::Get(options, Nan::New("includePaths").ToLocalChecked()));
  78. ctx_w->out_file = create_string(Nan::Get(options, Nan::New("outFile").ToLocalChecked()));
  79. ctx_w->source_map = create_string(Nan::Get(options, Nan::New("sourceMap").ToLocalChecked()));
  80. ctx_w->source_map_root = create_string(Nan::Get(options, Nan::New("sourceMapRoot").ToLocalChecked()));
  81. sass_option_set_output_path(sass_options, ctx_w->out_file);
  82. sass_option_set_output_style(sass_options, (Sass_Output_Style)Nan::To<int32_t>(Nan::Get(options, Nan::New("style").ToLocalChecked()).ToLocalChecked()).FromJust());
  83. sass_option_set_is_indented_syntax_src(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("indentedSyntax").ToLocalChecked()).ToLocalChecked()).FromJust());
  84. sass_option_set_source_comments(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("sourceComments").ToLocalChecked()).ToLocalChecked()).FromJust());
  85. sass_option_set_omit_source_map_url(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("omitSourceMapUrl").ToLocalChecked()).ToLocalChecked()).FromJust());
  86. sass_option_set_source_map_embed(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("sourceMapEmbed").ToLocalChecked()).ToLocalChecked()).FromJust());
  87. sass_option_set_source_map_contents(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("sourceMapContents").ToLocalChecked()).ToLocalChecked()).FromJust());
  88. sass_option_set_source_map_file(sass_options, ctx_w->source_map);
  89. sass_option_set_source_map_root(sass_options, ctx_w->source_map_root);
  90. sass_option_set_include_path(sass_options, ctx_w->include_path);
  91. sass_option_set_precision(sass_options, Nan::To<int32_t>(Nan::Get(options, Nan::New("precision").ToLocalChecked()).ToLocalChecked()).FromJust());
  92. sass_option_set_indent(sass_options, ctx_w->indent);
  93. sass_option_set_linefeed(sass_options, ctx_w->linefeed);
  94. v8::Local<v8::Value> importer_callback = Nan::Get(options, Nan::New("importer").ToLocalChecked()).ToLocalChecked();
  95. if (importer_callback->IsFunction()) {
  96. v8::Local<v8::Function> importer = importer_callback.As<v8::Function>();
  97. CustomImporterBridge *bridge = new CustomImporterBridge(importer, ctx_w->is_sync);
  98. ctx_w->importer_bridges.push_back(bridge);
  99. Sass_Importer_List c_importers = sass_make_importer_list(1);
  100. c_importers[0] = sass_make_importer(sass_importer, 0, bridge);
  101. sass_option_set_c_importers(sass_options, c_importers);
  102. }
  103. else if (importer_callback->IsArray()) {
  104. v8::Local<v8::Array> importers = importer_callback.As<v8::Array>();
  105. Sass_Importer_List c_importers = sass_make_importer_list(importers->Length());
  106. for (size_t i = 0; i < importers->Length(); ++i) {
  107. v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(Nan::Get(importers, static_cast<uint32_t>(i)).ToLocalChecked());
  108. CustomImporterBridge *bridge = new CustomImporterBridge(callback, ctx_w->is_sync);
  109. ctx_w->importer_bridges.push_back(bridge);
  110. c_importers[i] = sass_make_importer(sass_importer, importers->Length() - i - 1, bridge);
  111. }
  112. sass_option_set_c_importers(sass_options, c_importers);
  113. }
  114. v8::Local<v8::Value> custom_functions = Nan::Get(options, Nan::New("functions").ToLocalChecked()).ToLocalChecked();
  115. if (custom_functions->IsObject()) {
  116. v8::Local<v8::Object> functions = custom_functions.As<v8::Object>();
  117. v8::Local<v8::Array> signatures = Nan::GetOwnPropertyNames(functions).ToLocalChecked();
  118. unsigned num_signatures = signatures->Length();
  119. Sass_Function_List fn_list = sass_make_function_list(num_signatures);
  120. for (unsigned i = 0; i < num_signatures; i++) {
  121. v8::Local<v8::String> signature = v8::Local<v8::String>::Cast(Nan::Get(signatures, Nan::New(i)).ToLocalChecked());
  122. v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(Nan::Get(functions, signature).ToLocalChecked());
  123. CustomFunctionBridge *bridge = new CustomFunctionBridge(callback, ctx_w->is_sync);
  124. ctx_w->function_bridges.push_back(bridge);
  125. char* sig = create_string(signature);
  126. Sass_Function_Entry fn = sass_make_function(sig, sass_custom_function, bridge);
  127. free(sig);
  128. sass_function_set_list_entry(fn_list, i, fn);
  129. }
  130. sass_option_set_c_functions(sass_options, fn_list);
  131. }
  132. return 0;
  133. }
  134. void GetStats(sass_context_wrapper* ctx_w, Sass_Context* ctx) {
  135. Nan::HandleScope scope;
  136. char** included_files = sass_context_get_included_files(ctx);
  137. v8::Local<v8::Array> arr = Nan::New<v8::Array>();
  138. if (included_files) {
  139. for (int i = 0; included_files[i] != nullptr; ++i) {
  140. Nan::Set(arr, i, Nan::New<v8::String>(included_files[i]).ToLocalChecked());
  141. }
  142. }
  143. v8::Local<v8::Object> result = Nan::New(ctx_w->result);
  144. assert(result->IsObject());
  145. v8::Local<v8::Value> stats = Nan::Get(
  146. result,
  147. Nan::New("stats").ToLocalChecked()
  148. ).ToLocalChecked();
  149. if (stats->IsObject()) {
  150. Nan::Set(
  151. stats.As<v8::Object>(),
  152. Nan::New("includedFiles").ToLocalChecked(),
  153. arr
  154. );
  155. } else {
  156. Nan::ThrowTypeError("\"result.stats\" element is not an object");
  157. }
  158. }
  159. int GetResult(sass_context_wrapper* ctx_w, Sass_Context* ctx, bool is_sync = false) {
  160. Nan::HandleScope scope;
  161. v8::Local<v8::Object> result;
  162. int status = sass_context_get_error_status(ctx);
  163. result = Nan::New(ctx_w->result);
  164. assert(result->IsObject());
  165. if (status == 0) {
  166. const char* css = sass_context_get_output_string(ctx);
  167. const char* map = sass_context_get_source_map_string(ctx);
  168. Nan::Set(result, Nan::New("css").ToLocalChecked(), Nan::CopyBuffer(css, static_cast<uint32_t>(strlen(css))).ToLocalChecked());
  169. GetStats(ctx_w, ctx);
  170. if (map) {
  171. Nan::Set(result, Nan::New("map").ToLocalChecked(), Nan::CopyBuffer(map, static_cast<uint32_t>(strlen(map))).ToLocalChecked());
  172. }
  173. }
  174. else if (is_sync) {
  175. Nan::Set(result, Nan::New("error").ToLocalChecked(), Nan::New<v8::String>(sass_context_get_error_json(ctx)).ToLocalChecked());
  176. }
  177. return status;
  178. }
  179. void PerformCall(sass_context_wrapper* ctx_w, Nan::Callback* callback, int argc, v8::Local<v8::Value> argv[]) {
  180. if (ctx_w->is_sync) {
  181. Nan::Call(*callback, argc, argv);
  182. } else {
  183. callback->Call(argc, argv, ctx_w->async_resource);
  184. }
  185. }
  186. void MakeCallback(uv_work_t* req) {
  187. Nan::HandleScope scope;
  188. Nan::TryCatch try_catch;
  189. sass_context_wrapper* ctx_w = static_cast<sass_context_wrapper*>(req->data);
  190. struct Sass_Context* ctx;
  191. if (ctx_w->dctx) {
  192. ctx = sass_data_context_get_context(ctx_w->dctx);
  193. }
  194. else {
  195. ctx = sass_file_context_get_context(ctx_w->fctx);
  196. }
  197. int status = GetResult(ctx_w, ctx);
  198. if (status == 0 && ctx_w->success_callback) {
  199. // if no error, do callback(null, result)
  200. PerformCall(ctx_w, ctx_w->success_callback, 0, 0);
  201. }
  202. else if (ctx_w->error_callback) {
  203. // if error, do callback(error)
  204. const char* err = sass_context_get_error_json(ctx);
  205. v8::Local<v8::Value> argv[] = {
  206. Nan::New<v8::String>(err).ToLocalChecked()
  207. };
  208. PerformCall(ctx_w, ctx_w->error_callback, 1, argv);
  209. }
  210. if (try_catch.HasCaught()) {
  211. Nan::FatalException(try_catch);
  212. }
  213. sass_free_context_wrapper(ctx_w);
  214. }
  215. NAN_METHOD(render) {
  216. v8::Local<v8::Object> options = Nan::To<v8::Object>(info[0]).ToLocalChecked();
  217. char* source_string = create_string(Nan::Get(options, Nan::New("data").ToLocalChecked()));
  218. struct Sass_Data_Context* dctx = sass_make_data_context(source_string);
  219. sass_context_wrapper* ctx_w = sass_make_context_wrapper();
  220. ctx_w->async_resource = new Nan::AsyncResource("node-sass:sass_context_wrapper:render");
  221. if (ExtractOptions(options, dctx, ctx_w, false, false) >= 0) {
  222. int status = uv_queue_work(uv_default_loop(), &ctx_w->request, compile_it, (uv_after_work_cb)MakeCallback);
  223. assert(status == 0);
  224. }
  225. }
  226. NAN_METHOD(render_sync) {
  227. v8::Local<v8::Object> options = Nan::To<v8::Object>(info[0]).ToLocalChecked();
  228. char* source_string = create_string(Nan::Get(options, Nan::New("data").ToLocalChecked()));
  229. struct Sass_Data_Context* dctx = sass_make_data_context(source_string);
  230. struct Sass_Context* ctx = sass_data_context_get_context(dctx);
  231. sass_context_wrapper* ctx_w = sass_make_context_wrapper();
  232. int result = -1;
  233. if ((result = ExtractOptions(options, dctx, ctx_w, false, true)) >= 0) {
  234. compile_data(dctx);
  235. result = GetResult(ctx_w, ctx, true);
  236. }
  237. sass_free_context_wrapper(ctx_w);
  238. info.GetReturnValue().Set(result == 0);
  239. }
  240. NAN_METHOD(render_file) {
  241. v8::Local<v8::Object> options = Nan::To<v8::Object>(info[0]).ToLocalChecked();
  242. char* input_path = create_string(Nan::Get(options, Nan::New("file").ToLocalChecked()));
  243. struct Sass_File_Context* fctx = sass_make_file_context(input_path);
  244. sass_context_wrapper* ctx_w = sass_make_context_wrapper();
  245. ctx_w->async_resource = new Nan::AsyncResource("node-sass:sass_context_wrapper:render_file");
  246. if (ExtractOptions(options, fctx, ctx_w, true, false) >= 0) {
  247. int status = uv_queue_work(uv_default_loop(), &ctx_w->request, compile_it, (uv_after_work_cb)MakeCallback);
  248. assert(status == 0);
  249. }
  250. }
  251. NAN_METHOD(render_file_sync) {
  252. v8::Local<v8::Object> options = Nan::To<v8::Object>(info[0]).ToLocalChecked();
  253. char* input_path = create_string(Nan::Get(options, Nan::New("file").ToLocalChecked()));
  254. struct Sass_File_Context* fctx = sass_make_file_context(input_path);
  255. struct Sass_Context* ctx = sass_file_context_get_context(fctx);
  256. sass_context_wrapper* ctx_w = sass_make_context_wrapper();
  257. int result = -1;
  258. if ((result = ExtractOptions(options, fctx, ctx_w, true, true)) >= 0) {
  259. compile_file(fctx);
  260. result = GetResult(ctx_w, ctx, true);
  261. };
  262. free(input_path);
  263. sass_free_context_wrapper(ctx_w);
  264. info.GetReturnValue().Set(result == 0);
  265. }
  266. NAN_METHOD(libsass_version) {
  267. info.GetReturnValue().Set(Nan::New<v8::String>(libsass_version()).ToLocalChecked());
  268. }
  269. NAN_MODULE_INIT(RegisterModule) {
  270. Nan::SetMethod(target, "render", render);
  271. Nan::SetMethod(target, "renderSync", render_sync);
  272. Nan::SetMethod(target, "renderFile", render_file);
  273. Nan::SetMethod(target, "renderFileSync", render_file_sync);
  274. Nan::SetMethod(target, "libsassVersion", libsass_version);
  275. SassTypes::Factory::initExports(target);
  276. }
  277. NODE_MODULE(binding, RegisterModule);