Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create shared library #92

Open
terkin opened this issue Jul 26, 2022 · 1 comment
Open

Create shared library #92

terkin opened this issue Jul 26, 2022 · 1 comment
Labels

Comments

@terkin
Copy link

terkin commented Jul 26, 2022

Hi, I really want to use at my python code, and for that I need to compile shared library, I tried to make it from bin/handlebarsc.c but probably its not correct, cause at result SO file I have a lot of undefined reference, can you please help me if its possible at all?

@jbboehr
Copy link
Owner

jbboehr commented Jul 28, 2022

I'm not really familiar with Python or Heroku unfortunately.

What sort of undefined references are you getting? Can you go into a bit more detail on what you've tried?

Depending on which flags you compile it with, libhandlebars.so does depend on several other shared libraries (libyaml, json-c, talloc - use readelf -d /path/to/libhandlebars.so to see which are required) in the environment.

Assuming you want the full execution pipeline, you can see my other project here for implementing this in PHP, although it's a bit more in-depth than the bare-minimum required functionality:
https://github.com/jbboehr/php-handlebars

If the data is JSON-like and can be converted to the types internally used by handlebars.c, the implementation can be fairly simple:

static int do_execute(void)
{
struct handlebars_context * ctx;
struct handlebars_parser * parser;
struct handlebars_compiler * compiler;
struct handlebars_string * tmpl;
struct handlebars_ast_node * ast;
struct handlebars_program * program;
HANDLEBARS_VALUE_DECL(partials);
jmp_buf jmp;
ctx = handlebars_context_ctor_ex(root);
// Save jump buffer
if( handlebars_setjmp_ex(ctx, &jmp) ) {
fprintf(stderr, "ERROR: %s\n", handlebars_error_message(ctx));
handlebars_context_dtor(ctx);
return 1;
}
parser = handlebars_parser_ctor(ctx);
compiler = handlebars_compiler_ctor(ctx);
if (enable_partial_loader) {
struct handlebars_string *partial_path_str = NULL;
struct handlebars_string *partial_extension_str = NULL;
partial_path_str = handlebars_string_ctor(ctx, partial_path, strlen(partial_path));
partial_extension_str = handlebars_string_ctor(ctx, partial_extension, strlen(partial_extension));
(void) handlebars_value_partial_loader_init(ctx, partial_path_str, partial_extension_str, partials);
}
handlebars_compiler_set_flags(compiler, compiler_flags);
// Read
readInput();
tmpl = handlebars_string_ctor(HBSCTX(parser), input_buf, strlen(input_buf));
// Preprocess
if( compiler_flags & handlebars_compiler_flag_compat ) {
tmpl = handlebars_preprocess_delimiters(ctx, tmpl, NULL, NULL);
}
// Read context
HANDLEBARS_VALUE_DECL(input);
if( input_data_name ) {
size_t input_data_name_len = strlen(input_data_name);
char * input_str = file_get_contents(input_data_name);
if (input_str && strlen(input_str)) {
if (handlebars_value_is_empty(input) && input_data_name_len > 5 && (0 == strcmp(input_data_name + input_data_name_len - 5, ".yaml") ||
0 == strcmp(input_data_name + input_data_name_len - 4, ".yml"))) {
#ifdef HANDLEBARS_HAVE_YAML
handlebars_value_init_yaml_string(ctx, input, input_str);
#else
fprintf(stderr, "Failed to process input data: YAML support is disabled");
exit(1);
#endif
}
if (handlebars_value_is_empty(input)) {
#ifdef HANDLEBARS_HAVE_JSON
// assume json
handlebars_value_init_json_string(ctx, input, input_str);
if (convert_input) {
handlebars_value_convert(input);
}
#else
fprintf(stderr, "Failed to process input data: JSON support is disabled");
exit(1);
#endif
}
}
}
// Parse
ast = handlebars_parse_ex(parser, tmpl, compiler_flags);
// Compile
program = handlebars_compiler_compile_ex(compiler, ast);
// Serialize
struct handlebars_module * module = handlebars_program_serialize(ctx, program);
// Execute
struct handlebars_string * buffer = NULL;
do {
if (buffer) {
handlebars_talloc_free(buffer);
buffer = NULL;
}
struct handlebars_vm * vm;
vm = handlebars_vm_ctor(ctx);
handlebars_vm_set_flags(vm, compiler_flags);
handlebars_vm_set_partials(vm, partials);
buffer = handlebars_vm_execute(vm, module, input);
buffer = talloc_steal(ctx, buffer);
handlebars_vm_dtor(vm);
} while(--run_count > 0);
if (buffer) {
fwrite(hbs_str_val(buffer), sizeof(char), hbs_str_len(buffer), stdout);
}
if (newline_at_eof) {
fwrite("\n", sizeof(char), 1, stdout);
}
HANDLEBARS_VALUE_UNDECL(input);
HANDLEBARS_VALUE_UNDECL(partials);
handlebars_context_dtor(ctx);
return 0;
}

The data would have to be converted to a JSON string first, or the equivalent of handlebars_value_init_json_string for python types would have to be implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants