Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snippets that make nimlsp crash #129

Open
ghost opened this issue May 6, 2022 · 0 comments
Open

snippets that make nimlsp crash #129

ghost opened this issue May 6, 2022 · 0 comments

Comments

@ghost
Copy link

ghost commented May 6, 2022

I assume nimlsp (0.4.0) crashes because of nimsuggest (1.6.6) (or so i am told)
If you comment the "OFFENDING LINE" it should stop crashing
1:

var out:

2:

jobs[] = init[int, pointer]()

3:

{.experimental: "caseStmtMacros".}

let capts = (page: "",)
case capts:
    of (page: ""):
        discard
    else: discard

4:

import httpcore,
       fusion/matching,
       guildenstern/[ctxheader, ctxbody]

{.experimental: "caseStmtMacros".}

template handleSearch(relpath: string, ctx: HttpCtx) =
    var headers: array[1, string]
    ctx.parseHeaders(["abc"], headers) # OFFENDING LINE

proc handleGet(ctx: HttpCtx) {.gcsafe, raises: [].} =
    doassert ctx.parseRequestLine
    var
        relpath = ctx.getUri()
        page: string
    try:
        let capts = (page: "")
        case capts:
            of (page: "s"):
                handleSearch(relpath, ctx)
            else:
                discard
    except:
        try:
            let msg = getCurrentExceptionMsg()
        except:
            ctx.reply(Http501)
            discard
        discard

when isMainModule:

    var server = new GuildenServer
    server.initHeaderCtx(handleGet, 5050, false)
    echo "GuildenStern HTTP server serving at 5050"
    server.serve(loglevel = INFO)

5:

import
    tables,
    locks

export tables,
       locks

proc put*[T, K, V](t: T, k: K, v: V): V = (t[k] = v; v)

type PathLock* = Table[string, Lock]
var locksBuffer*: seq[ref Lock]

proc get*(b: var seq[ref Lock]): ref Lock =
    try:
        return b.pop()
    except:
        discard

proc acquireOrWait*(pl: PathLock, k: string): bool =
    try:
        withLock(pl[k][]):
            discard
        result = false
    except KeyError:
        pl.put(k, locksBuffer.get)[].acquire() # OFFENDING LINE
        result = true

6:

import macros,
       locks

template lockedStore*(name: untyped): untyped {.dirty.} =
    type
        `Lock name Obj`[K, V] = object
            lock: Lock
            storage {.guard: lock.}: name[K, V]
        `Lock name`*[K, V] = ptr `Lock name Obj`[K, V]

    proc `lock name Impl`*[K, V](store: name[K, V]): `Lock name`[K, V] =
        result = createShared(`Lock name Obj`[K, V])
        initLock(result.lock)
        withLock(result.lock):
            result.storage = store

    template `init Lock name`*[K, V](args: varargs[untyped]): `Lock name`[K, V] =
        var store: name[K, V]
        store = when compiles(`init name`):
                    when varargsLen(args) > 0:
                        `init name`[K, V](args)
                    else:
                        `init name`[K, V]()
                elif compiles(`new name`):
                    when varargsLen(args) > 0:
                        `new name`[K, V](args)
                    else:
                        `new name`[K, V]()
                else:
                    `name`[K, V]()
        `lock name Impl`[K, V](store)

    proc `[]=`*[K, V](tbl: `Lock name`[K, V], k: K, v: V) =
        withLock(tbl.lock):
            tbl.storage[k] = v

when isMainModule:
    import tables
    lockedStore(Table)
    let x = initLockTable[string, string](100)
    x["a"] = "123" # OFFENDING LINE

7:

when is
    echo s

8:

template abc[T](s, keys: string): auto =
    var r: # OFFENDING LINE (also `var r = `)
    abc[T](s, r)

9:

import
       httpcore,
       tables,
       guildenstern/[ctxheader, ctxbody],
       uri

var pageCache  = initTable[string, string]()

proc fetch(): string =
    "abc"

template lgetOrPut*[T, K](c: T, k: K, v: untyped): untyped =
    ## Lazy `mgetOrPut`
    try:
        c.get(k)
    except KeyError:
        c.put(k, v)

template handlePath(relpath: string, ctx: HttpCtx) =
    page = pageCache.lgetOrPut(reqFile):
        fetch.c # OFFENDING LINE, if it doesn't crash, delete and type ".c" again


proc handleGet(ctx: HttpCtx) {.gcsafe, raises: [].} =
    doassert ctx.parseRequestLine
    var
        relpath = ctx.getUri()
        page: string
        headers: seq[string]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants