From 10c67474a89c298172a6703b91980ea37c60d5e5 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Thu, 10 Dec 2020 07:19:41 -0800 Subject: More callback refactoring (#713) This change: * Gets rid of the `.toC()` functions for Options objects, since they were redundant with the `populateXxxOptions()`. * Adds support for `errorTarget` to the `RemoteOptions`, since they are used in the same stack for some functions (like `Fetch()`). Now for those cases, the error returned by the callback will be preserved as-is. --- wrapper.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'wrapper.c') diff --git a/wrapper.c b/wrapper.c index f168425..282d9a8 100644 --- a/wrapper.c +++ b/wrapper.c @@ -14,7 +14,7 @@ // // // myfile.go // type FooCallback func(...) (..., error) -// type FooCallbackData struct { +// type fooCallbackData struct { // callback FooCallback // errorTarget *error // } @@ -60,20 +60,28 @@ // return git_my_function(..., (git_foo_cb)&fooCallback, payload); // } // -// * Otherwise, if the same callback can be invoked from multiple functions or -// from different stacks (e.g. when passing the callback to an object), a -// different pattern should be used instead, which has the downside of losing -// the original error object and converting it to a GitError: +// * Additionally, if the same callback can be invoked from multiple functions or +// from different stacks (e.g. when passing the callback to an object), the +// following pattern should be used in tandem, which has the downside of +// losing the original error object and converting it to a GitError if the +// callback happens from a different stack: // // // myfile.go // type FooCallback func(...) (..., error) +// type fooCallbackData struct { +// callback FooCallback +// errorTarget *error +// } // // //export fooCallback // func fooCallback(errorMessage **C.char, ..., handle unsafe.Pointer) C.int { -// callback := pointerHandles.Get(data).(*FooCallback) +// data := pointerHandles.Get(data).(*fooCallbackData) // ... -// err := callback(...) +// err := data.callback(...) // if err != nil { +// if data.errorTarget != nil { +// *data.errorTarget = err +// } // return setCallbackError(errorMessage, err) // } // return C.int(ErrorCodeOK) -- cgit v1.2.3