Skip to content

Commit

Permalink
tags started to be called, but my_printf still doesn't work
Browse files Browse the repository at this point in the history
  • Loading branch information
aodinokov committed Sep 15, 2024
1 parent 326ea67 commit 9ab3aa8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name": "(lldb) Launch - Macos",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/value_ffi_test",
"program": "${fileDirname}/c_print_args",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
Expand Down
35 changes: 19 additions & 16 deletions examples/c_print_args_alt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ double test_function3_with_args(list_t *p_list) {
}
METAC_GSYM_LINK(test_function3_with_args);

// int my_printf(const char * format, ...) {
// va_list l;
// va_start(l, format);
// int res = vprintf(format, l);
// va_end(l);
// return res;
// }
// METAC_GSYM_LINK(my_printf);
int my_printf(const char * format, ...) {
va_list l;
va_start(l, format);
int res = vprintf(format, l);
va_end(l);
return res;
}
METAC_GSYM_LINK(my_printf);


metac_tag_map_t * p_tagmap = NULL;
Expand All @@ -45,14 +45,14 @@ METAC_TAG_MAP_NEW(va_args_tag_map, NULL, {.mask =
METAC_TAG_MAP_ENTRY_CATEGORY_MASK(METAC_TEC_final),},)
/* start tags for all types */

// METAC_TAG_MAP_ENTRY(METAC_GSYM_LINK_ENTRY(my_printf))
// METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.n = "format"}),
// METAC_ZERO_ENDED_STRING()
// )
// METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.i = 1}),
// METAC_FORMAT_BASED_VA_ARG()
// )
// METAC_TAG_MAP_ENTRY_END
METAC_TAG_MAP_ENTRY(METAC_GSYM_LINK_ENTRY(my_printf))
METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.n = "format"}),
METAC_ZERO_ENDED_STRING()
)
METAC_TAG_MAP_SET_TAG(0, METAC_TEO_entry, 0, METAC_TAG_MAP_ENTRY_PARAMETER({.i = 1}),
METAC_FORMAT_BASED_VA_ARG()
)
METAC_TAG_MAP_ENTRY_END

METAC_TAG_MAP_END
//
Expand All @@ -65,6 +65,9 @@ int main() {
list_t * p_list = (list_t[]){{.x = 42.42, .p_next = (list_t[]){{ .x = 45.4, .p_next = NULL}}}};
printf("fn returned: %f\n", METAC_WRAP_FN_RES(NULL, test_function3_with_args, p_list));

// doesn't work yet
//printf("fn returned: %i\n", METAC_WRAP_FN_RES(p_tagmap, my_printf, "%d %d", 10, 22));

metac_tag_map_delete(p_tagmap);
return 0;
}
94 changes: 56 additions & 38 deletions examples/c_print_args_alt/print_args.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,52 @@

#include "metac/reflect.h"
#include "metac/backend/iterator.h"
#include "metac/backend/value.h"
#include "mr.h"

