Skip to content

Add the option to 'return' the bound argument via pass-by-reference.#867

Open
dandeto wants to merge 2 commits into
webview:masterfrom
dandeto:unbind-return2
Open

Add the option to 'return' the bound argument via pass-by-reference.#867
dandeto wants to merge 2 commits into
webview:masterfrom
dandeto:unbind-return2

Conversation

@dandeto
Copy link
Copy Markdown
Collaborator

@dandeto dandeto commented Dec 2, 2022

This will resolve #730 and also maintains backwards-compatibility.

void webview::unbind(const std::string &name) works as before.
void webview::unbind(const std::string &name, void *&arg) implements the new desired behavior of "returning" the bound argument via pass-by-reference.
void webview_unbind_return(webview_t w, const char *name, void *&arg); is the new C function. I had to change the name since overloading does not exist in C.

Unbind without returning the arg:

context_t context = {.w = w, .count = 0};
webview_bind(w, "increment", increment, &context);
...
webview_unbind(w, "increment");

Unbind, use the arg, and free:

context_t *context = (context_t*) malloc (sizeof(context_t));
context->w = w;
context->count = 0;
webview_bind(w, "increment", increment, context);
...
void* ptr;
webview_unbind_return(w, "increment", ptr);
context_t *context2 = (context_t*) ptr;
printf("Count: %d\n", context2->count)
free(ptr);

The correct way to do this in C++ would be to use a template like so: template<typename T> void unbind(const std::string &name, T *& arg). Unfortunately, I could not figure out how to get C to call a C++ template function. Maybe you guys know more about that. This continues the pattern of using void* to accept an argument of any type, but using void*& is a little unsafe.

@dandeto dandeto mentioned this pull request Dec 2, 2022
@dandeto
Copy link
Copy Markdown
Collaborator Author

dandeto commented Dec 2, 2022

Built just fine on my machine. I'll look into it later.

@SteffenL
Copy link
Copy Markdown
Collaborator

SteffenL commented Dec 3, 2022

You'll need void **arg in the C API.

Since this PR adds a new function, why not just add a get_bound_arg function instead?

@dandeto
Copy link
Copy Markdown
Collaborator Author

dandeto commented Dec 4, 2022

C++ could default the void *& arg = nullptr, but that didn't work when I added C99 compatibility, so yeah I might as well add get_bound_arg instead. Thank you for the tip about the C api, my C++ is much stronger than C :/

@SteffenL SteffenL added the type: feature Pull request is a feature label Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature Pull request is a feature

Development

Successfully merging this pull request may close these issues.

Proposal: have webview_unbind return the arg

2 participants