Skip to content

Commit 8703ac3

Browse files
committed
Expose variable environments via C-API
1 parent c012fcb commit 8703ac3

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

docs/api-function.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ const char* sass_callee_get_path (Sass_Callee_Entry);
5353
size_t sass_callee_get_line (Sass_Callee_Entry);
5454
size_t sass_callee_get_column (Sass_Callee_Entry);
5555
enum Sass_Callee_Type sass_callee_get_type (Sass_Callee_Entry);
56+
Sass_Env_Frame sass_callee_get_env (Sass_Callee_Entry);
57+
58+
// Getters and Setters for environments (lexical, local and global)
59+
union Sass_Value* sass_env_get_lexical (Sass_Env_Frame, const char*);
60+
void sass_env_set_lexical (Sass_Env_Frame, const char*, union Sass_Value*);
61+
union Sass_Value* sass_env_get_local (Sass_Env_Frame, const char*);
62+
void sass_env_set_local (Sass_Env_Frame, const char*, union Sass_Value*);
63+
union Sass_Value* sass_env_get_global (Sass_Env_Frame, const char*);
64+
void sass_env_set_global (Sass_Env_Frame, const char*, union Sass_Value*);
5665
```
5766
5867
### More links

include/sass/functions.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ extern "C" {
1111

1212

1313
// Forward declaration
14+
struct Sass_Env;
1415
struct Sass_Callee;
1516
struct Sass_Import;
1617
struct Sass_Options;
1718
struct Sass_Compiler;
1819
struct Sass_Importer;
1920
struct Sass_Function;
2021

22+
// Typedef helpers for callee lists
23+
typedef struct Sass_Env (*Sass_Env_Frame);
2124
// Typedef helpers for callee lists
2225
typedef struct Sass_Callee (*Sass_Callee_Entry);
2326
// Typedef helpers for import lists
@@ -81,6 +84,16 @@ ADDAPI const char* ADDCALL sass_callee_get_path (Sass_Callee_Entry);
8184
ADDAPI size_t ADDCALL sass_callee_get_line (Sass_Callee_Entry);
8285
ADDAPI size_t ADDCALL sass_callee_get_column (Sass_Callee_Entry);
8386
ADDAPI enum Sass_Callee_Type ADDCALL sass_callee_get_type (Sass_Callee_Entry);
87+
ADDAPI Sass_Env_Frame ADDCALL sass_callee_get_env (Sass_Callee_Entry);
88+
89+
// Getters and Setters for environments (lexical, local and global)
90+
ADDAPI union Sass_Value* ADDCALL sass_env_get_lexical (Sass_Env_Frame, const char*);
91+
ADDAPI void ADDCALL sass_env_set_lexical (Sass_Env_Frame, const char*, union Sass_Value*);
92+
ADDAPI union Sass_Value* ADDCALL sass_env_get_local (Sass_Env_Frame, const char*);
93+
ADDAPI void ADDCALL sass_env_set_local (Sass_Env_Frame, const char*, union Sass_Value*);
94+
ADDAPI union Sass_Value* ADDCALL sass_env_get_global (Sass_Env_Frame, const char*);
95+
ADDAPI void ADDCALL sass_env_set_global (Sass_Env_Frame, const char*, union Sass_Value*);
96+
8497
// Getters for import entry
8598
ADDAPI const char* ADDCALL sass_import_get_imp_path (Sass_Import_Entry);
8699
ADDAPI const char* ADDCALL sass_import_get_abs_path (Sass_Import_Entry);

src/eval.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,8 @@ namespace Sass {
913913
c->pstate().path,
914914
c->pstate().line + 1,
915915
c->pstate().column + 1,
916-
SASS_CALLEE_FUNCTION
916+
SASS_CALLEE_FUNCTION,
917+
{ env }
917918
});
918919

919920
// eval the body if user-defined or special, invoke underlying CPP function if native
@@ -953,7 +954,8 @@ namespace Sass {
953954
c->pstate().path,
954955
c->pstate().line + 1,
955956
c->pstate().column + 1,
956-
SASS_CALLEE_C_FUNCTION
957+
SASS_CALLEE_C_FUNCTION,
958+
{ env }
957959
});
958960

959961
To_C to_c;

src/expand.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ namespace Sass {
712712
c->pstate().path,
713713
c->pstate().line + 1,
714714
c->pstate().column + 1,
715-
SASS_CALLEE_MIXIN
715+
SASS_CALLEE_MIXIN,
716+
{ env }
716717
});
717718

718719
Env new_env(def->environment());

src/sass_functions.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cstring>
33
#include "util.hpp"
44
#include "context.hpp"
5+
#include "values.hpp"
56
#include "sass/functions.h"
67
#include "sass_functions.hpp"
78

@@ -132,6 +133,30 @@ extern "C" {
132133
size_t ADDCALL sass_callee_get_line(Sass_Callee_Entry entry) { return entry->line; }
133134
size_t ADDCALL sass_callee_get_column(Sass_Callee_Entry entry) { return entry->column; }
134135
enum Sass_Callee_Type ADDCALL sass_callee_get_type(Sass_Callee_Entry entry) { return entry->type; }
136+
Sass_Env_Frame ADDCALL sass_callee_get_env (Sass_Callee_Entry entry) { return &entry->env; }
137+
138+
// Getters and Setters for environments (lexical, local and global)
139+
union Sass_Value* ADDCALL sass_env_get_lexical (Sass_Env_Frame env, const char* name) {
140+
Expression_Ptr ex = SASS_MEMORY_CAST(Expression, (*env->frame)[name]);
141+
return ex != NULL ? ast_node_to_sass_value(ex) : NULL;
142+
}
143+
void ADDCALL sass_env_set_lexical (Sass_Env_Frame env, const char* name, union Sass_Value* val) {
144+
(*env->frame)[name] = sass_value_to_ast_node(val);
145+
}
146+
union Sass_Value* ADDCALL sass_env_get_local (Sass_Env_Frame env, const char* name) {
147+
Expression_Ptr ex = SASS_MEMORY_CAST(Expression, env->frame->get_local(name));
148+
return ex != NULL ? ast_node_to_sass_value(ex) : NULL;
149+
}
150+
void ADDCALL sass_env_set_local (Sass_Env_Frame env, const char* name, union Sass_Value* val) {
151+
env->frame->set_local(name, sass_value_to_ast_node(val));
152+
}
153+
union Sass_Value* ADDCALL sass_env_get_global (Sass_Env_Frame env, const char* name) {
154+
Expression_Ptr ex = SASS_MEMORY_CAST(Expression, env->frame->get_global(name));
155+
return ex != NULL ? ast_node_to_sass_value(ex) : NULL;
156+
}
157+
void ADDCALL sass_env_set_global (Sass_Env_Frame env, const char* name, union Sass_Value* val) {
158+
env->frame->set_global(name, sass_value_to_ast_node(val));
159+
}
135160

136161
// Getter for import entry
137162
const char* ADDCALL sass_import_get_imp_path(Sass_Import_Entry entry) { return entry->imp_path; }

src/sass_functions.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define SASS_SASS_FUNCTIONS_H
33

44
#include "sass.h"
5+
#include "environment.hpp"
6+
#include "functions.hpp"
57

68
// Struct to hold custom function callback
79
struct Sass_Function {
@@ -22,13 +24,20 @@ struct Sass_Import {
2224
size_t column;
2325
};
2426

27+
// External environments
28+
struct Sass_Env {
29+
// links to parent frames
30+
Sass::Env* frame;
31+
};
32+
2533
// External call entry
2634
struct Sass_Callee {
2735
const char* name;
2836
const char* path;
2937
size_t line;
3038
size_t column;
3139
enum Sass_Callee_Type type;
40+
struct Sass_Env env;
3241
};
3342

3443
// Struct to hold importer callback

0 commit comments

Comments
 (0)