// // this gets called in the context of _WRAP and has all of the
// #define _APPEND_PARAM(_NEXT_, _N_, args...) do { \
// WITH_METAC_DECLLOC(decl, typeof(MR_FIRST(args)) _x_val = MR_FIRST(args)); \
// metac_entry_t *p_param_entry = metac_entry_by_paremeter_id(p_val_entry, param_id); \
// if (metac_entry_is_unspecified_parameter(p_param_entry) != 0 || metac_entry_is_va_list_parameter(p_param_entry) != 0) { \
// metac_recursive_iterator_t * p_iter = metac_new_recursive_iterator(p_val); \
// metac_value_t * p = (metac_value_t *)metac_recursive_iterator_next(p_iter); /* value itself */ \
// metac_entry_tag_t * p_tag = metac_tag_map_tag(p_tag_map, p_param_entry); \
// if (p_tag != NULL && p_tag->handler) { \
// metac_value_event_t ev = { \
// .type = METAC_RQVST_va_list, \
// .va_list_param_id = param_id, /* TODO: to remove?*/ \
// .p_return_value = metac_parameter_storage_new_param_value(p_param_storage, param_id), \
// .p_va_list_container = NULL/*p_va_list_container TODO: we'll need another approah*/, \
// }; \
// if (ev.p_return_value == NULL) { \
// metac_recursive_iterator_fail(p_iter); \
// } else { \
// metac_recursive_iterator_done(p_iter, NULL); \
// } \
// } \
// } else { \
// metac_entry_t *p_entry = METAC_ENTRY_FROM_DECLLOC(decl, _x_val); \
// /* TODO: compare types, at least sizes*/ \
// if (metac_parameter_storage_append_by_buffer(p_param_storage, p_param_entry, sizeof(_x_val)) == 0) { \
// metac_value_t * p_param_value = metac_parameter_storage_new_param_value(p_param_storage, param_id); \
// if (p_param_value != NULL) { \
// memcpy(metac_value_addr(p_param_value), &_x_val, sizeof(_x_val)); \
// metac_value_delete(p_param_value); \
// } \
// } \
// } \
// ++param_id; \
// }while(0); \
// _NEXT_
static int _process_unspecified_params(
//context
metac_parameter_storage_t * p_param_storage,
metac_value_t * p_val,
metac_entry_t *p_param_entry,
metac_tag_map_t * p_tag_map,
metac_num_t param_id,
// and all params
int n, ...) {

metac_recursive_iterator_t * p_iter = metac_new_recursive_iterator(p_val);

struct va_list_container cntr = {};
va_start(cntr.parameters, p_tag_map);

metac_value_t * p = (metac_value_t *)metac_recursive_iterator_next(p_iter); /* value itself */
metac_entry_tag_t * p_tag = metac_tag_map_tag(p_tag_map, p_param_entry);
if (p_tag != NULL && p_tag->handler) {
metac_value_event_t ev = {
.type = METAC_RQVST_va_list,
.va_list_param_id = param_id, /* TODO: to remove?*/
.p_return_value = metac_parameter_storage_new_param_value(p_param_storage, param_id),
.p_va_list_container = &cntr,
};
if (ev.p_return_value == NULL) {
metac_recursive_iterator_fail(p_iter);
} else {
if (metac_value_event_handler_call(p_tag->handler, p_iter, &ev, p_tag->p_context) != 0) {
}
metac_value_delete(ev.p_return_value);
metac_recursive_iterator_done(p_iter, NULL);
}
} else {
metac_recursive_iterator_fail(p_iter);
}

va_end(cntr.parameters);

int failed = 0;
metac_recursive_iterator_get_out(p_iter, NULL, &failed);
metac_recursive_iterator_free(p_iter);
return failed;
}

#define _APPEND_PARAM_2(_NEXT_, _N_, args...) if (failure == 0) { \
metac_entry_t *p_param_entry = metac_entry_by_paremeter_id(p_val_entry, param_id); \
Expand All @@ -49,7 +58,7 @@
metac_entry_t *p_param_entry = metac_entry_by_paremeter_id(p_val_entry, param_id); \
metac_size_t param_entry_byte_size = 0; \
if (metac_entry_byte_size(p_param_entry, &param_entry_byte_size) != 0) { \
printf("param %d metac_entry_base_type_byte_size failed\n ", param_id); \
printf("param %d metac_entry_byte_size failed\n ", param_id); \
failure = 1; \
break; \
} \
Expand All @@ -64,8 +73,17 @@
metac_value_delete(p_param_value); \
} \
} \
} else if (metac_entry_is_va_list_parameter(p_param_entry) == 0) { \
} else if (metac_entry_is_unspecified_parameter(p_param_entry) == 0) { \
} else if (metac_entry_is_va_list_parameter(p_param_entry) != 0) { \
} else if (metac_entry_is_unspecified_parameter(p_param_entry) != 0) { \
if (metac_parameter_storage_append_by_parameter_storage(p_param_storage, p_param_entry) != 0) { \
failure = 5; \
break; \
} \
if (_process_unspecified_params(p_param_storage, p_val, p_param_entry, p_tag_map, param_id, _N_, args) != 0) { \
failure = 4; \
break; \
} \
break; \
} else { \
failure = 3; \
break; \
Expand Down

0 comments on commit 9ab3aa8

Please sign in to comment.