-
Notifications
You must be signed in to change notification settings - Fork 472
[WIP] add length of included_files array #1001
Conversation
|
IMO it's actually not very hard 😉 char** incs = sass_get_included_files;
while (*incs) { process(*incs); ++incs; } |
|
for perl :p. cgo isn't so gentle! On Sat, Mar 28, 2015 at 8:08 PM Marcel Greter notifications@github.com
|
|
I thought about doing: but this might cause bad runtime exceptions |
|
If you need to compute the length beforehand you could use plain C: size_t l = 0; while (*incs) { ++l; ++incs; } |
|
True!, but this makes it available in the API which felt like a good service to others coming across the same issue. Also, num_included_files was already in the interface but not used anywhere: https://github.com/sass/libsass/blob/master/sass_interface.h#L58 |
|
|
|
A shorter and future-proof (but slightly slower, if called multiple times) equivalent would be: ADDAPI size_t ADDCALL sass_context_get_included_files_size (struct Sass_Context* ctx);
size_t ADDCALL sass_context_get_included_files_size (struct Sass_Context* ctx)
{ size_t l = 0; auto i = ctx->included_files; while (i && *i) { ++i; ++l; } return l; } |
|
This might be of your interest: node-sass/src/binding.cpp#L135-L148: char** included_files = sass_context_get_included_files(ctx);
if (included_files) {
for (int i = 0; included_files[i] != nullptr; ++i) {
// do something
}
} |
|
You don't even need the for (; *included_files != nullptr; included_files++) {
// do something
} |
* As suggested by @QuLogic: sass/libsass#1001 (comment)
|
@QuLogic, thanks for the suggestion. It is fixed by sass/node-sass@8a194d0. 👍 However, we need the iterator index to insert the values in v8 array. |
|
No, wait, actually, there might be a case where the array is |
|
Yes we are checking it in all cases, but is there state, when libsass returns I think it always returns the valid pointer, since it converts C++ vector (context.hpp#L43) to array of C strings (sass_context.cpp#L475), while allocating memory in all cases: sass_interface.cpp#L35. Should we consider it safe? |
|
Libsass doesn't guarantee it (ie. if you call it too eraly, before compilation has taken place). I would say play safe here, as it doesn't really imrpove the performance anyway! |
This extends the API to add the length of the included_files array. Without this, it is difficult to determine the length of the C array exposed via the API interface.