Blame contrib/plugin.cpp

Packit Service 7770af
#include <cstring>
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <stdint.h>
Packit Service 7770af
#include <sass.h>
Packit Service 7770af
Packit Service 7770af
// gcc: g++ -shared plugin.cpp -o plugin.so -fPIC -Llib -lsass
Packit Service 7770af
// mingw: g++ -shared plugin.cpp -o plugin.dll -Llib -lsass
Packit Service 7770af
Packit Service 7770af
extern "C" const char* ADDCALL libsass_get_version() {
Packit Service 7770af
  return libsass_version();
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
union Sass_Value* custom_function(const union Sass_Value* s_args, Sass_Function_Entry cb, struct Sass_Compiler* comp)
Packit Service 7770af
{
Packit Service 7770af
  // get context/option struct associated with this compiler
Packit Service 7770af
  struct Sass_Context* ctx = sass_compiler_get_context(comp);
Packit Service 7770af
  struct Sass_Options* opts = sass_compiler_get_options(comp);
Packit Service 7770af
  // get the cookie from function descriptor
Packit Service 7770af
  void* cookie = sass_function_get_cookie(cb);
Packit Service 7770af
  // we actually abuse the void* to store an "int"
Packit Service 7770af
  return sass_make_number((intptr_t)cookie, "px");
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
extern "C" Sass_Function_List ADDCALL libsass_load_functions()
Packit Service 7770af
{
Packit Service 7770af
  // allocate a custom function caller
Packit Service 7770af
  Sass_Function_Entry c_func =
Packit Service 7770af
    sass_make_function("foo()", custom_function, (void*)42);
Packit Service 7770af
  // create list of all custom functions
Packit Service 7770af
  Sass_Function_List fn_list = sass_make_function_list(1);
Packit Service 7770af
  // put the only function in this plugin to the list
Packit Service 7770af
  sass_function_set_list_entry(fn_list, 0, c_func);
Packit Service 7770af
  // return the list
Packit Service 7770af
  return fn_list;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
Sass_Import_List custom_importer(const char* cur_path, Sass_Importer_Entry cb, struct Sass_Compiler* comp)
Packit Service 7770af
{
Packit Service 7770af
  // get the cookie from importer descriptor
Packit Service 7770af
  void* cookie = sass_importer_get_cookie(cb);
Packit Service 7770af
  // create a list to hold our import entries
Packit Service 7770af
  Sass_Import_List incs = sass_make_import_list(1);
Packit Service 7770af
  // create our only import entry (route path back)
Packit Service 7770af
  incs[0] = sass_make_import_entry(cur_path, 0, 0);
Packit Service 7770af
  // return imports
Packit Service 7770af
  return incs;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
extern "C" Sass_Importer_List ADDCALL libsass_load_importers()
Packit Service 7770af
{
Packit Service 7770af
  // allocate a custom function caller
Packit Service 7770af
  Sass_Importer_Entry c_imp =
Packit Service 7770af
    sass_make_importer(custom_importer, - 99, (void*)42);
Packit Service 7770af
  // create list of all custom functions
Packit Service 7770af
  Sass_Importer_List imp_list = sass_make_importer_list(1);
Packit Service 7770af
  // put the only function in this plugin to the list
Packit Service 7770af
  sass_importer_set_list_entry(imp_list, 0, c_imp);
Packit Service 7770af
  // return the list
Packit Service 7770af
  return imp_list;
Packit Service 7770af
}