Initial commit
[yaffs-website] / node_modules / node-sass / src / libsass / src / sass_functions.cpp
1 #include "sass.hpp"
2 #include <cstring>
3 #include "util.hpp"
4 #include "context.hpp"
5 #include "values.hpp"
6 #include "sass/functions.h"
7 #include "sass_functions.hpp"
8
9 extern "C" {
10   using namespace Sass;
11
12   Sass_Function_List ADDCALL sass_make_function_list(size_t length)
13   {
14     return (Sass_Function_List) calloc(length + 1, sizeof(Sass_Function_Entry));
15   }
16
17   Sass_Function_Entry ADDCALL sass_make_function(const char* signature, Sass_Function_Fn function, void* cookie)
18   {
19     Sass_Function_Entry cb = (Sass_Function_Entry) calloc(1, sizeof(Sass_Function));
20     if (cb == 0) return 0;
21     cb->signature = strdup(signature);
22     cb->function = function;
23     cb->cookie = cookie;
24     return cb;
25   }
26
27   void ADDCALL sass_delete_function(Sass_Function_Entry entry)
28   {
29     free(entry->signature);
30     free(entry);
31   }
32
33   // Deallocator for the allocated memory
34   void ADDCALL sass_delete_function_list(Sass_Function_List list)
35   {
36     Sass_Function_List it = list;
37     if (list == 0) return;
38     while(*list) {
39       sass_delete_function(*list);
40       ++list;
41     }
42     free(it);
43   }
44
45   // Setters and getters for callbacks on function lists
46   Sass_Function_Entry ADDCALL sass_function_get_list_entry(Sass_Function_List list, size_t pos) { return list[pos]; }
47   void sass_function_set_list_entry(Sass_Function_List list, size_t pos, Sass_Function_Entry cb) { list[pos] = cb; }
48
49   const char* ADDCALL sass_function_get_signature(Sass_Function_Entry cb) { return cb->signature; }
50   Sass_Function_Fn ADDCALL sass_function_get_function(Sass_Function_Entry cb) { return cb->function; }
51   void* ADDCALL sass_function_get_cookie(Sass_Function_Entry cb) { return cb->cookie; }
52
53   Sass_Importer_Entry ADDCALL sass_make_importer(Sass_Importer_Fn importer, double priority, void* cookie)
54   {
55     Sass_Importer_Entry cb = (Sass_Importer_Entry) calloc(1, sizeof(Sass_Importer));
56     if (cb == 0) return 0;
57     cb->importer = importer;
58     cb->priority = priority;
59     cb->cookie = cookie;
60     return cb;
61   }
62
63   Sass_Importer_Fn ADDCALL sass_importer_get_function(Sass_Importer_Entry cb) { return cb->importer; }
64   double ADDCALL sass_importer_get_priority (Sass_Importer_Entry cb) { return cb->priority; }
65   void* ADDCALL sass_importer_get_cookie(Sass_Importer_Entry cb) { return cb->cookie; }
66
67   // Just in case we have some stray import structs
68   void ADDCALL sass_delete_importer (Sass_Importer_Entry cb)
69   {
70     free(cb);
71   }
72
73   // Creator for sass custom importer function list
74   Sass_Importer_List ADDCALL sass_make_importer_list(size_t length)
75   {
76     return (Sass_Importer_List) calloc(length + 1, sizeof(Sass_Importer_Entry));
77   }
78
79   // Deallocator for the allocated memory
80   void ADDCALL sass_delete_importer_list(Sass_Importer_List list)
81   {
82     Sass_Importer_List it = list;
83     if (list == 0) return;
84     while(*list) {
85       sass_delete_importer(*list);
86       ++list;
87     }
88     free(it);
89   }
90
91   Sass_Importer_Entry ADDCALL sass_importer_get_list_entry(Sass_Importer_List list, size_t idx) { return list[idx]; }
92   void ADDCALL sass_importer_set_list_entry(Sass_Importer_List list, size_t idx, Sass_Importer_Entry cb) { list[idx] = cb; }
93
94   // Creator for sass custom importer return argument list
95   Sass_Import_List ADDCALL sass_make_import_list(size_t length)
96   {
97     return (Sass_Import**) calloc(length + 1, sizeof(Sass_Import*));
98   }
99
100   // Creator for a single import entry returned by the custom importer inside the list
101   // We take ownership of the memory for source and srcmap (freed when context is destroyd)
102   Sass_Import_Entry ADDCALL sass_make_import(const char* imp_path, const char* abs_path, char* source, char* srcmap)
103   {
104     Sass_Import* v = (Sass_Import*) calloc(1, sizeof(Sass_Import));
105     if (v == 0) return 0;
106     v->imp_path = imp_path ? sass_copy_c_string(imp_path) : 0;
107     v->abs_path = abs_path ? sass_copy_c_string(abs_path) : 0;
108     v->source = source;
109     v->srcmap = srcmap;
110     v->error = 0;
111     v->line = -1;
112     v->column = -1;
113     return v;
114   }
115
116   // Older style, but somehow still valid - keep around or deprecate?
117   Sass_Import_Entry ADDCALL sass_make_import_entry(const char* path, char* source, char* srcmap)
118   {
119     return sass_make_import(path, path, source, srcmap);
120   }
121
122   // Upgrade a normal import entry to throw an error (original path can be re-used by error reporting)
123   Sass_Import_Entry ADDCALL sass_import_set_error(Sass_Import_Entry import, const char* error, size_t line, size_t col)
124   {
125     if (import == 0) return 0;
126     if (import->error) free(import->error);
127     import->error = error ? sass_copy_c_string(error) : 0;
128     import->line = line ? line : -1;
129     import->column = col ? col : -1;
130     return import;
131   }
132
133   // Setters and getters for entries on the import list
134   void ADDCALL sass_import_set_list_entry(Sass_Import_List list, size_t idx, Sass_Import_Entry entry) { list[idx] = entry; }
135   Sass_Import_Entry ADDCALL sass_import_get_list_entry(Sass_Import_List list, size_t idx) { return list[idx]; }
136
137   // Deallocator for the allocated memory
138   void ADDCALL sass_delete_import_list(Sass_Import_List list)
139   {
140     Sass_Import_List it = list;
141     if (list == 0) return;
142     while(*list) {
143       sass_delete_import(*list);
144       ++list;
145     }
146     free(it);
147   }
148
149   // Just in case we have some stray import structs
150   void ADDCALL sass_delete_import(Sass_Import_Entry import)
151   {
152     free(import->imp_path);
153     free(import->abs_path);
154     free(import->source);
155     free(import->srcmap);
156     free(import->error);
157     free(import);
158   }
159
160   // Getter for callee entry
161   const char* ADDCALL sass_callee_get_name(Sass_Callee_Entry entry) { return entry->name; }
162   const char* ADDCALL sass_callee_get_path(Sass_Callee_Entry entry) { return entry->path; }
163   size_t ADDCALL sass_callee_get_line(Sass_Callee_Entry entry) { return entry->line; }
164   size_t ADDCALL sass_callee_get_column(Sass_Callee_Entry entry) { return entry->column; }
165   enum Sass_Callee_Type ADDCALL sass_callee_get_type(Sass_Callee_Entry entry) { return entry->type; }
166   Sass_Env_Frame ADDCALL sass_callee_get_env (Sass_Callee_Entry entry) { return &entry->env; }
167
168   // Getters and Setters for environments (lexical, local and global)
169   union Sass_Value* ADDCALL sass_env_get_lexical (Sass_Env_Frame env, const char* name) {
170     Expression_Ptr ex = Cast<Expression>((*env->frame)[name]);
171     return ex != NULL ? ast_node_to_sass_value(ex) : NULL;
172   }
173   void ADDCALL sass_env_set_lexical (Sass_Env_Frame env, const char* name, union Sass_Value* val) {
174     (*env->frame)[name] = sass_value_to_ast_node(val);
175   }
176   union Sass_Value* ADDCALL sass_env_get_local (Sass_Env_Frame env, const char* name) {
177     Expression_Ptr ex = Cast<Expression>(env->frame->get_local(name));
178     return ex != NULL ? ast_node_to_sass_value(ex) : NULL;
179   }
180   void ADDCALL sass_env_set_local (Sass_Env_Frame env, const char* name, union Sass_Value* val) {
181     env->frame->set_local(name, sass_value_to_ast_node(val));
182   }
183   union Sass_Value* ADDCALL sass_env_get_global (Sass_Env_Frame env, const char* name) {
184     Expression_Ptr ex = Cast<Expression>(env->frame->get_global(name));
185     return ex != NULL ? ast_node_to_sass_value(ex) : NULL;
186   }
187   void ADDCALL sass_env_set_global (Sass_Env_Frame env, const char* name, union Sass_Value* val) {
188     env->frame->set_global(name, sass_value_to_ast_node(val));
189   }
190
191   // Getter for import entry
192   const char* ADDCALL sass_import_get_imp_path(Sass_Import_Entry entry) { return entry->imp_path; }
193   const char* ADDCALL sass_import_get_abs_path(Sass_Import_Entry entry) { return entry->abs_path; }
194   const char* ADDCALL sass_import_get_source(Sass_Import_Entry entry) { return entry->source; }
195   const char* ADDCALL sass_import_get_srcmap(Sass_Import_Entry entry) { return entry->srcmap; }
196
197   // Getter for import error entry
198   size_t ADDCALL sass_import_get_error_line(Sass_Import_Entry entry) { return entry->line; }
199   size_t ADDCALL sass_import_get_error_column(Sass_Import_Entry entry) { return entry->column; }
200   const char* ADDCALL sass_import_get_error_message(Sass_Import_Entry entry) { return entry->error; }
201
202   // Explicit functions to take ownership of the memory
203   // Resets our own property since we do not know if it is still alive
204   char* ADDCALL sass_import_take_source(Sass_Import_Entry entry) { char* ptr = entry->source; entry->source = 0; return ptr; }
205   char* ADDCALL sass_import_take_srcmap(Sass_Import_Entry entry) { char* ptr = entry->srcmap; entry->srcmap = 0; return ptr; }
206
207 }