Skip to content

Commit

Permalink
Merge pull request sass#1626 from mgreter/feature/expose-call-stack
Browse files Browse the repository at this point in the history
Refactor C++ context to expose call stack
  • Loading branch information
mgreter committed Nov 1, 2015
2 parents 7a50f45 + aa915e2 commit f89e8d7
Show file tree
Hide file tree
Showing 25 changed files with 765 additions and 614 deletions.
15 changes: 12 additions & 3 deletions docs/api-function-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
#include <stdint.h>
#include "sass/context.h"

union Sass_Value* call_fn_foo(const union Sass_Value* s_args, void* cookie)
union Sass_Value* call_fn_foo(const union Sass_Value* s_args, Sass_Function_Entry cb, struct Sass_Compiler* comp)
{
// get context/option struct associated with this compiler
struct Sass_Context* ctx = sass_compiler_get_context(comp);
struct Sass_Options* opts = sass_compiler_get_options(comp);
// get information about previous importer entry from the stack
struct Sass_Import* import = sass_compiler_get_last_import(comp);
const char* prev_abs_path = sass_import_get_abs_path(import);
const char* prev_imp_path = sass_import_get_imp_path(import);
// get the cookie from function descriptor
void* cookie = sass_function_get_cookie(cb);
// we actually abuse the void* to store an "int"
return sass_make_number((intptr_t)cookie, "px");
}
Expand All @@ -23,11 +32,11 @@ int main( int argc, const char* argv[] )
struct Sass_Options* ctx_opt = sass_context_get_options(ctx);

// allocate a custom function caller
Sass_C_Function_Callback fn_foo =
Sass_Function_Entry fn_foo =
sass_make_function("foo()", call_fn_foo, (void*)42);

// create list of all custom functions
Sass_C_Function_List fn_list = sass_make_function_list(1);
Sass_Function_List fn_list = sass_make_function_list(1);
sass_function_set_list_entry(fn_list, 0, fn_foo);
sass_option_set_c_functions(ctx_opt, fn_list);

Expand Down
8 changes: 4 additions & 4 deletions docs/api-function-internal.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
```C
// Struct to hold custom function callback
struct Sass_C_Function_Descriptor {
const char* signature;
Sass_C_Function function;
void* cookie;
struct Sass_Function {
const char* signature;
Sass_Function_Fn function;
void* cookie;
};
```
28 changes: 15 additions & 13 deletions docs/api-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@ Note: The fallback implementation will be given the name of the called function

```C
// Forward declaration
struct Sass_C_Function_Descriptor;
struct Sass_Compiler;
struct Sass_Function;

// Typedef defining null terminated list of custom callbacks
typedef struct Sass_C_Function_Descriptor* (*Sass_C_Function_List);
typedef struct Sass_C_Function_Descriptor (*Sass_C_Function_Callback);
// Typedef defining custom function prototype and its return value type
typedef union Sass_Value*(*Sass_C_Function) (const union Sass_Value*, void* cookie);
// Typedef helpers for custom functions lists
typedef struct Sass_Function (*Sass_Function_Entry);
typedef struct Sass_Function* (*Sass_Function_List);
// Typedef defining function signature and return type
typedef union Sass_Value* (*Sass_Function_Fn)
(const union Sass_Value*, Sass_Function_Entry cb, struct Sass_Compiler* compiler);

// Creators for sass function list and function descriptors
Sass_C_Function_List sass_make_function_list (size_t length);
Sass_C_Function_Callback sass_make_function (const char* signature, Sass_C_Function fn, void* cookie);
ADDAPI Sass_Function_List ADDCALL sass_make_function_list (size_t length);
ADDAPI Sass_Function_Entry ADDCALL sass_make_function (const char* signature, Sass_Function_Fn cb, void* cookie);

// Setters and getters for callbacks on function lists
Sass_C_Function_Callback sass_function_get_list_entry(Sass_C_Function_List list, size_t pos);
void sass_function_set_list_entry(Sass_C_Function_List list, size_t pos, Sass_C_Function_Callback cb);
ADDAPI Sass_Function_Entry ADDCALL sass_function_get_list_entry(Sass_Function_List list, size_t pos);
ADDAPI void ADDCALL sass_function_set_list_entry(Sass_Function_List list, size_t pos, Sass_Function_Entry cb);

// Getters for custom function descriptors
const char* sass_function_get_signature (Sass_C_Function_Callback fn);
Sass_C_Function sass_function_get_function (Sass_C_Function_Callback fn);
void* sass_function_get_cookie (Sass_C_Function_Callback fn);
ADDAPI const char* ADDCALL sass_function_get_signature (Sass_Function_Entry cb);
ADDAPI Sass_Function_Fn ADDCALL sass_function_get_function (Sass_Function_Entry cb);
ADDAPI void* ADDCALL sass_function_get_cookie (Sass_Function_Entry cb);
```
### More links
Expand Down
13 changes: 9 additions & 4 deletions docs/api-importer-internal.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
```C
// External import entry
struct Sass_Import {
char* rel;
char* abs;
char* imp_path; // path as found in the import statement
char *abs_path; // path after importer has resolved it
char* source;
char* srcmap;
// error handling
char* error;
size_t line;
size_t column;
};

// Struct to hold importer callback
struct Sass_C_Import_Descriptor {
Sass_C_Import_Fn function;
struct Sass_Importer {
Sass_Importer_Fn importer;
double priority;
void* cookie;
};
```
24 changes: 15 additions & 9 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,26 +538,32 @@ namespace Sass {
// necessary to store a list of each in an Import node.
////////////////////////////////////////////////////////////////////////////
class Import : public Statement {
std::vector<std::string> files_;
std::vector<Expression*> urls_;
ADD_PROPERTY(List*, media_queries);
std::vector<Expression*> urls_;
std::vector<Include> incs_;
ADD_PROPERTY(List*, media_queries);
public:
Import(ParserState pstate)
: Statement(pstate),
files_(std::vector<std::string>()),
urls_(std::vector<Expression*>()),
incs_(std::vector<Include>()),
media_queries_(0)
{ statement_type(IMPORT); }
std::vector<std::string>& files() { return files_; }
std::vector<Expression*>& urls() { return urls_; }
std::vector<Expression*>& urls() { return urls_; }
std::vector<Include>& incs() { return incs_; }
ATTACH_OPERATIONS()
};

// not yet resolved single import
// so far we only know requested name
class Import_Stub : public Statement {
ADD_PROPERTY(std::string, file_name)
Include resource_;
public:
Import_Stub(ParserState pstate, std::string f)
: Statement(pstate), file_name_(f)
std::string abs_path() { return resource_.abs_path; };
std::string imp_path() { return resource_.imp_path; };
Include resource() { return resource_; };

Import_Stub(ParserState pstate, Include res)
: Statement(pstate), resource_(res)
{ statement_type(IMPORT_STUB); }
ATTACH_OPERATIONS()
};
Expand Down
3 changes: 3 additions & 0 deletions src/ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ namespace Sass {
class Complex_Selector;
class Selector_List;

// common classes
class Context;

}

#endif
Loading

0 comments on commit f89e8d7

Please sign in to comment.