Skip to content

Commit 50c6da2

Browse files
committed
Add back custom functions
1 parent 9719966 commit 50c6da2

File tree

8 files changed

+24
-47
lines changed

8 files changed

+24
-47
lines changed

src/capi_functions.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ extern "C" {
1414
{
1515
struct SassFunction* cb = new SassFunction{};
1616
if (cb == 0 || signature == 0) return 0;
17-
cb->signature = sass_copy_c_string(signature);
17+
cb->signature = signature;
1818
cb->function = callback;
1919
cb->cookie = cookie;
2020
return cb;
2121
}
2222

2323
void ADDCALL sass_delete_function(struct SassFunction* function)
2424
{
25-
sass_free_c_string(function->signature);
2625
delete function;
2726
}
2827

@@ -32,7 +31,7 @@ extern "C" {
3231

3332
const char* ADDCALL sass_function_get_signature(struct SassFunction* function)
3433
{
35-
return function->signature;
34+
return function->signature.c_str();
3635
}
3736

3837
SassFunctionLambda ADDCALL sass_function_get_function(struct SassFunction* function)

src/capi_functions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// Struct to hold custom function callback
99
struct SassFunction {
10-
char* signature;
10+
sass::string signature;
1111
SassFunctionLambda function;
1212
void* cookie;
1313
};

src/color_maps.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/////////////////////////////////////////////////////////////////////////
2-
/////////////////////////////////////////////////////////////////////////
1+
/*****************************************************************************/
2+
/* Part of LibSass, released under the MIT license (See LICENSE.txt). */
3+
/*****************************************************************************/
34
#include "color_maps.hpp"
45

5-
#include "util_string.hpp"
66
#include "string_utils.hpp"
77

88
namespace Sass

src/compiler.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ namespace Sass {
9898
// Value& val(Value::unwrap(value));
9999
// Compiler& compiler(Compiler::unwrap(comp));
100100
// compiler.setVariable(key.assertString(compiler, "name")->getText(), &val, false, false);
101-
return sass_make_null();
101+
return value;
102102
}
103103

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

473473
// Get the external importer function
474-
SassImporterLambda fn = sass_importer_get_callback(importer);
474+
SassImporterLambda fn = importer->importer;
475475

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

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

738-
// Create a new external callable from the sass function. Parses
739-
// function signature into function name and argument declaration.
740-
ExternalCallable* Compiler::makeExternalCallable(struct SassFunction* function)
738+
// Register an external custom sass function on the global scope.
739+
// Main entry point for custom functions passed through the C-API.
740+
void Compiler::registerCustomFunction(struct SassFunction* function)
741741
{
742-
// Create temporary source object for signature
743-
SourceStringObj source = SASS_MEMORY_NEW(SourceString,
744-
"sass://signature", function->signature);
745-
// Create a new scss parser instance
742+
EnvRoot root(*this);
743+
SourceDataObj source = SASS_MEMORY_NEW(SourceString,
744+
"sass://signature", sass::string(function->signature));
746745
ScssParser parser(*this, source.ptr());
747746
ExternalCallable* callable =
748747
parser.parseExternalCallable();
749748
callable->function(function);
750-
return callable;
751-
}
752-
// EO makeExternalCallable
753-
754-
// Register an external custom sass function on the global scope.
755-
// Main entry point for custom functions passed through the C-API.
756-
void Compiler::registerCustomFunction(struct SassFunction* function)
757-
{
758-
// EnvRoot root(*this);
759-
// Create a new external callable from the sass function
760-
// ExternalCallable* callable = makeExternalCallable(function);
761-
// Currently external functions are treated globally
762-
// if (fnLookup.count(callable->envkey()) == 0) {
763-
// fnLookup.insert(std::make_pair(callable->envkey(), callable));
764-
// varRoot.createFunction(callable->envkey());
765-
// fnList.push_back(callable);
766-
// }
749+
auto& functions(varRoot.intFunction);
750+
uint32_t offset((uint32_t)functions.size());
751+
varRoot.intFunction.push_back(callable);
752+
varRoot.idxs->fnIdxs[callable->name()] = offset;
753+
varRoot.privateFnOffset = offset;
767754
}
768755
// EO registerCustomFunction
769756

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

904891
// registerCustomFunction(qwe);
905892

906-
// registerCustomFunction(sass_make_function("set-local($name, $value)", fn_set_local, (void*)31));
893+
registerCustomFunction(sass_make_function("set-local($name, $value)", fn_set_local, (void*)31));
907894
// registerCustomFunction(sass_make_function("set-global($name, $value)", fn_set_global, (void*)31));
908895
// registerCustomFunction(sass_make_function("set-lexical($name, $value)", fn_set_lexical, (void*)31));
909896
//

src/compiler.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,6 @@ namespace Sass {
212212
bool callCustomLoader(const sass::string& imp_path, SourceSpan& pstate, ImportRule* rule,
213213
const sass::vector<struct SassImporter*>& importers, bool singletone = true);
214214

215-
/////////////////////////////////////////////////////////////////////////
216-
// Create a new external callable from the sass function. Parses
217-
// function signature into function name and argument declaration.
218-
// The function you pass in will be taken over and freed by us!
219-
/////////////////////////////////////////////////////////////////////////
220-
ExternalCallable* makeExternalCallable(struct SassFunction* function);
221-
222215
/////////////////////////////////////////////////////////////////////////
223216
// Register an external custom sass function on the global scope.
224217
// Main entry point for custom functions passed through the C-API.

src/memory/allocator.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55

66
#ifdef SASS_CUSTOM_ALLOCATOR
77
#include "memory_pool.hpp"
8-
#endif
98

109
namespace Sass {
1110

12-
#ifdef SASS_CUSTOM_ALLOCATOR
13-
1411
// You must only use PODs for thread_local.
1512
// Objects get very unpredictable init order.
1613
static thread_local MemoryPool* pool;
@@ -46,6 +43,6 @@ namespace Sass {
4643

4744
}
4845

49-
#endif
50-
5146
}
47+
48+
#endif

src/randomize.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace Sass {
4141
}
4242
// Use some sensible default
4343
if (seed == 0) {
44+
// Fibonacci/Golden Ratio Hashing
4445
seed = 0x9e3779b9;
4546
}
4647
// Return the seed

src/units.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ namespace Sass {
9595
else if (s == "mm") return UnitType::MM;
9696
else if (s == "cm") return UnitType::CM;
9797
else if (s == "in") return UnitType::INCH;
98-
else if (s == "q") return UnitType::QMM;
98+
else if (s == "q") return UnitType::QMM;
9999
// angle units
100100
else if (s == "deg") return UnitType::DEG;
101101
else if (s == "grad") return UnitType::GRAD;

0 commit comments

Comments
 (0)