Blame docs/plugins.md

Packit Service 7770af
Plugins are shared object files (.so on *nix and .dll on win) that can be loaded by LibSass on runtime. Currently we only provide a way to load internal/custom functions from plugins. In the future we probably will also add a way to provide custom importers via plugins (needs more refactoring to [support multiple importers with some kind of priority system](https://github.com/sass/libsass/issues/962)).
Packit Service 7770af
Packit Service 7770af
## plugin.cpp
Packit Service 7770af
Packit Service 7770af
```C++
Packit Service 7770af
#include <cstring>
Packit Service 7770af
#include <iostream>
Packit Service 7770af
#include <stdint.h>
Packit Service 7770af
#include "sass_values.h"
Packit Service 7770af
Packit Service 7770af
union Sass_Value* ADDCALL call_fn_foo(const union Sass_Value* s_args, void* cookie)
Packit Service 7770af
{
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" const char* ADDCALL libsass_get_version() {
Packit Service 7770af
  return libsass_version();
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
extern "C" Sass_C_Function_List ADDCALL libsass_load_functions()
Packit Service 7770af
{
Packit Service 7770af
  // allocate a custom function caller
Packit Service 7770af
  Sass_C_Function_Callback fn_foo =
Packit Service 7770af
    sass_make_function("foo()", call_fn_foo, (void*)42);
Packit Service 7770af
  // create list of all custom functions
Packit Service 7770af
  Sass_C_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, fn_foo);
Packit Service 7770af
  // return the list
Packit Service 7770af
  return fn_list;
Packit Service 7770af
}
Packit Service 7770af
```
Packit Service 7770af
Packit Service 7770af
To compile the plugin you need to have LibSass already built as a shared library (to link against it). The commands below expect the shared library in the `lib` sub-directory (`-Llib`). The plugin and the main LibSass process should "consume" the same shared LibSass library on runtime. It will propably also work if they use different LibSass versions. In this case we check if the major versions are compatible (i.e. 3.1.3 and 3.1.1 would be considered compatible).
Packit Service 7770af
Packit Service 7770af
## Compile with gcc on linux
Packit Service 7770af
Packit Service 7770af
```bash
Packit Service 7770af
g++ -O2 -shared plugin.cpp -o plugin.so -fPIC -Llib -lsass
Packit Service 7770af
```
Packit Service 7770af
Packit Service 7770af
## Compile with mingw on windows
Packit Service 7770af
Packit Service 7770af
```bash
Packit Service 7770af
g++ -O2 -shared plugin.cpp -o plugin.dll -Llib -lsass
Packit Service 7770af
```