<commit_before>import async_http_request
export async_http_request

when not defined(js):
    import sdl2, sdl_perform_on_main_thread, marshal, streams

    proc storeToSharedBuffer*[T](a: T): pointer =
        let s = newStringStream()
        store(s, a)
        result = allocShared(s.data.len + sizeof(uint64))
        cast[ptr uint64](result)[] = s.data.len.uint64
        copyMem(cast[pointer](cast[int](result) + sizeof(uint64)), addr s.data[0], s.data.len)
        s.close()

    proc readFromSharedBuffer*[T](p: pointer, res: var T) =
        let bufLen = cast[ptr uint64](p)[]
        var str = newStringOfCap(bufLen)
        str.setLen(bufLen)
        copyMem(addr str[0], cast[pointer](cast[int](p) + sizeof(uint64)), bufLen)
        let s = newStringStream(str)
        load(s, res)
        s.close()

    proc sendRequest*(meth, url, body: string, headers: openarray[(string, string)], handler: Handler) =
        type SdlHandlerContext = object
            handler: Handler
            data: pointer

        let ctx = cast[ptr SdlHandlerContext](allocShared(sizeof(SdlHandlerContext)))
        ctx.handler = handler

        proc callHandler(c: pointer) {.cdecl.} =
            let ctx = cast[ptr SdlHandlerContext](c)
            var r: Response
            readFromSharedBuffer(ctx.data, r)
            deallocShared(ctx.data)
            ctx.handler(r)
            deallocShared(ctx)

        proc sdlThreadSafeHandler(r: Response) =
            ctx.data = storeToSharedBuffer(r)
            performOnMainThread(callHandler, ctx)

        sendRequestThreaded(meth, url, body, headers, sdlThreadSafeHandler)<commit_msg>Thread safety in http_request<commit_after>import async_http_request
export async_http_request

when not defined(js):
    import sdl2, sdl_perform_on_main_thread, marshal, streams

    proc storeToSharedBuffer*[T](a: T): pointer =
        let s = newStringStream()
        store(s, a)
        result = allocShared(s.data.len + sizeof(uint64))
        cast[ptr uint64](result)[] = s.data.len.uint64
        copyMem(cast[pointer](cast[int](result) + sizeof(uint64)), addr s.data[0], s.data.len)
        s.close()

    proc readFromSharedBuffer*[T](p: pointer, res: var T) =
        let bufLen = cast[ptr uint64](p)[]
        var str = newStringOfCap(bufLen)
        str.setLen(bufLen)
        copyMem(addr str[0], cast[pointer](cast[int](p) + sizeof(uint64)), bufLen)
        let s = newStringStream(str)
        load(s, res)
        s.close()

    proc sendRequest*(meth, url, body: string, headers: openarray[(string, string)], handler: Handler) =
        type SdlHandlerContext = object
            handler: Handler
            data: pointer

        let ctx = cast[ptr SdlHandlerContext](allocShared(sizeof(SdlHandlerContext)))
        ctx.handler = handler

        proc callHandler(c: pointer) {.cdecl.} =
            let ctx = cast[ptr SdlHandlerContext](c)
            var r: Response
            readFromSharedBuffer(ctx.data, r)
            deallocShared(ctx.data)
            ctx.handler(r)
            deallocShared(ctx)

        proc sdlThreadSafeHandler(r: Response, ctx: pointer) {.nimcall.} =
            cast[ptr SdlHandlerContext](ctx).data = storeToSharedBuffer(r)
            performOnMainThread(callHandler, ctx)

        sendRequestThreaded(meth, url, body, headers, sdlThreadSafeHandler, ctx)
{'fraction_non_alphanumeric': 0.11373134328358209, 'fraction_numerical': 0.008955223880597015, 'mean_word_length': 2.7525195968645018, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2933021', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1002, 'n_words': 225}