Skip to content

Commit

Permalink
Add back custom functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Dec 7, 2020
1 parent 9719966 commit 50c6da2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 47 deletions.
5 changes: 2 additions & 3 deletions src/capi_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ extern "C" {
{
struct SassFunction* cb = new SassFunction{};
if (cb == 0 || signature == 0) return 0;
cb->signature = sass_copy_c_string(signature);
cb->signature = signature;
cb->function = callback;
cb->cookie = cookie;
return cb;
}

void ADDCALL sass_delete_function(struct SassFunction* function)
{
sass_free_c_string(function->signature);
delete function;
}

Expand All @@ -32,7 +31,7 @@ extern "C" {

const char* ADDCALL sass_function_get_signature(struct SassFunction* function)
{
return function->signature;
return function->signature.c_str();
}

SassFunctionLambda ADDCALL sass_function_get_function(struct SassFunction* function)
Expand Down
2 changes: 1 addition & 1 deletion src/capi_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

// Struct to hold custom function callback
struct SassFunction {
char* signature;
sass::string signature;
SassFunctionLambda function;
void* cookie;
};
Expand Down
6 changes: 3 additions & 3 deletions src/color_maps.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/* Part of LibSass, released under the MIT license (See LICENSE.txt). */
/*****************************************************************************/
#include "color_maps.hpp"

#include "util_string.hpp"
#include "string_utils.hpp"

namespace Sass
Expand Down
41 changes: 14 additions & 27 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace Sass {
// Value& val(Value::unwrap(value));
// Compiler& compiler(Compiler::unwrap(comp));
// compiler.setVariable(key.assertString(compiler, "name")->getText(), &val, false, false);
return sass_make_null();
return value;
}

// so far only pow has two arguments
Expand Down Expand Up @@ -471,7 +471,7 @@ struct SassValue* fn_##fn(struct SassValue* s_args, Sass_Function_Entry cb, stru
for (struct SassImporter* importer : importers) {

// Get the external importer function
SassImporterLambda fn = sass_importer_get_callback(importer);
SassImporterLambda fn = importer->importer;

// std::cerr << "Calling custom loader " << fn << "\n";

Expand Down Expand Up @@ -735,35 +735,22 @@ struct SassValue* fn_##fn(struct SassValue* s_args, Sass_Function_Entry cb, stru
// Interface for external custom functions
/////////////////////////////////////////////////////////////////////////

// Create a new external callable from the sass function. Parses
// function signature into function name and argument declaration.
ExternalCallable* Compiler::makeExternalCallable(struct SassFunction* function)
// Register an external custom sass function on the global scope.
// Main entry point for custom functions passed through the C-API.
void Compiler::registerCustomFunction(struct SassFunction* function)
{
// Create temporary source object for signature
SourceStringObj source = SASS_MEMORY_NEW(SourceString,
"sass://signature", function->signature);
// Create a new scss parser instance
EnvRoot root(*this);
SourceDataObj source = SASS_MEMORY_NEW(SourceString,
"sass://signature", sass::string(function->signature));
ScssParser parser(*this, source.ptr());
ExternalCallable* callable =
parser.parseExternalCallable();
callable->function(function);
return callable;
}
// EO makeExternalCallable

// Register an external custom sass function on the global scope.
// Main entry point for custom functions passed through the C-API.
void Compiler::registerCustomFunction(struct SassFunction* function)
{
// EnvRoot root(*this);
// Create a new external callable from the sass function
// ExternalCallable* callable = makeExternalCallable(function);
// Currently external functions are treated globally
// if (fnLookup.count(callable->envkey()) == 0) {
// fnLookup.insert(std::make_pair(callable->envkey(), callable));
// varRoot.createFunction(callable->envkey());
// fnList.push_back(callable);
// }
auto& functions(varRoot.intFunction);
uint32_t offset((uint32_t)functions.size());
varRoot.intFunction.push_back(callable);
varRoot.idxs->fnIdxs[callable->name()] = offset;
varRoot.privateFnOffset = offset;
}
// EO registerCustomFunction

Expand Down Expand Up @@ -903,7 +890,7 @@ struct SassValue* fn_##fn(struct SassValue* s_args, Sass_Function_Entry cb, stru

// registerCustomFunction(qwe);

// registerCustomFunction(sass_make_function("set-local($name, $value)", fn_set_local, (void*)31));
registerCustomFunction(sass_make_function("set-local($name, $value)", fn_set_local, (void*)31));
// registerCustomFunction(sass_make_function("set-global($name, $value)", fn_set_global, (void*)31));
// registerCustomFunction(sass_make_function("set-lexical($name, $value)", fn_set_lexical, (void*)31));
//
Expand Down
7 changes: 0 additions & 7 deletions src/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,6 @@ namespace Sass {
bool callCustomLoader(const sass::string& imp_path, SourceSpan& pstate, ImportRule* rule,
const sass::vector<struct SassImporter*>& importers, bool singletone = true);

/////////////////////////////////////////////////////////////////////////
// Create a new external callable from the sass function. Parses
// function signature into function name and argument declaration.
// The function you pass in will be taken over and freed by us!
/////////////////////////////////////////////////////////////////////////
ExternalCallable* makeExternalCallable(struct SassFunction* function);

/////////////////////////////////////////////////////////////////////////
// Register an external custom sass function on the global scope.
// Main entry point for custom functions passed through the C-API.
Expand Down
7 changes: 2 additions & 5 deletions src/memory/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

#ifdef SASS_CUSTOM_ALLOCATOR
#include "memory_pool.hpp"
#endif

namespace Sass {

#ifdef SASS_CUSTOM_ALLOCATOR

// You must only use PODs for thread_local.
// Objects get very unpredictable init order.
static thread_local MemoryPool* pool;
Expand Down Expand Up @@ -46,6 +43,6 @@ namespace Sass {

}

#endif

}

#endif
1 change: 1 addition & 0 deletions src/randomize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace Sass {
}
// Use some sensible default
if (seed == 0) {
// Fibonacci/Golden Ratio Hashing
seed = 0x9e3779b9;
}
// Return the seed
Expand Down
2 changes: 1 addition & 1 deletion src/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace Sass {
else if (s == "mm") return UnitType::MM;
else if (s == "cm") return UnitType::CM;
else if (s == "in") return UnitType::INCH;
else if (s == "q") return UnitType::QMM;
else if (s == "q") return UnitType::QMM;
// angle units
else if (s == "deg") return UnitType::DEG;
else if (s == "grad") return UnitType::GRAD;
Expand Down

0 comments on commit 50c6da2

Please sign in to comment.