extern "C" Sass_Function_List ADDCALL libsass_load_functions()
{
  // allocate a custom function caller
  Sass_Function_Entry c_func =
    sass_make_function("foo()", custom_function, (void*)42);
  // create list of all custom functions
  Sass_Function_List fn_list = sass_make_function_list(1);
  // put the only function in this plugin to the list
  sass_function_set_list_entry(fn_list, 0, c_func);
  // return the list
  return fn_list;
}
int ExtractOptions(v8::Local<v8::Object> options, void* cptr, sass_context_wrapper* ctx_w, bool is_file, bool is_sync) {
  Nan::HandleScope scope;

  struct Sass_Context* ctx;

  v8::Local<v8::Value> result_ = Nan::Get(
    options,
    Nan::New("result").ToLocalChecked()
  ).ToLocalChecked();
  if (!result_->IsObject()) {
    Nan::ThrowTypeError("\"result\" element is not an object");
    return -1;
  }

  ctx_w->result.Reset(result_.As<v8::Object>());

  if (is_file) {
    ctx_w->fctx = (struct Sass_File_Context*) cptr;
    ctx = sass_file_context_get_context(ctx_w->fctx);
  }
  else {
    ctx_w->dctx = (struct Sass_Data_Context*) cptr;
    ctx = sass_data_context_get_context(ctx_w->dctx);
  }

  struct Sass_Options* sass_options = sass_context_get_options(ctx);

  ctx_w->is_sync = is_sync;

  if (!is_sync) {
    ctx_w->request.data = ctx_w;

    // async (callback) style
    v8::Local<v8::Function> success_callback = v8::Local<v8::Function>::Cast(Nan::Get(options, Nan::New("success").ToLocalChecked()).ToLocalChecked());
    v8::Local<v8::Function> error_callback = v8::Local<v8::Function>::Cast(Nan::Get(options, Nan::New("error").ToLocalChecked()).ToLocalChecked());

    ctx_w->success_callback = new Nan::Callback(success_callback);
    ctx_w->error_callback = new Nan::Callback(error_callback);
  }

  if (!is_file) {
    ctx_w->file = create_string(Nan::Get(options, Nan::New("file").ToLocalChecked()));
    sass_option_set_input_path(sass_options, ctx_w->file);
  }

  int indent_len = Nan::To<int32_t>(
    Nan::Get(
        options,
        Nan::New("indentWidth").ToLocalChecked()
    ).ToLocalChecked()).FromJust();

  ctx_w->indent = (char*)malloc(indent_len + 1);

  strcpy(ctx_w->indent, std::string(
    indent_len,
    Nan::To<int32_t>(
        Nan::Get(
            options,
            Nan::New("indentType").ToLocalChecked()
        ).ToLocalChecked()).FromJust() == 1 ? '\t' : ' '
    ).c_str());

  ctx_w->linefeed = create_string(Nan::Get(options, Nan::New("linefeed").ToLocalChecked()));
  ctx_w->include_path = create_string(Nan::Get(options, Nan::New("includePaths").ToLocalChecked()));
  ctx_w->out_file = create_string(Nan::Get(options, Nan::New("outFile").ToLocalChecked()));
  ctx_w->source_map = create_string(Nan::Get(options, Nan::New("sourceMap").ToLocalChecked()));
  ctx_w->source_map_root = create_string(Nan::Get(options, Nan::New("sourceMapRoot").ToLocalChecked()));

  sass_option_set_output_path(sass_options, ctx_w->out_file);
  sass_option_set_output_style(sass_options, (Sass_Output_Style)Nan::To<int32_t>(Nan::Get(options, Nan::New("style").ToLocalChecked()).ToLocalChecked()).FromJust());
  sass_option_set_is_indented_syntax_src(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("indentedSyntax").ToLocalChecked()).ToLocalChecked()).FromJust());
  sass_option_set_source_comments(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("sourceComments").ToLocalChecked()).ToLocalChecked()).FromJust());
  sass_option_set_omit_source_map_url(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("omitSourceMapUrl").ToLocalChecked()).ToLocalChecked()).FromJust());
  sass_option_set_source_map_embed(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("sourceMapEmbed").ToLocalChecked()).ToLocalChecked()).FromJust());
  sass_option_set_source_map_contents(sass_options, Nan::To<bool>(Nan::Get(options, Nan::New("sourceMapContents").ToLocalChecked()).ToLocalChecked()).FromJust());
  sass_option_set_source_map_file(sass_options, ctx_w->source_map);
  sass_option_set_source_map_root(sass_options, ctx_w->source_map_root);
  sass_option_set_include_path(sass_options, ctx_w->include_path);
  sass_option_set_precision(sass_options, Nan::To<int32_t>(Nan::Get(options, Nan::New("precision").ToLocalChecked()).ToLocalChecked()).FromJust());
  sass_option_set_indent(sass_options, ctx_w->indent);
  sass_option_set_linefeed(sass_options, ctx_w->linefeed);

  v8::Local<v8::Value> importer_callback = Nan::Get(options, Nan::New("importer").ToLocalChecked()).ToLocalChecked();

  if (importer_callback->IsFunction()) {
    v8::Local<v8::Function> importer = importer_callback.As<v8::Function>();

    CustomImporterBridge *bridge = new CustomImporterBridge(importer, ctx_w->is_sync);
    ctx_w->importer_bridges.push_back(bridge);

    Sass_Importer_List c_importers = sass_make_importer_list(1);
    c_importers[0] = sass_make_importer(sass_importer, 0, bridge);

    sass_option_set_c_importers(sass_options, c_importers);
  }
  else if (importer_callback->IsArray()) {
    v8::Local<v8::Array> importers = importer_callback.As<v8::Array>();
    Sass_Importer_List c_importers = sass_make_importer_list(importers->Length());

    for (size_t i = 0; i < importers->Length(); ++i) {
      v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(Nan::Get(importers, static_cast<uint32_t>(i)).ToLocalChecked());

      CustomImporterBridge *bridge = new CustomImporterBridge(callback, ctx_w->is_sync);
      ctx_w->importer_bridges.push_back(bridge);

      c_importers[i] = sass_make_importer(sass_importer, importers->Length() - i - 1, bridge);
    }

    sass_option_set_c_importers(sass_options, c_importers);
  }

  v8::Local<v8::Value> custom_functions = Nan::Get(options, Nan::New("functions").ToLocalChecked()).ToLocalChecked();

  if (custom_functions->IsObject()) {
    v8::Local<v8::Object> functions = custom_functions.As<v8::Object>();
    v8::Local<v8::Array> signatures = Nan::GetOwnPropertyNames(functions).ToLocalChecked();
    unsigned num_signatures = signatures->Length();
    Sass_Function_List fn_list = sass_make_function_list(num_signatures);

    for (unsigned i = 0; i < num_signatures; i++) {
      v8::Local<v8::String> signature = v8::Local<v8::String>::Cast(Nan::Get(signatures, Nan::New(i)).ToLocalChecked());
      v8::Local<v8::Function> callback = v8::Local<v8::Function>::Cast(Nan::Get(functions, signature).ToLocalChecked());

      CustomFunctionBridge *bridge = new CustomFunctionBridge(callback, ctx_w->is_sync);
      ctx_w->function_bridges.push_back(bridge);

      Sass_Function_Entry fn = sass_make_function(create_string(signature), sass_custom_function, bridge);
      sass_function_set_list_entry(fn_list, i, fn);
    }

    sass_option_set_c_functions(sass_options, fn_list);
  }
  return 0;
}
示例#3
0
// entry point for libsass to request custom functions from plugin
extern "C" Sass_Function_List ADDCALL libsass_load_functions()
{

    // create list of all custom functions
    Sass_Function_List fn_list = sass_make_function_list(33);

    // math/numeric functions
    sass_function_set_list_entry(fn_list,  0, sass_make_function("sign($x)", fn_sign, 0));

    // math/exponentiation functions
    sass_function_set_list_entry(fn_list,  1, sass_make_function("exp($x)", fn_exp, 0));
    sass_function_set_list_entry(fn_list,  2, sass_make_function("log($x)", fn_log, 0));
    sass_function_set_list_entry(fn_list,  3, sass_make_function("log2($x)", fn_log2, 0));
    sass_function_set_list_entry(fn_list,  4, sass_make_function("log10($x)", fn_log10, 0));
    sass_function_set_list_entry(fn_list,  5, sass_make_function("sqrt($x)", fn_sqrt, 0));
    sass_function_set_list_entry(fn_list,  6, sass_make_function("cbrt($x)", fn_cbrt, 0));
    sass_function_set_list_entry(fn_list,  7, sass_make_function("fact($x)", fn_fact, 0));
    sass_function_set_list_entry(fn_list,  8, sass_make_function("pow($base, $power)", fn_pow, 0));

    // math/trigonometry
    sass_function_set_list_entry(fn_list,  9, sass_make_function("sin($x)", fn_sin, 0));
    sass_function_set_list_entry(fn_list, 10, sass_make_function("cos($x)", fn_cos, 0));
    sass_function_set_list_entry(fn_list, 11, sass_make_function("tan($x)", fn_tan, 0));
    sass_function_set_list_entry(fn_list, 12, sass_make_function("csc($x)", fn_csc, 0));
    sass_function_set_list_entry(fn_list, 13, sass_make_function("sec($x)", fn_sec, 0));
    sass_function_set_list_entry(fn_list, 14, sass_make_function("cot($x)", fn_cot, 0));

    // math/hyperbolic
    sass_function_set_list_entry(fn_list, 15, sass_make_function("sinh($x)", fn_sinh, 0));
    sass_function_set_list_entry(fn_list, 16, sass_make_function("cosh($x)", fn_cosh, 0));
    sass_function_set_list_entry(fn_list, 17, sass_make_function("tanh($x)", fn_tanh, 0));
    sass_function_set_list_entry(fn_list, 18, sass_make_function("csch($x)", fn_csch, 0));
    sass_function_set_list_entry(fn_list, 19, sass_make_function("sech($x)", fn_sech, 0));
    sass_function_set_list_entry(fn_list, 20, sass_make_function("coth($x)", fn_coth, 0));

    // math/inverse-trigonometry
    sass_function_set_list_entry(fn_list, 21, sass_make_function("asin($x)", fn_asin, 0));
    sass_function_set_list_entry(fn_list, 22, sass_make_function("acos($x)", fn_acos, 0));
    sass_function_set_list_entry(fn_list, 23, sass_make_function("atan($x)", fn_atan, 0));
    sass_function_set_list_entry(fn_list, 24, sass_make_function("acsc($x)", fn_acsc, 0));
    sass_function_set_list_entry(fn_list, 25, sass_make_function("asec($x)", fn_asec, 0));
    sass_function_set_list_entry(fn_list, 26, sass_make_function("acot($x)", fn_acot, 0));

    // math/inverse-hyperbolic
    sass_function_set_list_entry(fn_list, 27, sass_make_function("asinh($x)", fn_asinh, 0));
    sass_function_set_list_entry(fn_list, 28, sass_make_function("acosh($x)", fn_acosh, 0));
    sass_function_set_list_entry(fn_list, 29, sass_make_function("atanh($x)", fn_atanh, 0));
    sass_function_set_list_entry(fn_list, 30, sass_make_function("acsch($x)", fn_acsch, 0));
    sass_function_set_list_entry(fn_list, 31, sass_make_function("asech($x)", fn_asech, 0));
    sass_function_set_list_entry(fn_list, 32, sass_make_function("acoth($x)", fn_acoth, 0));

    // return the list
    return fn_list;

}