sass / sassc

libsass command line driver
Other
785 stars 131 forks source link

sass_make_data_context adopts char* #257

Closed MrBenGriffin closed 5 years ago

MrBenGriffin commented 5 years ago

It is not clear in the documents (or examples) that the memory of the data char* being handed over to sass_make_data_context() will be owned by the context, and will be deleted with the context.

It is still not clear to me whether or not the options memory returned by sass_data_context_get_options is owned by the context. Likewise, if it is owned by the context, it is not clear to me whether or not memory being passed via options methods (e.g. sass_option_set_include_path and sass_option_set_source_map_file) is now adopted by the context, and will be deleted by the context.

mgreter commented 5 years ago

https://github.com/sass/libsass/blob/master/docs/api-doc.md#memory-handling-and-life-cycles Only the main data (sass/scss content) is taken over from the caller, all other strings are copied. When finished you can take over the result from libsass if wanted via e.g. sass_context_take_output_string. The result from sass_context_get_output_string will only be valid until you call sass_delete_context.

MrBenGriffin commented 5 years ago

See, I still don’t understand.. On the same page that you link me to, the example shows..

context = sass_make_data_context("div { a { color: blue; } }");

Yet, later (and in terms of what you suggest), this should really be written

context = sass_make_data_context(sass_copy_c_string("div { a { color: blue; } }");

What am I missing?

On 7 Aug 2019, at 19:37, Marcel Greter notifications@github.com wrote:

https://github.com/sass/libsass/blob/master/docs/api-doc.md#memory-handling-and-life-cycles Only the main data (sass/scss content) is taken over from the caller, all other strings are copied. When finished you can take over the result from libsass if wanted via e.g. sass_context_take_output_string. The result from sass_context_get_output_string will only be valid until you call sass_delete_context.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

mgreter commented 5 years ago

Yep, the pseudo code example is wrong there, care to PR to update the docs?

Here a working example with data_context:

#include <stdio.h>
#include "sass/context.h"

int main( int argc, const char* argv[] )
{

  // LibSass will take control of data you pass in
  // Therefore we need to make a copy of static data
  char* text = sass_copy_c_string("a{b:c;}");
  // Normally you'll load data into a buffer
  // Use `sass_alloc_memory` to get a buffer to pass to LibSass
  // then fill it with data you load from disk or somwhere else

  // create the data context and get all related structs
  struct Sass_Data_Context* data_ctx = sass_make_data_context(text);
  struct Sass_Context* ctx = sass_data_context_get_context(data_ctx);
  struct Sass_Options* ctx_opt = sass_context_get_options(ctx);

  // configure some options ...
  sass_option_set_precision(ctx_opt, 10);

  // context is set up, call the compile step now
  int status = sass_compile_data_context(data_ctx);

  // print the result or the error to the stdout
  if (status == 0) puts(sass_context_get_output_string(ctx));
  else puts(sass_context_get_error_message(ctx));

  // release allocated memory
  sass_delete_data_context(data_ctx);

  // exit status
  return status;

}
mgreter commented 5 years ago

Btw. the general rule is if the API takes const char* it will make a copy, it the API is char* it will take over memory ownership, so make sure to pass in memory that is allocated via sass_copy_c_string or sass_alloc_memory.

MrBenGriffin commented 5 years ago

Thanks - I will try to do a PR! Very useful help.

On 8 Aug 2019, at 04:07, Marcel Greter notifications@github.com wrote:

Yep, the pseudo code example is wrong there, care to PR to update the docs?

Here a working example with data_context:

include

include "sass/context.h"

int main( int argc, const char* argv[] ) {

// get the input data from first argument or use default const char* input = argc > 1 ? argv[1] : "styles.scss";

// LibSass will take control of data you pass in // Therefore we need to make a copy of static data char* text = sass_copy_c_string("a{b:c;}"); // Normally you'll load data into a buffer // Use sass_alloc_memory to get a buffer to pass to LibSass // then fill it with data you load from disk or somwhere else

// create the data context and get all related structs struct Sass_Data_Context data_ctx = sass_make_data_context(text); struct Sass_Context ctx = sass_data_context_get_context(data_ctx); struct Sass_Options* ctx_opt = sass_context_get_options(ctx);

// configure some options ... sass_option_set_precision(ctx_opt, 10);

// context is set up, call the compile step now int status = sass_compile_data_context(data_ctx);

// print the result or the error to the stdout if (status == 0) puts(sass_context_get_output_string(ctx)); else puts(sass_context_get_error_message(ctx));

// release allocated memory sass_delete_data_context(data_ctx);

// exit status return status;

} — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.