quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

commit f7b6b6bbafd589774e7f439c12e92415db0cae50
parent b96f6613c3848e0b0dc392153db7c8ca73d9ac6d
Author: Iván Ávalos <avalos@disroot.org>
Date:   Fri, 24 May 2024 12:06:20 -0600

make os.fetchHttp return cancellation function

Diffstat:
Mquickjs/quickjs-libc.c | 41+++++++++++++++++++++++++++++++++++++----
1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/quickjs/quickjs-libc.c b/quickjs/quickjs-libc.c @@ -22,6 +22,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ +#include "quickjs.h" #include <stdlib.h> #include <stdio.h> #include <stdarg.h> @@ -2286,6 +2287,21 @@ static void handle_http_resp(void *cls, struct JSHttpResponseInfo *resp_info) return; } +static JSValue cancel_http_req(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) +{ + JSRuntime *rt = JS_GetRuntime(ctx); + JSThreadState *ts = JS_GetRuntimeOpaque(rt); + int req_id; + int ret; + + JS_ToInt32(ctx, &req_id, argv[0]); + + // cancel HTTP request + ret = ts->http_client_impl->req_cancel(ts->http_client_impl->cls, req_id); + + return JS_NewInt32(ctx, ret); +} + static void free_http_headers(JSContext *ctx, char **headers) { @@ -2345,7 +2361,11 @@ exception: /** - * fetchHttp(url, { method, headers, body }): Promise<Response> + * fetchHttp(url, { method, headers, body }): { + * requestId: number, + * response: Promise<Response>, + * cancelFn: () => void, + * } */ static JSValue js_os_fetchHttp(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) @@ -2470,13 +2490,26 @@ static JSValue js_os_fetchHttp(JSContext *ctx, JSValueConst this_val, list_add_tail(&req_context->link, &ts->http_requests); - ret_val = JS_NewPromiseCapability(ctx, resolving_funs); - if (JS_IsException(ret_val)) { - goto done; + // requestId: number + JSValue requestId = JS_NewInt32(ctx, ret); + + // promise: Promise<Response> + JSValue promise = JS_NewPromiseCapability(ctx, resolving_funs); + if (JS_IsException(promise)) { + goto exception; } req_context->request_id = ret; req_context->resolve_func = resolving_funs[0]; req_context->reject_func = resolving_funs[1]; + + // cancelFn: () => void + JSValue cancelFn = JS_NewCFunction(ctx, &cancel_http_req, "cancelFn", 1); + + ret_val = JS_NewObject(ctx); + JS_SetPropertyStr(ctx, ret_val, "requestId", requestId); + JS_SetPropertyStr(ctx, ret_val, "promise", promise); + JS_SetPropertyStr(ctx, ret_val, "cancelFn", cancelFn); + done: free_http_headers(ctx, req.request_headers); JS_FreeValue(ctx, method);